HDU1397 POJ2909 UVA686 UVALive5674 ZOJ1657 Goldbach's Conjecture(II)【水题+打表】

Goldbach's Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7178    Accepted Submission(s): 2788


Problem Description
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
 

Input
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
 

Output
Each output line should contain an integer number. No other characters should appear in the output.
 

Sample Input
 
   
6 10 12 0
 

Sample Output
 
   
1 2 1
 

Source
Asia 1998, Tokyo (Japan)


Regionals 1998 >> Asia - Tokyo


问题链接:HDU1397 POJ2909 UVA686 UVALive5674 ZOJ1657 Goldbach's Conjecture(II)。

问题简述:参见上文。

问题分析

  这个问题是验证哥德巴赫猜想,对于输入的n,计算和等于n的奇素数对的个数。

  不预先打表时间上会超时。

程序说明

  这个问题,HDU1397和UVA686的测试数据是不一样的。最初做的程序,HDU中AC,而UVA中WA。估计原因是UVA868中的n是包含奇数的,例如7=2+5。所以后来写的程序将计算素数对的循环做了修改,程序通过了。

  这里分别给出C语言程序和C++语言程序的两个版本。


AC的C语言程序如下

/* HDU1397 POJ2909 UVA686 Goldbach's Conjecture(II) */

#include 
#include 

#define MAXN 32767

int prime[MAXN+2] = {0, 0, 1, 1, 0};

// 试除法判断一个数是否为素数
int isprime(int n)
{
    int end2, i;

    end2 = sqrt(n);
    for(i=3; i<=end2; i+=2) {
        if(n % i == 0)
            break;
    }

    return i > end2 ? 1 : 0;
}

void maketable(int n)
{
    int i;

    for(i=5; i

AC的C++语言程序如下:

/* HDU1397 POJ2909 UVA686 UVALive5674 Goldbach's Conjecture(II) */

#include 
#include 
#include 

using namespace std;

const int MAXN = 2000000;
bool prime[MAXN+1] = {false, false, true};

// 埃氏筛选法
void esieve(bool sflag[], int n)
{
    // 初始化
    for(int i=3; i<=n; i++) {
        sflag[i++] = true;
        sflag[i] = false;
    }

    // 筛选
    int max = sqrt(n);
    for(int i=3; i<=max; i++) {
        if(sflag[i]) {
            for(int j=i+i; j <= n; j+=i)
                sflag[j] = false;
        }
    }
}

int main()
{
    esieve(prime, MAXN);

    int n, count, i;

    while(scanf("%d", &n) != EOF) {
        // 判定结束条件
        if(n == 0)
            break;

        // 计算素数对个数
        count = 0;
        for(i=2; i<=n/2; i++)
            if(prime[i] && prime[n-i])
                count++;

        // 输出结果
        printf("%d\n", count);
    }

    return 0;
}


HDU中AC,而UVA中WA的C语言程序如下:

/* HDU1397 POJ2909 UVA686 Goldbach's Conjecture(II) */

#include 
#include 

#define MAXN 32767

int prime[MAXN+2] = {0, 0, 1, 3, 0};

// 试除法判断一个数是否为素数
int isprime(int n)
{
    int end2, i;

    end2 = sqrt(n);
    for(i=3; i<=end2; i+=2) {
        if(n % i == 0)
            break;
    }

    return i > end2 ? 1 : 0;
}

void maketable(int n)
{
    int i;

    for(i=5; i




你可能感兴趣的:(#,ICPC-备用二,#,ICPC-数论,#,ICPC-UVA,#,ICPC-HDU,#,ICPC-POJ,#,ICPC-ZOJ,#,ICPC-UVALive,#,ICPC-水题题解三)