signedmain(){ ios::sync_with_stdio(false), cin.tie(0); string n; int k; while (cin >> n >> k) { int len = n.length(); while (k--) { sort(n.begin(), n.end()); string temp = n; reverse(temp.begin(), temp.end()); int sum1=atoi(temp.c_str()),sum2=atoi(n.c_str()); n = to_string(sum1-sum2); } cout << n << endl; } system("pause"); return0; }
boolcheck(char *n, ll base){ ll sum = 0, shu = 1; for (ll i = strlen(n) - 1; i >= 0; --i) { if (isMulOverflow(n[i] - '0', shu) || isAddOverflow((n[i] - '0') * shu, sum)) return0; //此次转换后结果爆 long long sum += (n[i] - '0') * shu; if (i - 1 >= 0 && isMulOverflow(shu, base)) return0; //转换未结束但是下次的base爆longlong shu *= base; if (sum > m) return0; } return1; }
signedmain(){ ios::sync_with_stdio(false), cin.tie(0); while (cin >> x >> m) { ll base = *max_element(x, x + strlen(x)) - '0'; if (strlen(x) == 1) { if (base > m) cout << 0 << endl; else cout << 1 << endl; continue; } ll l = base + 1, r = 1+1e18; //小数+大数,否则浮点数溢出会丢弃1 while (l < r) { ll mid = (l + r) >> 1; if (check(x, mid)) l = mid + 1; else r = mid; } cout << (l - 1) - base << endl; //l-1是找到的最后一个符合的进制 } // system("pause"); return0; }