欢迎访问我的新博客:http://www.milkcu.com/blog/

原文地址:http://www.milkcu.com/blog/archives/uva10168.html

原创:Summation of Four Primes - PC110705

作者:MilkCu

题目描述

Summation of Four Primes

 

Waring's prime number conjecture states that every odd integer is either prime or the sum of three primes. Goldbach's conjecture is that every even integer is the sum of two primes. Both problems have been open for over 200 years.

In this problem you have a slightly less demanding task. Find a way to express a given integer as the sum of exactly four primes.

Input

Each input case consists of one integer n ( n10000000) on its own line. Input is terminated by end of file.

Output

For each input case n, print one line of output containing four prime numbers which sum up to n. If the number cannot be expressed as a summation of four prime numbers print the line ``Impossible." in a single line. There can be multiple solutions. Any good solution will be accepted.

Sample Input

24
36
46

Sample Output

3 11 3 7
3 7 13 13
11 11 17 7

解题思路

该题假定题目给出的两个猜想是正确的。

若n <= 7,则n不可能拆分为4个素数之和;

若n >= 8,
当n为偶数时,
n - 2 - 2为偶数,可写成两偶数的和,
所以n可以写成2和2,还有两个质数的和;
当n为奇数时,
n - 2 - 3为偶数,可写成两偶数的和,
所以n可以写成2和3,还有两个质数的和。

代码实现

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int isPrime(int x) {int s = sqrt(x);for(int i = 2; i <= s; i++) {if(x % i == 0) {return 0;}}return 1;
}
void twopart(int x, int & a, int & b) {for(int i = 2; i <= x / 2; i++) {if(isPrime(i) && isPrime(x - i)) {a = i;b = x - i;return;}}return;
}
int main(void) {int n;while(cin >> n) {if(n <= 7) {cout << "Impossible." << endl;continue;}if(n % 2) {int a, b;twopart(n - 2 - 3, a, b);cout << "2 3 " << a << " " << b << endl;} else {int a, b;twopart(n - 2 - 2, a, b);cout << "2 2 " << a << " " << b << endl;}}return 0;
}

(全文完)

本文地址:http://blog.csdn.net/milkcu/article/details/23599369

Summation of Four Primes - PC110705相关推荐

  1. UVA10168 Summation of Four Primes【筛选法】

    Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every ...

  2. 题解:Summation of Four Primes(整数分解四个素数和)

    每个大于4的整数可以以转换为两个素数和 Euler proved in one of his classic theorems that prime numbers are infinite in nu ...

  3. UVA - 10168 Summation of Four Primes(哥德巴赫猜想)

    题目链接:点击查看 题目大意:给出一个n,若能将其分解成四个质数之和,请分解,否则输出Impossible. 题目分析:首先我们知道,最小的质数是2,四个2之和是8,所以当n小于8的时候肯定无解 其次 ...

  4. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  5. π-Algorithmist分类题目(1)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(1) Sorting UVAL ...

  6. ICPC程序设计题解书籍系列之八:(美)斯基纳等:《挑战编程-程序设计竞赛训练手册》

    S书<挑战编程--程序设计竞赛训练手册>题目一览 1 Getting Started UVA100 POJ1207 HDU1032 The 3n + 1 problem[水题] - 海岛B ...

  7. ICPC程序设计题解书籍系列之六:吴永辉:《算法设计编程实验》

    第1章 Ad Hoc问题 POJ2661 HDU1141 ZOJ2545 UVA10916 Factstone Benchmark[Ad Hoc] UVA10037 Bridge[贪心] POJ257 ...

  8. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  9. UVa Online Judge 工具網站

    UVa Online Judge 工具網站 转自http://www.csie.ntnu.edu.tw/~u91029/uva.html Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 M ...

最新文章

  1. 从计算机视觉(slam)和摄影测量两个维度进行BA算法原理推导
  2. 备份oracle中的大表
  3. 戴维南定律和诺顿定律
  4. TextView显示不同颜色的文本,及文本变色可点击工具类
  5. CF464E The Classic Problem(主席树+哈希+最短路)
  6. java 注解 方法 参数_java在注解中绑定方法参数的解决方案
  7. javaweb中mysql数据库的回滚操作代码
  8. myeclipse 添加mysql数据库_myeclipse添加数据库
  9. 3.15 曝光:40 亿 AI 骚扰电话和 11 家合谋者
  10. [转载] python difference用法_set.difference() 的用法(python3)_python3 set集合,三元运算以及
  11. 0. Unix / Linux操作系统介绍
  12. NumberFormat的使用
  13. Cypress USB 芯片固件修改,改序列号(Serial Number)
  14. jde多目标_CVPR 2020 多目标跟踪算法JDE 训练
  15. html5 电子白板 直播,基于HTML5的电子白板的设计与实现
  16. 驾考通-小型汽车考试
  17. 主流实时流处理计算框架Flink初体验
  18. java数据类型面试题
  19. 【linux kernel】一文总结initramfs的使用
  20. password_hash/password_verify/(JAVA)

热门文章

  1. 图像隐写分析-Markov特征编程实现
  2. 曾经的豪言壮语,如今的喟然长叹
  3. A与B地相距n米,一个人第一步只能前进1米或者后退1米,第二步只能前进2米或者后退2米,A走到B最少需要几步
  4. linux3.5 usb 设备树,嵌入式 PowerPC Linux 平台扁平设备树FDT解析
  5. 李宏毅(2020)作业9:无监督学习降维、聚类、自编码
  6. python3爬取有道云翻译
  7. 学习笔记9-DHT11
  8. 网络安全保险的新兴发展与监管研究
  9. 打开量化的“黑箱”:阿尔法模型
  10. 企业大数据的八个典型应用