Summation of Four Primes - PC110705
欢迎访问我的新博客:http://www.milkcu.com/blog/
原文地址:http://www.milkcu.com/blog/archives/uva10168.html
原创:Summation of Four Primes - PC110705
作者:MilkCu
题目描述
|
![]() |
||||
![]() |
![]() |
![]() |
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的更多相关文章
- UVA 10168 Summation of Four Primes(数论)
Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler p ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- projecteuler Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two milli ...
- Summation of primes
是我算法不对,还是笔记本CPU太差? 我优化了两次,还是花了三四个小时来得到结果. 在输出上加1就是最终结果. The sum of the primes below 10 is 2 + 3 + 5 ...
- (Problem 10)Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two milli ...
- Problem 10: Summation of primes
def primeslist(max): ''' 求max值以内的质数序列 ''' a = [True]*(max+1) a[0],a[1]=False,False for index in rang ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- poj 2804 字典 (特里 要么 快排+二分法)
2804:词典 总时间限制: 3000ms 内存限制: 65536kB 描写叙述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.只是幸运的是,你有一本词典能够帮助你. 输入 首先输 ...
- 允许Android随着屏幕转动的控制自由转移到任何地方(附demo)
在本文中,Android ViewGroup/View流程,及经常使用的自己定义ViewGroup的方法.在此基础上介绍动态控制View的位置的三种方法,并给出最佳的一种方法. 一.ViewGroup ...
- String.Split()功能
我们在过去的教训 String.Join功能(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx).当中用到了String.SPli ...
- Entity Framework执行Sql语句返回DataTable
Entity Framework中对外开放了数据库连接字符串,使用的时候可以直接得到这个连接字符串,然后进行相关的操作.如果在使用的过程中,发现Entity Framework中有一些满足不了的需求的 ...
- easyui datagrid 单元格编辑 自动聚焦 、全选
$.extend($.fn.datagrid.methods, { editCell: function (jq, param) { return jq.each(function () { var ...
- Oracle性能分析11:系统统计信息
早期Oracle查询优化器的开销计算是基于运行SQL语句所须要的物理读,这种方法被叫做I/O开销模式(I/O cost model),这种方法的主要缺点是觉得单块读和多块读开销相当.在Oracle 8 ...
- 当今最流行的Node.js应用开发框架简介
快速开发而又容易扩展,高性能且鲁棒性强.Node.js的出现让所有网络应用开发者的这些梦想成为现实.但是,有如其他新的开发语言技术一样,从头开始使用Node.js的最基本功能来编写代码构建应用是一个非 ...
- Monkey源码分析之事件注入
本系列的上一篇文章<Monkey源码分析之事件源>中我们描述了monkey是怎么从事件源取得命令,然后将命令转换成事件放到事件队列里面的,但是到现在位置我们还没有了解monkey里面的事件 ...
- ASPX的Timer位置没放正确,导致整页刷新,而不是UpdatePanel里的内容刷新。
提示:Timer应该放在UpdatePanel的ContentTemplate标签里,才行.放在外面的话,会导致整页刷新.
- WebStorm荣获InfoWorld2014年度科技奖
InfoWorld年度科技奖是每年一月由InfoWorld评论家对过去一年的表现最好的信息产品的褒奖.产品包括硬件.软件.开发工具和云服务等. InfoWorld2014年度科技奖,包括35个获奖产品 ...