题干:
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 0
Sample 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 ;
}