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
水题
有t组数据 每组数据有a b 两个数 求a b之间有多少个素数 打表筛选即可
思路:先打一个素数表 找到第一个大于a的素数的倍数j 从j开始筛 筛到b为止 用flag标记 然后筛第二个素数 一直筛到sqrt(b)为止 剩下的就都是素数了 水题
#include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <map> #include <queue> const int maxn=100005; typedef long long LL; LL p[maxn]; bool flag[maxn]; bool isp[maxn]; LL i,j,n,t,x,ans; LL a,b; void getprime() { memset(isp,0,sizeof(isp)); int k=0; for(i=2;i<=maxn;i++) { if(!isp[i]) //别忘了加[i] { p[k++]=i; for(j=i+i;j<=maxn;j=j+i) isp[j]=1; } } } using namespace std; int main() { getprime(); cin>>t; for(x=1;x<=t;x++) { cin>>a>>b; memset(flag,0,sizeof(flag)); if(a<2) a=2; for(i=0;p[i]*p[i]<=b;i++) { j=p[i]*(a/p[i]); if(j<a) j+=p[i]; if(j==p[i]) j+=p[i]; //貌似可以优化一下 for(;j<=b;j=j+p[i]) flag[j-a]=1; } ans=0; for(i=0;i<=b-a;i++) { if(!flag[i]) ans++; } cout<<"Case "<<x<<": "<<ans<<endl; } return 0; }