质数(prime number)又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数;否则称为合数。
      最小的质数是2。

【例1】Goldbach's Conjecture (POJ 2262)

Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

Every even number greater than 4 can be

written as the sum of two odd prime numbers.

For example:

8 = 3 + 5. Both 3 and 5 are odd prime numbers.

20 = 3 + 17 = 7 + 13.

42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)

Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.

Input

The input will contain one or more test cases.

Each test case consists of one even integer n with 6 <= n < 1000000.

Input will be terminated by a value of 0 for n.

Output

For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."

Sample Input

8

20

42

0

Sample Output

8 = 3 + 5

20 = 3 + 17

42 = 5 + 37

(1)编程思路1。

对每个输入的n,从小到大依次对3~n/2之间的奇数i进行穷举,若i和n-i均是质数,则找到一组解,退出穷举。

判断一个数num是否为质数的方法是:用2~ 中的每一个整数m去除num,若某一个m能整除num,则num不是质数;否则,m是质数。

(2)源程序1。

#include <iostream>

#include <cmath>

using namespace std;

bool isPrime(int num)

{

int m;

if(num==2) return true;

for(m=2;m<=(int)sqrt((double)num);m++)

if (num%m==0)

return false;

return true;

}

int main()

{

int n,i;

while(cin>>n&&n)

{

for(i=3;i<=n/2;i+=2)

{

if(isPrime(i) && isPrime(n-i))

{

cout<<n<<" = "<<i<<" + "<<n-i<<endl;

break;

}

}

}

return 0;

}

(3)编程思路2。

上面的程序在穷举时,对每个穷举的整数i,都要调用函数isPrime(i) 和isPrime(n-i)来判断i和n-i是否为质数。实际上,可以预先生成一个判定数组flag[1000000],并且为了节省存储空间,可将flag数组定义为char类型(每个元素只占一个字节)。元素flag[i]==’1’表示整数i是质数;flag[i]==’0’表示整数i不是质数。

初始化数组flag的所有元素都为’1’,然后采用Eratosthenes筛法进行处理。

Eratosthenes筛法的基本思想是:把某范围内的自然数从小到大依次排列好。宣布1不是质数,把它去掉;然后从余下的数中取出最小的数,宣布它为质数,并去掉它的倍数。在第1步之后,得到质数2,筛中只包含奇数;第2步之后,得到素数3,一直做下去,直到筛子为空时结束。

采用这个思想,用如下循环完成对flag数组的设置。

for(i=2;i<1000000;i++)      // 用筛法构建质数表

{

if (flag[i]=='1')

for (j=2*i;j<1000000;j+=i)

flag[j]='0';

}

(4)源程序2。

#include <iostream>

using namespace std;

int main()

{

char flag[1000000];

int i,j,n;

for (i=2;i<1000000;i++)

flag[i]='1';

for(i=2;i<1000000;i++)      // 用筛法构建质数表

{

if (flag[i]=='1')

for (j=2*i;j<1000000;j+=i)

flag[j]='0';

}

while(cin>>n&&n)

{

for(i=3;i<n;i++)

{

if(flag[i]=='1' && flag[n-i]=='1')

{

cout<<n<<" = "<<i<<" + "<<n-i<<endl;

break;

}

}

}

return 0;

}

将源程序1和2分别提交给POJ评判系统,可得到如图1所示的评判结果。从结果可以看出,源程序2的执行效率比源程序1高,当然所占用的存储空间也比源程序1要多。可以说是“用空间换时间”。

图1  POJ给出的评判结果

【例2】Sum of Consecutive Prime Numbers (POJ 2739)

Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.

Your mission is to write a program that reports the number of representations for the given positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2

3

17

41

20

666

12

53

0

Sample Output

1

1

2

3

0

0

1

2

(1)编程思路1。

先将10000以内的所有质数求出来保存到数组prime中,并记下质数的个数len。

对于给定的测试数据n,用二重循环求出所有的连续质数和等于n的种数cnt。

(2)源程序1。

#include <iostream>

#include <cmath>

using namespace std;

bool isPrime(int num)

