moytrage

two_wisemen_and_two_numbers.cc

Oct 4th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. bool IsPrime(int n) {
  8.   if (n == 2) return true;
  9.   if (n % 2 == 0) return false;
  10.   for (int i = 3; i * i <= n; i += 2) {
  11.     if (n % i == 0) return false;
  12.   }
  13.   return true;
  14. }
  15.  
  16. void main() {
  17.   set<int> good_sums;
  18.   // Operations done by second wiseman.
  19.   for (int a = 2; a <= 100; ++a) {
  20.     for (int b = 2; b <= 100; ++b) {
  21.       int sum = a + b;
  22.       int mul = a * b;
  23.       bool good_sum = true;
  24.       for (int a2 = 2; a2 <= sum - 2; ++a2) {
  25.         int b2 = sum - a2;
  26.         if (IsPrime(a2) && IsPrime(b2)) {
  27.           good_sum = false;
  28.           //std::cout << "a2 = " << a2 << ", b2 = " << b2 << std::endl;
  29.         }
  30.       }
  31.       if (good_sum) {
  32.         good_sums.insert(sum);
  33.       }
  34.     }
  35.   }
  36.   cout << "GoodSums" << endl;
  37.   for (auto it = good_sums.begin(); it != good_sums.end(); ++it) {
  38.     cout << *it << " ";
  39.   }
  40.   cout << endl;
  41.   // Operations done by second wiseman.
  42.   set<int> good_muls;
  43.   for (int a_1 = 2; a_1 <= 100; ++a_1) {
  44.     for (int a_2 = 2; a_2 <= 100; ++a_2) {
  45.       if (!IsPrime(a_1) || !IsPrime(a_2)) continue;
  46.       int a1 = a_1 * a_2;
  47.       if (a1 > 100) continue;
  48.       for (int b1 = 2; b1 <= 100 - a1; ++b1) {
  49.         if (!IsPrime(b1)) continue;
  50.         if (b1 * a_2 > 100) continue;
  51.         if ((good_sums.count(a1 + b1) > 0 && good_sums.count(a_1 + b1 * a_2) == 0 && good_sums.count(a_2 + b1 * a_1) == 0) ||
  52.             (good_sums.count(a1 + b1) == 0 && good_sums.count(a_1 + b1 * a_2) > 0 && good_sums.count(a_2 + b1 * a_1) == 0) ||
  53.             (good_sums.count(a1 + b1) == 0 && good_sums.count(a_1 + b1 * a_2) == 0 && good_sums.count(a_2 + b1 * a_1) > 0)) {
  54.           good_muls.insert(a1 * b1);
  55.         }
  56.       }
  57.     }
  58.   }
  59.   cout << "Answers" << endl;
  60.   for (auto it = good_sums.begin(); it != good_sums.end(); ++it) {
  61.     int good_muls_count = 0;
  62.     int ans_a = 0, ans_b = 0;
  63.     for (auto a2 = 2; a2 <= *it - 2; ++a2) {
  64.       int b2 = *it - a2;
  65.       if (a2 > b2 || a2 > 100 || b2 > 100) continue;
  66.       if (good_muls.count(a2 * b2) > 0) {
  67.         ++good_muls_count;
  68.         ans_a = a2, ans_b = b2;
  69.       }
  70.     }
  71.     if (good_muls_count == 1) {
  72.       cout << "a = " << ans_a << ", b = " << ans_b << ", sum = " << (ans_a + ans_b) << ", mul = " << (ans_a * ans_b) << endl;
  73.     }
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment