 E - Help Hanzo（LightOJ 1197）

0
0
0 1. 云栖社区>
2. 博客>
3. 正文

E - Help Hanzo（LightOJ 1197）

angel_imp 2016-03-17 21:02:00 浏览1639

Pssword： nefu

Description

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.

Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.

He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 1e6+5;
const int mod = 1000000007;
const double eps = 1e-7;
bool prime[MAXN];
LL p[MAXN];
LL k;
void isprime()///素数筛
{
k = 0;
prime = false;
memset(prime, true, sizeof(prime));
for(LL i=2; i<MAXN; i++)
{
if(prime[i])
{
p[k++] = i;
for(LL j=i*i; j<MAXN; j+=i)
prime[j] = false;
}
}
}
LL a, b, tmp;
bool ok[MAXN];
void ShaiXuan()
{
memset(ok, true, sizeof(ok));
tmp = b - a;
for(LL i=0; p[i]*p[i]<=b&&i<k; i++)
{
LL tt = 0;
if(a%p[i])///第一个筛掉的数是(tt+a)%p[i] == 0
tt = p[i] - a%p[i];
if(a <= p[i])///防止是素数
tt += p[i];
for(; tt<=tmp; tt+=p[i])///筛掉p[i]的倍数
ok[tt] = 0;

}
}
int main()
{
isprime();
int T;
cin>>T;
for(int cas=1; cas<=T; cas++)
{
cin>>a>>b;
ShaiXuan();
LL ret = 0;
if(a == 1)
ret = -1;
for(int i=0; i<=tmp; i++)
if(ok[i])
ret++;
printf("Case %d: %lld\n",cas,ret);
}
return 0;
}

angel_imp
+ 关注