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: Runtime Error

Solution code:
import math

def countComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
NN = 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, NN + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

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

memo = [0 for _ in range(N)]
for a in A:
  memo[a-1] += 1

memo1 = [0 for _ in range(N)]
for n in range(N):
  memo1[n] = countComb(memo[n], 2, p)

total = sum(memo1)
for k in range(N):
  a = A[k]-1
  answer = 0
  answer += total
  answer -= memo1[a]
  answer += countComb(memo[a] - 1, 2, p)
  print(answer)

Submission status: Runtime Error

Solution code:
N = int(input())
An = [int(x) for x in input().split()]

L = [0,0]
for i in range(N):
    if i > 1:
        L.append(int(math.factorial(i) / (math.factorial(i-2) * 2)))

for k in range(N):
    Bn = [An[i] for i in range(N) if not i == k]
    Cn = [Bn.count(i) for i in range(N)]
    Dn = [Cn.count(i)*L[i] for i in range(N)] 
    
    print(sum(Dn))

Submission status: Runtime Error

Solution code:
import copy
n=int(input())
a=list(map(int,input().split()))
a2=copy.copy(a)
for i in a2:
    a.append(i)
    ad=a[i+1:n+i]
    ans = 0                                                                            adset=list(set(ad))
    for j in adset:
        k = ad.count(j)                                                                    if(k >= 2):                                                                           ans += (k*(k-1))//2                                                             else:
            continue
    print(ans)

Submission status: Runtime Error

Solution code:
from math import factorial

def comb(n,m):
    if n < m:
        return 0
    else:
        return int(factorial(n)/factorial(m)/factorial(n-m))

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

origin = list(set(AS))
num = []
for i in origin:
    num.append(AS.count(i))

all_num = 0
for i in num:
    all_num += comb(i,2)
    
for i in range(n):
    j = num[origin.index(AS[i])]
    k = j-1
    ans = all_num - comb(j,2) + comb(k,2)
    print(ans)

Submission status: Runtime Error

