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 numpy as np
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 n + 1
    m = int(np.sqrt(n)) + 1
    for b in range(2, m):
        if f(b, n) == s:
            return b
    for p in range(1, m):
        q = s - p
        if (n - q) % p != 0:
            continue
        b = (n - q) // p
        if (b > 1) and (f(b, n) == s):
            return b
    return -1

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

Submission status: Wrong Answer

Solution code:
import itertools
import math 
 
n,s = [int(input()) for _ in range(2)]
 
def n_base_sum(n,b):
    if(int(n/b)):
        return n_base_sum(int(n/b),b)+n%b
    else:
        return n%b
 
def search_p(n,s):
    for p in range(math.ceil(math.sqrt(n))+1,0,-1):
        b = (n-s)//p + 1
        if b >1:
            q = n % b
            if p+q ==s:
                return b
        
    return -1
 
ans =-1
flag = False
answers = []
if s == n:
    ans = n+1
    flag =True
    answers.append(ans)
for b in  range(2,math.ceil(math.sqrt(n))+1):
    if s== n_base_sum(n,b):
        ans = b
        flag = True
        answers.append(ans)
        break
ans = search_p(n,s)
if ans != -1:
    flag =True
    answers.append(ans)
 
 
if not flag:
    print(-1)
else:
    print(int(min(answers)))

Submission status: Wrong Answer

Solution code:
import itertools
import math 
 
n,s = [int(input()) for _ in range(2)]
 
def n_base_sum(n,b):
    if(int(n/b)):
        return n_base_sum(int(n/b),b)+n%b
    else:
        return n%b
 
def search_p(n,s):
    for p in range(math.ceil(math.sqrt(n))+1,0,-1):
        b = (n-s)//p + 1
        if b >0:
            q = n % b
            if p+q ==s:
                return b
        
    return -1
 
ans =-1
flag = False
answers = []
if s == n:
    ans = n+1
    flag =True
    answers.append(ans)
for b in  range(2,math.ceil(math.sqrt(n))+1):
    if s== n_base_sum(n,b):
        ans = b
        flag = True
        answers.append(ans)
        break
ans = search_p(n,s)
if ans != -1:
    flag =True
    answers.append(ans)
 
 
if not flag:
    print(-1)
else:
    print(int(min(answers)))

Submission status: Wrong Answer

Solution code:
import itertools
import math 

n,s = [int(input()) for _ in range(2)]

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

def search_p(n,s):
    for p in range(math.ceil(math.sqrt(n))+1,0,-1):
        b = (n-s)//p + 1
        q = n - p*b
        if p+q ==s and q < b:
            return b
    return -1

ans =-1
flag = False
answers = []
if s == n:
    ans = n+1
    flag =True
    answers.append(ans)
for b in  range(2,math.ceil(math.sqrt(n))+1):
    if s== n_base_sum(n,b):
        ans = b
        flag = True
        answers.append(ans)
        break
ans = search_p(n,s)
if ans != -1:
    flag =True
    answers.append(ans)


if not flag:
    print(-1)
else:
    print(int(min(answers)))

Submission status: Wrong Answer

Solution code:
import itertools
import math 

n,s = [int(input()) for _ in range(2)]

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

def search_p(n,s):
    for p in range(math.ceil(math.sqrt(n))+1,0,-1):
        b = (n-s)//p + 1
        q = n - p*b
        if p+q ==s and b !=0:
            return b
    return -1

ans =-1
flag = False
answers = []
if s == n:
    ans = n+1
    flag =True
    answers.append(ans)
for b in  range(2,math.ceil(math.sqrt(n))+1):
    if s== n_base_sum(n,b):
        ans = b
        flag = True
        answers.append(ans)
        break
ans = search_p(n,s)
if ans != -1:
    flag =True
    answers.append(ans)


if not flag:
    print(-1)
else:
    print(int(min(answers)))

Submission status: Wrong Answer

Solution code:
import itertools
import math 

n,s = [int(input()) for _ in range(2)]

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

def search_p(n,s):
    for p in range(math.ceil(math.sqrt(n))+1,0,-1):
        b = n//p
        q = n - b*p
        if p+q ==s:
            return b
    return -1

