LightOJ 1197 - Help Hanzo (模拟筛素法区间筛素)
来源:互联网 发布:淘宝客服平均工资 编辑:程序博客网 时间:2024/05/22 00:46
Amakusa, the evil spiritual leader has captured thebeautiful princess Nakururu. The reason behind this is he had a little problemwith Hanzo Hattori, the best ninja and the love of Nakururu. After hearing thenews Hanzo got extremely angry. But he is clever and smart, so, he kept himselfcool and made a plan to face Amakusa.
Before reaching Amakusa's castle, Hanzo has to pass someterritories. The territories are numbered asa, a+1, a+2, a+3 ... b. Butnot all the territories are safe for Hanzo because there can be other fighterswaiting for him. Actually he is not afraid of them, but as he is facingAmakusa, he has to save his stamina as much as possible.
He calculated that the territories which are primes are safefor him. Now givena and b he needs to know how many territoriesare safe for him. But he is busy with other plans, so he hired you to solvethis small problem!
Input
Input starts with an integer T (≤ 200),denoting the number of test cases.
Each case contains a line containing two integers aand b (1 ≤ a ≤ b < 231, b - a ≤ 100000).
Output
For each case, print the case number and the number of safeterritories.
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 exactlytwo different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...
思路:模拟一下素数筛的思想,因为区间最大为10w,所以说标记数组可以,过程减去边界就行了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 101000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-8using namespace std;ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}//headint prime[MAXN];int v[MAXN],cnt;void makeprime(){ mem(v);v[1]=1;cnt=0; for(int i=2;i<=100000;i++) { if(!v[i]) { prime[cnt++]=i; for(int j=i*2;j<=100000;j+=i) v[j]=1; } }}int main(){ makeprime();//打出素数表 ll l,r,t,i,j; int cas=0; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d",&l,&r); mem(v); for(i=0;i<cnt;i++) { ll k=(l/prime[i])*prime[i]; if(k<l) k+=prime[i]; if(k<(ll)prime[i]*prime[i]) k=(ll)prime[i]*prime[i];//可能会溢出RE for(j=k;j<=r;j+=prime[i]) //if(j-l>=0) v[j-l]=1; } int ans=0; if(l==1)//特殊情况 v[0]=1; for(i=0;i<=r-l;i++) if(v[i]) ans++; printf("Case %d: %d\n",++cas,r-l-ans+1); }return 0;}
- LightOJ 1197 - Help Hanzo (模拟筛素法区间筛素)
- 【区间筛法】 LightOJ 1197 Help Hanzo
- LightOJ 1197 Help Hanzo(区间素数筛)
- lightOJ 1197 Help Hanzo (区间找素数)
- LightOJ 1197 Help Hanzo(区间素数筛选)
- Help Hanzo(区间求素数) LightOJ
- LightOJ 1197 Help Hanzo
- LightOJ 1197 Help Hanzo
- LightOJ - 1197 Help Hanzo 素数筛
- E - Help Hanzo(LightOJ 1197)
- LightOJ 1197Help Hanzo (区间素数筛选法)
- LightOJ 1197 Help Hanzo 求区间内素数的个数
- Help Hanzo LightOJ
- LOJ - 1197 - 《Help Hanzo》【区间素数】
- Help Hanzo (素数筛+区间枚举)
- lightOJ 1197 Help Hanzo 两阶段素数筛选
- Light OJ 1197 1197 - Help Hanzo(大区间素数筛选)
- Help Hanzo light OJ 1197 “素数筛”
- hive中常量列的添加和查询
- Visual Studio环境变量使用实例:使用环境变量来组织工程
- 图片切换器:剖析Gallery和ImageSwitcher的一些细节
- CF_6B_President'sOffice
- 还是mysql的问题
- LightOJ 1197 - Help Hanzo (模拟筛素法区间筛素)
- IT行业导览-4.2-产品设计师
- 单向链表排序
- Java中权限修饰问题private -> protected -> (default) -> public
- 职场加薪步步高升的五大法则
- 34本设计师必看的书籍推荐
- VK Cup 2016 - Round 1 (Div. 2 Edition)C. Bear and Forgotten Tree 3(构造)
- 第3周项目4-用穷举法解决组合问题(3)年龄几何
- UVA 11404Palindromic Subsequence 【LCS】