欢迎访问我的新博客: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. listener.ora中PLSExtPro 和ExtProc的作用(转)

    默认安装时,会安装一个PL/SQL外部程序(ExtProc)条目在listener.ora中,是oracle为调用外部程序默认配置的监听,它的名字通常是ExtProc或PLSExtProc,但一般不会 ...

  2. SRM 587 Div II L3:ThreeColorabilityEasyy

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12699 这道题目是第一次在比赛的时候做出来的,开始还想用bru ...

  3. 第1章 单例模式(Single Pattern)

    原文 第1章 单例模式(Single Pattern) 单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 一.常用模式: 1 ...

  4. SQL ROW_NUMBER() OVER函数的基本用法用法

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW ...

  5. 谷歌、火狐浏览器下实现JS跨域iframe高度自适应的完美解决方法,跨域调用JS不再是难题!

    谷歌.火狐浏览器下实现JS跨域iframe高度自适应的解决方法 导读:今天开发的时候遇到个iframe自适应高度的问题,相信大家对这个不陌生,但是一般我们都是在同一个项目使用iframe嵌套页面,这个 ...

  6. BZOJ 3689 异或 Trie木+堆

    标题效果:特定n的数量,这种需求n数22 XOR的值前者k少 首先,我们建立了一个二进制的所有数字Trie木,您可以使用Trie木size域检查出一些其他的数字XOR值首先k少 然后,我们要保持一个堆 ...

  7. request的setAttribute()怎么用的?

    request.setAttribute()怎么用的?JSP1代码String [] test=new String[2];test[0]="1";test[1]="2& ...

  8. Linux内核源代码情景分析-中断半

    一.中断初始化 1.中断向量表IDT初始化 void __init init_IRQ(void) { int i; #ifndef CONFIG_X86_VISWS_APIC init_ISA_irq ...

  9. java环境变量配置四种方法

    原文:java环境变量配置四种方法 Java编程首要工作就是安装JDK(Java Development Kit).一通“NEXT”点完安装后就是最重要的环境变量设置了.也许有人会问为什么要设置环境变 ...

  10. Web API-属性路由

    路由(Routing)就是Web API如何将一个URI匹配到一个action的过程.Web API 2 支持一个新的路由方式-属性路由(attribute routing).顾名思义,属性路由使用标 ...