{

int m;

if(num==2) return true;

for(m=2;m<=(int)sqrt((double)num);m++)

if (num%m==0)

return false;

return true;

}

int main()

{

int prime[2000],i,j,len=0,n,cnt,sum;

for(i=2;i<10000;i++)

{

if(isPrime(i))

prime[len++]=i;

}

while (cin>>n && n!=0)

{

cnt=0;

for(i=0;i<len;i++)

{

sum=0;

for(j=i;j<len;j++)

{

sum+=prime[j];

if(sum>n)

break;

else if(sum==n)

{

cnt++;

break;

}

}

}

cout<<cnt<<endl;

}

return 0;

}

(3)编程思路2。

采用打表的方法。先用二重循环构造好答案表。

for(i=0;i<len;i++)

{

sum=0;

for(j=i;j<len;j++)

{

sum+=prime[j];

if(sum>10000) break;

ans[sum]++;      //  连续和为sum的种数加1

}

}

对于给定的测试数据n,直接查表ans[n]即可。

(4)源程序2。

#include <iostream>

using namespace std;

int main()

{

int flag[10000]={0},prime[2000],ans[10001]={0};

int i,j,len=0,n,sum;

for(i=2;i<10000;i++)           // 构造质数表

{

if (flag[i]==0)

{

prime[len++]=i;

for (j=2*i;j<10000;j+=i)

flag[j]=1;

}

}

for(i=0;i<len;i++)            // 构造答案表

{

sum=0;

for(j=i;j<len;j++)

{

sum+=prime[j];

if(sum>10000) break;

ans[sum]++;

}

}

while (cin>>n && n!=0)

{

cout<<ans[n]<<endl;

}

return 0;

}

【例3】Dirichlet's Theorem on Arithmetic Progressions (POJ 3006)

Description

If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777 - 1855) and was proved by Johann Peter Gustav Lejeune Dirichlet (1805 - 1859) in 1837.

For example, the arithmetic sequence beginning with 2 and increasing by 3, i.e.,

2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, ... ,

contains infinitely many prime numbers

2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, ... .

Your mission, should you decide to accept it, is to write a program to find the nth prime number in this arithmetic sequence for given positive integers a, d, and n.

Input

The input is a sequence of datasets. A dataset is a line containing three positive integers a, d, and n separated by a space. a and d are relatively prime. You may assume a <= 9307, d <= 346, and n <= 210.

The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

Output

The output should be composed of as many lines as the number of the input datasets. Each line should contain a single integer and should never contain extra characters.

The output integer corresponding to a dataset a, d, n should be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d.

FYI, it is known that the result is always less than 106 (one million) under this input condition.

Sample Input

367 186 151

179 10 203

271 37 39

103 230 1

27 104 185

253 50 85

1 1 1

9075 337 210

0 0 0

Sample Output

92809

6709

12037

103

93523

14503

2

899429

(1)编程思路。

定义数组char flag[1000000]用于质数的判定。元素flag[i]==’1’表示整数i是质数;flag[i]==’0’表示整数i不是质数。

对于输入的测试数据a、d和n,采用循环找到第n个质数在原等差数列中位置i。

count = 0;

for(i=0;count<n;i++)

{

if(flag[a+d*i]=='1')

count++;

}

(2)源程序。

#include <iostream>

using namespace std;

int main()

{

int a,d,n,i,j,count;

char flag[1000000];

for (i=2;i<1000000;i++)

flag[i]='1';

for(i=2;i<1000000;i++)      // 用筛法构建质数表

{

if (flag[i]=='1')

for (j=2*i;j<1000000;j+=i)

flag[j]='0';

}

flag[1]='0';

while(cin>>a>>d>>n && a+d+n!=0)

{

count = 0;

for(i=0;count<n;i++)

{

if(flag[a+d*i]=='1')

count++;

}

cout<<a+d*(i-1)<<endl;

}

return 0;

}

