Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 41142 | Accepted: 15754 |
Description
Every even number greater than 4 can be
written as the sum of two odd prime numbers.
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.
Input
Output
Sample Input
8 20 42 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
Source
题意:
输入一个数,把它表示成两个质数的和。
解题思路:
哥德巴赫猜想。
AC代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <algorithm> using namespace std; int prime[700000]; bool is_prime[10000000]; int tot; void solve() { tot=0; is_prime[0]=is_prime[1]=1; for(int i=2;i<10000000;i++) if(!is_prime[i]){ prime[tot++]=i; for(int j=i*2;j<10000000;j+=i){ is_prime[j]=1; } } } int main() { solve(); int n; while(scanf("%d",&n),n){ int res=0; for(int i=0;prime[i]<=n/2;i++){ int j=n-prime[i]; if(!is_prime[j]){ printf("%d = %d + %d\n",n,prime[i],j); break; } } } return 0; }