F - Dima and Lisa
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
- 1?≤?k?≤?3
- pi is a prime
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
Input27Output
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