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:
from collections import defaultdict

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

cnt = defaultdict(int)
for v in a:
  cnt[v] += 1

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

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

Submission status: Runtime Error

Solution code:
import math

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

nn = int(input())
aa = list(map(int,input().split()))

ne = [0]*200000
cn = [0]*200000
cnminus1 = [0]*nn
#print(nn,aa)
#print(len(ne))
for i in range(nn):
    ne[aa[i]] += 1

for i in range(nn):
 #   print(ne[i])
    if ne[i] == 0:
        cn[i] = 0
        cnminus1[i] = 0
    elif ne[i] == 1:
        cn[i] = 0
        cnminus1[i] = 0
    elif ne[i] == 2:
        cn[i] = 1
        cnminus1[i] = 0
    else:
        cn[i] = combinations_count(ne[i],2)
        cnminus1[i] = combinations_count(ne[i]-1,2)

sum_cn = sum(cn)

for i in range(nn):
    print(sum_cn - cn[aa[i]] + cnminus1[aa[i]])

Submission status: Runtime Error

Solution code:
import math

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

nn = int(input())
aa = list(map(int,input().split()))

ne = [0]*nn
cn = [0]*nn
cnminus1 = [0]*nn
#print(nn,aa)

for i in range(nn):
    ne[aa[i]] += 1

for i in range(nn):
 #   print(ne[i])
    if ne[i] == 0:
        cn[i] = 0
        cnminus1[i] = 0
    elif ne[i] == 1:
        cn[i] = 0
        cnminus1[i] = 0
    elif ne[i] == 2:
        cn[i] = 1
        cnminus1[i] = 0
    else:
        cn[i] = combinations_count(ne[i],2)
        cnminus1[i] = combinations_count(ne[i]-1,2)

sum_cn = sum(cn)

for i in range(nn):
    print(sum_cn - cn[aa[i]] + cnminus1[aa[i]])

Submission status: Runtime Error

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

c = 0
cnt = [0]*n

for i in range(n):
    cnt[A[i]] += 1

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

for i in range(n):
    c += combinations(cnt[i])

for i in range(n):
    ans = c
    ans -= combinations(cnt[A[i]])
    ans += combinations(cnt[A[i]]-1)
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += B[i]*(B[i]-1)/2

for i in range(N):
    if B[A[i]] < 2:
        print(S)
    else:
        print(S-B[A[i]]+1)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
S = 0

for i in range(1, N+1):
	B[i] = A.count(i)
	S += B[i]*(B[i]-1)/2

for i in range(N):
	if B[A[i]] < 2:
		print(S)
	else:
		print(S-B[A[i]]+1)

Submission status: Runtime Error

Solution code:
import sys, bisect, math, itertools, string, queue, copy
#import numpy as np
#import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
a = inpl()
kosuu = [0]*(200000+1)
cnt = [0]*(200000+1)
ans = [0]*(200000+1)

for i in range(n):
  kosuu[a[i]] += 1

s = 0
for i in kosuu:
  s += i * (i - 1) // 2

for i in range(n)
  tmp = kosuu[a[i]]
  #print(s - cnt[a[i]] + max(0,((tmp - 1)*(tmp - 2)//2)))
  print(s - max(0,tmp-1))

Submission status: Runtime Error

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

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

count = [0] * (n)
for i in range(n):
  if count[a[i]] != 0:
    continue
  
  inp[a[i]] -= 1
  for j in range(1,n):
    if inp[j] <= 0:
      continue
    count[a[i]] += inp[j]*(inp[j]-1) // 2
  inp[a[i]] += 1

for i in range(n):
  print(count[a[i]])

Submission status: Runtime Error

Solution code:
import collections

n = int(input())
arr = list(map(int,input().split()))
cnt = collectioms.Counter(arr)
total = 0

for key in cnt.keys():
  total += cnt[key] * (cnt[key] - 1)//2
  
for i in range(n):
  tmp = total
  tmp -= cnt[arr[i]]*(cnt[arr[i]] - 1)//2
  tmp += (cnt[arr[i]] - 1)*(cnt[arr[i]] - 2)//2
  print(tmp)

Submission status: Runtime Error

Solution code:
import collections

sys.stdin = io.StringIO(_INPUT)
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    acopy = a.copy()
    del acopy[i]
    c = collections.Counter(acopy)
    itm = c.values()
    ans = 0
    for j in itm:
        ans += j*(j-1)//2
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    acopy = a.copy()
    del acopy[i]
    c = collections.Counter(acopy)
    itm = c.values()
    ans = 0
    for j in itm:
        ans += j*(j-1)//2
    print(ans)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
