T3000

Untitled

Jun 1st, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using ll = long long;
  4. using ld = long double;
  5. using namespace std;
  6.  
  7. vector<ll> z1;
  8.  
  9. ll sol = 0;
  10. ll x;
  11. vector<bool> hp;
  12.  
  13. void solve(ll pos, ll n, ll w)
  14. {
  15.     if (pos == n)
  16.     {
  17.         if (w > x && (sol == 0 || sol > w))
  18.         {
  19.             sol = w;
  20.         }
  21.         w = 0;
  22.         return;
  23.     }
  24.     for (int i = 0; i < z1.size(); i++)
  25.     {
  26.         if (hp.at(i) == false)
  27.         {
  28.             hp.at(i) = true;
  29.             w = w * 10 + z1.at(i);
  30.             solve(pos + 1, n, w);
  31.             hp.at(i) = false;
  32.         }
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.  
  39.     cin >> x;
  40.  
  41.     ll ax;
  42.     ax = x;
  43.     while (ax > 0)
  44.     {
  45.         z1.push_back(ax % 10);
  46.         hp.push_back(false);
  47.         ax = ax / 10;
  48.     }
  49.  
  50.     solve(0, z1.size(), 0);
  51.  
  52.     cout << sol << endl;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment