Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

Solution code:
N = int(input())
As = [int(s) - 1 for s in input().split()]

Cs = [0 for _ in range(N)]
for A in As:
    Cs[A] += 1

a = [C * (C - 1) // 2 for C in Cs]
b = [(C - 1) * (C - 2) // 2 for C in Cs]

S = sum(a)

for A in As:
    print(S - a[A] + b[A])

Submission status: Accepted

Solution code:
from collections import  Counter
N = int(input())
A = list(map(int,input().split()))

cnt = Counter(A)

ans = 0
for k,v in cnt.items():
    ans += v*(v-1)//2


for a in A:
    n = cnt[a]
    print(ans - n*(n-1)//2 + (n-1)*(n-2)//2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    A=list(map(int,input().split()))
    d=defaultdict(int)
    s=set()
    for x in A:
        d[x]+=1
        s.add(x)
    W=0
    for x in s:
        W+=d[x]*(d[x]-1)//2
    for x in A:
        a=d[x]
        print(W-a*(a-1)//2+(a-1)*(a-2)//2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict, deque

N, = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]


def choose2(n):
    return (n * (n - 1)) // 2


counts = Counter(A)
total = 0
for x, count in counts.items():
    total += choose2(count)

for x in A:
    old = choose2(counts[x])
    new = choose2(counts[x] - 1)

    print(total - old + new)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
for i in a:
    d[i] += 1
ans = 0
for v in d.values():
    ans += v * (v - 1) // 2
for i in a:
    v = d[i]
    print(ans - v * (v - 1) // 2 + (v - 1) * (v - 2) // 2)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

def combination(n, k):
    numer = denom = 1
    for i in range(k):
        numer *= n-i
        denom *= i+1
    return numer // denom

N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
num1 = {}
num2 = {}
total = 0
for k, v in counter.items():
    total += combination(v, 2)
    num1[k] = combination(v, 2)
    num2[k] = combination(v-1, 2)
for a in A:
    print(total - num1[a] + num2[a])

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = [int(c) for c in input().split()]
dic = defaultdict(list)
for i,a in enumerate(A):
  dic[a].append(i)
s = 0
for k in dic.keys():
  n = len(dic[k])
  s += n*(n-1)//2
for i,a in enumerate(A):
  n = len(dic[a])
  print(s-n+1)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    aa=LI()
    ac=Counter(aa)
    s=sum(c*(c-1)//2 for a,c in ac.items())
    for a in aa:
        c=ac[a]
        print(s-c*(c-1)//2+(c-1)*(c-2)//2)

main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
memo = {}
for i in range(n):
    if a[i] not in memo:
        memo[a[i]] = 1
    else:
        memo[a[i]] += 1
ans = 0
for i in memo:
    ans += memo[i] * (memo[i] - 1) // 2
    
for i in range(n):
    print(ans - memo[a[i]] * (memo[a[i]] - 1) // 2 + (memo[a[i]] - 1) * (memo[a[i]] - 2) // 2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = [0]*(n+1)
for i in range(n):
    cnt[a[i]]+=1
res = 0
for i in range(n+1):
    res+=cnt[i]*(cnt[i]-1)//2
for i in range(n):
    c = cnt[a[i]]
    print(res-c*(c-1)//2+(c-1)*(c-2)//2)

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
import collections
D=collections.defaultdict(int)
for a in A:
  D[a]+=1
tot=0
for d in D.keys():
  if D[d]>=2:
    tot+=D[d]*(D[d]-1)//2
for a in A:
  ans=tot-D[a]*(D[a]-1)//2+(D[a]-1)*(D[a]-2)//2
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
al = list(map(int,input().split()))

from collections import defaultdict
dic = defaultdict(int)

for a in al:
    dic[a] += 1

sum_d = 0
for a in dic:
    sum_d += dic[a] * (dic[a] - 1) // 2

for a in al:
    k = dic[a] * (dic[a] - 1) // 2
    l = (dic[a] - 2) * (dic[a] - 1) // 2
    print(sum_d - k + l)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(i) for i in input().split()]

cnt = Counter(A)
mem = dict()
for k, v in cnt.items() :
  mem[k] = v * (v - 1) // 2

s = sum(mem.values())
for i in range(N) :
  ret = s - mem[A[i]] + (cnt[A[i]] - 1) * (cnt[A[i]] - 2) // 2
  print(ret)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
A = LI()

d = defaultdict(int)
for i in range(N):
    d[A[i]] += 1

s = 0
for k in d.keys():
    s += d[k]*(d[k]-1)//2

for i in range(N):
    print(s-(d[A[i]]-1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

from collections import defaultdict

d = defaultdict(lambda:0)
for i in range(N):
    d[A[i]] += 1

ans = 0
for k in d:
    ans += d[k]*(d[k]-1)//2

for i in range(N):
    print(ans-d[A[i]]+1)

Submission status: Accepted

Solution code:
import os
import sys
from collections import Counter

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

counter = Counter(A)
counts = [0] * (max(A) + 1)
for k, cnt in counter.items():
    if cnt >= 2:
        counts[k] = (cnt - 1) * cnt // 2

s = sum(counts)
ans = []
for a in A:
    cnt = counter[a] - 1
    if cnt >= 2:
        a = s - counts[a] + (cnt - 1) * cnt // 2
    else:
        a = s - counts[a]
    ans.append(a)

print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(A)

s = sum(c*(c-1)//2 for c in cnt.values())

for a in A:
    c = cnt[a]
    print(s-(c-1)*c//2 + (c-2)*(c-1)//2)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)
sm = 0
for v in c.values():
    sm += v * (v - 1) // 2

for e in a:
    v = c[e]
    ans = sm - v * (v - 1) // 2 + (v - 1) * (v - 2) // 2
    print(ans)

Submission status: Accepted

Solution code:
N = input()
A = [int(x)-1 for x in raw_input().split()]

C = [0]*N
for a in A:
  C[a] += 1
s = sum(c*(c-1)/2 for c in C)
for a in A:
  print s-C[a]+1

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

N += 1
L = [0] * N 

for i in A:
  L[i] += 1

Ans = 0
for i in range(N):
  t = L[i]
  Ans += (t * (t - 1)) // 2

for i in A:
  t = L[i]
  k = (t * (t - 1)) // 2
  t -= 1
  k -= (t * (t - 1)) // 2
  print(Ans - k)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import defaultdict
d=defaultdict(int)
for i in a:d[i]+=1
ans=0
for i in d:ans+=d[i]*(d[i]-1)//2
for i in a:
  x=d[i]*(d[i]-1)//2
  y=(d[i]-1)*(d[i]-2)//2
  print(ans-x+y)

Submission status: Accepted

Solution code:

N = int(input())

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

dic = {}

asum = 0

for i in A:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

for i in dic:

    asum += dic[i] * (dic[i]-1) // 2

for i in A:

    print (asum - (dic[i] * (dic[i]-1) // 2) + ((dic[i]-1)*(dic[i]-2)//2) )

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

from collections import Counter
c = Counter(a)

tmp = []
for key in c.keys():
    tmp.append(c[key] * (c[key] - 1) // 2)

tmpsum = sum(tmp)

for k in a:
    print(tmpsum - c[k] * (c[k] - 1) // 2 + (c[k] - 1) * (c[k] - 2) // 2)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
C = Counter(A)
cnt = 0
for v in C.values():
  cnt += v*(v-1)//2
for a in A:
  q = C[a]
  ans = cnt - q*(q-1)//2 + (q-1)*(q-2)//2
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

ball = [0]*200001
for i in range(n):
    ball[a[i]] += 1

ans = 0
for i in range(1, 200001):
    ans += ball[i]*(ball[i]-1)//2

for i in range(n):
    now = a[i]
    ans -= ball[now]*(ball[now]-1)//2
    ans += (ball[now]-1)*(ball[now]-2)//2
    print(ans)
    ans += ball[now]*(ball[now]-1)//2
    ans -= (ball[now]-1)*(ball[now]-2)//2

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

from collections import Counter

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

C = Counter(A)

ans = 0
for v in C.values():
    ans += v*(v-1)//2

for a in A:
    v = C[a]
    if v == 1:
        print(ans)
    else:
        print(ans - v*(v-1)//2 + (v-1)*(v-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
base = 0
for i in c.values():
    base += i*(i-1)//2

ans = []
for ai in a:
    k = c[ai]
    tmp = base - k*(k-1)//2 + (k-1)*(k-2)//2
    ans.append(tmp)

print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import numpy as np


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    from collections import Counter

    c = Counter(A)

    num = {x: n*(n-1)//2 for x, n in c.items()}
    tot = sum(num.values())

    for x in A:
        n = c[x] - 1
        ans = tot - num[x] + n*(n-1)//2
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


n,*a = [int(i) for i in read().split()]

from collections import Counter
c = Counter(a)
res = sum(v*(v-1)//2 for k,v in c.items())

for i in a:
    v = c[i]
    ans = res - v*(v-1)//2 + (v-1)*(v-2)//2
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()

nums = defaultdict(int)
ans = 0
for v in a:
    ans += nums[v]
    nums[v] += 1

for v in a:
    print(ans - (nums[v]-1))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
ans = sum([v * (v-1)//2 for v in c.values()])
for x in a:
	print(ans - c[x] + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
data = [0 for _ in range(2*10**5+1)]
for x in a:
    data[x] += 1

ans = 0
for i in range(2*10**5):
    ans += data[i]*(data[i]-1)//2

for x in a:
    print(ans - (data[x]-1))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

b = {}
for i in a:
    if i in b:
        b[i] += 1
    else:
        b[i] = 1

c = 0
for i in b:
    c += b[i] * (b[i] - 1) // 2

for i in a:
    print(c-b[i]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a)-1 for a in input().split()]
X = [0] * N
for a in A:
    X[a] += 1
s = 0
for x in X:
    s += x * (x-1) // 2

for a in A:
    print(s - (X[a] - 1))

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    N,*A=map(int, open(0).read().split())
    C=Counter(A)
    x=0
    for v in C.values():
        x+=v*(v-1)//2
    for a in A:
        print(x-(C[a]-1))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
ans = 0
for x in c.values():
  ans += x*(x-1)//2
for i in range(n):
  t = c[a[i]]
  print(ans-t+1)

Submission status: Accepted

Solution code:
N, = map(int, input().split())
X = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)
for i in range(N):
    d[X[i]] += 1

r = 0
for k in d:
    x = d[k]
    r += x*(x-1)//2

for i in range(N):
    x = d[X[i]]
    print(r - (x*(x-1)//2) + (x-1)*(x-2)//2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    d = defaultdict(int)
    for ai in a:
        d[ai] += 1
    ans = 0
    for key, value in d.items():
        ans += value * (value - 1) // 2
    for i in range(n):
        print(ans - d[a[i]] * (d[a[i]] - 1) // 2 + (d[a[i]] - 2) * (d[a[i]] - 1) // 2)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7


def inp(): return int(sys.stdin.readline())


def inpl(): return list(map(int, sys.stdin.readline().split()))


def inpl_str(): return list(sys.stdin.readline().split())


N = inp()
aa = inpl()

cnts = [0]*(N+10)

for a in aa:
    cnts[a] += 1

S = 0
for a in aa:
    S += cnts[a]-1

S //= 2

for a in aa:
    print(S - cnts[a] + 1)

Submission status: Runtime Error

Solution code:
def resolve():
    n = int(input())
    a = tuple(map(int,input().split()))
    A = collections.Counter(a)
    cnt_0 = 0
    for i in A.values():
        cnt_0 += int(i*(i-1)/2)
    for i in a:
        cnt_1 = A[i]-1
        print(cnt_0-cnt_1)
resolve()

Submission status: Runtime Error

Solution code:
n=(int)(nput())
a=input().split()
for i in range(n):
  a[i]=(int)(a[i])
  
dic={}
sum=0
for i in range(n):
  if not a[i] in dic:
    dic[a[i]]=1
  else:
    dic[a[i]]+=1
    sum+=dic[a[i]]-1

for i in a:
  print(sum-dic[i]+1)

Submission status: Runtime Error

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

def main():
    n = i_input()
    a = i_list()

    k = [0]
    for i in range(2*10):
        k.append(k[i]+(i+1))
    t = a.copy()
    t.sort()

    s = 0

    m = [0]*n

    for i,n in itertools.groupby(t):
        l = len(list(n))
        s += k[l-1]
        if l > 1:
            m[i-1] = k[l-2] - k[l-1]

    for i in a:
        print(s + m[i-1])







if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
def get_int():
	return int(input())

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

def choose2(n):
    return n*(n-1)/2
# インプット
N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N)
# ユニーク数の配列を求める
for i in range(N):
    uni[An[i]] += 1
sumWay = 0
# 各数値の2通りの選択通り数を足していく
for i in range(N):
    sumWay += choose2(uni[i])
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print(sumWay - choose2(uni[An[i]]) + choose2(uni[An[i]]-1)) 
# 4 - 3 + 1

Submission status: Runtime Error

Solution code:
n = int(input())
a =list(map(int,input().split()))
d = [0]*(n+1)
answer = 0
for i in range(n):
  d[a[i]]+=1
for j in range(n):
  answer+= (d[j]*(d[j]-1))//2
for k in range(n)  
  print(answer+1-(d[a[k]]))

Submission status: Runtime Error

Solution code:
N = int(input())
nums = list(map(int, input().split()))
counts = [0 for _ in range(N+1)]
for num in nums:
  counts[num] += 1
combination = 0
for count in counts:
  if count >= 2
    combination += (count*(count-1))//2
for i in range(1, N+1):
  print(combination-counts[i]+1)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

ball_count=[0]*(n+1)
count_count=[0]*(n+1)

for i in a:
    ball_count[i]+=1
for i in ball_count:
    count_count[i]+=1

def get_total(count_count):
    ans=0
    for i in range(2,n+1):
        ans+=(i*(i-1)//2)*count_count[i]
    return ans
total=get_total(count_count)
for i in range(1,n+1):
    num=ball_count[a[i-1]]
    if num==2:
        ans=total-(num*(num-1)//2)
    elif num>2:
        ans = total - (num * (num - 1) // 2)+((num-1) * (num - 2) // 2)
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

from collections import Counter
cnt = Counter(A)

ans = 0
for v in cnt.values():
    ans += v*(v-1)//2

for a in A:
    k = C[a]
    print(ans - (k*(k-1)//2) + (k-1)*(k-2)//2)

Submission status: Runtime Error

Solution code:
import collections

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

c=collections.Counter(a)

print(c)

for i in c.values():
  s+=i*(i-1)//2 #ボールを抜かない場合に2つのボールを選ぶ方法の数

for j in a:
  f=c[j]
  print(s-(f-1)) #下記メモ参照

'''
複数個のボールから1個のボールが抜かれる場合、i*(i-1)//2が(i-1)*(i-2)//2となる。

i*(i-1)//2-(i-1)*(i-2)//2
=i-1

よって、2つのボールを選ぶ方法が(i-1)だけ減る。
'''

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# import time


def main():
    # from scipy.special import comb
    from math import factorial

    N = int(input())
    A = [int(x) for x in input().split()]

    num_lst = [0] * (N + 1)
    for a in A:
        num_lst[a] += 1

    ans = 0
    for num in num_lst:
        # ans += comb(num, 2, exact=True)
        if num >= 2:
            ans += int(factorial(num) / 2 / factorial(num - 2))
        else:
            ans += 0

    # start = time.time()
    for a in A:
        print(ans - (num_lst[a] - 1))
    # end = time.time() - start
    # print(f'{end:6f}', '[sec]')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import time


def main():
    # from scipy.special import comb
    from math import factorial

    N = int(input())
    A = [int(x) for x in input().split()]

    num_lst = [0] * (N + 1)
    for a in A:
        num_lst[a] += 1

    ans = 0
    for num in num_lst:
        # ans += comb(num, 2, exact=True)
        if num >= 2:
            ans += int(factorial(num) / 2 / factorial(num - 2))
        else:
            ans += 0

    start = time.time()
    for a in A:
        print(ans - (num_lst[a] - 1))
    end = time.time() - start
    # print(f'{end:6f}', '[sec]')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import time


def main():
    # from scipy.special import comb
    from math import factorial

    N = int(input())
    A = [int(x) for x in input().split()]

    num_lst = [0] * (N + 1)
    for a in A:
        num_lst[a] += 1

    ans = 0
    for num in num_lst:
        # ans += comb(num, 2, exact=True)
        if num >= 2:
            ans += factorial(num) / 2 / factorial(num - 2)
        else:
            ans += 0

    start = time.time()
    for a in A:
        print(ans - (num_lst[a] - 1))
    end = time.time() - start
    print(f'{end:6f}', '[sec]')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import time


def main():
    from scipy.special import comb

    N = int(input())
    A = [int(x) for x in input().split()]
    # N = 10 ** 4
    # A = [int(x) for x in range(N)]

    num_lst = [0] * (N + 1)
    for a in A:
        num_lst[a] += 1

    cnt_lst = []
    for num in num_lst:
        cnt_lst.append(comb(num, 2, exact=True))

    for i in range(N):
        now = A[i]
        res = num_lst[now] - 1
        print(sum(cnt_lst[:now])
              + sum(cnt_lst[now + 1:])
              + (comb(res, 2, exact=True) if res >= 2 else 0))
    # print(num_lst)
    # print(cnt_lst)


if __name__ == '__main__':
    start = time.time()
    main()
    elapsed_time = time.time() - start
    # print(f'{elapsed_time:6f}', '[sec]')

Submission status: Runtime Error

Solution code:
N=int(input())
A=[int(a) for a in input().split()]
Acnt=[A.count(n) for n in range(N)]
Aprob=[Acnt[n]*(Acnt[n]-1)//2 for n in range(N)]
sumA=sum(Aprob)  

for a in A:
  """
  ai=[]
  ai=A[:n]+A[n+1:]
  #print(ai)
  Adic[A[n]]-=1
  ans=0
  for i,v in Adic.items():
    ans+=v*(v-1)//2
  """
  ans=(Acnt[a]-1)*(Acnt[a]-2)//2
  print(ans+sumA-Aprob[a])

Submission status: Runtime Error

Solution code:

if __name__ == '__main__':

    n = int(input())
    A = list(map(int,input().split()))

    #全体の数
    d = Counter(A)
    
    d2 = dict()
    #この時の組み合わせの合計
    for k,v in d.items():
        d2[k] = (v*(v-1)) // 2
    ans_sum = sum(d2.values())

    #結果表示
    for i in A:
        ans = 0
        if i in d2:
            #あるならば
            tmp = d[i] - 1
            before= d2[i]
            after = (tmp*(tmp-1)) //2
            ans = ans_sum - (before-after)
            print(ans)
        else:
            #ないならば
            print(ans_sum)

Submission status: Runtime Error

Solution code:
from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    a_c= Counter(A)
    values = a_c.values()
    val_cnt = Counter(values
    for a in A:
        val_cnt_c = deepcopy(val_cnt)
        val_cnt_c[a_c[a]] -= 1
        val_cnt_c[a_c[a]-1] += 1
        cnt = {int(k*(k-1)/2*v) if k > 1 else 0 for k, v in val_cnt_c.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce
from collections import Counter
from copy import deepcopy
import sys
sys.setrecursionlimit(100000)

def main():
    nCr = {}
    def cmb(n, r):
        if r == 0 or r == n: return 1
        if r == 1: return n
        if (n,r) in nCr: return nCr[(n,r)]
        nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
        return nCr[(n,r)]

    n = int(input())
    A = [int(x) for x in input().split()]
    a_c= Counter(A)
    cnt = 0
    for a in A:
        a_c_c = deepcopy(a_c)
        a_c_c[a] = a_c_c[a] - 1
        values = a_c_c.values()
        val_cnt = Counter(values)
        cnt = {cmb(k, 2) * v  if k > 1 else 0 for k, v in val_cnt.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce
from collections import Counter
from copy import deepcopy

def main():
    nCr = {}
    def cmb(n, r):
        if r == 0 or r == n: return 1
        if r == 1: return n
        if (n,r) in nCr: return nCr[(n,r)]
        nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
        return nCr[(n,r)]

    n = int(input())
    A = [int(x) for x in input().split()]
    a_c= Counter(A)
    cnt = 0
    for a in A:
        a_c_c = deepcopy(a_c)
        a_c_c[a] = a_c_c[a] - 1
        values = a_c_c.values()
        val_cnt = Counter(values)
        cnt = {cmb(k, 2) * v  if k > 1 else 0 for k, v in val_cnt.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    C = Counter(A)

    X = 0
    for v in C.values():
        X += factorial(v) // (factorial(2) * factorial(v-2))

    for i in range(N):
        print(X - C[A[i]] + 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    C = Counter(A)
    print(C)

    X = 0
    for v in C.values():
        X += factorial(v) // (factorial(2) * factorial(v-2))

    for i in range(N):
        print(X - C[A[i]] + 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def combination(n,r):
    n_=1
    r_=1
    for i in range(n,n-r,-1):
        n_*=i
    for i in range(1,r+1):
        r_*=i

    return n_//(n_r*r_)

def main():
    N=int(input())
    A=list(map(int,input().split()))

    ball_dic={}

    for a in A:
        if a in ball_dic.keys():
            ball_dic[a]+=1
        else:
            ball_dic[a]=1


    all_patern=0

    combination_dic={}

    for key in ball_dic.keys():
        if ball_dic[key]>=2:
            if (ball_dic[key] in combination_dic.keys()) is False:
                combination_dic[ball_dic[key]] = combination(ball_dic[key], 2)
            all_patern += combination_dic[ball_dic[key]]

    for a in A:
        if ball_dic[a]-1<2:
            if ball_dic[a] in combination_dic.keys():
                res=all_patern-combination_dic[ball_dic[a]]
            else:
                res=all_patern
        else:
            com=combination_dic[ball_dic[a]]
            res=all_patern-com+(com*(ball_dic[a]-2)//ball_dic[a])

        print(res)

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
def combination(n,r):
    n_=1
    r_=1
    n_r=1
    for i in range(1,n+1):
        n_*=i
    for i in range(1,n-r+1):
        n_r*=i
    for i in range(1,r+1):
        r_*=i

    return n_//(n_r*r_)

def main():
    N=int(input())
    A=list(map(int,input().split()))

    ball_dic={}

    for a in A:
        if a in ball_dic.keys():
            ball_dic[a]+=1
        else:
            ball_dic[a]=1


    all_patern=0

    combination_dic={}

    for key in ball_dic.keys():
        if ball_dic[key]>=2:
            if (ball_dic[key] in combination_dic.keys()) is False:
                combination_dic[ball_dic[key]] = combination(ball_dic[key], 2)
            all_patern += combination_dic[ball_dic[key]]

    for a in A:
        if ball_dic[a]-1<2:
            res=all_patern-combination_dic[ball_dic[a]]
        else:
            com=combination_dic[ball_dic[a]]
            res=all_patern-com+(com*(ball_dic[a]-2)//ball_dic[a])

        print(res)

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split()]

def com(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(n):
    ans = 0
    b = a.copy()
    del b[i]
    bb = collections.Counter(b)
    for j in bb:
        if bb[j] > 1:
            ans += com(bb[j],2)
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = []

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

  for k in range(0,N):
    identical_balls = 0
    for i in range(0,N):
      for j in range(i, N):
        if arr[i] == arr[j] and i != k and j != k:
          identical_balls += 1
          print(identical_balls)

Submission status: Runtime Error

Solution code:
N = int(input())
arr = []

for i in range(0,N):
  arr.append(int(input()))
  
for k in range(0,N):
  identical_balls = 0
  for i in range(0,N):
    for j in range(i, N):
      if arr[i] == arr[j] and i != k and j != k:
        identical_balls += 1
  print(identical_balls)