Hzu_5.F.Something Easier

http://vjudge.net  hzu5  F题

1356. Something Easier

Time limit: 2.0second

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 neededfor the purpose that their sum was equal toN. One said that itwasn’t known and the other that 3 was always enough. The question is how many.

Input

The first line containsT, an amount of tests. ThenTlineswith integerNfollow (0 ≤T≤ 20; 2 ≤N≤109).

Output

For each test in a separate line you should output prime numbers so thattheir sum equals toN. An amount of such prime numbers is to beminimal possible.

Sample

input

output

7

2

27

85

192

14983

3

72

23 2 2

2 83

11 181

14983

3

7

题目大意:

给出一个数,求出此数可以由素数相加而成,并且素数个数是最小的;

输入:

第一行:案例个数n,不超过20个

第二行至第n+1行:一个数,2至109个

输出:

第二行至第n+1行:几个数字,且满足都为质数,质数之和为该案例的数字。

解题思路:

数学定理:哥德马赫猜想:对于所有偶数,都可拆成2个素数之和;对于所有奇数,大部分都可拆成3个素数之和;

所以奇数情况要另外分析:

1.为素数时,直接printf;

//2 3情况的分法是因为素数中只有2为偶数,奇数-偶数=奇数,判定剩下的这个是否为素数

2.不为素数且减2之后为素数的情况:printf(2 n-2);

3.不为素数且减4之后为素数的情况:printf(2 2 n-4);

// 4情况是奇数-奇数=偶数,偶数可拆分为2个

4.不为素数,减3之后为偶数,printf(3 num n-3-num);此时的num和n-3-num需要遍历出来

相应代码:

#include

#include

usingnamespace std;

boolisPrime(int t){

int t1 = sqrt(t);

for(int i=2;i<=t1;i++){

if(t%i==0) return false;

}

return true;

}

intmain(){

//cout<

int T;

cin>>T;

while(T--){

int n;

cin>>n;

if(n%2==0){

if(n==2){

cout<<"2"<

}else if(n==4){

cout<<"22"<

}else{

for(int i=3;i<=n;i+=2){

if(isPrime(i)&&isPrime(n-i)){

cout<

break;

}

}

}

}else{

if(isPrime(n)){

cout<

}else if(isPrime(n-2)){

cout<<"2"<<(n-2)<

}else if(isPrime(n-4)){

cout<<"2 2"<<(n-4)<

}else{

//三素数定理中的分解可以必有3

for(intj = 3;j<=n;j+=2){

if(isPrime(j)&&isPrime(n-j-3)){

cout<<"3"<<""<

break;

}

}

}

}

}

return 0;

}

�sؔ ��

你可能感兴趣的:(Hzu_5.F.Something Easier)