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):
d = {}

N=int(input())

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


for a in A:
    if a in d.keys():
        d[a] += 1
    else:
        d[a] = 1

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

ret1 = {}
ret2 = {}
for k in d.keys():
    ret1[k]=solve(d[k])
    ret2[k]=solve(d[k]-1)


sums=sum(ret1.values())
for  i in range(N):
    ans = sums - ret1[A[i]] + ret2[A[i]]
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = [0] * (n+1)
for x in a: cnt[x] += 1
s = 0
for x in cnt: s += x * (x-1) // 2

for x in a:
    ans = s
    if cnt[x] >= 2: ans -= cnt[x]-1
    print(ans)

Solution (Python):

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

dict_count = {}


for a in A:
  if a in dict_count.keys():
    dict_count[a] += 1
  else:
    dict_count[a] = 1


count_max = 0
for key, value in dict_count.items():
  count_max += value*(value-1)//2


for a in A:
  print(count_max-dict_count[a]+1)

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[a[i]] = 1
st = 0
for i in dic.values():
    st += i*(i-1)//2
for i in range(n):
    j = a[i]
    if dic[j] == 1:
        print(st)
    else:
        d = dic[j]
        print(st-d*(d-1)//2 + (d-1)*(d-2)//2)

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


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



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


def solve(v):
    if v < 2:
        return ans
    return ans - cmb(v, 2) + cmb(v - 1, 2)


N = int(input())
A = inpl()
cA = Counter(A)

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

for a in A:
    print(solve(cA[a]))

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

def nc2(x): return x*(x-1)//2
dic = {i+1:0 for i in range(N)}
for a in A: dic[a] += 1

tmp = 0
for i in range(N):
    tmp += nc2(dic[i+1])

ans = [0] * (N+1)
for i in range(1,N+1):
    print(tmp+nc2(dic[A[i-1]]-1)-nc2(dic[A[i-1]]))

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

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


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



for i in range(n):
    
    
    
    tmp=ans-dic[a[i]]*(dic[a[i]]-1)//2+(dic[a[i]]-1)*(dic[a[i]]-2)//2
    print(tmp)

Solution (Python):
from collections import Counter

N=int(input())
A=[int(i) for i in input().split(" ")]
c=Counter(A)
c=dict(c)
base=0
for key in c:
    base+=c[key]*(c[key]-1)//2


for a in A:
    _base=base
    ca=c[a]
    if ca>1:
        _base-=ca*(ca-1)//2
        _base+=(ca-1)*(ca-2)//2
    print(_base)

Solution (Python):
from collections import Counter

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

count = Counter(A)
s = 0
for k, v in count.items():
    s += v*(v-1)//2

for i in range(len(A)):
    print(s - count[A[i]] + 1)

Solution (Python):
from collections import Counter

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

ct = Counter(A)
d = dict()
for k, v in ct.items():
    d[k] = v * (v - 1) // 2

s = sum(d.values())

for i in range(N):
    print(s - (ct[A[i]] - 1))

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    X = [0] * (N + 1)
    
    for a in A:
        X[a] += 1
    s = 0
    for x in X:
        s += x*(x-1)//2

    for a in A:
        print(s - X[a] + 1)



    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()))
d={}
for a in A:
    if a not in d:
        d[a]=1
    else:
        d[a]+=1
ans=0
for k in d.keys():
    e=d[k]
    ans+=e*(e-1)//2
for a in A:
    keep=ans
    e=d[a]
    temp=e*(e-1)//2
    ans-=temp
    ans+=(e-1)*(e-2)//2
    print(ans)
    ans=keep

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

ans = []
for a in A:
    ans.append(al - ctr[a] + 1)
print(*ans, sep='\n')

Solution (Python):

import sys
input = sys.stdin.readline
from collections import Counter

n = int(input())
a = [int(item) for item in input().split()]
comb = [0] * (n + 1)
cnt = Counter(a)