K = list(map(int,input().split()))
hashmap = {}
for k in K:
  if k not in hashmap:
    hashmap[k] = 0
  hashmap[k]+=1
total = 0
for v in hashmap.values():
  total+=math.factorial(v) // (math.factorial(v - 2) * 2)

for k in K:
  print(total-(hashmap[k]-1))

Submission status: Runtime Error

Solution code:
from scipy.special import comb


N = int(input())
A = list(map(int, input().split()))
sorted_A = sorted(A)
countNums = [0]*(N+1)

target = sorted_A[0]
for i in range(N):
    if target==sorted_A[i]:
        countNums[target] += 1
    else:
        target = sorted_A[i]
        countNums[target] += 1;


for i in range(N):
    a = A[i]
    countNums[a] -= 1
    ans = 0
    for c in countNums:
        ans += int(comb(c, 2))
    print(ans)
    countNums[a] += 1

Submission status: Runtime Error

Solution code:
rom collections import Counter
N=int(input())
A=list(map(int, input().split()))
 
count = Counter(A)
vals = count.values()
sum_ = 0
for v in vals:
  if v >= 2: sum_ += v*(v-1)
sum_ //= 2
 
for k in range(N):
  if count[A[k]] < 2: print(sum_)
  else:print(sum_ + 1 - count[A[k]])

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))
t = {}
niko = {}
ans = {}

for a in arr:
    if a in t:
        t[a] += 1
    else:
        t[a] = 1
    if t[a] >= 2:
        niko[a] = t[a]
print(niko)
for a in arr:
    if a not in ans:
        # 2個以上あるやつから選ぶ
        x = 0
        niko[a] -= 1
        for _, v in niko.items():
            if v >= 2:
                x += v * (v - 1) // 2
        niko[a] += 1
        ans[a] = x
    print(ans[a])

Submission status: Runtime Error

Solution code:
from collections import Counter


def tri(num):
    return int(num*(num+1)/2)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    s = sum([tri(j-1) for j in c.values()])
    for i in range(n):
        cnt = count(i)
        s = s - tri(cnt) + tri(cnt-1)
        print(s)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

a_2 = [0 for i in range(n)]
for i in range(n):
    a_2[a[i]-1] += 1

al = []
for i in a_2:
    pass
#    al.append(int(i * (i-1) / 2))

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sum(al)-(al[f-1]-t*(t-1)/2)))

Submission status: Runtime Error

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

a_2 = [0 for i in range(n)]
for i in range(n):
    a_2[a[i]-1] += 1

al = []
#for i in a_2:
#    al.append(int(i * (i-1) / 2))

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sum(al)-(al[f-1]-t*(t-1)/2)))

Submission status: Runtime Error

Solution code:
import numpy as np

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

'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
unique_num, counts =np.unique(A,return_counts=True) #Aから、各要素の出現回数を取得する。

# print(unique_num)
# print(counts)
dic0={} #数字と出現回数の組合わせ辞書
dic1={} #数字と組合わせ個数の組合わせ辞書
dic2={} #回答用の辞書

Combination_pattern　= []
for i in range(len(counts)):
    Combination = counts[i] * (counts[i]-1) // 2 #出現回数から、組み合わせを計算する。
    dic0[unique_num[i]]=counts[i]
    dic1[unique_num[i]]=Combination
    dic2.setdefault(unique_num[i])
    Combination_pattern.append(Combination)

totalcombination=sum(Combination_pattern)

# print(dic0)
# print(dic1)
# print(dic2)
# print(Combination_pattern)

'''
以下方針
・Aから数字を取得
・その数字の出現回数を1減らした状態の組み合わせの数を調べる。
・その数字に対応する元の組み合わせの数を減らし、1個前で計算した組合わせを足す。
・同じ数字を引いたときように答えを辞書に登録しておく。
・もし、過去に同じ数字を引いていたら、登録しておいた答えを回答する。
'''

for j in range (N):
    n=A[j] #Aから数字を取得
    if dic2[n]==None:
        reduced_pattern = (dic0[n]-1) * (dic0[n]-2) // 2 #その数字の出現回数を1減らした状態の組み合わせの数を調べる。
        ans=totalcombination-dic1[n]+reduced_pattern #その数字に対応する元の組み合わせの数を減らし、1個前で計算した組合わせを足す。
        dic2[n] = ans #同じ数字を引いたときように答えを辞書に登録しておく。
        print(ans)
    else:
        print(dic2[n]) #もし、過去に同じ数字を引いていたら、登録しておいた答えを回答する。

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for key in A:
    d[key] += 1
    #print(d)

