1397

来源:互联网 发布:淘宝迪曼宠物 编辑:程序博客网 时间:2024/06/13 17:54

Goldbach's Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4667    Accepted Submission(s): 1754
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1397

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
610120
 

Sample Output
121
解题思路:
题目大意就是给你一个n,把n拆成两个素数和形式。问有几个这样的素数集合。
先打个素数表,然后开始遍历素数表,为防止集合发生重复(例如(a,b)、(b,a)这种情况),所以
循环到n/2(包含n/2),当其中一个素数确定后,只要判断另一个数是不是素数就好了·····这时候可以
直接使用打表时用到的ispri数组来直接判断(不然用朴素判断会超时)·····
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 100001;bool ispri[maxn];int pri[maxn];int t;void dopri(){    memset(ispri , true , sizeof(ispri));    ispri[0] = ispri[1] = false;    t = 0;    for(int i = 2; i <= maxn ; i ++)    {        if(ispri[i])        {            pri[t ++] = i;            for(int j = i * 2 ; j <= maxn ; j += i)                ispri[j] = false;        }    }}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int n;    dopri();    while(~scanf("%d",&n) && n)    {        int cnt = 0;        for(int i = 0 ; pri[i] <= n / 2 ; i ++)        {            int  k = n - pri[i];            if(ispri[k])                cnt ++;        }        printf("%d\n",cnt);    }}


0 0