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


from collections import  Counter
L = Counter(A)

original_answer = 0
for v in L.values():
    original_answer += ( v * (v-1) )// 2

for i in range(N):
    print(original_answer - (L[A[i]]*(L[A[i]]-1))//2 + ((L[A[i]]-1)*(L[A[i]]-2))//2)

Solution (Python):
import collections

num = int(input())
a_i = list(map(int, input().split()))
anslist = collections.Counter(a_i)

ans = 0
for x in anslist.values():
    ans += x * (x - 1) // 2
for x in range(num):
    val = a_i[x]
    cnt = anslist[val]
    print(ans - (cnt - 1))

Solution (Python):
import collections

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

ans=0
dic=collections.Counter(A)
keys=list(dic.keys())
values=list(dic.values())


for i in values:
    
    ans+=(i*(i-1))//2

for j in A:
    ANS=ans
    num=dic[j]
    ANS=ANS-(num-1)
    print(ANS)

Solution (Python):
from collections import Counter


def main():
    n = int(input())
    arr = list(map(int, input().split()))

    cnt = Counter(arr)
    total = 0
    for key in cnt.keys():
        total += cnt[key]*(cnt[key]-1)//2

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


if __name__ == "__main__":
    main()

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

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

se = set()
dic = dict()

for i in range(N):
    if A[i] in se:
        dic[A[i]] += 1
    else:
        se.add(A[i])
        dic.update({A[i]: 1})

c = 0
for k in dic.keys():
    c += (dic[k])*(dic[k]-1)//2

for i in range(N):
    print(c - dic[A[i]]+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
li = [0 for i in range(N)]

for i in range(N):
    li[A[i]-1] += 1

ans = 0
for i in range(N):
    ans += li[i] * (li[i]-1) // 2
for i in A:
    i2 = i-1
    print(ans - li[i2] * (li[i2]-1) // 2 + (li[i2]-1) * (li[i2]-2) // 2)

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

cnt = Counter(A)
cnt_key = list(cnt.values())

ans = 0

for i in cnt.values():
    ans += i*(i-1)//2

for j in A:
    m = cnt[j]
    print(ans - m*(m-1)//2 + (m-1)*(m-2)//2)

Solution (Python):
def CountFrequency_mod(my_list): 
  
    
    freq = {} 
    for item in my_list: 
        if (item in freq): 
            freq[item] += 1
        else: 
            freq[item] = 1
    
    freq_mod = freq
    freq_val = list(freq.values())
    all_sum=0
    
    for i in freq_val:
        all_sum+=i*(i-1)/2
        
    for i in freq:
        freq_mod[i] = all_sum - freq[i]*(freq[i]-1)/2 + (freq[i]-1)*(freq[i]-2)/2
    return freq_mod


N = int(input())

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

dic = CountFrequency_mod(A)

for i in A:
    print(int(dic[int(i)]))

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

for i in b.values():
  total += i*(i-1)//2
  
for i in a:
  c = b[i]
  print(total-(c*(c-1)//2)+((c-1)*(c-2)//2))

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

Solution (Python):
N = int(input())
*a,= map(int, input().split())
count={}

for i in range(N):
  count[a[i]]=0
  
def ncn(n):
  return n*(n-1)//2

for i in range(N):
  count[a[i]]+=1

total=0
for key in count.keys():
  kc = count[key]
  total += ncn(kc)
  
def without(k):
  return total-count[a[k]] + 1

for k in range(N):
  print(without(k))

Solution (Python):
N = int(input())
*a,= map(int, input().split())
count,result={},{}

for i in range(N):
  count[a[i]]=0
  result[a[i]]=0
  
def ncn(n):
  return n*(n-1)//2

for i in range(N):
  count[a[i]]+=1

total=0
for key in count.keys():
  kc = count[key]
  result[key]=ncn(kc)
  total += result[key]
  
def without(k):
  return total-count[a[k]] + 1

for k in range(N):
  print(without(k))

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

Solution (Python):
c=[0]*8**6
_,a=open(0)
*a,=map(int,a.split())
for i in a:c[i]+=1
s=sum(i*~-i//2for i in c)
for i in a:print(s-c[i]+1)

Solution (Python):
c=[0]*8**6
_,a=open(0)
*a,=map(int,a.split())
for i in a:c[i]+=1
s=sum(i*~-i//2for i in c)
for i in a:print(s-c[i]+1)

Solution (Python):
c=[0]*8**6
_,a=open(0)
*a,=map(int,a.split())
for i in a:c[i]+=1
s=sum(i*~-i//2for i in c)
for i in a:print(s-2*~-c[i]//2)

Solution (Python):

from collections import Counter


def main():
    N, *A = map(int, open(0).read().split())
    cnt = Counter(A)
    total = sum(v * (v - 1) // 2 for k, v in cnt.items())
    ans = [total - (cnt[a] - 1) for a in A]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter


def main():
    N, *A = map(int, open(0).read().split())
    counter = Counter(A)
    total = 0
    dif = {}
    for k, v in counter.items():
        total += v * (v - 1) // 2
        dif[k] = v - 1
    ans = [total - dif[a] for a in A]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter


def main():
    N, *A = map(int, open(0).read().split())
    counter = Counter(A)
    total = 0
    dif = {}
    for k, v in counter.items():
        total += max(0, v * (v - 1) // 2)
        dif[k] = max(0, v - 1)
    ans = [total - dif[a] for a in A]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter


def main():
    N, *A = map(int, open(0).read().split())
    counter = Counter(A)
    patterns, less_patterns = {}, {}
    for k, v in counter.items():
        patterns[k] = max(0, v * (v - 1) // 2)
        less_patterns[k] = max(0, (v - 1) * (v - 2) // 2)
    total = sum(patterns.values())
    ans = [total - patterns[a] + less_patterns[a] for a in A]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

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

A_counter = Counter(A)
ans = 0
for A_content in A_counter.values():
  ans += A_content * (A_content-1)//2


for i in range(len(A)):
  dif = 0
  if A_counter[A[i]] > 1:
    dif = A_counter[A[i]] - 1
  print(ans - dif)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
e=list(set(a))
s=0
t=[0]*200000
for i in a:
    t[i]+=1
for j in e:
    k=t[j]
    s+=(k*(k-1))//2
for u in a:
    print(s-t[u]+1)

Solution (Python):
import collections
import math
def P(n,r):
    return math.factorial(n) // math.factorial(n-r)
def C(n,r):
    return P(n,r) // math.factorial(r)
n = int(input())
a = list(map(str,input().split()))
c = collections.Counter(a)
s = 0
for i in list(c.values()):
    s += i*(i-1)//2
for i in a:
    ans = s - (int(c[str(i)] - 1))
    print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
aset=list(set(a))
ans=0
al=[0]*200000
for i in a:
    al[i]+=1
for j in aset:
    k=al[j]
    ans+=(k*(k-1))//2
for u in a:
    print(ans-al[u]+1)

Solution (Python):
import collections
def I(): return int(input())
def LI(): return list(map(int,input().split()))

N = I()
A = LI()
count = collections.Counter(A)
sum = 0
for x in count.values():
    sum += x*(x-1)//2
for x in A:
    temp = count[x]
    minus = temp*(temp-1)//2
    plus = (temp-1)*(temp-2)//2
    print(sum-minus+plus)

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  import collections
  c = collections.Counter(a)
  j = 0
  
  for i in range(len(s)):
    j += int(c[s[i]]*(c[s[i]]-1)/2)
  
  for i in range(n):
      print(int(j-(c[a[i]]-1)))
if __name__ == '__main__':
    main()

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

N = int(stdin.readline())
A = list(map(int, stdin.readline().split()))

cnt = Counter(A)


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


sumall = 0

for i in cnt.values():
    sumall += comb(i)

for a in A:
    ans = sumall - (cnt[a] - 1)
    print(ans)

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

Solution (Python):
import collections

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

num = [0]*(N+1)
for i in range(N+1):
    num[i] = d[i]

s = 0
for i in range(N+1):
    s += num[i]*(num[i]-1)//2
    
for i in range(1,N+1):
    print(s-(d[A[i-1]])+1)

Solution (Python):
import math
import collections


def kumiawase(n):
    if n == 0 or n == 1:
        return 0
    return (n**2 - n) // 2


n = int(input())
aL = list(map(int, input().split(" ")))

d = collections.Counter(aL)

s = 0
for i in d.values():
    s += kumiawase(i)

for a in aL:
    print(s - kumiawase(d[a]) + kumiawase(d[a] - 1))

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

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

a = int(input())
A = list(map(int, input().split()))  
d = {}
for k in A:
  if k in d:
    d[k] += 1
  else:
    d[k] = 1
 
res = 0
for k in d:
    if d[k] >= 2:
      res += ncr(d[k], 2)

for k in A:
  temp = res
  m = d[k]
  d[k] -= 1
  if m >= 2:
    temp -= ncr(m, 2)

  if d[k] >= 2:
    temp += ncr(d[k], 2)
  print(temp)
  d[k] += 1

Solution (Python):
def I(): return int(input())
def LI(): return list(map(int,input().split()))

N = I()
A = LI()
count = [0]*(N+1)
for x in A:
    count[x] += 1
nC2 = {}
sum = 0
for x in count:
    nC2[x] = x*(x-1)//2
    sum += nC2[x]
for x in count:
    if x-1 in nC2 or x==0:
        continue
    nC2[x-1] = (x-1)*(x-2)//2
for x in A:
    minus = nC2[count[x]]
    plus = 0 if count[x]==0 else nC2[count[x]-1]
    print(sum-minus+plus)

Solution (Python):

import math

def combination2(n, r):
    if n <= 1:
        return 0
    return math.factorial(n) / (math.factorial(r) * math.factorial(n-r))

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

    X = 1
    for i in range(n, max(r, n - r), -1):
        X *= i
    
    if n - r <= 0:
        return X

    Y = math.factorial(min(r, n - r))

    return X / Y

class UnionFindTree():
    def __init__(self, n):
        self.parent = [] 
        self.rank = [] 
        self.number = [] 
        for i in range(n):
            self.parent.append(i)
            self.rank.append(0)
            self.number.append(1)

    
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.number[y] += self.number[x]
        else:
            self.parent[y] = x
            self.number[x] += self.number[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def same(self, x, y):
        return self.find(x) == self.find(y)

    
    def member_number(self, x):
        x = self.find(x)
        return self.number[x]

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

uft = UnionFindTree(N)

vals = {}
for i in range(N):
    if A[i] in vals:
        uft.unite(vals[A[i]], i)
    else:
        vals[A[i]] = i

comb = {}
group_numbers = {}
total = 0
comb_memo = {}
for k, v in vals.items():
    group_number = uft.member_number(v)
    if group_number not in comb_memo:
        comb_memo[group_number] = combination(group_number, 2)
    comb[k] = comb_memo[group_number]

    group_numbers[k] = group_number
    total += comb[k]

ans = {}
for k, v in vals.items():
    if group_numbers[k] - 1 not in comb_memo:
        comb_memo[group_numbers[k] - 1] = combination(group_numbers[k] - 1, 2)
    ans[k] = int(total - comb[k] + comb_memo[group_numbers[k] - 1])

for a in A:
    print(ans[a])

Solution (Python):

import collections


def solve(n, balls):
    counts = collections.Counter(balls)

    totalWays = 0

    for count in counts.values():
        totalWays += count*(count-1)//2

    results = []

    for ball in balls:
        waysWithoutBall = totalWays - (counts[ball]*(counts[ball]-1)//2)

        results.append(waysWithoutBall +
                       ((counts[ball]-1)*(counts[ball]-2)//2))

    return results


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

results = solve(n, balls)

for result in results:
    print(result)

Solution (Python):
def I(): return int(input())
def LI(): return list(map(int,input().split()))

N = I()
A = LI()
count = [0]*(N+1)
for x in A:
    count[x] += 1
nC2 = {}
sum = 0
for x in count:
    nC2[x] = x*(x-1)//2
    sum += nC2[x]
for x in count:
    if x-1 in nC2 or x==0:
        continue
    nC2[x-1] = (x-1)*(x-2)//2
for x in A:
    minus = nC2[count[x]]
    plus = nC2[count[x]-1]
    print(sum-minus+plus)

Solution (Python):



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


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


def main():
    w = {}
    for a in A:
        if a in w:
            w[a] += 1
        else:
            w[a] = 1
    
    v = 0
    for x in w.values():
        v += comb2(x)
    
    for a in A:
        ans = v - (comb2(w[a]) - comb2(w[a] - 1))
        print(ans)


if __name__ == "__main__":
    main()

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


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


n = int(input())
l = [int(x) for x in input().split()]
c = collections.Counter(l)


cd = {}



if not c:
    for _ in range(len(l)):
        print(0)
    sys.exit()

sum = 0
for a in c:
    nc = combinations_count(c[a], 2)
    sum += nc
    
    
    cd[a] = (nc - int(nc * (c[a] - 2) / c[a]))

"""
def my_index(lv):
    if lv not in c.keys():
        return sum
    else:
        return cd[lv]
"""

for lv in l:
    print(sum-cd[lv])

Solution (Python):
import math
import collections


def kumiawase(n):
    if n == 0 or n == 1:
        return 0
    return (n**2 - n) // 2


n = int(input())
aL = list(map(int, input().split(" ")))

d = collections.Counter(aL)

s = 0
for i in d.values():
    s += kumiawase(i)

for a in aL:
    print(s - d[a] + 1)

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

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

for i in range(N):
    print(int(C-(b[A[i]])+1))

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
from collections import Counter
def resolve():
    n = int(input())
    A = list(map(int,input().split()))
    C = Counter(A)
    total = 0
    for key, val in C.items():
        total += val * (val - 1) // 2
    for a in A:
        print(total - C[a] + 1)
resolve()

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
m=int(200200)
B=[0]*m

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

a=int(0)
for i in range(m):
    a+=int((B[i]-1)*B[i]/2)

for i in range(n):
    print(a-B[A[i]]+1)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
ma=int(200200)
B=[0]*ma

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

ans=int(0)
for i in range(ma):
    ans+=int((B[i]-1)*B[i]/2)

for i in range(n):
    print(ans-B[A[i]]+1)

Solution (Python):


n = int(input())
s = list(map(int,input().split()))
ans = {}


for i in range(n):
    if not s[i] in ans.keys():
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1

answer = 0
for i,j in ans.items():
    answer += j*(j-1)//2


for i in range(n):
    print(answer-ans[s[i]]+1)

Solution (Python):
from math import factorial
import collections

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

L = collections.Counter(A)

c = 0
for i in range(1,N+1):
    n = L[i]
    if n >=2:
        c += factorial(n) // factorial(2) // factorial(n - 2)

for k in range(N):
    c1 = c
    n = L[A[k]]
    if n >= 3:
        c1 -= n-1
    elif n == 2:
        c1 -= factorial(n) // factorial(2) // factorial(n-2)
    print(c1)

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
l = [0]*n
ans = 0

for k,v in collections.Counter(a).items():
    l[k-1] = v
    ans += v*(v-1)//2

for i in range(n):
    print (ans-l[a[i]-1]+1)

Solution (Python):


n = int(input())
s = list(map(int,input().split()))
ans = {}


for i in range(n):
    if not s[i] in ans.keys():
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1

answer = 0
for i,j in ans.items():
    answer += j*(j-1)//2


for i in range(n):
    print(answer-ans[s[i]]+1)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
cnt = dict(Counter(a))
ans = 0
for _,c in cnt.items():
  if c >= 2:
    ans += c*(c-1)//2
    
for i in range(n):
  pop_i = a[i]
  
  dif = cnt[pop_i]-1
  print(ans-dif)

Solution (Python):


n = int(input())
s = list(map(int,input().split()))
ans = {}


for i in range(n):
    if not s[i] in ans:
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1

answer = 0
for i,j in ans.items():
    answer += j*(j-1)//2


for i in range(n):
    print(answer-ans[s[i]]+1)

Solution (Python):


n = int(input())
s = list(map(int,input().split()))
ans = {}


for i in range(n):
    if not s[i] in ans:
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1

answer = 0
for i,j in ans.items():
    answer += j*(j-1)//2


for i in range(n):
    print(answer-ans[s[i]]+1)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
ma=int(200200)
B=[0]*ma

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

ans=int(0)
for i in range(ma):
    ans+=int((B[i]-1)*B[i]/2)

for i in range(n):
    now=A[i]
    Ans=ans
    Ans-=int((B[now]-1)*B[now]/2)
    Ans+=int((B[now]-1)*(B[now]-2)/2)
    print(Ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

lis = [0]*N
ans = 0
for key, v in Counter(A).items():
	lis[key-1] = v
	ans += v*(v-1)//2

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

Solution (Python):
import collections

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


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

l1 = []
l2 = []
c = collections.Counter(l)

total = sum([combinations_count(i, 2) for i in c.values()])

for a in l:
    l1.append(combinations_count(c[a], 2))
    l2.append(combinations_count(c[a]-1, 2))

for i in range(len(l)):
    print(total-l1[i]+l2[i])

Solution (Python):
import math
import collections

def cmb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
N = int(input())
M = input().split()

total = 0
count = collections.Counter(M)

for i in count.values():
    if i >= 2:
        total += cmb(i, 2)
    
for j in M:
    print(total - (count[j]-1))

Solution (Python):
import collections

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


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

l1 = []
l2 = []
c = collections.Counter(l)

total = sum([combinations_count(i, 2) for i in c.values()])

for a in l:
    print(total-combinations_count(c[a], 2)+combinations_count(c[a]-1, 2))

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

ans1=0
s=0

for A_i in A:
    C[A_i]+=1

for j in range(1,N+1):
    c=C[j]
    if c>1:
        s=c*(c-1)/2
        ans1=ans1+s
        
for A_k in A:
    d=C[A_k]
    if d<2:
        ans=ans1
    else:
        ans=ans1-(d-1)
    print(int(ans))

Solution (Python):
import math
import collections
m=int(input())
a=list(map(int,input().split()))
A=collections.Counter(a)
total=0
for j in A.values():
    total = total + j*(j-1)//2
for k in range(m):
    s=A[a[k]]
    d=total-s+1
    print(d)

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

from collections import defaultdict
d = defaultdict(int)
for i in num_list:
  d[i] += 1
 
total = 0
for i in set(num_list):
  x = d[i]
  total += x*(x-1)/2
 
for i in num_list:
  x = d[i]
  print(int(total-((x*(x-1)/2))+((x-1)*(x-2)/2)))

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

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

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

Solution (Python):
import numpy as np

c = np.zeros(int(2e5))
c_1 = np.ones(int(2e5))

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

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

ans = int(sum(c * (c - c_1) / 2))

for i in range(n):
    print(ans - int(c[a[i]-1]-1))

Solution (Python):


comb_dic = {}


def combination_formula(n, r):
    """nCrは、O(min(n-r, r))で実装する
    Notes:
        分子と分母がかなり大きな値になった場合、計算は遅くなるので注意
        求める値がmodをとった値でいい場合、フェルマーの小定理を使った方法が速い。
    """
    if r > n: return 0

    if n in comb_dic:
        return comb_dic[n]

    r = min(n-r, r)
    bunsi, bunbo = 1, 1
    for i in range(r):
        bunsi = bunsi*(n-i)
        bunbo = bunbo*(i+1)

    comb_dic[n] = bunsi//bunbo
    return comb_dic[n]


def solve():
    N = int(input())
    As = list(map(int, input().split()))

    dic = {}
    for a in As:
        if not a in dic:
            dic[a] = 0
        dic[a] += 1

    
    
    total = 0
    for key, value in dic.items():
        total += combination_formula(value, 2)

    for a in As:
        ans = total
        ans -= combination_formula(dic[a], 2)
        ans += combination_formula(dic[a]-1, 2)
        print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):


comb_dic = {}


def combination_formula(n, r):
    """nCrは、O(min(n-r, r))で実装する
    Notes:
        分子と分母がかなり大きな値になった場合、計算は遅くなるので注意
        求める値がmodをとった値でいい場合、フェルマーの小定理を使った方法が速い。
    """
    if r > n: return 0

    if n in comb_dic:
        return comb_dic[n]

    r = min(n-r, r)
    bunsi, bunbo = 1, 1
    for i in range(r):
        bunsi = bunsi*(n-i)
        bunbo = bunbo*(i+1)

    comb_dic[n] = bunsi//bunbo
    return comb_dic[n]


def solve():
    N = int(input())
    As = list(map(int, input().split()))

    dic = {}
    for a in As:
        if not a in dic:
            dic[a] = 0
        dic[a] += 1

    
    
    total = 0
    for key, value in dic.items():
        total += combination_formula(value, 2)

    for a in As:
        ans = total
        ans -= combination_formula(dic[a], 2)
        ans += combination_formula(dic[a]-1, 2)
        print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
cmb = {}
cmb_sub = {}
for key, val in cnt.items():
    cmb[key] = int(val * (val - 1) / 2)
    cmb_sub[key] = int((val - 1) * (val - 2) / 2)
cmbsum = sum(cmb.values())
dict_ans = {}
for key in cmb.keys():
    dict_ans[key] = cmbsum - cmb[key] + cmb_sub[key]
for a in A:
    print(dict_ans[a])

Solution (Python):
from collections import Counter
import math

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

c = Counter(A)

all_ans = 0
for n in c.values():
    if n <= 1:
        continue
    all_ans += math.factorial(n)//(math.factorial(n-2)*math.factorial(2))

for n in A:
    print(all_ans - (c[n]-1))

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = na()
a = [i - 1 for i in a]
b = [0] * n
for i in range(n):
    b[a[i]] += 1
r = 0
for i in range(n):
    r += b[i] * (b[i] - 1) // 2

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

Solution (Python):
import collections


m = int(input())
A = list(map(int,input().split()))[:m]
c = collections.Counter(A)
 
def hoge(n):
    return n * (n - 1) // 2

full = sum([ hoge(i) for i in c.values()])

for a in A:
    print(full - hoge(c[a]) + hoge(c[a]-1))

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

from collections import defaultdict

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

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

sum_cnt = 0
for a in cnt.values():
    sum_cnt += a * (a - 1) // 2

for a in A:
    cnta = cnt[a]
    print(sum_cnt - cnta * (cnta - 1) // 2 + (cnta - 1) * (cnta - 2) // 2)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
List={}
x=0
for i in range(n):
    if not a[i] in List:
        List[a[i]] = 1
    else:
        List[a[i]]+=1
    
sigma=0
 
for key,item in List.items():
    sigma += item*(item-1)//2
for i in range(n):
    print(sigma-List[a[i]]+1)

Solution (Python):

import sys
input = sys.stdin.readline
from collections import Counter
 
n = int(input())
a = [int(item) for item in input().split()]
comb = [0] * (n + 1)
cnt = Counter(a)
 
for key in cnt.keys():
    comb[key] = cnt[key] * (cnt[key] - 1) // 2
total = sum(comb)
 
for item in a:
    if comb[item] <= 1:
        diff = comb[item]
    else:
        diff = comb[item] - (comb[item] * (cnt[item] - 2)) // cnt[item]
    print(total - diff)

Solution (Python):
import collections
n = int(input())
a_list = list(map(int,input().split()))
c=collections.Counter(a_list)
result=0

for i in c.values():
    result+=i * (i - 1)//2

for a in a_list:
    minus=-(c[a]) * (c[a]-1)//2
    plus=(c[a]-1) * (c[a]-2)//2
    print(result + minus + plus)

Solution (Python):
N = int(input())
A = [int(a)-1 for a in input().split()]
X = [0] * N
for a in A:
	X[a] += 1
s = 0
for x in X:
	s += x * (x-1) // 2
for a in A:
	print(s - (X[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

nsum = [cnt[n]*(cnt[n]-1)//2 for n in range(N+1)]
SUM = sum(nsum)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
List={}
x=0
for i in range(n):
    if not a[i] in List:
        List[a[i]] = 1
    else:
        List[a[i]]+=1
    
sigma=0

for key,item in List.items():
    sigma += item*(item-1)//2
for i in range(n):
    print(sigma-List[a[i]]+1)

Solution (Python):
from collections import Counter

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

    counter = Counter(A)
    cmb = 0
    for v in counter.values():
        cmb += v*(v-1)//2
    for a in A:
        print(cmb - (counter[a] - 1))

    return 

if __name__ == "__main__":
    resolve()

Solution (Python):
import math
import collections
import numpy as np

def combinations_count(n, r):
    if n <= 1:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

c = collections.Counter(A)
ans = 0
for v in c.values():
    if v > 1:
        ans += v*(v-1)//2

for i in range(N):
    v = c[A[i]]
    print(ans + (v-1)*(v-2)//2 - v*(v-1)//2)

Solution (Python):
import numpy as np
import collections

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

dic = {}
after = {}

l = collections.Counter(A)

for key, val in l.items():
	dic[key] = val * (val - 1) // 2
	after[key] = (val - 1) * (val - 2) // 2

numsum = sum(dic.values())

ans = {}
for key in dic.keys():
	ans[key] = numsum - dic[key] + after[key]

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

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

a_dict = {}
for a in a_list:
    if not(a in a_dict):
        a_dict[a] = 0
    a_dict[a] += 1
    
ans = sum(map(lambda x: x*(x-1)//2, a_dict.values()))

for i in range(n):
    k = a_list[i]
    before = a_dict[k]*(a_dict[k]-1) // 2
    after = (a_dict[k]-1)*(a_dict[k]-2) // 2
    print(ans - (before - after))

Solution (Python):
from scipy.misc import comb
from collections import Counter
import datetime

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

num_dict = {}

num_counter = Counter(num_list)
  

ans_dict_1 = {}

ans_dict_2 = {}
rap_2 = datetime.datetime.now()
for k, v in num_counter.most_common():
  ans_dict_1[k] = comb(v,2,exact=True)
  ans_dict_2[k] = comb(v-1,2,exact=True)

total = sum(ans_dict_1.values())

for i in num_list:
  print(total - ans_dict_1[i] + ans_dict_2[i])