ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier
Memory limit: 64 MB
Input
Output
Sample
input | output |
---|---|
7 |
2 |
题意
一位物理学家和一位数学家正在争论最少几个质数的和为N。
其中一个说这无从知晓,另一个说3个就够了。
input
第一行包含一个整数T,表示测试点数量。
接下来T行,每行一个整数N。
(0<=T<=20,2<=N<=10^9)
output
输出一些质数,使它们的和为N,质数的个数要尽量少。
思路:根据哥德巴赫猜想,
任一大于2的偶数都可写成两个质数之和。
任一大于7的奇数都可写成三个素数之和。
详细内容可参照维基百科http://zh.wikipedia.org/wiki/%E5%93%A5%E5%BE%B7%E5%B7%B4%E8%B5%AB%E7%8C%9C%E6%83%B3。
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <set>
#include <deque>
#include <bitset>
#include <functional>
#include <utility>
#include <iomanip>
#include <cctype>
using namespace std; #define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define max(a,b) ((a) > (b)) ? (a) : (b)
#define min(a,b) ((a) < (b)) ? (a) : (b)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x) typedef long long LL;
typedef vector<int> VI; const int MAXN = ;
const int hash_size = ;
const int INF = 0x7f7f7f7f; bool p[MAXN]={}, flag;
int prime[MAXN]={}, q[], n, d;
void init()
{
int i;
for(i = ; i <= ; i++)
if(!p[i])
{
prime[]+=;
prime[prime[]]=i;
for(int j=i+i;j<=;j+=i)
p[j]=true;
}
} bool isprime(int n){//判断n是否是一个质数
if (n == )
return true;
else {
int sq, i;
sq = int(sqrt(n*1.0));
for (i = ; i <= sq+; ++i)
if (n%i == )
return false;
return true;
}
} void dfs(int k,int x,int y)
{//将奇数进行分解
int i;
if (flag) return;
if (k == )
{
if (isprime(x))
{
FORD(i, d, )
printf("%d ", prime[q[i]]);//进行输出
printf("%d\n", x);
flag = true;//找到了一个分解
}
return;
}
for (i = y; i<=prime[]; ++i)
{
if (prime[i]*k > x) return;
q[k] = i;
dfs(k-, x-prime[i], i);
}
} int main()
{
init();
int t, i;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
if (isprime(n))
printf("%d\n", n);
else if (n&) {
d = ;
flag = false;
while (!flag)
dfs(++d, n, );//先分2分,再分3个
}
else {
int tmp;
FORE(i, , prime[]) {
tmp = n - prime[i];
if (isprime(tmp)) {
printf("%d %d\n", prime[i], tmp);
break;
}
}
}
}
return ;
}
ural 1356. Something Easier(数论,哥德巴赫猜想)的更多相关文章
- URAL 1356. Something Easier(哥德巴赫猜想)
题目链接 题意 : 给你一个数n,让你找出几个素数,使其相加为n,输出这些素数. 思路 : 哥德巴赫猜想 : 任何一个大于 6的偶数都可以表示成两个素数之和. 任何一个大于9的奇数都可以表示成三个素数 ...
- 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拆成任意个数的 ...
- *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 ...
- 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 ...
随机推荐
- CodeForces 672D Robin Hood
思维. 当$k$趋向于正无穷时,答案会呈现出两种情况,不是$0$就是$1$.我们可以先判断掉答案为$1$和$0$的情况,剩下的情况都需要计算. 需要计算的就是,将最小的几个数总共加$k$次,最小值最大 ...
- 1、<img />标签
alt:当图片不显示时的文字说明 title:鼠标悬停在图片上的出现的文字说明
- sqlserver 操作xml
1.xml.exist 输入为XQuery表达式,返回0,1或是Null.0表示不存在,1表示存在,Null表示输入为空 2.xml.value 输入为XQuery表达式,返回一个SQL ...
- UIBezierPath与CAShapeLayer结合画扇形
/*让半径等于期望半径的一半 lineWidth等于期望半径 就可以画圆*/ 可以看出layer的走势是从圆边的中间一半在圆外 一半在圆内 因此让半径等于期望半径的一半 lineWidth等于期望半径 ...
- 阿里云-对象储存OSS
最近 需要搞一个阿里云的文件上传 手机端主要做了图片上传 pod安装:pod 'AliyunOSSiOS', '~> 2.5.2' 主要就是需要四个参数:accessKey secretKey ...
- IE8下 Select文字垂直居中的办法
.select { padding: 4px 0; height: 30px; line-height: 26px; vertical-align: middle;} 处理前: 处理后:
- c++字体对齐
字节对齐的细节和编译器实现相关,但一般而言,满足三个准则: 1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除. 2) 结构体每个成员相对于结构体首地址的偏移量(offset)都是 ...
- UI_APPEARANCE_SELECTOR 延伸
iOS后属性带UI_APPEARANCE_SELECTOR 可以统一设置全局作用 例如: 1>开关控件 @property(nullable, nonatomic, strong) UIColo ...
- spring No adapter for handler 错误
为了实现controller对JSON数据的自动解析,在spring-mvc.xml文件中增加了 <bean class="org.springframework.web.servle ...
- redo log
1.redo log相关数据字典 v$log:display the redo log file information from the control file v$logfile:identif ...