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. Oracle常用常考集合

    登陆远程服务器 sqlplus scott/tiger@192.168.2.1[:port]/sid [as sysdba] 简单查询 select  table_name from user_tab ...

  2. Java-并发入门

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ Java中实现多线程的方法 实现Runnable接口 实现Runnable接口里的run()方法 ...

  3. 【BZOJ 4513】【SDOI 2016】储能表

    http://www.lydsy.com/JudgeOnline/problem.php?id=4513 设\(f(i,0/1,0/1,0/1)\)和\(g(i,0/1,0/1,0/1)\)分别表示d ...

  4. 【动态规划技巧题】POJ2229-Sumsets

    [题目大意] 把一个数n分成2的指数幂相加的形式,问有几种情况. [思路] 如果当前i为奇数,则必定有至少一个1,可以看作i-1的情形再加上一个1.即f[i]=f[i-1]. 如果当前i为偶数,假设没 ...

  5. 1.3(SQL学习笔记)计算字段及函数

    一.计算字段 1.1拼接字段 一般情况下返回的字段是指定列的属性名.如果有时我们对返回格式有特殊要求. 例如,我们需要将显示商品名,即商品价格,同时商品名后面的价格放在括号内. prod_name(p ...

  6. Problem D: 零起点学算法95——弓型矩阵

    #include<stdio.h> #include<string.h> int main() { ][]; while(scanf("%d%d",& ...

  7. Java高级架构师(一)第32节:Nginx的进程结构、基本配置

    核心模块.事件模块.标准Http模块.可选Http模块.邮件模块.第三方模块和补丁.

  8. #Html学习积累#分割线中间添加文字

    类似效果: ————————————xxxxx———————————————————— <!DOCTYPE HTML> <html> <head> <meta ...

  9. Oracle数据库查看用户状态

    一.当前ORACLE用户的状态可查看视图DBA_USERS;一般情况下在使用的正常用户均处于OPEN状态. 1 SQL> select username,account_status from  ...

  10. 万里长征第二步——django个人博客(第二步 ——日志记录器)

    定义日志记录器 可以在setting.py里设置日志记录器 # 自定义日志输出信息 LOGGING = { 'version': 1, 'disable_existing_loggers': True ...