URAL 1356. Something Easier(哥德巴赫猜想)
题意 : 给你一个数n,让你找出几个素数,使其相加为n,输出这些素数。
思路 :
哥德巴赫猜想 :
任何一个大于 6的偶数都可以表示成两个素数之和。
任何一个大于9的奇数都可以表示成三个素数之和。
而在该题中,偶数中2本身就是个素数,奇数中小于9的都是素数,所以只要写一个判断素数的函数即可,这样不在范围内的数就可以直接判断输出了。
任何一个整数N(N>=2)最多由三个素数相加构成。要分情况考虑:
1. 如果N为偶数,1)如果N==2,直接输出;
2)如果N>2,那么N一定可以写成两个素数的和;
2.如果N为奇数,1)如果N自身就是素数,则直接输出;
2)如果N由两个素数构成,这两个素数只可能是:2 和 N-2;
3)N为三个素数之和。
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std ; bool prime(int n)
{
for(int i = ; i * i <= n ; i++)
{
if(n % i == ) return false ;
}
return true ;
}
int main()
{
int T,n ;
scanf("%d",&T) ;
while(T--)
{
scanf("%d",&n) ;
if(n % == )
{
if(n == )
printf("2\n") ;
// else if(n == 4) printf("2 2\n") ;
else{
for(int i = ; i <= n ; i += )
{
if(prime(i) && prime(n-i))
{
printf("%d %d\n",i,n-i) ;
break ;
}
}
}
}
else
{
if(prime(n)) printf("%d\n",n) ;
else if(prime(n-)) printf("2 %d\n",n-) ;
//else if(prime(n-4)) printf("2 2 %d\n",n-4) ;
else
{
bool flag = false ;
for(int i = ; i <= n ; i += )
{
for(int j = ; j <= n ; j += )
{
if(prime(i) && prime(j) && prime(n-i-j))
{
printf("%d %d %d\n",i,j,n-i-j);
flag = true ;
break;
}
}
if(flag) break ;
}
}
}
}
return ;
}
URAL 1356. Something Easier(哥德巴赫猜想)的更多相关文章
- ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...
- *CF2.D(哥德巴赫猜想)
D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- C#实现哥德巴赫猜想
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Goet ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- CF735D Taxes 哥德巴赫猜想\判定素数 \进一步猜想
http://codeforces.com/problemset/problem/735/D 题意是..一个数n的贡献是它的最大的因子,这个因子不能等于它本身 然后呢..现在我们可以将n拆成任意个数的 ...
- Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想
D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...
- Codeforces 735D:Taxes(哥德巴赫猜想)
http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得 ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想
D. Dima and Lisa Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...
随机推荐
- javax.servlet-api-xx.jar和servlet-api.jar区别
一.简介支持servlet的jar包.应该叫servlet-api.jar如果编写过servlet就知道要用到HttpServletRequest和HttpServletResponse等对象,这些对 ...
- Python 函数 -slice()
功能: slice() 函数实现切片对象,主要用在切片操作函数里的参数传递.返回一个切片对象. 语法: class slice(stop) class slice(start, stop[, step ...
- tomcat启动报错:java.lang.NoClassDefFoundError
tomcat启动加载spring配置文件时报错,找不到类GetBooksRequest,经排查实际上该类已经存在.后来发现日志里还有一句: This is very likely to create ...
- TableView刷新 局部刷新等
1.对整个页面刷新 [ tableView reloadData]; 2.对某一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithI ...
- php-fpm.conf 配置文件详解
php-fpm.conf 配置文件详解 [global] pid = run/php-fpm.pid error_log = log/php-fpm.log log_level = notice # ...
- 多线程设计模式(二):Future模式
一.什么是Future模型: 该模型是将异步请求和代理模式联合的模型产物.类似商品订单模型.见下图: 客户端发送一个长时间的请求,服务端不需等待该数据处理完成便立即返回一个伪造的代理数据(相当于商品订 ...
- 2012_p2 寻宝 (treasure.cpp/c/pas)
2012_p2 寻宝 (treasure.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 23 解决: 9[提交][状态][讨论版][命题人:外部导入] 题目描述 2 ...
- 1140 Look-and-say Sequence
题意:略 思路:第1个数是1(读作“1有1个”),因此第2个数就是11(读作“1有2个”),因此第3个数就是12(读作“1有1个,2有1个”),因此第4个数是1121(读作“1有2个,2有1个,1有1 ...
- python's twenty-third day for me 面向对象进阶
普通方法:对象和类绑定的过程. class A: def func1(self):pass def func2(self):pass def func3(self):pass def func4(se ...
- Android 4 学习(13):Local Broadcast Manager & Intent Filter
参考<Professional Android 4 Development> Local Broadcast Manager 简介 Local Broadcast Manager由Andr ...