Light oj 1197 - Help Hanzo
2016-04-11 23:26
316 查看
1197 - Help Hanzo
PDF (English)
Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
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
Output for Sample Input
3
2 36
3 73
3 11
Case 1: 11
Case 2: 20
Case 3: 4
Note
A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...
用小素数来筛选掉大的和数,,,,,注意数组大小和long long........wrong了几十次,和别人的代码一行一行的对才发现是该用long long 的锅
代码:
PDF (English)
Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
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
Output for Sample Input
3
2 36
3 73
3 11
Case 1: 11
Case 2: 20
Case 3: 4
Note
A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...
用小素数来筛选掉大的和数,,,,,注意数组大小和long long........wrong了几十次,和别人的代码一行一行的对才发现是该用long long 的锅
代码:
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<cmath> #include<algorithm> using namespace std; #define MA 70005 //65536,,数组的大小 #define LL long long int pr[MA],kk=0,t; bool shu[101000],prim[MA]; LL a,b; int main() { long long i,j; memset(prim,true,sizeof(prim)); for (i=2;i<MA;i++) if (prim[i]) { pr[kk++]=i; for (j=i+i;j<MA;j+=i) prim[j]=false; }scanf("%d",&t); for (int ca=1;ca<=t;ca++) { scanf("%lld%lld",&a,&b); memset(shu,true,sizeof(shu)); if (a<2) a=2; long long sp,pp=b-a,s=0; for (i=0;i<kk;i++) { sp=pr[i]; j=sp*(a/sp); if (j<a) j+=sp; if (j<sp*sp)//j和sp都要用long long,,,, j=sp*sp; for (;j<=b;j+=sp) shu[j-a]=false; } for (i=0;i<=pp;i++) if (shu[i]) s++; printf("Case %d: %lld\n",ca,s); } return 0; }
相关文章推荐
- c语言 之求 Fibionacci 数列的前n个数
- block为什么用copy
- 简单的mvc 框架(二)
- 随便写写20160411
- Microsoft Azure Host OS&Guest OS
- 02-git删除文件
- Simulating queueing systems with simmer
- HTML标签marquee实现滚动效果
- 36.微改进的计算器,JComboBox下拉框
- 杭电ACM 1001
- 异步请求一个不存在的页面
- mysql事务
- mysql unsigned
- [转载]aptitude与apt-get的区别和联系
- iOS多线程编程
- List使用add方法添加数据时的覆盖问题
- POJ3469 Dual Core CPU 最小割求解最小费用划分集合
- CodeForces 589D Boulevard (一维区间判断相交 非线段交做法)
- mysql事务隔离级别REPEATABLE-READ幻读
- Xarmarin.forms for vs2015 搞了一整天没能配置成功,生成老是出错,暂决定不用建Xarmarin.forms项目