TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**9

def f():
    n,x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    mi = a.index(min(a))
    a = a[mi:] + a[:mi]
    print("a",a)
    r = sum(a)
    for t in range(n-1):
        for i in range(n-1,0,-1):
            a[i] = min(a[i],a[i-1])
        tr = ((t+1)*x) + sum(a)
        if r > tr:
            r = tr
        else:
            break
    return r

print(f())


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**9

def f():
    n,x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    mi = a.index(min(a))
    a = a[mi:] + a[:mi]
    r = sum(a)
    for t in range(n-1):
        for i in range(n-1,0,-1):
            a[i] = min(a[i],a[i-1])
        tr = ((t+1)*x) + sum(a)
        if r > tr:
            r = tr
        else:
            break
    return r

print(f())


Verdicts: Wrong Answer

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
# coding:utf-8

INF = float('inf')


def inpl(): return list(map(int, input().split()))


N, x = inpl()
A = inpl()

min_index = A.index(min(A))
B = A[min_index:] + A[:min_index]

cost = [B[i] for i in range(N)]

ans = INF
for magic in range(N):
    for i in range(N):
        if i - magic >= 0:
            cost[i] = min(cost[i], B[i - magic])

    ans = min(ans, sum(B) + x * magic)

print(ans)


# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, x = LI()
    A = LI()

    min_a, min_i = INF, -1
    res = 0
    for i, a in enumerate(A):
        if a < min_a:
            min_a = a
            min_i = i
        res += a
    A = A[min_i:] + A[:min_i]

    dp = [INF] + A
    for k in range(n - 1):
        tmp = 0
        for i in range(n)[::-1]:
            if dp[i + 1] > dp[i]:
                dp[i + 1] = dp[i]
            tmp += dp[i + 1]

        res = min(res, tmp + (k + 1) * x)

    return res


print(main())


Verdicts: Wrong Answer

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
import numpy as np
n, x = map(int, input().split())
A = np.array(input().split()[::-1], dtype=np.int64)
B = A.copy()
ans = float("inf")
for k in range(n):
  print(B)
  t = x*k + B.sum()
  ans = min(ans, t)
  np.minimum(B, np.roll(A, -k-1), out=B)
print(ans)

import sys
input = sys.stdin.readline

import numpy as np

def main():
  n, x = map(int, input().split())
  A = np.array(input().split()[::-1]*2, dtype=np.int64)
  B = A.copy()[:n]
  ans = float("inf")
  for k in range(n):
    t = x*k + B.sum()
    ans = min(ans, t)
    np.minimum(B, A[k+1:k+n+1], out=B)
  print(ans)
  
if __name__ == "__main__":
  main()

Verdicts: Wrong Answer

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
import numpy as np
N, x = map(int, input().split())
a = np.array(map(int, input().split()))
b = np.copy(a)
ans = float('inf') 
for i in range(N):
    c = np.roll(a,i)
    b = np.minimum(b,c)
    ans = min(ans, sum(b)+i*x)
print(ans)


import numpy as np
N, x = map(int, input().split())
a = np.array(list(map(int, input().split())))
b = np.copy(a)
ans = float('inf') 
for i in range(N):
    c = np.roll(a,i)
    b = np.minimum(b,c)
    ans = min(ans, sum(b)+i*x)
print(ans)


Verdicts: Runtime Error

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
n, x = map(int, input().split())
a = list(map(int, input().split()))

DP = a[:]
ans = sum(DP)
for i in range(n-1):
    new = [0]*n
    for j in range(n):
        new[i] = min(DP[j], DP[j-1])
    DP=new
    ans = min(ans, sum(DP) + (i+1)*x)
print(ans)

n, x = map(int, input().split())
a = list(map(int, input().split()))

DP = a[:]
ans = sum(DP)
for i in range(n-1):
    new = [0]*n
    for j in range(n):
        new[j] = min(DP[j], DP[j-1])
    DP=new
    ans = min(ans, sum(DP) + (i+1)*x)
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
def solve(n, x, a):
	costs = []
	cost_min = 0
	for k in range(n):
		if k == 0:
			costs = a.copy()
		else:
			for i in range(n):
				costs[i] = min(costs[i], a[(i-k)%n])
		cost_k = sum(costs) + k*x
		if k == 0:
			cost_min = cost_k
		else:
			cost_min = min(cost_min, cost_k)

n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
print(solve(n,x,a))




def solve(n, x, a):
	costs = []
	cost_min = 0
	for k in range(n):
		if k == 0:
			costs = a.copy()
		else:
			for i in range(n):
				costs[i] = min(costs[i], a[(i-k)%n])
		cost_k = sum(costs) + k*x
		if k == 0:
			cost_min = cost_k
		else:
			cost_min = min(cost_min, cost_k)
			if cost_min != cost_k:
				break
		if cost_min < (k+1) *x:
			break

	return cost_min
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
print(solve(n,x,a))




Verdicts: Wrong Answer

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
n,x = map(int,input().split())
a = list(map(int,input().split()))
MIN = sum(a)
ans = MIN
tori = a + [0]
tori.pop()
for i in range(1,n):
  for j in range(n):
    ido = i+j
    if ido >= n:
      ido-=n
    if tori[j] > a[ido]:
      tori[j] = a[ido]
      ans -= (tori[j] - a[ido])
  MIN = min(ans + i*x,MIN)
print(MIN)

n,x = map(int,input().split())
a = list(map(int,input().split()))
MIN = sum(a)
ans = MIN+0
tori = a + [0]
tori.pop()
for i in range(1,n):
  for j in range(n):
    ido = i+j
    if ido >= n:
      ido-=n
    if tori[j] > a[ido]:
      ans -= (tori[j] - a[ido])
      tori[j] = a[ido]
  MIN = min(ans + i*x,MIN)
print(MIN)

Verdicts: Wrong Answer

Accepted
Problem: Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.  Snuke can perform the following two actions:    * Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.    * Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.  Find the minimum time that Snuke needs to have slimes in all N colors.

Submission attempts (in order):
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def ris(): return list(input())
def pli(): return "".join(list(map(str, ans)))


N, x = rli()
a = rli()

catch_time = 0
magic_time = 0
mi = [2000000000001 for _ in range(N)]
for i in range(N):
    for j in range(N):
        if(mi[i] > a[i-j]+x*j):
            mi[i] = a[i-j]+x*j
            catch_slime = a[i-j]
            magic_time = max(magic_time, x*j)

print(sum(mi) + magic_time)


def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def ris(): return list(input())
def pli(): return "".join(list(map(str, ans)))


N, x = rli()
a = rli()
ans = 2000000000001
magic_time = 0
s_min = a[:]
for i in range(0, N):
    for j in range(N):
        s_min[j] = min(a[j-i], s_min[j])
    ans = min(sum(s_min) + x*i, ans)
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: There are N towns in Snuke Kingdom, conveniently numbered 1 through N. Town 1 is the capital.  Each town in the kingdom has a _Teleporter_ , a facility that instantly transports a person to another place. The destination of the Teleporter of town i is town a_i (1≤a_i≤N). It is guaranteed that **one can get to the capital from any town by using the Teleporters some number of times**.  King Snuke loves the integer K. The selfish king wants to change the destination of the Teleporters so that the following holds:    * Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.  Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.

Submission attempts (in order):
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

N,K = map(int,readline().split())
A = [int(x)-1 for x in readline().split()]

desc_edge = [set() for _ in range(N)]
parent = [0] * N
for i,x in enumerate(A[1:],1):
    
    desc_edge[x].add(i)
    parent[i]=x

depth = [None] * N
q = [0]
depth[0]=0
while q:
    x = q.pop()
    for y in desc_edge[x]:
        q.append(y)
        depth[y] = depth[x]+1

V = set(range(N))

q=[]
depth_to_v = [[] for _ in range(N)]
for v,x in enumerate(depth):
    depth_to_v[x].append(v)
for d in range(N):
    q += depth_to_v[d]


import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

N,K = map(int,readline().split())
A = [int(x)-1 for x in readline().split()]

desc_edge = [set() for _ in range(N)]
parent = [0] * N
for i,x in enumerate(A[1:],1):
    
    desc_edge[x].add(i)
    parent[i]=x

depth = [None] * N
q = [0]
depth[0]=0
while q:
    x = q.pop()
    for y in desc_edge[x]:
        q.append(y)
        depth[y] = depth[x]+1

V = set(range(N))

q=[]
depth_to_v = [[] for _ in range(N)]
for v,x in enumerate(depth):
    depth_to_v[x].append(v)
for d in range(N-1,-1,-1):
    q += depth_to_v[d]

q

answer = 0
for x in q:
    if x == 0:
        break
    if x not in V:
        continue
    if depth[x] <= K:
        break
    answer += 1
    y=x
    for _ in range(K-1):
        y = parent[y]
    
    qq = [y]
    while qq:
        z = qq.pop()
        V.remove(z)
        for w in desc_edge[z]:
            if w in V:
                qq.append(w)

if A[0] != 0:
    answer += 1
print(answer)

Verdicts: Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n,a=map(int,input().split())
x=list(map(int,input().split()))
y=[]
for i in x:
  y.append(i-a)
s=2*n*a
dp=[[0 for i in range(s+1)] for j in range(n+1)]
dp[0][n*a]=1
for i in range(n):
  for j in range(s+1):
    x=j-y[i]
    if(0<=x and x<=2*n*a):
      dp[i+1][j]=dp[i][j-y[i]]+dp[i][j]
    else:
      dp[i+1][j]=dp[i][j]

print(dp[n][n*a])

n,a=map(int,input().split())
x=list(map(int,input().split()))
y=[]
for i in x:
  y.append(i-a)
s=2*n*a
dp=[[0 for i in range(s+1)] for j in range(n+1)]
dp[0][n*a]=1
for i in range(n):
  for j in range(s+1):
    x=j-y[i]
    if(0<=x and x<=2*n*a):
      dp[i+1][j]=dp[i][j-y[i]]+dp[i][j]
    else:
      dp[i+1][j]=dp[i][j]

print(dp[n][n*a]-1)

Verdicts: Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
N, A = map(int, input().split())
X = list(map(int, input().split()))
Y = [x - A for x in X]

dp = [0] * 5010
dp[2500] = 1

for y in Y:
    for i in range(5009, -1, -1):
        if i - y < 0 or i - y >= 5010:
            dp[i] = 0
        else:
            dp[i] += dp[i - y]
print(dp[2500] - 1)


N, A = map(int, input().split())
X = list(map(int, input().split()))
Y = [x - A for x in X]

dp = [0] * 5010
dp[2500] = 1

for y in Y:
    r = []
    if y >= 0:
        r = range(5009, -1, -1)
    else:
        r = range(5010)
    for i in r:
        if i - y < 0 or i - y >= 5010:
            continue
        dp[i] += dp[i - y]
print(dp[2500] - 1)


Verdicts: Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):

from collections import defaultdict


def main():
    N, A = tuple(map(int, input().split()))
    X = tuple(map(int, input().split()))
    Y = [i - A for i in X]
    D = defaultdict(int)
    D[0] = 1
    for i in Y:
        for j, k in D.items():
            D[i + j] += k
    print(D[0] - 1)


if __name__ == "__main__":
    main()


from collections import defaultdict


def main():
    N, A = tuple(map(int, input().split()))
    X = tuple(map(lambda x: int(x) - A, input().split()))
    D = defaultdict(int)
    D[0] = 1
    for i in X:
        for j, k in list(D.items()):
            D[i + j] += k
    print(D[0] - 1)


if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
def dp(k, value):
    if k==n:
        if value==0:
            return 1
        else:
            return 0
    if memo[k][value]>=0:
        return memo[k][value]
    memo[k][value]=dp(k+1, value+x[k])+dp(k+1, value)
    return memo[k][value]
print(dp(0, 0)-1)

n, a =map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
memo=[[-1]*110 for i in range(60)]
def dp(k, value):
    if k==n:
        if value==0:
            return 1
        else:
            return 0
    if memo[k][value]>0:
        return memo[k][value]
    memo[k][value]=dp(k+1, value+x[k])+dp(k+1, value)
    print(memo[k][value], k, value)
    return memo[k][value]
print(dp(0, 0)-1)

n, a =map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
memo=[[-1]*6000 for i in range(60)]
def dp(k, value):
    if k==n:
        if value==0:
            return 1
        else:
            return 0
    if memo[k][value]>=0:
        return memo[k][value]
    memo[k][value]=dp(k+1, value+x[k])+dp(k+1, value)
    return memo[k][value]
print(dp(0, 0)-1)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
import bisect
from collections import Counter, deque
from itertools import combinations, accumulate
from math import factorial
from operator import neg
n, a = list(map(int, input().split()))
xs = list(map(lambda x: int(x) - a, input().split()))

xs.sort()
l, r = bisect.bisect_left(xs, 0), bisect.bisect_right(xs, 0)
low, n0, high = xs[:l], r - l, xs[r:]
if len(low) > len(high):
    low, high = list(map(neg, high)), list(map(neg, low))
else:
    high = high[::-1]
acc = list(accumulate(high[::-1]))[::-1]

factor = 2 ** n0
lsums = Counter([0])
for v in low:
    tmps = tuple(lsums.items())
    for u, cnt in tmps:
        lsums[u + v] += cnt
del lsums[0]
ans = 0

print(ans)

import bisect
from collections import Counter, deque
from itertools import combinations, accumulate
from math import factorial
from operator import neg
n, a = list(map(int, input().split()))
xs = list(map(lambda x: int(x) - a, input().split()))

xs.sort()
l, r = bisect.bisect_left(xs, 0), bisect.bisect_right(xs, 0)
low, n0, high = xs[:l], r - l, xs[r:]
if len(low) > len(high):
    low, high = list(map(neg, high)), list(map(neg, low))
else:
    high = high[::-1]
acc = list(accumulate(high[::-1]))[::-1]

factor = 2 ** n0
lsums = sum((Counter(map(sum, combinations(low, 1 + i))) for i in range(len(low))), Counter())

ans = 0
# for lsum, cnt in lsums.items():
#     tmp = 0


#     while len(q) > 0:

#         if i + 1 < len(high) and v + acc[i + 1] >= 0:


#             continue


#             tmp += 1
#             continue


#     ans += tmp * cnt

# ans *= factor
# ans += 2 ** n0 - 1

print(ans)

import bisect
from collections import Counter, deque
from itertools import combinations, accumulate
from math import factorial
from operator import neg
n, a = list(map(int, input().split()))
xs = list(map(lambda x: int(x) - a, input().split()))

xs.sort()
l, r = bisect.bisect_left(xs, 0), bisect.bisect_right(xs, 0)
low, n0, high = list(map(neg, xs[:l][::-1])), r - l, xs[r:]

def sums(arr):
    cntr = Counter([0])
    for v in arr:
        tmps = tuple(cntr.items())
        for u, cnt in tmps:
            cntr[u + v] += cnt
    del cntr[0]
    return cntr

lsums = sums(low)
hsums = sums(high)

ans = sum(hsums[a] * b for a, b in lsums.items())

ans *= 2 ** n0
ans += 2 ** n0 - 1

print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
def inpl(): return [int(i) for i in input().split()]
def hmd(a, b):
    ctr = -1
    while H[ctr+1][a] < b:
        ctr += 1
    vn = H[ctr][a]
    if (vn == b and ctr == 0) or ctr == -1:
        return 1
    return 2**ctr + hmd(vn,b)
N = int(input())
x = inpl() + [10**10]
L = int(input())
H = []
G = [N-1]*N
b = 0
for a in range(N):
    while x[b+1] - x[a] <= L:
        if b == N-1:
            break
        b += 1
    G[a] = b
H.append(G)
while G[0] != N-1:
    G = [G[i] for i in G]
    H.append(G)
H.append([10**10]*N)
Q = int(input())
for _ in range(Q):
    a, b = sorted(inpl())
    print(hmd(a-1,b-1))

from collections import defaultdict
H = defaultdict(lambda: 0)
H[0] = 1
def inpl(): return [int(i) for i in input().split()]
N, A = inpl()
x = inpl()
x = [i-A for i in x]
H[0]= 1
for i in x:
    for ni,nv in H.copy().items():
        H[ni+i] += nv
print(H[0]-1)

Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n,a=map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
d={0:0}
for i in x:
    for j,k in list(d.items()):
        d[i+j]=d.get(i+j,0)+k
print(d[0])
#sample code.


n,a=map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
d={0:1}
for i in x:
    for j,k in list(d.items()):
        d[i+.j]=d.get(i+j,0)+k
print(d[0]-1)
#sample code

n,a=map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
d={0:0}
for i in x:
    for j,k in list(d.items()):
        d[i+j]=d.get(i+j,0)+k
print(d[0]-1)
#sample code.

n,a=map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
d={0:2}
for i in x:
    for j,k in list(d.items()):
        d[i+j]=d.get(i+j,0)+k
print(d[0]-2)
#sample code.


n,a=map(int, input().split())
x=list(map(int, input().split()))
x=[i-a for i in x]
d={0:1}
for i in x:
    for j,k in list(d.items()):
        d[i+j]=d.get(i+j,0)+k
print(d[0]-1)
#sample code.

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):

line1=input().split()
n=int(line1[0])
a=int(line1[1])
nums=input().split()
for i in range(n):
    nums[i]=int(nums[i])-a

works={}

print (works)
s=0
for x in nums:

    if x>=0:
        for i in range(50*n,-50*n, -1):
            try:
                works[i]+=works[i-x]
            except:
                try:
                    works[i]=works[i-x]
                except:
                    pass
    else:
        for i in range (-50*n,50*n):
            try:
                works[i+x]+=works[i]
            except:
                try:
                    works[i+x]=works[i]
                except:
                    pass

print (works[0]-1)



line1=input().split()
n=int(line1[0])
a=int(line1[1])
nums=input().split()
for i in range(n):
    nums[i]=int(nums[i])-a

works={}

works[0]=1
s=0
for x in nums:

    if x>=0:
        for i in range(50*(n+1),-50*(n+1), -1):
            try:
                works[i]+=works[i-x]
            except:
                try:
                    works[i]=works[i-x]
                except:
                    pass
    else:
        for i in range (-50*(n+1),50*(n+1)):
            try:
                works[i+x]+=works[i]
            except:
                try:
                    works[i+x]=works[i]
                except:
                    pass

print (works[0]-1)


Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
N,A = map(int,input().split())
X = list(map(lambda x:int(x)-A,input().split()))

MX = max(X+[0])

dp = [[0 for i in range(2*MX*N+1)] for _ in range(N+1)]

for i in range(N+1):
    for t in range(2*MX*N+1):
        if i==0 and t==N*MX:
            dp[i][t] = 1
        elif i>=1:
            if t-X[i-1]>=0 and t-X[i-1]<=2*MX*N:
                dp[i][t] = dp[i-1][t]+dp[i-1][t-X[i-1]]
            else:
                dp[i][t] = dp[i-1][t]
        else:
            dp[i][t] = 0
    
print(dp[N][N*MX]-1)
print("\n".join([str(d) for d in dp]))

#ARC060 C



N,A = map(int,input().split())
X = list(map(lambda x:int(x)-A,input().split()))

MX = max(X+[0])

dp = [[0 for i in range(2*MX*N+1)] for _ in range(N+1)]

for i in range(N+1):
    for t in range(2*MX*N+1):
        if i==0 and t==N*MX:
            dp[i][t] = 1
        elif i>=1:
            if t-X[i-1]>=0 and t-X[i-1]<=2*MX*N:
                dp[i][t] = dp[i-1][t]+dp[i-1][t-X[i-1]]
            else:
                dp[i][t] = dp[i-1][t]
        else:
            dp[i][t] = 0
    
print(dp[N][N*MX]-1)
#print("\n".join([str(d) for d in dp]))

Verdicts: Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
def main():
    N, A = map(int, input().split())
    x = [0].extend(list(map(int, input().split())))

    dp = [[[0 for _ in range(N * A + 1)] for _ in range(N + 1)] for _ in range(N + 1)]
    for i in range(N + 1):
        dp[i][0][0] = 1

    for i in range(1, N + 1):
        for j in range(1, i + 1):
            for k in range(1, N * A + 1):
                if k < x[k]:
                    dp[i][j][k] = dp[i - 1][j][k]
                else:
                    dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]]

    ans = 0
    for j in range(1, N + 1):
        ans += dp[N][j][A * j]

    print(ans)


if __name__ == '__main__':
    main()


def main():
    N, A = map(int, input().split())
    x = [0]
    x.extend(list(map(int, input().split())))

    dp = [[[0] * (N * A + 1) for _ in range(N + 1)] for _ in range(N + 1)]
    for i in range(N + 1):
        dp[i][0][0] = 1

    for i in range(1, N + 1):
        for j in range(1, i + 1):
            for k in range(1, N * A + 1):
                if k < x[i]:
                    dp[i][j][k] = dp[i - 1][j][k]
                else:
                    dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]]

    ans = 0
    for j in range(1, N + 1):
        ans += dp[N][j][A * j]

    print(ans)


if __name__ == '__main__':
    main()


Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
import numpy as np
hi = np.zeros((5000,60),np.int64)
hi[0,0]=1
n,a = [int(it) for it in input().split()]
li = [int(it) for it in input().split()]
for i in li:
  　hi_ = hi.copy()
    hi[i:-1,1:]+=hi_[:-1-i,:-1]
s = 0
for i in range(n):
  s+=hi[(i+1)*a,i+1]
print (s)


import numpy as np
hi = np.zeros((5000,60),np.int64)
hi[0,0]=1
n,a = [int(it) for it in input().split()]
li = [int(it) for it in input().split()]
for i in li:
    hi_ = hi.copy()
    hi[i:-1,1:]+=hi_[:-1-i,:-1]
s = 0
for i in range(n):
  s+=hi[(i+1)*a,i+1]
print (s)


Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
import numpy as np

N, A = map(int, input().split())
X = [0] + list(map(int, input().split()))
NX = N * max(X + [A])

dp = np.zeros((N + 1, N + 1, NX + 1))

dp[0][0][0] = 1

for j in range(N + 1):
    for k in range(N + 1):
        for s in range(NX + 1):
            if j >= 1 and s < X[j]:
                dp[j][k][s] = dp[j - 1][k][s]
            elif j >= 1 and k >= 1 and s >= X[j]:
                dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - X[j]]

ret = 0

for k in range(1, N + 1):
    ret += dp[N][k][k * A]

print(ret)


import numpy as np

N, A = map(int, input().split())
X = [0] + list(map(int, input().split()))
NX = N * max(X + [A])

dp = np.zeros((N + 1, 2 * NX + 1), dtype=int)

dp[0][NX] = 1

for j in range(1, N + 1):
    y = X[j] - A
    for t in range(2 * NX + 1):
        if 0 <= t - y < 2 * NX:
            dp[j][t] = dp[j - 1][t] + dp[j - 1][t - y]
        else:
            dp[j][t] = dp[j - 1][t]

print(dp[N][NX] - 1)


Verdicts: Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n,a,*x=map(int,open(0).read().split());dp={0:1}
for i in x:
	for k,v in dp.items():
		dp[i+k-a]=dp.get(i+k-a,0)+v
print(dp[0]-1)

def main():
	n,a=map(int,input().split())
	y=[a-int(x) for x in input().split()]
	dp={0:1}
	for i in y:
		for k,v in list(dp.items()):
			dp[i+k]=dp.get(i+k,0)+v
	print(dp[0]-1)
if __name__=="__main__":
	main()

Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
N, A = map(int, input().split())  
X = tuple(map(int, input().split()))  

dp = [[[0]*(50*N+1) for _ in range(N+1)] for _ in range(N+1)]

dp[0][0][0] = 1  

for i in range(N):
    for j in range(N+1):
        for s in range(50*N+1):
            if s >= X[i]:  
                dp[i+1][j+1][s] += dp[i][j][s-X[i]]  
            dp[i+1][j][s] += dp[i][j][s]  
ans = 0
for i in range(1, N+1):
    ans += dp[N][i][i*A]
print(ans)


N, A = map(int, input().split())  
X = tuple(map(int, input().split()))  

dp = [[[0]*(50*N+1) for _ in range(N+1)] for _ in range(N+1)]

dp[0][0][0] = 1  

for i in range(N):
    now_card = X[i]
    for j in range(N+1):
        for s in range(50*N+1):
            if s >= now_card:  
                dp[i+1][j+1][s] += dp[i][j][s-now_card]  
            dp[i+1][j][s] += dp[i][j][s]  
ans = 0
for i in range(1, N+1):
    ans += dp[N][i][i*A]
print(ans)


N, A = map(int, input().split())  

X = list(map(int, input().split()))  


dp = [[[0]*(max(X)*N+1) for _ in range(N+1)] for _ in range(N+1)]


dp[0][0][0] = 1

for i in range(len(X)):
    for j in range(1, N+1):
        for k in range(1, N+1):
            for s in range(1, max(X)*N+1):
                if s < X[i]:
                    dp[j][k][s] = dp[j-1][k][s]
                if s >= X[i]:
                    
                    dp[j][k][s] = dp[j-1][k][s] + dp[j-1][k-1][s-X[i]]


ans = 0
for j in range(1, N+1):
    for k in range(1, N+1):
        ans += dp[j][k][k*A]

print(ans)


N, A = map(int, input().split())  

X = tuple(map(int, input().split()))  


dp = [[[0]*(50*N+1) for _ in range(N+1)] for _ in range(N+1)]


dp[0][0][0] = 1

for i in range(N):
    for j in range(N+1):
        for k in range(N*50+1):
            if dp[i][j][k]:  
                dp[i+1][j][k] += dp[i][j][k]  
                dp[i+1][j+1][k+X[i]] += dp[i][j][k]  

ans = 0
for i in range(1, N+1):
    ans += dp[N][i][i*A]

print(ans)


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n, a = map(int, input().split())
xlist = list(map(int, input().split()))

j = n
k = n
t = n * a
maxv = max(xlist)


dp = [[[0 for t in range(t + 1)] for k in range(k + 1)] for j in range(j + 1)]

dp[1][0][0] = 1
dp[0][0][0] = 1


#     print(d)
# print(dp[1][1][15])

for ji in range(0, j + 1):
    for ki in range(0, ji + 1):
        for ti in range(0, ki * maxv + 1):
            
            if ji == 0 and ki == 0 and ti == 0:
                # print('branch 1')
                dp[ji][ki][ti] = 1
            elif ji >= 1 and ti < xlist[ji - 1]:
                dp[ji][ki][ti] = dp[ji - 1][ki][ti]
                # print('branch 2')
            elif ji >= 1 and ki >= 1 and ti >= xlist[ji - 1]:
                
                dp[ji][ki][ti] = dp[ji - 1][ki][ti] + dp[ji - 1][ki - 1][ti - xlist[ji - 1]]
            # print(dp[ji][ki][ti])

count = 0
for i in range(1, k + 1):
    count += dp[j][i][i*a]

print(count)

n, a = map(int, input().split())
xlist = list(map(int, input().split()))

j = n
k = n
t = n * a
maxv = max(a, max(xlist))


dp = [[[0 for t in range(n*maxv + 1)] for k in range(k + 1)] for j in range(j + 1)]

dp[1][0][0] = 1
dp[0][0][0] = 1


#     print(d)
# print(dp[1][1][15])

for ji in range(0, j + 1):
    for ki in range(0, ji + 1):
        for ti in range(0, ki * maxv + 1):
            
            if ji == 0 and ki == 0 and ti == 0:
                # print('branch 1')
                dp[ji][ki][ti] = 1
            elif ji >= 1 and ti < xlist[ji - 1]:
                dp[ji][ki][ti] = dp[ji - 1][ki][ti]
                # print('branch 2')
            elif ji >= 1 and ki >= 1 and ti >= xlist[ji - 1]:
                
                dp[ji][ki][ti] = dp[ji - 1][ki][ti] + dp[ji - 1][ki - 1][ti - xlist[ji - 1]]
            # print(dp[ji][ki][ti])

count = 0
for i in range(1, k + 1):
    count += dp[j][i][i*a]

print(count)

Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n,a=map(int,input().split())
m=list(map(lambda x:int(x)-a,input().split()))
x=max(abs(max(m)),abs(min(m)))*n
dp=[[0for _ in range(2*x+1)]for i in range(n)]
dp[0][0+x]=1
dp[0][m[0]+x]=1
for i in range(1,n):
  y=m[i]
  for j in range(x):
    dp[i][j+y]+=dp[i-1][j]
    dp[i][j]+=dp[i-1][j]
print(dp[n-1][0]-1)

n,a=map(int,input().split())
m=list(map(lambda x:int(x)-a,input().split()))
x=max(abs(max(m)),abs(min(m)))*n
dp=[[0for _ in range(2*x+1)]for i in range(n)]
dp[0][0+x]=1
dp[0][m[0]+x]=1
for i in range(1,n):
  y=m[i]
  for j in range(x):
    if dp[i-1][j]==0:
      continue
    dp[i][j+y]=dp[i-1][j+y]+dp[i-1][j]
    dp[i][j]=dp[i-1][j]
print(dp[n-1][0+x]-1)

n,a=map(int,input().split())
m=list(map(lambda x:int(x)-a,input().split()))
x=max(abs(max(m)),abs(min(m)))*n
dp=[[0for _ in range(2*x+1)]for i in range(n)]
dp[0][0+x]=1
dp[0][m[0]+x]=1
for i in range(1,n):
  y=m[i]
  for j in range(x):
    dp[i][j+y]=dp[i-1][j+y]+dp[i-1][j]
    dp[i][j]=dp[i-1][j]
print(dp[n-1][0+x]-1)

n,a=map(int,input().split())
m=list(map(lambda x:int(x)-a,input().split()))
x=max(abs(max(m)),abs(min(m)))*n
dp=[[0for _ in range(2*x+1)]for i in range(n)]
dp[0][0+x]=1
dp[0][m[0]+x]=1
for i in range(1,n):
  y=m[i]
  for j in range(x):
    dp[i][j+y]+=dp[i-1][j]
    dp[i][j]+=dp[i-1][j]
print(dp[n-1][0+x]-1)

n,a=map(int,input().split())
m=list(map(lambda x:int(x)-a,input().split()))
x=max(abs(max(m)),abs(min(m)))*n
dp=[[0for _ in range(2*x+1)]for i in range(n)]
dp[0][0+x]+=1
dp[0][m[0]+x]+=1
for i in range(1,n):
  y=m[i]
  for j in range(2*x+1):
    if 0<=j+y and j+y<2*x+1:
      dp[i][j+y]+=dp[i-1][j]
    dp[i][j]+=dp[i-1][j]
  #print(y,dp[i])
print(max(0,dp[n-1][0+x]-1))

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n,a=map(int,input().split())
x=[0]+[i-a for i in map(int,input().split())]
d={}
def f(i):
    if i==n+1:
        return c([0])
    if i in d:
        return d[i]
    d[i]={}
    for j in range(i+1,n+2):
        for k,v in f(j).items():
            d[i].setdefault(x[i]+k,0)
            d[i][x[i]+k]+=v
    return d[i]
print(f(0)[0]-1)

n,a=map(int,input().split())
x=[0]+[i-a for i in map(int,input().split())]
d={}
def f(i):
    if i==n+1:
        return {0:1}
    if i in d:
        return d[i]
    d[i]={}
    for j in range(i+1,n+2):
        for k,v in f(j).items():
            d[i].setdefault(x[i]+k,0)
            d[i][x[i]+k]+=v
    return d[i]
print(f(0)[0]-1)

Verdicts: Runtime Error

Accepted
Problem: Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Submission attempts (in order):
n,a=map(int,input().split())
X=[int(i)-a for i in input().split()]

memo={0:1}
for x in X:
    for k,v in list(memo.items()):
        memo[x+k]=memo.get(x+k,0)+v
        print(memo)
print(memo[0]-1)


n,a=map(int,input().split())
X=[int(i)-a for i in input().split()]

memo={0:1}
for x in X:
    for k,v in list(memo.items()):
        memo[x+k]=memo.get(x+k,0)+v
print(memo[0]-1)



Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
s = int(input())

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

if s == n:
    print(n+1)
    exit()
if s == 1:
    print(n)
    exit()
for i in range(1, int(n**0.5) + 10):
    if(f(i, n) == s):
        print(i)
        exit()
ans = 10**15
for i in range(1, int(n**0.5) + 1):
    b = (n-s) // i + 1
    if f(b, n) == s and b>p and b>q:
        ans = min(ans, b)
if ans == 10**15:
    print(-1)
else:
    print(ans)

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

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

if s == n:
    print(n+1)
    exit()
for i in range(2, int(n**0.5) + 1):
    if(f(i, n) == s):
        print(i)
        exit()
ans = 10**15
for i in range(1, int(n**0.5) + 1):
    b = (n-s) // i + 1
    if b < 2:
        continue
    if f(b, n) == s:
        ans = min(ans, b)

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

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import math

N = int(input())
S = int(input())
L = N-S

def search(b, n=N):
    ans = 0
    while n >= b:
        ans += n%b
        n //= b
    ans += n
    return ans
s
if L < 0:
    print(-1)
elif L == 0:
    print(1)
else:
    P = []
    for l in range(1, int(math.sqrt(L))+1):
        if L%l == 0:
            P.append(l+1)
            P.append(L//l+1)
    P.sort()

    ans = -1
    for p in P:
        S0 = search(p)
        if S == S0:
            ans = p
            break

    print(ans)

import math

N = int(input())
S = int(input())
L = N-S

def search(b, n=N):
    ans = 0
    while n >= b:
        ans += n%b
        n //= b
    ans += n
    return ans


def main():

    if L < 0:
        print(-1)
    elif L == 0:
        print(N+1)
    else:
        P = []
        for l in range(1, int(math.sqrt(L))+2):
            if L%l == 0:
                P.append(l+1)
                P.append(L//l+1)
        P.sort()

        ans = -1
        for p in P:
            if p == 1: continue
            S0 = search(p)
            if S == S0:
                ans = p
                break

        print(ans)

if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
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)






import itertools
import math 
 
n,s = [int(input()) for _ in range(2)]
 
def n_base_sum(n,b):
    if(math.floor(n/b)):
        return n_base_sum(math.floor(n/b),b)+n%b
    else:
        return n%b
 
def search_p(n,s):
    for p in range(math.floor(math.sqrt(n))+1,0,-1):
        b = (n-s)//p + 1
        if b >1 :
            q = n % b
            if p+q ==s and q < b and n == p*b+q and p<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)))

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n = int(input())
s = int(input())

ans = []
for i in range(2, n):
    use_n = n
    tmp_ans = 0
    if i*i > n:
        break
    while use_n:
        tmp_ans += use_n%i
        use_n //= i
    if tmp_ans == s:
        print("a")
        print(i)
        exit()
    
for p in range(2, n):
    if p*p > n or p >= 100:
        break
    q = s - p
    if not 0<=q<100:
        continue
    b = (n-q)//p
    if n == p*b + q:
        ans.append(b)
if len(ans):
    print(min(ans))
else:
    print(-1)


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

sqrt = 0
for i in range(n+1):
    if i*i >= n:
        sqrt = i
        break

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


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

# b = 2 ~ sqrt

for b in range(2, sqrt+1):
    use_n = n
    ans_tmp = dsum(n, b)
    if ans_tmp == s:
        print(b)
        exit()




for p in range(1, sqrt)[::-1]:
    b = (n-s)//p + 1
    if b > 1 and dsum(n, b) == s:
        print(b)
        exit()
print(-1)


Verdicts: Wrong Answer

Accepted