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:
N=int(input())
S=int(input())
res = 0
for i in range(2,int(N**0.5)+1):
    tmp = N
    tmp2 = 0
    while tmp > 0:
        tmp2 += tmp%i
        tmp = tmp // i
    if tmp2==S:
        res = i
        break
if res == 0:
    for i in range(int(N**0.5)+1,1,-1):
        b = (N-S)//i + 1
        tmp = N
        tmp2 = 0
        while tmp > 0:
            tmp2 += tmp % i
            tmp = tmp // i
        if tmp2 == S:
            res = i
            break

if S==1:
    print(N)
elif res==0 or N<S:
    print(-1)
else:
    print(res)

Submission status: Wrong Answer

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

import math
def f(b, n):
    if n<b:
        return n
    else:
        x = f(b, n//b) + n%b
        return x
ans = 0
for b in range(2,int(math.sqrt(n))+500):
    if f(b, n) == s:
        ans = b
        break
print(ans if ans != 0 else -1)

Submission status: Wrong Answer

Solution code:
import math
def f(b,n):
    return n if n<b else f(b,n//b)+n%b
def main():
    n=int(input())
    s=int(input())
    n2=int(math.sqrt(n))
    if n==s:
        return print(n+1)
    for b in range(2, n2+1):
        if f(b,n)==s:
            return print(b)
    for p in range(n2,0,-1):
        if (n-s)%p==0:
             b = (n-s)//p+1
             if 0<=(s-p)<b:
                return print(b)
    return print('-1')
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
def f(b,n):
    return n if n<b else f(b,n//b)+n%b
def main():
    n=int(input())
    s=int(input())
    n2=int(math.sqrt(n))
    n2 = n2+1 if n2**2 < n else n2
    if n==s:
        return print(n+1)
    for b in range(2, n2):
        if f(b,n)==s:
            return print(b)
    for p in range(1,n2):
        if (n-s)%p==0:
             b = (n-s)//p+1
             if 0<=(s-p)<b:
                return print(b)
    return print('-1')
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
def f(b,n):
    return n if n<b else f(b,n//b)+n%b
def main():
    n=int(input())
    s=int(input())
    n2=int(math.sqrt(n))
    n2 = n2+1 if n2**2 < n else n2
    if n==s:
        return print(n+1)
    for b in range(2, n2+1):
        if f(b,n)==s:
            return print(b)
    for p in range(2,n2+1):
        if (n-s)%p==0 and 0<=(s-p)<b:
            return print((n-s)//p+1)
    return print('-1')
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
def f(b,n):
    return n if n<b else f(b,n//b)+n%b
def main():
    n=int(input())
    s=int(input())
    n2=int(math.sqrt(n))
    if n==s:
        print(n+1)
        return
    for b in range(2, n2+1):
        if f(b,n)==s:
            return print(b)
    for p in range(2,n2):
        if (n-s)%p==0 and 0<=(s-p)<b:
            return print((n-s)//p+1)
    return print('-1')
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:

Submission status: Wrong Answer

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

num = 1
b = 2
N = n

while num <= 100001:
    N = n
    S= ""
    sum1 =0
    while N//b > 0:
        
        S = S + str(N%b)
        
        sum1 += int(str(N%b))#
        #print(S,b,sum1)
        N = N//b
        #print(N,N//b)
        if N//b == 0:
            S +=str(N)
            sum1 += int(str(N%b))#
            #print(S,sum1)
        #print(S,b,sum1,s)
        
        if sum1 == s:
            print(b)
            exit()
    b +=1
    num +=1
print(-1)

Submission status: Wrong Answer

Solution code:
def solve(n, s):
    if n < s:
        return -1
    if n == s:
        return n + 1

    sqn = int((n - 1) ** 0.5) + 1
    # 3桁以上
    for b in range(2, sqn + 1):
        tmp = 0
        k = n
        while k > 0:
            d, m = divmod(k, b)
            tmp += m
            k = d
        if tmp == s:
            return b
    # 2桁
    for p in range(sqn, 0, -1):
        b, m = divmod(n - s, p)
        if m != 0:
            continue
        b += 1
        if p + n % b == s:
            return b
    return -1


n = int(input())
s = int(input())
print(solve(n, s))

Submission status: Wrong Answer

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

n=int(input())
s=int(input())
t=-1
for i in range(n-2):
  if f(i+2,n)==s:
    t=i+2
    break
print(t)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
sq = n**.5
ans = '-1'

for b in range(2, int(sq)+1):
	sum = 0
	tmp = n
	while tmp//b != 0:
		sum += tmp%b
		tmp //= b
	sum += tmp
	if sum == s:
		ans = b
		break

if ans == '-1':
	if sq.is_integer():
		sq -= 1 
	for p in range(int(sq), 0, -1):
		b = (n-s+p)/p
		if b.is_integer() and 0 <= (s-p) and (s-p) < b:
			ans = int(b)
			break

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
sq = int(n**.5)
ans = '-1'

for b in range(2, sq+1):
	sum = 0
	tmp = n
	while tmp//b != 0:
		sum += tmp%b
		tmp //= b
	sum += tmp
	if sum == s:
		ans = b
		break

if ans == '-1':
	for p in range(sq+1, 0, -1):
		b = (n-s+p)/p
		if b.is_integer() and 0 <= (s-p) and (s-p) < b:
			ans = int(b)
			break

print(ans)

Submission status: Wrong Answer

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

m = n
for b in range(2,m):
    if b > 2:
        x = 26 
    else:
        x = 37
    
    dp =[0]*x
    for i in range(x):
        dp[i]=b**i
        m = n
        if dp[i] >m:
            maxi = i
            m = n
            dp2 =[0]*(maxi+1)
            for j in range(maxi,-1,-1):                
                dp2[j] = m // dp[j]
                m = m % dp[j]
                if sum(dp2) == s:
                    print(b)
                    exit()
            
print(-1)

Submission status: Wrong Answer

Solution code:

"""
Writer: SPD_9X2
https://atcoder.jp/contests/abc044/tasks/arc060_b

f(b,n)ってbが大きくなるほど大きくなる？(単調増加になる?)
→そんなことはない
nより大きかったら不可能。最小はn進法で桁和1

3桁以上になる物は全探索する
2桁は、良い感じにすべて調べ上げる
実装だるそ～

2桁の時 xyとなるとする
n//b = x
n%b  = y

n = bx+y
x+y = s

(n-S)//x+1 = b
かつ S >= x >= S-b

xについてn-sの約数を全列挙して、bを求める & 下の条件を満たしているか確認すればよい？
"""

def f(b,n):

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

import sys
n = int(input())
s = int(input())

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

for b in range(2,int(n**0.5+10)):

    if f(b,n) == s:
        print (b)
        sys.exit()

#ここからだるいパート
ans = float("inf")

for x1 in range(1,10**6):

    if (n-s) % x1 == 0:

        x2 = (n-s)//x1

        b1 = (n-s)//x1+1
        if b1 ** 2 > n and s >= x1 >= s-b1:
            ans = min(ans , b1)

        b2 = (n-s)//x2+1
        if b2 **2 > n and s >= x2 >= s-b2:
            ans = min(ans , b2)

if ans == float("inf"):
    print (-1)
else:
    print (ans)

Submission status: Wrong Answer

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

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

def solve(N, S):
    if S > N:
        return -1
    elif 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, -1):
            if (N - S) % p == 0:
                b = (N - S) // p + 1
                print(b)
                if f(b, N) == S:
                    return b
        return -1

print(solve(N, S))

Submission status: Wrong Answer

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

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

def solve(N, S):
    if S > N:
        return -1
    elif 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):
            if (N - S) % p == 0:
                b = (N - S) // p + 1
                print(b)
                if f(b, N) == S:
                    return b
        return -1

print(solve(N, S))

Submission status: Wrong Answer

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

b_list = []    
    
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_kouho = syou + 1
    b_list.append(b_kouho)
  
if b_list != []:
  print(min(b_list))
else:
  print(-1)

Submission status: Wrong Answer

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

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 = syou + 1
    print(b)
    exit()
  
if b == -1:
  print(b)

Submission status: Wrong Answer

Solution code:
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))

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

if b == -1:
  print(b)

Submission status: Wrong Answer

Solution code:
import math

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

def _func(b,n):
  x = n
  ret = 0
  while True:
    if b > x:
      return ret + x
    else:
      ret += x % b
      x = math.floor(x/b)

def solve(n,s):
  if n == s:
    return n+1
  ans = float('inf')
  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 b < 2:
        continue
      if _func(b, n) == s:
        ans = min(ans, b)

  return -1

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

Submission status: Wrong Answer

Solution code:
import math

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

def _func(b,n):
  x = n
  ret = 0
  while True:
    if b > x:
      return ret + x
    else:
      ret += x % b
      x = math.floor(x/b)

def solve(n,s):
  if n == s:
    return n+1
  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 b < 2:
        continue
      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: Wrong Answer

Solution code:
import math

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

def _func(b,n):
  x = n
  ret = 0
  while True:
    if b > x:
      return ret + x
    else:
      ret += x % b
      x = math.floor(x/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 b < 2:
        continue
      if _func(b, n) == s:
        return b

  return -1

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

Submission status: Wrong Answer

Solution code:
import math

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

def _func(b,n):
  x = n
  ret = 0
  while True:
    if b > x:
      return ret + x
    else:
      ret += x % b
      x = math.floor(x/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 b < 2:
        continue
      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: Wrong Answer

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//=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]:
  q=s-p
  b=(n-s)//p+1
  if 1<=p<b and 1<=q<b and (n-s)%p==0:
    b=(n-s)//p+1
    if f(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(0)

m = int((n-1)**.5) + 2
for i in range(2, m):
  t = 0
  f = n
  while f:
    t += f % i
    f //= i
  if t == s:
    print(i)
    exit(0)

a = 10**12
for i in range(1, min(m, s+1)):
  j = s - i
  if (n - j) % i == 0:
    b = (n - j) // i
    if j < b:
      a = min(a, b)
print(a if a < 10**12 else -1)

Submission status: Wrong Answer

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()
if s>n:
  print(-1)
  exit()

A=[]
for i in range(2,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: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    n=I()
    s=I()
    
    if s==n:
        print(n+1)
    elif s>n:
        print(-1)
    else:
        #nをb進法にして，桁和を計算
        def calc(b,n):
            temp=0
            while n!=0:
                temp+=n%b
                n=n//b
            return temp
        
        ans=1000000000000
        N=1000000
        
        #まずはbを10**6程度まで探索
        for b in range(2,N):
            if calc(b,n)==s:
                ans=b
                break
            
        if ans!=1000000000000:
            print(ans)
            
        #もし，b>10**6で，うまく表現できるならばそれは2桁となる，b>10**6より，上位桁は最高でも10**6程度(max_n=10**11だから)
        else:
            #a1が上位の桁,a0が下位の桁
            for a1 in range(1,N):
                a0=s-a1
                if a0<0:
                    break
                if (n-a0)%a1==0:
                    b=(n-a0)//a1
                    if b>1000000:
                        ans=min(ans,b)
                        
            if ans!=1000000000000:
                print(ans)
            else:
                print(-1)
        
        
    
    
    
    
    
    
            
    
        

main()

Submission status: Wrong Answer

Solution code:
import math
import sys

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)
elif s > n:
    print(-1)
else:
    #sqrt(n)まで探索
    for i in range(2,int(up)+1):
        if colsum(n,i) == s:
            print(i)
            sys.exit()

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

    print(-1)

Submission status: Wrong Answer

Solution code:
import math
import sys

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)
elif s > n:
    print(-1)
else:
    #sqrt(n)まで探索
    for i in range(2,int(up)+1):
        if colsum(n,i) == s:
            print(i)
            sys.exit()

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

    print(-1)

Submission status: Wrong Answer

Solution code:
import math
import sys

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)
elif s > n:
    print(-1)
else:
    #sqrt(n)まで探索
    for i in range(2,int(up)+1):
        if colsum(n,i) == s:
            print(i)
            sys.exit()

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

    print(-1)

Submission status: Wrong Answer

Solution code:
import math
import sys

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

up = math.sqrt(n)

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

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

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

    print(-1)

Submission status: Wrong Answer

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)
elif s > n:
    print(-1)
else:
    #sqrt(n)まで探索
    for i in range(2,int(up)+1):
        if colsum(n,i) == s:
            print(i)
            sys.exit()

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

    print(-1)

Submission status: Wrong Answer

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)
elif s > n:
    print(-1)
else:
    #sqrt(n)まで探索
    for i in range(2,int(up)+1):
        if colsum(n,i) == s:
            print(i)
            sys.exit()

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

    print(-1)

Submission status: Wrong Answer

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(int(up)+1,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: Wrong Answer

Solution code:
n = int(input())
s = int(input())
x = int(n**0.5)+1
def dsum(x,t):
  ret = 0
  while x>0:
    ret += x%t
    x //= t
  return ret
for i in range(2,x+1):
  if dsum(n,i) == s:
    print(i)
    exit()
for i in range(x,0,-1):
  if (n-(s-i))%i == 0:
    digit = (n-(s-i))//i
    if 0 < i < digit and 0 <= s-i < digit:
      print((n-(s-i))//i,i)
      exit()
if n == s:
  print(n+1)
  exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
x = int(n**0.5)+1
def dsum(x,t):
  ret = 0
  while x>0:
    ret += x%t
    x //= t
  return ret
for i in range(2,x+1):
  if dsum(n,i) == s:
    print(i)
    exit()
for i in range(x,0,-1):
  if (n-(s-i))%i == 0:
    digit = (n-(s-i))//i
    if 0 < i < digit and 0 <= s-i < digit:
      print((n-(s-i))//i)
      exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
x = int(n**0.5)+1
def dsum(x,t):
  ret = 0
  while x>0:
    ret += x%t
    x //= t
  return ret
for i in range(2,x+1):
  if dsum(n,i) == s:
    print(i)
    exit()
for i in range(x,0,-1):
  if s-i>=0:
    if (n-(s-i))%i == 0:
      if (n-(s-i))//i > x:
        print((n-(s-i))//i)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
x = int(n**0.5)+1
def dsum(x,t):
  ret = 0
  while x>0:
    ret += x%t
    x //= t
  return ret
for i in range(2,x+1):
  if dsum(n,i) == s:
    print(i)
    exit()
for i in range(x,0,-1):
  if (n-(s-i))%i == 0:
    print((n-(s-i))//i)
    exit()

Submission status: Wrong Answer

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 sqrt(n: int):
    lb = 0
    hb = n + 1
    while hb - lb > 1:
        m = (lb + hb) // 2
        if m * m <= n:
            lb = m
        else:
            hb = m
    return lb


def solve(n: int, s: int):
    if s == n:
        print(n + 1)
        return
    sn = sqrt(n)
    for b in range(2, sn + 1):
        if f(b, n) == s:
            print(b)
            return
    for p in reversed(range(1, sn)):
        q = s - p
        if q < 0:
            continue
        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: Wrong Answer

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,-1):
        b = float(n-s)/float(p)-1.0
        if abs(b-int(b))<10**(-30):
            print(int(b))
            No=1
            exit()
if No==0:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
A = []
if n >= s:
  for i in range(1, int((n-s)**(1/2))+1):
    if (n - s) % i == 0:
      if (n - s) // i != i:
        A.append(i+1)
        A.append((n - s) // i + 1)
      else:
        A.append(i+1)
def digit_sum(b, m):
  if m < b:
    return m
  else:
    return digit_sum(b, m // b) + m % b
B = []
for i in A:
  if digit_sum(i, n) == s:
    B.append(i)
if B == [] or n < s:
  print(-1)
elif n == s:
  print(n + 1)
else:
  print(min(B))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
A = []
if n >= s:
  for i in range(1, int((n-s)**(1/2))+1):
    if (n - s) % i == 0:
      if (n - s) // i != i:
        A.append(i+1)
        A.append((n - s) // i + 1)
      else:
        A.append(i+1)
def digit_sum(b, m):
  if m < b:
    return m
  else:
    return digit_sum(b, m // b) + m % b
B = []
for i in A:
  if digit_sum(i, n) == s:
    B.append(i)
if B == [] or n < s:
  print(-1)
elif n == s:
  print(n)
else:
  print(min(B))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
A = []
if n >= s:
  for i in range(1, int((n-s)**(1/2))+1):
    if (n - s) % i == 0:
      if (n - s) // i != i:
        A.append(i+1)
        A.append((n - s) // i + 1)
      else:
        A.append(i+1)
def digit_sum(b, m):
  if m < b:
    return m
  else:
    return digit_sum(b, m // b) + m % b
B = []
for i in A:
  if digit_sum(i, n) == s:
    B.append(i)
if B == [] or n < s:
  print(-1)
else:
  print(min(B))

Submission status: Wrong Answer

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

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

if n<s:
    print(-1)
elif n==s:
    print(n+1)
else:
    for i in range(2,int(math.sqrt(n))+1):
        if f(i,n)==s:
            print(i)
            break
    else:
        for p in range(int(math.sqrt(n)),0,-1):
            if (n-s)%p==0:
                b=(n-s)//p+1
                print(b)
                print(f(b,n))
                if f(b,n)==s:
                    print(b)
                    break
        else:
            print(-1)

Submission status: Wrong Answer

Solution code:
import math 
import bisect
import sys
def f(b,n):
    m=math.floor(n/b)
    if n>=b:
        return f(b,m) + n%b
    else:
        return n
n=int(input())
s=int(input())
for i in range(2,int(math.sqrt(n))+1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
if n-int(math.sqrt(n))<2:
    l=2
else:
    l=n-int(math.sqrt(n))
for i in range(n+1,l-1,-1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
print(-1)

Submission status: Wrong Answer

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

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

if n<s:
    print(-1)
elif n==s:
    print(n+1)
else:
    for i in range(2,int(math.sqrt(n))+1):
        if f(i,n)==s:
            print(i)
            break
    else:
        for p in range(int(math.sqrt(n)),0,-1):
            q=s-p
            if (n-q)%p==0:
                b=(n-q)//p
                if f(b,n)==s:
                    print(b)
                    break
                #print(b)
        else:
            print(-1)

Submission status: Wrong Answer

Solution code:
import math 
import bisect
import sys
def f(b,n):
    m=math.floor(n/b)
    if n>b:
        return f(b,m) + n%b
    else:
        return n
n=int(input())
s=int(input())
for i in range(2,int(math.sqrt(n))+1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
if n-int(math.sqrt(n))<2:
    l=2
else:
    l=n-int(math.sqrt(n))
for i in range(n+1,l-1,-1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math 
import bisect
import sys
def f(b,n):
    m=math.floor(n/b)
    if n>b:
        return f(b,m) + n%b
    else:
        return n
n=int(input())
s=int(input())
for i in range(2,int(math.sqrt(n))+1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
for i in range(n,min(n-int(math.sqrt(n)),1),-1):
    #print(i)
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math 
import bisect
import sys
def f(b,n):
    m=math.floor(n/b)
    if n>=b:
        return f(b,m) + n%b
    else:
        return n
n=int(input())
s=int(input())
for i in range(2,int(math.sqrt(n))+1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
if s>=n//2:
    print(-1)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
import math 
import bisect
import sys
def f(b,n):
    m=math.floor(n/b)
    if n>=b:
        return f(b,m) + n%b
    else:
        return n
n=int(input())
s=int(input())
for i in range(2,int(math.sqrt(n))+1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
if s>=int(math.sqrt(n)):
    print(-1)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
import math 
import bisect
import sys
def f(b,n):
    m=math.floor(n/b)
    if n>=b:
        return f(b,m) + n%b
    else:
        return n
n=int(input())
s=int(input())
for i in range(2,int(math.sqrt(n))+1):
    x=f(i,n)
    if x==s:
        print(i)
        sys.exit()
if s>int(math.sqrt(n)):
    print(-1)
else:
    print(n)

Submission status: Wrong Answer

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