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: Runtime Error

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

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

def solve(N, S):
    if N == S:
        return N+1
    else:
        for b in range(2, int(N**(1/2))+1):
            if f(b, N) == S:
                return b
        for p in range(1, int(N**(1/2))+1):
            b = int((N - S) / p + 1)
            if f(b, N) == S:
                return b
        return -1

print(solve(N, S))

Submission status: Runtime Error

Solution code:
import math

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

b = -1

def keisan(k, w):
  l = []
  while k >= w:
    l.append(k % w)
    k = k // w
  else:
    l.append(k)
  return (sum(l))

if n == s:
  b = n + 1
  print(b)
  exit()
  
if n < s:
  b = -1
  print(b)
  exit()

n_r = math.floor(math.sqrt(n))

for i in range(2, n_r+1):
  if keisan(n, i) == s:
    b = i
    print(b)
    exit()

for i in range(1, n_r+1):
  w = n - s
  syou = w // i
  amari = w % i
  if syou > n_r-1 and amari == 0:
    b_list = []
    b_kouho = syou + 1
    b_list.append(b_kouho)
  
if b_list != []:
  print(min(b_list))
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import math

def func(b,n):
  if b > n:
    return n
  else:
    return func(b, math.floor(n/b)) + (n % b)

def solve(n,s):
  for b in range(2,math.floor(math.sqrt(n))+1):
    if func(b,n) == s:
      return b

  for p in range(1,math.floor(math.sqrt(n))+1):
    if (n-s) % p == 0:
      b = int((n-s)/p)+1
      if func(b, n) == s:
        return b

  return -1

if __name__  == "__main__":
  n = int(input())
  s = int(input())
  ans = solve(n,s)
  print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=int(input())
if s==n:
  print(n+1)
  exit()
def f(x):
  m=n
  t=[]
  while m>0:
    y=m%x
    t.append(y)
    m-=m%x
    m//=x
  return sum(t)
for b in range(2,int(n**0.5)+1):
  if f(b)==s:
    print(b)
    exit()
for p in range(1,int(n**0.5)+1):
  if (n-s)%p==0:
    b=(n-s)//p+1
    if f(b)==s:
      print(b)
      exit()
print(-1)

Submission status: Runtime Error

Solution code:
def D(n,d):
  r=0
  while n>0:
    r=r+n%d
    n=n//d
  return r

n=int(input())
s=int(input())
if n-s==0:
  print(n+1)
  exit()

A=[]
for i in range(1,int((n-s)**(1/2))+1):
  if (n-s)%i==0:
    A.append(i)
for i in range(len(A)):
  if D(n,A[i]+1)==s:
    print(A[i]+1)
    exit()
print(-1)

Submission status: Runtime Error

Solution code:
def D(n,d):
  r=0
  while n>0:
    r=r+n%d
    n=n//d
  return r

n=int(input())
s=int(input())
if n-s==-1:
  print(n)
  exit()
A=[]
for i in range(1,int((n-s)**(1/2))+3):
  if (n-s)%i==0:
    A.append(i)
for i in range(len(A)):
  if D(n,A[i]+1)==s:
    print(A[i]+1)
    exit()
print(-1)

Submission status: Runtime Error

Solution code:
def D(n,d):
  r=0
  while n>0:
    r=r+n%d
    n=n//d
  return r

n=int(input())
s=int(input())
A=[]
for i in range(1,int((n-s)**(1/2))+3):
  if (n-s)%i==0:
    A.append(i)
for i in range(len(A)):
  if D(n,A[i]+1)==s:
    print(A[i]+1)
    exit()
print(-1)

Submission status: Runtime Error

Solution code:
from math import floor, sqrt, ceil


def f(b, n):
    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


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

if n == s:
    print(n + 1)
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if f(b, n) == s:
            print(b)
            exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10000000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s >= n
if s == n:
    print(n+1)
    exit()
