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 ...
随机推荐
- java实例初始化块
实例初始化程序块用于初始化实例数据成员. 它在每次创建类的对象时运行.实例变量的初始化可以是直接的,但是可以在初始化实例初始化块中的实例变量时执行额外的操作. 什么是实例初始化块的使用,我们可以直接分 ...
- C# tcp udp 串口 通信
简单的实现tcp同步和异步,udp,串口通信 static List<TcpClientState> clientArray = new List<TcpClientState> ...
- lua load
load (chunk [, chunkname [, mode [, env]]]) 加载一个代码块. 如果 chunk 是一个字符串,代码块指这个字符串. 如果 chunk 是一个函数, load ...
- H5新特性监听手机的返回键
var hiddenProperty ='hidden' in document ? 'hidden' :'webkitHidden' in document ? 'webkitHidden' : ' ...
- 在C#中生成GUID的方法
var guid = Guid.NewGuid();Debug.WriteLine(guid.ToString()); //1f3c6041-c68f-4ab3-ae19-f66f541e3209 ...
- 触摸事件MotionEvent
触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...
- 通过 Powershell 来替换 ARM 模式下虚拟机的网络接口
需求描述 客户在部署完 ARM 模式的虚拟机以后,由于误操作在虚拟机内部禁用了网卡导致远程访问虚拟机受到限制,以下是通过 Powershell 命令来替换原有虚拟网络接口实现虚拟网卡重置功能. Not ...
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Linux文件寻址算法:逻辑地址到物理地址的转换
题目描述: 编写一个函数实现Linux文件寻址的算法,即读取文件当前位置到物理存储位置的转换函数,需要给出运行的测试数据,可以假设和模拟需要的数据和结构.即编写一个函数unsigned long lt ...
- SQA和测试规程
SQA *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...