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=int(input())
a=list(map(int,input().split()))

time={}
zentai=0
for i in range(n):
  if a[i] in time:
    zentai+=time[a[i]]
    time[a[i]]+=1
  else:
    time[a[i]]=1

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
num_A = [0]*(N+1)
for a in A:
  num_A[a]+=1

s=0
for n in num_A:
  s+=n*(n-1)//2

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

Solution (Python):
















def main():

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

    count = [0 for _ in range(N + 1)]
    comb = [0 for _ in range(N + 1)]

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

    for i in range(1, N + 1):
        comb[i] = count[i] * (count[i] - 1) * 0.5

    all_comb = sum(comb)
    
    
    

    for i in range(1, N + 1):
        num = A[i - 1]
        ans = all_comb - comb[num] + (count[num] - 1) * (count[num] - 2) * 0.5
        print(int(ans))


if __name__ == '__main__':
    main()

Solution (Python):
import math
n = int(input())
a = list(map(int,input().split()))
mx = max(a)
b=[0] * 2000000
ans = 0
for i in range(len(a)):
    b[a[i]-1] += 1

for i in range(mx):
    if(b[i] == 0 or b[i] == 1):
        ans += 0
    else:
        ans = ans + int(b[i] * (b[i]-1)/2)
ans1 = 0


for i in range(len(a)):
    ans1 = ans
    if(b[a[i]-1] >2):
        ans1 = ans1 -int((b[a[i]-1]*(b[a[i]-1]-1))/2) + int(((b[a[i]-1]-1) * (b[a[i]-1]-2))/2)
        print(int(ans1))
    elif(b[a[i]-1] == 2):
        print(int(ans1-1))
    elif(b[a[i]-1] < 2):
        print(int(ans1))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import Counter 
def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    d = Counter(A)
    pans = 0
    for v in d.values():
        pans += v*(v-1)//2
    for k in A:
        tmp = pans
        v = d[k]
        tmp -= v*(v-1)//2
        v -= 1
        tmp += v*(v-1)//2 
        print(tmp)
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)
B.append(0)
p = B[0]
y = dict()
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        y[p] = u
        u = 0
    u += 1
    p = q
All = 0
for l in y.values():
    All +=l*(l-1)//2
for j in range(N):
    x = A[j]
    print(All+(y[x]-1)*(y[x]-2)//2-y[x]*(y[x]-1)//2)

Solution (Python):
from collections import defaultdict
from copy import copy
n = int(input())
a = [int(i) for i in input().split()]
count_a = defaultdict(int)
diff_a = [0] * n
for ai in a:
        count_a[ai] += 1

for i in range(n):
    diff_a[i] = (count_a[a[i]] - 1)

count = 0    
for v in count_a.values():
    count += v*(v - 1) // 2
for i in range(n):
    count_copy = copy(count)
    print(count_copy - diff_a[i])

Solution (Python):
from collections import defaultdict
from copy import copy
n = int(input())
a = [int(i) for i in input().split()]
count_a = defaultdict(int)
diff_a = [0] * n
for ai in a:
        count_a[ai] += 1

for i in range(n):
    diff_a[i] = (count_a[a[i]] - 1)

count = 0    
for v in count_a.values():
    count += v*(v - 1) // 2
for i in range(n):
    count_copy = copy(count)
    print(count_copy - diff_a[i])

Solution (Python):
import math

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

def combi(n):
    if n == 1: return 0
    return math.factorial(n) // (math.factorial(n - 2) * 2)

a2 = sorted(a)
a2.append(n+1)
dic = {}
n = 0
cnt = 1
total = 0
for i in a2:
    if n != i:
        if cnt > 1:
            dic[str(n)] = cnt
            total += combi(cnt)
        n = i
        cnt = 1
    else:
        cnt += 1

for i in a:
    if str(i) in dic:
        cnt = dic[str(i)]
        print(total - cnt + 1)
    else:
        print(total)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Cs = {}
for A in As:
  if A not in Cs:
    Cs[A] = 0
  Cs[A] += 1

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

for A in As:
  C = Cs[A]
  r = r0-C*(C-1)//2+(C-1)*(C-2)//2
  print(r)

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

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

ans = 0
for i in C.values():
    ans += int(0.5* i * (i-1))
for j in A:
    print(ans-C[j]+1)

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)






