Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03673 (python)

Fixed submission:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = collections.deque(nl())
ans = collections.deque()
if n % 2 == 0:
    for i in range(n):
        if i % 2 == 0:
            ans.append(str(a.popleft()))
        else:
            ans.appendleft(str(a.popleft()))
else:
    for i in range(n):
        if i % 2 == 0:
            ans.appendleft(str(a.popleft()))
        else:
            ans.append(str(a.popleft()))
print(' '.join(ans))

Buggy submission (verdict: Runtime Error):
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = collections.deque(nl())
ans = collections.deque()
if n % 2 == 0:
    for i in range(n):
        if i % 2 == 0:
            ans.append(str(a.popleft()))
        else:
            ans.insert(0, str(a.popleft()))
else:
    for i in range(n):
        if i % 2 == 0:
            ans.insert(0, str(a.popleft()))
        else:
            ans.append(str(a.popleft()))
print(' '.join(ans))

Buggy submission (verdict: Runtime Error):
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = collections.deque(nl())
ans = collections.deque()
if n % 2 == 0:
    for i in range(n):
        if i % 2 == 0:
            ans.append(str(a.popleft()))
        else:
            ans.insert(0, str(a.popleft()))
else:
    for i in range(n):
        if i % 2 == 0:
            ans.insert(0, str(a.popleft()))
        else:
            ans.append(str(a.popleft()))
print(' '.join(ans))
Problem p03680 (python)

Fixed submission:
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
count = 0
nexti = 0
for i in range(n):
    if nexti == 1:
        print(count)
        exit()
    else:
        nexti = a[nexti] - 1
        count += 1
print(-1)

Buggy submission (verdict: Runtime Error):
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
count = 0
nexti = 0
for i in range(n):
    if nexti == 1:
        print(count)
        exit()
    else:
        nexti = a[a[nexti] - 1]
        count += 1
print(-1)
Problem p03681 (python)

Fixed submission:
import sys
import collections
sys.setrecursionlimit(100000)
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
memo = [1 for _ in range(max(n, m) + 1)]
mod = 10 ** 9 + 7

for i in range(1, max(n, m) + 1):
    memo[i] = memo[i - 1] * i % mod

if abs(n - m) >= 2:
    print(0)
else:
    nn = memo[n] % mod
    mm = memo[m] % mod
    if n == m:
        print(2 * nn * mm % mod)
    else:
        print(nn * mm % mod)

Buggy submission (verdict: Runtime Error):
import sys
import collections
sys.setrecursionlimit(100000)
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
memo = [-1 for i in range(max(n, m) + 1)]
mod = 10 ** 9 + 7

def fact(k):
    if memo[k] != -1:
        return memo[k]
    elif k == 1:
        memo[1] = 1
        return memo[1]
    else:
        memo[k] = k * fact(k - 1)
        return memo[k]

if abs(n - m) >= 2:
    print(0)
else:
    nn = fact(n) % mod
    mm = fact(m) % mod
    if n == m:
        print(2 * nn * mm % mod)
    else:
        print(nn * mm % mod)
Problem p03777 (python)

Fixed submission:
a, b = map(str, input().split())
if a == b:
  print('H')
else:
  print('D')

Buggy submission (verdict: Runtime Error):
a = input()
b = input()
if a == b:
  print('H')
else:
  print('D')
Problem p02780 (python)

Fixed submission:
n,k = map(int,input().split())
p = list(map(int,input().split()))
l = []
count = 0
for i in range(n):
    count += (p[i]+1)/2
    l.append(count)
ansl = []
for i in range(n-k):
    ansl.append(l[i+k]-l[i])
if n == k:
    ansl.append(l[n-1])
print(max(ansl))

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
p = list(map(int,input().split()))
l = []
count = 0
for i in range(n):
    count += (p[i]+1)/2
    l.append(count)
ansl = []
for i in range(n-k):
    ansl.append(l[i+k]-l[i])
print(max(ansl))
Problem p02814 (python)

Fixed submission:
import fractions
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = 1
c = 0
key = 1
key1 = 1
l = []
while key == 1:
    if a[0] % b == 0:
        b *= 2
        c += 1
    else:
        b //= 2
        c -= 0
        key = 0