ans =-1
flag = False
answers = []
if s == n:
    ans = n+1
    flag =True
    answers.append(ans)
for b in  range(2,math.ceil(math.sqrt(n))+1):
    if s== n_base_sum(n,b):
        ans = b
        flag = True
        answers.append(ans)
        break
ans = search_p(n,s)
if ans != -1:
    flag =True
    answers.append(ans)


if not flag:
    print(-1)
else:
    print(int(min(answers)))

Submission status: Wrong Answer

Solution code:
import itertools
import math 

n,s = [int(input()) for _ in range(2)]

def calc_s(b,n):
    i = 0
    s = 0
    while(1):
        s+= n %b
        n = math.floor(n/b)
        i+=1
        if n ==0:
            break
    return s

def search_p(n,s):
    for p in range(1,math.ceil(math.sqrt(n))+1):
        b = n//p
        q = n - b*p
        if p+q ==s:
            return b
    return -1

if s == n:
    ans = n+1
else:
    flag = False
    for b in  range(2,math.ceil(math.sqrt(n))+1):
        if s== calc_s(b,n):
            ans = b
            flag = True
            break
    if not flag:
        ans = search_p(n,s)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import math 

n,s = [int(input()) for _ in range(2)]

def calc_s(b,n):
    i = 0
    s = 0
    while(1):
        s+= n %b
        n = math.floor(n/b)
        i+=1
        if n ==0:
            break
    return s

def search_p(n,s):
    for p in range(1,math.ceil(math.sqrt(n))+1):
        b = n//p
        q = n - b*p
        if p+q ==s:
            return b
    return -1

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

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import math 

n,s = [int(input()) for _ in range(2)]

def calc_s(b,n):
    i = 0
    s = 0
    while(1):
        s+= n %b
        n = math.floor(n/b)
        i+=1
        if n ==0:
            break
    return s

def search_p(n,s):
    for p in range(2,math.ceil(math.sqrt(n))+1):
        b = n//p
        q = n - b*p
        if b+q ==s:
            return b
    return -1

if s == n:
    print(1)
elif s < math.sqrt(n):
    for b in  range(2,math.ceil(math.sqrt(n))+1):
        if s== calc_s(b,n):
            print(b)
            break
else:
    ans = search_p(n,s)
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input());s=int(input())
def f(s,t):
    c=0
    while s!=0:
        c+=s%t
        s//=t
    return c
for i in range(2,int(n**0.5)+1):
    if n%(i-1)==s%(i-1):
        if f(n,i)==s:
            print(i)
            break
else:
    for i in range(int(n**0.5)+1,0,-1):
        if (n-s)%i==0:
            b=(n-s)//i+1
            if b>1:
                if i+n%b==s:
                    print(b)
                    break
    else:
        if n==s:
            print(n+1)
        else:
            print(-1)

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

for i in range(2,int(n**(0.5))):
    if f(i, n) == s:
        print(i)
        exit()

print(-1)

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

for i in range(2,n):
    if f(i, n) == s:
        print(i)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
def f(_b, _n):
    ret = 0
    while _n > 0:
        ret += _n % _b
        _n = _n // _b
    return ret
INF = 10 ** 12

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

ans = INF
if n >= s:
    max_digits = len(str(bin(n))) - 2
    # 3桁まで
    for k in range(max_digits, 2, -1):
        min_b = max(2, -(int(- (n ** (1 / k)))))
        max_b = int((n - s + 1) ** (1 / (k - 1)))
        for b in range(min_b, max_b + 1, 1):
            if f(b, n) == s:
                ans = b
                break
        if ans != INF:
            break

    if ans == INF:
        # 2桁
        for a1 in range(1, s + 1):
            if (n - s) < (a1 - 1) * a1:
                break
            if (n - s) % a1 == 0:
                a0 = s - a1
                b = (n - a0) // a1
                if a0 < b and (b * a1) == (n - a0) and b < ans:
                    ans = b

    if ans == INF:
        # 1桁
        if n == s:
            ans = n + 1
if ans == INF:
    ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10*5)

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

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

is_b = False
if n < s:
  print(-1)
elif n == s:
  print(n + 1)
else:
  b = 2
  is_b = False
  while b * b <= n:
    if s == f(b, n):
      is_b = True
      break
    b += 1
  if is_b:
    print(b)
  else:
    p = 1
    while p * p <= n:
      if (n - s) % p != 0:
        p += 1
        continue
      else:
        b = (n-s) // p + 1
        if s == f(b, n):
          is_b = True
          break
      p += 1
    if is_b:
      print(b)
    else:
      print(-1)

Submission status: Wrong Answer

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

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

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

for i in range(2, int(math.sqrt(n)+2)):
    t = f(i, n)
    if s == t:
        print(i)
        quit()

for x in range(1, int(math.sqrt(n)+10)):
    y = s - x
    b = (n-s)//x+1
    if x < b and y < b and f(b, n) == s:
        print(b)
        quit()
print(-1)

Submission status: Wrong Answer

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

if s == 1:
    print(n)
    quit()

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

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

for i in range(2, int(math.sqrt(n)+2)):
    t = f(i, n)
    if s == t:
        print(i)
        quit()

for x in range(1, int(math.sqrt(n)+10)):
    y = s - x
    if (n-s)%x == 0:
        b = (n-s)//x
        if x < b and y < b and x*b+y == n and x+y == s:
            print(b)
            quit()
print(-1)

Submission status: Wrong Answer

Solution code:
from math import sqrt
import sys
sys.setrecursionlimit(10**6)
def f(b, n):
	if n < b:
		return n
	else:
		return f(b, n // b) + n % b

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

if n < s:
	print(-1)
elif n == s:
	print(n+1)
else:
	divs = []
	for i in range(1, int(sqrt(n-s))+1):
		if (n-s)%i == 0:
			if i>2:
				divs.append(i)
			if i != (n-s)//i:
				divs.append((n-s)//i)
	divs.sort()
	for d in divs:
		if f(d+1, n) == s:
			print(d+1)
			break
	else:
		print(-1)

Submission status: Wrong Answer

Solution code:
from math import floor
from math import sqrt

def f(bb,nn):
    if(nn<bb): return nn
    else: return f(bb,floor(nn/bb))+(nn%bb)
        

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

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

for i in range(2,int(sqrt(n))+2):
    if s==f(i,n):
        print(i)
        exit()

for i in reversed(range(1,int(sqrt(n))+2)):
    if (n-s)%i == 0:
        tmpb=(n-s)//i+1
        if tmpb >= sqrt(n) and f(tmpb,n)==s: 
            print(tmpb)
            exit()

Submission status: Wrong Answer

Solution code:
from math import floor
from math import sqrt

def f(bb,nn):
    if(nn<bb): return nn
    else: return f(bb,floor(nn/bb))+(nn%bb)
        

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

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

for i in range(2,int(sqrt(n))+2):
    if s==f(i,n):
        print(i)
        exit()

ans=10**12
for i in range(1,int(sqrt(n))+2):
    if (n-s)%i == 0:
        tmpb=(n-s)/i+1
        if tmpb >= sqrt(n) and f(tmpb,n)==s: 
            tmpb=min(ans,tmpb)

if ans>=10**12:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from math import floor
from math import sqrt

def f(bb,nn):
    if(nn<bb): return nn
    else: return f(bb,floor(nn/bb))+(nn%bb)
        

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

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

for i in range(2,int(sqrt(n))):
    if s==f(i,n):
        print(i)
        exit()

ans=10**12
for i in range(1,int(sqrt(n))):
    if (n-s)%i == 0:
        tmpb=(n-s)/i+1
        if tmpb >= sqrt(n)+1 and f(tmpb,n)==s: 
            tmpb=min(ans,tmpb)

if ans>=10**12:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

def gcd(a,b):
  if b==0:
    return a
  if a<b:
    return gcd(b,a)
  return gcd(b,a%b)

def ok(x):
  ret=0
  n_=n
  while n_>0:
    ret+=n_%x
    n_//=x
  if s == ret:
    return True
  return False

g = gcd(n,abs(n-s))
from math import sqrt,floor
for x in range(1,floor(sqrt(g)+10)):
  if g%x ==0 and ok(x+1):
    print(x+1)
    exit()
      
rev = list(range(1,floor(sqrt(g)+10)))
rev.reverse()
for x in rev:
  if g%x == 0 and ok(g//x+1):
    print(g//x+1)
    exit()
      
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
def f(a,x):
  if a>x:
    return x
  else:
    return f(a,x//a)+(x%a)
  
for i in range(2,int(n**(1/2))+1):
  if f(i,n)==s:
    print(i)
    exit()

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

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
def f(a,x):
  if a>x:
    return x
  else:
    return f(a,x//a)+(x%a)
  
for i in range(2,int(n**(1/2))+1):
  if f(i,n)==s:
    print(i)
    exit()

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

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
def f(a,x):
  if a>x:
    return x
  else:
    return f(a,x//a)+(x%a)
  
for i in range(2,int(n**(1/2))+1):
  if f(i,n)==s:
    print(i)
    exit()

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

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
def f(a,x):
  if a>x:
    return x
  else:
    return f(a,x//a)+(x%a)
  
for i in range(2,int(n**(1/2))+1):
  if f(i,n)==s:
    print(i)
    exit()

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

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
def f(a,x):
  if a>x:
    return x
  else:
    return f(a,x//a)+(x%a)
  
for i in range(2,int(n**(1/2))+1):
  if f(i,n)==s:
    print(i)
    exit()

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

print(-1)

Submission status: Wrong Answer

Solution code:

import numpy as np

n = int(input())
s = int(input())
flag=False
for i in range(2,int(np.sqrt(n))+1):
    score = 0
    n1=n
    while n1!=0:
        score+=(n1%i)
        n1=int(n1/i)
    if score==s:
        flag=True
        break

if flag:
    print(i)
else:
    for i in range(1,int(np.sqrt(n))+1):
        b = (n-s)/i+1
        if int(b)==b and b>0:
            b=int(b)
            score = 0
            n1=n
            while n1!=0:
                score+=(n1%b)
                n1=int(n1/b)
            if score==s:
                flag=True
                break
    if flag:
        print(b)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
for b in range(2,n+1):
  x = n
  k = 1
  a = 0
  while x>0:
    a += x%b
    x = x//b
  if a == s:
    print(b)
    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())
if N==S:
    print(N+1)
else:
    for b in range(2,int(math.sqrt(N))+2):
        if f(b,N)==S:
            print(b)
            exit()
    for p in range(int(math.sqrt(N))+2):
        b=(N-S)//(p+1)
        if b<=1:
            continue
        if f(b,N)==S:
            print(b)
            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())
if N==S:
    print(N+1)
else:
    for b in range(2,int(math.sqrt(N)+2)):
        if f(b,N)==S:
            print(b)
            exit()
    print(-1)

Submission status: Wrong Answer

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

n = int(input())
s = int(input())
inf = float('inf')
ans = inf

for i in range(2,int(n**(1/2))+1):
    if digitsum(n,i) == s:
        ans = i
        break
if n == s:
    ans = n+1
else:
    for i in range(1,int(n**(1/2))):
        if i*i-i >= n-s:
            break
        if (n-s) % i == 0:
            if s-i >= (n-s)//i +1 or i > s:
                continue
            ans = min(ans,((n-s)//i)+1)
            
if ans == inf:
    print(-1)
else:
    print(int(ans))

Submission status: Wrong Answer

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

n = int(input())
s = int(input())
inf = float('inf')
ans = inf

for i in range(2,int(n**(1/2))+1):
    if digitsum(n,i) == s:
        ans = i
        break

for i in range(1,int(n**(1/2))):
    if i*i-i >= n-s:
        break
    if (n-s) % i == 0:
        if s-i >= (n-s)//i +1 or i > s:
            continue
        ans = min(ans,((n-s)//i)+1)
            
if ans == inf:
    print(-1)
else:
    print(int(ans))

Submission status: Wrong Answer

Solution code:
from math import floor, sqrt

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 s == n:
        return n + 1

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

    for p in reversed(range(1, floor(sqrt(n)) + 1)):
        if (n - s) % p == 0:
            b = (n - s) // p + 1
            q = n - p * b
            if q < b:
                return b

    return -1


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

print(solve(n, s))

Submission status: Wrong Answer

Solution code:
def digit_sum(n, s):
    if s > n:
        return -1
    if s == n:
        return n + 1

    b = 2
    while b**2 < n:
        if f(b, n) == s:
            return b

        b += 1

    p = int(n * 0.5)
    while p > 1:
        b = (n - s) // p + 1
        if b > 1 and f(b, n) == s:
            return b
        
        p -= 1

    return -1


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

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

def solve():
    n = int(input())
    s = int(input())
    result = digit_sum(n, s)
    print(result)

solve()

Submission status: Wrong Answer

Solution code:
def digit_sum(n, s):
    if s > n:
        return -1
    if s == n:
        return n + 1

    b = 2
    while b**2 < n:
        if f(b, n) == s:
            return b

        b += 1

    p = 1
    while p**2 < n:
        b = (n - s) // p + 1
        if b <= 1:
            break

        if f(b, n) == s:
            return b
        
        p += 1

    return -1


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

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

def solve():
    n = int(input())
    s = int(input())
    result = digit_sum(n, s)
    print(result)

solve()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def digit_sum(num:int, base:int) -> int:

    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

n = ni()
s = ni()

ans = -1

if s > n:
    ans = -1
  
else:
    base = 2
    exist = False
    while base*base <= n:
        ds = digit_sum(n,base)
        if ds == s:
            ans = base
            exist = True
            break
        
        base += 1
        
    
    if not exist:
        for p in range(base,0,-1):
            if (n-s+p)%p == 0 and s-p >= 0:
                b = (n-s+p)//p
                if b > p:
                    ans = b
                    exist = True
                    break
            
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def digit_sum(num:int, base:int) -> int:

    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

n = ni()
s = ni()

ans = -1

if s > n:
    ans = -1
    
elif s == 1:
    ans = n
    
else:
    base = 2
    exist = False
    while base*base <= n:
        ds = digit_sum(n,base)
        if ds == s:
            ans = base
            exist = True
            break
        
        base += 1
        
    
    if not exist:
        for p in range(base,0,-1):
            if (n-s+p)%p == 0 and s-p >= 0:
                b = (n-s+p)//p
                ans = b
                exist = True
                break
            
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def digit_sum(num:int, base:int) -> int:

    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

n = ni()
s = ni()

ans = -1

if s > n:
    ans = -1
    
elif s == 1:
    ans = n
    
else:
    base = 2
    exist = False
    while base*base <= n:
        ds = digit_sum(n,base)
        if ds == s:
            ans = base
            exist = True
            break
        
        base += 1
        
    
    if not exist:
        for p in range(base,0,-1):
            if (n-s-p)%p == 0:
                b = (n-s-p)//p
                ans = b
                exist = True
                break
            
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def digit_sum(num:int, base:int) -> int:

    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

n = ni()
s = ni()

ans = -1

if s > n:
    ans = -1
    
elif s == 1:
    ans = n
    
else:
    base = 2
    exist = False
    while base*base <= n:
        ds = digit_sum(n,base)
        if ds == s:
            ans = base
            exist = True
            break
        
        base += 1
        
    if not exist:
        ten = n//base
        one = n - (n//base)*base
        while ten > 0:
            while one >= 0:
                ds = ten + one
                if ds == s:
                    ans = base
                    exist = True
                    break
                
                one -= ten
                base += 1
                
            if exist:
                break
            
            ten = n//base
            one = n - (n//base)*base
            
print(ans)

Submission status: Wrong Answer

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

n = int(input())
s = int(input())
if n == s:
    print(n + 1)
else:
    for i in range(2, int(n ** .5) + 1):
        if f(i, n) == s:
            print(i)
            exit()
    for p in range(1, int(n ** .5)):
        if n - s >= 0 and (n - s) % p == 0:
            b = (n - s) / p + 1
            if f(b, n) == s:
                print(int(b))
                exit()
    print(-1)

Submission status: Wrong Answer

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

n = int(input())
s = int(input())
if n // 2 < s:
    print(-1)
elif n == s:
    print(n + 1)
elif s == 1:
    print(n)
else:
    for i in range(2, n // 2 + 1):
        if f(i, n) == s:
            print(i)
            exit()
    print(-1)

Submission status: Wrong Answer

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

if n < s:
    print(-1)
    exit()

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

print(1)

Submission status: Wrong Answer

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

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

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

for b in range(2,int(n**0.5)+1):
  if f(b,n)==s:
    print(b)
    break
else:
  for p in range(1,int(n**0.5)+1):
    b,m=divmod(n-s+p,p)
    if m==0 and b>=2 and f(b,n)==s:
      print(b)
      break
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

n = input()
s = input()

for i in range(2, 10 ** 6):
    num = n
    S = 0
    while num > 0:
        S += num % i
        num /= i
    if S == s:
        print i
        exit()

if n < s:
    print -1
    exit()

ans = -1
for p in range(1, 10 ** 6):
    if (n - s) % p == 0:
        i = (n - s) / p + 1
        q = s - p
        if p < i and 0 <= q < i:
            ans = (n - s) / p + 1

print ans

Submission status: Wrong Answer

Solution code:
def main():
    n, s = int(input()), int(input())

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

    for b in range(2, int(n**0.5)+1):
        x, sb = n, 0
        while 0 < x:
            sb += x % b
            x //= b
        if sb == s:
            print(b)
            return

    for p in range(1, int(n**0.5)+1):
        if (n-s) % p:
            continue
        b, x, sb = (n-s) // p + 1, n, 0
        while 0 < x:
            sb += x % b
            x //= b
        if sb == s:
            print(b)
            return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, s = int(input()), int(input())

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

    for b in range(2, int(n**0.5)):
        x, sb = n, 0
        while 0 < x:
            sb += x % b
            x //= b
        if sb == s:
            print(b)
            return

    for p in range(1, int(n**0.5)):
        if (n-s) % p:
            continue
        b, x, sb = (n-s) // p + 1, n, 0
        while 0 < x:
            sb += x % b
            x //= b
        if sb == s:
            print(b)
            return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def fromten(a,y):

    b=0

    while a!=0:

        b+=a%y

        a=a//y

    return b

 

f=True

for i in range(2,int(m**0.5)+1):

    if fromten(m,i)==s:

        print(i)

        f=False

        break

if f:

    z=m-s

    flag=False

    for a in range(int(m**0.5),0,-1):###  z+1 

        if z%a==0:

            q=z//a+1

            b=m-a*q

            q=z//a+1

            if a+b==s and a*q+b==m and b<q:

                print(q)

                flag=True

                break

    if not flag:

        if m%s==0:

            print(m//s)

        else:

            print(-1)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
s=i1()
a=[]
import math
def f(b,n):
    if n<b:
       return n
    else:
       return f(b,n//b)+n%b
if s==n:
  print(n+1)
elif s>n:
  print(-1)
else:
  for i in range(2,int(math.sqrt(n))+1):
      if f(i,n)==s:
         a.append(i)
  for i in range(1,int(math.sqrt(n))):
      if n>=n/i+1-s/i>int(math.sqrt(n)):
         if f(n/i+1-s/i,n)==s:a.append(n/i+1-s/i)
  if len(a):print(int(min(a)))
  else:print(-1)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
s=i1()
a=[]
import math
def f(b,n):
    if n<b:
       return n
    else:
       return f(b,n//b)+n%b
if s==n:
  print(n+1)
elif s>n:
  print(-1)
else:
  for i in range(2,int(math.sqrt(n))+1):
      if f(i,n)==s:
         a.append(i)
  for i in range(1,int(math.sqrt(n))+1):
      if n>=n/i+1-s/i>int(math.sqrt(n)):
         if f(n/i+1-s/i,n)==s:a.append(n/i+1-s/i)
  if len(a):print(int(min(a)))
  else:print(-1)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
s=i1()
a=[]
import math
def f(b,n):
    if n<b:
       return n
    else:
       return f(b,n//b)+n%b
if s==n:
  print(n+1)
elif s>n:
  print(-1)
else:
  for i in range(2,int(math.sqrt(n))+1):
      if f(i,n)==s:
         a.append(i)
  for i in range(1,int(math.sqrt(n))+1):
      if n/i+1-s/i>=int(math.sqrt(n)):
         if f(n/i+1-s/i,n)==s:a.append(n/i+1-s/i)
  if len(a):print(int(min(a)))
  else:print(-1)