欢迎访问我的新博客: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. UVA 10168 Summation of Four Primes(数论)

    Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler p ...

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

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  3. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  4. 算法竞赛入门经典+挑战编程+USACO

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

  5. 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 ...

  6. Summation of primes

    是我算法不对,还是笔记本CPU太差? 我优化了两次,还是花了三四个小时来得到结果. 在输出上加1就是最终结果. The sum of the primes below 10 is 2 + 3 + 5 ...

  7. (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 ...

  8. Problem 10: Summation of primes

    def primeslist(max): ''' 求max值以内的质数序列 ''' a = [True]*(max+1) a[0],a[1]=False,False for index in rang ...

  9. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

随机推荐

  1. Git与TortoiseGit基本操作(转)

    1. GitHub操作 本节先简单介绍 git 的使用与操作, 然后再介绍 TortoiseGit 的使用与操作. 先看看SVN的操作吧, 最常见的是 检出(Check out ...), 更新 (U ...

  2. async和await关键字实现异步编程

    async和await关键字实现异步编程 异步编程   概念 异步编程核心为异步操作,该操作一旦启动将在一段时间内完成.所谓异步,关键是实现了两点:(1)正在执行的此操作,不会阻塞原来的线程(2)一旦 ...

  3. 阐述linux IPC(两):基于socket进程间通信(下一个)

    [版权声明:尊重原创.转载请保留源:blog.csdn.net/shallnet 要么 .../gentleliu,文章学习交流.请勿用于商业用途]     其中两个进程通信,有两个过程,以彼此的过程 ...

  4. HDU Today (图论)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  5. C++ friend 用法汇总

    C++这位朋友同意之类的非公共成员的机制是一个类或函数访问,根据朋友的类型分为三种类型:一般非类成员函数为好友,类成员函数为好友.类为好友. 1 内容朋友 包括报表朋友的朋友以及朋友的定义.明默的感觉 ...

  6. Blocks and Variables

    Blocks and Variables https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Blocks/A ...

  7. Android Animation 动画Demo(Frame帧动画)

    上一页介绍Animation动画第一:Tween吐温动画. 本文介绍了以下Animation也有动画的形式:Frame帧动画. Frame动画是一系列照片示出的顺序按照一定的处理,和机制,以放电影很阶 ...

  8. C# 使用 Code Snippet 简化 Coding

    在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符一个字符敲还是使用 Visual Studio 提 ...

  9. 谈论Hibernate级联删除——JPA根据Hibernate实现许多级联删除CascadeType.DELETE_ORPHAN

    声明: 1.这篇文章是原创.非复制或转载过来. 2.在本文中,参数都亲自做过实验证明. 3.这篇文章谈到了Hibernate配置基于注释的方法.hbm语法不可用. 不清JPA.Hibernate.EJ ...

  10. 乐视TV2015校园招聘A第二个大的发行量(of中国Academy科学大学站)

    标题叙述性说明:鉴于阵列A,尺寸n,数组元素1至n数字.但是一些数字多次出现.有些数字不出现.请设计算法和程序,统计数据不会出现什么.什么号码多次出现.在O(n)时间复杂度,O(1)在完成它的空间复杂 ...