Help Hanzo LightOJ - 1197
区间筛质数模板题
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2021.1.29 using namespace std; long long T,l,r,n,t,k,cnt,ans,temp; long long prime[N]; bool vis[N]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } void Euler(){ vis[1]=1; For(i,2,1e6){ if(!vis[i]) prime[++cnt]=i; for(long long j=1;j<=cnt&&i*prime[j]<=1e6;j++){ vis[i*prime[j]]=1; if(i%prime[j]==0) break; } } } signed main(){ in(T); Euler(); while(T--){ in(l);in(r);t=sqrt(r); For(i,0,r-l) vis[i]=0; For(i,1,cnt){ if(prime[i]>t) break; k=max((long long)2,l/prime[i]+(l%prime[i]!=0)); For(j,k,inf){ if(j*prime[i]>r) break; vis[j*prime[i]-l]=1; } } ans=0; For(i,0,r-l) ans+=(vis[i]==0); if(l==1) ans--; printf("Case %lld: %lld\n",++temp,ans); } return 0; }