This is the Meelo original Ieeextreme Extreme Programming contest
Xtreme 10.0-goldbach ' s Second conjecture
Topic Source 10th IEEE Extreme Programming Contest
Https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/goldbachs-second-conjecture
An integer P > 1 is called a prime if it only divisors be 1 and p itself. A famous conjecture about primes was Goldbach ' s conjecture, which states that
every
even integer greater than
2 can be expressed as the sum of
the primes.
The conjecture dates back to the year 1742, but still no one have been able to come up with a proof or find a Counterexampl E to it. We considered asking you prove it here, but realized it would is too easy. Instead we present here a more difficult conjecture, known as Goldbach ' s second conjecture:
every
odd integer greater than
5 can be expressed as the sum of
three primes.
In this problem we'll provide you a odd integer N greater than 5, and ask you to either find three primes P1, p2, p3 such that p1 + p2 + p3 = n, or inform us and N is a counterexamp Le to Goldbach ' s second conjecture.
Input Format
The input contains a single odd integer 5 < N ≤1018.
Output Format
Output three primes, separated by a single space on a, and whose sum is N. If There is multiple possible answers, output any one of them. If there is no possible answers, output a single line containing the text "counterexample" (without quotes).
Sample Input
65
Sample Output
23 31 11
Explanation
In the sample, input N is 65. Consider the three integers 11, 23, 31. They is all primes, and their sum is 65. Hence They form a valid answer. That's, a line containing "one by one", "one by one", or any permutation of the three integers would be accepted. Other possible answers include "one-Panax Notoginseng" and "11 11 43".
Problem analysis
An odd number is decomposed into three prime numbers, and an odd number is 1018. You can traverse the first two primes and then judge whether the difference between the odd and two prime numbers is still not prime. If 3 prime numbers have 1017, they will definitely time out.
In fact, there are no more than 1000 of the first two prime numbers of the solution. This time the key question becomes, how to judge a size has 1018 of the number is prime. The general method complexity is O (sqrt (n)), which times out. At this point, a bit of number theory is needed, and the Miller–rabin Prime test is able to determine in O ((LOGN) 2) Whether a count is prime. The algorithm is described in more detail in Wikipedia. The Miller–rabin prime number test in the following procedure uses the code on GitHub.
Program
C++
#include <cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>#include<bitset>using namespacestd;#defineMAXN 1000typedef unsignedLong LongUll;typedefLong LongLl;bitset<MAXN>nums;intPRIMES[MAXN];intNum_prime =0;voidGetprimes (Long Longmax) {//Get all primes under Max for(intI=2; I<=SQRT (max+0.5); i++) { if(Nums[i] = =false) {Primes[num_prime]=i; Num_prime++; for(Long Longn=2*i; n<max; n+=i) {Nums[n]=true; } } } for(intI=int(Sqrt (max+0.5))+1; i<max; i++) { if(Nums[i] = =false) {Primes[num_prime]=i; Num_prime++; }}}ll Multiplymod (ll A, ll B, ll MoD) {//computes A * b% modULL r =0; A%= MoD, b%=MoD; while(b) {if(B &1) R = (r + a)%MoD; b>>=1, a = ((ULL) a <<1) %MoD; } returnR;} Template<typename t>T Powermod (t A, T N, t MoD) {//computes a^n% modT r =1; while(n) {if(N &1) R =Multiplymod (R, a, MoD); N>>=1, a =Multiplymod (A, A, mod); } returnR;} Template<typename t>BOOLIsPrime (T N) {//determines if n is a prime number Const intPN =9, p[] = {2,3,5,7, One, -, -, +, at }; for(inti =0; i < PN; ++i)if(n% p[i] = =0)returnn = =P[i]; if(N < P[PN-1])return 0; T s=0, t = n-1; while(~t &1) T>>=1, ++s; for(inti =0; i < PN; ++i) {T pt= powermod<t>(P[i], T, N); if(pt = =1)Continue; BOOLOK =0; for(intj =0; J < s &&!ok; ++j) {if(pt = = N-1) OK =1; PT=Multiplymod (PT, PT, N); } if(!ok)return 0; } return 1;}intMain () {Long LongN; CIN>>N; Getprimes (MAXN); for(intI=0; i<num_prime; i++) { for(intJ=i; j<num_prime; J + +) { if(IsPrime (n-primes[j]-Primes[i])) {printf ("%lld%lld%lld", Primes[i], primes[j], n-primes[i]-Primes[j]); return 0; } } } return 0;}
The articles in the blog are Meelo original, please make sure to indicate the address in the link form
Ieeextreme 10.0-goldbach ' s Second conjecture