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



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

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

from collections import defaultdict
c = defaultdict(int)
cm = set()
c2 = list()
num = 0
for i in range(N):
  c[A[i]] += 1
  cm.add(A[i])
for i in list(cm):
  num += c[i]*(c[i]-1)//2
for i in range(N):
  ans = num - c[A[i]] + 1
  c2.append(ans)

for cs in c2:
  print(cs)

Submission status: Accepted

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

#N個のボールに対して、書かれている数字は1~Nまで

dic1 = {}
for i in range(N):
    dic1[i+1] = 0

#dicにkeyとなる数字の個数を入れていく
for i in range(N):
    if A[i] in dic1.keys():
        dic1[A[i]] += 1

dic2 = {}
#組み合わせの数を入れる   
for i in range(N):
    dic2[i+1] = dic1[i+1]*(dic1[i+1]-1)/2

ans1 = 0
for i in range(N):
    ans1 += dic2[i+1]

for i in range(N):
    ans2 = ans1
    ans2 -= dic2[A[i]] #組み合わせの数を引く
    ans2 += (dic1[A[i]]-1)*(dic1[A[i]]-2)/2 #新たな組み合わせの数を足す
    print(int(ans2))

Submission status: Accepted

Solution code:
N = int(input())
lis = input().split()

count_list = [0] * (N + 1)

for k in lis:
  count_list[int(k)] += 1

Sum = 0
for k in count_list:
  Sum += ( k * (k-1)) // 2



for i in lis:
  count = count_list[int(i)]
  s = int(count) - 1
  ans = Sum - s
  print(ans)

Submission status: Accepted

Solution code:
def f(m):
    return 0 if m < 2 else m * (m-1) // 2

def solve(n, a):
    D = {}
    for x in a:
        if not x in D:
            D[x] = 0
        D[x] += 1
    total = 0
    for x, num in D.items():
        total += f(num)
    for x in a:
        num = D[x]
        print(total - f(num) + f(num-1))

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split(" ")))
m = Counter(A)
def nck(n, k): 
    C = [[0 for x in range(k+1)] for x in range(n+1)] 
  
    # Calculate value of Binomial Coefficient in bottom up manner 
    for i in range(n+1): 
        for j in range(min(i, k)+1): 
            # Base Cases 
            if j == 0 or j == i: 
                C[i][j] = 1
  
            # Calculate value using previously stored values 
            else: 
                C[i][j] = C[i-1][j-1] + C[i-1][j] 
  
    return C

most_common,num_most_common = m.most_common(1)[0]
CHOOSE_MATRIX = nck(num_most_common, 2)
total = 0
for key, value in m.items():
        # if key == A[i]:
        #     continue
        # else:
    total += CHOOSE_MATRIX[value][2]


for i in range(len(A)):
    newtotal = total
    # m[A[i]] -= 1
    # for key, value in m.items():
    #     # if key == A[i]:
    #     #     continue
    #     # else:
    #     total += CHOOSE_MATRIX[value][2]
    # m[A[i]] += 1
    tosubtract = CHOOSE_MATRIX[m[A[i]]][2]
    newtotal -= tosubtract
    newtotal += CHOOSE_MATRIX[m[A[i]]- 1][2]
    print(newtotal)
    # print(uniquethings*(uniquethings)//2)
    # print(total)

Submission status: Accepted

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

lst = [0]*(2*10**5)
set = 0

for i in l:
    lst[i-1] += 1
for i in lst:
    if i>=2:
        set += i*(i-1)/2

for i in l:
    print(int(set - (lst[i-1]-1)))

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=[0]*N
sum=0
for i in range(N):
  b[a[i]-1]+=1

for i in range(N):
  sum+=b[i]*(b[i]-1)//2
for i in range(N):
  print(sum-b[a[i]-1]+1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import Counter as c
d=c(a)
s=0
for i in d.values():
  s+=i*(i-1)//2
for i in a:
  t=d[i]
  if t==1:
    print(s)
  else:
    print(s-(t*(t-1)//2)+((t-1)*(t-2)//2))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    total = 0
    for x in C:
        total += C[x] * (C[x] - 1) // 2
    for a in A:
        ans = total - C[a] * (C[a] - 1) // 2 + (C[a] - 1) * (C[a] - 2) // 2
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
a_list = list(map(int, input().split()))
a_counter = collections.Counter(a_list)

# print(a_counter)

sum = 0
for value in a_counter.values():
  if value == 1:
    continue
  sum += int(value * (value - 1) / 2)
  
# print(sum)

ans = 0
for a in a_list:
  ans = sum - (a_counter[a] - 1)
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
S = sum(c*(c-1)//2 for c in C.values())
for a in A:
  print(S-C[a]+1)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
ans = sum(cc*(cc-1)//2 for cc in c.values())
[print(ans-c[i]+1) for i in a]

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
x = collections.Counter(a)
d = []
s = 0
for i in x:
    c = x[i]*(x[i]-1)//2
    d.append([i,c])
    s += c
d = dict(d)
for i in a:
    ans = s
    ans -= d[i]
    ans += (x[i]-1)*(x[i]-2)//2
    print(ans)

Submission status: Accepted

Solution code:
inp = int(input())
lis = list(map(int, input().split()))

dic = {}

#count
for l in lis:
  if l in dic: dic[l] += 1
  else: dic[l] = 1

#dic_sum = dic.copy()
s = 0
#calcurate
for d in dic:
  s += int(dic[d]*(dic[d]-1)//2)
  
for i in range(inp):
  #dic_sum_n = dic_sum.copy()
  t = dic[lis[i]]
  print(s-int(t*(t-1)//2)+int((t-1)*(t-2)//2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections
import math

# 入力
N = int(input())
A = list(input().split())

c = collections.Counter(A)

def comb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

comb_sum = 0
for i in c:
    comb_sum += comb(c[i], 2)
     
for a in A:
    ans = comb_sum
    if c[a] >= 2:
        ans -= (c[a] - 1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import copy

def count(dic, key):
    ans = 0
    for i in key:
        d = dic[i]
        ans += d * (d - 1) // 2
    return(ans)

N = int(input())
A = list(map(int, input().split()))
a = dict(Counter(A))
k = list(a.keys())
m = count(a, k)
dic = {}
for i in k:
    dic[i] = m - a[i] + 1
for i in range(N):
    print(dic[A[i]])

Submission status: Accepted

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

C = [0] * (N + 1)
for i in A:
    C[i] += 1

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

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
cnts = Counter(a)

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

for ai in a:
    ans = _sum
    c = cnts[ai]
    ans -= c*(c-1)//2
    ans += (c-1)*(c-2)//2
    print(ans)

Submission status: Accepted

Solution code:
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
Total=0
for i in D:
    Total+=D[i]*(D[i]-1)//2

for i in A:
    print(Total-(D[i]-1))

Submission status: Accepted

Solution code:
def choose2(n):
    return n*(n-1) // 2
from collections import Counter
N = int(input())
*A, = map(int, input().split())
cnt = Counter(A)
ttl = 0
for v in cnt.values():
    ttl += choose2(v)
for a in A:
    ans = ttl
    ans -= choose2(cnt[a])
    ans += choose2(cnt[a] - 1)
    print(ans)
    cnt[a] + 1

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
*A, = map(int, input().split())
cnt = Counter(A)
ttl = 0
for v in cnt.values():
    ttl += v*(v-1)//2
for a in A:
    print(ttl - (cnt[a] - 1))

Submission status: Accepted

Solution code:
input_d1 = int(input()) 
input_d2 = list(input().split())

c = sorted(input_d2)
c.append('0')
e = {'0':0}
g = [0]
s = 0
f = 0

for i in range(0,input_d1):
    if c[i] == c[i+1]:
        pass
    else:
        g.append(i+1)
        e[c[i]] = i + 1 - g[s]
        s += 1
        f += e[c[i]]*(e[c[i]]-1)/2
        
e.pop('0')
d = {}

for i in e.keys():
    d[i] = 1 - e[i] + f


for i in input_d2:
    print(int(d[i]))

Submission status: Accepted

Solution code:
input_d1 = int(input()) 
input_d2 = list(input().split())

c = sorted(input_d2)
c.append('0')
e = {'0':0}
g = [0]
s = 0

for i in range(0,input_d1):
    if c[i] == c[i+1]:
        pass
    else:
        g.append(i+1)
        e[c[i]] = i + 1 - g[s]
        s += 1

e.pop('0')
d = {}
f = sum(map(lambda x:x*(x-1)/2 , e.values()))

for i in e.keys():
    d[i] = 1 - e[i] + f


for i in input_d2:
    print(int(d[i]))

Submission status: Accepted

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

b = [0]*200003

ans = 0

for i in a:
    b[i] += 1
    if b[i] >= 2:
        ans = ans + b[i]-1

for i in range(n):
    if b[a[i]] >= 2:
        print(ans-b[a[i]]+1)
    else:
        print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

counter = collections.Counter(A)

for val in counter.values():
    all_comb += combinations_count_two(val)

for i in A:
    ans = all_comb - combinations_count_two(counter[i]) + combinations_count_two(counter[i] - 1)
    print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

import collections
import copy

# 標準入力 <int>
N = int(input())

# 標準入力 <str> list[]
A = list(map(str, input().split()))

c = collections.Counter(A)
nodel = 0

for cv in c.values():
  if cv >= 2:
    nodel += int((cv * (cv-1))/2)

for a in A:
  ans = copy.copy(nodel)
  if c.get(a) >= 3:
    ans = ans - int((c.get(a)*(c.get(a)-1))/2) + int(((c.get(a)-1)*(c.get(a)-2))/2)
  elif c.get(a) == 2:
    ans -= 1
  print(ans)

Submission status: Accepted

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

L=0
C=[0]*N

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

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

Submission status: Accepted

Solution code:
import collections
import math


if __name__ == '__main__':
    N= int(input())
    a = list(map(int, input().split()))
    c = collections.Counter(a)
    result = 0
    for i in set(a):
        # c[i]の中から2個選ぶ
        result += (c[i]*(c[i]-1))/2

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    dict = {}
    for i in range(N):
        if A[i] not in dict:
            dict[A[i]] = 1
        else:
            dict[A[i]] += 1
    ans = 0
    for i, j  in dict.items():
        ans += (j - 1) * j // 2
    for i in range(N):
        print(ans - dict[A[i]] + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
# collections.counter 使い方
import collections

n=int(input())
a= list(map(int, input().split()))
c = collections.Counter(a)
d = c.most_common()
ans=0

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


for i in range(n):
    if c[a[i]]>=2:
        print(ans-c[a[i]]+1)
    else:
        print(ans)

Submission status: Accepted

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

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

ans = 0
for v in ad.values() :
  ans += v*(v-1)//2
  
for i in range(n) :
  v = ad[a[i]]
  print(ans-v*(v-1)//2+(v-1)*(v-2)//2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
c = [0] * (n + 1)
d = [0] * (n + 1)
for i in a:
    c[i] += 1
    d[i] = c[i] * (c[i] - 1) // 2
t = sum(d)
for i in a:
    if c[i] > 2:
        r = (((c[i] - 1) * (c[i] - 2)) // 2)
    else:
        r = 0
    print(t - d[i] + r)

Submission status: Accepted

Solution code:
N = int(input())
nlist = list(map(int, input().split()))

list = [0]*N
sum = 0
result = []

for i in range(N):
    list[nlist[i]-1] += 1

for j in range(N):
    sum += list[j]*(list[j]-1)/2

for k in range(N):
    result.append(int(sum+1-list[nlist[k]-1]))

for l in range(N):
    print(result[l])

Submission status: Accepted

Solution code:
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
T=0
for i in D:
    T+=D[i]*(D[i]-1)//2

for i in range(n):
    print(T-(D[A[i]]-1))

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = map(int, input().split())
li = []
for i in a:
    li.append(i)
collections.Counter(li)
m = 0
dic = collections.Counter(li)
for i in dic.values():
    m += int(i*(i-1)*0.5)
for k in range(n):
      print(m-dic[li[k]]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ind2clus = {}
clus2ind = {}
for i in range(N):
    if A[i] not in clus2ind:
        clus2ind[A[i]]=[]
    clus2ind[A[i]].append(i)
    ind2clus[i] = A[i]
ma = 0
for one in clus2ind:
    num = len(clus2ind[one])
    ma+=num*(num-1)//2
    
    
for i in range(N):
    num = len(clus2ind[ind2clus[i]])
    print(ma-num*(num-1)//2+(num-1)*(num-2)//2)

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
from collections import Counter
#template
#issueから始める
N = int(input())
A = inputlist()
c = Counter(A)
key = list(c.keys())
dic = {}
dica = {}
for i in key:
    dic[i] = c[i]*(c[i]-1)//2
    dica[i] = (c[i]-1)*(c[i]-2)//2
ans = 0
for i in key:
    ans += dic[i]
for i in range(N):
    suma = ans
    suma = suma - dic[A[i]] + dica[A[i]]
    print(suma)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A_list = list(map(int, input().split(' ')))
count_dict = collections.Counter(A_list)

sum_ = 0
for count in count_dict.values():
  sum_ += (count**2 - count) // 2

for A in A_list:
  print(sum_ - count_dict[A] + 1)

Submission status: Accepted

Solution code:
import random


def nC2(n):
    return int(n*(n-1)/2)

n = int(input())

a = [0]*n
a = list(map(int, input().split()))
a = [i-1 for i in a]

ty=[0]*n

for i in range(0, n):
    ty[a[i]]+=1

s = 0

for i in range(0, n):
    if ty[i]>=2:
        s += nC2(ty[i])

for k in range(0, n):
    if ty[a[k]]==2:
        print(s-1)
    elif ty[a[k]]>2:
        print(s-nC2(ty[a[k]])+nC2(ty[a[k]]-1))
    else:
        print(s)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [0 for i in range(n)]
s = 0

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

for i in range(n):
    s += (b[i]*(b[i]-1))//2

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

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
from collections import Counter
#template
#issueから始める
N = int(input())
A = inputlist()
c = Counter(A)
key = list(c.keys())
dic = {}
dica = {}
for i in key:
    dic[i] = c[i]*(c[i]-1)//2
    dica[i] = (c[i]-1)*(c[i]-2)//2
ans = 0
for i in key:
    ans += dic[i]
for i in range(N):
    suma = ans
    suma = suma - dic[A[i]] + dica[A[i]]
    print(suma)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import Counter
C = Counter(A)
ans = 0
for key,value in C.items():
    if value > 1:
        ans += value*(value-1)//2
for e in A:
    v = C[e]
    if v >= 3:
        print(ans-(v*(v-1)//2)+((v-1)*(v-2)//2))
    elif v == 2:
        print(ans-1)
    else:
        print(ans)

Submission status: Accepted

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

from collections import Counter
C=Counter(A)
ANS=0
for i in C.values():
    ANS+=i*(i-1)//2

for a in A:
    k=C[a]

    print(ANS-(k*(k-1)//2)+((k-1)*(k-2)//2))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))

data = [0]*(N+1)
for i in range(N): data[a[i]] += 1

all_c = 0
for i in range(1,N+1): all_c += data[i]*(data[i]-1)//2

for i in range(N):
    ans = all_c
    if data[a[i]] >= 2:
        ans -= data[a[i]]*(data[a[i]]-1)//2
        ans += (data[a[i]]-1)*(data[a[i]]-2)//2
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

a_cnt = Counter(a)

x = 0
for i in a_cnt.values():
    x += i * (i - 1) // 2
    
for i in a:
    ans = x - (a_cnt[i] * (a_cnt[i] - 1) // 2)
    ans += (a_cnt[i]-1) * (a_cnt[i]-2) // 2
    print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
An = list(map(int, input().split()))
lst = collections.Counter(An)
c_lst = {k: int(v*(v-1)/2) if v > 1 else 0 for k, v in lst.items()}
count = sum(c_lst.values())
for k in An:
  print(count - max(lst[k]-1, 0))

Submission status: Accepted

Solution code:
import copy

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

c=0

a_uniq=[0]*(n+1)

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

for i in a_uniq:
  c+=sum(range(1,i))
#a_uniq=list(set(a))
#for i in a_uniq:
#  c+=sum(range(1, a.count(i)))

for i in a:
  print('{:.0f}'.format(c-a_uniq[i]+1))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
dic={}
for a in A:
  if a in dic.keys():
    dic[a]+=1
  else:
    dic[a]=1
r=0
for a in dic.keys():
  c=dic[a]
  r+=c*(c-1)//2
for i in range(N):
  c=dic[A[i]]
  print(r - c*(c-1)//2 + (c-1)*(c-2)//2 )

Submission status: Accepted

Solution code:
N_ = int(input())

A_ = input().split()
#print(A_)

#U_ = set(A_)
#print(U_)


#def xC(num):
#    return (num * (num - 1)) //2
import collections
cnt = collections.Counter(A_)

num = 0
dict = {}
#judge = {}
#for u in U_:
#    judge[u] = False
#    num += xC(A_.count(u))
for v in cnt.values():
    num += v*(v - 1)//2
Re = []
for value in A_:
    Re.append(str(num - (cnt[value] - 1)))
print('\n'.join(Re))

Submission status: Accepted

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

import collections
cnt = collections.Counter(a)

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

res = []
for value in a:
    res.append(str( sum_cnt - (cnt[value]-1) ))
print('\n'.join(res))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

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

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

Submission status: Accepted

Solution code:
import collections

N = int(input())

input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line] 

Line2 = Line

c = collections.Counter(Line2)

# print(c)

mapped_list = map(lambda x: (x-1)*x/2,c.values())
nukanai_sum = sum(list(mapped_list))

# print(nukanai_sum)

# print(Line)

for i in Line:
    ans = nukanai_sum

    ans -= ((c[i]-1)*c[i]/2-(c[i]-2)*(c[i]-1)/2)
    print(int(ans))

Submission status: Accepted

Solution code:
# A = [list(map(int, input().split())) for _ in range(3)]
n = int(input())
# B = [int(input()) for _ in range(n)]
A = list(map(int, input().split()))
# a, b = map(int, input().split())
# S = input()


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


cnt = [0 for _ in range(n+1)]
for i in range(n):
    cnt[A[i]-1] += 1
total = 0
for i in range(n):
    total += choose2(cnt[i])
for i in range(n):
    ans = total
    ans -= choose2(cnt[A[i]-1])
    ans += choose2(cnt[A[i]-1]-1)
    print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())

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

c = collections.Counter(A)

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

for i in range(N):
  z = c[A[i]] - 1   
  print(ans-z, sep="\n")

Submission status: Accepted

Solution code:
#!/usr/bin/env/ python3
N = int(input())
A = [int(x) for x in input().split()]
mydict = {}
for i in range(len(A)):
    if A[i] in mydict: mydict[A[i]] += 1
    else: mydict[A[i]] = 1
#print(mydict)
ans = 0
for i in list(mydict.keys()):
    if mydict[i] > 1: ans += int(mydict[i] * (mydict[i] - 1) / 2)
for i in range(len(A)):
    print(ans - (mydict[A[i]] - 1))

Submission status: Accepted

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

b = [0] * (n + 5)
for i in range(n):
    b[a[i]] += 1

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

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

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter


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


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


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

n = int(input())
a = readints()
# print(a)
c = Counter(a)  # リストの各要素の出現回数をカウント
# print(c)
cnt = 0
# print(c.values())
for i in c.values():
    # 各要素の値に対してforループ処理
    cnt += i*(i-1)//2
for aa in a:
    x = c[aa]
    # print(x)
    ans = (cnt-x*(x-1)//2+(x-1)*(x-2)//2)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

A_cnt = Counter(A)

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

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

C = [combination(i) for i in range(N + 1) ]

T = sum([combination(B[i]) for i in range(len(B)) ])
# print(T)

for i in range(N):
    # print(T - C[A[i]])
    print( T - C[B[A[i]]] + C[B[A[i]] - 1] )

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

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

hamap = {}

for i in A:
    if i in hamap:
        hamap[i] = hamap[i] + 1

    else:
        hamap[i] = 1

ans = 0
hiku = {}
for i in hamap.keys():
    tempCmb = cmb(hamap[i], 2)
    ans += tempCmb
    hiku[i] = tempCmb - cmb(hamap[i] - 1, 2)
        
for i in A:
    # a = comb(n, r)
    print(ans - hiku[i])

Submission status: Accepted

Solution code:
GI = lambda: int(input()); GIS = lambda: map(int, input().split()); LGIS = lambda: list(GIS())

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

def main():
    GI()
    arr = LGIS()

    d = {}
    for x in arr:
        d[x] = d.get(x, 0) + 1

    tot = sum(count_unique_pairs(v) for v in d.values())
    
    for x in arr:
        print(tot - count_unique_pairs(d[x]) + count_unique_pairs(d[x]-1))

def main():
    GI()
    arr = LGIS()

    d = {}
    for x in arr:
        d[x] = d.get(x, 0) + 1

    tot = sum(count_unique_pairs(v) for v in d.values())
    
    for x in arr:
        print(tot - d[x] + 1)

main()

Submission status: Accepted

Solution code:
GI = lambda: int(input()); GIS = lambda: map(int, input().split()); LGIS = lambda: list(GIS())

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

def main():
    GI()
    arr = LGIS()

    d = {}
    for x in arr:
        d[x] = d.get(x, 0) + 1

    tot = sum(count_unique_pairs(v) for v in d.values())
    
    for x in arr:
        print(tot - count_unique_pairs(d[x]) + count_unique_pairs(d[x]-1))

def main():
    GI()
    arr = LGIS()

    d = {}
    for x in arr:
        d[x] = d.get(x, 0) + 1

    tot = sum(count_unique_pairs(v) for v in d.values())
    
    for x in arr:
        print(tot - d[x] + 1)

main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
nums = list(map(int, input().split()))

dic_c = {}
for e in nums:
    if e in dic_c:
        dic_c[e] += 1
    else:
        dic_c[e] = 1

allscore = 0
for key in dic_c.keys():
    allscore += dic_c[key]*(dic_c[key]-1)

for e in nums:
    if dic_c[e] == 1:
        print(int(allscore/2))
    elif dic_c[e] == 2:
        print(int((allscore-1)/2))
    else:
        print(int(       (allscore - (2*(dic_c[e]-1)))/2      ))

Submission status: Accepted

Solution code:
from sys import *
from math import *
from collections import deque
input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()

n = int(input())
arr = lmi()
freq = [0]*(n+1)
for i in arr:
    freq[i] += 1
tot = 0
for i in range(n+1):
    tot += freq[i]*(freq[i]-1)//2
for i in arr:
    print(tot-freq[i]+1)

Submission status: Accepted

Solution code:
GI = lambda: int(input()); GIS = lambda: map(int, input().split()); LGIS = lambda: list(GIS())

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

def main():
    GI()
    arr = LGIS()

    d = {}
    for x in arr:
        d[x] = d.get(x, 0) + 1

    tot = sum(count_unique_pairs(v) for v in d.values())
    
    for x in arr:
        print(tot - count_unique_pairs(d[x]) + count_unique_pairs(d[x]-1))

main()

Submission status: Accepted

Solution code:
n = int(input())
freq = [0 for i in range(n+1)]
combo = [0 for i in range(n+1)]
arr = list(map(int, input().split()))
for i in arr:
    freq[i] += 1
for i in range(1, n+1):
    if freq[i] > 1:
        combo[i] = (freq[i] * (freq[i]-1))//2
ans = sum(combo)
for i in range(n):
    ans -= combo[arr[i]]
    freq[arr[i]] -= 1
    if freq[arr[i]] > 1:
        ans += (freq[arr[i]] * (freq[arr[i]]-1))//2
    print(ans)
    ans -= (freq[arr[i]] * (freq[arr[i]]-1))//2
    freq[arr[i]] += 1
    ans += combo[arr[i]]

Submission status: Accepted

Solution code:
from math import factorial
def cmb(n,r):
  return factorial(n) // factorial(r) // factorial(n-r)

def nPr(n, r):
    num = 1
    for i in range(n, n-r, -1):
        num *= i
    return num

n=int(input())
A=list(map(int,input().split()))
num = [0]*(n+1)
base = 0
ans = []
for i in range(n):
  num[A[i]] += 1
for a in num:
  if a > 1:
    base += cmb(a,2)
for a in A:
  ans.append(base-(num[a]-1))
for a in ans:
  print(a)

Submission status: Accepted

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

import collections as cl
c_A = cl.Counter(A)
sum_c_A=0
for i in c_A.keys():
	if c_A[i]>1:
		sum_c_A += c_A[i]*(c_A[i]-1)/2

for j in A:
	print(int(sum_c_A-c_A[j]+1))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

numbers = defaultdict(int)
for a in as_:
    numbers[a] += 1

total = sum([m * (m - 1) // 2 for m in numbers.values()])

for k in range(1, n + 1):
    m_k = as_[k - 1]
    num_k = numbers[m_k]
    ans = total
    if num_k > 1:
        ans += - num_k * (num_k - 1) // 2 \
               + (num_k - 1) * (num_k - 2) // 2

    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
nlist=list(map(int,input().split()))

import collections

count_dict=collections.Counter(nlist)

anslist=[]
all_kumi=0
for i in count_dict.values():
    all_kumi += i*(i-1)//2
 
for j in range(0,n):
    print(all_kumi - count_dict[nlist[j]] + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = {}
ans = 0
for i in a:
    if i not in d:
        d[i] = 1
    else:
        d[i] += 1

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

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(),np.int64)

bincnt = np.bincount(A)

total = (bincnt * (bincnt - 1)).sum() // 2
ans = (total - (bincnt-1))[A].astype(str)

print('\n'.join(ans))