lightoj 1197 奇怪筛法

来源:互联网 发布:英国海军实力知乎 编辑:程序博客网 时间:2024/05/22 03:31

                                                                                                                         奇怪筛法
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

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


找一个区间中素数的个数,由于数字很大,但是区间范围并不大,那么可以把a看成0,,作为起始点,只筛一个区间的素数个数。


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <sstream>#define N 100090using namespace std;typedef long long ll;ll pri[N];int vis[N];int cnt;int f[N];void eulerpri(){    cnt=0;    for(ll i=2;i<=70000;i++)    {        if(vis[i]==0) pri[cnt++]=i;        for(int j=0;j<cnt && i*pri[j]<=70000;j++)        {            vis[i*pri[j]]=1;            if(i%pri[j]==0) break;        }    }}int main(){    //cout<<(77086800/209475)<<endl;    ll a,b;    int T;    eulerpri();   scanf("%d",&T);   for(int ca=1;ca<=T;ca++)   {       scanf("%lld%lld",&a,&b);       printf("Case %d: ",ca);//       for(int i=0;i<=50;i++)//       cout<<pri[i]<<" "<<endl;       int n=b-a;       memset(f,0,sizeof f);//由于a,b比较大,为了方便存储,把a看成0位置       for(int i=0;pri[i]*pri[i]<=b && i<cnt;i++)       {           int j=0;//用来存标号          j=(a/pri[i])*pri[i];//找a到b中第一个要被标记的数          if(j<a) j+=pri[i];//如果小于a,那么在前进一个素数周期          j-=a;//存的是下标           for(;j<=n;j+=pri[i])           {               if(j+a==pri[i])continue;//如果第一个数是该素数本身,那么不标记               f[j]=1;           }       }       int ans=0;       for(int i=0;i<=n;i++)       {           if(f[i]==0)           {               ans++;           }       }        if(a==1)        ans--;        printf("%d\n",ans);   }    return 0;}









0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩太叛逆怎么办关住 对不听话的孩子怎么办 孩子在学校不听话怎么办 孩子叛逆不回家怎么办 孩子厌学怎么办青春期叛逆 孩子青春期叛逆家长怎么办 孩子老哭不听话怎么办 8岁宝宝不听话怎么办 儿孑不听话叛逆怎么办 孩子太小不听话怎么办 得了只有很紧张怎么办 内向的妈妈孩子怎么办 对孩子没有耐心怎么办 孩子心理有问题怎么办 孩子心里有问题怎么办 父母打架我该怎么办 爸爸妈妈要离婚怎么办 父母吵架怎么办动手了 父母因为钱吵架怎么办 父母吵架孩子该怎么办 特别倔强的孩子怎么办 孩子胆小不自信怎么办 孩子不自信家长怎么办 二年级孩子厌学怎么办 儿童注意力不集中怎么办 小学数学成绩差怎么办 孩子英语成绩差怎么办 小学成绩差初中怎么办 小孩一年级成绩不好怎么办 孩子的记忆力差怎么办 普法考试没有考怎么办 小孩迷上玩手机怎么办 营养师证取消了怎么办 变频器输出缺相怎么办 体育中考来月经怎么办 不动精子率低怎么办 精子向前运动低怎么办 精子运动力低怎么办 精子正常形态低怎么办 精子畸形率100%怎么办 军训鞋子太硬怎么办