LightOJ 1197 - Help Hanzo (模拟筛素法区间筛素)
2016-03-30 20:45
351 查看
1197 - Help Hanzo
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 as
a, 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 given
a 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!
Each case contains a line containing two integers aand
b (1 ≤ a ≤ b < 231, b - a ≤ 100000).
思路:模拟一下素数筛的思想,因为区间最大为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-8
using 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;}
//head
int 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;
}
![]() ![]() | PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 as
a, 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 given
a 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-8
using 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;}
//head
int 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;
}
相关文章推荐
- 【LeetCode】257. Binary Tree Paths
- 2015-2016 下半学期 第三周 训练
- JNI/NDK开发指南(开山篇)
- 【Java学习-J.160330.0.3】笔记2-Linux基础
- Delphi数据类型转换(有几个字符串函数没见过,比如StringToWideChar和WideCharToString)
- having筛选结果集
- 加快XCode的编译链接速度
- 第3周项目4-用穷举法解决组合问题(3)年龄几何
- Sverlet生命周期和工作机制
- UVa401 - Palindromes
- bash shell 编程练习二
- 面试题69:序列化二叉树
- Mac下配置mysql环境变量的另一种方式
- There was a problem getting an AppDomain to run the transformation from the host. The process cannot
- VK Cup 2016 - Round 1 (Div. 2 Edition)C. Bear and Forgotten Tree 3(构造)
- Java中权限修饰问题private -> protected -> (default) -> public
- docker简明教程(一)
- 单向链表排序
- eclipse启动tomcat成功,但却一直处在starting状态
- 职场加薪步步高升的五大法则