lightoj1197区间素数筛

时间:2021-07-24 13:25:36

模板题,不过好像有点问题,当a==1的时候,答案把一也算进去了,要减去

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; bool prime[N],primesmall[N];
ll seg_prime(ll a,ll b)
{
memset(prime,,sizeof prime);
for(ll i=;i*i<b;i++)primesmall[i]=;
for(ll i=;i<=b-a;i++)prime[i]=;
for(ll i=;i*i<b;i++)
{
if(primesmall[i])
{
for(ll j=*i;j*j<b;j+=i)primesmall[j]=;
for(ll j=max((ll),(a+i-)/i)*i;j<=b;j+=i)prime[j-a]=;
}
}
ll ans=;
for(ll i=;i<=b-a;i++)
if(prime[i])
ans++;
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
ll t,cnt=;
cin>>t;
while(t--){
ll a,b;
cin>>a>>b;
if(a==)cout<<"Case "<<++cnt<<": "<<seg_prime(a,b)-<<endl;
else cout<<"Case "<<++cnt<<": "<<seg_prime(a,b)<<endl;
}
return ;
}
/*********************
9
2 36
3 73
3 11
1 1
1 2
2147383647 2147483647
23990500 24000500
24000501 24100501
23999500 24010501
*********************/