1356. Something Easier

Time limit: 1.0 second
Memory limit: 64 MB
“How do physicists define prime numbers? Very easily: prime numbers are the number 2 and all the odd numbers greater than 2. They may show that this definition corresponds to the mathematical one: 3 is prime, 5 is prime, 7 is prime… 9? 9 is certainly not prime. Then: 11 is prime, 13 is prime. So 9 is the experiment mistake.”
From mathematical analysis course
Once physicist and mathematician argued how many prime numbers one needed for the purpose that their sum was equal to N. One said that it wasn’t known and the other that 3 was always enough. The question is how many.

Input

The first line contains T, an amount of tests. Then T lines with integer N follow (0 ≤ T ≤ 20; 2 ≤ N ≤ 109).

Output

For each test in a separate line you should output prime numbers so that their sum equals to N. An amount of such prime numbers is to be minimal possible.

Sample

input output
7
2
27
85
192
14983
3
7
2
23 2 2
2 83
11 181
14983
3
7

题意

一位物理学家和一位数学家正在争论最少几个质数的和为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(数论,哥德巴赫猜想)的更多相关文章

  1. URAL 1356. Something Easier(哥德巴赫猜想)

    题目链接 题意 : 给你一个数n,让你找出几个素数,使其相加为n,输出这些素数. 思路 : 哥德巴赫猜想 : 任何一个大于 6的偶数都可以表示成两个素数之和. 任何一个大于9的奇数都可以表示成三个素数 ...

  2. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  3. CF735D Taxes 哥德巴赫猜想\判定素数 \进一步猜想

    http://codeforces.com/problemset/problem/735/D 题意是..一个数n的贡献是它的最大的因子,这个因子不能等于它本身 然后呢..现在我们可以将n拆成任意个数的 ...

  4. *CF2.D(哥德巴赫猜想)

    D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  5. C#实现哥德巴赫猜想

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Goet ...

  6. 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 ...

  7. Codeforces 735D:Taxes(哥德巴赫猜想)

    http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得 ...

  8. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  9. 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 ...

随机推荐

  1. js中的错误检测

    <!DOCTYPE html> <html> <body> <script> function myFunction() { try { var x=d ...

  2. 【转】【Egit】如何将eclipse中的项目上传至Git

    1.下载egit插件 打开Eclipse,git需要eclipse授权,通过网页是无法下载egit的安装包的.在菜单栏依次打开eclipse→help→install new software→add ...

  3. java 调用打印机 打印服务

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import javax ...

  4. DFS - leetcode [深度优先遍历]

    最短路径=>BFS    所有路径=>DFS 126. Word Ladder II BFS+DFS: BFS找出下一个有效的word进队 并记录step 更新两个变量:unordered ...

  5. NTFS 读写高手进阶 - Windows 格式硬盘 Mac存文件

    常识:硬盘格式:FAT32 - WIndows 硬盘分区格式, 有点通用性高, 缺点不支持单个大于 4G 的文件. exFAT - Windows 硬盘分区格式, 兼容性低. 稳定性不如 FAT32. ...

  6. from表单实现无跳转上传文件,接收页面后台数据。

    本文基于我刚写的http://www.cnblogs.com/iwang5566/p/6287529.html进行了简单的扩展,实现页面无跳转表单数据提交,并接收后台返回的数据. 下载好,上一篇文章d ...

  7. java回调机制(写的很好)

    本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273) 以前不理解什么叫回调,天天听人家说加一个回调方法啥的 ...

  8. SpringMVC通过注解获得参数

    SpringMVC可以通过RequestParam注解来映射获得参数,具体用法如下: 例子: 配置过程省略 1.新建controller类 package com.loger.controller; ...

  9. mysql 用户权限设置

    windows使用的zip包的mysql.配置成功后,root用户没有权限,需要进行权限分配.管理员运行cmd,执行以下命令,进入mysql. mysql -u root -p Passwd Pass ...

  10. openstack私有云布署实践【4.1 上层代理haproxy配置 (科兴环境)】

    官方文档上的高可用配置,它推荐的是使用haproxy的上层代理来实现服务组件的主备访问.或者负载均衡访问   一开始我也是使用haproxy来做的,但后来方式改了   测试环境:haproxy + n ...