light oj 1197 Help Hanzo
来源:互联网 发布:国家规划教材 知乎 编辑:程序博客网 时间:2024/06/05 18:39
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 asa, 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 givena andb 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 integersa andb (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
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<queue>#include<deque>#include<stack>#include<map>#include<set>using namespace std;typedef long long ll;#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define sf scanf#define pf printf#define mem(i,a) memset(i,a,sizeof i)const int Max=1e6+50;int p[Max+1],cou,m;ll a,b;bool vis[Max+1],vpn[Max];//vis储存是否为素数,当数据较大时就要用到vpn这个数组了void prime()//素数打表,我用的是线性筛法O(n),用埃氏筛法O(nlong n)也行,只是打表范围不能太小,也不能太大,不然TLE+MLE{ mem(vis,0); vis[1]=1; cou=0; for(ll i=2;i<=Max;i++) { if(!vis[i])p[cou++]=i; for(ll j=0;j<cou&&i*p[j]<=Max;j++) { vis[i*p[j]]=1; if(!(i%p[j]))break; } }}int main(){ prime(); sf("%d",&m); for1(i,m) { int cnt=0; sf("%I64d%I64d",&a,&b); if(b<=Max)//要求的范围小于打的素数表范围,直接枚举 { for(ll i=a;i<=b;i++) if(!vis[i]) cnt++; } else//大于打表范围 { mem(vpn,0); for(ll i=0;i<cou&&p[i]<=b;i++) { ll k=a/p[i]; if(k*p[i]<a)k++; for(ll j=k*p[i];j<=b;j+=p[i])//枚举所有含因数的数,用vpn标记为1 vpn[j-a]=1; } for(ll i=a;i<=b;i++)//枚举未被标记的数的个数即是答案 if(!vpn[i-a]) cnt++; } pf("Case %d: %d\n",i,cnt); } return 0;}
- Light OJ 1197 - Help Hanzo
- Light oj 1197 - Help Hanzo
- light oj 1197 Help Hanzo
- Help Hanzo light OJ 1197 “素数筛”
- Light OJ 1197 1197 - Help Hanzo(大区间素数筛选)
- LightOJ 1197 Help Hanzo
- LightOJ 1197 Help Hanzo
- Help Hanzo
- LOJ - 1197 - 《Help Hanzo》【区间素数】
- 【区间筛法】 LightOJ 1197 Help Hanzo
- E - Help Hanzo(LightOJ 1197)
- LightOJ - 1197 Help Hanzo 素数筛
- Help Hanzo(素数筛)
- Help Hanzo LightOJ
- LightOJ1197 Help Hanzo
- lightOJ 1197 Help Hanzo 两阶段素数筛选
- LightOJ 1197Help Hanzo (区间素数筛选法)
- lightOJ 1197 Help Hanzo (区间找素数)
- [转]深入理解Java Stream流水线
- Java代理
- STM32 中断优先级相关概念与使用笔记
- python实现求字符串最长公共子串
- Xcode代码快功能
- light oj 1197 Help Hanzo
- Tensorflow-MNIST入门实例
- Java_log2000_Java&Cpp&Python&JS等语言中对于循环语句局部变量的不同处理举例
- 使用webstorm进行javascript的Debug调试功能
- 算法学习开源代码
- jQuery定义插件的方法
- MFC不同类之间的函数调用
- Binary String Matching(南阳理工OJ)
- 设计模式——总概述