for key in cnt.keys():
    comb[key] = cnt[key] * (cnt[key] - 1) // 2
total = sum(comb)

for item in a:
    if comb[item] <= 1:
        diff = comb[item]
    else:
        diff = comb[item] - (comb[item] * (cnt[item] - 2)) // cnt[item]
    print(total - diff)

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

L = {}
for i in A:
    if i in L:
        L[i] += 1
    else:
        L[i]= 1

L2 = []
for i in L:
    L2.append(L[i]*(L[i]-1)/2)
    
res = sum(L2)
for i in A:
    print(int(res - L[i] + 1))

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

def cmb(m, n):
    if m - n < n:
        n = m - n
    if n == 0:
        return 1
    if n == 1:
        return m
    
    numerator = [m - n + k + 1 for k in range(n)]
    denominator = [k + 1 for k in range(n)]

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

    return result


Dict = {x:0 for x in range(n+1)}
for a in A:
    Dict[a] += 1

Non_minus = [0 for _ in range(n+1)]
one_minus = [0 for _ in range(n+1)]

for i in range(1, n+1):
    if Dict[i] <= 1:
        Non_minus[i] = 0
        one_minus[i] = 0
    elif Dict[i] == 2:
        Non_minus[i] = 1
        one_minus[i] = 0
    else:
        Non_minus[i] = cmb(Dict[i],2)
        one_minus[i] = cmb(Dict[i]-1, 2)

total_non = sum(Non_minus)




for i in range(n):
    ans = total_non - Non_minus[A[i]] + one_minus[A[i]]
    print(ans)

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

cnt = Counter(a)

ans = 0

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

