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

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

def dfs(v, d=0, p=-1):
    global dist
    dist[v] = d
    for u in to[v]:
        if u == p:
            continue
        dfs(u, d+1, v)


def calcDist(s):
    global dist
    dist = [0 for _ in range(N)]
    dfs(s)
    return dist


N, s, t = map(int, input().split())
s -= 1
t -= 1
to = [[] for _ in range(N)]
dist = [0 for _ in range(N)]


for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

distS = calcDist(s)
distT = calcDist(t)

mx = 0
for i in range(N):
    if distS[i] < distT[i]:
        mx = max(mx, distT[i])

ans = mx - 1
print(ans)

Buggy submission (verdict: Runtime Error):
def dfs(v, d=0, p=-1):
    global dist
    dist[v] = d
    for u in to[v]:
        if u == p:
            continue
        dfs(u, d+1, v)


def calcDist(s):
    global dist
    dist = [0 for _ in range(N)]
    dfs(s)
    return dist


N, s, t = map(int, input().split())
s -= 1
t -= 1
to = [[] for _ in range(N)]
dist = [0 for _ in range(N)]


for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

distS = calcDist(s)
distT = calcDist(t)

mx = 0
for i in range(N):
    if distS[i] < distT[i]:
        mx = max(mx, distT[i])

ans = mx - 1
print(ans)
Problem p02863 (python)

Fixed submission:
from copy import deepcopy

# 多次元配列を作成する
def make_multi_list(initial, degree):
    ans = [initial for _ in range(degree[-1])]
    for d in reversed(degree[:-1]):
        ans = [deepcopy(ans) for _ in range(d)]
    return ans


N, T = map(int, input().split())
AB = []
for i in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort()

ans = 0
dp = make_multi_list(0, [3005, 3005])
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        # iを使わないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        # iを使うパターン
        nj = j + a
        if nj < T:
            dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j] + b)
    now = dp[i][T - 1] + b
    ans = max(ans, now)

print(ans)

Buggy submission (verdict: Runtime Error):
from copy import deepcopy
# 多次元配列を作成する
def make_multi_list(initial, degree):
    ans = [initial for _ in range(degree[-1])]
    for d in reversed(degree[:-1]):
        ans = [deepcopy(ans) for _ in range(d)]
    return ans


N, T = map(int, input().split())
AB = []
for i in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort()

ans = 0
dp = make_multi_list(0, [3000, 3000])
for i in range(N):
    a, b = AB[i]
    for j in range(T):
        # iを使わないパターン
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        # iを使うパターン
        nj = j+a
        if nj < T:
            dp[i+1][nj] = max(dp[i + 1][nj], dp[i][j]+b)
    now = dp[i][T-1]+AB[i][1]
    ans = max(ans, now)

print(ans)
Problem p02990 (python)

Fixed submission:
N, B = map(int, input().split())
R = N-B
MOD = 10**9+7

paskal3 = [[1]]
for i in range(1, N+5):
    l = [1]
    for j in range(1, i):
        l.append((paskal3[i-1][j-1]+paskal3[i-1][j]) % MOD)
    l.append(1)
    paskal3.append(l)


def comb(n, r):
    try:
        return paskal3[n][r]
    except:
        return 0


for i in range(1, B+1):
    print((comb(R+1, i) * comb(B-1, i-1)) % MOD)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
MOD = 10**9+7

paskal3 = [[1]]
for i in range(1, N+5):
    l = [1]
    for j in range(1, i):
        l.append((paskal3[i-1][j-1]+paskal3[i-1][j]) % MOD)
    l.append(1)
    paskal3.append(l)


def comb(n, r):
    return paskal3[n][r]


if N == K:
    for i in range(1, K+1):
        if i == 1:
            print(1)
        else:
            print(0)
    exit()

for i in range(1, K+1):
    print((comb(N-K+1, i) * comb(K-1, i-1)) % MOD)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

paskal3 = [[1]]
for i in range(1, N+5):
    l = [1]
    for j in range(1, i):
        l.append(paskal3[i-1][j-1]+paskal3[i-1][j])
    l.append(1)
    paskal3.append(l)


def comb(n, r):
    return paskal3[n][r]


if N == K:
    for i in range(1, K+1):
        if i == 1:
            print(1)
        else:
            print(0)
    exit()

for i in range(1, K+1):
    print((comb(N-K+1, i) * comb(K-1, i-1)) % (10**9+7))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

paskal3 = [[1]]
for i in range(1, N):
    l = [1]
    for j in range(1, i):
        l.append(paskal3[i-1][j-1]+paskal3[i-1][j])
    l.append(1)
    paskal3.append(l)


def comb(n, r):
    return paskal3[n][r]


for i in range(1, K+1):
    print((comb(N-K+1, i) * comb(K-1, i-1)) % (10**9+7))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

paskal3 = [[1]]
for i in range(1, N):
    l = [1]
    for j in range(1, i):
        l.append(paskal3[i-1][j-1]+paskal3[i-1][j])
    l.append(1)
    paskal3.append(l)


def comb(n, r):
    return paskal3[n][r]


for i in range(1, K+1):
    ans = comb(N-K+1, i) * comb(K-1, i-1)
    print(ans % (10**9+7))

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

paskal3 = [[1]]
for i in range(1, n):
    l = [1]
    for j in range(1, i):
        l.append(paskal3[i-1][j-1]+paskal3[i-1][j])
    l.append(1)
    paskal3.append(l)


def comb(n, r):
    return paskal3[n][r]


for i in range(1,k+1):
    print(comb(n-k+1, i) * comb(k-1, i-1))
Problem p03011 (python)

Fixed submission:
def abc():
    p, q, r = map(int, input().split())
    print(min(p + q, q + r, r + p))


abc()

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
MOD = 1000000007

broken = []
for i in range(M):
    broken.append(int(input()))

dp = [0] * (N + 2)
dp[N] = 1
for i in reversed(range(N)):
    if i in broken:
        dp[i] = 0
        if len(broken) != 0:
            broken.pop(-1)
        continue
    dp[i] = (dp[i+1] + dp[i+2]) % MOD

print(dp[0])
Problem p03013 (python)

Fixed submission:
N, B = map(int, input().split())
ana = []
dp = [0] * (N+2)

for i in range(B):
    ana.append(int(input()))

mod = 1000000007

dp[N] = 1
for i in reversed(range(N)):
    if len(ana) >= 1 and i == ana[-1]:
        dp[i] = 0
        if len(ana) != 0:
            ana.pop(-1)
        continue
    dp[i] = dp[i + 1] + dp[i + 2]
print(dp[0] % mod)

Buggy submission (verdict: Runtime Error):
# DP
N, M = map(int, input().split())
broken = set(map(int, input().split()))

MOD = 10**9+7
dp = [0 for i in range(N+2)]
dp[N] = 1

for i in reversed(range(N)):
    if i in broken:
        continue
    dp[i] = dp[i+1] + dp[i+2]
    dp[i] %= MOD

print(dp[0])

Buggy submission (verdict: Runtime Error):
# DP
N, M = map(int, input().split())
broken = list(map(int, input().split()))

MOD = 10**9+7
dp = [0 for i in range(N+1)]
dp[N] = 1

for i in reversed(range(N)):
    if i in broken:
        continue
    dp[i] = dp[i+1] + dp[i+2]
    dp[i] %= MOD

print(dp[0])

Buggy submission (verdict: Runtime Error):
# DP
N, M = map(int, input().split())
broken = list(map(int, input().split()))

MOD = 10**9+7
dp = [0 for i in range(N+2)]
dp[N] = 1

for i in reversed(range(N)):
    if i in broken:
        continue
    dp[i] = dp[i+1] + dp[i+2]
    dp[i] %= MOD

print(dp[0])
Problem p03015 (python)

Fixed submission:

import sys

sys.setrecursionlimit(10 ** 7)
# ----------

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

s = input().strip()

dp = [[0] * 2 for _ in range(100005)]
dp[0][0] = 1

n = len(s)
for i in range(n):
    if s[i] == "0":
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
    else:
        dp[i + 1][1] = (dp[i][0] + dp[i][1]) % MOD

    if s[i] == "0":
        dp[i + 1][1] += dp[i][1] * 2 % MOD
    else:
        dp[i + 1][0] += dp[i][0] * 2 % MOD
        dp[i + 1][1] += dp[i][1] * 2 % MOD

ans = (dp[n][0] + dp[n][1]) % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 7)
# ----------

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

s = si()

dp = [[0] * 2 for _ in range(100005)]
dp[0][0] = 1

n = len(s)
for i in range(n):
    if s[i] == "0":
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
    else:
        dp[i + 1][1] = (dp[i][0] + dp[i][1]) % MOD

    if s[i] == "0":
        dp[i + 1][1] += dp[i][1] * 2 % MOD
    else:
        dp[i + 1][0] += dp[i][0] * 2 % MOD
        dp[i + 1][1] += dp[i][1] * 2 % MOD

ans = (dp[n][0] + dp[n][1]) % MOD
print(ans)
Problem p03253 (python)

Fixed submission:
# 逆元
# a^{-1} mod を計算する
def mod_inv(a, m):
    return pow(a, m-2, m)


def combination(a, b):
    if b > a - b:
        return combination(a, a - b)

    # 10C3 = (10*9*8)/(3*2*1)
    # 10*9*8 -> ans_mul
    # 3*2*1 -> ans_div
    ans_mul = 1
    ans_div = 1
    for i in range(b):
        ans_mul *= a - i
        ans_div *= i + 1
        ans_mul %= MOD
        ans_div %= MOD
    # ans_mul / ans_div をやりたい
    # ans_div の逆元を使って求める
    ans = ans_mul * mod_inv(ans_div, MOD)
    return ans


N, M = map(int, input().split())
MOD = 10**9+7
# M を素因数分解
rest = M
ans = 1
for i in range(2, int(M ** 0.5) + 1):
    cnt = 0
    while rest % i == 0:
        cnt += 1
        rest //= i
    # cnt が 2^X 1とか 3^X の X の部分
    ans *= combination(N+cnt-1, N-1)
    ans %= MOD
if rest != 1:
    cnt = 1
    ans *= combination(N+cnt-1, N-1)
    ans %= MOD

print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
MOD = 10**9+7


# 繰り返し二乗法
# 普通の pow() より高速
def pow2(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans


# 逆元
# a^{-1} mod を計算する
def mod_inv(a, m):
    return pow2(a, m-2, m)


def combination(a, b):
    if b > a - b:
        return combination(a, a - b)
    # 10C3 = (10*9*8)/(3*2*1)
    # 10*9*8 -> ans_mul
    # 3*2*1 -> ans_div
    ans_mul = 1
    ans_div = 1
    for i in range(b):
        ans_mul *= a - i
        ans_div *= i + 1
        ans_mul %= MOD
        ans_div %= MOD
    # ans_mul / ans_div をやりたい
    # ans_div の逆元を使って求める
    ans = ans_mul * mod_inv(ans_div, MOD)
    return ans


# M を素因数分解
m_nokori = M
ans = 1
for i in range(2, M+1):
    if m_nokori == 1:
        break
    cnt = 0
    while m_nokori % i == 0:
        cnt += 1
        m_nokori //= i
    # cnt が 2^X とか 3^X の X の部分
    ans *= combination(N+cnt-1, N-1)
    ans %= MOD

print(ans)
Problem p03634 (python)

Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)


def dfs(v, parent, d):
    depth[v] = d
    for to, cost in tree[v]:
        if to == parent:
            continue
        dfs(to, v, d + cost)


depth = defaultdict(int)
tree = defaultdict(list)
n = int(input())
for i in range(n - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    tree[a].append((b, c))
    tree[b].append((a, c))

q, k = map(int, input().split())
k -= 1

dfs(k, -1, 0)
for i in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(depth[x] + depth[y])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict


def dfs(v, parent, d):
    depth[v] = d
    for to, cost in tree[v]:
        if to == parent:
            continue
        dfs(to, v, d + cost)


depth = defaultdict(int)
tree = defaultdict(list)
n = int(input())
for i in range(n-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    tree[a].append((b, c))
    tree[b].append((a, c))

q, k = map(int, input().split())
k -= 1

dfs(k, -1, 0)
for i in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    print(depth[x] + depth[y])
Problem p02693 (python)

Fixed submission:
i=int(input())
n,m=map(int,input().split())
s=[]
if n%i==0:
  print('OK')
elif m%i==0:
  print('OK')
else:
  for j in range(n,m+1):
    if j%i==0:
      s.append(j)
  if len(s)>0:
    print('OK')
  else:
    print('NG')

Buggy submission (verdict: Runtime Error):

n=int(input())
m,t=map(int,input().split())
if n<=t-m:
  print('OK')
else:
  print('NG')
elif n==1:
  print('OK')
Problem p02729 (python)

Fixed submission:
a,b=map(int,input().split())
c=0
d=a*(a-1)//2
e=b*(b-1)//2
while d>0:
  d=d-1
  c=c+1
while e>0:
  e=e-1
  c=c+1
print(c)

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split()):
c=0
d=d*(d-1)//2
e=e*(e-1)//2
while d>0:
  d=d-1
  c=c+1
while e>0:
  e=e-1
  c=c+1
print(c)

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
c=0
for i in range(a+b)
  if (a-2)>0 or (b-2)>0:
    a=a-2
    b=b-2
    c=c+1
print(c)
Problem p02771 (python)

Fixed submission:
a,b,c=map(int,input().split())
if a==b and a!=c:
  print('Yes')
elif a==c and a!=b:
  print('Yes')
elif b==c and b!=a:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
a,b,c=map(int,input().split())
if a==b and a!=c
  print('Yes')
elif a==c and a!=b:
  print('Yes')
elif b==c and b!=a:
  print('Yes')
else:
  print('NO')
Problem p02785 (python)

Fixed submission:
a,b=map(int,input().split())
n=list(map(int,input().split()))
n.sort(reverse=True)
n[0:b]=[]
print(sum(n))

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
n=list(map(int,input().split()))
n.sort(reverse=True)
for i in range(b):
  n.remove(n[i])
print(sum(n))

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
n=list(map(int,input().split()))
n.sort(reverse=True)
for i in range(b):
  n.remove(n[i])
print(sum(n))
Problem p03135 (python)

Fixed submission:
T,X=map(int,input().split())
ans=T/X
print(ans)

Buggy submission (verdict: Runtime Error):
T,X=map(int,input.split())
ans=T/X
print(ans)
Problem p03315 (python)

Fixed submission:
S=list(input())
ans=0
if S[0]=='+':
    ans+=1
else :
    ans-=1
if S[1]=='+':
    ans+=1
else :
    ans-=1
if S[2]=='+':
    ans+=1
else :
    ans-=1
if S[3]=='+':
    ans+=1
else :
    ans-=1    
print(ans)

Buggy submission (verdict: Runtime Error):
S=list(input())
ans=0
if S[0]=='+':
    ans+=1
  else :
    ans-=1
if S[1]=='+':
    ans+=1
  else :
    ans-=1
if S[2]=='+':
    ans+=1
  else :
    ans-=1
if S[3]=='+':
    ans+=1
  else :
    ans-=1    
print(ans)
Problem p02790 (python)

Fixed submission:
[a,b] = [int(s) for s in input().rstrip().split(' ')]
W = ""
if a < b:
  for i in range(b):
    W += str(a)
  print(W)
else:
  for i in range(a):
    W += str(b)
  print(W)

Buggy submission (verdict: Runtime Error):
[a,b] = [int(s) for s in input().rstrip().split(' ')]
W = ""
if a < b:
  for i in range(b):
    W.append(str(a))
  print(W)
else:
  for i in range(a):
    W.append(str(b))
  print(W)
Problem p02773 (python)

Fixed submission:
import sys
from collections import Counter

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.read().split()

def main():
    c = Counter(s)
    ma = max(c.values())
    res = []
    for l, v in c.items():
        if v == ma:
            res.append(l)
    return sorted(res)

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter

n, *s = sys.stdin.read().split()

def main():
    c = Counter(s)
    m = max(c.values())
    ans = [s for s, c in cnt.items() if c == m]
    print(*sorted(ans), sep='\n')
         
if __name__ ==  '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter

n, *s = sys.stdin.read().split()

def main():
    c = Counter(s)
    m = max(c.values())
    ans = [s for s, c in cnt.items if c == m]
    print(*sorted(ans), sep='\n')
         
if __name__ ==  '__main__':
    main()
Problem p02815 (python)

Fixed submission:
import sys

MOD = 10 ** 9 + 7

def make_table(size=10**6, p=MOD):
    fac = [None] * (size + 1)
    fac[0] = 1
    for i in range(size):
        fac[i+1] = fac[i] * (i + 1) % p
    ifac = [None] * (size + 1)
    ifac[size] = pow(fac[size], p-2, p)
    for i in range(size, 0, -1):
        ifac[i-1] = ifac[i] * i % p
    return fac, ifac

fac, ifac = make_table(2*10**5)

def comb(n, r, mod=MOD):
    if r > n or r < 0:
        return 0
    return fac[n] * ifac[r] % mod * ifac[n-r] % mod

n, *c = map(int, sys.stdin.read().split())

def main():
    c.sort(reverse=True)
    res = 0
    # for i in range(n):
    #     cnt = 0
    #     for d in range(1, n+1):
    #         for j in range(d):
    #             cnt += comb(i, j) * comb(n-i-1, d-j-1) * (d - j) % MOD
        
    #     res += c[i] * cnt
    #     res %= MOD
    for i in range(n):
        res += c[i] * (i + 2)
        res %= MOD
    return res * 4 ** (n - 1) % MOD

if __name__ == '__main__':
    ans = main()
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys

MOD = 10 ** 9 + 7

def make_table(size=10**6, p=MOD):
    fac = [None] * (size + 1)
    fac[0] = 1
    for i in range(size):
        fac[i+1] = fac[i] * (i + 1) % p
    ifac = [None] * (size + 1)
    ifac[size] = pow(fac[size], p-2, p)
    for i in range(size, 0, -1):
        ifac[i-1] = ifac[i] * i % p
    return fac, ifac

fac, ifac = make_table(10**5)

def comb(n, r, mod=MOD):
    if r > n or r < 0:
        return 0
    return fac[n] * ifac[r] % mod * ifac[n-r] % mod

n, *c = map(int, sys.stdin.read().split())

def main():
    c.sort()
    res = 0
    for i in range(n):
        cnt = 0
        for d in range(1, n+1):
            for j in range(d):
                cnt += comb(i, j) * comb(n-i-1, d-j-1) * (d - j) % MOD
        
        res += c[i] * cnt
        res %= MOD
    return res * 2 ** n % MOD

if __name__ == '__main__':
    ans = main()
    print(ans)
Problem p02873 (python)

Fixed submission:
import sys
from collections import deque
s = deque(s for s in sys.stdin.readline().rstrip())

s.append(None)
ans = 0
while s[0] != None:
    l_c = 0
    while s[0] == '<':
        l_c += 1
        s.popleft()
    
    r_c = 0
    while s[0] == '>':
        r_c += 1
        s.popleft()

    if l_c >= r_c:
        ans += (1 + l_c) * l_c // 2 + r_c * (r_c - 1) // 2
    else:
        ans += l_c * (l_c - 1) // 2 + (1 + r_c) * r_c // 2

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
s = [s for s in sys.stdin.readline().rstrip()]

ans = 0
if s[0] == '>':
    count = 1
    i = 1
    while s[i] == '>':
        count += 1
        i += 1
    del s[:i]
    ans += (1 + count) * count // 2

if s[-1] == '<':
    count = 1
    i = -2
    while s[i] == '>':
        count += 1
        i -= 1
    del s[i+1:]
    ans += count
    (1 + count) * count // 2

s.append(None)

while s != [None]:
    l_c, r_c = 0, 0
    i = 0
    # if s[i] == '<':
    while s[i] == '<':
        l_c += 1
        i += 1
    del s[:i]

    j = 0
    # if s[j] == '>':
    while s[j] == '>':
        r_c += 1
        j += 1
    del s[:j]

    if l_c >= r_c:
        ans += (1 + l_c) * l_c // 2 + r_c * (r_c - 1) // 2
    else:
        ans += l_c * (l_c - 1) // 2 + (1 + r_c) * r_c // 2

print(ans)
Problem p02879 (python)

Fixed submission:
a, b = map(int, input().split())
if a <= 9 and b <= 9:
    ans = a * b
else:
    ans = -1

print(ans)

Buggy submission (verdict: Runtime Error):
a, b = map(int, input())
if a <= 9 and b <= 9:
    ans = a * b
else:
    ans = -1

print(ans)
Problem p02881 (python)

Fixed submission:
import math

n = int(input())

ans  = 10 ** 12 
for i in range(1, math.floor(math.sqrt(n)) + 1):
    if n%i != 0:
        continue
    j = int(n/i)
    ans = min(ans, i+j-2)

print(ans)

Buggy submission (verdict: Runtime Error):
import math

n = int(input())

ans  = 10 ** 7 
for i in range(1, math.sqrt(n) + 1):
    if n%i != 0:
        continue
    j = n/i
    ans = min(ans, i+j-2)

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())

