LightOJ-1197 Help Hanzo Posted on 2019-03-22 | Edited on 2019-10-04 | In lightoj | Views: 题意求 $a\sim b$ 间素数个数 $(1 \le a \le b < 2^{31}, b - a \le 1e5)$ 题解$b-a$ 这个区间比较小,所以可以用区间素数筛选的办法解决这个题目。 代码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#include <bits/stdc++.h>#define ull unsigned long long#define ll long long#define inf 0x3f3f3f3f#define mod (int)1e9+7#define for0(i, n) for (int i = 0; i < n; i++)#define for1(i, n) for (int i = 1; i <= n; i++)#define forl(i, l, r) for (int i = l; i <= r; i++)#define forn(i, n) for (int i = n; i >= 0; i--)#define mem0(a) memset(a, 0, sizeof(a))#define mem1(a) memset(a, 1 , sizeof(a))#define mem_1(a) memset(a, -1, sizeof(a))#define meminf(a) memset(a, inf, sizeof(a))using namespace std;const int N=100005;bool a[N+1];vector<int> prime;void getPrime(){ memset(a,true,sizeof(a)); for(int i=2;i<=N;i++){ if(a[i]) prime.push_back(i); for(int j=0;j<(int)prime.size()&&i*prime[j]<=N;j++){ a[i*prime[j]]=false; if(!(i%prime[j])) break; } }}int main(){ ll t,a,b; bool isprime[100005]; getPrime(); cin>>t; for1(k,t){ int ans=0; cin>>a>>b; mem1(isprime); for(int i=0;prime[i]*prime[i]<=b&&i<(int)prime.size();i++){ ll l=a/prime[i]; if (l*prime[i]<a) l++; if (l<2) l=2; for (; l*prime[i]<=b; l++) isprime[l*prime[i]-a]=0; } if (a==1) isprime[0]=0; for0(i,b-a+1) if (isprime[i])ans++; printf("Case %d: %d\n",k,ans ); } return 0;}