Sum = 0
for i in d:
    n = d[A[i]]
    Sum += (n * (n - 1)) // 2
    #print((n * (n - 1)) // 2)
    #print(d[A[i]],i)
    #print(d)

#print(Sum)

for i in range(N):
    n = d[A[i]]
    hoge = Sum - (n*(n-1))//2
    n -= 1
    hoge += (n*(n-1))//2
    print(hoge)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

# 0 0
# 1 0
# 2 1
# 3 3
# 4 6

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

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

C = [0]*(2*10**5+1)

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

ans = sum(i*(i-1)//2 for i in l)

for i in A:
    print(ans-l[i]+1)

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce

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

def cmb(n, r):
    if n < r:  return 0
    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

dic = {}
for i in range(N):
    if A[i] in dic:  dic[A[i]] += 1
    else:  dic[A[i]] = 1
        
sum_ = 0
for i in dic: sum += cmb(dic[i], 2)

for i in range(N):
    ans = sum_ - (dic[A[i]] - 1)
    print(ans)

Submission status: Runtime Error

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

f_list = [0 for i in range(N+1)]
f_list[0] = 1
for i in range(1, N+1):
    f_list[i] = f_list[i-1]*i

def c(n):
    return f_list[n]//f_list[n-2]//2

m = Counter(A)
s = 0
for x, y in m.items():
    else:
        s += c(y)
for a in A:
    print(s - (m[a] - 1))

Submission status: Runtime Error

Solution code:
from collections import Counter, defaultdict
N = int(input())
A = [int(i) for i in input().split()]

f_list = [0 for i in range(N+1)]
f_list[0] = 1
for i in range(1, N+1):
    f_list[i] = f_list[-1]*i

def c(n):
    return f_list[n]//f_list[n-2]//2

m = Counter(A)
ans = defaultdict(int)
s = 0
for x, y in m.items():
    if y == 1:
        ans[x] = 0
    else:
        ans[x] = c(y)
        s += ans[x]
for a in A:
    print(s - (m[a] - 1))

Submission status: Runtime Error

Solution code:
n = int(input())
 
a = list(map(int,input().split()))
 
total = 0
 
cnt = [0]*n
for i in range(n):
    cnt[a[i]] += 1
    
for i in cnt:
    if i > 0:
        total += (i*(i-1))//2
 
for i in range(n):
    ans = 0
    before = (cnt[a[i]]*(cnt[a[i]]-1))//2
    after = ((cnt[a[i]]-1)*(cnt[a[i]]-2))//2
    
    ans = total - before + after
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

n = int(input())

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

total = 0

cnt = [0]*n
for i in range(n):
    cnt[a[i]] += 1
    
for i in cnt:
    if i > 0:
        total += (i*(i-1))//2

for i in range(n):
    ans = 0
    before = (cnt[a[i]]*(cnt[a[i]]-1))//2
    after = ((cnt[a[i]]-1)*(cnt[a[i]]-2))//2
    
    ans = total - before + after
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

n = int(input())

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

total = 0

cnt = [0]*n
for i in range(n):
    cnt[a[i]] += 1
    
for i in cnt:
    total += (i*(i-1))//2

for i in range(n):
    ans = 0
    before = (cnt[a[i]]*(cnt[a[i]]-1))//2
    after = ((cnt[a[i]]-1)*(cnt[a[i]]-2))//2
    
    ans = total - before + after
    print(ans)

Submission status: Runtime Error

Solution code:
from math import factorial

N = int(input())
index = list(map(int, input().split()))
val = [0]*max(index)
for i in index:
    val[i-1] += 1
total = 0
for i in val:
    if i >= 2:
        total += comb(i, 2, exact=True)
for i in index:
    if val[i-1] >= 2:
        print(total-(comb(val[i-1], 2, exact=True)-comb(val[i-1]-1, 2, exact=True)))
    else:
        print(total)

Submission status: Runtime Error

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

for i in range(n):
    ans = 0
    num_ans = []
    a_tmp1 = a[i]
    a[i] = 0
    for num in num_list:
        num1 = a_tmp.count(num)
        num_ans.append(num1)
    for tmp in num_ans:
        ans += tmp*(tmp-1)/2
    a[i] = a_tmp1
    ans = int(ans)
    print(ans)

Submission status: Runtime Error

Solution code:

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

def choose2(x):
    return int(x*(x-1)/2)
    
total = 0
for i in b:
    total += choose2(a.count(i))

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

Submission status: Runtime Error

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

# N = 5
# A = [1, 1, 2, 1, 2]

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0

# print(lst)
    
for i in range(N):
    lst[A[i]] += 1

# print(lst)
# c = dict(lst) # c = dict(lst)で異なるオブジェクトとして等価なcを定義
              # c = lst だと、同一のオブジェクトとなってしまう？

for i in lst:
#     print('i: {}'.format(i))
    c[i] = 0
    tmp_lst = dict(lst)
#     print('tmp_lst {}'.format(tmp_lst))
#     print('tmp_lst[i]: {}'.format(tmp_lst[i]))
    tmp_lst[i] -= 1
#     print('tmp_lst[i]: {}'.format(tmp_lst[i]))
    for j in tmp_lst:
#         print(tmp_lst[j])
        c[i] += tmp_lst[j]*(tmp_lst[j]-1)//2
#         print('c[i]: {}'.format(c[i]))
        
for i in range(N):
    print(int(c[A[i]]))

Submission status: Runtime Error

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

# N = 5
# A = [1, 1, 2, 1, 2]

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0

# print(lst)
    
for i in range(N):
    lst[A[i]] += 1

# print(lst)
# c = dict(lst) # c = dict(lst)で異なるオブジェクトとして等価なcを定義
              # c = lst だと、同一のオブジェクトとなってしまう？

for i in lst:
#     print('i: {}'.format(i))
    c[i] = 0
    tmp_lst = dict(lst)
#     print('tmp_lst {}'.format(tmp_lst))
#     print('tmp_lst[i]: {}'.format(tmp_lst[i]))
    tmp_lst[i] -= 1
#     print('tmp_lst[i]: {}'.format(tmp_lst[i]))
    for j in tmp_lst:
#         print(tmp_lst[j])
        c[i] += tmp_lst[j]*(tmp_lst[j]-1)//2
#         print('c[i]: {}'.format(c[i]))
        
for i in range(N):
    print(c[A[i]])

Submission status: Runtime Error

Solution code:
from operator import mul
from functools import reduce

def comb(n, r):
    if n<r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

N = int(input())
A = list(map(int,input().split()))
D = {}
for k in A:
    D[k] = comb(A.count(k),2)

for k in A:
    count = 0
    for j in D.keys:
        if k == j:
            count += comb(A.count(j)-1,2)
        else:
            count += D[k]
    print(count)

Submission status: Runtime Error

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot,
                  log2, pi, radians, sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


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


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


def def_value():
    return 0


def ncr(n, r):
    if r > n:
        return 0
    return n*(n-1) // 2


n = inp()
a = inlist()

count = defaultdict(def_value)

for i in a:
    count[i] += 1

for i in range(n):
    res = 0
    for k in count:
        x = count[k]
        if k == a[i]:
            x = x - 1

        res += ncr(x, 2)
    print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = []
total = 0


for i in range(n):
    l.append(a.count(i))


for r in l:
    total += (r*(r-1))/2


#print(l)
#print(total)

for i in a:
    score = total - (l[i]-1)
    print(int(score))

Submission status: Runtime Error

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

for n in c.items():
  if n[1] > 1:
    total += n*(n-1)//2

for i in range(N):
  ans = total
  ans -= A.count(A[i])-1
  print(ans)

Submission status: Runtime Error

Solution code:
import collections
import math
N = int(input())
A = list(map(int, input().split()))
ans = 0

for i in range(N):
  li = A[:i] + A[i+1]
  c = collections.Counter(li)
  l = [i for i in C.items() if i > 1]
  for num in l:
    ans += math.factorial(num) // (math.factorial(num-2) * 2)
    
print(ans)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10**6)

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

