Summation of Four Primes - PC110705
来源:互联网 发布:佣金宝交易软件 编辑:程序博客网 时间:2024/05/15 06:28
欢迎访问我的新博客: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
243646
Sample Output
3 11 3 73 7 13 1311 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
- summation of four primes
- 110705 Summation of Four Primes
- 10168 Summation of Four Primes
- uva 10168 summation of four primes
- Uva 10168 Summation of Four Primes 素数
- UVA-10168 Summation of Four Primes
- UVa 10168 - Summation of Four Primes
- Summation of Four Primes(歌德巴赫猜想)
- Uva 10168 - Summation of Four Primes
- UVa 10168 Summation of Four Primes
- UVa Problem Solution: 10168 - Summation of Four Primes
- PC/UVa 110705/10168 Summation of Four Primes
- (数论2.1.2)UVA 10168Summation of Four Primes(欧拉筛法)
- UVA 10168 Summation of Four Primes(数论)
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVaOJ-10168-Summation of Four Primes 解题报告
- rman backup scripts
- 什么是web
- c++ 的前世今生
- 微软实习生第一题
- override 与overload 的区别
- Summation of Four Primes - PC110705
- 《C++ 沉思录》阅读笔记——句柄类
- pushScene,popScene,replaceScen切换场景之间的区别
- [LeetCode] ZigZag Conversion
- WebService学习(一)
- Ubuntu环境下配置J2EE开发环境jdk+tomcat+mysql+myeclipse
- 数位 dp HDU 2089 不要62
- JAVA的虚拟机
- Android Performance(7) Investigating Your RAM Usage