Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #ifdef Local
- #include "debug/debug.h"
- #else
- #define debug(...)
- #endif
- typedef long long ll;
- typedef long double ld;
- #define int ll
- #define vec vector
- #define str string
- #define all(x) (x).begin(), (x).end()
- #define rall(x) (x).rbegin(), (x).rend()
- #define rev(x) reverse(x)
- #define sz(x) (int)(x).size()
- using namespace std;
- void solve() {
- int n, s;
- cin >> n >> s;
- if (n == 1 && s == 1) {
- cout << 2;
- return;
- }
- int b = 2;
- bool r = false;
- for (; b <= (int)1e7; b++) {
- int cur = 0;
- int x = n;
- while (b <= x) {
- cur += x % b;
- x /= b;
- }
- cur += x;
- if (cur == s) {
- r = true;
- break;
- }
- }
- if (!r && n != 1) {
- b = n;
- int cur = 0;
- int x = n;
- while (b <= x) {
- cur += x % b;
- x /= b;
- }
- cur += x;
- if (cur == s)
- r = true;
- }
- if (!r) {
- b = n;
- for (; b >= 2 && b >= n - (int)1e7; b--) {
- int cur = 0;
- int x = n;
- while (b <= x) {
- cur += x % b;
- x /= b;
- }
- cur += x;
- if (cur == s) {
- r = true;
- break;
- }
- }
- }
- cout << (r ? b : -1) << '\n';
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- int tt = 1;
- // cin >> tt;
- while (tt-- > 0)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement