SPOJ PRIME1 - Prime Generator(线性筛)
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Example
Input:
2
1 10
3 5 Output:
2
3
5
7 3
5
Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)
Information
After cluster change, please consider PRINT as a more challenging problem.
这网站没法搜题??!!
这也太狗血了吧qwq。。。
算了说题,,
首先直接上线性筛是不行的。
但是考虑询问的$l,r$很小,所以我们可以依次枚举。
有一个非常重要的性质:对于一个合数$x$,其除$1$外最小的质因子$<= \sqrt{x}$,
然后每次判断只需要枚举到$sqrt(x)$就可以了。
感觉强上Miller-Rabin也行。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 1e6 + , B = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int prime[MAXN], vis[MAXN], tot = ;
void Pre() {
for(int i = ; i <= 1e5; i++) {
if(!vis[i]) prime[++tot] = i;
for(int j = ; j <= tot && prime[j] * i <= 1e5; j++) {
vis[i * prime[j]] = ;
if(!i % prime[j]) break;
}
}
}
int check(int x) {
int limit = sqrt(x);
if(x == ) return ;
for(int i = ; prime[i] <= limit; i++)
if((x % prime[i]) == ) return ;
return ;
}
main() {
Pre();
int qwq = read();
while(qwq--) {
int x = read(), y = read();
for(int i = x; i <= y; i++)
if(check(i))
printf("%d\n", i);
}
}
SPOJ PRIME1 - Prime Generator(线性筛)的更多相关文章
- Spoj PRIME1 - Prime Generator
题意翻译 求给定的两个数之间的素数 Translated by @kaiming 题目描述 Peter wants to generate some prime numbers for his cry ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- SPOJ #2 Prime Generator
My first idea was Sieve of Eratosthenes, too. But obviously my coding was not optimal and it exceede ...
- 素数筛法--SPOJ Problem 2 Prime Generator
质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单, ...
- P1217 [USACO1.5]回文质数 Prime Palindromes(技巧+暴力枚举+线性筛)
技巧:就是偶数位的回文数字一定不是质数---------证明:奇数位之和sum1==偶数位之和sum2的数字可以被11整除.(11除外,这是一个坑点) 最高位,最低位必须是 1, 3, 7, 9 暴力 ...
- 线性筛prime/phi/miu/求逆元模板
这绿题贼水...... 原理我不讲了,随便拿张草稿纸推一下就明白了. #include <cstdio> using namespace std; ; int su[N],ans,top; ...
- * SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)
题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演 ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
随机推荐
- 操作系统-Interrupts
- mysql 乱码问题的捣鼓
mysql在ubuntu的终端下出现中文乱码的问题: 先学着在不改数据库的情况下对my.cnf配置文件进行修改, 主要的是设置 default-character-set=utf8 但是设置完后数据库 ...
- 数据分析核心包——pandas
一.pandas简介 pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. 1.pandas的主要功能 (1)具备对其功能的数据结构DataFrame.Series (2)集 ...
- JavaScript(Two)
innerHtml xx.innerHtml 读取元素内的所有Html内容 xx.innerHtml = 新的值 替换元素内的所有Html内容 JS中不予许出现"-"; font- ...
- C++基础--extern的用法
extern作为外部变量扩展的用法: 1. 主要作用是扩展变量或者函数的应用范围: 2. extern的用法是相对于全局变量而言: 3. 在看到extern这个关键字的时候说明这个变量已经在别的源文件 ...
- SpringCloud的学习记录(8)
这一章节讲zipkin-server. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等 ...
- 全面了解 NOSQL
NoSQL被我们用得最多的当数key-value存储,想知道为什么要有NOSQL,就首先要明白传统关系SQL: 传统关系数据库的瓶颈 传统的关系数据库具有不错的性能,高稳定型,久经历史考验,而且使用简 ...
- 重复启动某一款应用,并传递intent参数
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName); intent.setFlags(Intent.F ...
- mvc4 坑啊
昨天下午出了个BUG.到今天上午才解决掉.就是mvc页面的属性名跟controller 中action 参数的名相同.导致action无法取得前台的值.这个问题浪费了很多时间.命名要规范. 如 页面 ...
- jquery:jqery表单属性 值操作
重置表单(且清空隐藏域) $('#myform')[0].reset() DOM属性相关操作 返回属性值 $(selector).attr(attribute) 设置属性值 $(selecto ...