Help Hanzo (素数筛+区间枚举)
2016-03-20 17:24
435 查看
Help Hanzo
题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000). (全题在文末)
题解:
a~b枚举必定TLE,普通打表MLE,真是头疼。。
b - a ≤ 100000 是关键。
类似素数筛的方法:
1.初始化vis[]=0 ;
2.素数的倍数vis[]=1;
3. b较小时,素数筛解决
b很大时,素数筛的vis[]会MLE,此时用vis2[i-a]保存vis[i]就不会MLE 了。。
Help Hanzo
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000). (全题在文末)
题解:
a~b枚举必定TLE,普通打表MLE,真是头疼。。
b - a ≤ 100000 是关键。
类似素数筛的方法:
1.初始化vis[]=0 ;
2.素数的倍数vis[]=1;
3. b较小时,素数筛解决
b很大时,素数筛的vis[]会MLE,此时用vis2[i-a]保存vis[i]就不会MLE 了。。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long LL; const int N=1e5+5; bool vis ,visab ; int prime ,cnt=0; void is_prime() { memset(vis,0,sizeof(vis)); vis[1]=1; for(int i=2;i<N;i++) { if(!vis[i]) { prime[cnt++]=i; for(int j=i+i;j<N;j+=i) vis[j]=1; } } } int main() { int t; cin>>t; is_prime(); for(int kase=1;kase<=t;kase++) { LL a,b; scanf("%lld%lld",&a,&b); int count=0; if(b<=N-1) { for(LL i=a;i<=b;i++) { if(!vis[i]) count++; } } else { memset(visab,0,sizeof(visab)); for(int i=0;i<cnt&&prime[i]<=b;i++) { LL k=a/prime[i]; if(k*prime[i]<a) k++; for(LL j=k*prime[i];j<=b;j+=prime[i]) { visab[j-a]=1; } } for(LL i=a;i<=b;i++) { if(!visab[i-a]) count++; } } printf("Case %d: %d\n",kase,count); } }
Help Hanzo
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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
相关文章推荐
- Maximum Product of Word Lengths
- 在软件开发的早期阶段为什么要进行可行性研究?应该从哪些方面研究目标系统的可行性?
- vs2013安装过程及使用心得
- 20145330《Java程序设计》第三周学习总结
- (6)框架技术解说
- 翻转一个数的二进制后的值
- C++作业2
- 如何实现一个教师与学生教学辅助平台?
- 浅谈C++类的继承与派生的关系
- 系统调用
- 简单的jQuery图片标注效果
- 玩骰子
- 样本间的距离或者相似度度量方法
- 03-百思不得姐(第三天)
- 数据库事务
- 三种循环语句
- Spring3.1.2与Hibernate4.1.8整合
- JVM内存区域划分Eden Space、Survivor Space、Tenured Gen,Perm Gen解释
- 实践中学习AngularJS中的表单
- 学习进度表第三周