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> ...
随机推荐
- 第二章平稳时间序列模型——AR(p),MA(q),ARMA(p,q)模型及其平稳性
1白噪声过程: 零均值,同方差,无自相关(协方差为0) 以后我们遇到的efshow如果不特殊说明,就是白噪声过程. 对于正态分布而言,不相关即可推出独立,所以如果该白噪声如果服从正态分布,则其还将 ...
- winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可
winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可
- Windows如何使用jstack跟踪异常代码
维护服务器时,会出现java进程在CPU.内存.硬盘上总是出现异常情况. 如何找到是哪些代码出现这些异常呢? 本文使用jstack来实现这个需求 工具/原料 java jstack Process ...
- Python制作统计图形
转载自:http://www.dcharm.com/?p=15 Python一般使用Matplotlib制作统计图形,用它自己的说法是‘让简单的事情简单,让复杂的事情变得可能’.(你说国外的“码农”咋 ...
- 第2月第3天 egorefresh
egorefresh是很老的下拉刷新,它是一个uiview,在uitableview 下拉的时候显示不同的界面. egorefresh和uitableview的耦合度很高,uitableview滚动和 ...
- C\C++ sizeof 陷阱&&总结
今天使用动态数组,本来想通过sizeof 获取动态数据,结果出现了错误. 先对自己做个测试,能做出下面这个题目,并做出合理解释,可以不用往下看了. ][]; cout<< cout< ...
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- 如何使用java自定义注解?demo
1.Description.java package kzfy.bk.com; import java.lang.annotation.Documented; import java.lang.ann ...
- javaweb项目中的WEB-INF与META-INF
/WEB-INF 放置web应用程序配置文件.jsp页面.lib包.classes文件等. /META-INF java打jar包时,自动生成的一个文件夹.相当于一个信息包,目录中的文件和目录获得ja ...
- ubuntu 14 谷歌拼音输入法
帮人倒腾了下,顺便记录下: https://rivercitylabs.org/install-google-pinyin-on-ubuntu-14-04/ sudo apt-get install ...