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. 【android错误】bitmap size exceeds 32bits

    使用图片缩放时遇到这么个问题: java.lang.IllegalArgumentException: bitmap size exceeds 32bits 后来一行行查代码,发现原来是 scale ...

  2. LINQ_to_SQL语法及实例大全

    LINQ To SQL 语法及实例大全 2011/4/5 目录 LINQ to SQL语句(1)之Where. 2 Where操作... 2 1.简单形式:... 2 2.关系条件形式:... 2 3 ...

  3. Struts2 网站上来多少人

    strut.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBL ...

  4. dll间接应用问题

    在项目prj引用一个dll,a.dll,时,此dll应用b.dll 此时不将b.dll引用添加到prj,会有问题

  5. 关于最新版本的log4net使用中遇到的问题

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

  6. Mybatis的传参

    最近重新温习了遍Mybatis ,觉得还是汇总一下比较好,方便自己以后的快速开发 最终要的一点事,自己写的话,记忆更加深刻: 首先自己先写了个静态块,防止代码冗余: private static Sq ...

  7. 在CDlinux下编译安装无线网卡驱动

    环境 主机:ThinkPadT440P 系统:CDlinux9.7.1 概述 准备利用CDlinux来破解周围的wifi密码来免费蹭网,由于笔记本是新买的,系统没有自带驱动,只能自己手动到网上下载.编 ...

  8. ASP.NET的三层架构(DAL,BLL,UI)

    ASP.NET的三层架构(DAL,BLL,UI) BLL   是业务逻辑层   Business   Logic   Layer DAL   是数据访问层   Data   Access   Laye ...

  9. HDU 5860 Death Sequence

    用线段树可以算出序列.然后o(1)询问. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...

  10. Windows上安装MongoDB步骤

    事前准备: 1.在mongoDB官网下载.msi文件,我下的是社区版,下载地址:https://www.mongodb.com/download-center#community 2.点击msi文件安 ...