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 deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
input = sys.stdin.readline

sys.setrecursionlimit(2147483647)

def main():
  n = int(input())
  a = list(map(int, input().split()))
  s = Counter(a)
  subete = 0
  herasu = [0]*(n+1)
  for key, value in s.items():
    if value>=2:
      subete += factorial(value)/(2*factorial(value-2))
      herasu[key] = value-1
  for i in a:
    print(int(subete-herasu[i]))
  

if __name__ == '__main__':
  main()

Solution (Python):
import collections

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

ans = {}
comb = 0

for k in c.keys():
  comb += int(c[k]*(c[k]-1)/2)
for k in c.keys():
  ans[k] = comb-(c[k]-1)

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

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

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

Solution (Python):
import collections

n = int(input())

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

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


for i in range(n):
    target = c[a[i]]
    print(total - target*(target-1)//2 + (target-1)*(target-2)//2)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    nums = [0] * N
    for i in A:
        nums[int(i) - 1] += 1
    
    sum = 0
    for num in nums:
        sum += num * ( num - 1 ) // 2

    for k in range(0, N):
        old = nums[int(A[k]) - 1]
        new = old - 1
        ans = sum - (old * (old - 1) // 2 - new * (new - 1) // 2)
        print(ans)
    

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

cnt = 0
L = [0] * (N + 1)
for a in A:
    L[a] += 1

for l in L:
    cnt += l * (l - 1) // 2

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

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

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

A=sorted(A0)
A.append(0)

N1={}

c = 1
for i in range(N):

    if A[i] == A[i+1]:
        c += 1
    else:
        N1[A[i]] = c
        c = 1




N2={}
for i in N1.keys():

    if N1[i] ==1:
        N2[i] = (0,0)
    else:
        N2[i] = (N1[i]*(N1[i]-1)/2,(N1[i]-1)*(N1[i]-2)/2)

T1 = 0
for i in N2.keys():
    T1 += N2[i][0]

for i in range(N):
    T2 = int(T1 - N2[A0[i]][0]+N2[A0[i]][1])
    print(T2)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
from operator import mul
from functools import reduce

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

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}
    maxv = 0
    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
            maxv += X[k]
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    ANS = {}
    for k in Y.keys():
        ANS[k] = maxv - (X[k] - Y[k])

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

if __name__ == '__main__':
    main()

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

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

class Combination:
    def __init__(self, N=2*10**5, mod=67280421310721):
        self.mod = mod
        self.N = N
        self.fact = [1, 1]  
        self.factinv = [1, 1]  
        self.inv = [0, 1]  
        for i in range(2, N + 1):
            self.fact.append((self.fact[-1] * i) % mod)
            self.inv.append((-self.inv[mod % i] * (mod // i)) % mod)
            self.factinv.append((self.factinv[-1] * self.inv[-1]) % mod)

    def nCr(self, n, r):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return self.fact[n] * self.factinv[r] * self.factinv[n-r] % self.mod

comb = Combination()

t = 0
for ci in c:
    t += comb.nCr(c[ci],2)

for ai in a:
    print(t - comb.nCr(c[ai],2) + comb.nCr(c[ai]-1,2))

Solution (Python):
from collections import Counter
import sys
n=int(input())
s=sys.stdin.readlines()
A=list(map(int,s[0].split()))
C=Counter(A)
R=0
for v in C.values():
    R+=v*(v-1)//2
for i in range(n):
    r=C[A[i]]
    X=R
    if r>=2:
        x=((r-1)*(r-2)//2-(r)*(r-1)//2)
        X+=x
    print(X)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
d = {}
d_sum = {}
for i in range(N):
  if A[i] in d:
    d[A[i]] += 1
    d_sum[A[i]] += d[A[i]] - 1
  else:
    d[A[i]] = 1
    d_sum[A[i]] = 0


ans = sum(d_sum.values())
for i in range(N):
  print(ans - (d[A[i]] - 1))

Solution (Python):

import collections
import itertools


def main():
    n = int(input())
    a_i = tuple(map(int, input().split()))

    count_item = {}
    all_cnt = 0
    for items in collections.Counter(a_i).items():
        if items[1] >= 2:
            now_cnt = int(items[1] * (items[1] - 1) / 2)
            pre_cnt = now_cnt - items[1] + 1
            count_item[items[0]] = (now_cnt, pre_cnt)
            all_cnt += now_cnt
        else:
            count_item[items[0]] = (0, 0)

    for k in a_i:
        items = count_item[k]
        print(all_cnt - (items[0] - items[1]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from collections import Counter


def resolve():
    
    N = [int(x) for x in sys.stdin.readline().split()][0]  
    
    a_list = [int(x) for x in sys.stdin.readline().split()]  

    
    
    
    

    logger.debug('{}'.format([]))

    counts = Counter(a_list)

    retval = 0
    for a, c in counts.items():
        retval += int(c * (c - 1) / 2)

    for k in range(N):
        to_remove = a_list[k]
        c = counts[to_remove]
        print(retval - c * (c - 1) // 2 + (c - 1) * (c - 2) // 2)


if __name__ == "__main__":
    resolve()





import sys
from io import StringIO
import unittest


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

Solution (Python):












def ncr(n, r):
    r = min(r, n - r)
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + i + 1 for i in range(r)]
    denominator = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= numerator[k]
    return int(result)


import math


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


import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
freq = {}
for i in a:
    if i in freq:
        freq[i] += 1
    else:
        freq[i] = 1

inf = 1001001001
dp = [inf for _ in range(200005)]
dp2 = [inf for _ in range(200005)]
mod = 10**13 + 3

base = 0
for k, v in freq.items():
    if v > 2:
        dp[k] = ncr(v, 2)
        dp2[k] = ncr(v - 1, 2)
    elif v == 2:
        dp[k] = 1
        dp2[k] = 0
    else:
        dp[k] = 0
        dp2[k] = 0
    base += dp[k]

for i in a:
    print(base - dp[i] + dp2[i])

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

"""
lsum=[0 for i in range(N+1)]
for i in range(N):
    lsum[i+1]=lsum[i]+lc[i+1]*(lc[i+1]-1)//2
"""
sum=0
for i in range(N):
    sum+=lc[i+1]*(lc[i+1]-1)//2

for i in range(1,N+1):
    
    print(sum-lc[la[i-1]]*(lc[la[i-1]]-1)//2+(lc[la[i-1]]-2)*(lc[la[i-1]]-1)//2)

Solution (Python):
import collections
N = int(input())
li = list(map(int, input().split()))
cnLi = collections.Counter(li)
CcnLi = [n*(n-1)//2 for n in cnLi.values()]
sumC = sum(CcnLi)
for k in range(N):
    print(sumC-cnLi[li[k]] + 1)

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
S=sorted(L)
D={}
c=1
t=0
for i in range(N-1):
  if S[i]==S[i+1]:
    c+=1
  else:
    D[S[i]]=c
    c=1
D[S[N-1]]=c

for i in D.values():
   t+=i*(i-1)//2 
for i in range(N):
  print(t-(D[L[i]]-1))

Solution (Python):
def Main(N, A):
    d1 = {}
    for a in A:
        if a not in d1:
            d1[a] = 1
        else:
            d1[a] += 1
    full = 0
    for k in d1:
        full += d1[k]*(d1[k]-1)//2
    ans = []
    for a in A:
        ans.append(full-d1[a]+1)
    return ans 
    

def main():
    N = int(input())
    A = list(map(int, input().split()))
    out = Main(N, A)
    for i in out:
        print(i)

if __name__ == '__main__':
    main()

Solution (Python):
N=(int)(input())
As=(input()).split(" ")

A_num = {}

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


total = 0
for num in A_num.values():
    total+=num*(num-1)//2

for A in As:
    print(total-A_num[A]+1)

Solution (Python):
import copy

N = int(input())

As = list(map(lambda i: int(i), input().split()))

ns = [0 for i in range(N)]

for a in As:
    ns[a-1] += 1

def get_comb(arr):
    res = 0
    for a in arr:
        if a == 0:
            continue
        res += a*(a-1)/2
    return int(res)

ba = get_comb(ns)

for a in As:
    print(ba - get_comb([ns[a-1]]) + get_comb([ns[a-1]-1]))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
lis = [0 for _  in range(N+1)]
lis1 = [0 for _ in range(N+1)]
lis2 = [0 for _ in range(N+1)]
for i in range(N):
    lis[A[i]] += 1

for i in range(N+1):
    num = lis[i]
    if num != 0:
        lis1[i] = int(num*(num-1)/2)
        lis2[i] = int((num-1)*(num-2)/2) - lis1[i]

total = sum(lis1)

for i in range(N):
    val = A[i]
    print(total + lis2[val])

Solution (Python):
def main():
  n = int(input())
  a = input_list()
  ac = collections.Counter(a)
  f_list = {}
  f_sum = 0
  for k, c in ac.items():
    if c <= 1:
      f_list[k] = (c, 0)
    else:
      cc = combinations_count(c, 2)
      f_list[k] = (c, cc)
      f_sum += cc
  for i in a:
    t = f_list[i]
    tv = t[1] - (t[0] - 1)
    print(f_sum - t[1] + tv)
          
  
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

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

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

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Solution (Python):
N = int(input())
a = list(map(int, input().split(" ")))
a_list = [[i, 0] for i in range(N+1)]
a_comb = [[i, 0] for i in range(N+1)]
a_comb_sub = [[i, 0] for i in range(N+1)]

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

for i in a_list:
    a_comb[i[0]][1] = i[1]*(i[1]-1) // 2

ans =0
for i in a_comb:
    ans+= i[1]


for i in a_list:
    if i[1] >=2:
        a_comb_sub[i[0]] = i[1]-1
    else:
        a_comb_sub[i[0]] = 0

for i in a:
    print(ans - a_comb_sub[i])

Solution (Python):
import collections

n = int(input())
a = [int(i) for i in input().split(' ')]
c = collections.Counter(a)
counts = {}
all_sum = 0
for k, v in c.items():
    val = v * (v-1) / 2
    counts[k] = val
    all_sum += val

for i in range(n):
    k = a[i]
    count = c[k] - 1
    val = counts[k]
    print(int(all_sum - val + count * (count - 1) / 2))

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

for j in a:
    d[j]-=1
    
    if ans>0:
        ans-=d[j]
        print(ans)
        ans+=d[j]
        d[j]+=1
    elif ans==0:
        print(0)

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

import collections

e = collections.Counter(a)

count = 0
for i in e.keys():
    m = e[i]
    count += m*(m-1)//2

for k in range(n):
    b = a[k]
    c = e[b] - 1
    ans = count - c
    print(ans)

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

for j in a:
    d[j]-=1
    
    if ans>0:
        ans-=d[j]
        print(ans)
        ans+=d[j]
        d[j]+=1
    elif ans==0:
        print(0)

Solution (Python):

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

def comb(n):
    if n > 1:
        return n * (n-1) // 2
    else:
        return 0

dict = {}

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

combs = {}

for k in dict:
    combs[k] = comb(dict[k])


total = sum(combs.values())

for i in range(N):
    diff = combs[A[i]] - comb(dict[A[i]]-1)
    print(total - diff)

Solution (Python):
import collections
N = int(input())
arr = list(map(int, input().split()))
count = collections.Counter(arr)
total = 0
for i in count.values():
    total += (i*(i-1)/2)
for i in arr:
    print(int(total- count[i]+1))

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

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

import math

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

n = int(input())
a = input().split()

dic = {}
for i in range(len(a)) :
    if a[i] in dic :
        dic[a[i]] = dic[a[i]] + 1
    else :
        dic[a[i]] = 1


dicSum = {}
total = 0

for key in dic:
    dicSum[key] = cmb(dic[key], 2)
    total += dicSum[key]



for i in range(n) :
    
    print(total - dicSum[a[i]] + cmb(dic[a[i]]-1, 2))

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
all_comb = 0
for v in c.values():
    all_comb += v*(v-1)/2

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

Solution (Python):
N = int(input())
A = [int(n) for n in input().split(' ')]

count_A = [0]*N
for a in A:
    count_A[a-1] += 1

ret = 0
for ca in count_A:
    if ca > 0:
        ret += int((ca * (ca - 1)) / 2)

for a in A:
    ca = count_A[a-1]
    print(ret-ca+1)

Solution (Python):
import sys, math

sys.setrecursionlimit(10 ** 9)
from collections import defaultdict

IA = lambda: map(int, input().split())
IAS = lambda: map(str, input().split())

n = int(input())
N = 200005
def f(n):
    if n < 2: return 0
    return n * (n - 1) // 2

a = list(IA())
vis = [0 for i in range(0, n + 1)]
for it in a:
    vis[it] += 1

sum1 = 0
sumsum=0
for i in range(1,n):
    sumsum+=1
for i in range(1, n + 1):
    sum1 += f(vis[i])
ans = [int(-1) for i in range(0, n + 1)]
for it in a:
    if ans[it] == -1:
        ans[it] = sum1 - f(vis[it]) + f(vis[it] - 1)
    print(ans[it])

Solution (Python):
import collections
n = int(input())
a = list(input().split())
b = collections.Counter(a)
num = list(b.values())
k = len(num)
an = 0

for i in range(k):
	an+=(num[i])*(num[i]-1)//2

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

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
c = dict(Counter(a))
ans = 0
for v in c.values():
    ans += v*(v-1)//2
for i in range(n):
    print(ans-c[a[i]]+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
b = {}
p = 0

for a in A:
  if not a in b:
    b[a] = 1
  else:
    b[a] += 1
    
for v in b.values():
  p += v * (v - 1) // 2
  
for a in A:
  d = b[a]
  ans = p - (d * (d - 1) // 2) + ((d - 1) * (d - 2) // 2)
  print(ans)

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

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

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

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

    return result


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

tot = 0
for key in d:
    if d[key] == 1: continue
    tot += cmb(d[key], 2)


for i in range(n):
    if d[a[i]] == 1:
        print(tot)
    elif d[a[i]] == 2:
        print(tot-1)
    else:
        print(tot - (d[a[i]] - 1))

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

import collections
count_dic = collections.Counter(a_list)

count_dic_new = {}
for key, value in count_dic.items():
    if value == 1:
        count_dic_new[key] = 0
    elif value == 2:
        count_dic_new[key] = 1
    else: 
        count_dic_new[key] = (value * (value - 1)) // 2

fund_values = sum(list(count_dic_new.values()))

for a in a_list:
    if count_dic[a] == 1:
        a_value = fund_values
    elif count_dic[a] == 2:
        a_value = fund_values - 1
    else:
        n = count_dic[a]
        a_value = fund_values + (((n-1) * (n-2)) // 2) - (((n) * (n-1)) // 2) 

    print(a_value)

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.setdefault(a[i],1)
comb={}
banned={}
for i in dic.keys():
    if dic[i]>1:
        comb.setdefault(i,dic[i]*(dic[i]-1)//2)
        banned.setdefault(i,(dic[i]-1)*(dic[i]-2)//2)
    else:
        comb.setdefault(i,0)
        banned.setdefault(i,0)
sumComb=sum(comb.values())
for i in range(n):
    print(sumComb-comb[a[i]]+banned[a[i]])

Solution (Python):
from collections import Counter

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

c = Counter(a)
dic = {}

for x in a:
    if x not in dic:
        ex = (c[x] - 1) * (c[x] - 2) / 2
        no = (c[x]) * (c[x] - 1) / 2
        dic[x] = (int(no), int(ex))

su = sum([int(x[0]) for x in dic.values()])

for i,x in enumerate(a):
    print(su - (dic[x][0] - dic[x][1]))

Solution (Python):
import collections
import math
N = int(input())
A = list(map(int, input().split(' ')))
c = collections.Counter(A)
d1 = {}
d2 = {}
for i in c:
    d1[str(i)] = (c[i]*(c[i]-1))/2
    d2[str(i)] = ((c[i]-1)*(c[i]-2))/2

s = sum(d1.values())

for i in range(N):
    ans = s - d1[str(A[i])]+d2[str(A[i])]
    print(int(ans))

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

nums = {}
for i in range(n):
    if A[i] not in nums.keys():
        nums[A[i]] = 1
    else:
        nums[A[i]] += 1

calc_list = {}
for key, v in nums.items():
    calc_list[key] = int(v*(v-1)/2)
s = sum(calc_list.values())


for i in range(n):
    tmp = nums[A[i]]-1
    diff = int(nums[A[i]]*(nums[A[i]]-1)/2 - tmp*(tmp-1)/2)
    print(s-diff)

Solution (Python):
from collections import Counter

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

c = Counter(A)
all_c = 0
for k,v in c.items():  
  all_c += v*(v-1)//2
    
for a in A:  
  print(all_c - (c[a]-1))

Solution (Python):
import collections

n=int(input())
As=list(map(int,input().split()))
ls=collections.Counter(As)
subd={}
an=ls.values()
ans=0
for k in an:
  ans+=k*(k-1)//2
sans=ans

for i in As:
  ans=sans+1-ls[i]
  print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
D = [0]*(N+1)
for a in A:
  D[a] += 1
tot = sum([D[i]*(D[i]-1)//2 for i in range(1,N+1)])





for a in A:
  print(tot - D[a]+1)

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

for i in range(N):
    if(size[i] >= 2):
        ans += int(size[i] * (size[i]-1) / 2)

for k in range(N):
    if(size[A[k]-1] <= 1):
        print(ans)
    else:
        print(ans - (size[A[k]-1]-1))

Solution (Python):

import collections

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

N = int(input())
A = list( map(int,input().split()))
c = dict(collections.Counter(A))
su = 0
for i in c.values():
    if (i >= 2):
        su += combinations_count(i, 2)

for i in A:
    if (c[i]  >= 3):
        print(su - combinations_count(c[i],2) + combinations_count(c[i]-1,2))
    elif (c[i] == 2):
        print(su - 1)
    else:
        print(su)

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

    A_product = [0] * N
    A_product_1 = [0] * N
    for i in range(N):
        a = A_num[i]
        A_product[i] = int(a * (a-1) / 2)
        if a == 0:
            A_product_1[i] = 0
        else:
            A_product_1[i] = int((a-1) * (a-2) / 2)


    su = sum(A_product)
    for k in A:
        print(su - A_product[k-1] + A_product_1[k-1])


if __name__ == "__main__":
    main()

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

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

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

cmb = [0] * N
for i in range(N):
	cmb[i] = cmbr2[cnt[i]]

asum = sum(cmb)
for i in A:
	print(asum - cmbr2[cnt[i - 1]] + cmbr2[cnt[i - 1] - 1])

Solution (Python):




import sys
input = sys.stdin.readline



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

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

    for i in A:
        count[i-1] += 1

    cmb = 0

    for i in range(N):
        M = count[i]
        cmb += M * (M-1) // 2

    for i in A:
        s = count[i-1]
        if s <= 1:
            dec = 0
        else:
            dec = s - 1
        temp = cmb - dec
        print(temp)



main()

Solution (Python):
from collections import Counter

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


con = Counter(a)
dic = {}




for x in a:
    if x not in dic:
        ex = (con[x] - 1) * (con[x] - 2) / 2
        no = (con[x]) * (con[x] - 1) / 2
        dic[x] = (int(no), int(ex))

su = sum([int(x[0]) for x in dic.values()])

for i,x in enumerate(a):
    print(su - (dic[x][0] - dic[x][1]))

Solution (Python):
import itertools,sys,collections,math,random,fractions,bisect;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]

N, = I()
A = I()
d = dict()
def dd(n):
    if n in d:
        return d[n]
    else:
        if n < 2:
            return 0
        else:
            d[n] = math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
            return d[n]
c = collections.Counter(A)
total = 0
for e in c.items():
    total += dd(e[1])
for e in A:
    print(total -  dd(c[e]) + dd(c[e]-1))

Solution (Python):
import math
import collections


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


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

cnt = collections.Counter(a)
each_ans = {}
for key, value in cnt.items():
    t = nc2(value)
    each_ans[key] = (t, t-value+1)
all_sum = sum(value[0] for _, value in each_ans.items())
ans = {}
for key in cnt.keys():
    ans[key] = all_sum - each_ans[key][0] + each_ans[key][1]
for i in a:
    print(ans[i])

Solution (Python):
from collections import defaultdict

N = int(input())
A = [0] * N
A = list(map(int, input().split()))

d = defaultdict(lambda: 0)

def comb2(x):
    return int(x * (x - 1) / 2)

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

comp = 0
for k,v in d.items():
    comp += comb2(v)


for i in A:
    ans = comp - comb2(d[i]) + comb2(d[i] - 1)
    print(ans)

Solution (Python):

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

from collections import defaultdict

def main():
    N = int(input())
    d = defaultdict(int)
    As = list(map(int, input().split()))
    for i in range(N):
        d[As[i]] += 1
    
    ans = 0
    for k in d.keys():
        j = d[k]
        if j > 1:
            ans += j*(j-1)//2

    for i in range(N):
        j = d[As[i]]
        print(ans+1-j)


if __name__ == '__main__':
    main()

Solution (Python):
def binom(n,r):
    if n<r or n<0:
        return 0

    result=1
    for i in range(r):
        result*=(n-i)
        result//=(i+1)
    return result

n=int(input())
an=[int(c) for c in input().split()]

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

sum_=0
minus=[0 for _ in range(n)]
for i in range(n):
    sum_+=binom(count[i],2)
    minus[i]=binom(count[i]-1,1)

for i in range(n):
    print(sum_-minus[an[i]-1])

Solution (Python):
import math
import collections

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

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

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

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

    return result


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

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)

Solution (Python):
import sys
sys.setrecursionlimit(4100000)
from collections import defaultdict
import math
 
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N=int(input())
A=list(map(int,input().split()))
md={}
for a in A:
  if a in md:
    md[a]+=1
  else:
    md[a]=1
md2={}
md3={}
for key,value in md.items():
  if value>1:
    md2[key]=combinations_count(value,2)
  else:
    md2[key]=0
  value-=1
  if value>1:
    md3[key]=combinations_count(value,2)
  else:
    md3[key]=0
s_md2=sum(md2.values())
for a in A:
  print(s_md2-md2[a]+md3[a])

Solution (Python):
n = int(input())
balls = list(map(int, input().split()))
num_dict = {}
total_pairs = 0

for b in balls:
    if b not in num_dict.keys():
        num_dict[b] = [1, 0]
    else:
        num_dict[b][0] += 1
        num_dict[b][1] = (num_dict[b][0] * (num_dict[b][0]-1) - (num_dict[b][0]-1) * (num_dict[b][0]-2))//2

for val in num_dict.values():
    v = val[0]
    total_pairs += v*(v-1)//2
    
for i in range(n):
    ball_val = balls[i]
    print(total_pairs - num_dict[ball_val][1])

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = {}
cnt2 = {}

for i in range(N):
  if A[i] in cnt2:
    cnt[A[i]] += cnt2[A[i]]
    cnt2[A[i]] += 1
  else:
    cnt[A[i]] = 0
    cnt2[A[i]] = 1
ans = sum(cnt.values())

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

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

A_sort = sorted(A)
before = A_sort[0]
num = 1
jisho = {}
for i in range(1,N):
    if before == A_sort[i]:
        num += 1
    else:
        jisho[before] = num
        num = 1
        before = A_sort[i]
jisho[before] = num

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

for i in range(N):
    n = jisho[A[i]]
    print(total-n+1)

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

c = dict(collections.Counter(a))

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

for i in a:
    temp = c[i] - 1
    ans = (temp * (temp - 1)) // 2
    ans += sum
    ans -= (c[i] * (c[i] - 1)) // 2
    print(ans)