【POJ - 2909 】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. There can be many such numbers. 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 215. 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 0Sample Output
1 2 1題目大意:
對于任何大于或等于4的偶數n,存在至少一對素數p1和p2,使得n = p1 + p2。我們認為(p1,p2)和(p2,p1)是相同的。問有多少對。。
解題報告:
? 像這種無序的二元組或者無序的三元組啥的,,,為了保證無序,其實只需要滿足他是一個遞增的就可以了。,就像那道 數的劃分? , 一樣。寫dfs的條件就是是遞增的然后去dfs。
AC代碼:
#include<cstdio> #include<cstring> #include<iostream> #define MAX 50001//求MAX范圍內的素數 using namespace std; long long su[MAX],cnt; bool isprime[MAX]; void prime() { cnt=1; memset(isprime,1,sizeof(isprime)); isprime[0]=isprime[1]=0;for(long long i=2;i<=MAX;i++) { if(isprime[i]) {su[cnt++]=i; } for(long long j=1;j<cnt&&su[j]*i<MAX;j++) { isprime[su[j]*i]=0;} } } int main() {prime();int n;int i,ans = 0; while(scanf("%d",&n) && n ) {ans = 0;for( i= 3; i+i<=n; i+=2) {if(isprime[ i ] == 1 && isprime[n-i] == 1) {ans++;}}printf("%d\n",ans);}return 0 ;}?
總結
以上是生活随笔為你收集整理的【POJ - 2909 】Goldbach's Conjecture (哥德巴赫猜想,数论,知识点结论)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ramaint.exe - ramain
- 下一篇: randomdigits.exe - r