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 ...
随机推荐
- String常用操作
常量池: 字符串一旦被初始化就不会被改变 String s="123"; s="abc"; System.out.print(s); 这段代码看上去s的值是被改 ...
- PAT 1083 List Grades
#include <cstdio> #include <cstdlib> using namespace std; class Stu { public: ]; ]; }; i ...
- sql and csharp: Split Function
T-SQL: declare @int int,@prov int,@city int,@str nvarchar(500) set @str='天河麗特青春:中國廣東省廣州市天河區天河路623號天河 ...
- 超级简单的jQuery纯手写五星评分效果
超级简单的评分功能,分为四个步骤轻松搞定: 第一步: 引入jquery文件:这里我用百度CDN的jquery: <script src="http://apps.bdimg.com/l ...
- es6新语法的使用
1.声明变量: let 声明变量 作用域代码块作用域{} 尽在模块 先使用后声明 会报错 { let a= 12; alert(a) } let 不允许重复声明同一个变量 const 声明是一个常量, ...
- GUID 格式化
1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid().ToSt ...
- win7 下vs2008试用版破解
用过微软的开发套件Visual Studio 2008,如果用的是试用版本,超过90天,就会过期,出现下面这张图片显示的 下面介绍破解的步骤: 1.首先打开控制面板——然后找到卸载或更改程序——然后找 ...
- idea打jar包经验总结
关于在idea下打jar问题,在日常工作中经常用到,这里总结下流程. 1.在项目上鼠标右键 --> Open Module Settings 2.如下图,点击 '+' 3. 选择JAR --&g ...
- gitlab 邮件服务器配置
一.修改 /etc/gitlab/gitlab.rb 文件,添加邮件服务器信息 SMTP settings 例如163 邮件服务器 external_url 'http://你的IP地址或域名' ## ...
- May 27th 2017 Week 21st Saturday
I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. I remember in the movie, The Da ...