Solution code:
import math
from collections import defaultdict
def per_count(n, r):
    result = 0
    if n >= r:
        result = (math.factorial(n) // math.factorial(n - r)) // 2
    return result
N = int(input())
A = [int(a) for a in input().split()]
d = defaultdict(int)
z = dict()
max = 0
for a in A:
    d[a] += 1
    if max < d[a]:
        max = d[a]
for dv in d.values():
    z[dv-1] = per_count(dv-1, 2)
z[max] = per_count(max, 2)
for a in A:
    d[a] -= 1
    s = set(d.values())
    result = 0
    for k in s:
        result += z[d[k]]
    print(result)
    d[a] += 1

Submission status: Runtime Error

Solution code:
import math
import collections
n=int(input())
a=list(map(int,input().split()))
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i in a:
    c=collections.Counter(a)
    ans=0
    c[i] -=1
    for k in c.values():
        if k != 1:
            ans +=combinations_count(k,2)
    print(ans)

Submission status: Runtime Error

Solution code:
from math import factorial
from scipy.special import comb

def each_check(ls):
  nums = list(set(ls))
  total = 0
  for i in nums:
    count = ls.count(i)
    total += comb(count, 2, exact=True)
  return int(total)

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

for i in L:
  target = L.pop(0)
  print(each_check(L))
  L.append(target)

Submission status: Runtime Error

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
import numpy as np
def resolve():
    N = ir()
    # A = np.array(lr(), np.int32)
    A = lr()
    A = [[A[i], i] for i in range(N)]
    A.append([2*10**5+1, N+1])
    A = sorted(A, key=lambda x: x[0])
    c = []
    d = []
    e = []
    tmp = 1
    st = 0
    for i in range(N+1):
        if tmp == A[i][0]:
            st += 1
        else:
            if st > 1:
                c.append(tmp)
                d.append(st)
                e.append(st*(st-1)//2)
            else:
                c.append(tmp)
                d.append(st)
                e.append(0)
            tmp = A[i][0]
            st = 1
    ans = []
    tmp = 0
    # print(c, d, e)
    for i in range(N):
        if A[i][0] > c[tmp]:
            tmp += 1
        num = d[tmp]
        plu = 0
        if num > 2:
            plu = (num-1)*(num-2)//2
        ans.append([sum(e)-e[tmp]+plu, A[i][1]])
    # print(ans)
    # print(iA)
    ans = sorted(ans, key=lambda x: x[1])
    for i in range(N):
        print(ans[i][0])

resolve()

Submission status: Runtime Error

Solution code:
n = int(input())
List=[int(l) for l in input().split()]
a = [0]*20001
for j in range(n):
    a[List[j]] += 1
c = 0
for i in range(20001):
    c = c+(a[i]*(a[i]-1))//2
for k in range(n):
    print(c-a[List[k]]+1)

Submission status: Runtime Error

Solution code:
import collections
from math import factorial

n = int(input())
s = list(map(str, input().split()))
ss = ''.join(s)
for i in range(n):
  sss = ss[:i] + ss[i+1:]
  c = collections.Counter(sss)
  #print(c)
  count=0
  for k,v in c.items():
    if v >= 2:
      vv = int(factorial(v) / factorial(2) / factorial(v-2))
      count += vv
  print(count)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n = int(input())
s = list(map(int , input().split()))
v = defaultdict(int)
sumi = 0
for i in s:
    v[i]+=1
for i in v:
    if v[i]>=2:
        sumi+=v[i]*(v[i]-1)/2
for i in s:
    if v[i]>=2:

        if v[i]==2:
            print(int(sumi-1))
        else:
            print(int(sumi-v[i]*(v[i]-1)/2+(v[i]-2)*(v[i]-1)/2))
    else:
        print(int(sum))

Submission status: Runtime Error

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

for i in range(N):
    nums = {}
    B = A[:i] + A[i + 1:]
    for j in B:
        nums[j] += 1
    count = 0
    for j in nums:
        count += nums[j] * (nums[j] - 1) // 2
    print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
maxim = max(A)
select_num = [0 for i in range(0,maxim+1)]
select_num2 = [0 for i in range(0,maxim+1)]
count_num = [0 for i in range(0,maxim+1)]
for i in range(1,maxim+1):
  count_num[i] = A.count(i)
  if(count_num[i] > 1):
    select_num[i] = (count_num[i] - 0)*(count_num[i] - 1)//2
  	select_num2[i] = count_num[i] - 1
sumS = sum(select_num)
for i in range(N):
  print(sumS -select_num2[A[i]])

Submission status: Runtime Error

Solution code:
from math import factorial
N = int(input())
A = [int(i) for i in input().split()]

balls_num = [0 for i in range(N)]
for num in A:
    balls_num[num-1] += 1


for num in A:
    count = 0
    balls_num[num-1] -= 1
    for n in balls_num:
        if n<=1:
            continue
        else:
            count+=int((factorial(n) / factorial(2) / factorial(n-2)))
    balls_num[num-1] += 1
    print(count)

Submission status: Runtime Error

Solution code:
from collections import Counter

def fn(n):
    if n <= 1:
        return 0
    return n - 1 + fn(n-1)

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

c = Counter(A)
kumi = 0
for k, v in c.items():
    kumi += fn(v)

for ai in A:
    print(kumi - (c[ai] - 1))

Submission status: Runtime Error

Solution code:
number=[]
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
  number.append(0)
for i in A:
    if i in range(N):
      number[i]+=1
    else:
      number[i]=1
for i in range(N):
  cnt=0
  kie=A.pop(i)
  number[kie]-=1
  for j in range(N-1):
    if number[j]>=1:
      cnt+=int((number[j]*(number[j]-1))/2)
  print(str(cnt))
  A.insert(i,kie)
  number[kie]+=1

Submission status: Runtime Error

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

b = [0]*n
d = [0]*n

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        l = cmb(k,2)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    if d[j-1] == 0:
        continue
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)

Submission status: Runtime Error

Solution code:
from scipy.special import comb

N = int(input())
temp = input().split()
A = {}
for i in range(0, N):
    A[i] = temp[i]

B = {}
for i in range(0, N):
    if not(A[i] in B.keys()):
        B[A[i]] = 1
    else:
        B[A[i]] += 1 

for i in range(0, N):
    B[A[i]] -= 1
    S = 0    
    for value2 in B.values():
        S = S + comb(int(value2), 2, exact=True)
    print(S)
    B[A[i]] += 1

Submission status: Runtime Error

Solution code:
n = int(input())
list = list(map(int,input().split()))
di = {}
for i in range(n):
	k = list[i]
	if k not in di:
		di[k]=1
	else:
		di[k]+=1
list_del=[]
for key in di.keys():
	if di[key]==1:
		list_del.append(key)
for i in range(len(list_del)):
	di.pop(list_del[i])
	
	
su=0
for valu in di.values():
	su += valu*(valu-1)/2
	
for i in range(n):
	k = list[i]
	if k in si:
		l = di[k]
		su_ch = su
		su_ch -= l*(l-1)/2
		su_ch += (l-1)*(l-2)/2
		print(int(su_ch))
	else:
		print(su)

Submission status: Runtime Error

Solution code:
import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

# kごとに、
# 異なる数がいくつあるかを調べる
# (2以上要素が存在する)異なる数ごとに、
# (要素数)C2を解く。

np_A = np.array(A)

# 辞書作成
dic = {}

for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)
    # print("tmp_k", tmp_k)   # k以上
    # print(np.unique(np_A))

    # 同じ組み合わせのケースを排除する。
    # (num, 要素数)の組み合わせを辞書かで作る
    # →単純にnCrなら(n, r)で保存する

    k_unique = np.unique(tmp_k)
    ans = 0

    for u in k_unique:
        count_u = np.count_nonzero(tmp_k==u)
        if count_u < 2:
            continue
        if f"{count_u}"+"2" in dic.keys():
            ans += dic[f"{count_u}"+"2"]
            continue

        # (count_u) C 2
        tmp = comb(count_u,2)
        ans += tmp
        # 値の更新
        dic[f"{count_u}"+"2"] = tmp
    
    print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ua=list(set(a))
