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

Solution (Python):
N_max = 2*(10**5)
N = int(input())

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

array = sorted(A)
quantity_of_num = [0]*(N_max + 1) 
type_index = list() 

count = 1 
for i in range(N-1):
    if array[i] == array[i+1]:
        count += 1
    else:
        type_index.append(array[i])
        quantity_of_num[array[i]] = count

        count = 1
type_index.append(array[i+1])
quantity_of_num[array[i+1]] = count









total = 0 
for i in range(len(type_index)):
        quantity = quantity_of_num[type_index[i]]
        
        
        total += quantity*(quantity - 1) // 2

for k in range(N):

    
    
    quantity = quantity_of_num[A[k]]
    ans = total - ( quantity*(quantity-1) // 2) + ( (quantity-1)*(quantity-2) // 2)
    
    print(ans)

Solution (Python):
import math

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

def comb(n):
  r = 2
  if n <= 1:
    return 0
  if n - r < r: r = n - r
  if r == 0: return 1
  if r == 1: return n
  
  numerator = [n - r + k + 1 for k in range(r)]
  denominator = [k + 1 for k in range(r)]
  
  for p in range(2,r+1):
    pivot = denominator[p - 1]
    if pivot > 1:
      offset = (n - r) % p
      for k in range(p-1,r,p):
        numerator[k - offset] /= pivot
        denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
dic = {}

for a in A:
  if a in dic:
    dic[a][0] += 1
  else:
    dic[a] = [1]


for v in dic.values():
  v.append(comb(v[0]))
  v.append(comb(v[0]-1))


grand = 0
for v in dic.values():
  grand += v[1]
  
for a in A:
  output = grand - dic[a][1] + dic[a][2]
  print(output)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
L = Counter(A)
anss = {}
sum_ans = sum([i*(i-1)//2 for i in L.values() if i >= 2])
for k,v in L.items():
    anss[k] = sum_ans - (v*(v-1)//2) + ((v-1)*(v-2)//2)
for i in A:
    print(anss[i])

Solution (Python):

from collections import defaultdict as dd
n=int(input())
a=list(map(int,input().split()))
d=dd(int)
for aa in a:
    d[aa]+=1
a_sum=0
for k,v in d.items():
    
    a_sum+=v*(v-1)//2

for i,aa in enumerate(a):
    s=a_sum
    t=d[a[i]]
    s-=t*(t-1)//2
    t-=1
    s+=t*(t-1)//2
    print(s)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

from decimal import *

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()
	A = inputList()

	ap = {}
	for i in A:
		if i in ap:
			ap[i] += 1
		else:
			ap[i] = 1

	mmax = 0
	for k, v in ap.items():
		if v > 1:
			mmax += cmb(v, 2)

	
	for i in A:
		if mmax == 0:
			print(0)
			continue
		conts = ap[i]
		conts2 = conts -1
		
		m = 0
		p = 0
		if conts == 2:
			m = cmb(conts, 2)
		elif conts == 1:
			print(mmax)
			continue
		else:
			m = cmb(conts, 2)
			p = cmb(conts2, 2)
		
		aaa = mmax - m + p
		print(aaa)







def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))



def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
nl = {}
cal = {}
kal = {}
cl = {}
all = 0
for num in l:
  if num in nl:
    nl[num] = nl[num] + 1
  else:
    nl[num] = 1
for n in nl:
  if nl[n] > 1:
    cal[n] = nl[n] * (nl[n]-1) / 2
  else:
    cal[n] = 0
  if nl[n]-1 > 1:
    kal[n] = (nl[n]-1) * (nl[n]-2) / 2
  else:
    kal[n] = 0
  all += cal[n]
for n in nl:
  cl[n] = int(all - cal[n] + kal[n])
for i in l:
  print(cl[i])

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())




count = [0]*(N+2)
for i in range(N):
    id = A[i]
    count[id] = count[id]+1

sumN = long(0)
for i in range(N+1):
    sumN = sumN + long(long(count[i]*(count[i]-1))/2)


for i in range(N):
    id = A[i]
    sumz = sumN - (count[id] - 1)
    print(sumz)

Solution (Python):
N = int(input())
Ai = list(map(int,input().split()))
Aisort = sorted(Ai) + [-1]
Aidic = {}
cnt = 1
for i in range(1,N+1):
    if Aisort[i] != Aisort[i-1]:
        Aidic.update([(str(Aisort[i-1]), cnt)])
        cnt = 1
    else:
        cnt = cnt + 1

cnt = 0
for i, indx in enumerate(Aidic):
    num = int(Aidic[indx])
    cnt = cnt + num*(num-1)//2


for A in Ai:
    num = int(Aidic[str(A)])
    num = cnt - (num-1)
    print(num)

Solution (Python):


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

As = set(A)

for a in A:
    if a in d:
        d.update({a: d.get(a) + 1})
    else:
        d.update({a: 1})

summ = 0
for x in As:
    y = d.get(x)
    summ += y*(y-1) // 2

for a0 in A:
    print(summ - (d.get(a0)-1))

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

from decimal import *

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()
	A = inputList()

	ap = {}
	for i in A:
		if i in ap:
			ap[i] += 1
		else:
			ap[i] = 1

	mmax = 0
	for k, v in ap.items():
		if v > 1:
			mmax += cmb(v, 2)

	
	for i in A:
		if mmax == 0:
			print(0)
			continue
		conts = ap[i]
		conts2 = conts -1
		
		m = 0
		p = 0
		if conts == 2:
			m = cmb(conts, 2)
		elif conts == 1:
			print(mmax)
			continue
		else:
			m = cmb(conts, 2)
			p = cmb(conts2, 2)
		
		aaa = mmax - m + p
		print(aaa)







def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))



def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Solution (Python):
import collections

N = int(input())
A = list(map(int,input().split()))
a = collections.Counter(A)
v = a.values()
l_v = list(v)


s = 0
for i in range(len(l_v)):
    n = l_v[i]-1
    s += n*(n+1)//2

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

Solution (Python):
from collections import Counter
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()))

a_cnt = Counter(a)

a_comb = {}
for key, val in a_cnt.items():
    if val >= 2:
        a_comb[key] = combinations_count(val, 2)
    else:
        a_comb[key] = 0

sum_v = sum(a_comb.values())

for num in a:
    print(sum_v-a_cnt[num]+1)

Solution (Python):
import collections
import math

def comb(n, r):
    if n < 2:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
L = input().split(" ")

c = collections.Counter(L)

d = {}
tmp_total = 0 

memo1 = {}
L2 = {}

for i in c:
    if c[i] not in memo1.keys():
        tmp = comb(c[i], 2)
        memo1[c[i]] = tmp
        tmp_total += tmp
    else:
        tmp_total += memo1[c[i]]
        
total = tmp_total

for i in L:
    ans = total - memo1[c[i]] + memo1[c[i]]*(c[i]-2)//c[i]
    print(ans)

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





syutsugen_counts = [0] * n

choice_counts = [0] * n

decrease_one_choice_counts = [0] * n

total_choice_count = 0

for i in range(n):
  ball_num = a[i] - 1
  syutsugen_counts[ball_num] += 1

for i in range(n):
  syutsugen_count = syutsugen_counts[i]
  if syutsugen_count < 2:
    continue
  
  choice_count = int((syutsugen_count * (syutsugen_count-1) / 2))
  choice_counts[i] = choice_count
  total_choice_count += choice_count
  
  
  if syutsugen_count < 3:
    continue
  
  
  decrease_one = syutsugen_count - 1
  decrease_one_choice_count = int((decrease_one * (decrease_one-1) / 2))
  decrease_one_choice_counts[i] = decrease_one_choice_count

for i in range(n):
  ball_num = a[i] - 1
  choice_count = choice_counts[ball_num]
  decrease_one_choice_count = decrease_one_choice_counts[ball_num]
  print(total_choice_count - choice_count + decrease_one_choice_count)

Solution (Python):

from collections import Counter as co
n=int(input())
a=[int(x) for x in input().split()]
d=dict(co(a))

