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 operator import mul
from functools import reduce
import collections

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

N = int(input())
M = input().split()

c = collections.Counter(M)

A = 0

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

Solution (Python):
from collections import Counter
n = int(input())
s = [int(i) for i in input().split()]

s_set = set(s)

counted = Counter(s)
ans = sum(i*(i-1)/2 for i in counted.values())

for i in s:
    x = counted[i]
    print(int(ans - x + 1))

Solution (Python):
from collections import Counter

def main():
    N = int(input())
    As = list(map(int,input().split()))
    Cntr = Counter(As)

    ans0 = 0
    for i in Cntr:
        n = Cntr[i]
        ans0 += n*(n-1)//2
    
    for ai in As:
        ni = Cntr[ai]
        print(ans0-ni+1)

    return

if __name__ == "__main__":
    main()

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 = collections.Counter(a)
s = 0
for i in set(a):
    s += c[i] * (c[i] - 1)
for i in a:
    print(int((s - (c[i] * (c[i] - 1)) + ((c[i] - 1) * (c[i] - 2))) / 2))

Solution (Python):
from collections import Counter


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


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    t = 0
    for i in c:
        t += combinations_count(c[i])

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

Solution (Python):

n = int(input())

def c2(x):
    return(((x)*(x-1))//2)

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

index = {}

for i in range(0,  len(line)):
    if line[i] in index.keys():
        index[line[i]] = index[line[i]] + 1
    else:
        index[line[i]] = 1

total = 0

for key in index:
    total = total + c2(index[key])

for u in range(0, len(line)):
    print(total-(index[line[u]]-1))

Solution (Python):
from collections import Counter
from functools import lru_cache

@lru_cache(maxsize=20000)
def combi2(n):
  return n * (n-1) // 2

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



base = 0
for value in c.values():
  base += combi2(value)

for a in A:
  ans = base
  ans -= combi2(c[a])
  ans += combi2(c[a]-1)
  print(ans)

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

a_dict = {}
for a in a_ls:
    if not a in a_dict.keys():
        a_dict[a] = 0
    a_dict[a] += 1   

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


every_dict = {}
for i in a_dict.keys():
    if a_dict[i] > 1:
        every_dict[i] = comb(a_dict[i],2)
    else:
        every_dict[i] = 0

values_ls = every_dict.values()
_sum = sum(values_ls)
ans_dict = {}
for i in set(a_ls):
    if a_dict[i] - 2 > 0:
        ans_dict[i] = _sum - every_dict[i] + (every_dict[i] *  (a_dict[i] - 2)) // a_dict[i]
    else:
        ans_dict[i] = _sum - every_dict[i]

for i in a_ls:
    print(ans_dict[i])

Solution (Python):
import collections
from sys import stdin
n = int(stdin.readline().rstrip())
ass = list(map(int, stdin.readline().rstrip().split()))

c = collections.Counter(ass)

cnum =[0]*len(c)
keys = c.keys()

cs = {}
for i in keys:
    key = c[i]
    combi = key *(key-1) / 2
    cs[i] = combi

sum = sum(cs.values())

for i in range(n):
    add = c[ass[i]]-1
    adds = add *(add-1) / 2
    ans = sum - cs[ass[i]] + adds
    print(int(ans))

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

sa=set(A)
ca=col.Counter(A)
num=0
for i in sa:
    if ca[i]==1:
        continue
    else:
        num += (ca[i]*(ca[i]-1))//2
for i in A:
    ans = num - (ca[i]-1)
    print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

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

def main():
    n = ni()
    A = []
    Onaji = {}
    for i in na():
        i -= 1
        A.append(i)
        if i in Onaji:
            Onaji[i] += 1
        else:
            Onaji[i] = 1

    kumi = {}
    minuskumi = {}
    Sum = 0
    for a in A:
        if Onaji[a] <= 1:
            continue
        if a in kumi:
            continue
        kumi[a] = cmb(Onaji[a], 2)
        if Onaji[a] - 1 <= 1:
            minuskumi[a] = 0
        else:
            minuskumi[a] = cmb(Onaji[a] - 1, 2)
        Sum += kumi[a]
    for a in A:
        if a in minuskumi and a in kumi:
            print(minuskumi[a] + Sum - kumi[a])
        else:
            print(Sum)
    
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [[] for i in range(N)]
ans = [0]*N
for k, a in enumerate(A):
    a = a - 1
    B[a].append(k)


S = 0
for b in B:
    lenb = len(b)
    if lenb >= 1:
        S += lenb * (lenb-1) / 2

for i, b in enumerate(B):
    for j, k in enumerate(b):
        ans[k] = int(S - (len(b) - 1))
for i in ans:
    print(i)

Solution (Python):
def parm(a):
    return(int(a*(a-1)/2))

n=int(input())
a=list(input().split())
dic={}
ans=[0]*n
sumans=0
for i in range(n):
    if a[i] in dic.keys():
        dic[a[i]]+=1
    else:
        dic[a[i]]=1
for i in dic.values():
    sumans+=parm(i)
for i in range(n):
    print(sumans-parm(dic[a[i]])+parm(dic[a[i]]-1))

Solution (Python):
from copy import copy
n = int(input())
a = [int(i) for i in input().split()]
count_a = {}
diff_a = [0] * n
for ai in a:
    try:
        count_a[ai] += 1
    except KeyError:
        count_a[ai] = 1

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
dictA = {}
for x in A:
    dictA[x] = 0
for x1 in A:
    dictA[x1] += 1

combisum = 0
for y in dictA.values():
    combisum += y * (y - 1) // 2

for z in range(N):
    print(combisum - dictA[A[z]] + 1)

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


a

import collections
pair_list=collections.Counter(a).most_common()

mass=0
for pair in pair_list:
        mass+=pair[1]*(pair[1]-1)/2

d={str(s[0]): s[1]-1  for s in pair_list}

for  k in range(1,n+1):
    print(int(mass-d[str(a[k-1])]))

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

comb = []
diff = []
su = 0
for i in range(0,n):
    su = su+i
    comb.append(su)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
c = collections.Counter(a)
total = 0
for i in c.values():
    total += comb[i-1]
for i in a:
    print(total - (c[i]-1))

Solution (Python):












mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n = int(input())
    a = list(map(int,input().split()))
    mp = {}
    for i in range(n):
        if a[i] in mp.keys():
            mp[a[i]] += 1
        else:
            mp[a[i]] = 1
    res = 0
    b = [0] * (n + 1)
    for k in mp.keys():
       x = mp[k] * (mp[k] - 1) // 2
       res += x
       b[k] = x - (mp[k] - 1) * (mp[k] - 2) // 2
    for i in range(n):
        print(res - b[a[i]])
    return

if __name__=='__main__':
    main()

Solution (Python):
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
N=int(input())
A=list(map(int,input().split()))

l=[0]*(N+1)
cnt=0

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

for k in range(N):
	print(cnt-l[A[k]]+1)

Solution (Python):

"""
Created on Sun Mar 22 20:55:58 2020

@author: naoki
"""

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

aC2 = [0] * (N+1)

for i in range(2,N+1):
    aC2[i] = int(i*(i-1)/2)

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

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

all_count = 0
for i in range(N+1):
    all_count += aC2[count[i]]

for i in range(N):
    print(all_count - aC2[count[A[i]]] + aC2[count[A[i]]-1])

Solution (Python):
from collections import defaultdict
from itertools import groupby

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    
    def cmb(n, r):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)

        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

    B = sorted(A)
    d = defaultdict(int)
    gr = groupby(B)
    
    s = 0
    for key, group in gr:
        c = len(list(group))
        d[key] = c
        s += cmb(c, 2)
    
    for a in A:
        ans = s - cmb(d[a], 2) + cmb(d[a]-1, 2)
        print(ans)
    