cnt = [0 for i in range(n)]

for i in range(n):
    cnt[a_l[i]]+=1
ans = 0

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

Submission status: Runtime Error

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

cnt = [0 for i in range(n)]

for i in range(n):
    cnt[a_l[i]]+=1
ans = 0

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

Submission status: Runtime Error

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

cnt = [0 for i in range(n)]

for i in range(n):
    cnt[a_l[i]]+=1
ans = 0

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

Submission status: Runtime Error

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


def nCr(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


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

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0)
    else:
        c = nCr(v, 2)
        ac[k] = (v, c)
        ans += c

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + nCr(v[0] - 1, 2))
from collections import Counter
import math


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


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

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0)
    else:
        c = nCr(v, 2)
        ac[k] = (v, c)
        ans += c

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + nCr(v[0] - 1, 2))

Submission status: Runtime Error

Solution code:
import math
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
data_cnt=collections.Counter(data)
ans=[0]*n
count=0
for i in data_cnt.values():
    count+=calc_combi2(i)
for a,b in data_cnt.items():
    if 2<=b:
        ans[a]=int(count-b+1)
for i in data:
    print(ans[i-1])

Submission status: Runtime Error

Solution code:
import math
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
data_cnt=collections.Counter(data)
ans=[0]*n
count=0
for i in data_cnt.values():
    count+=calc_combi2(i)