d={}
total=0
for i in range(n):
    y=a[i]
    if y in d:
        print(d[y])
        continue
    if ua:
        for x in ua:
            c = a.count(x)
            if c==1:
                ua.remove(c)
            total+=sum(range(c - (1 if x==y else 0)))
        print(total)
        d[y]=total
        total=0
    else:
        print(0)

Submission status: Runtime Error

Solution code:
N = int(input())

B = [0] * N
A = input().split(' ')
for i in range(N):
    B[int(A[i])] += 1

C = []
ans = 0
for i in range(N):
    if(B[i] > 1):
        C.append(B[i]*(B[i]-1)/2)
    else:
        C.append(0)

    ans += C[i]

for i in range(N):
    if(B[int(A[i])] < 2):
        print(int(ans))
    else:
        tmp = ans - C[int(A[i])] + (B[int(A[i])]-1)*(B[int(A[i])]-2)/2
        print(int(tmp))

Submission status: Runtime Error

Solution code:


def main():
    import math
    import collections
    import numpy as np

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

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

    As = np.tile(A, (N, 1))

    for i, A in enumerate(As):
        A[i] = 0
        tmp_A = collections.Counter(A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                su += combinations_count(pairs, 2)
            else:
                break
        print(su)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
l = collections.Counter(a)
print(l)

for 

values, counts = zip(*l.most_common())
num = len(counts)
p = 0
for i in range(num):
  p += int(counts[i]*(counts[i]-1)/2)
print(p)

Submission status: Runtime Error

Solution code:
def check_block(comb, K):
    block = 0
    pre = (comb & 1) > 0
    mapping = {0: 0}
    cut_num = 0
    for i in range(1, K):
        cur = ((1 << i) & comb) > 0
        if pre != cur:
            block += 1
            pre = cur
            cut_num += 1
        mapping[i] = block
    return mapping, cut_num

H, W, K = map(int, input().split())
graph = [input() for ele in range(H)]
#
ans = 1319203291031903
for comb in range(1<<(H-1)):
    mapping, cut_num = check_block(comb, K)
    pre = [0] * (cut_num + 1)
    gg = False
    # preprocess 
    for j in range(H):
        pre[mapping[j]] += (graph[j][0] == '1')
    for ele in pre:
        if ele > K:
            gg = True
            break
    if gg: continue

    for i in range(1, W):
        cum = pre[:]
        for j in range(H):
            cum[mapping[j]] += (graph[j][i] == '1')
        for ii in range(len(cum)):
            if cum[ii] - pre[ii] > K:
                gg = True
                break
            if cum[ii] > K:
                cut_num += 1
                for idx in range(len(cum)):
                    cum[idx] -= pre[idx]
                break
        pre = cum
        if gg: break

    if not gg:
        ans = min(ans, cut_num)
print(ans)

Submission status: Runtime Error

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

b = [0]*n
d = [0]*n

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

def re(n):
  if n < 2:
    return 1
  return n*re(n-1)

def c(n):
  return re(n)//re(n-2)//2

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        l = c(k)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    if d[j-1] == 0:
        continue
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)

Submission status: Runtime Error

