Summation of Four Primes(歌德巴赫猜想)
来源:互联网 发布:ubuntu刻录光盘 编辑:程序博客网 时间:2024/05/01 21:28
Summation of Four Primes
题目链接:click here~
题目要求:
给出一个整数要你将其拆成由四个素数组成的数。如果,不能拆则输出“Impossible.”
思路解析:
根据题目给出的两个猜想,可以得到。如果,一个数是奇数的话则其一定可以拆成一个奇数+一个偶数的形式,而一个数是偶数的话可拆的就有两种奇数+奇数/偶数+偶数。而Waring的猜想是一个奇数可以拆成3个素数,所以显然题目给出的一个整数是奇数的时候我们就不能保留奇数拆偶数了。我们应该一开始就把奇数拆成两个素数,然后再拆偶数,而一个偶数可以拆成两个素数是猜想给出的,所以我们可以认为其实正确的。同理,当给出的数是偶数的时候,我们只能是保留两个偶数的形式,且可以一开始就把一个偶数给拆成两个素数的形式。然后,遍历另外一个偶数。而一个为n的整数内的素数个数为n/ln(n)。所以,算法总时间为O(n/ln(n));
#include <iostream>#include <algorithm>#include <vector>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int MAXN = 10000000+10;int top,prim[700000];bool vst[MAXN];void Isprim(){ top = 0; memset(vst,0,sizeof(vst)); vst[0] = vst[1] = 1; for(int i = 2;i < MAXN;++i)if(!vst[i]){ prim[top++] = i; for(int j = i+i;j < MAXN;j += i)vst[j] = 1; }}void output(int n){ for(int i = 0;i < top&&prim[i] < n;++i){ if(!vst[n-prim[i]]){ cout<<prim[i]<<" "<<n-prim[i]<<endl; break; } }}int main(){ Isprim(); //test// cout<<"top: "<<top<<endl; int n; while(cin>>n) { if(n<8){ cout<<"Impossible."<<endl; continue; } if(n&1){ cout<<"2 "<<"3 "; n -= 5; output(n); } else{ cout<<"2 "<<"2 "; n -= 4; output(n); } } return 0;}
2 0
- Summation of Four Primes(歌德巴赫猜想)
- summation of four primes
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- 110705 Summation of Four Primes
- Summation of Four Primes - PC110705
- 10168 Summation of Four Primes
- 练习 歌德巴赫猜想
- 歌德巴赫猜想
- 歌德巴赫猜想
- 歌德巴赫猜想
- 验证歌德巴赫猜想
- 验证歌德巴赫猜想
- 歌德巴赫猜想
- 验证歌德巴赫猜想
- 二重歌德巴赫猜想
- java歌德巴赫猜想
- uva 10168 summation of four primes
- Android屏幕旋转处理,保持Activity内容
- 常用企业管理工具介绍
- CentOS yum 源的配置与使用
- MySQL服务器最大连接数的合理设置
- JAVA把毫秒数转换成日期
- Summation of Four Primes(歌德巴赫猜想)
- 在一个整形数组中去掉重复的数字
- 2014年7月18日中午笔记
- 搭建: canal部署与实例运行
- SQLServer数据库自增长标识列的更新修改操作
- Codeforces Round #256 (Div. 2) C
- 5-0. 求序列前N项和
- tomcat安装配置、内存设置、绿色版添加后台服务启动,动态IP无公网IP发布tomcat应用到外网
- 变量定义与函数调用