lightoj 1197 奇怪筛法
来源:互联网 发布:英国海军实力知乎 编辑:程序博客网 时间:2024/05/22 03:31
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;}
- lightoj 1197 奇怪筛法
- 【区间筛法】 LightOJ 1197 Help Hanzo
- #LightOJ 1422 Halloween奇怪的区间dp
- LightOJ 1197
- LightOJ 1197
- LightOJ - 1197 Help Hanzo 素数筛
- LightOJ 1197 Help Hanzo
- LightOJ 1197 Help Hanzo
- 奇怪
- 奇怪
- 奇怪
- 奇怪
- 奇怪
- 奇怪!
- 奇怪
- 奇怪
- 奇怪
- 奇怪
- 信息系统项目管理案例分析(2)
- 用C++实现最小公倍数和最大公约数
- 谷歌宣布Android Studio将取代Eclipse
- 从CMD 连接db2 数据库
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元异或矩阵)
- lightoj 1197 奇怪筛法
- html中frameset的详细使用方法
- What is the difference between static and global variables ?
- git查看本地和创建分支、上传分支、提交代码到分支、删除分支等
- DOM知识点归纳一
- 星际之门(一)(凯莱定理+快速幂)
- 开发经验总结-点滴积累
- Hibernate4查询的占位提示
- 【Hadoop系列】第二章:安装Hadoop(上)单机版Hadoop