首页 > 其他分享 >POJ 2909 Goldbach's Conjecture

POJ 2909 Goldbach's Conjecture

时间:2023-02-07 12:35:37浏览次数:58  
标签:prime even int 2909 Goldbach number long POJ include


Goldbach's Conjecture

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 11574

 

Accepted: 6807

Description

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

Source

​Svenskt Mästerskap i Programmering/Norgesmesterskapet 2002​

算法分析:

题意:

给一个数n,满足n=a+b,a,b为质数,记录这样对数,(a,b)和(b,a)算一种情况

实现

直接素数打表就行,枚举小于它的一半素数就行,一遍水过

代码实现:

#include<cstdio>  
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
typedef long long ll;
int n,flag=0,k,sum=0,y;

const long N = 40005;
long prime[N] = {0},num_prime = 0; //prime存放着小于N的素数
int isNotPrime[N] = {1, 1}; // isNotPrime[i]如果i不是素数,则为1
int Prime()
{
for(long i = 2 ; i < N ; i ++)
{
if(! isNotPrime[i])
prime[num_prime ++]=i;
//无论是否为素数都会下来
for(long j = 0 ; j < num_prime && i * prime[j] < N ; j ++)
{
isNotPrime[i * prime[j]] = 1;
if( !(i % prime[j] ) ) //遇到i最小的素数因子
//关键处1
break;
}
}
return 0;
}

int main()
{
int n;
Prime();
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
int ans=0;

for(int i=0;i<num_prime;i++)
{
if(prime[i]>n/2) break; //枚举到n/2就行了
if(isNotPrime[n-prime[i]]!=1)
{
ans++;

}
}
printf("%d\n",ans);
}
return 0;
}

 

标签:prime,even,int,2909,Goldbach,number,long,POJ,include
From: https://blog.51cto.com/u_14932227/6041969

相关文章