Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())

pow2 = [1]
for i in range(1, k):
    pow2.append(pow2[i-1]*2)
win = 0
for i in range(1, n+1):
    for j, x in enumerate(pow2):
        if x > k/i:
            index = j
            break
    win += pow(1/2, index)
print(win/n)

Fixed submission:
n, k = map(int, input().split())
win = 0
for i in range(1, n+1):
    index = 0
    while pow(2, index) < k/i:
        index += 1
    win += pow(0.5, index)/n
print(win)

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

pow2 = [1]
for i in range(1, k):
    pow2.append(pow2[i-1]*2)
win = 0
for i in range(1, n+1):
    for j, x in enumerate(pow2):
        if x > k/i:
            index = j
            break
    bunbo = pow(2, index)
    win += 1/bunbo
print(win/n)

Fixed submission:
n, k = map(int, input().split())
win = 0
for i in range(1, n+1):
    index = 0
    while pow(2, index) < k/i:
        index += 1
    win += pow(0.5, index)/n
print(win)
Buggy python submission (verdict: Runtime Error):
a, b, c = list(map(int, input().split()))
if a[-1] == b[0] and b[-1] == c[0]:
    print('YES')
else:
    print('NO')

Fixed submission:
a, b, c = list(input().split())
if a[-1] == b[0] and b[-1] == c[0]:
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Runtime Error):
import itertools
import numpy as np

N, M = map(int, input().split())
X = [[] for _ in range(M)]
for i in range(M):
    X[i] = list(map(int, input().split()))
permutations_list = list(itertools.permutations([i for i in range(N-1)]))

count = 0
for i in range(len(permutations_list)):
    x = np.argsort(permutations_list[i])
    if [1, x[0]+2] not in X and [x[0]+2, 1] not in X:
        count += 1
        continue
    for j in range(N-2):
        pair = [x[j]+2, x[j+1]+2]
        reverse = list(reversed(pair))
        if pair not in X and reverse not in X:
            count += 1
            break
print(len(permutations_list) -count)

Fixed submission:
import itertools
import numpy as np

N, M = map(int, input().split())
X = [[] for _ in range(M)]
for i in range(M):
    X[i] = list(map(int, input().split()))
permutations_list = list(itertools.permutations([i for i in range(N-1)]))

count = 0
for i in range(len(permutations_list)):
    x = np.argsort(permutations_list[i])
    if [1, x[0]+2] not in X and [x[0]+2, 1] not in X:
        count += 1
        continue
    for j in range(N-2):
        pair = [x[j]+2, x[j+1]+2]
        reverse = list(reversed(pair))
        if pair not in X and reverse not in X:
            count += 1
            break
print(len(permutations_list) -count)
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	ll n,m,k;
	cin>>n>>m>>k;
	ll a[n+1];
	ll b[m+1];
	a[0]=0;
	b[0]=0;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		a[i]+=a[i-1];
	}
	for(ll i=1;i<=m;i++)
	{
		cin>>b[i];
		b[i]+=b[i-1];
	}
	ll j=m;
	ll ans=0;
	for(ll i=1;i<=n;i++)
	{
		if(a[i]>k)break;
		while(j>=0 && a[i]+b[j]>k)
		{
			j--;
		}
		if(j>=0)
		ans=max(ans,i+j);
	}
	cout<<ans;
	return 0;
}

Fixed submission:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a, b = [0], [0]
for i in range(N):
	a.append(a[i] + A[i])
for i in range(M):
	b.append(b[i] + B[i])

ans, j = 0, M
for i in range(N + 1):
	if a[i] > K:
		break
	while b[j] > K - a[i]:
		j -= 1
	ans = max(ans, i + j)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
s = 1
for a in A:
    s = s * a   
if s > 10**18 :
    print('-1')
else:
    print(s)
        

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
s = 1
over = False
zero = False

for i in range(N):
    if A[i] == 0:
        zero = True
        break

if zero:
    print(0)
else:
    for i in range(N):
        s *= A[i]
        if s > 10**18:
            over = True
            break
    if over :
        print('-1')
    else:
        print(s)
Buggy python submission (verdict: Runtime Error):
import bisect
import math
 
N,M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
 
for m in range(M):
    A[N-1] = math.floor( A[N-1]/2 )
    temp = A[N-1]
    del A[N-1]
    # 挿入    
    bisect.insort(A, temp)
print(sum(A))

Fixed submission:
S = input()
if S == "Sunny" : 
  print("Cloudy")

if S == "Cloudy" : 
  print("Rainy")

if S == "Rainy" : 
  print("Sunny")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = 0
