D. Dima and Lisa
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 ≤ 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.
Sample test(s)
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.
------------------------------------------------------EDITORIAL-------------------------------------------------------------
There is a fact that the distance between adjacent prime numbers isn't big. For n = 109 maximal distanse is 282. So let's find maximal prime p, such that p < n - 1 (we can just decrease n while it's not prime(we can check it in
)). We know that n - p < 300. Now we have even (because n and p are odd) number n - p and we should divide it into a sum of two primes. As n - p < 300, we can just iterate over small primes P and check if P is prime and n - p - P is prime. You can check that there is a solution for all even numbers less than 300 by bruteforce.
--------------------------------------CODE---------------------------------------------------------------------------------------
#include<iostream> #include<stdio.h> #include<bits/stdc++.h> using namespace std; #define mn 100000000 vector<int> v; bool pcheck(int num); int main() { int num; cin>>num; if(num==5) { cout<<"1"<<endl; cout<<"5"<<endl; return 0; } if(num==3) { cout<<"1"<<endl; cout<<"3"<<endl; return 0; } if(num==7) { cout<<"3"<<endl; cout<<"2 2 3"<<endl; return 0; } cout<<"3"<<endl; cout<<"3 "; num-=3; //cout<<pcheck(21)<<endl; for(int j=3;j<=num;j+=2) { if(pcheck(j)&pcheck(num-j)) { cout<<j<<" "<<num-j<<endl; break; } } return 0; } bool pcheck(int num) { if(num==0) return 0; if(num==1) return 0; if(num==3) return 1; if(num==2) return 1; for(int i=2;i<=sqrt(num);i++) { if(num%i==0) return 0; } return 1; }
No comments:
Post a Comment