for x in range(1, 10**6+1):
    if x**2 >= n:
        y = x
        break

i, j = y, y
for x in range(10**5):
    if i*j == n:
        ans = i + j - 2
        break
    elif i*j > n:
        i -= 1
    elif i*j < n:
        j += 1

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())

for x in range(1, 10**6):
    if x**2 >= n:
        y = x
        break

i, j = y, y
for x in range(10**6):
    if i*j == n:
        ans = i + j - 2
        break
    elif i*j > n:
        i -= 1
    elif i*j < n:
        j += 1

print(ans)
Problem p02891 (python)

Fixed submission:
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            # if k % 2 == 0:
            ans = len(s) * k // 2
            # else:
            #     ans = len(s) // 2 * (k // 2 + 1) + (len(s) // 2 + 1) * k // 2
        print(ans)
        sys.exit()

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()
Problem p02954 (python)

Fixed submission:
# 2019-11-16 10:55:08(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import re

def main():
    # 左がRで右がLとなっている箇所でそれぞれどちらかにあつまる。10^100 は実質無限なので距離が奇数か偶数かで場合わけ
    s = sys.stdin.readline().rstrip() + 'R' # 後々のループ処理のときのことを考えて最後にRを足しておく
    n = len(s) - 1
    all_occurrences_of_rl = [m.start() for m in re.finditer('RL', s)]
    
    counts = [0 for _ in range(n)]
    left = 0
    for i in range(len(all_occurrences_of_rl)):
        right = all_occurrences_of_rl[i] + 1
        while s[right+1] == 'L': # 次がLだったら右端を拡張していく
            right += 1
        
        r_count = 1 + (all_occurrences_of_rl[i] - left) // 2 + (right - all_occurrences_of_rl[i]) // 2
        l_count = (right - left + 1) - r_count
        counts[all_occurrences_of_rl[i]] = r_count
        counts[all_occurrences_of_rl[i] + 1] = l_count

        left = right + 1
    
    for i in range(n):
        print(counts[i], end=' ')
        

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# 2019-11-16 10:55:08(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import re

def main():
    # 左がRで右がLとなっている箇所でそれぞれどちらかにあつまる。10^100 は実質無限なので距離が奇数か偶数かで場合わけ
    s = sys.stdin.readline().rstrip() + 'R' # 後々のループ処理のときのことを考えて最後にRを足しておく
    n = len(s) - 1
    all_occurrences_of_rl = [m.start() for m in re.finditer('RL', s)]
    
    counts = [0 for _ in range(n)]
    left = 0
    for i in range(len(all_occurrences_of_rl)):
        right = all_occurrences_of_rl[i] + 1
        while s[right+1] == 'L':　# 次がLだったら右端を拡張していく
            right += 1
        
        r_count = 1 + (all_occurrences_of_rl[i] - left) // 2 + (right - all_occurrences_of_rl[i]) // 2
        l_count = (right - left + 1) - r_count
        counts[all_occurrences_of_rl[i]] = r_count
        counts[all_occurrences_of_rl[i] + 1] = l_count

        left = right + 1
    
    for i in range(n):
        print(counts[i], end=' ')
        

if __name__ == "__main__":
    main()