Advertisement
skaram

Untitled

Sep 21st, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef Local
  3. #include "debug/debug.h"
  4. #else
  5. #define debug(...)
  6. #endif
  7.  
  8. typedef long long ll;
  9. typedef long double ld;
  10.  
  11. #define int ll
  12.  
  13. #define vec vector
  14. #define str string
  15. #define all(x) (x).begin(), (x).end()
  16. #define rall(x) (x).rbegin(), (x).rend()
  17. #define rev(x) reverse(x)
  18. #define sz(x) (int)(x).size()
  19.  
  20. using namespace std;
  21.  
  22. void solve() {
  23.     int n, s;
  24.     cin >> n >> s;
  25.     if (n == 1 && s == 1) {
  26.         cout << 2;
  27.         return;
  28.     }
  29.     int b = 2;
  30.     bool r = false;
  31.     for (; b <= (int)1e7; b++) {
  32.         int cur = 0;
  33.         int x = n;
  34.         while (b <= x) {
  35.             cur += x % b;
  36.             x /= b;
  37.         }
  38.         cur += x;
  39.         if (cur == s) {
  40.             r = true;
  41.             break;
  42.         }
  43.     }
  44.     if (!r && n != 1) {
  45.         b = n;
  46.         int cur = 0;
  47.         int x = n;
  48.         while (b <= x) {
  49.             cur += x % b;
  50.             x /= b;
  51.         }
  52.         cur += x;
  53.         if (cur == s)
  54.             r = true;
  55.     }
  56.     if (!r) {
  57.         b = n;
  58.         for (; b >= 2 && b >= n - (int)1e7; b--) {
  59.             int cur = 0;
  60.             int x = n;
  61.             while (b <= x) {
  62.                 cur += x % b;
  63.                 x /= b;
  64.             }
  65.             cur += x;
  66.             if (cur == s) {
  67.                 r = true;
  68.                 break;
  69.             }
  70.         }
  71.     }
  72.     cout << (r ? b : -1) << '\n';
  73. }
  74.  
  75. signed main() {
  76.     ios::sync_with_stdio(false);
  77.     cin.tie(nullptr);
  78.  
  79.     int tt = 1;
  80.     //    cin >> tt;
  81.     while (tt-- > 0)
  82.         solve();
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement