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:
from math import sqrt


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


def divisors(P):
    res = set([])
    for i in range(1, P+1):
        if i**2 > P:
            break
        else:
            if P % i == 0:
                res.add(i)
                res.add(P//i)
    return res


def main():
    N = int(input())
    S = int(input())
    res = 10**18
    if N <= 10**6:
        for b in range(2, N+2):
            if f(b, N) == S:
                res = b
                break
        if res < 10**18:
            print(res)
        else:
            print(-1)
            return

    else:
        #N > 10**6
        for b in range(2, 10**6+2):
            if f(b, N) == S:
                res = b
                break
        if res < 10 ** 18:
            print(res)
            return
        else:
            if N <= S:
                print(-1)
                return
            else:
                div = divisors(N-S)
                for y in div:
                    sub_res = y + 1
                    if f(sub_res, N) == S:
                        res = min(res, sub_res)
                if res < 10**18:
                    print(res)
                else:
                    print(-1)
                return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
s = int(input())
ans = -1
if n > s:
    sqrt_n = math.ceil(math.sqrt(n))
    for b in range(2, sqrt_n + 1):
        m = n
        f = 0
        while m >= b:
            f += (m % b)
            m //= b
        f += m
        if f == s:
            ans = b
            break
    b = 2
    if ans == -1:
        for p in range(sqrt_n - 1, 0, -1):
            if (n - s) % p == 0:
                b = (n - s) // p + 1
            if n % b + n // b == s:
                ans = b
                break
elif n == s:
    ans = n + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
s = int(input())
ans = -1
if n > s:
    sqrt_n = int(math.sqrt(n))
    for b in range(2, sqrt_n + 1):
        m = n
        f = 0
        while m >= b:
            f += (m % b)
            m //= b
        f += m
        if f == s:
            ans = b
            break
    b = 2
    if ans == -1:
        for p in range(sqrt_n - 1, 0, -1):
            if (n - s) % p == 0:
                b = (n - s) // p + 1
            if n % b + n // b == s:
                ans = b
                break
elif n == s:
    ans = n + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
s = int(input())
ans = -1
if n >= s:
    sqrt_n = int(math.sqrt(n))
    for b in range(2, sqrt_n + 1):
        m = n
        f = 0
        while m >= b:
            f += (m % b)
            m //= b
        f += m
        if f == s:
            ans = b
            break
    if ans == -1:
        for p in range(sqrt_n, 0, -1):
            if (n - s) % p == 0:
                b = (n - s) // p + 1
            if n % b + n // b == s:
                ans = b
                break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def yakusu(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())
def NIJIGEN(H): return [list(input()) for i in range(H)]
N=int(input())
K=int(input())
if N<K:
  print(-1)
  exit()
L=list()
setL=set()
#N-Kはa-1の倍数
L=yakusu(N-K)
L=[i+1 for i in L]
for i in L:
  ans=0
  m=N
  while True:
    if m<i:
      ans+=m
      break
    else:
      ans+=m%i
      m=m//i
  if ans==K:
    print(i)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if n==s:
  print(n+1)
  exit()
if (n+3)//2<=s:
  print(-1)
  exit()
print(n-s+1)

Submission status: Wrong Answer

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

f = lambda b,n:n if n < b else n%b + f(b,n//b)
import math
ans = math.inf
if n == s: ans = n+1
for b in range(2,math.ceil(math.sqrt(n))): #O(√n)の全探索
    if f(b,n) == s:
        ans = b; break
else:
    for p in range(1,math.ceil(math.sqrt(n))): #n=pb+q (b>√n, p+q=s)に基づいたO(√n)の全探索
        b = (n-s)/p+1
        if b == int(b) and b > 1 and f(b,n) == s:
           ans = min(ans,int(b))
if ans == math.inf: ans = -1
print(ans)

Submission status: Wrong Answer

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

f = lambda b,n:n if n < b else n%b + f(b,n//b)
import math
ans = math.inf
for b in range(2,math.ceil(math.sqrt(n))): #O(√n)の全探索
    if f(b,n) == s:
        ans = b; break
else:
    for p in range(math.ceil(math.sqrt(n))): #n=pb+q (b>√n, p+q=s)に基づいたO(√n)の全探索
        if p > 0: b = (n-s)/p+1
        else: b = n+1
        if b == int(b) and b > 1 and f(b,n) == s:
           ans = min(ans,int(b))
if ans == math.inf: ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(input())
S = int(input())

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

MAX = 10 ** 6
for b in range(2, MAX + 1):
    res = f(b, N)
    if res == S:
        print(b)
        exit()

x = N - S
if x < 0:
    print(-1)
else:
    ans = -1
    for p in range(1, MAX + 1):
        if x % p == 0:
            b = (x // p) + 1
            if b > MAX and N // b == p:
                ans = b
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(input())
S = int(input())

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

MAX = 10 ** 6
for b in range(2, MAX + 1):
    res = f(b, N)
    if res == S:
        print(b)
        exit()

x = N - S
if x < 0:
    print(-1)
else:
    ans = INF
    for p in range(1, MAX + 1):
        if x % p == 0:
            b = (x // p) + 1
            if b > MAX and N // b == p:
                ans = min(b, ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(input())
S = int(input())

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

MAX = 10 ** 6
for b in range(2, MAX + 1):
    res = f(b, N)
    if res == S:
        print(b)
        exit()

x = N - S
if x < 0:
    print(-1)
else:
    ans = INF
    for p in range(1, MAX + 1):
        if x % p == 0:
            b = (x // p) + 1
            if b >= MAX and N // b == p:
                ans = min(b, ans)
    print(ans)

Submission status: Wrong Answer

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


def divisors(N):
    d = []
    for i in range(1, N):
        if i**2 == N:
            d.append(i)
            break
        if i**2 > N:
            break
        if N % i == 0:
            d.append(i)
            d.append(N//i)
    d.sort()
    return d


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


sqrtN = math.ceil(math.sqrt(N))
data = defaultdict(list)

for i in range(2, sqrtN+1):
    v = func(i, N)
    data[v].append(i)

for l in range(sqrtN+10, 0, -1):
    if S >= l:
        div = divisors(N-S+l)
        for num in div:
            if num > S-l and math.ceil(N/(l+1)) < num <= N//l:
                data[S].append(num)
                break

if len(data[S]) == 0:
    print(-1)

else:
    print(min(data[S]))

Submission status: Wrong Answer

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


def divisors(N):
    d = []
    for i in range(1, N):
        if i**2 == N:
            d.append(i)
            break
        if i**2 > N:
            break
        if N % i == 0:
            d.append(i)
            d.append(N//i)
    d.sort()
    return d


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


sqrtN = math.ceil(math.sqrt(N))
data = defaultdict(list)
for i in range(2, sqrtN+1):
    v = func(i, N)
    data[v].append(i)

for l in range(sqrtN+1, 0, -1):
    if S >= l:
        div = divisors(N-S+l)
        for num in div:
            if num >= S-l and N//(l+1) < num <= N//l:
                data[S].append(num)
                break

if len(data[S]) == 0:
    print(-1)

else:
    print(min(data[S]))

Submission status: Wrong Answer

Solution code:
def func(b, n):
    res = 0
    while n > 0:
        res += n % b
        n //= b
    return res


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

    if n == s:
        return n + 1
    elif n < s:
        return -1

    for b in range(2, n + 1):
        if b**2 > n:
            break
        if func(b, n) == s:
            return b

    for p in range(1, n + 1):
        if p**2 > n:
            break

        b = (n - s) // p + 1
        if func(b, n) == s:
            return b

    return -1


ans = solve()
print(ans)

Submission status: Wrong Answer

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

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

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

for b in range(2,10**6+5):
  if f(b,n)==s:
    print(b)
    exit()

print(-1)

Submission status: Wrong Answer

Solution code:
def divisor(n):
    divisors = []
    i = 1
    while i * i < n:
        if n % i == 0:
            divisors.append(i)
            divisors.append(n // i)
        i += 1
    if i * i == n:
        divisors.append(i)
    divisors.sort()
    return divisors

n = int(input())
s = int(input())
if n == s:
    print(n + 1)
    exit()
if n < s:
    print(-1)
    exit()
sr = int(n ** 0.5)
for b in range(2, sr + 1):
    m = n
    a = 0
    while m:
        a += m % b
        m //= b
    if a == s:
        print(b)
        exit()
for d in divisor(n - s):
    b = d + 1
    q = (n - s) // d
    r = n % b
    if q + r == s:
        print(b)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import math

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


r = 0

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

sq = int(math.sqrt(n))


# 2 <= d <= √n
for i in range(2, sq+1):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
# √n < d <= n
for i in range(sq, 0, -1):
    b = (n - s) // i + 1
    
    st = n % b + n // b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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


r = 0

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

sq = int(math.sqrt(n))


# 2 <= d <= √n
for i in range(2, sq+1):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
# √n < d <= n
for i in range(sq-1, 0, -1):
    b = (n - s) // i + 1
    
    st = n % b + n // b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

sq = int(math.sqrt(n))


# 2 <= d <= √n
for i in range(2, sq+1):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
# √n < d <= n
for i in range(sq+1, 0, -1):
    b = (n - s) // i + 1
    
    st = n % b + n // b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

nn = int(math.sqrt(n)) + 1

for i in range(2, nn):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
for i in range(nn-1, 0, -1):
    nt = n
    st = 0

    b = (n - s) // i + 1
    
    st = n % b + n // b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

nn = int(math.sqrt(n)) + 1

for i in range(2, nn):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
for i in range(nn, 0, -1):
    nt = n
    st = 0

    b = (n - s) // i + 1
    
    st = n % b + n // b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

nn = int(math.sqrt(n)) + 1

for i in range(2, nn):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
for i in range(nn, 1, -1):
    nt = n
    st = 0

    b = (n - s) // i + 1
    
    st = n % b + n // b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

nn = int(math.sqrt(n)) + 1

for i in range(2, nn):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
for i in range(nn, 0):
    nt = n
    st = 0

    b = (n - s) // i + 1
    
    while nt > 0:
        st += nt % b
        nt //= b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

nn = int(math.sqrt(n)) + 1

for i in range(2, nn):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
for i in range(2, nn):
    nt = n
    st = 0

    b = (n - s) // i + 1
    
    while nt > 0:
        st += nt % b
        nt //= b

    if st == s:
        print(b)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math

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

r = 0

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

else:
    nn = int(math.sqrt(n)) + 2

    for i in range(2, nn):
        nt = n
        st = 0

        while nt > 0:
            st += nt % i
            nt //= i

        if st == s:
            print(i)
            exit()
        
    print(-1)

Submission status: Wrong Answer

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

r = 0

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

else:
    t1 = 1
    t2 = n
    while abs(t1-t2) != 1:
        nn = n
        nt = 0
        t3 = (t1 + t2) // 2
        while nn != 0:
            nt += nn % t3
            nn = nn // t3

        if nt < s:
            t2 = t3
        elif nt > s:
            t1 = t3
        else:
            print(t3)
            exit()

    nn1 = n
    nn2 = n
    nt1 = 0
    nt2 = 0

    while nn1 != 0:
        nt1 += nn1 % t1
        nn1 = nn1 // t1

    if nt1 == s:
        print(t1)
        exit()

    while nn2 != 0:
        nt2 += nn2 % t2
        nn2 = nn2 // t2

    if nt2 == s:
        print(t2)
        exit()

    print(t1)
    print(t2)

    print(-1)
    exit()

Submission status: Wrong Answer

Solution code:
def f(b,n):
    if b>=2 and n >= 1:
        if n < b:
            return n
        else:
            return f(b,n//b) + (n%b)
n = int(input())
s = int(input())
i = 1
while True:
    if f(i,n) == s:
        res = i
        break
    if s == 1:
        res = n
        break
    if n < 2*s:
        res = -1
        break
    i += 1
print(res)

Submission status: Wrong Answer

Solution code:
import sys
import math

n=int(sys.stdin.readline().strip())
s=int(sys.stdin.readline().strip())

def divlist(n):
   sl=[]
   ll=[]
   for x in range(1,int(math.sqrt(n))+1):
       if n%x==0:
           sl.append(x)
           ll.append(n//x)
   ll.reverse()
   sl.extend(ll)
   return sl

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

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

dl=divlist(n-s)
for x in dl:
    if s==f(x+1,n):
        print(x+1)
        sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    s = int(readline())

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

    for i in range(2, n):
        k = i ** 2
        if k > n:
            break
        x = n
        cur = 0
        while x > 0:
            cur += x % i
            x = x // i

        if cur == s:
            print(i)
            sys.exit()

    ans = INF
    for i in range(1, n):
        k = i ** 2
        if k > n:
            break
        if (n - s) % i == 0:
            b = (n-s) // i + 1
            p = n // b
            q = n % b
            if p + q == s:
                ans = min(ans,b)

    if ans == INF:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


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


def solve():
    """
    87654
    138
    -----
    100

    87654
    45678
    -----
    -1

    """
    n = read_int()
    s = read_int()
    for b in range(2, math.ceil(math.sqrt(n))+1):
        if f(b, n) == s:
            return b
    """
    if b>math.sqrt(n) then n=pb+q, p, q < b
                           s=p+q
    n-s=p*(b-1), b-1 >= p
    """
    if n-s < 0:
        return -1
    for p in range(1, math.ceil(math.sqrt(n-s))):
        if (n-s)%p == 0 and f((n-s)//p+1, n) == s:
            return (n-s)//p+1
    return -1


if __name__ == '__main__':
    #print(f(10, 87654))
    #print(f(100, 87654))
    print(solve())

Submission status: Wrong Answer

Solution code:
n=int(input())
s=int(input())
if s>n:
  print(-1)
elif s==n:
  print(n+1)
elif s==1:
  print(n)
else:
  # 2<=b<=n**0.5 で全探索
  for b in range(2,int(n**0.5)+1):
    tmp=0
    n0=n
    while n0>0:
      tmp+=n0%b
      n0//=b
    if tmp==s:
      print(b)
      exit()
  # n**0.5<bについて
  for p in range(int(n**0.5)+1,0,-1):
    b=(n-s)//p+1
    tmp=0
    n0=n
    while n0>0:
      tmp+=n0%b
      n0//=b
    if tmp==s:
      print(b)
      exit()
  print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
if s == 1 and n == 1:
    print(2)
    exit()
if s > n:
    print(-1)
    exit()
if n%2 == 0 and n > s > n//2:
    print(-1)
    exit()
if n%2 == 1 and n > s > n//2+1:
    print(-1)
    exit()

ans = float("INF")
for i in range(2,int(n**0.5)+1):
    x = n
    count = 0
    while x >= i:
        count += x%i
        x //= i
    if count + x == s:
        print(i)
        exit()
now = 2
li = int(n**0.5)+1
while True:

    x = n//now
    if x < li:
        break
    count = 0
    y = n
    while y >= x:
        count += y%x
        y //= x
    count += y
    if count <= s and (s-count)%now == 0:
        z = x-n//(now+1)
        if (s-count)//now < z:
            ans = x-(s-count)//now
    now += 1
    
print(min(ans,n-s+1))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
if s == 1 and n == 1:
    print(2)
    exit()
if n > s:
    print(-1)
    exit()
if n%2 == 0 and n > s > n//2:
    print(-1)
    exit()
if n%2 == 1 and n > s > n//2+1:
    print(-1)
    exit()

ans = float("INF")
for i in range(2,int(n**0.5)+1):
    x = n
    count = 0
    while x >= i:
        count += x%i
        x //= i
    if count + x == s:
        print(i)
        exit()
now = 2
li = int(n**0.5)+1
while True:

    x = n//now
    if x < li:
        break
    count = 0
    y = n
    while y >= x:
        count += y%x
        y //= x
    count += y
    if count <= s and (s-count)%now == 0:
        z = x-n//(now+1)
        if (s-count)//now < z:
            ans = x-(s-count)//now
    now += 1
    
print(min(ans,n-s+1))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
if s == 1 and n == 1:
    print(2)
    exit()
if n%2 == 0 and n > s > n//2:
    print(-1)
    exit()
if n%2 == 1 and n > s > n//2+1:
    print(-1)
    exit()

ans = float("INF")
for i in range(2,int(n**0.5)+1):
    x = n
    count = 0
    while x >= i:
        count += x%i
        x //= i
    if count + x == s:
        print(i)
        exit()
now = 2
li = int(n**0.5)+1
while True:

    x = n//now
    if x < li:
        break
    count = 0
    y = n
    while y >= x:
        count += y%x
        y //= x
    count += y
    if count <= s and (s-count)%now == 0:
        z = x-n//(now+1)
        if (s-count)//now < z:
            ans = x-(s-count)//now
    now += 1
    
print(min(ans,n-s+1))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
if s == 1:
    print(n)
    exit()
if s == n:
    print(n+1)
    exit()
if n%2 == 0 and s > n//2:
    print(-1)
    exit()
if n%2 == 1 and s > n//2+1:
    print(-1)
    exit()

ans = n-s+1
for i in range(2,int(n**0.5)+1):
    x = n
    count = 0
    while x >= i:
        count += x%i
        x //= i
    if count + x == s:
        print(i)
        exit()
now = 2
li = int(n**0.5)+1
while True:

    x = n//now
    if x < li:
        break
    count = 0
    y = n
    while y >= x:
        count += y%x
        y //= x
    count += y
    if count <= s and (s-count)%now == 0:
        z = x-n//(now+1)
        if (s-count)//now < z:
            ans = x-(s-count)//now
    now += 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
if s == 1:
    print(n)
    exit()
if s == n:
    print(n+1)
    exit()
if n%2 == 0 and s > n//2:
    print(-1)
    exit()
if n%2 == 1 and s > n//2+1:
    print(-1)
    exit()

ans = n-s+1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
if s == 1:
    print(n)
    exit()
if s == n:
    print(n+1)
    exit()
if n%2 == 0 and s > n//2:
    print(-1)
    exit()
if n%2 == 1 and s > n//2+1:
    print(-1)
    exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
def f(b, n):
  if n < b:
    return n
  return f(b, n//b) + n%b
if n == s:
  print(n+1)
  exit()
U = int(n**0.5)
for b in range(2, U+1):
  if f(b, n) == s:
    print(b)
    exit()
for p in range(U-1, 0, -1):
  if (n-s)%p:
    continue
  b = (n-s)//p + 1
  if b < 2:
    continue
  if f(b, n) == s:
    print(b)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
def f(b, n):
  if n < b:
    return n
  return f(b, n//b) + n%b
if n == s:
  print(n+1)
  exit()
U = int(n**0.5)+5
for b in range(2, U+1):
  if f(b, n) == s:
    print(b)
    exit()
for p in range(1, U):
  if (n-s)%p:
    continue
  b = (n-s)//p + 1
  if b < 2:
    continue
  if f(b, n) == s:
    print(b)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = int(input())
def f(b, n):
  if n < b:
    return n
  return f(b, n//b) + n%b
if n == s:
  print(n+1)
  exit()
U = int(n**0.5)
for b in range(2, U+1):
  if f(b, n) == s:
    print(b)
    exit()
for p in range(1, U):
  if (n-s)%p:
    continue
  b = (n-s)//p + 1
  if b < 2:
    continue
  if f(b, n) == s:
    print(b)
    exit()
print(-1)

Submission status: Wrong Answer

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

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

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

for b in range(2,int(n**0.5)+1):
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)

#n=pb+q
for p in range(1,10**9):
  if p**2>=n:
    break
  
  q=s-p
  b=(n-q)//p
  if q<0 or b<=q or b<2 or (n-q)%p!=0:
    continue
    
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)
    
print(-1)

Submission status: Wrong Answer

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

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

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

for b in range(2,int(n**0.5)+1):
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)

#n=pb+q
for p in range(1,int(n**0.5)):
  q=s-p
  b=(n-q)//p
  if q<0 or b<=q or (n-q)%p!=0:
    continue
    
  ss=f(b,n)
  if ss==s:
    print(b)
    sys.exit(0)
    
print(-1)

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

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

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

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

for b in range(2,n+1):
  ss=f(b,n)
  if ss==s:
    print(b)
    break
else:
  print(-1)

Submission status: Wrong Answer

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

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

result = -1
for b in range(2,n//2+2):
  after = list(Base_10_to_n(n,b))
  _sum = 0
  for i in after:
    _sum += int(i)
  if _sum == s:
    result = b
    break
    
print(result)

Submission status: Wrong Answer

Solution code:
def base10to(n, b):
 if (int(n/b)):
  return base10to(int(n/b), b) + str(n%b)
 return str(n%b)

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

for i in range(2,n+1):
    tmp=base10to(n,i)
    su=0
    for y in tmp:
        su+=int(y)
    if su==s:
        print(i)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=int(input())
res=0
check=True
for i in range(2,int(math.sqrt(n))+2):
  sum=0
  hoge=n
  while True:
    hoge2=hoge%i
    sum+=hoge2
    hoge//=i
    if hoge==0 :
      if sum==s:
        print("{}".format(i))
        check=False
      break
  if check==False:
    break
if check:
    for i in range(int(math.sqrt(s)),-1,-1):
        if i==0:
            print("-1")
            break
        if (n-s)%i==0:
            b=(n-s)//i+1
            if b>int(math.sqrt(s)):
              print("{}".format(b))
            else:print("-1")
            break

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=int(input())
res=0
check=True
for i in range(2,int(math.sqrt(s))+2):
  sum=0
  hoge=n
  while True:
    hoge2=hoge%i
    sum+=hoge2
    hoge/=i
    if hoge==0:
      break
  if sum==s:
      print("{}".format(i))
      check=False
      break
if check:
    for i in range(int(math.sqrt(s))+2,-1,-1):
        if i==0:
            print("-1")
            break
        if (n-s)%i==0:
            b=(n-s)//i+1
            if b>int(math.sqrt(s)):
              print("{}".format(b))
            else:print("-1")
            break

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=int(input())
res=0
check=True
for i in range(2,int(math.sqrt(n))+2):
  sum=0
  hoge=n
  while True:
    hoge2=hoge%i
    sum+=hoge2
    hoge//=i
    if hoge==0 :
      if sum==s:
        print("{}".format(i))
        check=False
      break
  if check==False:
    break
if check:
    for i in range(int(math.sqrt(n))+2,-1,-1):
        if i==0:
            print("-1")
            break
        if (n-s)%i==0:
            print("{}".format((n-s)//i+1))
            break

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
        if i*(b-1)+S != N:
            continue
        tmp = N
        tmp2 = 0
        while tmp > 0:
            tmp2 += tmp % b
            tmp = tmp // b
        if tmp2 == S:
            res = b
            break
if N==S:
    print(N+1)
elif S==1:
    print(N)
elif res==0 or N<S:
    print(-1)
else:
    print(res)