URAL 1356
来源:互联网 发布:手机遥控器软件哪个好 编辑:程序博客网 时间:2024/05/21 09:43
题目大意:将T个正整数N拆成若干个素数之和,最小化素数个数。
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
数据规模:0<=T<=20,2<=N<=10^9。
理论基础:哥德巴赫猜想:任一大于2的偶数,都可表示成两个素数之和。大于等于7的奇数都可以表示成三个质数之和。
题目分析:唉,这理论一摆出来,我都无话可说了,自己想吧。(虽然没完全证明,可是在数据规模内尚无反例。嘎嘎、、、)
代码如下:
#include<iostream>#include<cstdio>using namespace std;typedef long long LL;typedef long unsigned LU;bool isprime(int x){ for(int i=2;i*i<=x;i++) { if(x%i==0)return false; } return true;}int main(){ int T; scanf("%d\n",&T); while(T--) { int n,k; scanf("%d",&n); if(isprime(n)) { printf("%d\n",n); continue; } if(n%2==0) { k=n-3; while(!(isprime(k)&&isprime(n-k)))k-=2; printf("%d %d\n",n-k,k); } else { int k=n-2; if(isprime(k)) { printf("2 %d\n",k); continue; } k-=2; while(!isprime(k))k-=2; printf("%d ",k); n=n-k; if(n==4) { printf("2 2\n"); continue; } k=n-3; while(!(isprime(k)&&isprime(n-k)))k-=2; printf("%d %d\n",k,n-k); } }return 0;}其中,减三和减二是因为素数肯定是奇数(2除外)。
参考文献:
http://zh.wikipedia.org/wiki/%E5%93%A5%E5%BE%B7%E5%B7%B4%E8%B5%AB%E7%8C%9C%E6%83%B3
by:Jsun_moon http://blog.csdn.net/Jsun_moon
- URAL 1356
- URAL 1356 哥德巴赫猜想
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- C# DllImport的用法
- 自我介绍
- flody求最短路 一般点不超过100个 o(n3)
- 非递归版的归并排序以及希尔排序
- 40 种网页技巧
- URAL 1356
- fedora17安装内核源码
- js验证密码强度
- 炫彩界面库v1.4.2-360安全卫士v8.5-UI设计器使用教程
- [leetcode刷题系列]Construct Binary Tree from Inorder and Postorder Traversal
- sql导入导出全部DBF,excel等文件
- 生成流水号码
- CAS SSO改造步骤(1)
- matlab进行离散点的曲线拟合