for i in range(n):
    if a[i] % b == 0:
        if (a[i]//b) % 2 == 0:
            key1 = 0
            break
        else:
            l.append(a[i]//b)
    else:
        key1 = 0
        break
if key1 == 0 or c == 0:
    print(0)
else:
    d = l[0]
    for i in range(1,n):
        d = d*l[i]// fractions.gcd(d,l[i])
        if m < d:
            break
    d = d*b//2
    if (m//d)%2 == 0:
        print((m//d)//2)
    else:
        print((m//d)//2+1)

Buggy submission (verdict: Runtime Error):
import math
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = 1
c = 0
key = 1
key1 = 1
l = []
while key == 1:
    if a[0] % b == 0:
        b *= 2
        c += 1
    else:
        b //= 2
        c -= 0
        key = 0
for i in range(n):
    if a[i] % b == 0:
        if (a[i]//b) % 2 == 0:
            key1 = 0
            break
        else:
            l.append(a[i]//b)
    else:
        key1 = 0
        break
if key1 == 0 or c == 0:
    print(0)
else:
    d = l[0]
    for i in range(1,n):
        d = d* l[i]// math.gcd(d,l[i])
    d = d*b//2
    if (m//d)%2 == 0:
        print((m//d)//2)
    else:
        print((m//d)//2+1)
Problem p03059 (python)

Fixed submission:
a,b,t =(int(x) for x in input().split())
c = t//a
print(c*b)

Buggy submission (verdict: Runtime Error):
s = input()
d = s.split("/")
m = int(d[1])
if m > 4:
 print("TBD")
else:
 print("Heisei")
Problem p03061 (python)

Fixed submission:
import fractions
n = int(input())
a = list(map(int,input().split()))
l = []
m = []
l.append(0)
m.append(0)
for i in range(n):
    l.append(fractions.gcd(l[i],a[i]))
    m.append(fractions.gcd(m[i],a[-(i+1)]))
b = [fractions.gcd(l[i],m[-(i+2)]) for i in range(n)]
print(max(b))

Buggy submission (verdict: Runtime Error):
from fractions import gcd
import functools
import math

n = int(input())
a = list(map(int,input().split()))
l = []
m = []
l.append(0)
m.append(0)
for i in range(n):
    l.append(math.gcd(l[i],a[i]))
    m.append(math.gcd(m[i],a[-(i+1)]))
b = [math.gcd(l[i],m[-(i+2)]) for i in range(n)]
print(max(b))

Buggy submission (verdict: Runtime Error):
import functools
import math

n = int(input())
a = [int(i) for i in input().split()]
l = []
for i in range(n):
 b = a[i]
 del a[i]
 l.append(functools.reduce(math.gcd, a))
 a.insert(i,b)
print(max(l))
Problem p03071 (python)

Fixed submission:
a,b =map(int,input().split())
print(max(a + b, a*2 - 1, b*2 - 1))

Buggy submission (verdict: Runtime Error):
a,b = int(input().split())
print(max(a + b, a*2 - 1, b*2 - 1))
Problem p03076 (python)

Fixed submission:
a,b,c,d,e = [int(input()) for i in range(5)]
count = a + b + c + d + e
l = [a%10,b%10,c%10,d%10,e%10]
s = [a%10,b%10,c%10,d%10,e%10]
m = [a%10,b%10,c%10,d%10,e%10]
for x in range(len(l)):
    if l[x] == 0:
        m.remove(0)
for i in range (len(s)):
    count = count -s[i]
if len(m) == 0:
    ans= count
    print(ans)
else:
    ans = count + 10*len(m) + min(m) -10
    print(ans)

Buggy submission (verdict: Runtime Error):
a,b,c,d,e = [int(input()) for i in range(5)]
count = a + b + c + d + e
l = [a%10,b%10,c%10,d%10,e%10]
s = [a%10,b%10,c%10,d%10,e%10]
m = [a%10,b%10,c%10,d%10,e%10]
for x in range(len(l)):
    if l[x] == 0:
        m.remove(0)
for i in range (len(s)):
    count = count -s[i]
ans = count + 10*len(m) + min(m) -10
print(ans)
Problem p03109 (python)

Fixed submission:
s = input()
day = (s.split("/"))
if int(day[1]) <= 4:
 print("Heisei")
else:
 print("TBD")

Buggy submission (verdict: Runtime Error):
s = input()
day = (s.split("/"))
if int(day[1]) <= "4":
 print("Heisei")
else:
 print("TBD")

Buggy submission (verdict: Runtime Error):
s = input()
d = s.split("/")
if day[1] > 4:
 print("TBD")
else:
 print("Heisei")
Problem p03110 (python)

Fixed submission:
n = int(input())
money = 0
for i in range(n):
    x,u = input().split()
    if u == "JPY":
        x = int(x)
        money = money + x
    else:
        x = float(x)
        money = money + x*380000
print(money)

Buggy submission (verdict: Runtime Error):
n = int(input())
v = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
count = 0
for x in range(n):
 if v[x] > c[x]:
  count = count + v[x] - c[x]
print(count)

Buggy submission (verdict: Runtime Error):
n = int(input())
v = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
count = 0
for x in range(n):
 if v[x] > c[x]:
  count = count + v[x] - c[x]
print(count)
Problem p03437 (python)

Fixed submission:
import math
x,y = map(int,input().split())
if x%y == 0:
    print(-1)
else:
    print(x)

Buggy submission (verdict: Runtime Error):
n = int(input())
count = 0
for i in range(1,n+1):
    count = count + i*i
print(count)
Problem p03948 (python)

Fixed submission:
n,t = map(int,input().split())
a = list(map(int,input().split()))
mn = a[0]
l = []
for i in range(n-1):
    if a[i] < a[i+1]:
        l.append(a[i+1]-mn)        
    if a[i+1] < mn:
        mn = a[i+1]
if len(l) > 0:
    d = max(l)
ans = l.count(d)
print(ans)

Buggy submission (verdict: Runtime Error):
n,t = map(int,input().split())
a = list(map(int,input().split()))
l = []
count = 0
for i in range(n-1):
    if a[i] <= a[i+1]:
        count = count + a[i+1] - a[i]
    else:
        l.append(count)
        count = 0
l.append(count)
l.remove(0)
d = max(l)
ans = l.count(d)
print(ans)
Problem p04015 (python)

Fixed submission:
n,a = map(int,input().split())
x = list(map(int,input().split()))
dp = []
for i in range(n+1):
    dp.append([[] for j in range(i+1)])
    for k in range(i+1):
        dp[i][k] = [0 for y in range(50*k)]
for i in range(n+1):
        dp[i][0].append(0)
for i in range(1,n+1):
    for j in range(1,i+1):
        if j == 1:
                dp[i][j][x[i-1]-1] += 1
        for k in range(50*(j-1)):
                dp[i][j][k+x[i-1]] += dp[i-1][j-1][k]
                dp[i][j-1][k] += dp[i-1][j-1][k]
count = 0
for j in range(1,i+1):
        for k in range(len(dp[i][j])):
                if k + 1 == j*a:
                        count += dp[n][j][k]
print(count)

Buggy submission (verdict: Runtime Error):
n,a = map(int,input().split())
x = list(map(int,input().split()))
dp = []
for i in range(n+1):
    dp.append([[] for j in range(i+1)])
    for k in range(i+1):
        dp[i][k] = [0 for y in range(50*k)]
for i in range(n+1):
        dp[i][0].append(0)
for i in range(1,n+1):
    for j in range(1,i+1):
        if j == 1:
                dp[i][j][x[i-1]-1] += 1
        for k in range(len(dp[i-1][j-1])):
                dp[i][j][k+x[i-1]] += dp[i-1][j-1][k]
                dp[i][j-1][k] += dp[i-1][j-1][k]
count = 0
for j in range(1,i+1):
        for k in range(len(dp[i][j])):
                if k + 1 == j*a:
                        count += dp[n][j][k]
print(count)
Problem p03214 (python)

Fixed submission:
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split(' ')))

    A_average = sum(A) / N
    result = sorted(range(N), key=lambda i: abs(A[i] - A_average))

    print(result[0])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import numpy as np
import itertools

and_memo = {}


def list_subarray_sums(arr, length):
    cumsum = np.insert(np.cumsum(arr), 0, 0)
    for l in range(1, length + 1):
        for i in range(length - l + 1):
            yield cumsum[l + i] - cumsum[i]


def logical_and(arr, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    key = tuple(arr[idx:])
    if key in and_memo:
        return and_memo[key]

    result = arr[idx] & logical_and(arr, lenght, idx + 1)
    and_memo[key] = result
    return result


if __name__ == '__main__':
    N, K = map(int, input().split(' '))
    A = np.array(list(map(int, input().split(' '))))

    sums = set(list_subarray_sums(A, N))
    combinations = itertools.combinations(sums, K)
    ands = map(lambda arr: logical_and(arr, K), combinations)

    print(max(ands))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import numpy as np
import itertools
import sys

sys.setrecursionlimit(3000)

and_memo = {}


def list_subarray_sums(arr, length):
    cumsum = np.insert(np.cumsum(arr), 0, 0)
    for l in range(1, length + 1):
        for i in range(length - l + 1):
            yield cumsum[l + i] - cumsum[i]


def logical_and(arr, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    key = tuple(arr[idx:])
    if key in and_memo:
        return and_memo[key]

    result = arr[idx] & logical_and(arr, lenght, idx + 1)
    and_memo[key] = result
    return result


if __name__ == '__main__':
    N, K = map(int, input().split(' '))
    A = np.array(list(map(int, input().split(' '))))

    sums = set(list_subarray_sums(A, N))
    combinations = itertools.combinations(sums, K)
    ands = map(lambda arr: logical_and(arr, K), combinations)

    print(max(ands))
Problem p03231 (python)

Fixed submission:
# -*- coding: utf-8 -*-

import math
import fractions


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


if __name__ == '__main__':
    N, M = map(int, input().split(' '))
    S = input()
    T = input()

    L = lcm(N, M)

    S_index_X = [int(i * L / N) for i in range(N)]
    T_index_X = [int(i * L / M) for i in range(M)]

    common_index = set(S_index_X).intersection(set(T_index_X))
    for i in common_index:
        si = int(i * N / L)
        ti = int(i * M / L)
        if S[si] != T[ti]:
            print(-1)
            exit(0)
    print(L)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import math


def lcm(x, y):
    return (x * y) // math.gcd(x, y)


def divisors(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list


if __name__ == '__main__':
    N, M = map(int, input().split(' '))
    S = input()
    T = input()

    L = lcm(N, M)

    m = max(N, M)
    m_divisors = divisors(m)
    m_divisors.remove(1)

    S = S * int(math.ceil(m / N))
    T = T * int(math.ceil(m / M))

    for i in [d - 1 for d in m_divisors]:
        if S[i] == T[i]:
            print(-1)
            exit(0)
    print(L)
Problem p03160 (python)

Fixed submission:
n=int(input())
l=list(map(int,input().split(" ")))
f=[0]*n
f[1]=abs(l[1]-l[0])

for i in range(2,n):
	x1=f[i-1]+abs(l[i-1]-l[i])
	x2=f[i-2]+abs(l[i-2]-l[i])
	f[i]=min(x1,x2)
print(f[-1])

Buggy submission (verdict: Runtime Error):
# n=int(input())
n,k=list(map(int,input().split(" ")))
a=list(map(int,input().split(" ")))
a.sort()
m=(10**9)+7
mat=[[(1,1) for i in range(k+1)] for j in range(n+1)]
for j in range(1,k+1):
	for i in range(1,n+1):
		# i=i1-1
		if(j==1):
			if(i==1 and a[0]>0):
				x=1
				
			elif(i>0 and a[i-1]>0 ):
				x=mat[i-1][j][0]+1
				
			elif(i>0 and a[i-1]==0):
				x=mat[i-1][j][0]
				
		else:
			start=j-a[i-1]
			start=max(start,0)
			if(start==0):
				x=mat[i-1][j][1]
				
			else:
				x=mat[i-1][j][1]-mat[i-1][start-1][1]
		mat[i][j]=(x%m,(mat[i][j-1][1]+x)%m)
		# mat[i][j]=mat[i][j]%m
# print(mat)			

print(mat[n][k][0])
Problem p02866 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
cnt = [0]*n
for i in a:
    cnt[i] += 1

output = 1
if a[0] != 0 or cnt[0] != 1:
    print(0)
elif max(a) == 1:
    print(1)
else:
    for i in range(2, n):
        output *= cnt[i-1]**cnt[i]
        output %= 998244353
    print(output)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
 
output = 1
if a[0] != 0:
    print(0)
elif max(a) == 1:
    print(1)
else:
    for i in range(2, max(a)+1):
        if a.count(i) != 0
            output *= pow(a.count(i-1), a.count(i), 998244353)
    print(output)
Problem p02867 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

ab = [[a[i], b[i]] for i in range(n)]
ab.sort(key=lambda x:x[1])

a = [ab[i][0] for i in range(n)]
b = [ab[i][1] for i in range(n)]

a2 = [[a[i], i] for i in range(n)]
a2.sort(key=lambda x:x[0])

flag1 = 0
for i in range(n):
    if a2[i][0] > b[i]:
        flag1 = 1

flag2 = 1
idx = a2[0][1]
cnt = 0
while idx != 0:
    idx = a2[idx][1]
    cnt += 1

if cnt == n-1:
    for i in range(n-1):
        if a2[i+1][0] <= b[i]:
            flag2=0
            break
else:
    flag2 = 0

if flag1 == 1 or flag2 == 1:
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

ab = [[a[i], b[i]] for i in range(n)]
ab.sort(key=lambda x:x[1])

a = [ab[i][0] for i in range(n)]
b = [ab[i][1] for i in range(n)]

a2 = [[a[i], i] for i in range(n)]
a2.sort(key=lambda x:x[0])

flag1 = 0
for i in range(n):
    if a2[i][0] > b[i]:
        flag1 = 1

flag2 = 1
idx = a2[0][1]
cnt = 0
while idx != 0:
    idx = a2[idx][1]
    cnt += 1

if cnt == n-1:
    for i in range(n-1):
        if a2[i+1] <= b[i]:
            flag2=0
            break
else:
    flag2 = 0

if flag1 == 1 or flag2 == 1:
    print('No')
else:
    print('Yes')
Problem p02995 (python)

Fixed submission:
from fractions import gcd
a,b,c,d = map(int,input().split())
g = c*d//gcd(c, d)
print(b-a+1 - b//c-b//d+b//g + (a-1)//c+(a-1)//d-(a-1)//g)

Buggy submission (verdict: Runtime Error):
from math import gcd
a,b,c,d = map(int,input().split())
g = c*d//gcd(c, d)
print(b-a+1 - b//c-b//d+b//g + (a-1)//c+(a-1)//d-(a-1)//g)
Problem p03262 (python)

Fixed submission:
from fractions import gcd
n, x = map(int,input().split())
p = list(map(lambda s: abs(s-x), list(map(int,input().split()))))
g = p[0]
for i in p:
    g = gcd(g, i)
print(g)

Buggy submission (verdict: Runtime Error):
from math import gcd
n, x = map(int,input().split())
p = list(map(lambda s: abs(s-x), list(map(int,input().split()))))
g = p[0]
for i in p:
    g = gcd(g, i)
print(g)
Problem p03739 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
m1 = 0
cnt1 = 0
f = 1
for i in range(n):
    f *= -1
    m1 += a[i]
    if f == 1:
        if m1 <= 0:
            cnt1 += f-m1
            m1 += f-m1
    else:
        if m1 >= 0:
            cnt1 += m1-f
            m1 -= m1-f
m2 = 0
cnt2 = 0
f = -1
for i in range(n):
    f *= -1
    m2 += a[i]
    if f == 1:
        if m2 <= 0:
            cnt2 += f-m2
            m2 += f-m2
    else:
        if m2 >= 0:
            cnt2 += m2-f
            m2 -= m2-f

print(min(cnt1, cnt2))

Buggy submission (verdict: Runtime Error):
a = list(map(int,input().split()))
if a[0] > 0:
    f = 1
else:
    f = -1
m = a[0]
cnt = 0
for i in range(1, n):
    f *= -1
    m += a[i]
    if f == 1:
        if m > 0:
            continue
        else:
            cnt += f-m
            m += f-m
    else:
        if m < 0:
            continue
        else:
            cnt += m-f
            m += f-m
print(cnt)
Problem p04014 (python)

Fixed submission:
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

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

if flag == 0:
    print(-1)

Buggy submission (verdict: Runtime Error):
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

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

if flag == 0:
    print(-1)

Buggy submission (verdict: Runtime Error):
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

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

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

if flag == 0:
    print(-1)

Buggy submission (verdict: Runtime Error):
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

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

if flag == 0:
    print(-1)

Buggy submission (verdict: Runtime Error):
def f(p, q):
    if q < p:
        return q
    else:
        return f(p, q//p) + q%p

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

if flag == 0:
    print(-1)