Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13,…
寻找比n小的所有质数的方法. 2是质数, 2*i都是质数,同样3是质数,3*i也都是质数 代码如下 int n; vector<, true); prime[] = prime[] = false; ; i<=n; ++i) if (prime[i]) if (i * 1ll * i <= n) for (int j=i*i; j<=n; j+=i) prime[j] = false;…
题意: 给出n,求把n写成若干个连续素数之和的方案数. 分析: 这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的优化. 首先筛出10000以内的素数,放到一个数组中,然后求出素数的前缀和B.这样第i个素数一直累加到第j个素数,就可表示为Bj - Bi-1 枚举连续子序列的右端点j,我们要找到Bj - Bi-1 = n,也就是找到Bi-1 = Bj - n. 因为Bj是递增的,所以Bi-1也是递增的,所以我们就…
Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime numbers are used in allkinds of circumstances, particularly in fields such as cryptography, hashing among many others. Any method w ill be sufficient…
埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的方法.从建立一个整数2~N的表着手,寻找i? 的整数,编程实现此算法,并讨论运算时间. 由于是通过删除来实现,而1和0则不是素数,所以从2,3,5以及其倍数删除. 用Data[]来储存所有的数,将替换好的数字存在Data[]当中 而只需做出将2,3,5以及能将这些数整除的数字替换为零:if(Data[j] % i == 0 ) Data[j]==0; 实现的代码段为: for (i = 2; i < n;…
拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). 算法思想:先假定所有的数都是素数,然后从最小的素数2出发,把素数的所有倍数筛出去.又因为一个数的质因数都是成对出现的,比如100 = 1*100 = 2*50 = .....= 10*10,所以筛素数时只用筛到 n的开平方就行了. 伪代码如下: 对于任意的范围n, 设bool prime[ ],初始…
Given a number N, the output should be the all the prime numbers which is less than N. The solution is calledSieve of Eratosthenes: First of all, we assume all the number from 2 to N are prime number (0 & 1 is not Prime number). According to the Prim…
素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还有java与c++版源代码. void runEratosthenesSieve(int upperBound) { int upperBoundSquareRoot = (int)sqrt((double)upperBound); ]; memset(isComposite, , )); ; m…
1009: 一带一路 时间限制: 1 Sec  内存限制: 128 MB 题目描述 一带一路是去去年习大大提出来的建设“新丝绸之路经济带”和“21世纪海上丝绸之路”的战略构想.其中就包括我们新疆乌鲁木齐,所以这也是对新疆来说是一个巨大的机遇.我们新疆要加强和周边国家的经济文化交流,就必然要和周边国家城市修许多路.现在假设有N座城市,每一个城市都有一个投资指标数,例如A城市和B城市的投资指标数分别为Ia和Ib,如果Ia或Ib或Ia-Ib的绝对值为素数或者为1,则A城市和B城市可以修路,否则不能.他…
题目描述 题目来源于 LeetCode 204.计数质数,简单来讲就是求"不超过整数 n 的所有素数个数". 常规思路 一般来讲,我们会先写一个判断 a 是否为素数的 isPrim(int a) 函数: bool isPrim(int a){ for (int i = 2; i < a; i++) if (a % i == 0)//存在其它整数因子 return false; return true; } 然后我们会写一个 countIsPrim(int n) 来计算不超过 n…
http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 什么叫唯一分解定理:算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1a1P2a2P3a3......Pnan,这里P1<P2<P3......<Pn均为质数,其中指数ai是正整数.这样的分解称为 N 的标准分解式 我们求出n的因…
D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the seco…
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairs…
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问有几组这样的(a,b) 比较简单的哥德巴赫猜想题,不需要多说,但一般的素数判定会TLE,所以这里用的是素数筛选法 这里需要注意的是筛选出来素数数组的大小 #include<stdio.h> #include<math.h> #include<string.h> #inclu…
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven…
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34323   Accepted: 13169 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic…
Problem Description Everybody knows any number can be combined by the prime number. Now, your task is telling me what position of the largest prime factor. The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc. Specially, LPF(1) = 0.   In…
废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后我就开始一顿胡蒙,各种举例子.找规律下面为我取n = 10的过程. 1.首先1肯定不用取.因为它太特殊了,如果 取它的话,只能判断是不是1,因为所有数都是1的倍数:反之其他所有情况排除了就选它,不用浪费次数来取它. 2.素数肯定是要取的.因为......因为这题一看就是考素数的题. 3.以10为例,…
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description The branch of mathematics called number theory and itself). The first prime numbers are ,,, but they quickly become less frequent. One of the…
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为int根号后大小不超过60000. 即因子的大小不会超过60000,除非本身是质数. int 4 -2147438648-+2147438647long int 4 -2147438648-+2141438647 #include <iostream> #include <cstdio>…
用vis标记出现过的id,checked标记询问过的id.至于如何判断排名为素数,用素数筛选法预处理一下即可,水题. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; /* 素数筛选法 */ +; int isPrime[maxn]; int vis[maxn]; int checked[maxn]; int…
题目链接:http://poj.org/problem?id=1811 题目解析:2<=n<2^54,如果n是素数直接输出,否则求N的最小质因数. 求大整数最小质因数的算法没看懂,不打算看了,直接贴代码,以后当模版用. 数据比较大,只能先用Miller_Rabin算法进行素数判断. 在用Pollard_rho分解因子.   #include <iostream> #include <stdio.h> #include <string.h> #include…
http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进行素数筛选(没法开那么大的数组),我们可以将a当做0,将b当做b-a 这样求[a,b]之间就变成了求[0, b - a]之间,这样就可以开数组来筛选 下图是代码式子j = j + prime[i] - a % prime[i]的由来 #include<stdio.h> #include<ma…
Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 ques…
How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description   Give you a lot of positive integers, just to find out how many…
题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围比较大,无法把这个区间内的所以素数全部筛选出来,但是b-a这个区间比较小,所以可以用区间素数筛选的办法解决这个题目. 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorith…
题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == n: a = p1^c1 * p2^c2 ..... pk^ck. b = p1^e1 * p2^e2 .... pk^ek. xi = max(ci, ei). 对于有序数对(a,b),有唯一分解定理知,每一个素因数的幂都决定了一个独一无二的数. 求(a,b)的种数就可以转化为求(ci,ei)的种数:…
转自:http://blog.csdn.net/dream_you_to_life/article/details/43883367 作者:Sky丶Memory 1.一个数是否为质数的判定. 质数,只有1和其本身才是其约数,所以我们判定一个数是否为质数,只需要判定2~(N - 1)中是否存在其约数即可,此种方法的时间复杂度为O(N),随着N的增加,效率依然很慢.这里有个O()的方法:对于一个合数,其必用一个约数(除1外)小于等于其平方根(可用反证法证明),所以我们只需要判断2-之间的数即可. b…
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include<stdio.h> //素数筛选法 +];//1 合数, 0 素数 void Prime() { memset(pri,,sizeof(pri)); pri[]=pri[]=; ;i<;i++) { ) ;j=j+i) pri[j]=; } } int main() { int a,b; Prime…
题意:给出一个区间[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…