Problem:
 Score : 400 points    Problem Statement  We have N balls. The i -th ball has an integer A_i written on it. For each k=1, 2, ..., N , solve the following problem and print the answer.   Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k -th ball so that the integers written on them are equal.       Constraints   3 \leq N \leq 2 \times 10^5  1 \leq A_i \leq N  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output  For each k=1,2,...,N , print a line containing the answer.        Sample Input 1 5
1 1 2 1 2      Sample Output 1 2
2
3
2
3  Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2 . From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal. Thus, the answer for k=1 is 2 .       Sample Input 2 4
1 2 3 4      Sample Output 2 0
0
0
0  No two balls have equal numbers written on them.       Sample Input 3 5
3 3 3 3 3      Sample Output 3 6
6
6
6
6  Any two balls have equal numbers written on them.       Sample Input 4 8
1 2 1 4 2 1 4 1      Sample Output 4 5
7
5
7
7
5
7
5

Solution (Python):
from collections import Counter


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

counts = Counter(A)

total = 0
for num, count in counts.items():
    pattern = count * (count - 1) // 2
    total += pattern

for a in A:
    count = counts[a]
    if count == 1:
        print(total)
        continue
    prev = count * (count - 1) // 2
    after = (count - 1) * (count - 2) // 2
    delta = prev - after
    print(total - delta)

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:
    print(sum-(count[x]-1))

Solution (Python):
import sys
from collections import Counter

def choose(k, r):
    return k*(k-1) // r*(r-1)

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

C = Counter(A)

ANS = sum([choose(v, 2) for v in C.values()])

for k in A:
    v = C[k]
    print(ANS
          - choose(v, 2)
          + choose(v-1, 2)
         )

Solution (Python):
import collections

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

C = collections.Counter(L)

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

ans = 0
for key in C:
    a = cmb(C[key])    
    ans += a
    
for i in L:
    x = C[i] -1
    y =  cmb(C[i]) - cmb(x) 
    print(ans - y)

Solution (Python):




n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
all_sum_select = 0
for key in list(a_dic.keys()):
    all_sum_select += int(a_dic[key] * (a_dic[key]-1) / 2)
    

for k, ak in enumerate(a_list):
    sum_select_at_k = all_sum_select + int((a_dic[ak]-1) * (a_dic[ak]-2) / 2) \
                       - int(a_dic[ak] * (a_dic[ak]-1) / 2)


    print(sum_select_at_k)

Solution (Python):
n = int(input())
dA = {}
A = []
for a in input().split():
    A.append(a)
    if a in dA:
        dA[a] += 1
    else:
        dA[a] = 1

cA = {}
for a in dA:
    cA[a] = dA[a] * (dA[a] - 1) // 2

s = sum(cA.values())
for a in A:
    print(s - dA[a] + 1)

Solution (Python):
from collections import Counter

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    cnt = 0
    for c in C.values():
        cnt += choose(c)

    for a in A:
        ans = cnt
        ans -= choose(C[a])
        ans += choose(C[a] - 1)
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
dic={}
for i in A:
  if i not in dic.keys():
    dic.update({i:1})
  else:
    dic[i]+=1
ans=sum([int(j*(j-1)/2) for j in dic.values()])
for k in A:
  num=dic[k]
  if num>=1:
    print(int(ans-num+1))
  else:
    print(ans)

Solution (Python):