for k in a:
    print(ans - cnt[k] + 1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import math
def ncr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
import collections
d=collections.defaultdict(int)

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

total=0
for i in d.values():
    if i>=2:
        total+=ncr(i,2)

for i in a:
    print(total-d[i]+1)

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

Solution (Python):
import sys
import itertools
import bisect
from operator import itemgetter
 
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()
def combi(n, r):
  r = min(r, n-r)
  numer = denom = 1
  for i in range(1, r+1):
    numer = numer * (n+1-i) %mod
    denom = denom * i %mod
  return numer * pow(denom, mod-2, mod) %mod
N = I()
a = IL()

dic = {}
for i in a:
  if i in dic:
    dic[i] += 1
  else:
    dic[i] = 1

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

for i in a:
  n = dic[i]
  print(ans - n*(n-1)//2 + (n-1)*(n-2)//2 )

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

li = [0]*(n+1)

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

li2 = [[0]*2 for _ in range(n+1)]

for i in range(n+1):
    for j in range(2):
        if j == 0:
            li2[i][j] = li[i] * (li[i] - 1) // 2
        else:
            li2[i][j] = max((li[i] - 1) * (li[i] - 2) // 2, 0)

sum1 = 0
for i in range(n+1):
    sum1 += li2[i][0]

sum2 = [0]*(n+1)

for i in range(n+1):
    sum2[i] = sum1 - li2[i][0] + li2[i][1]

for i in a:
    print(sum2[i])

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

Solution (Python):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 

sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input():
    return sys.stdin.readline()[:-1]
def pri(x):
    if type(x) is list:
        print('\n'.join(map(str, x)))
    else:
        print(x)

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


c = Counter(A)
cnt = 0
for ci in c.values():
    cnt += ci*(ci-1)//2

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

Solution (Python):

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


x=[0]*N
y=[0]*N
z=[0]*N
for i in range(N):
     j=A[i]
     x[j-1] += 1

for i in range(N):
    j=i
    y[i]=(x[j]*(x[j]-1))//2
    if x[j]==0:
        z[i]=0
    else:
        z[i]=((x[j]-2)*(x[j]-1))//2

yy=sum(y)
zz=sum(z)


for i in range(N):
    j=A[i]
    print(yy-y[j-1]+z[j-1])

Solution (Python):
from collections import defaultdict

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

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


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


for i in range(N):
    a = A[i]
    print(total - (d[a] - 1))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
from collections import defaultdict
n = int(input())
a = [int(x) for x in input().split()]
d = defaultdict(int)
for v in a:
    d[v] += 1
r = 0
for v in d:
    r += d[v] * (d[v] - 1) // 2
for v in a:
    if d[v] == 1:
        print(r)
    else:
        r1 = r - d[v] * (d[v] - 1) // 2 + (d[v] - 2) * (d[v] - 1) // 2
        print(r1)

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

Solution (Python):
from collections import Counter

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

d = Counter(a)

ans = 0

for i, c in d.items():
    ans += d[i] * (d[i] - 1) // 2

for i in a:
    ans_ = ans
    ans_ -= d[i] * (d[i] - 1) // 2
    ans_ += (d[i] - 1) * (d[i] - 2) // 2

    print(ans_)

Solution (Python):
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
d = defaultdict(int)
for a in A:
    d[a] += 1
ans = 0
for v in d.values():
    ans += v*(v-1) //2 

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

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

def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]

    t = Counter(A)
    total = 0
    for k, v in t.items():
        total += v * (v-1) // 2

    for a in A:
        tmp1 = t[a] * (t[a]-1) // 2
        tmp2 = (t[a]-1) * (t[a]-2) // 2
        print(total - tmp1 + tmp2)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter, deque







import sys



readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    cntr = Counter(a)
    tsum = 0

    for key, value in cntr.items():
        temp = (value - 1) * value // 2
        tsum += temp

    for i in range(n):
        k = a[i]
        v = cntr[k]
        print(tsum-(v-1))


if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
cnt = [0]*200001
for i in a:
    cnt[i] += 1
ans = 0
for i in range(200001):
    if cnt[i] <= 1: continue
    else: 
        ans += cnt[i]*(cnt[i]-1)//2
for i in range(n):
    print(ans-cnt[a[i]]+1)

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

for i in range(N):
    part = c[A[i]]-1
    print(zenbu-part)

Solution (Python):

import sys
from collections import Counter as cl

def solve(N: int, A: "List[int]"):
    d = cl(A)
    ct = 0
    for x in d.keys():
        ct += d[x] * (d[x] - 1) //2
    for x in A:
        print(ct - (d[x]-1))

    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 Counter

n = int(input())
lst = list(map(int,input().split()))
c = Counter(lst)
res = 0
for k,v in c.items():
    if v >= 2:
        res += v*(v-1)//2
for x in lst:
    if c[x]<2:
        print(res)
    else:
        print(res - (c[x] - 1))

Solution (Python):
from collections import defaultdict

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

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

total = 0
for k, v in nums.items():
    total += v * (v - 1) // 2

for a in A:
    v = nums[a]
    diff = v * (v - 1) // 2 - (v - 1) * (v - 2) // 2
    print(total - diff)

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

from collections import Counter

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

c = Counter(a)

total = 0

for i in c.keys():
    
    if c[i] - 1 > 0:
        total += int((1 / 2) * c[i] * (c[i] - 1))



for i in a:
    ans = total - int((1 / 2) * c[i] * (c[i] - 1))
    if c[i] - 2 > 0:
        ans += int((1 / 2) * (c[i] - 1) * (c[i] - 2))
    print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    B = [0] * (N + 1)
    for a in A:
        B[a] += 1
    S = 0
    for b in B:
        S += b * (b-1) // 2
    for a in A:
        c = B[a]
        print(S - c*(c-1)//2 + (c-1)*(c-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):
n = int(input())
a = list(map(int, input().split()))

from collections import Counter
ca = Counter(a)

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

for i in a:
    diff = ca[i]-1
    print(total - diff)

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

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    counts = [0]*N
    combinations = [(0,0)] *N
    ncombinations =[0]*N
    for i in range(N):
        counts[A[i]] += 1
    sum = 0
    for i in range(N):
        c1 = counts[i] * (counts[i]-1)//2 if counts[i] >=2 else 0
        c2 = (counts[i] - 1) * (counts[i] - 2)//2 if counts[i] >= 3 else 0
        combinations[i] = (c1,c2)
        sum += c1
    for i in range(N):
        ncombinations[i] = sum - (combinations[i][0] - combinations[i][1])
    for i in range(N):
        print(ncombinations[A[i]])
    return

def int1(s):
    return int(s)-1

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 = [int1(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

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

c_dic = {}
for i in range(1,n+1):
    c_dic[i] = 0

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

ans_dic = {}
ans = 0
for i in range(1,n+1):
    ans_dic[i] = (c_dic[i] * (c_dic[i] -1) )//2
    ans += ans_dic[i]

for i in range(n):
    print(ans - max(c_dic[a[i]] -1 , 0))

Solution (Python):
def main():
    N = int(input())
    A = [int(n) for n in input().split()]
    dic = [0]*(N+1)
    for a in A:
        dic[a]+=1
    S = 0
    for i in range(1,N+1):
        S += dic[i] * (dic[i]-1) // 2
    for a in A:
        print(S - dic[a] * (dic[a]-1) // 2 + (dic[a]-1) * (dic[a]-2) // 2)
main()

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

aa = {}
bb = {}
mx = 0
for k,v in d.items():
    aa[k] = v * (v-1) // 2
    bb[k] = (v-1) * (v-2) // 2
    mx += aa[k]

for i in a:
    print(mx - aa[i] + bb[i])

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

c=Counter(a)
s=0

for i,m in c.items():
    s+=m*(m-1)//2

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

Solution (Python):
n=int(input())
d={}
a=list(map(int,input().split()))
for ii in a:
    d[ii]=d.get(ii,0) + 1
ans=0
for i in d:
    ans+=(d[i]*(d[i]-1))//2
for i in a:
    p=(d[i]*(d[i]-1))//2
    q=((d[i]-1)*(d[i]-2))//2
    print(ans -p+q)

Solution (Python):
from collections import defaultdict

def main():
    N = int(input())
    A = list(map(int, input().split()))
    d = defaultdict(int)
    for a in A:
        d[a] += 1

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

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



if __name__ == "__main__":
    main()

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

ct = Counter(As)
ans = 0
for k, v in ct.items():
    ans += (v * (v-1)) // 2

for a in As:
    c = ct[a]
    if c >= 2:
        print(ans - ((c * (c-1)) // 2) + (((c-1) * (c-2)) // 2))
    else:
        print(ans)

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

my_dict = dict()
k_dict = dict()

for i in range(len(a_list)):
    if a_list[i] in my_dict:
        my_dict[a_list[i]] = my_dict[a_list[i]] + 1
    else:
        my_dict[a_list[i]] = 1
    k_dict[i] = a_list[i]

num_c = 0

for item in my_dict:
    num = my_dict[item]
    num_c += ((num * (num - 1)) // 2)

for i in range(n):
    if k_dict[i] in my_dict:
        num = my_dict[k_dict[i]]
        num_1 = num - 1
        print(num_c - ((num * (num - 1)) // 2) + ((num_1 * (num_1 - 1)) // 2))

Solution (Python):
n=int(input())
t=[int(i) for i in input().split()]
cnt=[0]*(n+1)
for i in t:
	cnt[i]+=1
	
s = 0
for i in cnt:
	s+=i*(i-1)//2

for i in t:
	g = cnt[i]
	ans = s - g*(g-1)//2 + (g-1)*(g-2)//2
	print(ans)

Solution (Python):
import bisect

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

b=sorted(a)

kazu=[]
shurui=[]

gen=0
for i in range(n):
    if(gen!=b[i]):
        kazu.append(1)
        shurui.append(b[i])
        gen=b[i]
    else:
        kazu[-1]+=1


gokei=0
herasi=[]

for k in kazu:
    gokei+=k*(k-1)//2


ans=[]
for i in range(n):
    basho=bisect.bisect_left(shurui,a[i])
    print(gokei-kazu[basho]+1)

Solution (Python):
import bisect

n=int(input())
alist = list(map(int, input().split()))
blist=sorted(alist)
clist=[]
dlist=[]
mae=0
for bl in blist:
    if bl==mae:
        clist[-1][1]+=1
    else:
        clist.append([bl, 1])
        dlist.append(bl)
    mae=bl

wa=0
for cl in clist:
    wa+=(cl[1]*(cl[1]-1))//2
for al in alist:
    pos=bisect.bisect_left(dlist, al)
    moto=clist[pos][1]
    if moto==0:
        print(wa)
    else:
        print(wa-moto+1)

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

L=[0]*(max(A)+1)
for i in A:
    L[i]+=1

M=[0]*(len(L))
for i in range(len(L)):
    M[i]=L[i]*(L[i]-1)//2

N=[0]*(len(L))
for i in range(len(L)):
    N[i]=(L[i]-1)*(L[i]-2)//2

S=sum(M)
for i in range(n):
    print(S-M[A[i]]+N[A[i]])

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

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

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

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

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)
for v in a:
    d[v] += 1

max_ans = 0
for k, v in d.items():
    max_ans += v * (v-1) // 2

ans = []
for v in a:
    if d[v] > 1:
        ans.append(max_ans - ((d[v]) * (d[v]-1) // 2) + ((d[v]-1) * (d[v]-2) // 2))
    else:
        ans.append(max_ans)

print("\n".join(list(map(str, ans))))

Solution (Python):



from math import *
from copy import *
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize
from operator import *				
from itertools import *
from collections import Counter		
from collections import defaultdict 

'''
5
1 1 2 1 2
'''

def solve(l):
	n = len(l)
	d = dict(Counter(l))
	ans = []
	tot = 0
	for i in d:
		j = d[i]
		tot += j*(j-1)//2
	for i in range(n):
		curr = d[l[i]]
		temp = tot - curr + 1
		ans.append(temp)
	return ans


N = int(stdin.readline())
l = list(map(int, stdin.readline().rstrip().split()))
ans = solve(l)
for i in ans:
	print(i)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
D = {}
for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
S = 0
for i in D:
  S += (D[i] * (D[i] - 1)) // 2
for i in range(N):
  print(S - (D[A[i]] - 1))

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
bl = [(i * (i - 1)) // 2 for i in range(N + 3)]
L = [0, 0]
for i in range(1, N + 1):
    L.append(bl[i + 1] - bl[i])
D = defaultdict(int)
for a in A:
    D[a] += 1
RES = 0
for d in D.values():
    RES += bl[d]

for a in A:
    print(RES - L[D[a]])

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

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

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

for a in A:
    n = d[a]
    if n >= 2:
        print(ans - n + 1)
    else:
        print(ans)

Solution (Python):
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N = int(input())
A = [int(x) for x in input().strip().split()]
C = defaultdict(int)
for a in A:
    C[a] += 1

sum = 0
for i, c in C.items():
    sum += (c * (c-1)) // 2

for a in A:
    print(sum - (C[a]*(C[a]-1))//2 + ((C[a]-1)*(C[a]-2))//2)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
from collections import Counter
 
def main():
    N = int(input())
    a = list(map(int,input().split()))
    
    c = [0 for _ in range(max(a))]
    for num in a:
        c[num-1] += 1
    
    count = 0
    for num in c:
        count += (num*(num-1))//2

    for i in a:
        if c[i-1] == 1:
            print(count)
        else:
            num = c[i-1]
            d = (num*(num-1))//2
            p = ((num-1)*(num-2))//2
            print(count-d+p)
    
if __name__ == "__main__":
    main()

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

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

def binom(n, r):
    if n < r or n < 0 or r < 0: return 0
    
    if n - r < r: r = n - r
    if r == 0: return 1
    elif r == 1: return n

    numerator = [n - r + 1 + k for k in range(r)]  
    denominator = [1 + k 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
    
    
    return reduce(lambda x, y: x*y, numerator)

N = int(input())
*A, = map(int, input().split())
count = Counter(A)
use_all = {}
use_minus_one = {}
total = 0
for k, v in count.items():
    temp = binom(v, 2)
    total += temp
    use_all[k] = temp
    use_minus_one[k] = binom(v - 1, 2)
for a in A:
    ans = total - use_all[a] + use_minus_one[a]
    print(ans)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
D={}
for i in range(N):
    if A[i] not in D:
        D[A[i]]=1
    else:
        D[A[i]]+=1
        
MAX=0
for a,b in D.items():
    MAX+=b*(b-1)//2


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

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

Solution (Python):
import sys
from functools import reduce
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res


N = n_in()
A = l_in()

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

for a in A:
    C[a-1] += 1

res = reduce(lambda memo, c: memo+c*(c-1)//2, C, 0)

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

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

Solution (Python):
from itertools import groupby
import copy

def equalfind(A):
    A.sort()
    s=1
    count=0
    if len(A)==1:
        return count
    else:
        for i in range(0,len(A)-1):
            if A[i]==A[i+1]:
                s+=1
            else:
                count+=s*(s-1)//2
                s=1
        return count+s*(s-1)//2


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

B=copy.copy(A)
s=equalfind(B)
data=groupby(B)
ans=[0 for i in range(0,N+1)]
for key,group in data:
    g=len(list(group))
    ans[key]=g

for i in range(0,N):
    minus=ans[A[i]]-1
    print(s-minus)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
mod = 92709568269121

N = int(input())

def factrial(N):
    fc = [1, 1] + [None] * N
    inv = [0, 1] + [None] * N
    fcInv = [1, 1] + [None] * N
    for i in range(2, N+1):
        fc[i] = i*fc[i-1]%mod
        inv[i] = mod-(inv[mod%i]*(mod//i))%mod
        fcInv[i] = fcInv[i-1]*inv[i]%mod
    return fc, fcInv
fc, fcInv = factrial(N+1)

def modCom(n, k):
    return 0 if n < k else fc[n] * fcInv[k] * fcInv[n-k] % mod

A = [int(x) for x in input().split()]
C = Counter(A)
X = 0
dic1 = defaultdict(int)
dic2 = defaultdict(int)
for x in C.keys():
    dic1[x] = modCom(C[x], 2)
    dic2[x] = modCom(C[x]-1, 2)
    X = X + dic1[x]
for x in A:
    print(X - dic1[x] + dic2[x])

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

d = defaultdict(int)
for a in As:
    d[a] += 1
cnt = 0
for k in d.keys():
    
    cnt += (d[k]-1)*(d[k])//2

for a in As:
    x = d[a]
    waru = x*(x-1)//2
    kakeru = (x-1)*(x-2) //2
    print(cnt-waru+kakeru)

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

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

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

rec = [0]*(n+1)
for a in ls:
  rec[a] += 1

ans = 0
for a in rec:
  ans += a*(a-1)//2
  
for a in ls:
  print(ans - rec[a] +1)

Solution (Python):
def main():
    N = int(input())
    *a, = map(int, input().split())

    d = [0] * (N + 1)
    for x in a:
        d[x] += 1

    b = 0
    for x in d:
        b += x * (x - 1) // 2

    for x in a:
        t = b - (d[x] - 1)

        print(t)


if __name__ == '__main__':
    main()

Solution (Python):
import collections

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

c = collections.Counter(A)

total = sum([ c[n]*(c[n] -1) //2 for n in c])

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

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))
B = [0] * (n+1)
for i in range(n):
    B[A[i]] += 1

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

su = sum(C)

for i in range(n):
    print(su - C[A[i]] + (B[A[i]]-1)*(B[A[i]]-2)//2)