zoj 1951 Goldbach's Conjecture(素数筛选继续水)
来源:互联网 发布:c4d python生成器 编辑:程序博客网 时间:2024/06/11 16:22
注:这题似乎有bug, two odd prime numbers 不应该是奇素数吗,所以若输入7,应该是不可以拆的,可是输出“7 = 2 + 5”和"Goldbach's conjecture is wrong.\n"都可以AC,有知道的希望能解答下疑惑
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 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.
Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)
Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.
思路:直接素数筛选预处理,然后枚举,我把素数也开了个数组存起来了,这样枚举的话应该是可以优化时间的。
因为题目存在bug,所以我两个代码都贴下:都可AC(C语言)
1:拆成两素数
#include<stdio.h>#include<string.h>#define N 1000010int p[N],prime[N],tot=0;void Prime(){ int i,j; memset(prime,0,sizeof(prime)); prime[0]=prime[1]=1; for(i=2;i<N;i++){ if(!prime[i]){ p[tot++]=i; for(j=i+i;j<N;j+=i) prime[j]=1; } }}int main(){ int n,i,j,ok; Prime(); while(scanf("%d",&n)!=EOF){ if(n==0) break; ok=0; for(i=0;i<tot&&p[i]<=n/2;i++){ if(prime[n-p[i]]==0){ ok=1; break; } } if(ok) printf("%d = %d + %d\n",n,p[i],n-p[i]); else printf("Goldbach's conjecture is wrong.\n"); } return 0;}
#include<stdio.h>#include<string.h>#define N 1000010int p[N],prime[N],tot=0;void Prime(){ int i,j; memset(prime,0,sizeof(prime)); prime[0]=prime[1]=1; for(i=2;i<N;i++){ if(!prime[i]){ p[tot++]=i; for(j=i+i;j<N;j+=i) prime[j]=1; } }}int main(){ int n,i,j,ok; Prime(); while(scanf("%d",&n)!=EOF){ if(n==0) break; ok=0; for(i=1;i<tot&&p[i]<=n/2;i++){ if(prime[n-p[i]]==0){ ok=1; break; } } if(ok) printf("%d = %d + %d\n",n,p[i],n-p[i]); else printf("Goldbach's conjecture is wrong.\n"); } return 0;}
0 0
- zoj 1951 Goldbach's Conjecture(素数筛选继续水)
- Goldbach`s Conjecture 素数筛选
- zoj 1951 Goldbach's Conjecture (水``)
- POJ 2262 Goldbach's Conjecture ( 筛选法求素数 )
- Goldbach's Conjecture(筛选法求素数)
- POJ 2262 Goldbach's Conjecture(素数筛选法)
- LIGHT OJ 1259 - Goldbach`s Conjecture(素数筛选)
- POJ 2262 Goldbach's Conjecture 素数筛选
- ZOJ 1951 Goldbach's Conjecture(素数打表)
- POJ 2262/ ZOJ 1951:Goldbach\'s Conjecture - 筛法打素数表
- ZOJ.1951 Goldbach's Conjecture【素数】 2015/09/22
- zoj 1951 Goldbach's Conjecture
- zoj 1951 Goldbach's Conjecture
- HDOJ 1397:Goldbach's Conjecture 哈希加筛选法求素数
- HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
- Goldbach's Conjecture(数论--素数判断)
- zoj 1951 || poj 2262 Goldbach's Conjecture(水水~)
- zoj--1951 Goldbach's Conjecture(math)
- java之ArrayList的toArray方法
- Hibernate内置映射
- windows的socket_tcp服务端与linux的socket_tcp客服端简单通讯
- 机试算法讲解: 第41题 广度优先搜索之迷宫问题
- 使用live writer发布typecho博客含图片文章,支持1.0
- zoj 1951 Goldbach's Conjecture(素数筛选继续水)
- qt学习之键盘事件( keyPressEvent)
- 【ASP.NET】——AdRotator控件
- 机试算法讲解: 第42题 广度优先搜索之我该如何倒可乐
- 8/1
- Android 自定义底部导航栏&消息显示
- ACCESS+ADO学习记录一点点
- onvif学习1-框架介绍
- setsockopt的作用