Friday, November 4, 2016

UVA 10168 - Summation of Four Primes

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(LL i = 0; i<MAX; i++)
#define MAX 10000000+10
using namespace std;
bool prime[MAX];
vector<LL>P;
void init_prime() {
  prime[2] = true;
  for (LL i = 3; i < MAX; i += 2) prime[i] = true;

  for (LL i = 3; i*i < MAX; i += 2)
    if (prime[i])
      for (LL j = i*i; j < MAX; j += i+i)
        prime[j] = false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    init_prime();
   LL n;
   while(cin >> n){
    bool found = false;
    if(n < 8){
        cout << "Impossible.\n";
        continue;
    }
    if(n%2 != 0){
        cout <<"2 3 ";
        n-=5;
    }
    else{
        cout <<"2 2 ";
        n-=4;
    }
    if(n == 4){
        cout << "2 2 ";
    }
    else{
        for (int i = 3; i <=(n/2) ; i+=2)
        {
            if(prime[i] && prime[n-i]){
                cout << i <<' '<<n-i<<'\n';
                found = true;
                break;
            }
        }
    }
    if(!found) cout << "Impossible.\n";
   }

 return 0;
}

No comments:

Post a Comment