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
随机推荐
- HDU 5890 Eighty seven(DP+bitset优化)
题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...
- 洛谷—— P1875 佳佳的魔法药水
https://www.luogu.org/problemnew/show/1875 题目背景 发完了 k 张照片,佳佳却得到了一个坏消息:他的 MM 得病了!佳佳和大家一样焦急 万分!治好 MM 的 ...
- 洛谷——P2527 [SHOI2001]Panda的烦恼
P2527 [SHOI2001]Panda的烦恼 题目描述 panda是个数学怪人,他非常喜欢研究跟别人相反的事情.最近他正在研究筛法,众所周知,对一个范围内的整数,经过筛法处理以后,剩下的全部都 ...
- qt使用
1安装qt软件(.run文件) . chmod 777 qt.....run ./qt...run或sudo ./qt...run(安装目录不一样) 2.sudo gedit .bashrc添加以下内 ...
- Java随机数技巧-新手篇
package 向家康; import java.util.concurrent.ThreadLocalRandom; public class 练习21 { public static void m ...
- 【hibernate postgresql】注解@TypeDef/@Enumerated/数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" is of type gender but expression is of type character varying
数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" ...
- 【bootstrap】使用支持bootstrap的时间插件daterangepicker
其中的架包和代码,具体可以去GitHub下查看: https://github.com/AngelSXD/myagenorderdiscount 1.引入js和css <link href=&q ...
- sql server 2008出现远程过程调用失败
sql server 2008出现远程过程调用失败解决方式有下面几种: 1.依照网上说的方法.是由于装了vs 2012或是vs2013或是vs2015等高版本号时.安装了Microsoft SQL ...
- vue 给组件绑定原生事件
有时候,你可能想在某个组件的根元素上监听一个原生事件.可以使用 v-on 的修饰符 .native.例如: <my-component v-on:click.native="doThe ...
- 串匹配算法之BM算法
参考资料: http://blog.csdn.net/eric491179912/article/details/6210009 http://blog.163.com/pengfeicui@ye ...