Solution code:
from math import factorial
n = int(input())
a = [int(x) for x in input().split()]
nmap = {}


def ncr(n, r):
    if n - r >= 0:
        return factorial(n) // factorial(r) // factorial(n - r)
    else:
        return 0


at = sorted(list(set(a)))
for i in at:
    al = [x for x in a if x == i]
    nmap[f'{i}'] = ncr(len(al) - 1, 2)
    nmap[f'{i}a'] = ncr(len(al), 2)

for i in a:
    result = 0
    for j in at:
        if j == i:
            result += nmap[f'{j}']
        else:
            result += nmap[f'{j}a']
    print(result)

Submission status: Runtime Error

Solution code:
import math


def main():
    import collections
    import numpy as np

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

    As = np.tile(A, (N, 1))

    for i, A in enumerate(As):
        A[i] = 0
        tmp_A = collections.Counter(A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                su += combinations_count(pairs, 2)
            else:
                break
        print(su)


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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter

cmb = [0, 0, 1]
for i in range(3, 2 * 10 ** 5 + 1):
    cmb.append(int(cmb[i-1]*(i/(i-2))))

N = int(input())
A = list(map(int, input().split()))
c = Counter()
for i in A:
    c.update(str(i))
cmbL = {}
gsum = 0
for i in c.keys():
    tmp1 = cmb[int(c[i])]
    tmp2 = tmp1 - (tmp1 * (1 - 2/c[i]))
    cmbL[i] = [tmp1, tmp2]
    gsum += tmp1

for i in A:
    print(int(gsum - cmbL[str(i)][1]))

Submission status: Runtime Error

Solution code:
import collections
from scipy.special import comb

n = int(input())
a_list = list(map(int, input().split()))
a_col = collections.Counter(a_list)
c_dic = {}
sm = sum([c * (c - 1) for c in a_col.values()]) // 2

for a in a_list:
    if a in c_dic:
        print(c_dic[a])
    else:
        ans = sm - comb(a_col[a], 2) + comb(a_col[a] - 1, 2)
        print(ans)
        c_dic[a] = ans

Submission status: Runtime Error

Solution code:
import collections
import sys

def LI():
	return [ int(s) for s in input().split() ]

N = int(input())
A = LI()

c_counter = collections.Counter(A)
c_dict = dict(c_counter)

total = 0
total_dict = {}

for k,v in c_dict.items():
  if v >= 2:
    comb = v*(v-1)//2
    total_dict[k] = comb
    total += comb

for a in A:
  if total == 0:
    print(0)
  else:
    print(total - total_dict[a] + (c_dict[a]-1)*(c_dict[a]-2)//2)

Submission status: Runtime Error

Solution code:
from collections import Counter

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


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

S = Counter(A)

for i in range(l):
  ans = 0
  S[A[i]] -= 1
  for j in S.keys():
    ans += cmb(S[j],2,mod)
  S[A[i]] += 1
  print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
list = list(map(int,input().split()))
di = {}
for i in range(n):
	k = list[i]
	if k not in di:
		di[k]=1
	else:
		di[k]+=1
list_del=[]
for key in di.keys():
	if di[key]==1:
		list_del.append(key)
for i in range(len(list_del)):
	di.pop(list_del[i])
	
	
su=0
for valu in di.values():
	su += valu*(valu-1)/2
	
for i in range(n):
	k = list[i]
	l = di[k]
	su_ch = su
	su_ch -= l*(l-1)/2
	su_ch += (l-1)*(l-2)/2
	print(int(su_ch))

Submission status: Runtime Error

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

b = [0]*n
d = [0]*n

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

def re(n):
  if n < 2:
    return 1
  return n*re(n-1)

def c(n,r):
  return re(n)//re(n-r)//2

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        if k == 1:
            continue
        l = c(k,2)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    if d[j-1] == 0:
        continue
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)

Submission status: Runtime Error

Solution code:
import collections
from math import factorial
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N):
    x = A[i]
    del A[i]
    aa = collections.Counter(A)
    A.insert(i,x)
    for v in aa.values():
        if v >= 2:
            num = factorial(v) / factorial(2) / factorial(v - 2)
            ans += num
    print(int(ans))
    ans = 0

Submission status: Runtime Error

Solution code:
N = int(input())
import numpy as np

A = list(map(int, input().split()))
dp = np.zeros((N,N), dtype=np.int32)
for i in range(N):
  tmp = [0]*(A[i]-1)+[1]+[0]*(N-A[i])
  tmp = np.array(tmp, dtype=np.int32)
  dp[i] = tmp+dp[i-1]

