Advertisement
CloneTrooper1019

UVa 10168 - Summation of Four Primes

Nov 26th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #ifdef _MSC_VER
  2. #define _CRT_SECURE_NO_WARNINGS 1
  3. #endif
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. const int maxVal = 10e7;
  12. bool sieve[maxVal];
  13.  
  14. int main()
  15. {
  16.     // Load sieve array.
  17.     sieve[0] = true;
  18.     sieve[1] = true;
  19.  
  20.     for (int i = 2; i*i <= maxVal; i++)
  21.         if (!sieve[i])
  22.             for (int j = i*2; j < maxVal; j += i)
  23.                 sieve[j] = true;
  24.  
  25.     int n;
  26.  
  27.     while (scanf("%d", &n) != EOF)
  28.     {
  29.         if (n < 8)
  30.             cout << "Impossible.\n";
  31.         else
  32.         {
  33.             int remainder;
  34.  
  35.             if (n % 2 == 0)
  36.             {
  37.                 cout << "2 2 ";
  38.                 remainder = n - 4;
  39.             }
  40.             else
  41.             {
  42.                 cout << "2 3 ";
  43.                 remainder = n - 5;
  44.             }
  45.  
  46.             for (int i = 2; i < remainder; i++)
  47.             {
  48.                 int rmi = remainder - i;
  49.                 if (!sieve[i] && !sieve[rmi])
  50.                 {
  51.                     cout << i << " " << rmi << "\n";
  52.                     break;
  53.                 }
  54.             }
  55.         }
  56.     }
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement