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:
import collections

# input
N = int(input())
A = input().split()
# calculatioin
cnt = collections.Counter(A)
full_cnt = int(sum(cnt[key]*(cnt[key]-1)/2 for key in cnt))
for i in range(N):
  print(full_cnt - cnt[A[i]]+1)

Submission status: Accepted

Solution code:
import math
n=int(input())
a=list(map(int,input().split(' ')))
l={}
fl={}
for i in a:
  l[i]=l.get(i,0)+1
  fl[i]=(l.get(i,0)*(l.get(i,0)-1))//2
s=sum(fl.values())
for i in a:
  buf = (l[i]-1)*(l[i]-2)//2
  print(s-fl[i]+buf)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
from collections import Counter
C=Counter(A)
Q=list(C.values())
def qq(F):
    n=len(F)
    ans=0
    for i in range(n):
        ans+= F[i]*(F[i]-1)//2
    return ans
ans=qq(Q)
for i in range(n):
    print(ans-C[A[i]]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
num = [0 for i in range(N)]
zu = 0
ans = []
for i in A:
    num[i-1]+=1
for i in range(N):
    temp = 1/2*num[i]*(num[i]-1)
    zu += temp
for i in A:
    kawari =zu
    temp1 = 1/2*num[i-1]*(num[i-1]-1)
    num[i-1] -= 1
    temp2 = 1/2*num[i-1]*(num[i-1]-1)
    kawari += -temp1+temp2
    num[i-1] += 1
    print(int(kawari))

Submission status: Accepted

Solution code:
from math import factorial

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

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

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

c = 0
for i in range(N):
  n = L[i]
  if n >= 2:
    c += factorial(n) // factorial(2) // factorial(n - 2)

for i in range(N):
  n = L[int(A[i])-1]
  if n >= 2:
    print(c - n + 1)
  else:
      print(c)

Submission status: Accepted

Solution code:
import collections as c

# 入力
N = int(input())
An = [int(x) for x in input().split()]
 
# 各数字の個数を求める
Cn = c.Counter(An)
 
# 全部の組み合わせ
s = 0
for v in Cn.values():
    s += int(v*(v-1) / 2)

for i in range(0,N):
    print(s - (Cn[An[i]] - 1))

Submission status: Accepted

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

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

n_balls = [0 for _ in A]
for a_i in A:
    n_balls[a_i - 1] += 1

n_combinations = [n_c_2(n) for n in n_balls]
total_combinations = sum(n_combinations)

for i, a_i in enumerate(A):
    diff = n_c_2(n_balls[a_i - 1]) - n_c_2(n_balls[a_i - 1] - 1)
    print(total_combinations - diff)

Submission status: Accepted

Solution code:
import collections as c
N = int(input())
A = list(map(int, input().split()))
deck = c.Counter(A)
tot = 0
for a in deck.values():
	tot += a*(a-1)//2

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

Submission status: Accepted

Solution code:
import math
import collections


def C(n, r):
    if n < 2:
        return 0
    else:
        return n*(n-1)//r


N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
dic = {}
total = 0
for key, value in c.items():
    C_val = C(value, 2)
    dic[key] = C_val
    total += C_val
for i in range(N):
    print(total - dic.get(A[i]) + C(c[A[i]]-1, 2))

Submission status: Accepted

Solution code:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
input=lambda :sys.stdin.readline().rstrip()

N=int(input())
A=list(map(int,input().split()))
C=Counter(A)
num=sum([C[i]*(C[i]-1)//2 for i in C])
for a in A:
    c=num-C[a]*(C[a]-1)//2
    n=C[a]-1
    if n>=2:
        c+=n*(n-1)//2
    print(c)

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)
for i in a:print(s-c[i]+1)

Submission status: Accepted

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

import collections

dct=collections.Counter(lst)


#for i in range(N):
Sum=0
for v in dct.values():
        Sum+=v*(v-1)//2


for i in range(N):
    print(Sum-dct[lst[i]]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
import collections as col
 
num_list = set(A)
ca = col.Counter(A)

num = 0

for i in num_list:
    if ca[i] == 1:
        continue
    else:
        num += (ca[i]*(ca[i]-1))//2
        
for i in A:
    ans = num - (ca[i]-1)
    print(ans)

Submission status: Accepted

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

cont = {i : 0 for i in range(max(A) + 1)}
for a in A:
  cont[a] += 1

# all pattern
all_pair = 0
for key in cont:
  all_pair += (cont[key] * (cont[key] - 1)) // 2
  
for a in A:
  print(all_pair - (cont[a] - 1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import Counter
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    b = Counter(a)
    diff = dict()
    total = 0

    # 余事象
    for key, value in b.items():
        comb1 = value * (value - 1) // 2
        comb2 = (value - 1) * (value - 2) // 2
        total += comb1
        diff[key] = max(0, comb1 - comb2)

    ans = [0 for _ in range(n)]

    for index, ai in enumerate(a):
        ans[index] = total - diff[ai]

    print('\n'.join(map(str, ans)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

M = max(A) + 1
B = [0] * M
for a in A:
    B[a] += 1
l = [0] * M
for i in range(M):
    l[i] = B[i] * (B[i] - 1) // 2
ans = sum(l)
for a in A:
    if a == 0:
        print(ans)
    else:
        print(ans - l[a] + ((B[a] - 1) * (B[a] - 2) // 2))

Submission status: Accepted

Solution code:
import numpy as np

num = input()
num = int(num)

str = input()
list_in = list(map(int, str.split()))

count_arr = [0]*num

for item in list_in:
    count_arr[item-1] += 1

sum = 0
for i in range(num):
    tmp = count_arr[i-1]
    tmp = (tmp * (tmp -1))//2
    sum += tmp

for item in list_in:
    tmp = count_arr[item-1]
    result = sum - (tmp - 1)
    print(result)

Submission status: Accepted

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

A_keys = collections.Counter(A)
keys_list= A_keys.keys()

combi_sum = {}
for i in keys_list:
    cnt = A_keys[i]
    combi_sum[i] = cnt * (cnt-1)//2 

v_sum = sum(combi_sum.values())
for k in range(N):
    Ak = A[k]
    k_cnt = A_keys[Ak] - 1
    result = v_sum - combi_sum[Ak] + k_cnt*(k_cnt -1)//2
    print(int(result))

Submission status: Accepted

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

Submission status: Accepted

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

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

cnt = 0
for i in X:
    cnt += i* (i-1) // 2

for i in A:
    ans = cnt - (X[i-1] - 1)
    print(ans)

Submission status: Accepted

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

import collections
c = collections.Counter(a)

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
def cmb(n):
    if n == 1:
        return 0
    else:
        return n * (n-1) /2
mydict = {}
for i in range(N):
    a = A[i]
    if a in mydict.keys():
        mydict[a] += 1
    else:
        mydict[a] = 1
s = 0
for i in mydict.values():
	s += cmb(i)
for i in range(N):
    a = A[i]
    x = mydict[a] - 1
    print(int(s-x))

Submission status: Accepted

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

ans_li = [0]*(n+10)

for i in li:
    ans_li[i] += 1
    
result = 0    
for i in ans_li:
    result += (i * (i-1)) // 2
    
for i in li:
    ans = result
    ans += ((ans_li[i] - 1) * (ans_li[i] - 2)) // 2- (ans_li[i] * (ans_li[i] - 1)) // 2
    print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
arr=list(map(int,input().split()))
cnt=collections.Counter(arr) #各数Akの個数を数える
total=0
for key in cnt.keys():
  total+=cnt[key]*(cnt[key]-1)//2 #各数Akについて、cnt[Ak]個から異なる2個のボールを取り出す場合の数を事前に計算する
for i in range(n): #各iについて上記の方法で答えを求める
  print(total-cnt[arr[i]] + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
sum = 0
for i in range(N+1):
  B.append(0)
for i in range(N):
  B[A[i]] += 1
for i in range(N+1):
  if B[i] != 0:
    sum += B[i]*(B[i]-1)//2
for i in range(N):
  x = B[A[i]]
  if x != 1:
  	print(sum-x*(x-1)//2+(x-1)*(x-2)//2)
  else:
    print(sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(lambda x: int(x)-1, input().split()))
d = [0] * N
for i in A:
    d[i] += 1
s = sum(map(lambda x: x*(x-1)//2, d))
for i in A:
    print(s + (1 - d[i]))

Submission status: Accepted

Solution code:
import numpy as np
import collections
n=int(input())
a=list(np.fromstring(input(),int,sep=' '))
dic=collections.Counter(a)
ans=0

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

Submission status: Accepted

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

select_num = 0
count_num = [0]*(N+1)
for i in A:
  count_num[i] += 1
for i in range(1,N+1):
  select_num += (count_num[i])*(count_num[i] - 1)//2
for i in A:
  print(select_num -count_num[i] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

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


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

for i in l:
    v = cl[i]
    ans = cnt - (v * (v - 1) // 2) + (v - 1) * (v - 2) // 2
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for i in range(N):
        dd[A[i]]+=1
        
    S=0
    for k,v in dd.items():
        S+=(v*(v-1))//2
        
    for i in range(N):
        v=dd[A[i]]
        ans=S-(v-1)
        print(ans)

main()

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
count_dict = collections.Counter(A)
combination_count = 0
for value in count_dict.values():
    combination_count += int(value*(value-1)/2)
for i in range(N):
    print(combination_count - count_dict[A[i]] +1)

Submission status: Accepted

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

Submission status: Accepted

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

D={}
for i in A:
      if i not in D:
            D[i]=1
      else:
            D[i]+=1
S=0
DD=D.copy()
for i in DD:
      bu=DD[i]
      DD[i]=bu*(bu-1)//2
      S+=DD[i]

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

Submission status: Accepted

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

# a_set = sorted(set(a))

count = [0] * (n + 1)


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


sum = 0
for i in a:
    count[i] += 1
for i in count:
    sum += f(i)

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
X=[0]*(N + 1)

for i in A:
    X[i] += 1

cnt=0
for i in X:
    cnt += (i * (i - 1)// 2)

for i in A:
    print(cnt - (X[i] - 1))

Submission status: Accepted

Solution code:
n  = int(input())
A = list(map(int,input().split()))
l = []
from collections import Counter
counter = Counter(A)
total = 0
def choose2(n):
    return n*(n-1)/2
for i in counter.most_common():
    c = choose2(i[1])
    total += c

for i in A:
    print(int(total-choose2(counter[i])+choose2(counter[i]-1)))

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:
def nPr(n, r):
  ans = 1
  for i in range(n,n-r,-1):
    ans *= i
  return ans

def nCr(n, r):
  tmp = 1
  for i in range(r,1,-1):
    tmp *= i
  return int(nPr(n, r) / tmp)

n = int(input())
a = list(map(int,input().split()))
count = len(a)
d = {}
x = 0

for i in a:
  if i not in d:
    d[i] = 1
  else:
    d[i] += 1

for i in d:
  x += nCr(d[i], 2)

for i in a:
  ans = x - d[i] + 1
  print(ans)

Submission status: Accepted

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

num_list = [0]*(n+1)
for i in a:
    num_list[i] += 1

cnt = 0
for i in num_list:
    cnt += i*(i-1)/2

for i in a:
    print(int(cnt +1 -num_list[i]))

Submission status: Accepted

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

#各要素が何個あるか
num_list = [0]*(N+1)
for i in A:
    num_list[i] += 1

#全体の組み合わせ
total = 0
for j in num_list:
    total += j*(j-1)//2

for k in A:
    print(total+1-num_list[k])

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	n = int(input())
	al = list(IN())
	vv=defaultdict(lambda : 0)
	for v in al:
		vv[v]+=1
	
	dd=dict()
	total=0
	for key in vv:
		num=vv[key]
		use_all = num*(num-1)//2
		in_k = ((num-1)*(num-2))//2
		dd[key]=use_all - in_k
		total+=use_all
	
	for v in al:
		pp=total-dd[v]
		print(pp)
		
	
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)
a_sum = 0
for i in C.values():
    a_sum += i*(i-1)//2

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
lis = list(map(int,input().split()))
 
clis = collections.Counter(lis)

count = 0
for kk in clis.values():
	count += (kk * (kk - 1)) // 2
    
for ll in lis:
	print(count - clis[ll] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
d = {}

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

for key, val in c.items():
    d[key] = fac2(val)

d = {key:fac2(val) for key, val in c.items()}
sum_val = sum(d.values())
ans = [sum_val - d[a] + fac2(c[a]-1) for a in A]
print(*ans, sep="\n")

Submission status: Accepted

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

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

d = {key:fac2(val) for key, val in c.items()}
sum_val = sum(d.values())
for a in A:
    ans = sum_val - d[a] + fac2(c[a]-1)
    print(ans)

Submission status: Accepted

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

freqs = {}
for ball in A:
	freqs[ball] = freqs.get(ball, 0) + 1
    
unbanned = 0
for v in freqs.values():
	unbanned += (v * (v - 1)) // 2
    
for ball in A:
	print(unbanned - freqs[ball] + 1)

Submission status: Accepted

Solution code:
import sys
myin = sys.stdin.readlines()

n = int(myin[0])
a = [int(x) for x in myin[1].split()]

cnt = [0] * (n+1)
for v in a:
    cnt[v] += 1

tmp = 0
for c in cnt:
    tmp += c * (c-1) // 2
for v in a:
    print(tmp - cnt[v] + 1)

Submission status: Accepted

Solution code:
import sys
myin = [x.rstrip() for x in sys.stdin.readlines()]

n = int(myin[0])
a = [int(x) for x in myin[1].split()]

cnt = [0] * (n+1)
for v in a:
    cnt[v] += 1

tmp = 0
for c in cnt:
    tmp += c * (c-1) // 2
for v in a:
    print(tmp - cnt[v] + 1)

Submission status: Accepted

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

cnt = [0] * (n+1)
for v in a:
    cnt[v] += 1

tmp = 0
for c in cnt:
    tmp += c * (c-1) // 2
for v in a:
    print(tmp - cnt[v] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
a_ = set(a)
x = Counter(a)
y = {i:x[i]*(x[i]-1)//2 for i in a_}

res = sum(y.values())
for i in a:
    t = x[i]
    t_ = y[i]
    print(res-t_+(t-1)*(t-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter
 
N = int(input())
A = list(map(int, input().split()))
cntA = Counter(A)
 
S = 0
for c in cntA.values():
    S += c * (c - 1) // 2
 
ans = []
for a in A:
    ans.append(S - (cntA[a] - 1))
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import Counter
import sys

input = sys.stdin.readline

def main():
    n = int(input().rstrip())
    l = list(input().rstrip().split())

    c = Counter(l)
    total = 0
    for key in c:
        total += c[key] * (c[key] -1) // 2
    for item in l:
        print(total - (c[item] -1))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline=sys.stdin.readline

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

from collections import defaultdict
dic=defaultdict(int)

for i in range(len(A)):
  dic[A[i]]+=1
  
all=0
for v in dic.values():
  all+=v*(v-1)//2
  
for i in range(len(A)):
  print(all-(dic[A[i]]-1))

Submission status: Accepted

Solution code:
import sys
import os
import collections

def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.buffer.readline().rstrip())
    A = list(map(int, sys.stdin.buffer.readline().split()))

    obj = collections.Counter(A)

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

    for a in A:
        print(p - (obj[a] - 1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# D
N = int(input())
As = list(map(int, input().split()))

items = {}
for a in As:
    items[a] = items[a]+1 if a in items.keys() else 1

sum_items = 0
for k in items.keys():
    if items[k] >= 2:
        sum_items += (items[k]*(items[k]-1))/2
sum_items = int(sum_items)
res = [0] * len(As)
for i,k in enumerate(As):
    res[i] = sum_items 
    if items[k] >= 2:
        res[i] -= (items[k]-1)

print(*res, sep="\n")

Submission status: Accepted

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

Submission status: Accepted

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

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1
res = 0
for key in nums.keys():
    res += nums[key]*(nums[key]-1)//2
for a in A:
    print( res-nums[a]+1 )

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
lis = inpl()
counts = [0]*(N+10)
for i in lis:
    counts[i] += 1

S = 0
for i in lis:
    S += counts[i] - 1

S //= 2

for i in lis:
    print(S - counts[i] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
a = Counter(A)
tmp = 0
for x, y in a.items():
    if y != 0:
        tmp += y*(y-1)//2
ans = {}
for x, y in a.items():
    if y > 1:
        ans[x] = tmp-(y-1)
    else:
        ans[x] = tmp
for x in A:
    print(ans[x])

Submission status: Accepted

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

b = Counter(a)
sumnum=0
for j in b.keys(): 
    sumnum += (b[j] * (b[j]-1)) / 2

for i in range(N):
    print(int(sumnum - (b[a[i]]-1)))

Submission status: Accepted

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

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

counts = Counter(A)
total = 0
for n, count in counts.items(): 
    #初めに、同じ数同士から2つ選んだ時の組み合わせの総和を求める
    combi = choose2(count)
    total += combi 

for i in A:
    #上で求めた組み合わせの総和から、消える数字について1つ減らしたときの組み合わせを減らす
    temp = total - choose2(counts[i])
    temp += choose2(counts[i] - 1) 
    print(temp)

Submission status: Accepted

Solution code:
import collections

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

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

counter = collections.Counter(A)

ttl = 0
for key in counter:
  ttl += choose2(counter[key])
    
for i in range(N):
  ans = ttl - choose2(counter[A[i]]) + choose2(counter[A[i]] - 1)
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

ctr = Counter(aa)

cmb2 = [0] * 200001
for n in range(2, 200001):
    r = 2
    num = 1
    for i in range(1, r + 1):
        num = num * (n - i + 1) // i
    cmb2[n] = num

x = 0
for k, v in ctr.items():
    x += cmb2[v]

for a in aa:
    k = ctr[a] - 1
    print(x - k)

Submission status: Accepted

Solution code:
import collections

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

#A_set = set(A)

#ボールに書かれている 数字の内訳を辞書に登録
A_dic = collections.Counter(A)

#すべてのボールから同じボールを取り出す方法数
all_count = 0

for a in A_dic.keys():
    count_a = A_dic.get(a)
    all_count += (count_a * (count_a - 1) // 2)

#n個あるボールから 1個取り除いた場合の方法数
#すべてのボールから取り出す方法数 - (n-1)通り

for i in range(N):
    print(all_count - A_dic.get(A[i])+1)

Submission status: Accepted

Solution code:
n = int(input())
balls = list(map(int, input().split()))
conbi = [0]*n
for i in balls:
    conbi[i-1] +=1
result = 0
for j in range(n):
    result += (conbi[j]*(conbi[j]-1))//2
for k in balls:
    print(result-conbi[k-1]+1, end=" ")

Submission status: Accepted

Solution code:


from collections import Counter
N=int(input())
A=list(map(int,input().split()))
d=dict(Counter(A))
sum=0
s=[]
s.append(0)
for i in range(1,2*10**5+1):
    s.append(s[i-1]+i-1)
for value in d.values():
    sum+=s[value]
for x in A:
    print(sum-(d[x]-1))

Submission status: Accepted

Solution code:
# from collections import deque
from collections import Counter
# from scipy.misc import comb
# from copy import deepcopy

n = int(input())
a = list(map(int, input().split()))
C = Counter(a)
S = 0
"""
for key in C:
    S += comb(C[key], 2, exact=True)
"""
for i in C.keys():
    S += C[i]*(C[i]-1)//2

for k in range(n):
    A = a[k]
    print(S - C[A] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = [int(x) for x in input().split()]

A = Counter(a)

total = sum([A[i] * (A[i] - 1) //2 for i in A.keys()])

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

Submission status: Accepted

Solution code:
import collections

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

C = collections.Counter(a)
total = 0
for i in C.keys():
    total += C[i] * (C[i] - 1) // 2
for i in range(n):
    A = a[i]
    print(total - C[A] + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
dic = {}
for i in range(n):
  if a[i] in dic.keys():
    dic[a[i]] += 1
  else:
    dic[a[i]] = 1
total = 0
loss = [0]*(n+1)
for k in dic.keys():
  res = dic[k]*(dic[k]-1)//2
  total += res
  loss[k] = res-(dic[k]-1)*(dic[k]-2)//2
for i in range(n):
  print(total-loss[a[i]])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

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

for v in cnt.values():
    ans += v*(v-1)//2
    
for Ai in A:
    ans_i = ans
    ans_i -= cnt[Ai]*(cnt[Ai]-1)//2
    ans_i += (cnt[Ai]-1)*(cnt[Ai]-2)//2
    
    print(ans_i)

Submission status: Accepted

Solution code:
from collections import Counter

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

A_C=Counter(A)

for name,x in A_C.items():
    Com+=int(x*(x-1)/2)
    
for i in range(N):
    y=A_C[A[i]]
    print(Com-y+1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# from collections import deque
from collections import Counter
from scipy.misc import comb
# from copy import deepcopy

n = int(input())
a = list(map(int, input().split()))
C = Counter(a)
S = 0
for key in C:
    S += comb(C[key], 2, exact=True)

for k in range(n):
    A = a[k]
    print(S - C[A] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
import collections
C = collections.Counter(a)
S = 0
for i in C.keys():
    S += C[i]*(C[i]-1)//2

for i in range(n):
    A = a[i]
    print(S-C[A]+1)

Submission status: Accepted

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

counter=defaultdict(int)

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

ans=0
prob=defaultdict(int)
for k,v in counter.items():
    p=v*(v-1)//2
    ans+=p
    if v>1:
        p2=(v-1)*(v-2)//2
        prob[k]=p2-p

for a in a_list:
    print(ans+prob[a])

Submission status: Accepted

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

    counter = Counter(A)
    
    res = 0
    for i in range(N+1):
        res += counter[i] * (counter[i] - 1) / 2
    
    for i in range(N):
        val = counter[A[i]]
        before = val * (val-1)/2
        after = (val - 1) * (val - 2) / 2
        print(int(res + after - before))

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

    counter = defaultdict(int)

    for i in range(N):
        counter[A[i]] += 1
    
    res = 0
    for i in range(N+1):
        res += counter[i] * (counter[i] - 1) / 2
    
    for i in range(N):
        val = counter[A[i]]
        before = val * (val-1)/2
        after = (val - 1) * (val - 2) / 2
        print(int(res + after - before))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter

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

S = Counter(listA)

##key##
key = S.keys()
out = 0
for i in key:
  out = out + S[i] * (S[i]-1) /2

for i in listA:
  test = out - S[i] * (S[i]-1) /2 + (S[i] -1) * (S[i]-2) /2
  print(int(test))

Submission status: Accepted

Solution code:
import collections

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

A_counter= collections.Counter(A)

_sum = 0
for count in A_counter:
    _sum += (A_counter[count]* (A_counter[count]-1)) // 2

for k in range(N):
    l = (A_counter[A[k]]) *  (A_counter[A[k]]-1)//2
    m = (A_counter[A[k]]-1) *  (A_counter[A[k]]-2)//2
    # print(_sum)
    # print(c_a)
    print(_sum -l + m)