Advertisement
gruslan

1542B. Сложить и умножить

Jan 29th, 2023 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define lli long long int
  4.  
  5. #define all(x) x.begin(), x.end()
  6. #define in(x, y) y.find(x) != y.end()
  7. #define not_in(x, y) y.find(x) == y.end()
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void print(auto &data, auto sep=", ", auto end=endl)
  13. {
  14.     cout << "[";
  15.     for (auto i: data)
  16.     {
  17.         cout << i << sep;
  18.     }
  19.     cout << "]" << end;
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25.     lli t, n, a, b;
  26.     bool TF;
  27.  
  28.     cin >> t;
  29.     while (t--)
  30.     {
  31.         cin >> n >> a >> b;
  32.  
  33.         if (n == 1 || n % a == 0)
  34.         {
  35.             cout << "Yes\n";
  36.         } else if (a == 1)
  37.         {
  38.             cout << ((n % b == 1)?"Yes":"No") << "\n";
  39.         } else
  40.         {
  41.             set<lli> front;
  42.             set<lli> old_front;
  43.             set<lli> new_front;
  44.  
  45.             front.insert(1);
  46.  
  47.             TF = false;
  48.             while (!front.empty())
  49.             {
  50.                 new_front.clear();
  51.  
  52.                 old_front.insert(all(front));
  53.                 for (auto i: front)
  54.                 {
  55.                     if (i * a == n || i + b == n)
  56.                     {
  57.                         TF = true;
  58.                         break;
  59.                     }
  60.  
  61.                     if (i * a <= n - b && not_in(i * a, old_front))
  62.                     {
  63.                         new_front.insert(i * a);
  64.                     }
  65.  
  66.                     if (i + b <= n - b && not_in(i + b, old_front))
  67.                     {
  68.                         new_front.insert(i + b);
  69.                     }
  70.                 }
  71.  
  72.                 // print(old_front);
  73.                 // print(new_front);
  74.                 // cout << "\n";
  75.  
  76.                 if (TF)
  77.                 {
  78.                     break;
  79.                 }
  80.  
  81.                 front.clear();
  82.                 front.insert(all(new_front));
  83.             }
  84.  
  85.             cout << ((TF)?"Yes":"No") << "\n";
  86.         }
  87.     }
  88.  
  89.     return 0;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement