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. 洛谷——P3908 异或之和

    P3908 异或之和 题目描述 求1 \bigoplus 2 \bigoplus\cdots\bigoplus N1⨁2⨁⋯⨁N 的值. A \bigoplus BA⨁B 即AA , BB 按位异或. ...

  2. ES5 方法学习

    Object 1. Object.getPrototypeOf(o)获取对象的prototype对象.等价于以前的o.__proto__ var o = {}; Object.getPrototype ...

  3. AGC 022 C - Remainder Game

    题面在这里! 显然权值是 2^i 这种的话就是要你贪心,高位能不选就不选. 并且如果 % x 之后再去 % 一个>=x的数是没有用的,所以我们可以把操作的k看成单调递减序列. 这样的话就是一个有 ...

  4. HDU 6039 Gear Up(线段树+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6039 [题目大意] 给出一些齿轮,有些齿轮是边相连,也就是拥有相同的线速度, 有的齿轮是轴相连,也 ...

  5. 【Huffman树贪心+优先队列】POJ3253-Fence Repair

    思路详见之前的贪心专题,用优先队列来代替之前的插入排序,效率为O(nlogn) #include<iostream> #include<cstdio> #include< ...

  6. 一段javascript设计模式应用场景

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  7. oracle增加表空间的四种方法

    1. 查看所有表空间大小 select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_nam ...

  8. 域名做CDN来通过隐藏服务器真实IP的方法来防止DDoS攻击(转)

    隐藏服务器真实IP是解决问题最好和最快的方法,但只针对小流量,大流量同样会扛不住. 服务器前端加CDN中转,比如阿里云.百度云加速.360网站卫士.加速乐.安全宝等,如果资金充裕的话,可以购买高防的盾 ...

  9. Arch linux

    这里将介绍Arch Linux使用问题的解决方法,在这里拿出来和大家分享一下Linux目前较适用于小型的网络,Arch Linux 是个针对 i686i686/x86-64 优化的 Linux 发行版 ...

  10. 第七章Openwrt安装服务器环境php+uhttpd+mysql

    在前面的文章中刷openwrt.配置网络环境.挂载u盘都配置成功了之后,下面的操作就变得简单起来!!!! 1. putty连接到路由器 2. 安装php opkg install php5-fastc ...