UVA 10168 Summation of Four Primes(数论)
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(数论)的更多相关文章
- Summation of Four Primes - PC110705
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10168.html 原创:Summ ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- UVa 106 - Fermat vs Pythagoras(数论题目)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVA 10831 - Gerg's Cake(数论)
UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...
- UVA 12103 - Leonardo's Notebook(数论置换群)
UVA 12103 - Leonardo's Notebook 题目链接 题意:给定一个字母置换B.求是否存在A使得A^2=B 思路:随意一个长为 L 的置换的k次幂,会把自己分裂成gcd(L,k) ...
- UVa 1363 - Joseph's Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1640 - The Counting Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 11582 - Colossal Fibonacci Numbers!(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11246 - K-Multiple Free set(数论推理)
UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...
随机推荐
- 洛谷——P1104 生日
P1104 生日 题目描述 cjf君想调查学校OI组每个同学的生日,并按照从大到小的顺序排序.但cjf君最近作业很多,没有时间,所以请你帮她排序. 输入输出格式 输入格式: 有2行, 第1行为OI组总 ...
- Knockout.js(一):简介
Knockout是一款很优秀的JavaScript库,通过应用MVVM模式使JavaScript前端UI简单化.任何时候你的局部UI内容需要自动更新,KO都可以很简单的帮你实现,并且非常易于维护. K ...
- 下载好的AE模板怎么用
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 下载好的AE模板怎么用_百度经验
- 【后缀自动机】poj1509 Glass Beads
字符串最小表示 后缀自动机 O(n) 把串复制一次,链接在后面之后,建立SAM,贪心地在SAM上转移,每次贪心地选择最小的字符,转移的长度为n时停止. 输出时由于要最靠前的,所以要在endpos集合中 ...
- The Responsive jQuery Content Slider
jquery slider 效果 http://bxslider.com/ http://www.cnblogs.com/lhb25/archive/2012/08/13/jquery-image-e ...
- 简单理解SNAT回流中的概念:路由器怎么知道外网返回的数据是局域网中哪台主机的
内网到外网用的是NAT技术(地址封装)外网到内网用的是端口映射(PNAT)计算机的端口又65535(0-65534),你说的那些有名气的端口大多都是0-1023之间的你说的这个问题很简单,但首先你要懂 ...
- Android Logcat Security(转)
讲解了在Android开发中logcat使用不当导致的安全问题 原帖地址:http://drops.wooyun.org/tips/3812 0x00 科普 development version : ...
- arcgis10.2转shp文件中文乱码问题解决方案
从ArcGIS的数据源入手,自己升级ArcGIS for Desktop到10.2.0,然后用该版本ArcGIS软件重新导出数据,竟然还是乱码.经咨询ArcGIS技术支持,技术支持说必须保证shp文件 ...
- [转]解决Eclipse更新ADT插件时遇到的Eclipse reports rendering library more recent than ADT plug-in问题
使用 SDK Manager 工具更新下载新版本后,无法显示可视化布局,同时提示 This version of the rendering library is more recent than y ...
- tomcat+mysql在Kubernetes环境
基于PV作为交换目录将应用最终拷贝入/tomcat/webapps目录 进入Docker后,修改/bin/catalina.sh,加入jdbc的类 \webapps\mytestsql\WEB-INF ...