for a,b in data_cnt.items():
    ans[a]=int(count-b+1)
for i in data:
    print(ans[i])

Submission status: Runtime Error

Solution code:
import collections
import math

def nC2(n):
    if n == 1:
        return 0
    return nCr(n, 2)

def nCr(n,r):
    f = math.factorial
    return f(n) / f(r) / f(n-r)

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

c = collections.Counter(A)
sum_values = sum(map(nC2, c.values()))

for n in range(N):
    print(int(sum_values - (nC2(c[A[n]]) - nC2(c[A[n]] - 1))))

Submission status: Runtime Error

Solution code:
import collections
import math

def nC2(n):
    if n == 1:
        return 0
    return nCr(n, 2)

def nCr(n,r):
    f = math.factorial
    return f(n) / f(r) / f(n-r)

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

c = collections.Counter(A)
sum_values = sum(map(nC2, c.values()))

for n in range(N):
    if c[A[n]] == 1:
        print(int(sum_values))
    else:
        print(int(sum_values - (nC2(c[A[n]]) - nC2(c[A[n]] - 1))))

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int,input().split())))
L = [0]*(N+1)
L = np.array(L)

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

LL  = np.multiply(L,L-1)/2

gros = int(sum(LL))

for k in A:
    print(gros +1 - L[k])

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1

MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod

comInit(MOD)

# total = 0 
# diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
#     total += x
#     if c - 1 <= 0:
#         diff[i] = x
#     else:
#         y = com(c - 1, 2, MOD)
#         diff[i] = x - y

# for i in range(n):
#     print(total - diff[a[i]])

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1

MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
    total += x
    # if c - 1 <= 0:
    #     diff[i] = x
    # else:
    #     y = com(c - 1, 2, MOD)
    #     diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1


MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
    total += x
    if c - 1 <= 0:
        diff[i] = x
    else:
        y = com(c - 1, 2, MOD)
        diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Submission status: Runtime Error

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1


MAX = 10 ** 5
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = com(c, 2, MOD)
    total += x
    y = com(c - 1, 2, MOD)
    diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Submission status: Runtime Error

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from fractions import gcd
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


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


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


def fact(n):
    if n == 0:
        return 1
    return fact(n-1)*n


def ncr(n, r):
    if n < r:
        return 0
    return fact(n)//(fact(r)*(fact(n-r)))


n = inp()

a = inlist()
count = dict()
for k in a:
    if k in count:
        count[k] += 1
    else:
        count[k] = 1

for i in range(n):
    res = 0
    for k in count:
        bad = a[i]
        if k == bad:
            res += ncr(count[k]-1, 2)
        else:
            res += ncr(count[k], 2)
    print(res)

Submission status: Runtime Error

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

N=int(input())
A=list(map(int,input().split()))
B=set(A)
res=0
C=[0]*(N+1)+

for b in B:
    C[b]=A.count(b)
    if A.count(b)>1:
            res+=combinations_count(A.count(b),2)
for a in A:
    ans=res
    if C[a]<2:
        print(ans)
    else:
        ans-=combinations_count(C[a],2)
        if C[a]!=2:
            ans+=combinations_count(C[a]-1,2)
        print(ans)

Submission status: Runtime Error

Solution code:
from scipy.special import comb

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

number_set = list(set(A))
#print("number set is :", number_set)    
    
for k in range(n):
    out = 0
    ans = []
    ans += A[0:k]
    ans += A[k+1:n]
    #print(ans)
    for i in range(len(number_set)):
        #print(ans.count(number_set[i]))
        out += comb(ans.count(number_set[i]), 2 ,exact = True)
        
    print(out)