solve()

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
from functools import lru_cache

N = I()
A = LI()

@lru_cache(maxsize=None)
def ncr(n, r):
    return int(n*(n-1)/2)

tmp = 0
AA = collections.Counter(A)
As = AA.items()
for n, t in As:
    tmp += ncr(t, 2)

for a in A:
    an = AA[a]
    print(tmp - ncr(an, 2) + ncr(an - 1, 2))

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
m = {}
total = 0
diff = [0]*N
for a in c.items():
    m[a[0]] = a[1]
    total += a[1]*(a[1]-1)//2
for i in range(N):
    com1 = (m[A[i]])*(m[A[i]]-1)//2
    com2 = (m[A[i]]-1)*(m[A[i]]-2)//2
    diff[i] = com1-com2
for i in range(N):
    print(total-diff[i])

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
a_list=[0]*(n+1)
for i in a:
  a_list[i]+=1
ans1=[0]*(n+1)
ans2=[0]*(n+1)
for i,m in enumerate(a_list):
  if m>=2:
    ans1[i]=m*(m-1)//2
    ans2[i]=(m-2)*(m-1)//2
all=sum(ans1)
for i in a:
  print(all-ans1[i]+ans2[i])

Solution (Python):
import collections
import math

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

d = [-1]*200010
Cf = [C(i) for i in range(200010)]
sm = 0
for k,v in c.most_common():

    d[k] = v
    sm += C(v)

