线筛

  洛谷P3383:https://www.luogu.org/problemnew/show/P3383

  

  此处用的是 真正 的欧拉筛,至于什么普通的筛法一律不考虑,自己打去吧(敲减单的)。

  其实也没有什么可讲的,只是数学题上用的贼多~

var
        i,m,n,tot,j:longint;
        v,zhi:array[0..10000001] of longint;
begin
        v[1]:=1;
        readln(n,m);
        for i:=2 to n do
        begin
                if v[i]=0 then
                begin
                        inc(tot);
                        zhi[tot]:=i;
                end;
                j:=1;
                while (j<=tot) and (zhi[j]*i<=n) do
                begin
                        v[zhi[j]*i]:=1;
                        if i mod zhi[j]=0 then break;
                        inc(j);
                end;
        end;
        for i:=1 to m do
        begin
                readln(tot);
                if v[tot]=0 then writeln('Yes') else writeln('No');
        end;
end.

猜你喜欢

转载自www.cnblogs.com/t-s-y/p/9746001.html