UVA, 543 Goldbach's Conjecture

题意:给你一个数,认为它能拆成两个素数之和的形式,能,则输出x = a + b ,不能则输出 "Goldbach‘s conjecture is wrong."

思路:打印素数表,x-prime[a]检查是否为素数

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 #define MAXN 1000001
 7 #define ll
 8 int cnt;
 9 long long num;
10 bool isprime[MAXN];
11 long long prime[MAXN];
12
13 bool datecin()
14 {
15     if(scanf("%lld",&num)!=EOF)
16     {
17         if(num==0)
18             return false;
19         return true;
20     }
21     return false;
22 }
23
24 void getprime()
25 {
26     int i,j;
27     memset(isprime,1,sizeof(isprime));
28     isprime[0]=isprime[1]=0,cnt=0;
29     for(i=2;i*i<MAXN;i++)
30     {
31         if(isprime[i])
32         {
33             for(j=i*i;j<MAXN;j+=i)
34             {
35                 if(isprime[j])
36                 {
37                     isprime[j]=false;
38                 }
39             }
40
41         }
42     }
43     for(i=2;i<MAXN;i++)
44     {
45         if(isprime[i])
46             prime[cnt++]=i;
47     }
48 }
49
50 void showisp()
51 {
52     for(int i=0;i<MAXN;i++)
53     {
54         if(isprime[i])
55         cout<<i<<‘ ‘;
56     }
57     cout<<endl;
58     for(int i=0;i<cnt;i++)
59     {
60         cout<<prime[i]<<‘ ‘;
61     }
62 }
63
64 void datecal()
65 {
66     for(int i=0;i<cnt;i++)
67     {
68         if(num<prime[i])
69             break;
70         if(isprime[num-prime[i]])
71         {
72             cout<<num<<" = "<<prime[i]<<" + "<<num-prime[i]<<endl;
73             return;
74         }
75     }
76     cout<<"Goldbach‘s conjecture is wrong."<<endl;
77 }
78 int main()
79 {
80     getprime();
81     //showisp();
82     #ifdef ll
83     while(datecin())
84     {
85         datecal();
86     }
87     #endif // ll
88     return 0;
89 }

UVA, 543 Goldbach's Conjecture

时间: 2024-10-10 21:27:38

UVA, 543 Goldbach's Conjecture的相关文章

UVA, 686 Goldbach&#39;s Conjecture (II)

题意:给你一个数,认为它能拆成两个素数之和的形式,有几组素数则输出几 例:10= 3+7 思路:打印素数表,判断x-prime[a]是否为素数即可 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 #define MAXN 1000001 7 #define ll 8 int cnt,counts; 9 long long num;

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

素数筛 uva 543

给你一个n求出n由2个奇质因子的和  这2个因子差最大 没有就输出'Goldbach's conjecture is wrong. #include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #include<math.h> using namespace std; #define MAXN 1000010 bool pri[MAXN]; int main()

Goldbach&#39;s Conjecture(哥德巴赫猜想)

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5277    Accepted Submission(s): 2022点我 Problem Description Goldbach's Conjecture: For any even number n greater than or equa

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

题目1440:Goldbach&#39;s Conjecture

题目1440:Goldbach's Conjecture 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: 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 prove

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 数学常识 难度: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 / 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