dp = np.array(dp)
ans = []
for i in range(N):
  if i > 0 and i < N-1:
    tmp = dp[N-1] - dp[i] + dp[i-1]
    tmp = tmp*(tmp-1)//2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum(dtype=np.int32))
  if i == 0:
    tmp = dp[N-1] - dp[i]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum(dtype=np.int32))
  if i == N-1:
    tmp = dp[N-2]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum(dtype=np.int32))
for i in ans:
  print(i)

Submission status: Runtime Error

Solution code:
import collections
import sys

def LI():
	return [ int(s) for s in input().split() ]

N = int(input())
A = LI()

c_counter = collections.Counter(A)
c_dict = dict(c_counter)


total = 0
total_dict = {}

for k,v in c_dict.items():
  if v >= 2:
    comb = v*(v-1)//2
    total_dict[k] = comb
    total += comb
    
if total == 0:
  print(0)
  sys.exit()

for a in A:
  print(total - total_dict[a] + (c_dict[a]-1)*(c_dict[a]-2)//2)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

N = int(input())
S = [int(_) for _ in input().split()]

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

def solve(N, S):
    
    if len(set(S)) == N:
        for i in range(N):
            print(0)
        return 
    elif len(set(S)) == 1:
        for i in range(N):
            print(N+1)
        return 
    
    else:
        ty = list(set(S))
        counts = []
        c_counts = []
        
        for i in ty:
            # 個数を数える
            n = S.count(i)
            counts.append(n)

            # 組み合わせを探す
            c_counts.append(combinations_count(n, 2))
        sums = sum(c_counts)

        for i in range(N):
            ind = ty.index(S[i])
            if counts[ind]-1 >= 2:
                cal = sums - c_counts[ind] + combinations_count(counts[ind]-1, 2)
            else:
                cal = sums - c_counts[ind]
            print(cal)
solve(N, S)

Submission status: Runtime Error

Solution code:
 = int(input())
import numpy as np

A = list(map(int, input().split()))
dp = np.zeros((N,N), dtype=np.int32)
for i in range(N):
  tmp = [0]*(A[i]-1)+[1]+[0]*(N-A[i])
  tmp = np.array(tmp, dtype=np.int32)
  dp[i] = tmp+dp[i-1]

dp = np.array(dp)
ans = []
for i in range(N):
  if i > 0 and i < N-1:
    tmp = dp[N-1] - dp[i] + dp[i-1]
    tmp = tmp*(tmp-1)//2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum(dtype=np.int32))
  if i == 0:
    tmp = dp[N-1] - dp[i]
    tmp = tmp*(tmp-1)//2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum(dtype=np.int32))
  if i == N-1:
    tmp = dp[N-2]
    tmp = tmp*(tmp-1)//2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum(dtype=np.int32))
for i in ans:
  print(i)

Submission status: Runtime Error

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

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

for i in range(N):
    ans = 0
    l = A.copy()
    l.pop(i) 
    dupl = [x for x in set(l) if l.count(x) > 1]
    for j in dupl:
        ans = ans + combinations_count(l.count(j),2)
    print(ans)

Submission status: Runtime Error

Solution code:
def conbination_2(n):
  if n <= 1:
    return 0
  else:
    return int(n * (n - 1) / 2)


N = int(input())
A = list(map(int,input().split()))
S = 0
Count = [0] * (N - 1)

for i in range(N):
  Count[A[i] - 1] += 1
  
for i in range(N - 1):
  S += conbination_2(Count[i])
  
for i in range(N):
  Ans = S - (Count[A[i] - 1] - 1)
  print(Ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
def ans(a,x):
  b=0
  c=a[:x]+a[x+1:]
  d=list(set(c))
  for i in range(len(d)):
    if c.count(d[i])>1:
      b=b+(c.count(d[i])*(c.count(d[i])-1))/2
      return b
for k in range(n):
  print(int(ans(a,k)))

Submission status: Runtime Error

Solution code:

import math


def main():
    import collections
    import numpy as np

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

    As = np.tile(A, (N, 1))

    for i, A in enumerate(As):
        # tmp_A = ignore(A, k)
        # tmp_A = [a in A: if ]
        A[i] = 0

        # tmp_A = collections.Counter(tmp_A)
        tmp_A = collections.Counter(A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                su += combinations_count(pairs, 2)
            else:
                break
        print(su)


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




if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from copy import deepcopy
dp = {}

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

balls = {}
fact = 1
for (i, a) in enumerate(A):
  count = i+1
  dp[count] = count * (count-1) // 2
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1

res = 0
for count in balls.values():
  res += dp[count]

for a in A:
  print(res - dp[balls[a]] + dp[balls[a]-1])

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
import math
import collections
def nCr(n,r):
    return math.factorial(n) / math.factorial(r) / math.factorial(n-r) if n>1 else 0
counter=collections.Counter(A)
penult=0
for i in range(1,N+1):
    penult=penult+nCr(counter[i],2)
for i in range(0,N):
    print(int(penult-(counter[A[i]]-1)))

Submission status: Runtime Error

Solution code:
import sys
# sys.stdin = open('input.txt')

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 = [i for i in map(int, input().split())]

cnt = {}
for i in a:
    if i not in cnt.keys():
        cnt[i] = 1
    else:
        cnt[i] += 1

total = 0

for i in cnt.values():
    if i >= 2:
        total += cmb(i, 2)

for i in range(n):
    ans = total
    x = cnt[a[i]]
    if x >= 2:
        ans = total - cmb(x, 2)
    if x-1 >= 2:
        ans += cmb(x-1, 2)
    print(ans)

Submission status: Runtime Error

Solution code:
import collections

def LI():
	return [ int(s) for s in input().split() ]

N = int(input())
A = LI()

c_counter = collections.Counter(A)
c_dict = dict(c_counter)


total = 0
total_dict = {}

for k,v in c_dict.items():
  if v >= 2:
    comb = v*(v-1)//2
    total_dict[k] = comb
    total += comb

for a in A:
  print(total - total_dict[a] + (c_dict[a]-1)*(c_dict[a]-2)//2)

Submission status: Runtime Error

Solution code:
import math
import collections

def combinations_count(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
def main():
  N = int(input())
  l = map(int, input().split())
  temp = l
  for i in range(N):
    l.pop(i)
    c = collections.Counter(l)
  	for v in c.values():
      if v >= 2:
        print(combinations_count(v, 2))
      else:
        print(0)
    l = temp
if __name__ == '__main__'
  main()

Submission status: Runtime Error

Solution code:
import sys
import collections
from math import factorial
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()

C = collections.Counter(A)

for x in A:
    ans = 0
    tmp = C[x] - 1
    if tmp >= 2:
        ans += factorial(tmp) / factorial(2) / factorial(tmp - 2)
    for c in C.keys():
        if x != c:
            v = C[c]
            if v >= 2:
                ans += factorial(v) / factorial(2) / factorial(v - 2)
    print(int(ans))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_sort = sorted(a)

    ball_dict = {a[0]:1}
    for i in range(1, n):
        if a_sort[i] == a_sort[i - 1]:
            ball_dict[a_sort[i]] += 1
        else:
            ball_dict.setdefault(a_sort[i], 1)

    for i in range(n):
        new_dict = ball_dict.copy()
        new_dict[a[i]] -= 1
        ans = 0
        for v in new_dict.values():
            ans += v * (v - 1) // 2
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
from collections import defaultdict
def per_count(n, r):
    result = 0
    if n >= r:
        result = (math.factorial(n) // math.factorial(n - r)) // 2
    return result
N = int(input())
A = [int(a) for a in input().split()]
d = defaultdict(int)
z = dict()
max = 0
for a in A:
    d[a] += 1
    if max < d[a]:
        max = d[a]
for dv in d.values():
    z[dv-1] = per_count(dv-1, 2)
z[max] = per_count(max, 2)
for a in A:
    d[a] -= 1
    result = 0
    for k in d.keys():
        result += z[d[k]]
    print(result)
    d[a] += 1

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from math import factorial
n = int(input())
a_list = list(map(int, input().split()))
a_dict = {}
for a in a_list:
    if a in a_dict:
        a_dict[a] += 1
    else:
        a_dict[a] = 1
a_cal_dict = {}
cal_dict = {}
for a in a_list:
    if a in a_cal_dict:
        print(a_cal_dict[a])
    else:
        ans = 0
        for key in a_dict:
            n = 0
            if key == a:
                n = a_dict[key] - 1
            else:
                n = a_dict[key]
            if n < 2:
                continue
            if n in cal_dict:
                ans += cal_dict[n]
            else:
                cal_dict[n] = int(factorial(n) / factorial(2) / factorial(n - 2))
                ans += cal_dict[n]
        print(ans)
        a_cal_dict[a] = ans