A%B Problem
题目描述
区间质数个数
输入输出格式
输入格式:
一行两个整数 询问次数n,范围m
接下来n行,每行两个整数 l,r 表示区间
输出格式:
对于每次询问输出个数 t,如l或r∉[1,m]输出 Crossing the line
输入输出样例
2 5 1 3 2 6
2 Crossing the line
说明
【数据范围和约定】
对于20%的数据 1<=n<=10 1<=m<=10
对于100%的数据 1<=n<=1000 1<=m<=1000000 -10^9<=l<=r<=10^9 1<=t<=1000000
分析:用筛选法筛一下然后求出s[i](1~i中有多少素数),输出时减一下就行了。
代码
const
maxn=1000000;
var
a:array[0..maxn] of boolean;
s:array[0..maxn] of longint;
n,m,x,y,i,j,ans:longint;
begin
readln(n,m);
a[1]:=true;
for i:=2 to trunc(sqrt(m)) do
for j:=2 to m div i do
a[i*j]:=true;
for i:=1 to m do
if not a[i] then s[i]:=s[i-1]+1 else s[i]:=s[i-1];
for i:=1 to n do
begin
readln(x,y);
if (y>m) or (x<1) then
begin
writeln('Crossing the line');
continue;
end
else writeln(s[y]-s[x-1]);
end;
end.