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> ...
随机推荐
- JLS(Third Edition) Chapter12 Execution
这一章详细说明在一个program执行时,发生的activities. 它根据JVM和组成program的类.接口.实例的生命周期 组织. 一个JVM从加载一个特定的类并调用它的main方法开始启 ...
- nyoj 15 括号匹配(二)动态规划
当时看到(二)就把(一)做了, 一很容易,这道题纠结了好几天,直到今晚才看懂别人的代码谢,勉强才写出来.................... 不愧是难度6的题. #include <stdio ...
- ajax浅析---ScriptManagerProxy
使用ScriptManagerProxy控件 在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-Page和C ...
- Maven 跳过测试目录
在命令行使用 mvn install -Dmaven.skipTests 或 mvn install -Dmaven.test.skip=true 或在pom.xml设置 <build> ...
- MySQL事物控制
有时候我们需要保证事物的各个步骤都执行成功的前提下才能让每一步骤的事物执行,此时就需要事物控制. 事物控制用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml语句要么全部成功,要么全部失败 ...
- 游戏BUFF设计
游戏中的BUFF/DEBUFF我们见过很多,我见到的玩得比较泛滥的就属WAR3.魔兽世界.九阴真经.仿DOTA类的如LOL. 总体上来说,BUFF/DEBUFF都属于“临时的技能效果”,因此它们可以沿 ...
- php导出csv数据在浏览器中输出提供下载或保存到文件的示例
来源:http://www.jb51.net/article/49313.htm 1.在浏览器输出提供下载 /** * 导出数据到CSV文件 * @param array $data 数据 * @pa ...
- tornado 排程
https://groups.google.com/forum/#!topic/python-tornado/KEmAg97zUg8 鉴于不是所有人都能跨越GFW,摘抄如下: Scheduled jo ...
- [ruby on rails] 跟我学之(10)数据输入验证
这里简单加上几个验证,非空,最小长度,唯一 修改模型 修改app/models/post.rb文件,如下: class Post < ActiveRecord::Base #attr_acces ...
- TO BE OPEN
我们通常都在一个很狭隘的世界里. 却以为我们有了整个蓝天.