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. 并发系列4-大白话聊聊Java并发面试问题之公平锁与非公平锁是啥?【石杉的架构笔记】

  2. RabbitMQ (九) 消息的参数详解

    上篇文章讲了声明一个队列时的参数设置,这篇文章主要说一说发布消息时的参数设置. 发布消息时的完整入参是这样的: channel.BasicPublish ( exchange: "test_ ...

  3. ACM的奇计淫巧_bitset优化

    什么是bitset bitset 是STL库中的二进制容器,根据C++ reference 的说法,bitset可以看作bool数组,但优化了空间复杂度和时间复杂度,并且可以像整形一样按位与或. 使用 ...

  4. Meeting Rooms II -- LeetCode

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. http请求及缓存框架 GalHttprequest

    GalHttprequest 是一个android平台上一个轻量级的http网络请求及缓存框架.当前GalHttpRequest支持以下功能: 同步请求Stirng.InputStream.Bitma ...

  6. 基于指定文本的百度地图poi城市检索的使用(思路最重要)

    (转载请注明出处哦)具体的百度地图权限和apikey配置以及基础地图的配置不叙述,百度地图定位可以看这个链接的http://blog.csdn.net/heweigzf/article/details ...

  7. Email the output of a concurrent program as Attachment

    This article illustrates the steps to be followed to Email a concurrent program's output. Write a pr ...

  8. Android内存优化13 内存泄漏常见情况4 资源泄漏

    资源未关闭或释放导致内存泄露 在使用IO.File流或者Sqlite.Cursor等资源时要及时关闭.这些资源在进行读写操作时通常都使用了缓冲,如果及时不关闭,这些缓冲对象就会一直被占用而得不到释放, ...

  9. maven本地仓库地址的设置

    对于大公司的jenkins来说,仓库是很大的,那么存储仓库的目录空间一定要足够大才可以. 可以对linux进行外挂,实现磁盘扩容,把仓库挂在外挂上. 默认情况下,mvn的配置文件在~/.m2/sett ...

  10. [NS2]TCL语言基本语法

    (来自:<NS2仿真实验-多媒体和无线网络通信>) 1. 变量(Variable)和变量替换(Variable Substitution) tcl变量是在第一次使用set的指令来指派变量的 ...