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 ...
随机推荐
- 12C 新特性--全库缓存
Force Full Database Caching Mode 意思就是可以把整个数据库缓存到内存中,当然你内存一定要非常大,起码要等于数据库的大小,才能容下整个数据库. 在RAC环境下,对于一个良 ...
- IaaS vs PaaS vs SaaS
在云计算的早期阶段,企业面临的最大问题是他们是否应该使用公共云服务.如今,几乎所有的组织都在采用一些公共云服务.更重要的问题是企业应该使用哪种云服务:基础设施即服务(IaaS),平台即服务(PaaS) ...
- SCSI, (P)ATA, SAS, NL-SAS and SATA, what’s the difference?
Everybody needs storage space nowadays. Whether it is used for high performance computing or simply ...
- Linux安装JRE tomcat配置java环境
安装JRE 到http://www.oracle.com/technetwork/java/javase/downloads/index.html下载JRE软件. 1.wget http://down ...
- Ehcache/Redis/Tair缓存性能对比[转载]
FW : http://xulingbo.net/?p=434 版权归许令波所有.此处只是一个转载. 后面介绍的不同方式都有测试数据,这些测试数据都是在同一的测试环境下得出的测试结果: 测试机器的配置 ...
- jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后 ...
- Python之二维数组N*N顺时针旋转90度
需求:把一个二维数组顺时针旋转90度,现实数据的替换. 比如把4*4的二维数组顺时针旋转90度 原始数据是一个嵌套列表:[['A', 'B', 'C', 'D'], ['A', 'B', 'C', ' ...
- krpano之鼠标样式修改
引入cursors.js. cursors.js代码: <krpano > <events onxmlcomplete="action(qtvrcursor);" ...
- Tornado 高并发源码分析之二---Tornado启动和请求处理流程
Tornado 服务器启动流程 因为Tornado 里使用了很多传类的方式,也就是delegate,之所以要这么做,其实和 iOS 开发那样,也很多的 delegate, 如此来实现高度解耦,但是比较 ...
- 【知识碎片】Asp.Net 篇
51.app.config 连接字符串 <?xml version="1.0" encoding="utf-8"?> <configurati ...