for i in range(1,N):
    s = s + divmod(i,i+1)[1]
s = s + divmod(N,1)[1]
print(s)

Fixed submission:
N = int(input())
print((N-1)*N//2)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
d = list(map(int, input().split()))
c = 0

c_R = 0
c_B = 0

d_min = min(d)
d_max = max(d)

for K in range(d_min,d_max+1):
    c_R = 0
    c_B = 0
    for i in range(N):
        if K <= d[i] :
            c_R = c_R + 1
        else:
            c_B = c_B + 1
    if c_R == c_B:
        c = c + 1

print(c)
        
    


Fixed submission:
N = int(input())
d = list(map(int, input().split()))
d.sort()
K = d[N//2]
J = d[N//2 - 1]
if K == J :
    print(0)
else:
    print(K - J)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        self.parent[self.find(b)] = self.find(a)

n, m = map(int, input().split())
UF = UnionFind(n)
for _ in range(m):
    a,b = map(int, input().split())
    UF.union(a-1,b-1)
o=set()
for i in range(n):
    o.add(UF.fins(i))
print(len(o) - 1)

Fixed submission:
import sys
input = sys.stdin.readline

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def union(self, a, b):
        self.parent[self.find(b)] = self.find(a)

n, m = map(int, input().split())
UF = UnionFind(n)
for _ in range(m):
    a,b = map(int, input().split())
    UF.union(a-1,b-1)
o=set()
for i in range(n):
    o.add(UF.find(i))
print(len(o) - 1)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

n += 5

sa = set(a)
sb = set(b)

count = [0] * n
counta = [0] * n
countb = [0] * n
for v in a:
    count[v] += 1
    counta[v] += 1
for v in b:
    count[v] += 1
    countb[v] += 1

outA = dict()
for v in a:
    outA[v] = []

import heapq
q = []
for v in range(n):
    if count[v]:
        heapq.heappush(q,(-count[v],v))
    
while q[0][0] < 0:
    c, v = heapq.heappop(q)
    if count[v] + c:
        heapq.heappush(q,(-count[v],v))
        continue
    if counta[v]:
        for v2 in sb:
            if v2 != v:
                break
        else:
            print('No')
            exit()
        outA[v].append(v2)
        count[v] -= 1
        count[v2] -= 1
        counta[v] -= 1
        countb[v2] -= 1
        if counta[v] == 0:
            sa.remove(v)
        if countb[v2] == 0:
            sb.remove(v2)
    else:
        for v2 in sa:
            if v2 != v:
                break
        else:
            print('No')
            exit()
        outA[v2].append(v)
        count[v] -= 1
        count[v2] -= 1
        counta[v2] -= 1
        countb[v] -= 1
        if counta[v2] == 0:
            sa.remove(v2)
        if countb[v] == 0:
            sb.remove(v)
    if count[v]:
        heapq.heappush(q, (-count[v], v))

out = []
for v in a:
    out.append(outA[v].pop())
print('Yes')
print(' '.join(map(str,out)))


Fixed submission:
print(1-int(input()))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def dist(p1,p2):
    return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

points = []
N = int(input())
for i in range(N):
    x, y = map(int, input().split())
    points.append((x,y))

cand = []
for i in (-1,0,1):
    for j in (-1,0,1):
        points.sort(key = lambda x: i*x[0] + j*x[1])
        cand.append(points[0])
outs = []
for i in cand:
    for p in points:
        outs.append(dist(i,p))
print(max(outs))


Fixed submission:
import sys
input = sys.stdin.readline

def dist(p1,p2):
    return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

points = []
N = int(input())
for i in range(N):
    x, y = map(int, input().split())
    points.append((x,y))

cand = []
for i in (-1,0,1):
    for j in (-1,0,1):
        best = -10 ** 9
        bestP = (0,0)
        for p in points:
            val = i * p[0] + j * p[1]
            if val > best:
                best = val
                bestP = p
        cand.append(bestP)
                
best = 0
for i in cand:
    for p in points:
       best = max(best,dist(i,p))
print(best)

Buggy python submission (verdict: Time Limit Exceeded):
def dist(p1,p2):
    return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

points = []
N = int(input())
for i in range(N):
    x, y = map(int, input().split())
    points.append((x,y))

cand = []
for i in (-1,0,1):
    for j in (-1,0,1):
        points.sort(key = lambda x: i*x[0] + j*x[1])
        cand.append(points[0])
outs = []
for i in cand:
    for p in points:
        outs.append(dist(i,p))
print(max(outs))


Fixed submission:
import sys
input = sys.stdin.readline

def dist(p1,p2):
    return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

points = []
N = int(input())
for i in range(N):
    x, y = map(int, input().split())
    points.append((x,y))

cand = []
for i in (-1,0,1):
    for j in (-1,0,1):
        best = -10 ** 9
        bestP = (0,0)
        for p in points:
            val = i * p[0] + j * p[1]
            if val > best:
                best = val
                bestP = p
        cand.append(bestP)
                
best = 0
for i in cand:
    for p in points:
       best = max(best,dist(i,p))
print(best)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

R, C, K = map(int, input().split())

items = dict()

for _ in range(K):
    r, c, v = map(int, input().split())
    items[(r-1,c-1)] = v

dp = [0] * 4 * R * C

if (0,0) in items:
    dp[1] = items[(0,0)]

def ind(i,j,k):
    return 4 * C * i + 4 * j + k

for row in range(R):
    for col in range(C):
        for pick in range(4):
            curr = dp[ind(row,col,pick)]
            #Go Right
            if col + 1 < C:
                dp[ind(row,col+1,pick)] = max(curr, dp[ind(row,col+1,pick)])
                if (row, col+1) in items and pick < 3:
                    dp[ind(row,col+1,pick+1)] = max(curr + items[(row,col+1)], dp[ind(row,col+1,pick+1)])
            #Go Down
            if row + 1 < R:
                dp[ind(row+1,col,0)] = max(curr, dp[ind(row+1,col,0)])
                if (row + 1, col) in items:
                    dp[ind(row+1,col,1)] = max(curr + items[(row+1,col)], dp[ind(row+1,col,1)])
                    

print(max(dp))
            


Fixed submission:
import sys
input = sys.stdin.readline

R, C, K = map(int, input().split())

items = [0] * R * C

for _ in range(K):
    r, c, v = map(int, input().split())
    items[C * (r-1) + c-1] = v

dp = [0] * 4 * R * C

dp[1] = items[0]

def ind(i,j,k):
    return 4 * C * i + 4 * j + k

for row in range(R):
    for col in range(C):
        for pick in range(4):
            curr = dp[ind(row,col,pick)]
            #Go Right
            if col + 1 < C:
                dp[ind(row,col+1,pick)] = max(curr, dp[ind(row,col+1,pick)])
                if pick < 3:
                    dp[ind(row,col+1,pick+1)] = max(curr + items[C * row + col+1], dp[ind(row,col+1,pick+1)])
            #Go Down
            if row + 1 < R:
                dp[ind(row+1,col,0)] = max(curr, dp[ind(row+1,col,0)])
                dp[ind(row+1,col,1)] = max(curr + items[C * (row+1) + col], dp[ind(row+1,col,1)])
                    

print(max(dp))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def gcd(x, y):
    x = abs(x)
    y = abs(y)

    if x < y:
        x, y = y, x

    while y:
        x, y = y, x%y
        
    return x

N = int(input())
seen = dict()
for _ in range(N):
    a, b = map(int, input().split())
    if a < 0:
        a, b = -a,-b
    if b < 0:
        a, b = -b, a
        flip = 1
    else:
        flip = 0
    g = gcd(a,b)
    a//=g
    b//=g
    if (a,b) not in seen:
        seen[(a,b)] = [0,0]
    
    seen[(a,b)][flip] += 1

MOD = 10 ** 9 + 7
    
out = 1
for v in seen:
    a, b = seen[v]
    out *= (pow(2,a,MOD) + pow(2,b,MOD) - 1)
    out %= MOD
out -= 1
out %= MOD
print(out)


Fixed submission:
import sys
input = sys.stdin.readline

def gcd(x, y):
    x = abs(x)
    y = abs(y)

    if x < y:
        x, y = y, x

    while y:
        x, y = y, x%y
        
    return x

N = int(input())
seen = dict()
zeroes = 0
for _ in range(N):
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        zeroes += 1
        continue
    
    if a < 0 or a == 0 and b < 0:
        a, b = -a,-b
    if b <= 0:
        a, b = -b, a
        flip = 1
    else:
        flip = 0
    g = gcd(a,b)
    a//=g
    b//=g
    if (a,b) not in seen:
        seen[(a,b)] = [0,0]
    
    seen[(a,b)][flip] += 1

MOD = 10 ** 9 + 7
    
out = 1
for v in seen:
    a, b = seen[v]
    out *= (pow(2,a,MOD) + pow(2,b,MOD) - 1)
    out %= MOD
out -= 1

out += zeroes
out %= MOD
print(out)
Buggy python submission (verdict: Runtime Error):
	s, w = map(int,input().split())
if w >= s:
    print('un',end='')
print('safe')


Fixed submission:
s, w = map(int,input().split())
if w >= s:
    print('un',end='')
print('safe')
Buggy python submission (verdict: Time Limit Exceeded):
n, m, s = map(int,input().split())
adj = [[] for i in range(n)]
for _ in range(m):
    u, v, a, b = map(int,input().split())
    adj[u - 1].append((v - 1,-a,b))
    adj[v - 1].append((u - 1,-a,b))

for i in range(n):
    c, d = map(int,input().split())
    adj[i].append((i,c,d))

out = [-1] * n

LARGE = 10000

done = dict()
from heapq import *
q = []
if s > LARGE:
    s = LARGE
heappush(q, (0, 0, s))

while q:
    time, loc, curr = heappop(q)
    if out[loc] == -1:
        out[loc] = time
    for nex, cost, timeD in adj[loc]:
        currNex = curr + cost
        timeNex = time + timeD
        if currNex > LARGE:
            currNex = LARGE
        if currNex < 0:
            continue
        if (nex, currNex) not in done or timeNex < done[(nex, currNex)]:
            done[(nex, currNex)] = timeNex
            heappush(q, (timeNex, nex, currNex))
print(*out[1:], sep = '\n')
    


Fixed submission:
n, m, s = map(int,input().split())
adj = [[] for i in range(n)]
for _ in range(m):
    u, v, a, b = map(int,input().split())
    adj[u - 1].append((v - 1,-a,b))
    adj[v - 1].append((u - 1,-a,b))

for i in range(n):
    c, d = map(int,input().split())
    adj[i].append((i,c,d))

out = [-1] * n
count = 0

LARGE = 50 * n

done = dict()
from heapq import *
q = []
if s > LARGE:
    s = LARGE
heappush(q, (0, 0, s))

while q:
    time, loc, curr = heappop(q)
    if out[loc] == -1:
        out[loc] = time
        count += 1
        if count == n:
            break
    for nex, cost, timeD in adj[loc]:
        currNex = curr + cost
        timeNex = time + timeD
        if currNex > LARGE:
            currNex = LARGE
        if currNex < 0:
            continue
        if (nex, currNex) not in done or timeNex < done[(nex, currNex)]:
            done[(nex, currNex)] = timeNex
            heappush(q, (timeNex, nex, currNex))
print(*out[1:], sep = '\n')
Buggy python submission (verdict: Runtime Error):
print(6.28318530717958623200*int(input())

Fixed submission:
print(6.28318530717958623200*int(input()))
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""
dp、無理な組み合わせを引く、追加された黒マスの数は決まっている
各マスのdp
下から来たものと左から来たものをdpで保持
下から来たものは次だけ制約あり
dp = [下から、左から]
"""
MOD = 998244353
A, B, C, D = lr()  # Cは縦
dp = [[[0, 0] for _ in range(D+1)] for _ in range(C+1)]   # 1-indexed
dp[A][B] = [0, 1]
for i in range(A, C+1):
    for j in range(B, D+1):
        # iが縦
        if i == A and j == B:
            continue
        if i > A:
            dp[i][j][0] = sum(dp[i-1][j]) * j
        if j > B:
            dp[i][j][1] = dp[i][j-1][0] + dp[i][j-1][1] * i
        dp[i][j][0] %= MOD
        dp[i][j][1] %= MOD

answer = sum(dp[C][D])
print(answer%MOD)



Fixed submission:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""
各マスのdp
下から来たものと左から来たものを足す
その後、重なっているところを引く
"""
MOD = 998244353
A, B, C, D = lr()  # Cは縦
dp = [[0] * (D+1) for _ in range(C+1)]   # 1-indexed
dp[A][B] = 1
for i in range(A, C+1):
    for j in range(B, D+1):
        # iが縦
        if i > A:
            dp[i][j] += dp[i-1][j] * j
        if j > B:
            dp[i][j] += dp[i][j-1] * i
        if i > A and j > B:
            dp[i][j] -= dp[i-1][j-1] * (i-1) * (j-1)
        dp[i][j] %= MOD

answer = dp[C][D]
print(answer%MOD)
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 998244353
N, S = lr()
A = lr()
dp = np.zeros((N+1, S+1), np.int32)  # dp[i][j]はi個選んだ合計jの個数
dp[0][0] = 1
for a in A:
    for i in range(N-1, -1, -1):
        dp[i+1][a:] += dp[i][:-a]

dpS = dp[:, S]
answer = 0
power = 1
for x in dpS[1:][::-1]:
    answer += power * x
    answer %= MOD
    power *= 2; power %= MOD
print(answer%MOD)


Fixed submission:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 998244353
N, S = lr()
A = lr()
dp = np.zeros(S+1, np.int64)
dp[0] = 1
for a in A:
    prev = dp.copy()
    dp[a:] += dp[:-a]
    dp += prev
    dp %= MOD

answer = dp[S]
print(answer%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 998244353
N, S = lr()
A = lr()
dp = np.zeros((N+1, S+1), np.int32)
dp[0][0] = 1
#dp = np.array([[0] * (S+1) for _ in range(N+1)])  # dp[i][j]はi個選んだ合計jの個数
for a in A:
    for i in range(N-1, -1, -1):
        dp[i+1][a:] += dp[i][:-a]

dpS = dp[:, S]
answer = 0
for i, x in enumerate(dpS):
    answer += pow(2, (N-i), MOD) * x
    answer %= MOD
print(answer)


Fixed submission:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 998244353
N, S = lr()
A = lr()
dp = np.zeros(S+1, np.int64)
dp[0] = 1
for a in A:
    prev = dp.copy()
    dp[a:] += dp[:-a]
    dp += prev
    dp %= MOD

answer = dp[S]
print(answer%MOD)
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr())
answer = 0
for i, a in enumerate(A):
    B = np.arange(1, N-i) - a
    add = (A[i+1:] == B).sum()
    answer += add

print(answer)


Fixed submission:
# coding: utf-8
import sys
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
dic = defaultdict(int)
for i, a in enumerate(A):
    x = a - i
    if x in dic:
        dic[x] += 1
    else:
        dic[x] = 1

answer = 0
for i, a in enumerate(A):
    y = a + i
    answer += dic[-y]
    x = a - i
    dic[x] -= 1

print(answer)
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  float r, ans;
  cin >> r;
  ans = 2 * r * M_PI;
  cout << ans << '\n';
  return 0;
}

Fixed submission:
# coding: utf-8
import sys
import math

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

R = ir()
answer = 2 * R * math.pi
print(answer)
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr(), dtype=np.int32)
counter = np.bincount(A)
case = (counter * (counter-1) // 2).sum()
answer = case - (counter[A] - 1)
print('\n'.join(map(str, answer)))


Fixed submission:
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
counter = Counter(A)
total = sum(x*(x-1)//2 for x in counter.values())
for a in A:
    x = counter[a]
    temp = total
    temp -= x * (x-1) // 2
    if x > 2:
        temp += (x-1) * (x-2) // 2
    print(temp)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
from math import factorial
from collections import Counter, defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def comb(n, r):  # 組み合わせ
    if n <= 0 or r > n: return 0
    return factorial(n) // factorial(n - r) // factorial(r)

N = ir()
A = lr()
counter = Counter(A)
case = 0
dic = defaultdict(int)
for k, v in counter.items():
    dic[k] = v
    case += comb(v, 2)

for a in A:
    num = dic[a]
    answer = case - comb(num, 2) + comb(num-1, 2)
    print(answer)


Fixed submission:
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
counter = Counter(A)
total = sum(x*(x-1)//2 for x in counter.values())
for a in A:
    x = counter[a]
    temp = total
    temp -= x * (x-1) // 2
    if x > 2:
        temp += (x-1) * (x-2) // 2
    print(temp)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, P = lr()
S = sr()
# 累積和
remain = [0] * N
cur = 0
for i in range(N):
    num = 10 * cur + int(S[i])
    cur = num % P
    remain[i] = cur

counter = Counter(remain)
answer = 0
v = counter[0]
if v >= 1:
    answer += v * (v+1) // 2

for i in range(N):
    if remain[i] == 0:
            continue
    for j in range(i+1, N):        
        diff = j - i
        if (remain[j] - remain[i] * (10**diff)) % P == 0:
            answer += 1

print(answer)


Fixed submission:
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, P = lr()
S = sr()
# 累積和
remain = [0] * N
cur = 0
answer = 0
S_rev = S[::-1]
power = 1
for i in range(N):
    if i >= 1:
        power *= 10
        power %= P
    num = cur + power * int(S_rev[i])
    cur = num % P
    remain[i] = cur

if P == 2 or P == 5:
    ind = (i for i, x in enumerate(S, 1) if int(x) % P == 0)
    answer = sum(ind)

else:
    counter = Counter(remain)
    answer += counter[0]
    for k, v in counter.items():
        answer += v * (v-1) // 2

print(answer)