2017ecjtu-summer training #7 POJ 2689】的更多相关文章

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18731   Accepted: 5006 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th…
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑逆向思维: 我们先用线性筛 筛出前50000的素数,在int范围内的区间的合数的因子都在我们之前筛出来了, 然后我们就把这个区间的合数标记出来,剩下的就是素数了. 标记合数也是用全部的素数去筛一下,这样就能快速的标记这个区间的合数,然后在暴力枚举一下这个区间,更新一下答案. #include<cst…
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 区间长度不超过1000000.就是要筛选出[L,U]之间的素数 */ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib&…
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <cmath> #include <ve…
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,147,483,647) R - L <= 1000000 思路:数据量太大不能直接筛选,要采用两次素数筛选来解决.我们先筛选出2 - 50000内的所有素数,对于上述范围内的数,如果为合数,则必定有2 - 50000内的质因子.换一句话说,也就是如果一个数没有2 - 50000内的质因子,那么这个数为素…
题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousan…
[题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数,对于每个询问,用筛出的质数标记[l,r]中的合数,即可 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale&g…
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛素数是不行的,数组就开不了.可是能够依据素数筛的原理.我们先筛出sqrt(2147483647)以内的素数,然后拿这些素数去筛[L,U]之间的素数,即两次素数筛.可是L,U还是非常大,但U-L<=1000000,所以进行区间平移,将[L,U]平移为[0,U-L],就能用数组放得下. #include &…
找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数组清零://vis[]数组用于标记是否已被检验过 prime[]数组全赋初值false://prime[]数组从下标0开始记录素数 for i = 2 to MAXN (i++) if 数i未被检验过 prime[tot++]=i; for j = i*i to MAXN (j+=i) //j是i的…
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt(b)内的素数先筛出来,然后再对[a,b]区间用那些筛出来的素数再次线性筛. 代码如下: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using na…
素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define MAXN 47000 #define inf 100000000 bool z[MAXN]; int x[MAXN]; ]; ]; int main() { int a,b; ;i<=;i++) //可以从小的素数开始筛 { if(!z[i]) for(int j=i*i;j<MA…
Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th…
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U)最大也只有2^16,所以我们可以用素数筛选法,先预处理出2~2^16之间的素数,然后再用这些素数筛选出L~U之间的素数.接着就好办了. 有几个要注意的是:1:L为1的情况,可以通过令L=2或者标记isp[0]=false.2:建议用long long,否则很容易在过程中超int范围,导致数组越界RE…
题意:求[l, r]区间中的间隔距离最大与最小的相邻两个素数,r<2200000000, r-l<10^6 题解: 对于<a的合数,其必然存在一个素因子b<=sqrt(a). 因此先预处理出所有sqrt(MAXINT)的素数,之后对[l, r]区间进行筛法求素数即可 #include <cstdio> #include <cmath> #include <cstring> #define LL long long #define MAXN 220…
/** 给定一定范围求其内的素数 注意: **/ #include <iostream> #include <math.h> #include <cstring> using namespace std; #define maxn 1000000 ]; ]; ]; ]; long long q; void getprime(){ //memset(isprime,0,sizeof(isprime)); q =-; long long i,j; isprime[] = i…
称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们能够打出5*10^4内得素数.然后,在用一次筛法把在[L.R]内得合数找到,则剩下的就是素数了.这里要用到离散化.把一个数 x - L 保存在数组里.由于,直接保存肯定不行.可是我们发现区间特点较小.所以.能够想到离散化. #include <iostream> #include <algo…
A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 38280   Accepted: 12452 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different gender…
Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29218   Accepted: 10408 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices…
Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent primes. 找出给定范围内,距离最远和最近的素数.(不停超时 - -) 给的上界很大,所以全处理肯定不行. 先处理sqrt(2147483647). 然后再在l 与 r之间筛选素数. #include <cstdio> #include <cstring> #include <…
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no prop…
Description 给定两个整数 \(L,R\;(1\leq L\leq R\leq 2^{31},R-L\leq 10^6)\) ,求闭区间 \([L,R]\) 中相邻两个数最大的差是多少,输出这些质数. Solution 任何一个合数 \(N\) 必定包含一个不超过 \(\sqrt N\) 的质因子. 所以,我们只需要用筛法求出 \(2-\sqrt N\) 之间的所有质数.对于每个质数 \(p\) ,把 \([L,R]\) 中能被 \(p\) 整除的数标记. 最终所有未被标记的数就是 \…
DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出的U的范围超出了int的最大.所以不能直接打出1-U的素数表.[我们知道.利用素数筛法时是把所有的合数都筛掉了.那么我们也想办法把L-U内的合数都筛掉就可以了.那么只要用sqrt(U)内的素数去筛掉L-U的合数就可以了.]只是大概理解....... #include <stdio.h> #incl…
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18707   Accepted: 4998 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that ha…
Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is…
一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int Range=50000; const int N=1000000+233…
Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th…
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素数去筛$[L,R]$. 注意1不是素数,但不会被筛掉,需要特殊处理(巨坑~~) #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f; ll L,R…
Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number the…
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no prop…
素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: http://blog.csdn.net/maxichu/article/details/45459533 然后是参考了kuangbin的模板: http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html 模板如下: //快速乘 (a…