LightOJ1197 Help Hanzo —— 大区间素数筛选
题目链接:https://vjudge.net/problem/LightOJ-1197
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
|
3 2 36 3 73 3 11 |
Case 1: 11 Case 2: 20 Case 3: 4 |
题解:
1. 可知如果要求出 1~n 内的素数,那么只需要知道 1~sqrt(n)内的素数,然后通过这些素数,就可以进一步筛选出 1~n 内的素数。因为把 i*j 筛掉, 其中i、j都是 sqrt(n) 内的素数,所以最大能筛到 sqrt(n) * sqrt(n) = n 。
2.此题为单纯的大数区间筛选,由于n<=2^31,因此只需先求出 1e5内的素数,然后再对大区间进行筛选就可以了。
3.因为1不是素数,所以需要特判大区间是否从1开始。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e5+; int prime[MAXN+];
bool notprime[MAXN+];
void getPrime()
{
memset(notprime, false, sizeof(notprime));
notprime[] = notprime[] = true;
for(int i = ; i<=MAXN; i++)
{
if(!notprime[i]) prime[++prime[]]=i;
for(int j = ; j<=prime[]&&prime[j]<=MAXN/i; j++)
{
notprime[prime[j]*i] = ;
if(i%prime[j]==) break;
}
}
} void getPrime2(int L, int R)
{
memset(notprime, false, sizeof(notprime));
for(int i = ; i<=prime[]&& prime[i]<=R/prime[i]; i++)
for(int j = max(,(int)ceil(1.0*L/prime[i])); j<=R/prime[i]; j++)
notprime[j*prime[i]-L+] = true;
} int main()
{
getPrime();
int T, kase = ;
scanf("%d", &T);
while(T--)
{
int L, R;
scanf("%d%d",&L,&R);
getPrime2(L, R);
int sum = ;
for(int i = ; i<=R-L+; i++)
if(!notprime[i])
sum++; if(L==) sum--; //1不是素数,需要特判
printf("Case %d: %d\n", ++kase, sum);
}
}
LightOJ1197 Help Hanzo —— 大区间素数筛选的更多相关文章
- 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的值太大没法直接进 ...
- LightOJ 1197 Help Hanzo(区间素数筛选)
E - Help Hanzo Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- 大区间素数筛选 POJ2689
题意: 给一个区间[L,U],(1<=L< U<=2,147,483,647),U-L<=1000000,求出[L,U]内距离近期和距离最远的素数对. 因为L,U都小于2^32 ...
- 大区间素数筛选(POJ 2689)
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...
- 2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)
题目链接 Problem Description In mathematics, the function d(n) denotes the number of divisors of positiv ...
- M - Help Hanzo LightOJ - 1197 (大区间素数筛法)
题解:素数区间问题.注意到a和b的范围是1<<31,所以直接暴力打表肯定不可以.如果一个数是合数,他的两个因子要么是两个sqrt(x),要么就分布在sqrt(x)两端,所以我们可以根据sq ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- Prime Count 求大区间素数个数
http://acm.gdufe.edu.cn/Problem/read/id/1333 https://www.zhihu.com/question/29580448/answer/44874605
随机推荐
- 【APIO2015】Palembang Bridges
题目描述 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 $A$ 和区域 $B$. 每一块区域沿着河岸都建了恰好 $1000000001$ 栋的建筑,每条岸边的建筑都从 $0$ 编号到 $100 ...
- 邁向IT專家成功之路的三十則鐵律 鐵律十一:IT人應對之道-靈活
身為一位優秀的IT專家,不能夠只是在技術面的應對能力強,而必須是在人事的應對能力上也要能夠靈活與彈性,否則就算一天給你48小時,你也會把自己的身心弄垮,再強的專業.技術.能力也會瞬間化為泡影. 坦白說 ...
- 【GitHub】删除GitHub上的文件
想要删除已经提交上GitHub上的文件, 删除之后,如果这个文件夹下没有文件了,这个文件夹也会被删除! 并且在它的上层文件夹后面 有提示删除了这个文件的信息!!
- 017.View与窗口:AttachInfo
每一个View都需要依赖于窗口来显示,而View和窗口的关系则是放在View.AttachInfo中,关于View.AttachInfo的文章少,因为这个是View的内部类而且不是公共的,在应用层用的 ...
- 低成本安全硬件(二):RFID on PN532
引言 鉴于硬件安全对于大多数新人是较少接触的,而这方面又非常吸引我,但是部分专业安全研究设备较高的价格使人望而却步.在该系列中,笔者希望对此感兴趣的读者在花费较少金钱的情况下体会到硬件安全的魅力所在. ...
- makefile 与android.mk中加信息打印
makefile里面加打印: [table]@echo ' zImage - Compressed kernel image' android.mk里面加信息打印: $(warning TEXT... ...
- CString和string头文件
在使用了MFC库的工程中CString可以直接使用,在没有使用MFC库的工程中加入#include <atlstr.h> 要使用STL里的string,要加入#include <st ...
- Unsupported major.minor version (jdk版本错误)解决方案 办法
如果你遇到了 Unsupported major.minor version ,请认真看一下,说不定会有帮助. 我花两个小时总结的经验,你可能10分钟就得到了. ^**^ 一.错误现象: 当改变了jd ...
- response响应和User-Agent历史
返回百度的源码,没有任何伪装: response是服务器响应的类文件,除了支持文件操作的方法以外,还支持以下方法:
- 使用Python处理CSV文件的一些代码示例
笔记:使用Python处理CSV文件的一些代码示例,来自于<Python数据分析基础>一书,有删改 # 读写CSV文件,不使用CSV模块,仅使用基础Python # 20181110 wa ...