POJ 2262 Goldbach's Conjecture(素数相关)

POJ 2262 Goldbach‘s Conjecture(素数相关)

http://poj.org/problem?id=2262

题意:

给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式。如果存在多组解,请输出两个素数差值最大的解。

分析:

首先我们用素数筛选法求出100W以内的所有素数。

筛选法求素数可见:

http://blog.csdn.net/u013480600/article/details/41120083

对于给定的数X,如果存在素数a+素数b==X且a与b的差距最大。那么一定只需要从小到大枚举素数a就行,然后用X-a就是b。再判断b是否是素数即可。(想想是不是

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000000;

//素数筛选法
bool not_prime[maxn+5];//not_prime[i]=true表i不是素数
int prime[maxn+5];
int get_prime()
{
    memset(prime,0,sizeof(prime));
    //注意:not_prime[i]=false时,i为素数
    memset(not_prime,0,sizeof(not_prime));

    for(int i=2;i<=maxn;i++)
    {
        if(!not_prime[i]) prime[++prime[0]]=i;
        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++)
        {
            not_prime[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
    return prime[0];
}

int main()
{
    //生成100W以内所有素数
    get_prime();

    int x;
    while(scanf("%d",&x)==1 && x)
    {
        int a,b;//x被拆分为a和b两个数
        bool ok=false;
        for(int i=1;i<=prime[0] && prime[i]<=x/2;i++)
        {
            a=prime[i];
            b=x-a;
            if(!not_prime[b])//若b是素数
            {
                ok=true;
                break;
            }
        }
        if(ok) printf("%d = %d + %d\n",x,a,b);
        else printf("Goldbach's conjecture is wrong.\n");
    }
    return 0;
}

POJ 2262 Goldbach's Conjecture(素数相关)

时间: 2024-10-14 23:20:26

POJ 2262 Goldbach's Conjecture(素数相关)的相关文章

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture (求解素数的一般筛和线性筛)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40944   Accepted: 15664 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture(素数筛选法)

Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: Every even number greater than 4 can be written as the sum of two odd prime numbers. For example: 8 =

poj 2262 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39353   Accepted: 15077 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture 数学常识 难度:0

题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分析: 在1e6内约有8e4个奇质数,因为a <= b,时间复杂度在T*4e4+1e6等级.一般T为1e3,足以承受 空间复杂度为1e6,足以承受 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm&g

POJ 2262 Goldbach&amp;#39;s Conjecture(素数相关)

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.假设存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的全部素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,假设存在素数a+素数b

Poj 2262 / OpenJudge 2262 Goldbach&#39;s Conjecture

1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37791   Accepted: 14536 Description In 1742, Christian Goldbach, a German a

POJ 2909 Goldbach&#39;s Conjecture(简单题)

[题意简述]:输入一个数,输出有几对素数对可以使他们的和正好等于这个数 [分析]:暴力打表,再暴力循环求解 //268K 125Ms #include<iostream> using namespace std; #define N 35000 // 2^15 bool isprime[N]; int prime[N],nprime;//prime[N]用来存储素数,nprime是此时一共有多少素数 void doprime(int n) { int i,j; nprime = 1; mems

POJ 2909 Goldbach&#39;s Conjecture

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10333   Accepted: 6118 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