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):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
d = {}

def fac2(n):
    if n < 2:
        return 0
    ans = n * (n-1) // 2
    return ans

for key, val in c.items():
    d[key] = fac2(val)

sum_val = sum(d.values())
for a in A:
    ans = sum_val - d[a] + fac2(c[a]-1)
    print(ans)

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

def f(x):
    res = x * (x - 1) // 2

    return max(0, res)


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

if n == 1:
    print(0)
    exit()

counts = [0] * (n + 1)

for x in a:
    counts[x] += 1

s = sorted(counts)
m1 = s[-1]
m2 = s[-2]


max_result = 0
for k in range(1, n + 1):
    max_result += f(counts[k])

for k in range(1, n + 1):
    kk = a[k - 1]
    print(max_result - f(counts[kk]) + f(counts[kk] - 1))

Solution (Python):
from collections import Counter
N=int(input())
A=list(map(int,input().split(' ')))
A_count = Counter(A)
S=sum([i*(i-1)//2 for i in A_count.values()])
A_dic = {k:v*(v-1)//2-(v-1)*(v-2)//2 for k,v in A_count.items()}
for i in A:
    print(S-A_dic[i])

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
ac = Counter(A)
comblist = [0 for i in range(max(ac.values())+1)]
if max(ac.values()) == 1:
    for i in range(N):
        print(0)
else:
    comblist[2] = 1
    for i in range(3,max(4,max(ac.values())+1)):
        comblist[i] = comblist[i-1]*i//(i-2)
    ans = {}
    anssum = 0
    for k,v in ac.items():
        if v > 1:
            anssum += comblist[v]
        if k not in ans:
            if v == 2:
                ans[k] = -1
            elif v == 1:
                ans[k] = 0
            else:
                ans[k] = comblist[v-1] - comblist[v]
    for i in A:
        print(anssum+ans[i])

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

arr = [0 for i in range(N+1)]

for i in range(len(List)):
  a = List[i]
  arr[a] += 1
  
ans1 = [0 for i in range(N+1)]
ans2 = [0 for i in range(N+1)]

for i in range(len(arr)):
  a = arr[i]
  b = int(a*(a-1)/2)
  if a == 0:
    c = 0
  else:
    c = int((a-1)*(a-2)/2)
    
  ans1[i] = b
  ans2[i] = c
    
total = sum(ans1)

for i in range(len(List)):
  a = List[i]
  print(int(total - ans1[a] + ans2[a]))

Solution (Python):
from collections import Counter

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

c = Counter(A)



def f(v):
    return v * (v - 1) // 2


combi = {key: f(value) for key, value in c.items()}

sum_combi = sum(combi.values())


for i in range(N):
    v = c[A[i]]
    diff = f(v) - f(v - 1)
    print(sum_combi - diff)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
dic = {}
for A in As:
    if A in dic.keys():
        dic[A] += 1
    else:
        dic[A] = 1
total = 0
for A in dic.keys():
    a = dic[A]
    if a > 1:
        temp = a*(a-1)/2
        total += temp
for i in range(N):
    Ai = As[i]
    if dic[Ai] == 2:
        print(int(total-1))
    elif dic[Ai] > 2:
        a = dic[Ai]
        diff = (a*(a-1) - (a-1)*(a-2))/2
        print(int(total - diff)) 
    else:
        print(int(total))

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

from collections import Counter
c = Counter(a)

ans0 = sum(cc*(cc-1)//2 for cc in c.values())

for k in range(n):
    m = c[a[k]]
    print(ans0-m+1)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
a_dict = {a: 0 for a in range(1, n + 1)}
for a in a_list:
    a_dict[a] += 1
base_ans = 0
for m in a_dict.values():
    base_ans += m * (m - 1) // 2
for a in a_list:
    m = a_dict[a]
    ans = base_ans - m + 1
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
l = [0 for i in range(n)]
for x in a:
  l[x-1]+= 1
sum = 0
for x in l:
  sum += x * (x - 1) // 2
for x in a:
  y = l[x-1]
  r = y * (y - 1) // 2 - (y - 1) * (y - 2) // 2
  print(sum - r)

Solution (Python):
def f(x):
	return x * (x-1) // 2

n = int(input())
a = list(map(int, input().split()))
b = [0] * n
c = [0] * n
d = [0] * n
e = [0] * n
s = 0
for aa in a:
	b[aa-1] += 1
for i in range(n):
	c[i] = f(b[i])
	d[i] = f(b[i] - 1)
	s += c[i]
for i in range(n):
	e[i] = s - c[i] + d[i]

for i in range(n):
	print(e[a[i]-1])

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
sa=sorted(a)
count=1
k=[0]*(n+2)
for i in range(n-1):
  if sa[i+1]==sa[i]:
    count+=1
  else:
    k[sa[i]]=count
    count=1
k[sa[-1]]=count
ans=0
for i in range(len(k)):
  ans+=k[i]*(k[i]-1)/2 
for i in a:
  print(int(ans-(k[i]*(k[i]-1)/2)+((k[i]-1)*(k[i]-2)/2)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
r = [0 for i in range(n+1)]
t = [0 for i in range(n+1)]
for ai in a:
    r[ai] += 1
def c2(n):
    return n * (n-1) // 2
t = list(map(lambda i:c2(i-1), r))
r = list(map(c2, r))
s = sum(r)
for i in range(n):
    l = a[i]
    print(s - r[l] + t[l])

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
d = {}
for i in A:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
summ = 0
for i in d:
    summ += d[i] * (d[i] - 1) // 2
for i in A:
    print(summ - d[i] + 1)

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ball = dict()
    for i in range(n):
        if a[i] in ball:
            ball[a[i]] += 1
        else:
            ball[a[i]] = 1

    ans = 0
    for b in ball:
        if ball[b] > 1:
            ans += ball[b] * (ball[b]-1) // 2

    for i in range(n):
        if a[i] in ball and ball[a[i]] > 1:
            tmp = ans
            tmp -= ball[a[i]] * (ball[a[i]]-1) // 2
            tmp += (ball[a[i]]-1) * (ball[a[i]]-2) // 2
            print(tmp)
        else:
            print(ans)

Solution (Python):



def main():
  n = int(input())
  la = list(map(int, input().split(' ')))
  counts = [0] * (n+1)
  
  def f(n):
    return int((n * (n-1)) / (2))
  
  for a in la:
    counts[a] += 1
    
  ans = 0
  for count in counts:
    if count >= 2:
      ans += f(count)
      
  for a in la:
    if counts[a] >= 2:
      fc = f(counts[a])
      count = counts[a]
      print(ans - fc + int(fc * ((count-2) / count)))
    else:
      print(ans)    

if __name__=='__main__':
  main()

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


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


def MS(): return map(str, input().split())


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


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def cmb(n, r):
    p = 1
    c = 1
    for i in range(r):
        p *= (n - i)
        c *= (i + 1)
    return p // c


def main():
    N = I()
    A = LI()
    a = Counter(A)
    c = 0
    org = {}
    one = {}
    org_tital = 0
    for k, v in a.items():
        org[k] = cmb(v, 2)
        one[k] = cmb(v-1, 2)
        org_tital += org[k]

    

    for i in range(N):
        diff = org[A[i]] - one[A[i]]
        print(org_tital - diff)
    


if __name__ == '__main__':
    main()

Solution (Python):
import copy
x=int(input())
y=list(map(int, input().split()))
z=copy.deepcopy(y)
y.sort()
n=[0]*(len(y)+1)
t=0
ans=0
for i in range(len(y)):
	t=y[i]
	n[t]=n[t]+1

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

for i in range (len(z)):
	t=z[i]
	k=n[t]
	print(ans-((k*(k-1))//2)+(((k-1)*(k-2))//2))

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

tmp = {}
for i, a in enumerate(A):
  if a not in tmp: tmp[a] = 0
  tmp[a] += 1

total = 0
map = {}
for a, v in tmp.items():
  n = v * (v - 1) // 2
  total += n
  map[a] = (n, n - max(0, (v - 1) * (v - 2) // 2))

for k in range(N):
  a = A[k]
  print(total - map[a][1])

Solution (Python):
from collections import Counter
n = int(input())

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

c = Counter(la)

total = 0
for i, num in c.most_common():
  if num > 1:
    total += num * (num-1) // 2
  
for ai in la:
  n = c[ai]
  print(total-n+1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
m = [0] * n
for i in a:
    m[i - 1] += 1
ans = 0
for i in m:
    if i >= 2:
        ans += i * (i - 1) // 2
for i in a:
    if m[i - 1] >= 2:
        print(ans - m[i - 1] * (m[i - 1] - 1) // 2 + (m[i - 1] - 1) * (m[i - 1] - 2) // 2)
    else:
        print(ans)

Solution (Python):
from collections import Counter

def n_combi_2(n):
    if n < 2:
        return 0
    return n * (n - 1) // 2
  
N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)

patterns_by_number = {key: n_combi_2(counter[key]) for key in counter.keys()}
total = sum(patterns_by_number.values())
res_dict = {}
for a in A:
    if a in res_dict.keys():
        print(res_dict[a])
    else:
        res = total - patterns_by_number[a] * (2) // counter[a]
        res_dict[a] = res
        print(res)

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

dic = {}
for v in a:
  tmp = dic.get(v, 0) + 1
  dic[v] = tmp
  
ans_dic1 = {}
ans_dic2 = {}
for k in dic.keys():
  tmp = dic.get(k)
  ans_dic1[k] = tmp * (tmp-1) // 2
  ans_dic2[k] = (tmp-1) * (tmp-2) // 2
  
plus = sum(ans_dic1.values())


for i in range(n):
  ans = plus - ans_dic1[a[i]] + ans_dic2[a[i]]
  print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
num_list = [0]*(2*(10**5)+10)       
  
for i in A:
  num_list[i] += 1

sum_all = 0
for i in range(2*(10**5)+10):
  n = num_list[i]          
  sum_all += n*(n-1)//2 
  
for i in A:  
  n = num_list[i]          
  ans = sum_all - n*(n-1)//2 + (n-1)*(n-2)//2
  print(ans)

Solution (Python):
from collections import Counter

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

def nC2(n):
    if n < 2:
        return 0
    return n * (n-1) // 2

def solve(N, A):
    counter = Counter(A)
    nc2_sum = sum([nC2(n) for _, n in counter.items()])
    for k in range(N):
        n = counter[A[k]]
        u = nc2_sum - nC2(n) + nC2(n-1)
        print(u)


if __name__ == '__main__':
    inputs = read()
    solve(*inputs)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
mx=max(a)
cnt=[0 for _ in range(mx)]
ans=[0 for _ in range(mx)]
for x in a:
  cnt[x-1]+=1
prm=0
for x in cnt:
  if x>1:
    prm+=x*(x-1)//2



for x in a:
  z=cnt[x-1]
  if z==1:
    print(prm)
  else:
    print(prm-z+1)

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

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

dic = {}
for A in As:
    if A in dic.keys():
        dic[A] += 1
    else:
        dic[A] = 1

total = 0
for A in dic.keys():
    a = dic[A]
    if a > 1:
        temp = a*(a-1)/2
        total += temp

for i in range(N):
    Ai = As[i]
    if dic[Ai] == 2:
        print(int(total-1))
    elif dic[Ai] > 2:
        a = dic[Ai]
        diff = (a*(a-1) - (a-1)*(a-2))/2
        print(int(total - diff)) 
    else:
        print(int(total))

Solution (Python):
from collections import defaultdict

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

cnt = defaultdict(int)
for i, a in enumerate(A):
    cnt[a] += 1

res = 0
for val, num in cnt.items():
    res += num * (num - 1) // 2

for a in A:
    ans = res
    num = cnt[a]
    ans -= num * (num - 1) // 2
    ans += max((num - 1) * (num - 2) // 2, 0)
    print(ans)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
num = {}
for a in A:
    if a in num:
        num[a] += 1
    else:
        num[a] = 1
total = 0
for a in num:
    n = num[a]
    total += int(n * (n-1) / 2)
for a in A:
    print(total - num[a] + 1)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

c = Counter(A)
c_c = defaultdict(int)
for key, value in c.items():
    c_c[key] = value * (value - 1) // 2
wa = 0
for key, value in c_c.items():
    wa += value

ruiseki = [0] * N

for i in range(N):
    new_value = c[A[i]] - 1    
    ruiseki[i] = wa - c_c[A[i]] + new_value * (new_value - 1) // 2
    
    
print(*ruiseki, sep= "\n")

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

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

s0 = sum([d[k] * (d[k] - 1) // 2 for k in d.keys()])

def c2(i):
    if i == 1:
        return 0
    else:
        return i * (i-1) // 2


for i in range(n):
    v = d[a[i]]
    print(s0 - c2(v) + c2(v-1))

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    A = tuple(iim())

    d1 = dict()
    for ai in A:
        if ai in d1:
            d1[ai] += 1
        else:
            d1[ai] = 1

    aa = []
    cnt = 0
    d2 = dict()
    for k, v in d1.items():
        v = v * (v-1) // 2
        cnt += v
        d2[k] = v

    for ak in A:
        ans = 0
        v = d1[ak] - 1
        v = v * (v-1) // 2
        aa.append(cnt-d2[ak]+v)
    print(*aa, sep="\n")


if __name__ == "__main__":
    resolve()

Solution (Python):
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
C = Counter(A)
t = {k: int(v*(v-1)/2) for k, v in C.items()}
s = sum(t.values())

for a in A:
    res = s - t[a] + int((C[a]-1)/2*(C[a]-2))
    print(res)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

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

def cnt_ans(ball_sets):
    cnt = 0
    for ball_set in ball_sets:
        if ball_set > 1:
            cnt += ball_set * (ball_set - 1) // 2
    return cnt
    
basic_ans = cnt_ans(ball_dic.values())

for i in range(N):
    n = ball_dic[A[i]]
    if n > 1:
        ans = basic_ans - (ball_dic[A[i]] - 1)
    else:
        ans = basic_ans
    print(ans)

Solution (Python):
from collections import defaultdict



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

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

sel = defaultdict(int)
total = 0
for k, v in cnt.items():
    total += v*(v-1)//2

for i in range(N):
    a = A[i]
    c = cnt[a]
    before = c*(c-1)//2
    after = (c-1)*(c-2)//2
    print(total-before+after)

Solution (Python):


import itertools
from math import factorial
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7


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

Solution (Python):
def main():
    import collections

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

    balls_dict = collections.Counter(balls)
    num_patterns = 0
    for value in balls_dict.values():
        num_patterns += value * (value-1) // 2

    for ball in balls:
        print(num_patterns - balls_dict[ball] + 1)


if __name__ == '__main__':
    main()

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

cnt = {}
for ai in a:
    if ai in cnt:
        cnt[ai] += 1
    else:
        cnt[ai] = 1
s = sum([v * (v - 1) // 2 for _, v in cnt.items()])
dic = { k: s - (v - 1) for k, v in cnt.items() }
ans = "\n".join(map(lambda ai: str(dic[ai]), a))
print(ans)

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().strip().split()))
C = Counter(A)
ans = 0
for k, v in C.items():
    ans += v*(v-1)//2

for a in A:
    count = C[a]
    if count < 2:
        print(ans)
    else:
        print(ans-count+1)

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

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

cnt = 0

for i in range(len(l)):
  cnt += l[i]*(l[i]-1)//2

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

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

N = INT()
A = LIST()

C = Counter(A)
total = 0
for k, v in C.items():
    total += nC2(v)

ans = [0] * N
for i, a in enumerate(A):
    ans[i] = total - nC2(C[a]) + nC2(C[a]-1)
[print(a) for a in ans]

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

cnt=[0]*(n+1)
s=0

for x in a:
    cnt[x]+=1

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

for k in range(1,n+1):
    if cnt[a[k-1]]>=2:
        print(s-cnt[a[k-1]]+1)
    else:
        print(s)

Solution (Python):
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    N = get_input(int)
    A = get_inputs(int)

    counter = Counter(A)

    sigma = 0
    for n in counter.values():
        sigma += n * (n-1) // 2

    for a in A:
        print(sigma - (counter[a] - 1))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

    cache = {}
    D = {}

    total = 0
    for a, c in C.items():
        if c == 1:
            D[a] = 0
            continue
        choose = c * (c-1) // 2
        D[a] = choose
        total += choose

    for k in range(N):
        a = A[k]
        if a in cache:
            print(cache[a])
            continue
        
        c = C[a]
        x = total
        x -= D[a]
        if c >= 3:
            x += (c-1) * (c-2) // 2

        cache[a] = x
        print(x)
solve()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = dict()
c = dict()
d = dict()
for i in range(n):
    if a[i] not in b:
        b[a[i]] = 0
    b[a[i]] += 1
b = list(b.items())
for i in range(len(b)):
    c[b[i][0]] = b[i][1] * (b[i][1]-1) // 2
    d[b[i][0]] = (b[i][1]-1) * (b[i][1]-2) // 2
c_sum = sum(list(c.values()))
for i in range(n):
    num = a[i]
    print(c_sum - c[num] + d[num])

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
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)]

from collections import defaultdict
def main():
    N = int(input())
    A = list(map(int, input().split()))
    counter = defaultdict(int)
    for a in A:
        counter[a] += 1

    total = 0
    for key, ct in counter.items():
        if ct > 1:
            total += cmb(ct, 2)
    
    for a in A:
        ans = total
        ct = counter[a]
        if ct > 1:
            before = cmb(ct, 2)
            after  = before * (ct-2) // ct
            ans -= (before-after)
        print(ans)

    

main()

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

import collections

c = collections.Counter(A)

sum_all = sum([v * max(v - 1, 0) // 2 for k, v in c.items()])

ans = []
for a in A:
  b = c[a]
  ans_a = sum_all - (b * max(b - 1, 0) // 2) + (max(b - 1, 0) * max(b - 2, 0) // 2)
  ans.append(ans_a)
print(*ans, sep='\n')

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
Nums = [0 for i in range(N)]
for i in A:
    Nums[i-1] += 1

NormalAns = [i*(i-1)//2 for i in Nums]
AnotherAns = [((i-1)*(i-2)//2 if i-2>=0 else 0) for i in Nums]
Ans = sum(NormalAns)
for i in A:
    print(Ans-NormalAns[i-1]+AnotherAns[i-1])

Solution (Python):
n = int(input())
A =list(map(int, input().split()))
d = {}

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

d2 = {}
d3 = {}
for k, v in d.items():
    if v < 2:
        d2[k] = 0
    else:
        d2[k] = v*(v-1)//2
    if v-1 < 2:
        d3[k] = 0
    else:
        d3[k] = (v-1)*(v-2)//2
s = sum(list(d2.values()))

for i in range(n):
    print(s-d2[A[i]]+d3[A[i]])

Solution (Python):
from collections import Counter

n = int(input())
k_list = [int(w) for w in input().split()]
c = Counter(k_list)


max_pat = 0
for n in c.values():
    max_pat += (n * (n - 1)) // 2

for i in k_list:
    cnt = c[i]
    tmp = max_pat

    if cnt >= 2:
        tmp -= (cnt * (cnt - 1)) // 2
        tmp += ((cnt - 1) * (cnt - 2)) // 2

    print(tmp)

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


def n2(n):
  if n == 0:
    return 0
  elif n == 1:
    return 0
  else:
    return int(n * (n - 1) / 2)

all_ans = 0
x = -1
count = 0
for i in range(N):
  if B[i] == x:
    count += 1
  else:
    all_ans += n2(count)
    x = B[i]
    count = 1
  
  
all_ans += n2(count)    

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

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

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

import collections

C = collections.Counter(A)

t = 0

for i in C.values():
    t += i * (i - 1) //2

for i in A:
    print(t - C[i] + 1)

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)
for num in nums:
    d[num] += 1

all_sum = 0
diff_d = {}
for num, freq in d.items():
    
    all_sum += freq * (freq-1) // 2
    
    tmp = freq - 1
    diff_d[num] = tmp

for num in nums:
    print(all_sum - diff_d[num])

Solution (Python):
from collections import defaultdict
def comb(n,k):
    n1, k = n+1, min(k, n-k)
    numer = denom = 1
    for i in range(1,k+1):
        numer = numer*(n1-i)
        denom = denom*i
    return numer//denom
def main():
    N = int(input())
    a = list(map(int,input().split()))
    d = defaultdict(int)
    for A in a:
        d[A] += 1
    S = defaultdict(int)
    total = 0
    for v in d.items():
        key = v[0]
        n = v[1]
        if n < 2:S[key] = 0
        else:S[key] = comb(n,2)
        total += S[key]
    for A in a:
        m = d[A]-1
        if m < 2:
            print(total-S[A])
        else:
            t = comb(m,2)
            print(total-S[A]+t)
        
    
if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    if A[i] not in C:
        C[A[i]] = 0
    C[A[i]] += 1
K = 0
for j in C:
    K += (C[j]*(C[j]-1))//2
for k in range(N):
    j = A[k]
    print(K-C[j]+1)

Solution (Python):
if __name__ == '__main__':
    def comb(n, k):
        a = n * (n - 1)
        return a // 2
    n = int(input())
    a = list(map(int, input().split()))

    count = {}
    
    for i in a:
        count[i] = count.get(i, 0) + 1

    all_sum = 0
    
    for i in count.values():
        all_sum += comb(i, 2)

    
    for i in a:
        ans = all_sum - comb(count[i], 2) + comb(count[i] - 1, 2)
        print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import Counter
n=int(input())
a=[int(i) for i in input().split()]
d=Counter(a) ;ans=0
for i in d:
    ans+=(d[i]*(d[i]-1))//2
for i in a:
    print(ans-(d[i]-1))

Solution (Python):
N = [int(i) for i in input().split()][0]
An = [int(i) for i in input().split()]

counts = [0 for _ in range(N)]
for a in An:
  counts[a-1] += 1
  
total = 0
for c in counts:
  if c >= 2:
    total += c * (c - 1) // 2

for a in An:
  c = counts[a-1]
  if c >= 2:
    minus = c * (c-1)//2  - (c-1)*(c-2)//2
  else:
    minus = 0
  print(total - minus)

Solution (Python):
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)

def main():
    N = int(input())
    A = list(map(int,input().split()))
    A_sorted = sorted(A)
    count = [0] * (N+1)

    pre = A_sorted[0]
    count[pre] = 1
    for i in range(1,N):
        if pre == A_sorted[i]:
            count[pre] += 1
        else:
            pre = A_sorted[i]
            count[pre] = 1

    sum = 0
    for i in range(len(count)):
        sum += count[i] * (count[i]-1) // 2

    for a in A:
        minus = count[a] * (count[a]-1) // 2
        plus = (count[a]-1) * (count[a]-2) // 2
        print(sum + plus - minus)




if __name__ == "__main__":
  main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A_set = list(set(A))
l = [0]*(N+2)
ans = [0]*(N+2)
for a in A:
  l[a]+=1
comb = [0]*(N+2)
for i in range(2,N+1):
  comb[i] = i*(i-1)//2
  
for i in range(2,N+1):
  ans[i]=comb[i]-comb[i-1]
  
total = 0
for a in A_set:
  total += comb[l[a]]

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

Solution (Python):
import collections
n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
d = c.copy()
ans_1 = 0
for v in d.values():
  v = int(v)
  ans_1 += v*(v-1)//2
for k in li:
  print(ans_1 - (c[k]-1))

Solution (Python):

N = int(input())
A = list(map(int,input().split()))
L = [0]*N
S = 0
for i in range(N):
    L[A[i]-1] =  L[A[i]-1] + 1

for i in range(N):
    if L[i] != 0:
        S += (L[i] * (L[i]-1)) // 2

for i in range(N):
    n = A[i]
    S2 = S
    S2 = S2 - ((L[A[i]-1] * (L[A[i]-1]-1)) // 2)
    if L[A[i]-1] - 1 > 0:
        S2 += (((L[A[i]-1]-1) * (L[A[i]-1]-2)) // 2)
    print(S2)

Solution (Python):

import sys
from collections import Counter

def comb(n, k):
    if n <= 1:
        return 0
    ans = 1
    for i in range(1, k + 1):
        ans = (ans * (n + 1 - i) // i)
    return ans

def solve(N: int, A: "List[int]"):
    counter = dict(Counter(A))

    MAX = 0
    for _,value in counter.items():
        MAX += comb(value,2)

    for i in range(N):
        value = counter[A[i]]

        print(MAX-comb(value,2)+comb(value-1,2))



    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    total = 0
    for i in C.values():
        if i == 1:
            continue
        total += i*(i-1)//2
    
    for i in range(N):
        a = C[A[i]] - 1
        print(total - a)


if __name__ == '__main__':
    main()

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
def com(x):
    return(x*(x-1)//2)
temp = []
for i in range(0, 2*10**5 + 1):
    temp.append(com(c[i]))
temp_sum = sum(temp)
for i in A:
    print(temp_sum-temp[i]+com(c[i]-1))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
dic = {}
for i in range(N):
    if A[i] in dic:
        dic[A[i]] += 1
    else:
        dic[A[i]] = 1
cnt = 0
for key in dic:
    cnt += dic[key]*(dic[key]-1)//2
for i in range(N):
    print (cnt-dic[A[i]]+1)