Help Hanzo
Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 231, b - a ≤ 100000).
Output
For each case, print the case number and the number of safe territories.
Sample Input
3
2 36
3 73
3 11
Sample Output
Case 1: 11
Case 2: 20
Case 3: 4
解题思路:
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) print f("%d\n", n)
#define pc(n) print f("%c", n)
#define pdd(n,m) print f("%d %d", n, m)
#define pld(n) print f("%int d\n", n)
#define pldd(n,m) print f("%int d %int d\n", n, m)
#define sld(n) scanf("%int d",&n)
#define sldd(n,m) scanf("%int d%int d",&n,&m)
#define slddd(n,m,k) scanf("%int d%int d%int d",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define aint (x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
#define pii map<int ,int >
#define mk make_pair
#define rtl rt<<1
#define rtr rt<<1|1
#define Max(x,y) (x)>(y)?(x):(y)
#define int long long
typedef pair<int ,int > PII;
typedef long long ll;
typedef unsigned long long ull ;
typedef long double ld;
const int MOD = 1e9 + 7;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int INF = 0x3f3f3f3f3f3f3f3f;
//const int inf = 0x3f3f3f3f;
inline int read(){int ret = 0, sgn = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')sgn = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){ret = ret*10 + ch - '0';ch = getchar();}
return ret*sgn;}
inline void Out(int a){if(a>9) Out(a/10);putchar(a%10+'0');}
int qmul(int a,int b,int mod){int res=0;while(b){if(b&1)res=(res+a)%mod;a=(a+a)%mod;b>>=1;}return res;}
int qpow(int m,int k,int mod){int res=1%mod,t=m%mod;while(k){if(k&1)res=qmul(res,t,mod);t=qmul(t,t,mod);k>>=1;}return res;}
int gcd(int a,int b){if(b > a) swap(a,b); return b==0?a : gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
int inv(int x,int mod){return qpow(x,mod-2,mod)%mod;}
//const int N = 3e3+15;
int t = 1,cas = 1;
int n,m;
const int N = 1e5+15;
int prime[700000],pcnt;
bool mark[N];
// 如果变量名都相同的话,就不用传参了
//void getPrimes(int prime[],int N,int &pcnt)
void getPrimes()
{
memset(mark,0,sizeof(mark));
mark[0] = mark[1] = 1;
pcnt = 0;
for(int i = 2; i < N ; i ++)
{
if(!mark[i])
prime[pcnt++] = i;
for(int j = 0 ; i*prime[j] < N && j < pcnt ; j ++)
{
mark[i*prime[j]] = 1;
if(i%prime[j] == 0)
break;
}
}
}
int prime2[220000];
bool flag[200100];
int p2cnt;
// pcnt 和 prime都是 getPrime产生的
void getPrimes2(int L,int R)
{
if(L <= 1) L = 2;
memset(flag, false, sizeof flag);
for(int i=0; i < pcnt && prime[i]<R; i++){
for(int j=(L+0ll+prime[i]-1)/prime[i]*prime[i]; j<=R; j+=prime[i]){
if(j!=prime[i]) flag[j-L]=true;
}
}
p2cnt=0;
for(int i=0;i<=R-L;i++){
if(!flag[i]) prime2[p2cnt++]=L+i;
}
}
signed main(){
getPrimes();
int t,k;
scanf("%lld",&t);
while(t--){
int l,r;
scanf("%lld%lld",&l,&r);
getPrimes2(l,r);
cout<<"Case "<<cas++<<": "<<p2cnt<<endl;
}
return 0;
}