p={}
for x in d:
    y=d[x]
    p[x]=max((y*(y-1))//2,0)

ss=0
for x in p:
    ss+=(p[x])

for i in a:
    x=d[i]-1
    
    y=max(x*(x-1)//2,0)
    print(ss-p[i]+y)

Solution (Python):
n = int(input(''))
stra = input('')

alist = stra.split(' ')
dic = {}
for i in range(0,len(alist)):
  if alist[i] in dic.keys():
    dic[alist[i]]+=1
  else:
    dic[alist[i]] = 1

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

    
for k in range(0,n):
  cnt = 0
  key = alist[k]
  minus_cnt1 = (dic[key] * (dic[key]-1))/2
  minus_cnt2 = ((dic[key]-1) * (dic[key]-2))/2
  print(int(all_cnt -  minus_cnt1 + minus_cnt2))

Solution (Python):
from sys import stdin
from collections import Counter

def main():

    input = stdin.readline

    N = int(input())

    A = [0] * N

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

    all_case = 0

    num_all = Counter(A) 

    for k in num_all.values():
        all_case += (k * (k-1)) // 2

    for i in A:
        ans = all_case - (num_all[i] - 1)
        print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())

count = [0 for _ in range(200000)]
line = [int(i) for i in input().split()]
pattern = [0 for _ in range(200000)]
sum = 0


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

for num in count:
	sum += (num*(num-1))//2



for i in line:
	if count[i] > 2:
		result = sum - (count[i]-1) 
	elif count[i] == 2:
		result = sum - 1
	elif count[i] == 1:
		result = sum
	print(result)

Solution (Python):
import sys
import collections
input = sys.stdin.readline

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

    c = collections.Counter(A)

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

    for x in A:
        print(s - (c[x] - 1))


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

def cmb(n, r, mod=10**9+7):
    return n * (n - 1) // 2



N = int(input())
ball_com = [0] * (N + 1)
balls = list(map(int, input().split()))
balls_counter = Counter(balls)
for key , val in balls_counter.items():
    ball_com[key - 1] = cmb(val, 2)

cmb_sum = sum(ball_com)
for ball in balls:
    print(cmb_sum - ball_com[ball - 1] + cmb(balls_counter[ball] - 1, 2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
lis = [0] * (n *3)
for i in range(n):
    lis[a[i]] += 1

cum = [0] * len(lis)
for i in range(n*3):
    cnt = (lis[i] * (lis[i]-1)) // 2
    cum[i] = cnt
ans = sum(cum)
cum2 = [0] * len(lis)
for i in range(2,(n*3)):
    cum2[i] = (i * (i-1)) // 2






for i in range(n):
    print(ans - (cum2[lis[a[i]]]-cum2[lis[a[i]]-1]))

Solution (Python):

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

def sum(num):
    if num < 2:
        return 0
    s = 0
    for i in range(num):
        s += i
    return s

x = [0] * (n+1)

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

ans = 0

for i in range(n+1):
    ans += sum(x[i])

for i in range(n):
    ans2 = ans - (x[a[i]]-1)
    print(ans2)

Solution (Python):
import bisect

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

B = sorted(A)
cnt = [0] * (N+1)
for k in range(1, N+1):
    cnt[k] = bisect.bisect_left(B, k+1)
for k in range(N):
    cnt[-1-k] -= cnt[-2-k]

ALL = 0
for i in range(1, N+1):
    ALL += cnt[i]*(cnt[i]-1)//2

for k in range(N):
    ans = ALL - cnt[A[k]]*(cnt[A[k]]-1)//2 + (cnt[A[k]]-1)*((cnt[A[k]]-1)-1)//2
    print(ans)

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

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

total=0

for k,v in B.items():
    total+=v*(v-1)//2

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

Solution (Python):
from collections import Counter

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

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

    for i in range(n):
        print(cnt - (Da[A[i]] - 1))

if __name__ == "__main__":
    main()

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


Ac = [ikutu[x] for x in ikutu]


kumi = 0
for i in range(len(Ac)):
    if Ac[i] != 1:
        kumi += (Ac[i]*(Ac[i]-1))//2

for j in range(N):
    if ikutu[A[j]] != 1:
        Ans = kumi - ((ikutu[A[j]])*(ikutu[A[j]]-1))//2 + ((ikutu[A[j]]-1)* (ikutu[A[j]]-2))//2
    else:
        Ans = kumi
    print(Ans)

Solution (Python):
import collections

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

ctr_A = collections.Counter(A).most_common()
less_A = collections.Counter(A)

total = 0

for i in range(len(ctr_A)):
    n = ctr_A[i][1]
    total += n * (n - 1) // 2

for i in range(N):
    summ = total
    num = A[i]
    n = less_A[num]

    if (n == 2):
        summ -= 1
    else:
        new = (n - 1) * (n - 2) // 2
        old = n * (n - 1) // 2
        summ += new - old
    
    ans = max(0, summ)

    print(ans)

Solution (Python):
from collections import Counter

N = int(input())

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

set_A = set(A)

ans = 0
sens = {}
for value in Counter(A).items():
    key = value[0]
    v = value[1]
    ans += v * (v - 1) /2
    sens[key] = v * (v - 1) /2 - (v - 1) * (v - 2)/2

for a in A:
    print(int(ans - sens[a]))

Solution (Python):
n = int(input())
k = [int(i) for i in input().split()]
pair = {}

for i in range(n):
    pair.setdefault(k[i], []).append(i)

sum = 0
for value in pair.values():
    l = len(value)
    sum += l*(l-1)//2

for i in range(n):
    len_pair = len(pair[k[i]])
    if len_pair != 1:
        ans = sum - ((len_pair*(len_pair-1)//2)-((len_pair-1)*(len_pair-2)//2))
        print(ans)
    else:
        print(sum)

Solution (Python):
from math import factorial
from functools import lru_cache

@lru_cache(maxsize=None)
def cmbcount(n):
    if n > 1:
        return factorial(n) // (factorial(n - 2) * factorial(2))
    return 0

N = int(input())
A = list(map(int, input().split()))
samecnt = [0] * (N + 1)
for a in A:
    samecnt[a] += 1
cnts = [cmbcount(n) for n in samecnt]
allcnt = sum(cnts)
for i in range(N):
    a = A[i]
    o = cnts[a]
    n = cmbcount(samecnt[a] - 1)
    ans = allcnt - (o - n)
    print(ans)

Solution (Python):
from scipy.misc import comb
N,*A = map(int,open(0).read().split())
d = {}
for i in A:
    d[i]= d[i]+1 if i in d else 1
count_d = {}
sum = 0
for k,v in d.items():
    count_d[k] = comb(v,2,exact=True)
    sum += count_d[k] 
 
for i in A:
    if count_d[i]>=1:
        print(sum - (d[i]-1))
    else:
        print(sum)

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

listlist = []
result = {}
for i in range(0,n):
  num = a[i]
  if num in result:
    result[num] = result[num] + 1
  else:
    result[num] = 1
    
vals = list(result.values())
l = len(vals)

count = 0
for j in range(0,l):
  attack = vals[j]
  count = count + attack*(attack-1)//2

for k in range(0, n):
  block = a[k]
  if block in result:
    fighter = result[block]
    listlist.append(count - fighter +1)
  else:
    listlist.append(count)
     
for si in listlist:
  print(si)

Solution (Python):
from functools import lru_cache

N = int(input().strip())
A = [int(v) for v in input().strip().split()]
C = {}


@lru_cache(maxsize=None)
def pair(k):
    return k * (k - 1) // 2

@lru_cache(maxsize=None)
def calc(s, a):
    return s - pair(C[a]) + pair(C[a] - 1)

for a in A:
    C.setdefault(a, 0)
    C[a] += 1

S = sum([pair(v) for v in C.values()])

for a in A:
    print(calc(S, a))

Solution (Python):



def main():
    n = int(input())
    a = [int(s) for s in input().split(" ")]
    counts = {}
    for i in a:
        if i in counts:
            counts[i] += 1
        else:
            counts[i] = 1
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    total = 0
    comb = {}
    for k in counts:
        if counts[k] > 1:
            y = counts[k]
            x = (y * (y - 1)) // 2
            total += x
            comb[k] = x
        else:
            comb[k] = 0
    for i in a:
        print(total - (counts[i] - 1))


if __name__ == '__main__':
    main()

Solution (Python):
def comb(a):
    if a < 2:
        return 0
    else:
        return a * (a-1) // 2


N = int(input())
A = list(map(int, input().split()))
ans = []
total = 0
dct = {}
for a in A:
    if (dct.get(a) != None):
        total -= comb(dct[a])
        dct[a] += 1
        total += comb(dct[a])
    else:
        dct[a] = 1
for a in A:
    print(total - comb(dct[a])+comb(dct[a]-1))

Solution (Python):
from collections import *
n=int(input())
numbers=input().rstrip().split(" ")
S=Counter(numbers)

sigma=0

for i in S.values():
    sigma+=i*(i-1)/2
alls=int(sigma)
answers=[]
for number in numbers:
    minus=S[number]-1
    answers.append(alls-minus)
for answer in answers:
    print(answer)

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
import collections
c = collections.Counter(P)
t = 0
for j in c.values():
    t = t+j*(j-1)/2
for i in range(N):
    t1 = t -(c[P[i]]-1)
    print(int(t1))

Solution (Python):
import collections

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

N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
c_l = list(c.values())
d2 = sum([c2c(i) for i in c_l])


for i in range(len(A)):
    print(d2-(c[A[i]]-1))

Solution (Python):
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

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

Solution (Python):
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter

N = int(stdin.readline().rstrip())
A = [int(s) for s in stdin.readline().rstrip().split()]

B = copy.copy(A)
B.sort()

d = {}
e = {}
count = 0
pos = 0
for el in B:
    if pos == 0:
        count += 1
    elif B[pos] == B[pos - 1]:
        count += 1
    elif B[pos] != B[pos - 1]:
        e[str(el-1)] = int(count*(count-1)/2)
        count = 1
    d[str(el)] = count
    pos += 1
    if len(B) == pos:
        e[str(el)] = int(count*(count-1)/2)
res = 0
for el in e.values():
    res += el
for el in A:
    print(res-int((d[str(el)]*(d[str(el)]-1)/2)) +
          int(((d[str(el)]-2)*(d[str(el)]-1)/2)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = [0 for _ in range(n+1)]


def num_comb(a):
    return a * (a - 1) // 2


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

num_combs = [num_comb(cnt[i]) for i in range(n+1)]
total = sum(num_combs)

for i in range(n):
    res = total - num_combs[a[i]] * 2 // cnt[a[i]]
    print(res)

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

counter = Counter(A_list)
result = 0
for (key, value) in counter.items():
    
    result += int(value * (value - 1) / 2)


for a in A_list:
    value = counter.get(a)
    
    answer = result - (int(value * (value - 1) / 2)) + int((value - 1) * (value - 2) / 2)
    print(answer)

Solution (Python):
import collections

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

c = collections.Counter(A)
cv = c.values()
ck = c.keys()

ori=0
for x in cv:
    ori += (x-1) * ((x-1)+1) // 2


for i in range(N):
    tmp = A[i]
    print(ori - (c[tmp]-1))

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
s = 0
for v in c.values():
    if v >= 2:
        s += v*(v-1)//2

for i in range(n):
    k = a[i]
    ans = s
    v = c[k]
    if v >= 3:
        ans -= v-1
    if v <= 2:
        ans -= v*(v-1)//2
    print(ans)

Solution (Python):
import math

def permutations_count(n, r):
    if (n < 2):
        return 0
    else:
        return math.factorial(n) // math.factorial(n - r)

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

bar = {}

for i in range(N):
    if (A[i] in bar):
        bar[A[i]] = bar[A[i]] + 1
    else:
        bar[A[i]] = 1

partOfNum = {}
lackPartOfNum = {}

for j in bar.keys():
    tmp = permutations_count(bar[j], 2)
    partOfNum[j] = int(tmp / 2)

    lackPartOfNum[j] = partOfNum[j] - (bar[j] - 1)

allSum = 0
for l in partOfNum.keys():
    allSum += partOfNum[l]

for i in range(N):
    print(allSum - partOfNum[A[i]] + lackPartOfNum[A[i]])

Solution (Python):


n = input()
an = map(int, raw_input().split())

d = {}
for a in an:
    if not a in d:
        d[a] = 1
    else:
        d[a] += 1


sa = 0
for ai in d:
    si = d[ai] * (d[ai] - 1) // 2
    sa += si

for a in an:
    a1 = d[a] * (d[a] - 1) // 2
    a2 = (d[a] - 1) * (d[a] - 2) // 2

    print sa - a1 + a2

Solution (Python):
from collections import Counter


def choose(n: int) -> int:
    if n < 2:
        return 0
    else:
        return (n * (n - 1)) // 2


N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
d = {key: choose(value) for key, value in c.items()}
if len(d) > 1:
    sum_d = sum(d.values())
    for a in A:
        print(sum_d - d[a] + choose(c[a] - 1))
else:
    ans = choose(N - 1)
    for _ in range(N):
        print(ans)

Solution (Python):
N = int(input())
list = list(map(int,input().split()))
kosuu = [0] * N
kosuuu = [0] * N
total = 0
listed = sorted(list)
listed.append(-1)
i = 0
count = 1
while i < N:
    a = listed[i]
    if a == listed[i+1]:
        count += 1
    else:
        kosuu[a-1] = count
        total += ( count * (count-1) ) //2
        count = 1
    i += 1
for i in range(N):
    a = kosuu[list[i]-1]
    print(total-a*(a-1)//2 + (a-1)*(a-2)//2)

Solution (Python):
from collections import Counter

def nCk(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
c = {}
d = {}
for k, v in b.items():
    c[k] = nCk(v, 2) if v > 1 else 0
    d[k] = nCk(v-1, 2) if v > 2 else 0
total = sum(c.values())
for i in range(n):
    ans = total - c[a[i]] + d[a[i]]
    print(ans)

Solution (Python):
from collections import defaultdict
def main():
    n=int(input())
    A=list(map(int,input().split()))
    d=defaultdict(int)
    m=max(A)
    for a in A:
        d[a]+=1
    c=0
    for i in range(1,m+1):
        if d[i]>=2:
            c+=(d[i]*(d[i]-1))//2
    for a in A:
        if d[a]<2:
            print(c)
        else:
            print(c-(d[a]-1))
    
if __name__=='__main__':
    main()

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
wlists = collections.Counter(A)
sum_num = 0
for i in wlists:
  sum_num += wlists[i] * (wlists[i] - 1) // 2
for i in range(N):
  num = sum_num - wlists[A[i]] * (wlists[A[i]] - 1) // 2
  if wlists[A[i]] > 1:
    num += (wlists[A[i]] - 1) * (wlists[A[i]] - 2) // 2
  print(num)

Solution (Python):
from collections import defaultdict
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = tuple(map(int, input().split()))

    ball = defaultdict(int)
    for i in A:
        ball[i] += 1
    total = 0
    sel = defaultdict(int)
    for k, v in ball.items():
        n = v * (v - 1) // 2
        sel[k] = n
        total += n
    for a in A:
        n = ball[a] - 1
        x = (n * (n - 1)) // 2
        print(total - (sel[a] - x))


main()

Solution (Python):
from copy import deepcopy
dp = {0: 0}

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])

Solution (Python):
from collections import Counter
'''
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
'''

def cmb(n,r):
    return n*(n-1)//2

N=int(input())
A=list(input().split())
c=Counter(A)
itms=list(c.items())
lenC=len(c)
num=0

for i in range(lenC):
    itmsi=itms[i][1]
    if itmsi>=2:
        num+=cmb(itmsi,2)


for i in range(N):
    num2=c[A[i]]
    if num2>=3:
        print(num-cmb(num2,2)+cmb(num2-1,2))

    elif num2==2:
        print(num-1)
    else:
        print(num)