码迷,mamicode.com
首页 > 其他好文 > 详细

F - Dima and Lisa

时间:2018-06-08 23:20:29      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:bre   names   when   lag   枚举   ril   哥德巴赫猜想   put   The   

Problem description

Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.

More formally, you are given an odd numer n. Find a set of numbers pi (1?≤?i?≤?k), such that

  1. 1?≤?k?≤?3
  2. pi is a prime
  3. 技术分享图片

The numbers pi do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.

Input

The single line contains an odd number n (3?≤?n?<?109).

Output

In the first line print k (1?≤?k?≤?3), showing how many numbers are in the representation you found.

In the second line print numbers pi in any order. If there are multiple possible solutions, you can print any of them.

Examples

Input
27
Output
3
5 11 11

Note

A prime is an integer strictly larger than one that is divisible only by one and by itself.

解题思路:将一个奇数拆分成1~3个素数,暴力即过!

哥德巴赫猜想:随便取某一个奇数,比如77,可以把它写成三个素数之和,即77=53+17+7;再任取一个奇数,比如461,可以表示成461=449+7+5,也是三个素数之和,461还可以写成257+199+5,仍然是三个素数之和。例子多了,即发现“任何大于5的奇数都是三个素数之和。”

AC代码(31ms):

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 bool isprime(int x){
 4     if(x<=1)return false;
 5     for(int i=2;i*i<=x;++i)
 6         if(x%i==0)return false;
 7     return true;
 8 }
 9 int main(){
10     int n;cin>>n;bool flag=false;
11     if(isprime(n))cout<<"1\n"<<n<<endl;//如果本身是素数,直接输出即可
12     else{
13         for(int i=3;i<=n;i+=2){//从3开始按奇数来枚举
14             if(isprime(i)){
15                 int tmp=n-i;
16                 if(isprime(tmp)){cout<<"2\n"<<i<< <<tmp<<endl;break;}
17                 for(int j=3;j<=n;j+=2)//从3开始按奇数来枚举
18                     if(isprime(j) && isprime(tmp-j)){cout<<"3\n"<<i<< <<j<< <<(tmp-j)<<endl;flag=true;break;}
19                 if(flag)break;
20             }
21         }
22     }
23     return 0;
24 }

 

F - Dima and Lisa

标签:bre   names   when   lag   枚举   ril   哥德巴赫猜想   put   The   

原文地址:https://www.cnblogs.com/acgoto/p/9157876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!