egogoboy

Сумма двух чисел

Oct 28th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<cmath>
  6. #define all(container) container.begin(), container.end()
  7. #define fors(counter, start, finish) for (int counter = start; counter < finish; ++counter)
  8. #define forb(counter, start, finish) for (int counter = start; counter >= finish; --counter)
  9. #define vec(type) std::vector<type>
  10. #define dvec(type) std::vector<std::vector<type>>
  11. #define cout(val) std::cout << val << std::endl;
  12.  
  13. vec(int) num_a;
  14. vec(int) num_b;
  15. vec(int) temp_a;
  16. vec(int) temp_b;
  17. vec(bool) done_a;
  18. vec(bool) done_b;
  19. long long global_a, global_b, c, da = 0;
  20. long long preob(vec(int)& s, int size) {
  21.     long long temp = 0;
  22.     fors(i, 0, s.size()) {
  23.         temp += std::pow(10, s.size() - i - 1) * s[i];
  24.     }
  25.     temp *= std::pow(10, size - s.size());
  26.     return temp;
  27. }
  28.  
  29. bool check(vec(int)& x) {
  30.     fors(j, 0, temp_b.size()) {
  31.         fors(i, 0, num_b.size()) {
  32.             if (num_b[i] == temp_b[j] && !done_b[i]) {
  33.                 done_b[i] = true;
  34.                 break;
  35.             }
  36.         }
  37.     }
  38.     bool f = true;
  39.     fors(i, 0, done_b.size()) {
  40.         if (!done_b[i] && num_b[i] != 0) {
  41.             f = false;
  42.         }
  43.         done_b[i] = false;
  44.     }
  45.     return f;
  46. }
  47.  
  48. vec(int) int_to_vec(int x) {
  49.     vec(int) temp;
  50.     while (x != 0) {
  51.         temp.push_back(x % 10);
  52.         x /= 10;
  53.     }
  54.     std::reverse(all(temp));
  55.     return temp;
  56. }
  57.  
  58. void perest(int a, int b) {
  59.     if (da < num_a.size()) {
  60.         fors(i, 0, num_a.size()) {
  61.             if (!done_a[i]) {
  62.                 temp_a.push_back(num_a[i]);
  63.                 done_a[i] = true; da++;
  64.                 perest(a + 1, b);
  65.                 temp_a.resize(temp_a.size() - 1);
  66.                 done_a[i] = false; da--;
  67.             }
  68.         }
  69.     }
  70.     else {
  71.         long long x, y;
  72.         x = preob(temp_a, num_a.size()); y = c - x;
  73.         temp_b = int_to_vec(y);
  74.         /*std::cout << x << " + " << y << (x + y == c ? " = " : " != ") << c <<
  75.             "                    vaiant #" << nom << std::endl;*/
  76.         if (check(temp_b)) {
  77.             if (x < global_a || y < global_b) {
  78.                 global_a = x; global_b = y;
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. int main() {
  85.     std::ifstream fin("input.txt");
  86.     fin >> global_a >> global_b >> c;
  87.     bool f = false;
  88.     if (global_a > global_b) {
  89.         std::swap(global_a, global_b);
  90.         f = true;
  91.     }
  92.     num_a = int_to_vec(global_a);
  93.     num_b = int_to_vec(global_b);
  94.     done_a.resize(num_a.size());
  95.     done_b.resize(num_b.size());
  96.     global_a = 1000000001, global_b = 1000000001;
  97.     perest(0, 0);
  98.     if (global_a != 1000000001 && global_b != 1000000001) {
  99.         if (f)
  100.             std::swap(global_a, global_b);
  101.         std::cout << "YES" << std::endl << global_a << " " << global_b << std::endl;
  102.         return 0;
  103.     }
  104.     std::cout << "NO" << std::endl;
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment