模板题,不过好像有点问题,当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-7; const int N=100000+10,maxn=500+100,inf=0x3f3f3f; bool prime[N],primesmall[N]; ll seg_prime(ll a,ll b) { memset(prime,0,sizeof prime); for(ll i=0;i*i<b;i++)primesmall[i]=1; for(ll i=0;i<=b-a;i++)prime[i]=1; for(ll i=2;i*i<b;i++) { if(primesmall[i]) { for(ll j=2*i;j*j<b;j+=i)primesmall[j]=0; for(ll j=max((ll)2,(a+i-1)/i)*i;j<=b;j+=i)prime[j-a]=0; } } ll ans=0; for(ll i=0;i<=b-a;i++) if(prime[i]) ans++; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll t,cnt=0; cin>>t; while(t--){ ll a,b; cin>>a>>b; if(a==1)cout<<"Case "<<++cnt<<": "<<seg_prime(a,b)-1<<endl; else cout<<"Case "<<++cnt<<": "<<seg_prime(a,b)<<endl; } return 0; } /********************* 9 2 36 3 73 3 11 1 1 1 2 2147383647 2147483647 23990500 24000500 24000501 24100501 23999500 24010501 *********************/