POJ中和质数相关的三个例题(POJ 2262、POJ 2739、POJ 3006)的更多相关文章

  1. [hihoCoder] #1158 : 质数相关

    时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被称为质数相关,是指S中 ...

  2. 【Nature 子刊】I型HLA基因中和癌症相关的体细胞突变--转载

    肿瘤的发生与免疫系统的功能密切相关.在免疫系统中,MHC(主要组织相容性复体,majorhistocompatibilitycomplex)是所有生物相容复合体抗原的一种统称.HLA(humanleu ...

  3. 2015编程之美 初赛第一场C题 质数相关 二分图的最大匹配

    质数相关 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/msbop2015round2a/prob ...

  4. HihoCoder 1158 : 质数相关 (最大独立集)

    质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被称为质数相关 ...

  5. BJFU 质数相关

    /* BJFU 质数相关 http://101.200.220.237/contest/19/problem/116/ 二分图 按质因数奇偶性建立二分图 * * */ #include <cst ...

  6. Linux2.6内核--VFS层中和进程相关的数据结构

          系统中的每一个进程都有自己的一组打开的文件,像根文件系统,当前工作目录,安装点等.有三个数据结构将 VFS 层和系统的进程紧密的联系在一起,它们分别是: file_struct,fs_st ...

  7. POJ 3713 Transferring Sylla (三连通图)

    [题目链接] http://poj.org/problem?id=3713 [题目大意] 给出一个图判断是不是三连通图,三连通图的意思是对于图中任意两点, 至少有三条路是可以相互连通的. [题解] 我 ...

  8. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)

    由于这是第一天去实现polya题,所以由易到难,先来个铺垫题(假设读者是看过课件的,不然可能会对有些“显然”的地方会看不懂): 一:POJ1286 Necklace of Beads :有三种颜色,问 ...

  9. 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753

    下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...

随机推荐

  1. hibernate 的缓存机制

    这是面试中经常问到的一个问题,楼主可以按照我的思路回答,准你回答得很完美,首先说下Hibernate缓存的作用(即为什么要用缓存机制),然后再具体说说Hibernate中缓存的分类情况,最后可以举个具 ...

  2. 使用AngelaSmith.产生测试数据

    1.安装库程序包.打开NUGET库程序包管理器控制台:输入 Install-Package AngelaSmith -Version 1.0.1                //1.1.1版本可能有 ...

  3. (转)IE内存泄露,iframe内存泄露造成的原因和解决方案

    http://my.oschina.net/jsan/blog/11169 http://blog.csdn.net/tianma630/article/details/8502395 jQuery ...

  4. HDU 4891 The Great Pan (题意题+模拟)

    题意:给定一个文章,问你有多少种读法,计算方法有两种,如果在$中,如果有多个空格就算n+1,如果是一个就算2的次方,如果在{}中, 那么就是把每个空格数乘起来. 析:直接模拟,每次计算一行,注意上一行 ...

  5. UVa 11538 Chess Queen (排列组合计数)

    题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...

  6. HDU 2914 Triangle (Fibnacci 数)

    题意:给你一个长度为 n 的木棒,求至少拿掉几根使得剩余的木棒构成不了三角形. 析:为了保证不形成三角形,所以保证两边之和等于最大边是最优,这不就是Fibnacci 数么,由于 n 很小,if-els ...

  7. bzoj 4818: [Sdoi2017]序列计数【容斥原理+dp+矩阵乘法】

    被空间卡的好惨啊---- 参考:http://blog.csdn.net/coldef/article/details/70305596 容斥,\( ans=ans_{没有限制}-ans{没有质数} ...

  8. Luogu P1280 Niko的任务【线性dp】By cellur925

    Nikonikoni~~ 题目传送门 这是当时学长讲dp的第一道例题,我还上去献了个丑,然鹅学长讲的方法我似董非董(??? 我当时说的怎么设计这道题的状态,但是好像说的是二维,本题数据范围均在1000 ...

  9. Hihocoder [Offer收割]编程练习赛70 解题报告 By cellur925

    并没有第四题.(还不会矩阵乘法加速线性数列) 题目1 : 数位翻转 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 n,你可以进行若干次操作,每次操作可以翻转 ...

  10. cxf CXF搭建webService服务器

    http://observer.blog.51cto.com/4267416/1231205 手动发布: public class ServerMain { public static void ma ...