for a in A:
    print (sm - Cf[d[a]] + Cf[d[a]-1])

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

def main():

    N = int(input())

    a_l = [0] * N

    nums = list(map(int, input().split()))
    for i in nums:
        a_l[i-1] += 1

    total = 0
    for j in a_l:
        if j > 1:
            total += cmb(j)

    for k in nums:
        if a_l[k-1] > 1:
            print(total - cmb(a_l[k-1]) + cmb(a_l[k-1] - 1))
        else:
            print(total)


if __name__ == '__main__':
    main()

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

L=list(map(int,input().split()))
L.insert(0,0)

H=list()
for _ in range(N+1):
    H.append(0)
for i in range(1,N+1):
    H[L[i]]+=1


def culc(x):
    return x*(x-1)//2

A=0
for j in range(1,N+1):
    A+=culc(H[j])



for k in range(1,N+1):
    number=L[k]
    print(A-(H[number]-1))

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

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

c = Counter(a)
sum = 0
for i in c:
    x = c[i]
    if x > 1:
        sum += x*(x-1)//2

for i in a:
    x = c[i]
    print(sum-x+1)

Solution (Python):
N = int(input())
ARR = [int(s) for s in input().split(" ")]



def calculate(n,arr):

    dict = {}
    brr = {}
    for ar in arr:

        if dict.get(ar) == None:
            dict.__setitem__(ar,1)
        else:
            tmp = dict.get(ar)
            tmp = tmp + 1
            brr.__setitem__(ar,(tmp * (tmp - 1)) // 2 )
            dict.__setitem__(ar,tmp)

    sumValue = sum(brr.values())

    for ar in arr:
        tmp = dict.get(ar)

        print(sumValue - (tmp - 1))

calculate(N,ARR)

Solution (Python):
import collections
import numpy as np

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

cnt = collections.Counter(A)

B = [cnt[a] for a in A]

def cmb(n):
    return n * (n - 1) // 2
    
ans = int(sum([cmb(k) for k in cnt.values()]))

for b in B:
    print(ans - cmb(b) + cmb(b - 1))

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

N = int(input())
A = np.fromstring(input(), np.int, sep=" ")

uniq_count = defaultdict(int)
for a in A:
    uniq_count[a] += 1
uniq = np.fromiter((uniq_count.keys()), np.int)
counts = np.fromiter(uniq_count.values(), np.int)

p = lambda n: n * (n - 1) // 2

n = p(counts)
m = p(counts - 1)

ans = n.sum() - n + m

mapping = dict(zip(uniq, ans))

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

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

d = collections.Counter(A)
su = 0
for val in d.values():
    su += int(val*(val-1)//2)
for i in range(N):
    val = d[A[i]]
    print(su - int(val*(val-1)//2) + int((val-1)*(val-2)//2))

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

def main():

    N = int(input())

    a_l = [0] * N

    nums = list(map(int, input().split()))
    for i in nums:
        a_l[i-1] += 1

    total = 0
    for j in a_l:
        if j > 1:
            total += cmb(j)

    for k in nums:
        if a_l[k-1] > 1:
            print(total - cmb(a_l[k-1]) + cmb(a_l[k-1] - 1))
        else:
            print(total)


if __name__ == '__main__':
    main()

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

sortedList = sorted(myList)

myDict = {sortedList[0]:1}
for myIndx1 in range(1,N):
    if sortedList[myIndx1] == sortedList[myIndx1-1]:
        myDict[sortedList[myIndx1-1]] = myDict[sortedList[myIndx1-1]] + 1
    else:
        myDict[sortedList[myIndx1]] = 1

total = 0
for myVal in myDict.values():
    total = total + int((myVal*(myVal-1))/2)

for item in myList:
    print(total - (myDict[item]-1))

Solution (Python):
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
A__=list(set(A))
A_=Counter(A)
S=0
for a in A__:
    k=a
    v=A_[a]
    if v<2:
        continue
    a=v*(v-1)//2
    S+=a

for a in A:
    x=A_[a]
    y=x*(x-1)//2
    S-=y
    z=(x-1)*(x-2)//2
    print(S+z)
    S+=y

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

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


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


for a in As:
    print(cnt-(d[a]-1))

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

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

N = int(input())
items = list(map(int, input().split()))
counts = Counter(items)
cobinations = {}
count_sum = 0

for key,value in counts.items():
  if value not in cobinations:
    if value > 1:
      count = cmb(value, 2)
      cobinations[value] = count
    else:
      cobinations[value] = 0
  count_sum+=cobinations[value]

for key in items:
  value = counts[key]
  if value - 1 not in cobinations:
    if value - 1 > 1:
      
      
      
      count = cmb(value-1, 2)
      cobinations[value-1] = count
    else:
      cobinations[value-1] = 0
  
  before = cobinations[value]
  after = cobinations[value-1]
  print(count_sum - before + after)

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

c = collections.Counter(As)

sum_ = 0
for key, val in c.items():
  sum_ += (val*(val-1)/2)

sum_ = int(sum_)

for i in range(N):
  print(sum_-(c[As[i]]-1))

Solution (Python):
def nCr(n, r):
    import math
    
    
    if r > n:
        return 0
    return math.factorial(n) // ((math.factorial(n - r) * math.factorial(r)))

memo = [-1] * 300000
memo2 = [-1] * 300000
memo3 = [-1] * 300000

n = int(input())
dat = list(map(int, input().split()))
from collections import Counter
c = Counter(dat)
total = 0

for k in c:
    if c[k] > 1:
        memo[k] = nCr(c[k], 2)
        memo2[k] = nCr(c[k]-1, 2)
        memo3[k] = memo[k] - memo2[k]
        total += memo[k]




for i in range(n):
    if memo3[dat[i]] == -1:
        print(total)
    else:
        print(total - memo3[dat[i]])

Solution (Python):
input();l=list(map(int,input().split()))
from collections import*;c=Counter(l)
t=sum(i*~-i//2 for i in c.values())
for i in l:print(t-c[i]+1)

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    dica = {}
    for i in range(n):
        if a[i] in dica:
            dica[a[i]] += 1
        else:
            dica[a[i]] = 1
    count = list(dica.values())
    total = list(map(lambda x: x*(x-1)/2, count))
    sumtotal = sum(total)

    for i in range(n):
        test = a[i]
        valuetest = dica[test]
        subtract = valuetest*(valuetest-1)/2
        add = (valuetest-1)*(valuetest-2)/2
        print(int((sumtotal-subtract+add)))
resolve()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
c = [0 for _ in range(n+1)]
r = []
for i in range(n):
    c[a[i-1]-1] += 1
for i in range(n):
    r.append(c[i])
kai = []
v = []
for i in range(len(r)):
    kai.append((r[i]*(r[i]-1)//2))
    v.append(kai[i] - max(((r[i]-1)*(r[i]-2)//2),0))

ans = sum(kai)
for i in range(len(a)):
    print(ans - v[a[i]-1])

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

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

all_pairs = 0
for value in value_count:
	all_pairs += (value_count[value] * (value_count[value] -1)) // 2

for i in range(n):
	value = a[i]
	used_to =  (value_count[value] * (value_count[value] -1)) // 2
	now =  ((value_count[value] - 2) * (value_count[value] -1)) // 2
	print(all_pairs - used_to + now)

Solution (Python):
import collections
import math

def combinations_count2(n, memo):
    if n <= 1:
        return 0
    if n in memo.keys():
        return memo[n]
    else:
        memo[n]= math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
        return memo[n]


n = input()
li = input().split()
c = collections.Counter(li)
memo = dict()
Sum = 0
for cv in c.values():
    Sum += combinations_count2(cv,memo)
for l in li:
    print(Sum - c[l] +1)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
countA = Counter(A)
ansA = [v*(v-1)//2 for k,v in countA.items()]
sumA = sum(ansA)

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

Solution (Python):
n = int(input())
hoge = input().split()

m = {}
for i in range(len(hoge)):
    if not(hoge[i] in m):
        m[hoge[i]] = 0
    m[hoge[i]] += 1

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

for i in range(len(hoge)):
    num1 = m[hoge[i]]
    if num1 < 2:
        print(int(al))
    else:
        num2 = (num1 * (num1 -1)/2) - ((num1 - 1) * (num1 -2)/2)
        print(int(al - num2))

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    d = dict()
    for i in A:
        d[i] = 1 + d.get(i, 0)

    d_sel = dict()
    total = 0
    for k,v in d.items():
        sel = v * (v-1) // 2
        d_sel[k] = sel
        total += sel

    for i in range(N):
        print(total - d_sel[A[i]] + (d[A[i]]-1) * (d[A[i]]-2)//2)

    return



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

if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

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

count = defaultdict(int)
for i in balls:
  count[i] += 1

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

sum = 0
for i in count:
   sum += cmb(count[i])








for i in balls:
  print(sum - (count[i]-1))

Solution (Python):
import collections as cc
n=int(input())
l=list(map(int,input().split()))
ff=cc.Counter(l)
ff=dict(ff)
ans=0
for i in ff:
    x=ff[i]
    ans+=(x*(x-1))//2
v={}
for i in set(l):
    vv=max(ff[i]-1,0)
    te=ans-(ff[i]*(ff[i]-1))//2+(vv*(vv-1))//2
    v[i]=te
for i in l:
    print(v[i])

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

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

S = [0]*(N+1)
for key, val in d.items():
  S[key] = (val * (val-1))//2

ans = sum(S)
for k in range(N):
  t = d[A[k]]*(d[A[k]]-1)//2
  add = (d[A[k]]-1)*(d[A[k]]-2)//2
  print(ans - t + add)

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

from collections import defaultdict

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


for key, val in list(cnt.items()):
    if val < 2:
        cnt.pop(key)


total = 0
for val in cnt.values():
    if val >= 2:
        total += val*(val-1)//2

for i in range(n):
    diff = 0
    if a[i] in cnt:
        y = (cnt[a[i]]-1)*cnt[a[i]]//2
        x = (cnt[a[i]]-1)*(cnt[a[i]]-2)//2
        diff = y - x
    print(total - diff)

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

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

Solution (Python):

from collections import Counter
def C2(x):
    return x*(x-1)//2

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    d = Counter(a) 
    pans = 0
    for v in d.values():
        pans += C2(v)

    for k in range(n):
        p = pans
        val = d[a[k]]
        p = p - C2(val) + C2(val-1)
        print(p)
main()

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

dicts = {}
for i in range(n):
    if input_list[i] not in dicts.keys():
        dicts[input_list[i]] = 0
    dicts[input_list[i]] += 1

sums = 0
dicts2 = {}
for key, value in dicts.items():
    if dicts[key] >= 2:
        dicts2[key] = value * (value - 1) // 2
    else:
        dicts2[key] = 0
    sums += dicts2[key]


for j in range(n):
    if dicts[input_list[j]] >= 2:
        value1 = dicts[input_list[j]]
        res = sums - dicts2[input_list[j]] + (value1-1) * (value1 - 2) // 2
    else:
        res = sums
    print(str(res))

Solution (Python):
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, = I()
a = I()

d = dd(int)
for i in a:
    d[i] += 1

def get(x):
    return x * (x - 1) // 2

total = 0
for i in d:
    total += get(d[i])

for i in range(n):
    ans = total - get(d[a[i]]) + get(d[a[i]] - 1)
    print(ans)

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

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

nums = {}
for a in A:
    if a in nums:
        nums[a] += 1
    else:
        nums[a] = 1

sum = 0
for key in nums:
    num = nums[key]
    sum += cmb2(num)

for k in range(N):
    ans = sum - cmb2( nums[A[k]] ) + cmb2( nums[A[k]]-1 )
    print(ans)

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

adic = ddic(int)
for a in AA:
    adic[a] += 1
    
patdic = {}
asum = 0
for key in adic.keys():
    num = adic[key]
    seki = num * (num-1)//2
    
    patdic[key] = seki
    asum += seki
    


    






for a in AA:
    ans = asum - (patdic[a] ) + (patdic[a] - adic[a]+1)
    print(ans)

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

dict = {}

for i in A:
  if i in dict:
    dict[i] = dict[i] + 1
  else:
    dict[i] = 1


total = 0;
for i in dict:

  total += dict[i]*(dict[i]-1) // 2



for i in A:
  print(total - dict[i] + 1)

Solution (Python):

import collections


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

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

c = collections.Counter(A)

comb_sum = sum([comb_count(n) for n in c.values()])

for a in A:
    print(comb_sum - (c[a] - 1))

Solution (Python):
import math
import collections

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

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

C = collections.Counter(A)
csum = 0

for c in C.values():
    if c > 1:
        csum += combinations_count(c, 2)

for a in A:
    print(csum - (C[a]-1))

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

Cntr = collections.Counter(A)
Sum = 0
for c in Cntr.items():
    key = c[0]
    val = c[1]
    if val > 1:
        Sum += val * (val - 1) // 2

for a in A:
    print(Sum-(Cntr[a]-1))

Solution (Python):
from collections import defaultdict

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

count_dict = defaultdict(int)
for cal in A:
  count_dict[cal] += 1
    
from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

count_result = defaultdict(list)
for k,v in count_dict.items():
  if v > 2:
    count_result[k] = [combinations_count(v,2), combinations_count(v-1,2)]
  elif v == 2:
    count_result[k] = [1,0]    
  else:
    count_result[k] = [0,0]

all_ans = 0
for v in count_result.values():
  all_ans += v[0]


for _ in A:
  ans = all_ans - count_result[_][0] + count_result[_][1]
  print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = sorted(A)
C = [0 for i in range(N+1)]

bef = 0
for i in range(N):
  if bef!=B[i]:
    bef = B[i]
    C[B[i]] = 1
  else: C[B[i]]+=1

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


for i in range(N):
  dif = C[A[i]]*(C[A[i]]-1)//2 - (C[A[i]]-1)*(C[A[i]]-2)//2
  print(sum-dif)

Solution (Python):
from collections import Counter

n = int(input())
ball = [int(i) for i in input().split()]
counter = Counter(ball)
result = sum([i * (i - 1) // 2 for i in counter.values()])

for i in range(n):
  c = counter[ball[i]]
  print(result - (c - 1))

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

c = dict(Counter(a))
dp_on = {}
dp_off = {}
for k, v in c.items():
    if v == 1:
        dp_on[k] = 0
        dp_off[k] = 0
    elif v == 2:
        dp_on[k] = 1
        dp_off[k] = 0
    else:
        dp_on[k] = int(v*(v-1)/2)
        dp_off[k] = int((v-1)*(v-2)/2)
comb_sum = sum([v for v in dp_on.values()])
for i in range(n):
    print(comb_sum - dp_on[a[i]] + dp_off[a[i]])

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
ball = [0] * N
ans = 0

for A in A_list:
    ans += ball[A - 1]
    ball[A - 1] += 1
    

[print(ans - (ball[A_list[k] - 1]) + 1) for k in range(N)]

Solution (Python):
from collections import Counter
import math

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


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

Ac = Counter(A)

sumc = 0

for Ae in dict(Ac):
    sumc += comb(Ac[Ae])



for k in A:
    print(sumc - comb(Ac[k]) + comb(Ac[k]-1))

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

from collections import Counter

c = Counter(a)

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

base = 0
for aa in c.keys():
    x = c[aa]
    base += comb(x)

for aa in a:
    ans = base - comb(c[aa]) + comb(c[aa] - 1)
    print(ans)

Solution (Python):
import collections
def input_int_list():
    return [int(thing) for thing in input().split(" ")]

nCr = {}

def nC2(n):
    if n in nCr:
        return nCr[n]
    else:
        val=n*(n-1)//2
        nCr[n]=val
        return val



N=int(input())
A_list=input_int_list()
int_count=collections.Counter(A_list)
the_sum=sum([nC2(int_count[i]) for i in int_count])
for i in range(N):
    number=A_list[i]
    c=int_count[number]
    print(the_sum-c+1)

Solution (Python):
import collections
n = int(input())
a = [int(i) for i in input().split()] 

num_dict = collections.Counter(a)
min_dict = {}
max_num = 0
for k,v in num_dict.items():
  keymax = v*(v-1)/2
  max_num += keymax
  if (v == 1):
    min_dict[k] = 0
  else:
    min_dict[k] = keymax - (v-1)*(v-2)/2

for i in a:
  print(int(max_num-min_dict[i]))