Problem:


Score : 500 points

Problem Statement

For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:

f(b,n) = n, when n < b

f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b

Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b,
and n \ {\rm mod} \ b denotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b.
For example, the following hold:

f(10,\,87654)=8+7+6+5+4=30

f(100,\,87654)=8+76+54=138

You are given integers n and s.
Determine if there exists an integer b (b \geq 2) such that f(b,n)=s.
If the answer is positive, also find the smallest such b.

Constraints

1 \leq n \leq 10^{11}

1 \leq s \leq 10^{11}

n,\,s are integers.

Input

The input is given from Standard Input in the following format:

n
s

Output

If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b.
If such b does not exist, print -1 instead.

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1



Sample input:
87654
30


Sample output:
10

Submission status: Wrong Answer

Solution code:
import math


def digit_sum(b, n):
    if b > n:
        return n
    else:
        return digit_sum(b=b, n=n // b) + n % b


def main(n, s):
    if s > n:
        return -1
    elif n == s:
        return n + 1
    elif s == 1:
        return n
    else:
        b = 2
        while b * b <= n:
            if digit_sum(b, n) == s:
                return b
            b += 1
        b_min = 100000000000000
        p = 2
        while p * p <= n:
            if (n - s) % p == 0 and (n - s) // p != 0:
                b = (n - s) // p + 1
                if digit_sum(b, n) == s:
                    b_min = min(b_min, b)
            p += 1
    if b_min == 100000000000000:
        return -1
    else:
        return b_min


if __name__ == '__main__':
    N = int(input())
    S = int(input())
    print(main(n=N, s=S))

Submission status: Wrong Answer

Solution code:
import math


def digit_sum(b, n):
    if b > n:
        return n
    else:
        return digit_sum(b=b, n=n // b) + n % b


def main(n, s):
    if s > n:
        return -1
    if n == s:
        return n + 1
    else:
        sqrt_n = int(math.sqrt(n))
        for b in range(2, sqrt_n + 1):
            if digit_sum(b, n) == s:
                return b
        b = n * n
        for p in range(1, sqrt_n + 1):
            if (n - s) % p == 0 and (n - s) // p != 0:
                _b = (n - 1) // p + 1
                if digit_sum(_b, n) == s:
                    b = min(b, _b)
    if b == n * n:
        return -1
    else:
        return b


if __name__ == '__main__':
    N = int(input())
    S = int(input())
    print(main(n=N, s=S))

Submission status: Wrong Answer

Solution code:
import math


def digit_sum(b, n):
    if b > n:
        return n
    else:
        return digit_sum(b=b, n=n // b) + n % b


def main(n, s):
    if s > n:
        return -1
    if n == s:
        return n + 1
    else:
        sqrt_n = int(math.sqrt(n))
        for b in range(2, sqrt_n + 1):
            if digit_sum(b, n) == s:
                return b
        b = n + 1
        for p in range(1, sqrt_n + 1):
            if (n - s) % p == 0 and (n - s) // p != 0:
                _b = (n - 1) // p + 1
                if digit_sum(_b, n) == s:
                    b = min(b, _b)
    if b == n + 1:
        return -1
    else:
        return b


if __name__ == '__main__':
    N = int(input())
    S = int(input())
    print(main(n=N, s=S))

Submission status: Wrong Answer

Solution code:
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for b in range(2,loop+1):
            cur = n
            ansC = 0
            while cur>0:
                ansC += cur%b
                cur = cur//b
            if ansC==s:
                ans = b
                break
    if ans==-1:
        for p in range(loop+1,0,-1):
            bcandi = (n-s)//p + 1
            if bcandi>=2:
                ansC = n % bcandi + n // bcandi
                if ansC == s:
                    ans = bcandi
                    break

    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Wrong Answer

Solution code:
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for b in range(2,loop+1):
            cur = n
            ansC = 0
            while cur>0:
                ansC += cur%b
                cur = cur//b
            if ansC==s:
                ans = b
                break
    if ans==-1:
        for p in range(loop+1,0,-1):
            bcandi = (n-s)//p + 1
            if bcandi>=2:
                ansC = n % bcandi + n // bcandi
                if ansC == s:
                    ans = int(bcandi)
                    break

    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Wrong Answer

Solution code:
# python3 (3.4.3)
import sys
input = sys.stdin.readline

# main
N = int(input())
S = int(input())

def f(b,n):
    if n < b:
        return n
    else:
        return f(b, n//b) + n%b

ans = -1
if S==N:
    ans = N+1
else:
    # bをN^(1/2)まで全探索
    for b in range(2, int(N**.5)+1):
        if f(b,N)==S:
            ans = b
            break

# まだ答えが見つからなければ
if ans == -1:
    # bがN^(1/2)より大きい可能性が残っており、b進数で表すと2桁
    # 1 <= p < b, N = pb+q <= pb < p^2, p > N^(1/2) なので pで全探索
    # p+q = S より b = (N-S)/p+1
    for p in range(1, int(N**.5)+1):
        b = (N-S)//p + 1
        # bが1のときは対象外にしないと無限ループ！
        if b >= 2 and f(b,N)==S:
            ans = b
            break

print(ans)

Submission status: Wrong Answer

Solution code:
def base_10_to_n(X, n):
    X_dumy = X
    out = ''
    lis = []
    while X_dumy>0:
        # out = str(X_dumy%n) + out
        lis.append((X_dumy % n))
        X_dumy = int(X_dumy / n)
    return lis
 
 
def solve():
    N, S = int(input()), int(input())
    # print(N, S)
 
    def f(b, n):
        lis = base_10_to_n(n, b)
        return sum(lis)
 
    for b in range(2, N + 1):
        if f(b, N) == S:
            return b
    return -1
  
 
if __name__ == '__main__':
    res = solve()
    print(res)

Submission status: Wrong Answer

Solution code:
def base_10_to_n(X, n):
    X_dumy = X
    out = ''
    lis = []
    while X_dumy>0:
        # out = str(X_dumy%n) + out
        lis.append((X_dumy % n))
        X_dumy = int(X_dumy / n)
    return lis


def solve():
    N, S = int(input()), int(input())
    # print(N, S)

    def f(b, n):
        lis = base_10_to_n(n, b)
        return sum(lis)

    for b in range(2, N + 1):
        if f(b, N) == S:
            return b
    return -1
  

if __name__ == '__main__':
    res = solve()
    print(res)

Submission status: Wrong Answer

Solution code:
import math
 
 
def solve():
    N, S = int(input()), int(input())
    # print(N, S)
    
    floor = math.floor
    def f(b, n):
        if n < b:
            return n
        if n >= b:
            return f(b, floor(n / b)) + (n % b)
 
 
    for b in range(2, N):
        if f(b, N) == S:
            return b
    return -1
 
 
if __name__ == '__main__':
    res = solve()
    print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = int(input())

def f(b,n):
    if n//b==0:
        return n
    else:
        return n%b + f(b,n//b)

ans = -1
if S==N:
    ans = N+1
else:
    for b in range(2,int(N**.5)+1):
        if f(b,N)==S:
            ans = b
            break

if ans == -1:
    for i in range(int(N**.5)-1,0,-1):
        b = (N-S)//i + 1
        if b>1:
            if f(b,N)==S:
                ans = b
                break

print(ans)

Submission status: Wrong Answer

Solution code:
import math
def f(b,n):
    if n<b:
        return n
    else:
        ans = 0
        while n>=b:
            ans += n%b
            n //=b
        ans += n

        return int(ans)
def main():
    n=int(input())
    s=int(input())
    if n==s:
        print(n+1)
        exit()
    elif n <s:
        print(-1)
        exit()
    else:
        for i in range(2,int(math.sqrt(n))+1):
            if f(i,n)==s:
                print(i)
                exit()
        for j in range(1,int(math.sqrt(n))):
            b=(n-s)/j+1
            if f(b,n)==s:
                print(int(b))
                exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    s=int(input())
    if n==s:
        print(n)
        exit()
    if n<s:
        print(-1)
        exit()
    for i in range(2,n):
        tmp = 0
        tmp_n=n
        while tmp_n >=i:
            tmp += tmp_n%i
            tmp_n //=i
        tmp += tmp_n
        if tmp ==s:
            print(i)
            exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n // b) + n % b


n = int(input())
s = int(input())
bmax = math.floor(math.sqrt(n))

for b in range(2, bmax + 1):
    if f(b, n) == s:
        print(b)
        exit()
for k in range(bmax - 1, 0, -1):
    bb = n // k
    remaining = s - k - n % bb
    if remaining < 0 or remaining > bb - remaining // k:
        continue
    if remaining % k == 0:
        print(bb - remaining // k)
        exit()
if s == n:
    print(n + 1)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math


def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n // b) + n % b


n = int(input())
s = int(input())
bmax = math.floor(math.sqrt(n))

for b in range(2, bmax + 1):
    if f(b, n) == s:
        print(b)
        exit()
for k in range(bmax - 1, 0, -1):
    bb = n // k
    remaining = s - k - n % bb
    if remaining < 0 or remaining >= bb - remaining // k:
        continue
    if remaining % k == 0:
        print(bb - remaining // k)
        exit()
if s == n:
    print(n + 1)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math


def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n // b) + n % b


n = int(input())
s = int(input())
bmax = math.floor(math.sqrt(n))

for b in range(2, bmax + 1):
    if f(b, n) == s:
        print(b)
        exit()
for k in range(bmax - 1, 0, -1):
    bb = n // k
    remaining = s - k - n % bb
    if remaining < 0 or remaining >= bb - remaining // k:
        continue
    if remaining % k == 0:
        print(bb - remaining // k)
        exit()
if s == 1:
    print(n)
    exit()
elif s == n:
    print(n + 1)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math


def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n // b) + n % b


n = int(input())
s = int(input())
bmax = math.floor(math.sqrt(n))

for b in range(2, bmax + 1):
    if f(b, n) == s:
        print(b)
        exit()
for k in range(bmax - 1, 1, -1):
    bb = n // k
    remaining = s - k - n % bb
    if remaining < 0 or remaining >= bb:
        continue
    if remaining % k == 0:
        print(bb + remaining // k)
        exit()
if s == 1:
    print(n)
    exit()
elif s == n:
    print(n + 1)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = int(input())

result = -1
if n == s:
  result = n + 1
elif s > n:
  pass
else:
  sqrt_n = math.floor(math.sqrt(n))
  for b in range(2, sqrt_n + 1):
    temp_n = n
    temp_b = 0
    while temp_n >= b:
      temp_b += temp_n % b
      temp_n = temp_n // b
    if temp_b + temp_n == s:
      result = b
      break
  if result == -1:
    for quot in range(sqrt_n, 0, -1):
      if n - s % quot == 0:
        b = (n - s) // quot + 1
        if quot + n % b == s:
          result = b
          break
print(result)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
ans="-1"
def f(b,n):
    if n<b:
        return n
    else:
        return f(b,n//b)+n%b
if n==s:
    ans=n+1
for b in range(2,int(n**(1/2)+1)):
    if f(b,n)==s:
        ans=b
        break
if ans=="-1":
    for p in range(int(n**(1/2)+1),1,-1):
        b=(n-s)/p+1
        if b>=2:
            if f(b,n)==s:
            	ans=b
            	break
print(int(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
ans="-1"
def f(b,n):
    if n<b:
        return n
    else:
        return f(b,n//b)+n%b
if n==s:
    ans=n+1
for b in range(2,int(n**(1/2)+1)):
    if f(b,n)==s:
        ans=b
        break
if ans=="-1":
    for p in range(1,int(n**(1/2)+1)):
        b=(n-s)/p+1
        if b>=2:
            if f(b,n)==s:
            	ans=b
            	break
print(int(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
ans="-1"
def f(b,n):
    if n<b:
        return n
    else:
        return f(b,n//b)+n%b
if n==s:
    ans=n+1
for b in range(2,int(n**(1/2)+1)):
    if f(b,n)==s:
        ans=b
        break
if ans=="-1":
    for p in range(1,int(n**(1/2)+1)):
        b=(n-s)/p+1
        if b>=2:
            if f(b,n)==s:
                ans=b
                break
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b,n):
    if n < b:
        return n
    elif n >= b:
        return f(b,n//b) + n % b

res = -1

import sys

if s == 1:
    print(n)
    sys.exit()

for b in range(2,n+1):
    if f(b,n) == s:
        res = b
        break

print(res)

Submission status: Wrong Answer

Solution code:
import math
#平方オーダーなら収まるので平方根とって方針を分ける
n = int(input())
s = int(input())
if n == s:
  print(n + 1)
  exit(0)
def sqrt(k):
  return int(math.sqrt(k))
def digitsum(b, m):
  if m < b:
    return m
  return digitsum(b, m // b) + m % b
#√nより小さいなら
#全探索余裕 桁は多くて40程度なので
for b in range(2, sqrt(n) + 1):
  if digitsum(b, n) == s:
    print(b)
    exit(0)
#√nより大きいなら
#b^2 > n である。２桁ということ
#2桁目がpなら、1桁目はs-pである
#pb + s - p = n
#b = (n - s) // p + 1
for p in range(1, sqrt(n)):
  b = (n - s) // p + 1
  if b <= 1: continue
  if digitsum(b, n) == s:
    print((n - s) // p + 1)
    exit(0)
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=int(input())
if n==s:
  print(n+1)
  sys.exit()
if n<s:
  print(-1)
  sys.exit()
for i in range(2,int(n**0.5)+1):
  ct=0;x=n;b=i
  while x>=b:
    ct+=x%b
    x=x//b
  ct+=x
  if ct==s:
    print(b)
    sys.exit()
for i in range(1,int(n**0.5)+1):
  if (n-s)%i==0:
    ct=0;x=n;b=(n-s)//i+1
    while x>=b:
      ct+=x%b
      x=x//b
    ct+=x
    if ct==s:
      print(b)
      sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC44 D

import sys
import math
import bisect
import time
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

#√n < bの場合はnをb進数表記すると2桁になることに着目
n = I()
s = I()
def f(x,y):
    if y < x:
        return y
    else:
        return f(x,math.floor(y/x)) + y % x

if n == s:
    print(n+1)
    quit()
for i in range(2,int(math.sqrt(n))+1):
    if f(i,n) == s:
        print(i)
        quit()
for p in range(1,int(math.sqrt(n)))[::-1]:
    b = (n-s)//p + 1
    if b <= 1:
        continue
    if f(b,n) == s:
        print(b)
        quit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if n==s:
  print(n+1)
  exit()
if n<s:
  print(-1)
  exit()
for b in range(2,int(n**(1/2))+1):
  m=n
  wa=0
  while m>0:
    wa+=m%b
    m=m//b
  if wa==s:
    print(b)
    exit()

for p in range(int(n**(1/2))-1,0,-1):
  if (n-s)%p==0:
    b=(n-s)//p+1
    if p+n%b==s:
      print(b)
      exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if n==s:
  print(n+1)
  exit()
if n<s:
  print(-1)
  exit()
for b in range(2,int(n**(1/2))+1):
  m=n
  wa=0
  while m>0:
    wa+=m%b
    m=m//b
  if wa==s:
    print(b)
    exit()

for p in range(int(n**(1/2)),0,-1):
  if (n-s)%p==0:
    b=(n-s)//p+1
    if p+n%b==s:
      print(b)
      exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if n==s:
  print(n+1)
  exit()
for b in range(2,int(n**(1/2)+1)):
  m=n
  wa=0
  while m>0:
    wa+=m%b
    m=m//b
  if wa==s:
    print(b)
    exit()

for p in range(int(n**(1/2)),0,-1):
  if p+n%p==s:
    print(n//p)
    exit()
print('-1')

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if n==s:
  print(n+1)
  exit()
for b in range(2,int(n**(1/2)+1)):
  m=n
  wa=0
  while m>0:
    wa+=m%b
    m=m//b
  if wa==s:
    print(b)
    exit()

for p in range(int(n**(1/2)),0,-1):
  if p+n%p==s:
    print(n//p)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())

b_ans=1



if n==s:
  b_ans=n+1
if b_ans==1:
  for b in range(2,int(n**0.5)+1):
    nn=n
    st=[]
    while(nn>0):
      st.append(nn%b)
      nn=(nn-st[-1])/b
  
    if sum(st)==s:
      b_ans=b
      break


if b_ans==1:
  for p in range(int(n**0.5),0,-1):
    b=(n-s+p)/p

    if b.is_integer() and 0<=s-p<b:
      b_ans=int(b)
      break

if b_ans!=1:
  print(b_ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())

b_ans=1



if n==s:
  b_ans=n+1
if b_ans==1:
  for b in range(2,int(n**0.5)+1):
    nn=n
    st=[]
    while(nn>0):
      st.append(nn%b)
      nn=(nn-st[-1])/b
  
    if sum(st)==s:
      b_ans=b
      break


if b_ans==1:
  for p in range(int(n**0.5),0,-1):
    b=(n-s+p)/p

    if b.is_integer() and s-p<b:
      b_ans=int(b)
      break

if b_ans!=1:
  print(b_ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())

b_ans=1



if n==s:
  b_ans=n+1
if b_ans==1:
  for b in range(2,int(n**0.5)+1):
    nn=n
    st=[]
    while(nn>0):
      st.append(nn%b)
      nn=(nn-st[-1])/b
  
    if sum(st)==s:
      b_ans=b
      break


if b_ans==1:
  for p in range(int(n**0.5),0,-1):
    b=(n-s+p)/p

    if b.is_integer():
      b_ans=int(b)
      break

if b_ans!=1:
  print(b_ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())

b_ans=1



if n==s:
  b_ans=n+1
if b_ans==1:
  for b in range(2,int(n**0.5)+1):
    nn=n
    st=[]
    while(nn>0):
      st.append(nn%b)
      nn=(nn-st[-1])/b
  
    if sum(st)==s:
      b_ans=b
      break


if b_ans==1:
  for p in range(int(n**0.5),0,-1):
    b=int(n/p)
    q=n-p*b
    if s==p+q and q<b:
      b_ans=b
      break

if b_ans!=1:
  print(b_ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())

b_ans=1

if n==s:
  b_ans=n+1
if b_ans==1:
  for b in range(2,int(n**0.5)+1):
    nn=n
    st=[]
    while(nn>0):
      st.append(nn%b)
      nn=(nn-st[-1])/b
  
    if sum(st)==s:
      b_ans=b
      break


if b_ans==1:
  for p in range(1,int(n**0.5)):
    b=int(n/p)
    q=n-p*b
    if s==p+q and q<b:
      b_ans=b
      break

if b_ans!=1:
  print(b_ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())

b_ans=1

if n==s:
  b_ans=n+1
if b_ans==1:
  for b in range(2,int(n**0.5)+1):
    nn=n
    st=[]
    while(nn>0):
      st.append(nn%b)
      nn=(nn-st[-1])/b
  
    if sum(st)==s:
      b_ans=b
      break


if b_ans==1:
  for p in range(1,int(n**0.5)):
    b=int(n/p)
    q=n-p*b
    if s==p+q:
      b_ans=b
      break

if b_ans!=1:
  print(b_ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
def sol():
    N = int(input())
    S = int(input())

    if N == S:
        print(N + 1)
        return

    def digitSum(n, b):
        if b < 2:
            return -1
        if n < b:
            return n
        return digitSum(n // b, b) + n % b

    for b in range(2, int(N**0.5) + 1):
        if digitSum(N, b) == S:
            print(b)
            return

    for p in range(1, int(N**0.5) + 1):
        b = (N - S) // p + 1
        if digitSum(N, b) == S:
            print(b)
            return
    print('-1')

sol()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int (input())
def f(b,num):
    # print('B:',b,'NUM:',num)
    if b>num:
        return num
    else:
        return (f(b,num//b)+num%b)
ans = []
run = 10
while(run<=n):
    # print('run:',run)
    a = f(run,n)
    ans.append([run,a])
    run*=10
if s==1:
    print(n)
else:
    for x,y in ans:
        if y == s:
            print(x)
            exit()
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = int(input())
import math

def Base_10_to_n(X, nn):
    X_dumy = X
    out = ''
    total = 0
    while X_dumy > 0:
        total = X_dumy % nn + total
        X_dumy = int(X_dumy / nn)
    return total

if S == 1:
  print(N)
  exit()
if N == S:
  print(10)
  exit()
  
for b in range(2, int(N**0.5)+1):
  if Base_10_to_n(N, b) == S:
    print(b)
    exit()

for b in range(int(N**0.5)+2, N):
  if N / b == S:
    print(b)
    exit()
  

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = int(input())
import math

def Base_10_to_n(X, nn):
    X_dumy = X
    out = ''
    total = 0
    while X_dumy > 0:
        total = X_dumy % nn + total
        X_dumy = int(X_dumy / nn)
    return total

if S == 1:
  print(N)
  exit()
if N == S:
  print(10)
  exit()
  
for b in range(2, int(N**0.5)+1):
  if Base_10_to_n(N, b) == S:
    print(b)
    exit()

#for b in range(int(N**0.5)+2, N):
  

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = int(input())
import math

def Base_10_to_n(X, nn):
    X_dumy = X
    out = ''
    total = 0
    while X_dumy > 0:
        out = str(X_dumy % nn)+out
        total = X_dumy % nn + total
        X_dumy = int(X_dumy / nn)
    return total
if S ==1:
  print(N)
  exit()
  
for b in range(2, math.floor(N**0.5) +1):
  if Base_10_to_n(N, b) == S:
    print(b)
    exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = int(input())
import math

def Base_10_to_n(X, nn):
    X_dumy = X
    out = ''
    total = 0
    while X_dumy > 0:
        out = str(X_dumy % nn)+out
        total = X_dumy % nn + total
        X_dumy = int(X_dumy / nn)
    return total
if S ==1:
  print(N)
  exit()
  
for b in range(2, N):
  if Base_10_to_n(N, b) == S:
    print(b)
    exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

fs = {}
def f( b, n ) :
	if b > n : return n
	if (b,n) in fs : return fs[b,n]
    
	flr = n//b    
	n_mod_b = n % b
	ret = f( b, flr ) + n_mod_b
	fs[ b, n ] = ret
	return ret


n = int( input() )
s = int( input() )

for b  in range( 2, n+1 ):
	if b > n :
		if n == s :
			print( b )
			exit()
		else :
			print( "-1" )
			exit()
	else:
		if f( b, n ) == s :
			print( b )
			exit()

print( "-1" )

Submission status: Wrong Answer

Solution code:

import sys


def check(num, s, b):
    sum_keta = 0
    while num > 0:
        sum_keta += num % b
        num //= b
    if sum_keta == s:
        return 1
    else:
        return 0


def main():
    num = int(input())
    s = int(input())

    if num == s:
        print(num + 1)
        sys.exit()

    for b in range(2, int(num ** 0.5) + 2):
        if check(num, s, b):
            print(b)
            sys.exit()

    for p in range(1, int(num ** 0.5) + 2):
        b = (num - s) // p + 1
        if b <= 1:
            continue
        if check(num, s, b):
            print(b)
            sys.exit()

    print(-1)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if s>n:
    print(-1)
    quit()
if s==n or s==1:
    print(n+1)
    quit()
def func(b,n):
    res=0
    while(n):
        res+=n%b
        n//=b
    return res
j=1
i=n
ans=10**30
while(i>1):
#for i in range(n,1,-1):
    k=func(i,n)
#k+x*j<i-x such x
#(j+1)*x<i-k
#x<(i-k)/(j+1)
    if (i-k)%(j+1)==0:
        x=max(0,(i-k)//(j+1)-1)
    else:
        x=max(0,(i-k)//(j+1))
    i-=x
    #print("i",i,x,k)
    l=func(i,n)
    i-=1
    if k<=s<=l and (l-s)%j==0:
        #print("WW")
        #print(i+1+(l-s)//j)
        #print(func(i+1+(l-s)//j,n))
        ans=min(ans,i+1+(l-s)//j)
    j+=1
if ans==10**30:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    if b <= 0:
        return -1
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

if s > n:
    print("-1")
    exit(0)
if s == n:
    print(n + 1)
    exit(0)

for b in range(2, 330000):
    if b * b > n:
        break
    if f(b, n) == s:
        print(b)
        exit(0)

rs = n
for b in range(1, 330000):
    if b * b > (n - s):
        break
    now = n // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = s // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = (n - s) // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = (n - s) // b + 1
    if f(now, n) == s:
        rs = min(rs, now)

print(rs)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    if b <= 0:
        return -1
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

if s > n:
    print("-1")
    exit(0)
if s == n:
    print(n + 1)
    exit(0)

for b in range(2, 330000):
    if b * b > n:
        break
    if f(b, n) == s:
        print(b)
        exit(0)

rs = n
for b in range(1, 330000):
    if b * b > n:
        break
    now = n // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = s // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = (n - s) // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = (n - s) // b + 1
    if f(now, n) == s:
        rs = min(rs, now)

print(rs)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    if b <= 0:
        return -1
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

mxns = max(n, s)
for b in range(2, 330000):
    if b * b > mxns:
        break
    if f(b, n) == s:
        print(b)
        exit(0)

rs = max(n, s)
for b in range(2, 330000):
    if b * b > mxns:
        break
    now = n // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = s // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = (n - s) // b
    if f(now, n) == s:
        rs = min(rs, now)

if n != 1 and f(n, n) == s:
    print(n)
    exit(0)
if f(n + 1, n) == s:
    print(n + 1)
    exit(0)
if f(rs, n) == s:
    print(rs)
    exit(0)
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    if b <= 0:
        return -1
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

for b in range(2, 330000):
    if f(b, n) == s:
        print(b)
        exit(0)

rs = max(n, s)
for b in range(2, 330000):
    if b * b > max(n, s):
        break
    now = n // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = s // b
    if f(now, n) == s:
        rs = min(rs, now)
    now = (n - s) // b
    if f(now, n) == s:
        rs = min(rs, now)

if n != 1 and f(n, n) == s:
    print(n)
    exit(0)
if f(n + 1, n) == s:
    print(n + 1)
    exit(0)
if f(rs, n) == s:
    print(rs)
    exit(0)
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

for b in range(2, 320000):
    if f(b, n) == s:
        print(b)
        exit(0)

if n != 1 and f(n, n) == s:
    print(n)
    exit(0)
if f(n + 1, n) == s:
    print(n + 1)
    exit(0)
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

for b in range(2, 500000):
    if f(b, n) == s:
        print(b)
        exit(0)
if f(n + 1, n) == s:
    print(n + 1)
    exit(0)
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

for b in range(2, 320000):
    if f(b, n) == s:
        print(b)
        exit(0)
if f(n + 1, n) == s:
    print(n + 1)
    exit(0)
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n):
    sm = 0
    while n > 0:
        sm += n % b
        n //= b
    return sm

for b in range(2, 320000):
    if f(b, n) == s:
        print(b)
        exit(0)
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n) :
    ret = 0
    while n != 0 :
        ret += n % b
        n //= b
        
    return ret

def solve(n, s) :
    if n == s :
        return n + 1
    if s > n :
        return -1
    
    b = 2
    while b * b <= n :
        if f(b, n) == s :
            return b
        b += 1
            
    p = -(-(n ** 0.5 - 1) // 1)
    while p > 0 :
        if (n - s) % p == 0 :
            b = 1 + (n - s) // p
            if p < b and 0 <= s - p < b :
                return b
            
        p -= 1
        
    return -1
            
print(solve(n, s))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n) :
    ret = 0
    while n != 0 :
        ret += n % b
        n //= b
        
    return ret

def solve(n, s) :
    if n == s :
        return n + 1
    if s > n :
        return -1
    
    b = 2
    while b * b < n :
        if f(b, n) == s :
            return b
        b += 1
            
    p = -(-(n ** 0.5 - 1) // 1)
    while p > 0 :
        if (n - s) % p == 0 :
            b = 1 + (n - s) // p
            if p < b and 0 <= s - p < b :
                return b
            
        p -= 1
        
    return -1
            
print(solve(n, s))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())

def f(b, n) :
    ret = 0
    while n != 0 :
        ret += n % b
        n //= b
        
    return ret

def solve(n, s) :
    if n == s :
        return n + 1
    if s > n :
        return -1
    
    b = 2
    while b * b < n :
        if f(b, n) == s :
            return b
        b += 1
            
    p = int(n ** 0.5 - 1)
    while p > 0 :
        if (n - s) % p == 0 :
            b = 1 + (n - s) // p
            if 2 <= b and p < b and 0 <= s - p < b :
                return b
            
        p -= 1
        
    return -1
            
print(solve(n, s))