Summation of Four Primes

Input: standard input

Output: standard output

Time Limit: 4 seconds

Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four positive primes? I don’t know the answer. May be you can help!!! I want your solution to be very efficient as
I have a 386 machine at home. But the time limit specified above is for a Pentium III 800 machine. The definition of prime number for this problem is “A prime number is a positive number which has exactly two distinct integer factors”. As for example 37 is
prime as it has exactly two distinct integer factors 37 and 1.

Input

The input contains one integer number N (N<=10000000) in every line. This is the number you will have to express as a summation of four primes. Input is terminated by end of file.

Output

For each line of input there is one line of output, which contains four prime numbers according to the given condition. 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。是否能找出四个素数使得它们的和恰好是n,假设找不到。输出 “Impssible.";

分析:由于最小的素数是2,所以当n<8时无解;又一个偶数能够写成两个素数的和,所以当n>=8时,能够先把n变成一个偶数,然后找两个素数使得它们的和恰好是那个偶数就可以。

#include<stdio.h>
#include<string.h>
const int MAXN = 10000005;
int vis[MAXN], prime[700000], num; void get_prime()
{
num = 0;
memset(vis, 0, sizeof(vis));
vis[0] = vis[1] = 1;
for(int i = 2; i < MAXN; i++)
{
if(!vis[i])
{
prime[num++] = i;
for(int j = i + i; j < MAXN; j += i)
vis[j] = 1;
}
}
} int main()
{
int n;
get_prime();
while(~scanf("%d",&n)) {
if(n < 8) {
printf("Impossible.\n");
continue;
}
if(n&1) {
printf("2 3 ");
n -= 5;
}
else {
printf("2 2 ");
n -= 4;
} //n已变成偶数,找两个素数使得它们的和恰好是n
for(int i = 0; i < num; i++)
if(!vis[n-prime[i]]) {
printf("%d %d\n", prime[i], n-prime[i]);
break;
}
}
return 0;
}

UVA 10168 Summation of Four Primes(数论)的更多相关文章

  1. Summation of Four Primes - PC110705

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10168.html 原创:Summ ...

  2. UVA.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  3. UVa 106 - Fermat vs Pythagoras(数论题目)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  4. UVA 10831 - Gerg&#39;s Cake(数论)

    UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...

  5. UVA 12103 - Leonardo&#39;s Notebook(数论置换群)

    UVA 12103 - Leonardo's Notebook 题目链接 题意:给定一个字母置换B.求是否存在A使得A^2=B 思路:随意一个长为 L 的置换的k次幂,会把自己分裂成gcd(L,k) ...

  6. UVa 1363 - Joseph's Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. UVa 1640 - The Counting Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVa 11582 - Colossal Fibonacci Numbers!(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA 11246 - K-Multiple Free set(数论推理)

    UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...

随机推荐

  1. shell脚本报错退出

    在shell脚本中,比如有以下的代码: cd /root/test88 rm -rf  backup 如果目录/root/test88不存在,脚本不会停止,依然会执行rm -rf backup这个命令 ...

  2. MC资源整理

    MC模拟简介 蒙特卡罗模拟,因摩纳哥著名的赌场而得名.它能够帮助人们从数学上表述物理.化学.工程.经济学以及环境动力学中一些非常复杂的相互作用. 蒙特卡罗(Monte Carlo)方法,又称随机抽样或 ...

  3. [BZOJ1860][ZJOI2006]Mahjong(DP)

    1860: [Zjoi2006]Mahjong麻将 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 412  Solved: 248[Submit][Sta ...

  4. JZYZOJ 1382 光棍组织 状压dp

    http://172.20.6.3/Problem_Show.asp?id=1382   水得过分了,本来以为要用lzx学长的写法写,抱着试试看的想法写了个特暴力的dp+dfs,过了,真是...   ...

  5. 【树形dp】Computer

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 使用redis时出现java.util.ArrayList cannot be cast to java.lang.Long

    java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long at redis.clients. ...

  7. lnmp配置信息 4核8g优化

    MYSQL   my.conf# The following options will be passed to all MySQL clients[client]#password       = ...

  8. DN安卓2014版(5-9)

    DN安卓2014版(5-9) 联系2g32@sina.com

  9. Linux Shell编程与编辑器使用详解

    <Linux Shell编程与编辑器使用详解> 基本信息 作者: 刘丽霞 杨宇 出版社:电子工业出版社 ISBN:9787121207174 上架时间:2013-7-22 出版日期:201 ...

  10. OpenShift应用镜像构建(4) - fabric8-maven-plugin

    适合开发的构建fabric8-maven-plugin 在项目过程中越来越多的出现在开发阶段就需要把部分微服务直接做容器化发布,然后自己的代码还需要和这些发布后的微服务进行调用的开发过程,这个阶段基本 ...