elif s > n:
    print(-1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(1,int(up)+1)[::-1]:
    if (n-s)%j == 0:
        b = (n-s)/j + 1
        if s == colsum(n,b) and b > 1:
            print(i)
            exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10000000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num <= base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s >= n
if s == n:
    print(n+1)
    exit()
elif s > n:
    print(-1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(1,int(up)+1):
    b = int((n-s)/j) + 1
    if s == colsum(n,b) and b > 1:
        print(i)
        exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10000000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s = n(n<b)
if s == n:
    print(n+1)
    exit()

#s > n
if s > n:
    print(-1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(1,int(up)+1):
    b = int((n-s)/j) + 1
    if s == colsum(n,b) and b > 1:
        print(i)
        exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10000000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s = n(n<b)
if s == n:
    print(n+1)
    exit()

#s > n
if s == n:
    print(-1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(1,int(up)+1):
    b = int((n-s)/j) + 1
    if s == colsum(n,b) and b > 1:
        print(i)
        exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10000000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s = n(n<b)
if s == n:
    print(n+1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(1,int(up)+1):
    b = int((n-s)/j) + 1
    if s == colsum(n,b) and b > 1:
        print(i)
        exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10000000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s = n(n<b)
if s == n:
    print(n+1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(1,int(up)+1):
    if (n-s)%j == 0:
        b = (n-s)/j + 1
        if s == colsum(n,b) and b > 1:
            print(i)
            exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(100000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s = n(n<b)
if s == n:
    print(n+1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(2,int(up)+1):
#    if (n-s)%j == 0:
        b = (n-s)//j + 1
        if s == colsum(n,b) and b > 1:
            print(i)
            exit()

print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(100000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return num%base + colsum(num//base,base)

#s = n(n<b)
if s == n:
    print(n+1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(2,int(up)+1):
    if (n-s)%j == 0:
        b = (n-s)/j + 1
        if s == colsum(n,b) and b > 1:
            print(i)
            exit()



print(-1)

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(100000)

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

up = math.sqrt(n)

def colsum(num, base):
    if num < base:
        return num
    else:
        return colsum(math.floor(num/base),base)+num%base

#s = n
if s == n:
    print(n+1)
    exit()


#sqrt(n)まで探索
for i in range(2,int(up)+1):
    if s == colsum(n,i):
        print(i)
        exit()

#2桁しかあり得ない場合(sqrt(n)<b<n)
for j in range(2,int(up)+1):
    if (n-s)%j == 0:
        b = (n-s)/j + 1
        if s == colsum(n,b):
            print(i)
            exit()



print(-1)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def f(b: int, n: int) -> int:
    if n < b:
        return n
    return f(b, n // b) + n % b


def solve(n: int, s: int):
    for b in range(2, int(n ** 0.5) + 1):
        if f(b, n) == s:
            print(b)
            return
    for p in reversed(range(int(n ** 0.5))):
        q = s - p
        if p == 0:
            b, r = n + 1, 0
        else:
            b, r = divmod(n - q, p)
            if r != 0 or b == 1:
                continue
        if f(b, n) == s:
            print(b)
            return
    print(-1)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    s = int(next(tokens))  # type: int
    solve(n, s)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def f(b: int, n: int) -> int:
    if n < b:
        return n
    return f(b, n // b) + n % b


def solve(n: int, s: int):
    for b in range(2, int(n ** 0.5) + 1):
        if f(b, n) == s:
            print(b)
            return
    for p in reversed(range(int(n ** 0.5))):
        q = s - p
        if p == 0:
            b, r = n + 1, 0
        else:
            b, r = divmod(n - q, p)
        if r != 0:
            continue
        if f(b, n) == s:
            print(b)
            return
    print(-1)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    s = int(next(tokens))  # type: int
    solve(n, s)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

No=0
if n==s:
    print(n+1)
    No =1
else:
    for b in range(2,int(n**0.5)+2):
        m=0
        N=n
        while N!=0:
            m+=N%b
            N=N//b
            if m>s:
                break    
        if m==s:
            print(b)
            No=1
            exit()
    for p in range(int(n**0.5-1),0,-1):
        b = float(n-s)/float(p)+1.0
        if abs(b-int(b))<10**(-10):
            b=int(b)
            m=0
            N=n
            while N!=0:
                m+=N%b
                N=N//b
                if m>s:
                    break    
            if m==s:
                print(b)
                No=1
                exit()
if No==0:
    print(-1)

Submission status: Runtime Error

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

No=0
if n==s:
    print(n+1)
    No =1
else:
    for b in range(2,int(n**0.5)+1):
        m=0
        N=n
        while N!=0:
            m+=N%b
            N=N//b
            if m>s:
                break    
        if m==s:
            print(b)
            No=1
            exit()
    for p in range(int(n**0.5-1),0,-1):
        b = float(n-s)/float(p)+1.0
        if abs(b-int(b))<10**(-10):
            b=int(b)
            m=0
            N=n
            while N!=0:
                m+=N%b
                N=N//b
                if m>s:
                    break    
            if m==s:
                print(b)
                No=1
                exit()
if No==0:
    print(-1)

Submission status: Runtime Error

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)
  
print(Base_10_to_n(input(),input()))

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    S = I()
    
    if N==S:
        print(N+1)
        return 

    def check(N,b):
        res = 0
        while N:
            res += N%b
            N//=b
        return res == S


    for b in range(2,int(sqrt(N)+1)):
        if check(N,b):
            print(b)
            return
    for p in range(1,int(sqrt(N)+1))[::-1]:
        b = (N-S+p)//p
        if check(N,b):
            print(b)
            return
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    S = I()
    
    if N==S:
        print(N+1)
        return 

    def check(N,b):
        res = 0
        while N:
            res += N%b
            N//=b
        return res == S


    for b in range(2,int(sqrt(N)+1)):
        if check(N,b):
            print(b)
            return
    raise Exception
    for p in range(1,int(sqrt(N))):
        b = (N-S)//p + 1
        if check(N,b):
            print(b)
            return 
        else:
            continue
        if S < p:
            break
        q = S - p
        N_ = N - q

        if N_ % p == 0 and 0 <= q < N_//p:
            print(N_//p)
            return
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    S = I()
    
    if N==S:
        print(N+1)
        return 

    def check(N,b):
        res = 0
        while N:
            res += N%b
            N//=b
        return res == S


    for b in range(2,int(sqrt(N)+1)):
        if check(N,b):
            print(b)
            return
    for p in range(1,int(sqrt(N))):
        b = (N-S)//p + 1
        if check(N,b):
            print(b)
            return 
        else:
            continue
        if S < p:
            break
        q = S - p
        N_ = N - q

        if N_ % p == 0 and 0 <= q < N_//p:
            print(N_//p)
            return
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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())


def find_b(n, s):
    if n == s:
        return -1
    p = int(math.sqrt(n))
    i = 2
    while i < (n-s)/p + 1:
        if f(i, n) == s:
            return i
        i += 1
    while p >= 1:
        b = int((n-s)/p + 1)
        if f(b, n) == s:
            return b
        p -= 1
    return -1


print(find_b(n, s))

Submission status: Runtime Error

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())


def find_b(n, s):
    p = int(math.sqrt(n))
    i = 2
    while i < (n-s)/p + 1:
        if f(i, n) == s:
            return i
        i += 1
    while p >= 1:
        b = int((n-s)/p + 1)
        if f(b, n) == s:
            return b
        p -= 1
    return -1


print(find_b(n, s))

Submission status: Runtime Error

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

ans = float('inf')

for b in range(2, 10**6):
    res = n
    tmp = 0
    while res > 0:
        tmp += res % b
        res = (res-res % b)//b
    if tmp == s:
        ans = min(ans, b)

if n < 10**6:
    print(ans if ans != float('inf') else -1)
    exit()

for p in range(1, 10**6):
    if (n-s) % p != 0:
        continue
    else:
        b = (n-s)//p + 1
        if n % b == s-p:
            ans = min(ans, b)
        
print(ans if ans != float('inf') else -1)

Submission status: Runtime Error

Solution code:
n,s=int(input())
if n*2<=s+1:
  print(s-n+1)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
s = int(input())
ans = -1

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

if n < s:
    ans = -1
elif n == s:
    ans = n+1
else:
    for b in range(2,int(math.sqrt(n))):
      if func(b, n) == s:
          ans = b
          break

    if ans == -1:
        for p in range(1,int(math.sqrt(n))):
            if func((n-s)/p+1, n) == s:
                ans = int((n-s)/p+1)
print(ans)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
s = int(input())
ans = -1

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

if n > s:
    ans = -1
elif n == s:
    ans = n+1
else:
    for b in range(2,int(math.sqrt(n))):
      if func(b, n) == s:
          ans = b
          break

    if ans == -1:
        for p in range(1,int(math.sqrt(n))):
            if func((n-s)/p+1, n) == s:
                ans = int((n-s)/p+1)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()

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

def resolve():
    n=int(input())
    s=int(input())

    if(s>n):
        print(-1)
        return
    if(s==n):
        print(n+1)
        return

    k=int(n**.5)
    for b in range(2,k+1):
        if(f(b,n)==s):
            print(b)
            return

    B=[]
    for p in range(1,b):
        q=s-p
        if(0>q or b-1<q): continue
        if((n-q)%p): continue
        t=(n-q)//p
        if(k<t<=n):
            B.append(t)

    print(min(B) if(B) else -1)
resolve()

Submission status: Runtime Error

Solution code:
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

n = int(input())
s = int(input())
flag = 0
if n < s:
    print(-1)
    flag = 1
if flag == 0 and n == s:
    print(n+1)
    flag = 0
if flag == 0:
    for i in range(2, int(n**0.5)+2):
        if f(i, n) == s:
            print(i)
            flag = 1
            break
if flag == 0:
    for i in range(1, int(n**0.5)+2):
        if (n-s)%i == 0:
            if f((n-s)//i+1, n) == s:
                print((n-s)//i+1)
                flag = 1
                break

if flag == 0:
    print(-1)

Submission status: Runtime Error

Solution code:
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

n = int(input())
s = int(input())
flag = 0
if n < s:
    print(-1)
    flag = 1

if flag == 0:
    for i in range(2, int(n**0.5)+2):
        if f(i, n) == s:
            print(i)
            flag = 1
            break
if flag == 0:
    for i in range(1, int(n**0.5)+2):
        if (n-s)%i == 0:
            if f((n-s)//i+1, n) == s:
                print((n-s)//i+1)
                flag = 1
                break

if flag == 0:
    print(-1)

Submission status: Runtime Error

Solution code:
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

n = int(input())
s = int(input())
flag = 0
if s == 1:
    print(n)
    flag=1
if n == 1:
    print(-1)
    flag=1
if flag == 0:
    for i in range(2, int(n**0.5)+2):
        if f(i, n) == s:
            print(i)
            flag = 1
            break
if flag == 0:
    for i in range(2, int(n**0.5)+2):
        if (n-s)%i == 0:
            if f((n-s)//i+1, n) == s:
                print((n-s)//i+1)
                flag = 1
                break

if flag == 0:
    print(-1)

Submission status: Runtime Error

Solution code:
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

n = int(input())
s = int(input())
flag = 0
if s == 1:
    print(n)
    flag=1
if n == 1:
    print(-1)
    flag=1
for i in range(2, int(n**0.5)+2):
    if f(i, n) == s:
        print(i)
        flag = 1
        break
if flag == 0:
    for i in range(2, int(n**0.5)+2):
        if (n-s)%i == 0:
            if f((n-s)//i+1, n) == s:
                print((n-s)//i+1)
                flag = 1
                break

if flag == 0:
    print(-1)

Submission status: Runtime Error

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
    else:
        sqrt_n = int(math.sqrt(n))
        for b in range(2, sqrt_n + 1):
            if digit_sum(b, n) == s:
                return b
        b_min = 1L << 55
        for p in range(1, sqrt_n):
            if (n - s) % p == 0:
                b = int((n - s) / p + 1)
                if digit_sum(b, n) == s:
                    b_min = min(b_min, b)
            p += 1
    if b_min == 1L << 55:
        return -1
    else:
        return b_min


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

Submission status: Runtime Error

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 n == s:
        return n + 1
    elif s == 1:
        return n
    elif s > n:
        return -1
    else:
        sqrt_n = int(math.sqrt(n))
        for b in range(2, sqrt_n + 1):
            if digit_sum(b, n) == s:
                return b
        for p in range(1, sqrt_n + 1):
            b = (n - s) // (p + 1)
            if digit_sum(b, n) == s:
                return b
    return -1


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

Submission status: Runtime Error

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,0,-1):
            bcandi = (n-s)//p + 1
            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: Runtime Error

Solution code:
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for i in range(2,loop+1):
            cur = n
            ansC = 0
            while cur>0:
                ansC += cur%i
                cur = cur//i
            if ansC==s:
                ans = i
                break
    if ans==-1:
        for p in range(loop,0,-1):
            bcandi = (n-s)/p + 1
            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: Runtime Error

Solution code:
def examD():
    n = I(); s = I()
    ans = -1
    if s==n:
        ans = n+1
    else:
        loop = int(n**(0.5))
        for i in range(2,loop+1):
            cur = n
            ansC = int(0)
            while cur>0:
                ansC += cur%i
                cur = cur//i
            if ansC==s:
                ans = i
                break
    if ans==-1:
        for p in range(1,loop+1):
            bcandi = (n-s)/p + 1
            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: Runtime Error

Solution code:
import math
import sys
import math

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

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

if(s==n):
    print(n+1)
    sys.exit()
for b in range(2,int(math.sqrt(n))):
    if(f(b,n)==s):
        print(b)
        sys.exit()
    
for b in range(int(math.sqrt(n)),n+1):
    b=(n-s)//b+1
    if(f(b,n)==s):
        print(b)
        sys.exit()
print(-1)

'''
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),0,-1):
        b = (N-S)//i + 1
        if b>1:
            if f(b,N)==S:
                ans = b
                break
 
print(ans)
'''

Submission status: Runtime Error

Solution code:
import math
import sys
import math

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

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

if(s==n):
    print(n+1)
    sys.exit()
for b in range(2,int(math.sqrt(n))):
    if(f(b,n)==s):
        print(b)
        sys.exit()
    
for b in range(int(math.sqrt(n)),n+1):
    if(f(b,n)==s):
        print(b)
        sys.exit()
print(-1)

Submission status: Runtime Error

Solution code:
import math
 

def f(n, base):
    X_dumy = base
    lis = []
    while X_dumy > 0:
        lis.append(X_dumy % n)
        X_dumy = X_dumy // n
    return sum(lis)
 
 
def solve():
    N, S = int(input()), int(input())
    # print(N, S)
 
    if N == S:
        return N + 1
 
    for b in range(2, int(math.sqrt(N)) + 1):
        if f(b, N) == S:
            return b
 
    largest = N**2
    lowest = largest
    for p in range(1, int(math.sqrt(N)) + 1):
        b = int((N - S) / p + 1)
        if f(b, N) == S:
            lowest = min(lowest, b)
 
    if not lowest == largest:
        return lowest
 
    return -1
 

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