def run():
    N = int(sysread())
    A = list(map(int, sysread().split()))
    count = [0] * (N+1)
    for val in A:
        count[val] += 1
    base_sum = 0
    for val in count:
        if val <= 1:continue
        base_sum += val * (val-1) // 2
    for i in range(N):
        if count[A[i]] == 1:
            print(base_sum)
        else:
            print(base_sum - count[A[i]] + 1)
if __name__ == "__main__":
    run()

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

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

from collections import defaultdict

d = defaultdict(int)
for i in A:
    d[i] += 1
max = sum([d[i]*(d[i]-1)//2 for i in range(1,N+1)])
ans = [max - d[i] + 1 for i in A]

for x in ans:
    print(x)

Solution (Python):
from collections import defaultdict
def banned_k(n,a):
  d=defaultdict(int)
  for x in a:
    d[x]+=1
  total = sum([d[i]*(d[i]-1)//2 for i in range(1,n+1)])
  ans = [total - d[x] + 1 for x in a]
  return ans

n = int(input())
a = list(map(int,input().split()))
ans = banned_k(n,a)
for x in ans:
  print(x)

Solution (Python):
from collections import Counter
N = int(input())
A=list(map(int,input().split()))
B=Counter(A)
tmp=0
for i in B.values():
    tmp+=int(0.5*(i)*(i-1))
    
for i in A:            
    print(tmp-B[i]+1)

Solution (Python):
from collections import Counter

def combination2(n):
    return int(n * (n-1) / 2)

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

c = Counter(a)
s = 0

for i in c.values():
    s += combination2(i)

for i in a:
    n = c[i]
    ans = s - combination2(n) + combination2(n-1)
    print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
ans = 0
d = defaultdict(int)

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

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

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

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
ans = 0
c = Counter(A)

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

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

Solution (Python):
import collections

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

c = collections.Counter(la)
lv = list(c.values())
lvmin = [i-1 for i in lv]

s = sum(i*(i-1)//2 for i in lv)

for i in range(N):
    ans = s - c[la[i]] + 1
    print(ans)

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

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

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
c = [0 for _ in range(n)]
count = 0
for i in range(n):
  c[a[i]-1] += 1
for i in range(n):
  if(c[i] >= 2):
    count += int(c[i]*(c[i]-1)/2)

for i in range(n):
  sum = count
  now = a[i]
  now_c = c[now-1]
  sum -= now_c-1
  print(sum)

Solution (Python):


import math

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

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

cnb=[0 for i in range(n)]
sum=0
for i in range(n):
    if count[i]>=2:
        cnb[i]=int(count[i]*(count[i]-1)/2)
        sum+=cnb[i]





for k in range(n):
    if count[a[k]-1]<2:
        print(sum)
    else:
        print(sum-cnb[a[k]-1]+int((count[a[k]-1]-1)*(count[a[k]-1]-2)/2))

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
cnt = collections.Counter(a)
total = 0
for key in cnt.keys():
    total += cnt[key]*(cnt[key]-1) // 2
for i in a:
    ans = total
    ans -= cnt[i] * (cnt[i]-1) / 2
    ans += (cnt[i]-1) * (cnt[i]-2) / 2
    print(int(ans))

Solution (Python):
import sys
from scipy.misc import comb

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() 
ni = lambda: int(readline().rstrip()) 
nm = lambda: map(int, readline().split()) 
nl = lambda: list(map(int, readline().split())) 

n = ni()
a = nl()

count = [0 for _ in range(n+1)]
for i in a:
    count[i] += 1
ans_ele = 0
for i in range(1, n+1):
    
    if count[i] >= 2:
        ans_ele += comb(count[i], 2, exact=True)

for i in range(n):
    if count[a[i]] <= 1:
        print(ans_ele)
    elif count[a[i]] == 2:
        print(ans_ele - 1)
    else:
        print(ans_ele - comb(count[a[i]], 2, exact=True) + comb(count[a[i]]-1, 2, exact=True))

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

from collections import defaultdict
dict = defaultdict(int)
for a in A:
    dict[a] += 1

ans = 0
for key, value in list(dict.items()):
    ans += value*(value-1) // 2

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
d=dict()
for i in range(n):
    if l[i] not in d:
        d.update({l[i]:1})
    else:
        d[l[i]]+=1
ans=0
for j in d:
   ans+=(d[j]*(d[j]-1))//2
for i in range(n):
    k=l[i]
    tot=ans+1-d[k]
    print(tot)

Solution (Python):
from collections import defaultdict
from pprint import pprint


n = int(input())

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

a_dict = defaultdict(int)

for i in a_list:
    a_dict[i] += 1








total = 0


for j in a_dict.values():
    total += j*(j-1)/2

for k in a_list:
    print(int(total - (a_dict[k]-1)))

Solution (Python):
from scipy.misc import comb
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)


ans = 0
for k,v in C.items():
  ans += v * (v-1) // 2
  
for a in A:
  cnt = C[a]
  if cnt > 0:
    print(ans - cnt*(cnt-1)//2 + (cnt-1)*(cnt-2)//2)
  else:
    print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
d={}
for i in a:
  d[i]=d.get(i,0)+1
ans=sum([i*(i-1)//2 for i in d.values()])
for i in a:
    print(ans+1-d[i])

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    nums = [0] * (N+1)
    for i in A:
        nums[i] += 1
    
    comb = sum((i*(i-1)//2 for i in nums))
    for k in range(N):
        print(comb-(nums[A[k]]-1))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int,input().split()))
balls = [[] for _ in range(N+1)]
for i in range(N):
  balls[A[i]].append(i)
  
K = 0  
for i in range(N+1):
  if len(balls[i]) < 2:
    continue
  else:
    u = len(balls[i])
    K += int(u*(u-1)/2)
  
t = 0
q = deque()
for i in range(N):
  s = A[i]
  a = len(balls[s])
  if a >=2:
    d = a-1
  else:
    d = 0
  q.append(K-d)


while q:
  v = q.popleft()
  print(v)

Solution (Python):
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools

def solve():
  n = int(input())
  a = list(map(int, input().split()))
  c = collections.Counter(a)
  s = 0
  for i in list(c.values()):
    s += i*(i-1)//2
  for i in a:
    print(s-(c[i]-1))
  return 0
 
if __name__ == "__main__":
  solve()

Solution (Python):
from collections import defaultdict
from math import factorial

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

number = defaultdict(int)
for a in A:
    number[a] += 1

def combination(n, r):
    denominator = 1
    for i in range(n, n - r, -1):
        denominator *= i
    numerator = factorial(r)
    return denominator // numerator

combi = {}
combi2 = {}
for k, v in number.items():
    combi[k] = combination(v, 2)
    combi2[k] = combination(v - 1, 2)
combination_all = sum(combi.values())


for a in A:
    print(combination_all - (combi[a] - combi2[a]))

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    from collections import Counter
    c=Counter(l)
    cnt=0
    for i in c.values():
        if i>=2:
            cnt+=i*(i-1)//2
    for j in l:
        x=(c[j]-1)*(c[j]-2)//2-c[j]*(c[j]-1)//2
        print(cnt+x)
resolve()

Solution (Python):
from operator import mul
from functools import reduce
import collections

def combinations_count(n, r):
    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

def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = combinations_count(c,2)
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Solution (Python):
n = int(input())
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
cnt = 0
for k, v in d.items():
    if v > 1:
        cnt += v * (v - 1) // 2
for a in L:
    if d[a] >= 2:
        print(cnt - d[a] * (d[a] - 1) // 2 + ((d[a] - 1) * (d[a] - 2)) // 2)
    else:
        print(cnt)

Solution (Python):
N = int(input())
Data = list(map(int,input().split()))
dic = {}
for d in Data:
    if not d in dic.keys():
        dic[d] = 0
    dic[d]+=1

allsum = 0
for d in dic.values():
    allsum += d*(d-1)//2
for d in Data:
    n = dic[d]
    ans = allsum - n*(n-1)//2
    if n > 1:
        ans += (n-1)*(n-2)//2
    print(ans)

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

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
d = {}
for i in l:
    if i in d:
        d[i]+=1
    else:
        d[i] = 1
cp = 0
for i in d.values():
    cp+= i*(i-1)//2
for i in l:
    print(cp-d[i]+1)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
d = {}
for i in l:
    if i in d:
        d[i]+=1
    else:
        d[i] = 1
cp = 0
for i in d.values():
    cp+= i*(i-1)//2
for i in l:
    print(cp-d[i]+1)

Solution (Python):
import collections
def comb(n):
    return n*(n-1)//2

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

normal_cal_cnt = [0] * N
minus_cal_cnt = [0] * N
for i in range(N):
    normal_cal_cnt[i] = comb(cnt[i])
    minus_cal_cnt[i] = comb(cnt[i]-1)
ans = sum(normal_cal_cnt)
for i in range(N):
    tmp = ans
    tmp -= normal_cal_cnt[A[i]-1]
    tmp += minus_cal_cnt[A[i]-1]
    print(tmp)

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

a_dict = {}

for i in a:
    if i in a_dict:
        a_dict[i] += 1
    else:
        a_dict[i] = 1

total_com = 0
for i in a_dict.values():
    total_com += i*(i-1)//2

tmp = 0
for i in a:
    tmp = a_dict[i] - 1
    print(total_com - tmp)

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

Solution (Python):
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
counter_comb = list(map(lambda y: int(y*(y-1)/2), counter.values()))
sum_counter = sum(counter_comb)
for i in range(n):
    z = counter[A[i]]
    if z>0:
        print(sum_counter-(z-1))
    else:
        print(sum_counter)

Solution (Python):
import collections
n = int(input())
A = list(map(int,input().split()))
B = collections.Counter(A)
C = B.copy()
total = 0
for i in B.values():
    total += i*(i-1)//2
for i in A:
    print(total - B[i]+1)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def comb(n):
    return int(n*(n-1)/2)

def main():
    N = I()
    A_list = LI()
    A_ctr = collections.Counter(A_list)
    base = 0
    for i in range(1,N+1):
        base += comb(A_ctr[i])
    for i in range(1,N+1):
        i_val = A_ctr[A_list[i-1]]
        before = comb(i_val)
        after = comb(i_val-1)
        print(int(base - before + after))
main()

Solution (Python):
from collections import defaultdict


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


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

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

whole = sum(list(map(combi, list(d.values()))))

diff = []
for i in range(N):
    diff.append(combi(d[A[i]]) - combi(d[A[i]] - 1))

for x in diff:
    print(whole - x)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1
total = 0
for i in d.values():
    total += i*(i-1)//2
for i in range(n):
    diff = d[a[i]]*(d[a[i]]-1)//2 - (d[a[i]]-1)*(d[a[i]]-2)//2
    print(total - diff)

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 v in b.values():
    total += v*(v-1)/2

for k in range(N):
    n = b[A[k]]
    ans = int(total - n*(n-1)/2 + (n-1)*(n-2)/2)
    print(ans)

Solution (Python):
from math import factorial

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

for i,a in enumerate(A):
  if a in dict:
    dict[a].append(i)
  else:
    dict[a]=[i]

ans = 0    
    
for k,v in dict.items():
  if len(v) >= 2:
    ans += len(v) * (len(v)-1) //2


  
for i in range(n):
  wnum = A[i]
  l=dict[wnum]

  if 2 < len(dict[wnum]):



    print(int(ans -(len(l) * (len(l)-1) //2) + (len(l)-1) * (len(l)-2) //2))
  elif 2 == len(dict[wnum]):
    print(int(ans - 1))
  else:
    print(int(ans))

Solution (Python):
import math
N=int(input())
A=list(map(int,input().split()))
B=sorted(A)
C=[]
D=[]
E=[]
F=[]
C.append(1)
D.append(B[0])
for i in range(1,N):
    if B[i]==B[i-1]:
        C[-1]+=1
    else:
        C.append(1)
        D.append(B[i])
for i in range(len(C)):
    if C[i]==1:
        E.append(0)
        F.append(0)
    else:
        if C[i]==2:
            E.append(1)
            F.append(0)
        else:
            E.append(int(math.factorial(C[i])/(math.factorial(2)*math.factorial(C[i]-2))))
            F.append(int(math.factorial(C[i]-1)/(math.factorial(2)*math.factorial(C[i]-3))))
e=sum(E)
import bisect
for i in range(N):
    s=bisect.bisect_left(D,A[i])
    print(e-E[s]+F[s])

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
CA = Counter(A)
all_count = 0
for ca in CA.values():
  all_count += ca*(ca-1)//2

for a in A:
  print(all_count - (CA[a]-1))

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
def fun(x):
    return x*(x-1)/2
d ={}
for i in range(n):
    if l[i] in d:
        d[l[i]] +=1
    else:
        d[l[i]] =1
total = 0
for i in d:
    total += fun(d[i])

for i in range(n):
    print(int(total-fun(d[l[i]])+fun(d[l[i]]-1)))

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

count_list = [0]*n

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

for i in count_list:
    tmp_ans += math.floor((i*(i-1))/2)

for i in range(n):
    exclution = a[i]
    diff = math.floor((count_list[a[i]-1]*(count_list[a[i]-1]-1))/2) - math.floor(((count_list[a[i]-1]-1)*(count_list[a[i]-1]-2))/2)
    print(tmp_ans-diff)

Solution (Python):
from collections import Counter


n = int(input())

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

c = Counter(a)


res = []

for i in range(1, n+1):
    m  = c.get(i, 0)
    res.append(m * (m-1) // 2)

s = sum(res)


for i in range(1, n+1):
    x = a[i-1]
    l = c.get(x, 0)


    print(s - res[x-1] + (l-1) * (l-2) // 2)

Solution (Python):
def nc2(n):
    return int(n*(n-1)/2)


def main():
    import sys
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    from collections import Counter as cc
    a_ = cc(a)
    sum_ = sum([nc2(k) for k in a_.values() if k > 1])
    for i in range(n):
        if a_[a[i]] < 2:
            print(sum_)
        else:
            print(sum_ - (a_[a[i]] -1))


if __name__ == '__main__':
    main()

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

c = Counter(a)
key = c.keys()
comb = 0
for i in key:
    comb += (c[i] * (c[i]-1)) // 2

for i in a:
    ans = comb - (c[i] - 1)
    print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))

    c_n = []
    for i in range(n + 1):
        c_n.append(i * (i - 1) // 2)

    sorted_a = sorted(a)
    g = {}
    count = 1
    tmp = 0
    for ai in sorted_a:
        if tmp == ai:
            count += 1
        else:
            g[tmp] = count
            count = 1
        tmp = ai
    g[tmp] = count

    ans = 0
    for k in g:
        ans += c_n[g[k]]

    for ai in a:
        print(ans - g[ai] + 1)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):

import sys
import time
import unittest
from io import StringIO
from collections import defaultdict


def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    dd = defaultdict(int)
    idx = 0
    cmb = [0] * (N + 1)
    for a in A:
        dd[a] += 1
        idx += 1
        cmb[idx] = cmb[idx - 1] + (idx - 1)
    sum = 0
    for j in dd.keys():
        sum += cmb[dd[j]]
    for i in range(N):
        print(sum - (cmb[dd[A[i]]] - cmb[dd[A[i]] - 1]))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

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

counter=[0]*(n+1)
for a in al:
    counter[a]+=1

sum=0
for c in counter:
    sum+=c*(c-1)//2

for a in al:
    print(sum-(counter[a]-1))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = [0] * (n + 1)
for i in range(n):
    cnt[a[i]] += 1
conb1, conb2 = [], []
for i in range(n + 1):
    conb1.append(cnt[i] * (cnt[i] - 1) // 2)
    if cnt[i] == 0:
        conb2.append(0)
    else:
        conb2.append((cnt[i] - 2) * (cnt[i] - 1) // 2)
sumall = sum(conb1)
for i in range(n):
    ans = sumall - conb1[a[i]] + conb2[a[i]]
    print(ans)

Solution (Python):
n = (int)(input())
a = list(map(int, input().split(" ")))
temp = [0 for i in range(n + 1)]
aaa = sorted(a)
count = 0
for i in range(n - 1):
  if aaa[i] == aaa[i + 1]:
    count+= 1
  else:
    temp[aaa[i]] = count + 1
    count = 0
else:
  if len(a) == 1:
    temp[aaa[0]] = 1
  else:
    temp[aaa[-1]] = count + 1
ans = 0




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




for i in range(n):
  kotae = ans
  if temp[a[i]] >= 3:
    
    
    kotae = ans - (temp[a[i]] * (temp[a[i]] - 1) / 2) + ((temp[a[i]] - 1) * (temp[a[i]] - 2) / 2)
  elif temp[a[i]] == 2:
    kotae = ans -1
  print((int)(kotae))

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

balls = {}

for a in A:
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1
res = 0
for b in balls:
  count = balls[b]
  res += count * (count-1) // 2

for a in A:
  res -= balls[a] * (balls[a]-1) // 2
  balls[a] -= 1
  res += balls[a] * (balls[a]-1) // 2
  print(res)
  res -= balls[a] * (balls[a]-1) // 2
  balls[a] += 1
  res += balls[a] * (balls[a]-1) // 2

Solution (Python):
import math
import sys

n = int(sys.stdin.readline().rstrip())
a = list(map(int, sys.stdin.readline().rstrip().split()))
num = [0] * n
for ai in a:
    num[ai - 1] += 1

c = 0
for n in num:
    c += n * (n - 1) // 2

for ai in a:
    print(c - num[ai-1] + 1)

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

cnt = [ 0 ] + [ 0 ] * n

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

all_combi = 0
for m in cnt:
    all_combi += m * ( m - 1 ) // 2

for k in range( 1, n+1 ):
    print( all_combi - ( cnt[a[k-1]] - 1 ) )

Solution (Python):
import math

n = int(input())
a_list = list(map(int,input().split()))
num = [0]*(n+1)
for a in a_list:
  num[a] += 1
c = 0
for n in num:
  if n != 0:
    c += n*(n-1)//2
for a in a_list:
  print(c-num[a]+1)

Solution (Python):
from collections import Counter
n = int(input())
su = 0
l = list(map(int,input().split()))
l1 = Counter(l)
for k in l1.values():
  su += k*(k-1)//2
for t in l:
  a = l1[t]
  q = su-a*(a-1)//2+(a-1)*(a-2)//2
  print(q)

Solution (Python):
from collections import Counter
N = int(input())
An = list(map(int, input().split()))
counter = Counter(An)
cnt = 0
for c in counter.values():
    cnt += c * (c-1) // 2
for a in An:
    print(cnt - (counter[a] - 1))

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

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

num_dict = {}
for a in a_list:
    num_dict.setdefault(a,0)
    num_dict[a] += 1

all_comb = 0
for num,cnt in num_dict.items():
    comb = cnt*(cnt-1)//2
    all_comb += comb

for a in a_list:
    a_cnt = num_dict[a]
    a_comb_diff = a_cnt*(a_cnt-1)//2 - (a_cnt-1)*(a_cnt-2)//2
    print(all_comb-a_comb_diff)

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

from collections import Counter		

C=Counter(a)

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

for k in range(n):
	x=C[a[k]]
	print(sum-(x*(x-1)//2)+((x-1)*(x-2)//2))

Solution (Python):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr(), dtype=np.int32)
counter = np.bincount(A)
case = (counter * (counter-1) // 2).sum()
answer = case - (counter[A] - 1)
print('\n'.join(answer.astype(str)))

Solution (Python):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr(), dtype=np.int32)
counter = np.bincount(A)
case = (counter * (counter-1) // 2).sum()
answer = case - (counter[A] - 1)
print('\n'.join(map(str, answer)))

Solution (Python):

import sys
from math import factorial
from collections import Counter, defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def comb(n, r):  
    if n <= 0 or r > n: return 0
    return factorial(n) // factorial(n - r) // factorial(r)

N = ir()
A = lr()
counter = Counter(A)
case = 0
dic = defaultdict(int)
for k, v in counter.items():
    dic[k] = v
    case += comb(v, 2)

for a in A:
    num = dic[a]
    answer = case - (num - 1)
    print(answer)

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

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

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

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

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

x = [0]*(n+1)
for i in range(1, n+1):
    x[a[i-1]] += 1
sumx = 0
for i in range(1, n+1):
    sumx += x[i] * (x[i]-1) // 2

for i in range(1, n+1):
    print(sumx - x[a[i-1]] * (x[a[i-1]]-1) // 2 + (x[a[i-1]]-1) * (x[a[i-1]]-2) // 2)

Solution (Python):
from collections import Counter
N = int(input())
An = list(map(int, input().split()))
counter = Counter(An)
cnt = 0
for c in counter.values():
    cnt += c * (c-1) // 2
for a in An:
    print(cnt - (counter[a] - 1))

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

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

cnt=[0]*(n+1)
for i in range(n):
    cnt[a[i]]+=1 
aa=0
ans=[]
for i in range(n+1):
    aa+=(cnt[i]*(cnt[i]-1)//2)
for i in range(n):
    print(aa-(cnt[a[i]]*(cnt[a[i]]-1))//2 +((cnt[a[i]]-1)*(cnt[a[i]]-2))//2, end ="\n")