def main():
    from collections import Counter
    import sys

    input()
    a = list(map(int, sys.stdin.readline().split()))

    b = Counter(a)

    c = sum(v * (v - 1) // 2 for v in b.values())
    d = [str(c - (b[v] - 1)) for v in a]
    print('\n'.join(d))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter


def comb(n, r):
    ans = 1
    for i in range(1, r+1):
        ans = ans * ((n + 1 - i) / i)

    return int(ans)


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

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

gsum = 0
for i in range(1, N + 1):
    if (c[i] != 0):
        gsum += comb(c[i], 2)

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

Solution (Python):
import collections

n = input()
al = tuple(x for x in input().split())
aset = set(al)


ccc = collections.Counter
counts_d = ccc(al)


v_counts = ccc(v for v in counts_d.values())

whole_patterns = sum(
    (int(k * (k - 1) / 2)) * v for k, v in v_counts.items() if k > 1
)

[print(whole_patterns - counts_d[a] + 1) for a in al]

Solution (Python):


def main():
    import collections 
    import sys

    input()
    a = list(map(int, sys.stdin.readline().split()))

    b = collections.Counter(a)

    c = sum(v * (v - 1) // 2 for v in b.values())
    d = [str(c - (b[v] - 1)) for v in a]
    print('\n'.join(d))


if __name__ == '__main__':
    main()

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:
    print(result - c[a] + 1)

Solution (Python):

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

n = int(input())
a = list(map(int, input().split()))
dct = {}
for i in range(n):
    if a[i] in dct:
        dct[a[i]] += 1
    else:
        dct[a[i]] = 1
tot = 0
for key, value in dct.items():
    tot += choose2(value)

for i in range(n):
    ans = tot
    ans -= choose2(dct[a[i]])
    ans += choose2(dct[a[i]]-1)
    print(ans)

Solution (Python):

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

Solution (Python):
def main():
    from collections import Counter
    import sys

    input()
    a = list(map(int, sys.stdin.readline().split()))

    d = Counter(a)

    base = sum(v * (v - 1) // 2 for v in d.values())
    e = [str(base - (d[v] - 1)) for v in a]
    print('\n'.join(e))


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import collections


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


d_c = collections.Counter(l_a)
l_c = list(d_c.values())


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


for i in range(n):
    print(o - d_c[l_a[i]] + 1)

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

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

num_count = {}

for e in a:
    if e in num_count.keys():
        num_count[e] += 1
    else:
        num_count[e] = 1

s = 0
c = set()

for i in range(n):
    if a[i] not in c:
        num = num_count[a[i]]
        s += num*(num-1) / 2
        c.add(a[i])

for i in range(n):
    print(int(s - (num_count[a[i]] - 1) ))

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

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

for a in A:
    k = B[a]
    print(ans - (k-1))

Solution (Python):
n = int(input())
data = [int(x) for x in input().split()]
cnt = [0]*(n+1)


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

total = int(sum([x*(x-1)/2 for x in cnt]))

for k in range(n):
    answer = total - cnt[data[k]] + 1
    print(answer)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
from collections import defaultdict
d = defaultdict(int)
for i in a:
  d[i] += 1

total = 0
for i in set(a):
  x = d[i]
  total += x*(x-1)/2

for i in a:
  x = d[i]
  print(int(total-((x*(x-1)/2))+((x-1)*(x-2)/2)))

Solution (Python):
def main():

    from collections import Counter
    import sys

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

    d = Counter(a)

    base = sum(v * (v - 1) // 2 for v in d.values())
    e = {k: base - (v >= 2) * (v - 1) for k, v in d.items()}
    for i in a:
        print(e[i])


if __name__ == '__main__':
    main()

Solution (Python):
def main():

    from collections import Counter
    import sys

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

    d = Counter(a)

    base = sum(v * (v - 1) // 2 for v in d.values())
    e = {k: base - (v >= 2) * (v - 1) for k, v in d.items()}
    p = [str(e[x]) for x in a]
    q = '\n'.join(p)
    print(q)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict,Counter
import sys

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

d = Counter(a)
e = defaultdict(int)

base = sum(v * (v - 1) // 2 for v in d.values())
e = {k: base - (v >= 2) * (v - 1) for k, v in d.items()}

for x in a:
  print(e[x])

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
count_dic = defaultdict(int)
for x in A:
    count_dic[x] +=1
ans = 0
for v in count_dic.values():
    ans += v*(v-1)//2
for x in A:
    print(ans -(count_dic[x]-1))

Solution (Python):
from collections import Counter
import numpy as np
 
N = int(input())
A = np.array(list(map(int, input().split())))
A_set = np.unique(A)
count_dict= Counter(A)
count_sum = np.sum([count_dict[i]*(count_dict[i]-1)/2 for i in A_set])
    
for i in A:
    print(int(count_sum-count_dict[i]+1))

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

Solution (Python):
MOD = 10**9
fact_inv, inv = [0]*10**6, [0]*10**6

def init_nCk():
    fact_inv[0] = fact_inv[1] = 1
    inv[1] = 1
    for i in range(2,10**6):
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        fact_inv[i] = fact_inv[i-1] * inv[i] % MOD

def nCk(n,k):
    if n<k:
        return 0
    if n<0 or k<0:
        return 0
    ans = 1
    for i in range(n, n-k+1 - 1, -1):
        ans *= i
        ans %= MOD
    return ans * fact_inv[k] % MOD
init_nCk()

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

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

for a in A:
    k = B[a]
    print(ans - nCk(k-1,1))

Solution (Python):

from collections import Counter
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
A_set = np.unique(A)
count_dict= Counter(A)
count_sum = np.sum([count_dict[i]*(count_dict[i]-1)/2 for i in A_set])

out = [int(count_sum-count_dict[i]+1) for i in A]
for i in out:
    print(i)

Solution (Python):
def main():

    from collections import Counter
    import sys

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

    d = Counter(a)


    base = sum(v * (v - 1) // 2 for v in d.values())
    e = {k: base - (v >= 2) * (v - 1) for k, v in d.items()}
    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

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


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


counts = [0] * (N + 1)
for a in A:
    counts[a] += 1
total = sum(map(comb2, filter(lambda n: n > 1, counts)))

for a in A:
    print(total - (counts[a] - 1))

Solution (Python):
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    base = sum(v * (v - 1) // 2 for v in d.values())
    e = {k: base - (v >= 2) * (v - 1) for k, v in d.items()}

    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

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


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


counts = [0] * (N + 1)
for a in A:
    counts[a] += 1
total = sum(map(lambda n: comb2(n), filter(lambda n: n > 1, counts)))

for a in A:
    print(total - (counts[a] - 1))

Solution (Python):
from collections import Counter

N, *A = map(int, open(0).read().split())

C = Counter(A)

full = sum(n * (n - 1) // 2 for n in C.values())
for a in A:
    print(full - (C[a] - 1))

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))
c=collections.Counter(A)
ans=0
for i in c:
    ans+=c[i]*(c[i]-1)//2
for i in range(N):
    a=A[i]
    Ans=ans-c[a]+1
    print(Ans)

Solution (Python):
N=int(input())
nums=list(map(int, input().split()))
dic={}
for each in nums:
  if each not in dic.keys():
    dic.update({each:1})
  else:
    dic.update({each:dic[each]+1})

ans=sum([ int( m*(m-1)/2 ) for m in dic.values() ] )

for each in nums:
  m=dic[each]

  if m<=1:
    print(ans)
  else:
    print(ans-m+1)

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

def test():
    duplication = {}
    ans = 0
    for x in A:
        if x in duplication:
            continue
        count = A.count(x)
        if count > 1:
            duplication[x] = count
            ans += count*(count-1)//2
    for x in A:
        if x not in duplication:
            print(ans)
        else:
            print(ans -(duplication[x]-1))
from collections import defaultdict

sum = 0
count_dic = defaultdict(int)
for x in A:
    count_dic[x] +=1
for v in count_dic.values():
    sum += v*(v-1)//2
for x in A:
    print(sum -(count_dic[x]-1))

Solution (Python):
import collections
N = int(input())
A = list(map(int , input().split()))
a = collections.Counter(A)
k = 0
def comb(n):
    return n * (n - 1) // 2
for j in a.values():
    k += comb(j)
    
for i in range(N):
    s = A[i]
    p = a[s]
    print(int(k - p + 1))

Solution (Python):
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    base = sum(v * (v - 1) // 2 for v in d.values())

    for k, v in d.items():
        if v <= 1:
            e[k] = base - 0
        else:
            e[k] = base - (v - 1)

    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

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

for i in range(N):
  if str(A[i]) in Adic.keys():
    Adic[str(A[i])] += 1
  else:
    Adic[str(A[i])] = 1
else:
  countDic = {}
  count = 0
  for j in Adic.keys():
    comb = Adic[j] * (Adic[j] - 1) // 2
    count += comb
    countDic[j] = comb
  else:
    for i in A:
      print(count - countDic[str(i)] + ((Adic[str(i)] - 1) * (Adic[str(i)] - 2) // 2))

Solution (Python):
import math
 
def permutations_count(n, r):
    if r == 2:
        return int( (n*(n-1) ) / r )
    if r == 1:
        return n
 
 
N = int(input())
A = list(map(int,input().split()))
 
data = [0] * (N+1)
 
for i in range(N):
    data[A[i]] += 1
 
total = 0
 
for i in range(N+1):
    if data[i] >= 2:
        total += permutations_count(data[i],2)
 
 
 
for i in range(N):
    if data[A[i]] - 1 > 0 :
        print( total - permutations_count(data[A[i]]-1,1) )
    else:
        print(total)

Solution (Python):
import collections

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

gokei = 0
counter = collections.Counter(A)
for i in counter.values():
    gokei += i * (i - 1) // 2

ans = [0]*(N+1)
for i in range(N):
    z = A[i]
    d = counter[z]
    if ans[z] == 0:
        e = gokei - (d * (d - 1) // 2 - (d-1) * (d - 2) // 2)
        ans.append(e)
        print(e)
    else:
        print(ans[z])

Solution (Python):
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    base = sum(v*(v-1)//2 for v in d.values())

    for k, v in d.items():
        if v <= 1:
            e[k] = base - 0
        else:
            e[k] = base - (v - 1)

    p = [str(e[i])+'\n' for i in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

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

from collections import Counter
c = Counter()

for ai in A:
    c[ai] += 1

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



for ai in A:
    n = c[ai]
    if n >= 2:
        org = (n * (n-1))//2
        new = ((n-1) * (n-2))//2
        diff = org - new
        r = cnt - diff
        print(r)
    else:
        print(cnt)

Solution (Python):
from collections import defaultdict
N = int(input())
a = [int(x) for x in input().split()]
d = defaultdict(int)

for k in range(N):
    d[a[k]] += 1

S = 0

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

for k in range(N):
    num = d[a[k]]
    if num:
        print(S - num + 1)
    else:
        print(S)

Solution (Python):
import collections
import math

n = int(input())

m = list(map(str,input().split()))

c = dict(collections.Counter(m))
d = {}
hyouji = {}
def ans(a):
    if a<2:
        return 0
    else:
        return math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))

cnt = 0
for i,j in c.items():
    n = ans(j)
    d[i] = n
    cnt += n


for k in m:
    if k in hyouji:
        print(hyouji[k])
    else: 
        if c[k] < 2:
            q = cnt
        elif c[k] == 2:
            q = cnt - 1
        else:
                q = cnt -d[k] + ans(c[k] - 1)

        hyouji[k] = q
        print(hyouji[k])

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
N = int(input())
ai = list(map(int, input().split()))
c = collections.Counter(ai)

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

for i in range(len(ai)):
    b=c[ai[i]]
    print(val-b+1)

Solution (Python):
import collections
from collections import defaultdict

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

dict = defaultdict(int)
for i in l:
    dict[i]+=1

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

for k in l:
    print(ans - (dict[k] - 1))

Solution (Python):
import sys
import numpy as np

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n = I[0]
a = I[1:]

def main():
    c = np.bincount(a) 
    tot = np.sum(c * (c - 1) // 2)
    res = tot - (c[a] - 1)
    return res
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

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

all=0
for i,j in c.items():
    all += j*(j-1)//2
for i in range(N):
    print(all-(c[a[i]]-1))

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

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










N = int(input())
A = [int(i) for i in input().split()]
dp = [0]*(N+1)
ans = 0
cnt = 0
tle = 0

As = list(set(A))
c = Counter(A)

for i in As:
  cnt = c[i]
  if(cnt<2):
    dp[i] = 0
  else:
    dp[i] = combinations_count(cnt, 2)

tle = sum(dp)


for i in range(N):
  ans = tle - (c[A[i]]-1)
  if(ans < 0):
    ans = 0
  print(int(ans))

Solution (Python):
import sys
import numpy as np

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n = I[0]
a = I[1:]

def main():
    c = np.bincount(a, minlength=np.amax(a)+1) 
    tot = np.sum(c * (c - 1) // 2)
    res = tot - (c[a] - 1)
    return res
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)



setrecursionlimit(1000000)


def main():
    n = int(input())
    k = list(map(int,input().split()))
    l = Counter(k)
    ans_ = sum([i*(i-1)//2 for i in l.values()])
    for i_n in range(n):
        d = k[i_n]
        print(ans_ - l[d]*(l[d]-1)//2 + (l[d]-1)*(l[d]-2)//2)

main()

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

Solution (Python):
import collections





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


num=0
for i in c.values():
    num += i*(i-1)/2
    
for i in range(x):
    print(int(num-c[a[i]]+1))

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

c = collections.Counter(a)
ans = 0

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

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

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

for k in l:
    print(int(M-c[k]+1))

Solution (Python):
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    func = lambda x: x * (x - 1) // 2 if x >= 2 else 0
    v = tuple(d.values())
    base = sum(tuple(map(func, v)))

    for k, v in d.items():
        if v <= 1:
            e[k] = base - 0
        else:
            e[k] = base - (v - 1)

    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import numpy as np

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

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n = I[0]
a = I[1:]

def main():
    c = np.bincount(a, minlength=np.amax(a)+1) 
    tot = comb(c).sum()
    res = tot - comb(c[a]) + comb(c[a]-1)
    return res
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
import numpy as np
from collections import Counter


N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
np_A = np.array(A)


tmp_all = np.array(A)
unique = np.unique(tmp_all)

ans_all = 0
for u in unique:
    count_u = c[u]
    
    if count_u < 2:
        continue
    
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp




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

Solution (Python):



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

import collections
c = collections.Counter(a)







l = {}
ll = {}




    

    
for key, value in c.items():
    l[key] = value
    ll[key] = int((value * (value - 1)) / 2)
    


    



c_sum = sum(ll.values())
for i in range(n):
    v = l[a[i]]
    vv = ll[a[i]]
    vvv = int(((v - 1) * (v - 2)) / 2)
    
    print(c_sum - (vv - vvv))

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

Solution (Python):
from collections import Counter

def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
A=list(map(int,input().split()))
C=Counter(A)
ans=0
for i in C.values():
    ans+=Comb(i,2)
for i in A:
    print(ans-C[i]+1)

Solution (Python):
import math

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

for i in range(0,N):
	point=A[i]
	deff=(cnt[point-1]*(cnt[point-1]-1))//2-((cnt[point-1]-1)*(cnt[point-1]-2))//2
	print(ans-deff)

Solution (Python):
n = int(input())
l = [int(x) for x in input().split()]
k = [0]*n
for i in range(n):
  k[l[i] - 1] = k[l[i] - 1] + 1
def motomeru(i):
  return int((k[i-1])*(k[i-1]-1)/2)
def herasu(i):
  if k[i-1] == 0 or k[i-1] == 1:
    return 0
  else:
    return int((k[i-1]-1)*(k[i-1]-2)/2)
zenbu = 0
for i in range(n):
  zenbu = zenbu + motomeru(i+1)
for i in range(n):
  a = zenbu - motomeru(l[i]) + herasu(l[i])
  print(a)

Solution (Python):
import collections


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

    counter = collections.Counter(A)
    ans = 0

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

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


resolve()

Solution (Python):
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    func = lambda x: x * (x - 1) // 2 if x >= 2 else 0
    v = list(d.values())
    base = sum(list(map(func, v)))

    for k, v in d.items():
        if v <= 1:
            e[k] = base - 0
        else:
            e[k] = base - (v * (v - 1) // 2 - (v - 1) * (v - 2) // 2)

    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

def main(N, lst_A):

    dct_cntA = Counter(lst_A)
    sum_comb = 0

    for cntA in dct_cntA.values():
        comb = (cntA * (cntA - 1)) // 2
        sum_comb += comb

    for A in lst_A:
        print(sum_comb - (dct_cntA[A] - 1))

    return 0

if __name__ == "__main__":

    main(N, lst_A)

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

from operator import mul
from functools import reduce
from functools import lru_cache

N = int(input())
A_list = [int(e) for e in input().split()]


number_dict = dict()

for i in range(N):
    ball_number = A_list[i]
    if ball_number not in number_dict:
        number_dict[ball_number] = 0
        
    number_dict[ball_number] += 1


comb_dict = dict()

@lru_cache(maxsize=10000000000000)
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
    
@lru_cache(maxsize=10000000000000)
def two_choice(N):
    if N == 2:
        return 1
    elif N <= 1:
        return 0
    elif N == 3:
        return 3
    else:
        return combinations_count(N,2)

all = 0

for in_dict in number_dict.items():
    ball_number = in_dict[0]
    balls = in_dict[1]
    comb_dict[ball_number] = two_choice(balls)
    all += comb_dict[ball_number]


for i in range(1,N+1):
    ball_number = A_list[i-1]
    now_balls = number_dict[ball_number] - 1
    print(int(all-comb_dict[ball_number]+two_choice(now_balls)))

Solution (Python):
import collections
from collections import defaultdict

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

dict = defaultdict(int)
for i in l:
    dict[i]+=1

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

for k in l:
    print(ans - (dict[k] - 1))

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
countdic = Counter(A)
goukei = 0
ans = ''
for key,value in countdic.items():
    goukei += (value*(value-1))//2
for i in A:
    ans = goukei -(countdic[i]*(countdic[i]-1))//2 + ((countdic[i]-1)*(countdic[i]-2))//2
    print(ans)

Solution (Python):
import math

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

C = list(set(A))
M = len(C)
t = []
anst = 0
dic = {}
for i in A:
  if not i in dic:
    dic[i] = 0
  dic[i] += 1

jisyo = {}
for i in range(M):
  temp = dic[C[i]]
  jisyo[C[i]] = temp
  
  if temp >=2:
    anst += temp*(temp-1)/2



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

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

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

Solution (Python):
from math import factorial

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


def comb(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


counts = [0] * (N + 1)
for a in A:
    counts[a] += 1
total = sum(map(lambda n: comb(n, 2), filter(lambda n: n > 1, counts)))

for a in A:
    print(total - (counts[a] - 1))

Solution (Python):
import collections

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

Ad = dict(collections.Counter(A))








score_sum = sum([x*(x-1)/2 for x in list(Ad.values())])

for a in A:
    print(int(score_sum - (Ad[a] - 1)))

Solution (Python):
from collections import Counter
n=int(input())
S=list(map(int,input().split()))
number=Counter(S)
comb=0
stock={}
for i in list(number.values()):
  comb += i*(i-1)/2
for i in S:
  ans = int(comb - number[i]*(number[i]-1)/2 + (number[i]-1)*(number[i]-2)/2)
  print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(balls)

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

for b in balls:
    k = c[b]
    print(sum_ball_combinations- (k*(k-1)//2)+ (k-1)*(k-2)//2)

Solution (Python):
def d_banned_k():
    from collections import Counter
    N = int(input())
    A = [int(i) for i in input().split()]

    count = Counter(A)
    total = sum(c * (c - 1) // 2 for c in count.values())

    ans = [total - (count[a] - 1) for a in A]
    return ' '.join(map(str, ans))

print(d_banned_k())

Solution (Python):
import collections
import math

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


sumall = 0
for x, y in c.items():
    sumall += (y*(y-1)//2)

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