LightOJ 1197 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
题意:t组数据,每组数据给定a和b,求ab之间素数的个数。
题解:ab范围是2的31次方,打10^5的素数表筛选即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
ll prime[maxn],cnt=;
bool isprime[maxn];
void get() //素筛模板 细节问题要注意
{
for(int i=; i<=maxn; i++)
{
if(!isprime[i])
{
prime[cnt++]=i;
for(int j=i+i; j<=maxn; j+=i)
isprime[j]=true;
}
}
}
int main()
{
get();
int t,cas=;
cin>>t;
while(t--)
{
ll a,b; //用int会Re 能long long 就long long
cin>>a>>b;
if(a<) //这里一定要注意 没有这句会wa 1 这个数要跳过去
a=; //1和0既非素数也非合数
bool vis[maxn];
memset(vis,,sizeof(vis));
for(int i=; prime[i]*prime[i]<=b; i++)
{
ll j;
j=prime[i]*(a/prime[i]);
if(j<a) //这句话相对于上一句一定要后加上 因为a=18 18/2*2=18 不需要加
j+=prime[i];
if(j==prime[i]) //这句话相当于上一句一定要后加上 因为a=2 2/5*5=0 0+5=5 5+5=10才对
j+=prime[i];
for(; j<=b; j+=prime[i])
vis[j-a]=;
}
ll ans=;
for(int i=a; i<=b; i++)
if(!vis[i-a])
ans++;
printf("Case %d: %d\n",cas++,ans);
}
return ;
}
LightOJ 1197 Help Hanzo(区间素数筛选)的更多相关文章
- LightOj 1197 Help Hanzo 区间素数筛
题意: 给定一个区间a,b,a-b>=100000,1<=a<=b<=231,求出给定a,b区间内的素数的个数 区间素数筛 (a+i-1)/ ii向上取整,当a为 i 的整数倍 ...
- LightOj 1197 - Help Hanzo(分段筛选法 求区间素数个数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 题意:给你两个数 a b,求区间 [a, b]内素数的个数, a and b ( ...
- M - Help Hanzo LightOJ - 1197 (大区间素数筛法)
题解:素数区间问题.注意到a和b的范围是1<<31,所以直接暴力打表肯定不可以.如果一个数是合数,他的两个因子要么是两个sqrt(x),要么就分布在sqrt(x)两端,所以我们可以根据sq ...
- LightOj 1197 Help Hanzo (区间素数筛选)
题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围 ...
- LightOJ 1197 LightOJ 1197(大区间素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进 ...
- LightOJ1197 Help Hanzo —— 大区间素数筛选
题目链接:https://vjudge.net/problem/LightOJ-1197 1197 - Help Hanzo PDF (English) Statistics Forum Tim ...
- LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)
题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...
- M - Help Hanzo LightOJ - 1197 (大区间求素数)
题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数 然后取倍数删 ...
- LightOJ 1197 Help Hanzo 素数筛
题意:筛一段区间内素数的个数,区间宽度10w,区间范围INT_MAX 分析:用sqrt(INT_MAX筛一遍即可),注意先筛下界,再筛上届,因为有可能包含 #include <cstdio> ...
随机推荐
- firstchild.data与childNodes[0].nodeValue意思(转)
x.firstchild.data:获取元素第一个子节点的数据: x.childNodes[0]::获取元素第一个子节点; x.childNodes[0].nodeValue.:也是获取元素第一个子节 ...
- 如何催促Apple进行App审核
为什么提交给 App Store 的应用进入"审核(In Review)"状态后, 仍然可能会等待好多天的时间 ? 不过你也可以通过催促Apple进行App审核来缩短这个时间.以下 ...
- nginx专题
1.Nginx和php性能优化相关 专家向磊http://slaytanic.blog.51cto.com/2057708/1173021 2.Puppet利用Nginx多端口实现负载均衡http:/ ...
- 【codevs1163】访问艺术馆
题目描述 皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画.艺术馆的结构,每条走廊要么分叉为二条走廊,要么通向一个展览室.皮尔知道每个展室里藏画的数量,并且他精确地测量了通过每条走廊的 ...
- hadoop之 mr输出到hbase
1.注意问题: 1.在开发过程中一定要导入hbase源码中的lib库否则出现如下错误 TableMapReducUtil 找不到什么-- 2.编码: import java.io.IOExceptio ...
- jQuery - 动态创建iframe并加载页面
<html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...
- android 在线升级借助开源中国App源码
android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...
- 忧桑三角形,调了半天,真忧桑TAT
忧桑三角形 试题描述 小J是一名文化课选手,他十分喜欢做题,尤其是裸题.有一棵树,树上每个点都有点权,现在有以下两个操作: 1. 修改某个点的点权 2. 查询点u和点v构成的简单路径上是否能选出三个点 ...
- xcode快捷方式 一 快速找到对应文件
事情是这样的,当一个项目进行到一定程度的时候,文件就多了.通过storyboard或xib,相对应的文件.m/.h拖线或其他的操作时,找到对应的文件稍显麻烦.今天,一个快捷方式解决了这个困扰. 注:在 ...
- BZOJ 1468: Tree
Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...