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:
""" スニペット """
def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split()))
""" スニペット """
import math
# 2つ選ぶ通り数を算出 nC2
def choose2(n):
    return n*(n-1)//2
# インプット
N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N+1)
# ユニーク数の配列を求める
for i in range(N):
    uni[An[i]] += 1
sumWay = 0
# 各数値の2通りの選択通り数を足していく
for i in range(N + 1):
    sumWay += choose2(uni[i])
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print((sumWay - choose2(uni[An[i]]) + choose2(uni[An[i]]-1)))

Submission status: Accepted

Solution code:
def abc159d_banned_k():
    def cmb(n, r):
        """ nCrの組み合わせ数を返す """
        if n - r < r: r = n - r
        if r == 0: return 1
        if r == 1: return n

        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]

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

        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])

        return result
    n = int(input())
    a = list(map(int, input().split()))
    dict_a = {}
    for item in a:
        if item in dict_a.keys():
            dict_a[item] += 1
        else:
            dict_a[item] = 1
    cnt = 0
    for k in dict_a:
        if dict_a[k] > 1:
            cnt += cmb(dict_a[k],2)
    for item in a:
        if dict_a[item] > 2:
            print(cnt - cmb(dict_a[item],2) + cmb(dict_a[item]-1,2))
        elif dict_a[item] == 2:
            print(cnt - 1)
        else:
            print(cnt)

abc159d_banned_k()

Submission status: Accepted

Solution code:
n = int(input())
cnt = [0] * (n + 1)
cmb = 0
a = list(map(int, input().split()))

for ai in a:
  cnt[ai] += 1
  cmb += cnt[ai] - 1

for ai in a:
  print(cmb - cnt[ai] + 1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
d=dict()
for i in range(N):
    if A[i] not in d:
        d[A[i]]=0
    d[A[i]]+=1
x=sum(x*(x-1)//2 for x in d.values())
for i in range(N):
    y=d[A[i]]
    print(x-y*(y-1)//2+(y-1)*(y-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
cnt=Counter(A)
tot=0
dif={}
for k,v in cnt.items():
  tot += v*(v-1)//2
  dif[k]=v-1
ans=[tot-dif[a] for a in A]
print('\n'.join(map(str,ans)))

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
from functools import reduce


def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under


n = int(input())
al = list(map(int, input().split()))
c = Counter(al)

k = 0
for v in c.values():
    if v == 1:
        continue
    else:
        k += cmb(v, 2)

for a in al:
    if c[a] == 1:
        print(k)
    elif c[a] == 2:
        print(k-cmb(c[a], 2))
    else:
        print(k-cmb(c[a], 2)+cmb(c[a]-1,2))

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
num_list = [0] * N

def combination(n):
    C = [0]
    for i in range(2,n+1):
        C.append(int(i*(i-1)/2))
    
    return C

com = combination(N)
sum_com = 0

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


for i in range(N):
    if num_list[i] == 0:
        continue
    else:
        sum_com += com[num_list[i]-1]

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

Submission status: Accepted

Solution code:
from collections import Counter

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

c=Counter(A)
s=0
for v in c.values():
  s+=v*(v-1)//2
for i in range(n):
  ans=s-(c[A[i]])+1
  print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    d = defaultdict(lambda:0)
    d2 = {}
    for i in a:
        d[i] += 1
    for i,j in d.items():
        x = j*(j-1)//2
        d2[i] = x
    ans = 0
    ans = sum(d2.values())
    for i in range(n):
        print(ans-d[a[i]]+1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math


def combination(n, r):
    if n <= 1:
        return 0
    elif n == 2:
        return 1
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def count_list(lst):
    count_lst = [0] * (max(lst) + 1)

    for i in lst:
        count_lst[i] += 1

    return count_lst


def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    lst1 = count_list(a_lst)
    lst2 = [0] * len(lst1)
    for i in range(len(lst1)):
        lst2[i] = combination(lst1[i], 2)
    tmp = sum(lst2)

    for i in range(n):
        a = a_lst[i]
        print(tmp - (lst1[a] - 1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from copy import copy
from collections import Counter
from scipy.special import comb

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

C = Counter(A)
S = 0
for i in range(1,n+1):
  S += comb(C[i],2,exact=True)

for k in range(n):
  p = A[k]
  ans = S - comb(C[p],2,exact=True) + comb(C[p]-1,2,exact=True)
  print(ans)

Submission status: Accepted

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

cc=collections.Counter(A)
L=list(cc.values())

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

ans=0
for a in A:
    temp=cc[a]*(cc[a]-1)//2
    ttemp=(cc[a]-1)*(cc[a]-2)//2
    ans=total-temp+ttemp
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

from operator import mul
from functools import reduce
def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom
ans = 0
for i in a:
    if  a[i] > 1:
        ans += combinations_count(a[i],2) 
for i in A:
    n = a[i]
    if n > 2:
        print(ans+combinations_count(n-1,2)-combinations_count(n,2))
    elif n == 2:
        print(ans-1)
    else:
        print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
# ABC159 D

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

c=Counter(A)
res=[]
for p,r in c.items():
    res.append((r*(r-1))//2)
sum_res=sum(res)
for k in range(N):
    r=c[A[k]]
    print(sum_res-(r*(r-1))//2+((r-1)*(r-2))//2)

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
counts = [0 for _ in range(N+1)]
for num in nums:
  counts[num] += 1
combination = 0
for count in counts:
  if count >= 2:
    combination += (count*(count-1))//2
for num in nums:
  print(combination-counts[num]+1)

Submission status: Accepted

Solution code:
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
#print(d)
s = 0
for k in d:
    s += d[k] * (d[k] - 1) // 2
#print("s=",s)
for i in range(N):
    t = d[A[i]]
    #print("t=",t)
    print(s - t * (t - 1) // 2 + (t - 1) * (t - 2) // 2)

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.special import comb

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

d = Counter(A)

sum_c = 0
for v in d.values():
    if v >= 2:
        sum_c += comb(v, 2, exact=True)

for a in A:
    x = sum_c - comb(d[a], 2, exact=True) + comb(d[a]-1, 2, exact=True)
    print(x)

Submission status: Accepted

Solution code:
def main():
    import math
    n = int(input())
    inlis = list(map(int, input().split()))
    indic = dict()
    total = 0

    for num in inlis:
        if num in indic:
            indic[num] += 1
        else:
            indic[num] = 1
    
    for k, v in indic.items():
        total += int(v * (v-1) / 2)
    
    for num in inlis:
        print(total - indic[num] + 1)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
s=0
A=list(map(int,input().split()))
li=[0]*(2*(10**5)+1)
for i in range(N):
    li[A[i]]+=1
for j in range(N+1):
    s+=li[j]*(li[j]-1)//2
for k in range(N):
    print(s-li[A[k]]+1)

Submission status: Accepted

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


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    cnt_list = [0] * n  # 各番号が書かれたボールが何個あるか

    for a in a_list:
        cnt_list[a - 1] += 1

    total_method = 0
    for i in range(n):
        total_method += nC2(cnt_list[i])

    for k in range(n):
        ans = total_method
        num_k = cnt_list[a_list[k] - 1]  # k番目のボールに書かれた数字が何個あるか
        ans -= nC2(num_k)
        num_k -= 1
        ans += nC2(num_k)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    num_list = [0] * n  # 各番号が書かれたボールが何個あるか
    method_list = [0] * n  # 同じ番号が書かれた異なる2つのボールを選ぶ方法

    for a in a_list:
        num_list[a - 1] += 1

    for i in range(n):
        b = num_list[i]
        if b >= 2:
            method_list[i] = b * (b - 1) // 2

    total_method = sum(method_list)

    for k in range(n):
        c = a_list[k]  # k番目のボールに書かれている数字
        ans = total_method - method_list[c - 1]
        d = num_list[c - 1]
        if d >= 3:
            e = (d - 1) * (d - 2) // 2
        else:
            e = 0
        ans += e
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

c = collections.Counter(A)
all_combi=0

for i in c:
    # print(c[i])
    all_combi +=  c[i]*(c[i]-1)//2

count = [c[A[i]] for i in range(len(A))]

for i in range(len(A)):
    

    n_same_i = count[i]-1 
    ans = all_combi - n_same_i
    
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = [0] * (N+1)
total = 0
 
for i in A:
  S[i] =S[i]+ 1
for i in S:
    total = i * (i - 1) // 2 + total
for i in A:
    #print(i,S[i])
    a=total-S[i]+1
    print(a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 22:04:16 2020

@author: liang
"""

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

for a in A:
    d[a-1] += 1
#print(d)
score = 0
for  a in d:
    score += a*(a-1)//2
#print(score)
for a in A:
    print(score - d[a-1] + 1)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
tt=[0]*(n+7)

total=0

for a in arr:
  tt[a]+=1

for t in tt:
  total += (t*(t-1)//2)

for i in range(n):
  n = tt[arr[i]]
  d = (n*(n-1)//2) - ((n-1)*(n-2)//2)
  print(total-d)
# x * x-1 // 2

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 22:04:16 2020

@author: liang
"""

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

for a in A:
    d[a-1] += 1
#print(d)
score = 0
for  a in d:
    score += a*(a-1)//2
#print(score)
for a in A:
    print(score - d[a-1] + 1)

Submission status: Accepted

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

from collections import Counter
counter = Counter(A)

from collections import defaultdict
res_dic = defaultdict(int)

base = 0
for key,n in counter.items():
  res_dic[key] = (n * (n - 1) // 2, (n - 1) * (n - 2) // 2)
  base += n * (n - 1) // 2
  
for i in range(N):
  ans = base - res_dic[A[i]][0] + res_dic[A[i]][1]
  print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

N,A = int(input()),lint()
S = dict()
for a in A:
    S.setdefault(a,0)
    S[a] += 1
ans = 0
for k in S:
    ans += S[k]*(S[k]-1)//2
for a in A:
    print(ans-(S[a]-1))

Submission status: Accepted

Solution code:
def f():
  n = int(input())
  *li,=map(int, input().split())
  from collections import Counter
  c = Counter()
  for i in li:
    c[i] += 1
  ans=0
  from math import comb
  for i in c.values():
    ans += comb(i, 2)
  for i in li:
    print(ans - comb(c[i], 2) + comb(c[i]-1, 2))
f()

Submission status: Accepted

Solution code:
n = int(input())
*li,=map(int, input().split())
from collections import Counter
c = Counter()
for i in li:
    c[i] += 1
ans=0
from math import comb
for i in c.values():
    ans += comb(i, 2)
for i in li:
    print(ans - comb(c[i], 2) + comb(c[i]-1, 2))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
def kC2(k):
  return k * (k - 1) // 2
A = list(map(int, input().split()))
AC = Counter(A)
total = sum(kC2(v) for v in AC.values())
print(*[total - kC2(AC[a]) + kC2(AC[a] - 1) for a in A], sep='\n')

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	A = [int(a) - 1 for a in input().split(" ")]
	int_cnt = [0] * N
	for a in A:
		int_cnt[a] += 1
	S = sum([combination_2(c) for c in int_cnt])
	for i in range(N):
		x = int_cnt[A[i]]
		print(S - combination_2(x) + combination_2(x - 1))

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

main()

Submission status: Accepted

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

s = [0]*(n+1)
for x in a:
    s[x] += 1
    

p = 0
for i in range(1,n+1):
    if s[i] >= 2:
        p += s[i]*(s[i]-1)//2

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

Submission status: Accepted

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

import collections
count = collections.Counter(a)

import math
def p(n, r):
    return math.factorial(n) // math.factorial(n - r)
def c(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# print(count)
s = 0
cmb = {}
for i in count.keys():
    cmb[i] = (c(count[i], 2),c(count[i] - 1, 2))
    s += cmb[i][0]
# print(s)
# print(cmb)

for i in range(n):
    ans = s - cmb[a[i]][0] + cmb[a[i]][1]
    print(ans)

Submission status: Accepted

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

num = [0] * n
for i in lst:
  num[i-1] += 1
  
total = 0
for i in range(n):
  m = num[i]
  total += m*(m-1)//2

for k in range(n):
  
  s = num[lst[k]-1]
  t = total + 1- s
  
  print(t)

Submission status: Accepted

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

count = [0]*(N+1)

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

#print(count)

total = 0
for j in range(1,N+1):
  cj = count[j]*(count[j]-1)//2
  #print(cj)
  total = total + cj

ans = total
for k in range(N):
  ans = total - (count[A[k]] - 1)
  print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

li = [0] * (N + 1)
total = 0

for i in A:
  li[i] += 1
for i in li[1:]:
  total += i * (i - 1) // 2

for i in A:
  j = li[i]
  print(total - (j * (j - 1) // 2) + ((j - 1) * (j - 2) // 2))

Submission status: Accepted

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

count = Counter(a)

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

Submission status: Accepted

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

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

for i in range(N):
    num=cnt.get(A[i])
    if num==1:
        ans=sum
    elif num==2:
        ans=sum-1
    else:
        ans=sum-num*(num-1)//2+(num-1)*(num-2)//2

    print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
from collections import Counter,defaultdict
def main():
    n, *a = map(int, read().split())
    aa = Counter(a)
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for k, v in aa.items():
        d1[k] = v * (v - 1) // 2
        v -= 1
        d2[k] = v * (v - 1) // 2
    sumd1 = sum(d1.values())
    for ae in a:
        r = sumd1 - d1[ae] + d2[ae]
        print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

c = Counter(a)
d = c.copy()
k = c.keys()
for i in k:
    d[i] = c[i]*(c[i]-1) // 2
s = sum(list(d.values()))
for i in a:
    print(s - d[i] + max(0, (c[i]-1)*(c[i]-2)//2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  dic[i+1] = 0
for ai in a:
  dic[ai] += 1
cnt = 0
for di in dic:
  cnt += dic[di] * (dic[di] - 1) // 2
for i in range(n):
  print(cnt - dic[a[i]] * (dic[a[i]] - 1) // 2 + (dic[a[i]] - 2) * (dic[a[i]] - 1) // 2)

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


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

for i in a :
    n=x[i]
    ans1=ans-(n-1)
    print(ans1)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
xm=max(x)
def ra(a): #圧縮された要素を非保持
    ll,l=[0]*xm,1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll[a[i]]+=l
            l=1
    ll.append(l)
    return ll
b=ra(sorted(x))
d=0
for i in b:
    d+=(i*(i-1))//2
for i in range(n):
    print(d-b[x[i]]+1)

Submission status: Accepted

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

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

C = 0
for i in set(A):
  n = ls[i]
  C += n * (n - 1) //2
  
for a in A:
  n = ls[a]
  print(C - (n - 1))

Submission status: Accepted

Solution code:
n=int(input())
A=[int(i) for i in input().split()]

L=[0 for i in range(n+1)]
for num in A:
    L[num]+=1

big=0
for num in L:
    big+=num*(num-1)//2

for num in A:
    sma=L[num]
    print(big-sma*(sma-1)//2+(sma-1)*(sma-2)//2)

Submission status: Accepted

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

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

ss = list(map(nC2, s))
sss = sum(ss)

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=[0]*(n+1)
l2=[0]*(n+1)
for i in a:
    l[i]+=1

for i in range(1,n+1):
    l2[i]=l[i]*(l[i]-1)//2
sum_l=sum(l2)
for i in range(1,n+1):
    print(sum_l-(l[a[i-1]]-1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(i) for i in input().split()]
cnt_list = Counter(A)

total = sum([cnt*(cnt-1)//2 if cnt > 1 else 0 for cnt in cnt_list.values()])
print(*[total-cnt_list[a]+1 for a in A], sep='\n')

Submission status: Accepted

Solution code:
from collections import Counter

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    counter = Counter(A)
    comb_2 = lambda n:n*(n-1)//2 if n > 1 else 0
    A_comb = sum(comb_2(v) for v in counter.values())
    # print(A_comb)
    
    for a in A:            
        q = counter[a]
        ret = A_comb -comb_2(q) + comb_2(q-1)
        
        print(ret, flush=True)
        
solve()

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = list(map(int, input().split()))
A_sum = 0
A2 = []
idx = dict()

for i in A:
  if i in idx:
    idx[i] += 1
  else:
    idx[i] = 1
    
for key in idx:
  if idx[key] == 1:
    idx[key] = [idx[key], 0]
  else:
    temp = int(idx[key] * (idx[key] - 1)/2)
    A_sum += temp
    temp = temp - int(temp * (idx[key] - 2) / idx[key])
    idx[key] = [idx[key], temp]
  
for i in range(N):
  key = A[i]
  print(A_sum - idx[key][1])

Submission status: Accepted

Solution code:
import collections

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = collections.Counter(A)
    comb_dict = {}
    for num, freq in c.items():
        comb_dict[num] = freq * (freq-1) // 2
    comb_sum = sum(comb_dict.values())

    ans_dict = {}
    for a in A:
        if a not in ans_dict:
            ans_dict[a] = comb_sum - comb_dict[a] + (c[a] - 1) * (c[a] - 2) // 2
        print(ans_dict[a])


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def check(L):
    r = {}
    a = 0
    c = 0
    for i in range(len(L)):
        if a != L[i]:
            if i != 0:
                r[a] = [comb(c), comb(c - 1)]
            a = L[i]
            c = 1 
        else:
            c += 1
    r[a] = [comb(c), comb(c - 1)]
    #print(r)
    return r

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

D = {}
for i in range(len(A)):
    D[i] = A[i]

D = sorted(D.items(), key=lambda x: x[1])
V = {}
for i in range(len(D)):
    V[D[i][0]] = i

AA = sorted(A)
rr = check(AA)
ss = 0
for k, v in rr.items():
    ss += v[0]
#print(ss)

for k in range(N):
    index = A[k]
    a = rr[index][0] - rr[index][1]
    print(ss - a)

Submission status: Accepted

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

ball_count = [0] * (n + 1)

for i in a:
    ball_count[i] += 1
def nCr(i):
    if i<=1:
        return 0
    else:
        return i*(i-1)//2

def get_total(ball_count):
    ans = 0
    for i in ball_count:
        ans += nCr(i)
    return ans


total = get_total(ball_count)
for i in range(1, n + 1):
    num = ball_count[a[i - 1]]
    ans = total - nCr(num) + nCr(num-1)
    print(ans)

Submission status: Accepted

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

N = int(input())
A = tuple(map(int,input().split()))
cnt = [0]*200005
tot = 0
for i in A:
    cnt[i] += 1
for i in cnt:
    tot += nC2(i)
for i in range(N):
    ans = tot - nC2(cnt[A[i]]) + nC2(cnt[A[i]]-1)
    print(ans)

Submission status: Accepted

Solution code:
import math
def combination(n,r):
    if r<n:
        o=math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        o=1 if r==n else 0
    return o

N=int(input())
A=list(map(int,input().split()))
l=[0]*N
m=[0]*N
s=0
for i in A:l[i-1]+=1
for i in range(N):
    c=combination(l[i],2)
    s+=c
    m[i]=c-combination(l[i]-1,2)
for i in A:print(s-m[i-1])

Submission status: Accepted

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

from collections import Counter
cnt = Counter(A)

ans = 0
for v in cnt.values():
    ans += v*(v-1)//2
for i in A:
    a = cnt[i]
    dif = a*(a-1)//2 - (a-1)*(a-2)//2
    print(ans-dif)

Submission status: Accepted

Solution code:
#21 D - Banned K AC TLE(hint)
import collections
N = int(input())
A = list(map(int,input().split()))

cnt = collections.Counter(A)
#先に組み合わせの個数を計算しておく
combinations = {}
for j in cnt.keys():
    if cnt[j] >= 2:
        comb = (cnt[j]*(cnt[j]-1))//2
        combinations[j] = comb      

#あるボールを選ばないとき、組み合わせの総数は
#(全組み合わせ数) - (選ばないボールと同じボールの数 - 1)
tot = sum(combinations.values())
ans = []
for i in A:
    a = tot - (cnt[i] - 1)
    ans.append(a)
    
for k in ans:
    print(k)

Submission status: Accepted

Solution code:
def nc2(n):
    if n==0:
        return 0
    return n*(n-1)/2


n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
ans=0
dic={}
for i in arr:
    dic[i]=dic.get(i,0)+1
for i in dic:
    if dic[i]==1 :
        continue
    ans+=nc2(dic[i])
    
#print(dic)

for i in range(n):
    curr_nc2=nc2(dic[arr[i]])
    # print(curr_nc2)
    print(int(ans-curr_nc2+nc2(dic[arr[i]]-1)))

Submission status: Accepted

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

#全体の数列について答えを求めるnC2=n(n-1)/2
ALL=0
for i in c.values():
  if i>1:
    ALL += i*(i-1)//2

#各要素が影響する分を求める
for i in A:
  if c[i]>2:
    print(ALL-c[i]+1)
  elif c[i]==2:
    print(ALL-1)
  else:
    print(ALL)

Submission status: Accepted

Solution code:
import collections
import math

def c2(x):
  c = math.factorial(x) // (math.factorial(x-2)*2)
  return c

n = int(input())

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

adic = collections.Counter(a)

cnt = 0

for i in adic:
  num = adic[i]
  if num > 1:
    cnt += c2(num)

for i in a:
  num = adic[i]
  if num == 1:
    print(cnt)
  else:
    print(cnt - (num-1))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
M = max(A)

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

S = 0
for i in range(M + 1):
    m = count[i]
    S += (m * (m - 1)) // 2

ans = [S for _ in range(N)]
for i in range(N):
    m = count[A[i]]
    ans[i] -= m - 1

for i in range(N):
    print(ans[i])

Submission status: Accepted

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

from collections import Counter
cnt = Counter(A)

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

for a in A:
    k = cnt[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()))
B = [0 for i in range(N)]
for a in A:
  B[a-1] += 1
s = 0
for b in B:
  s += b * (b-1) // 2
for a in A:
  print(s - (B[a-1]-1))

Submission status: Accepted

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

x = 0
for j in range(n):
    x += ((b[j] - 1) * b[j] / 2)

for k in range(n):
    print(int(x - b[a[k] - 1] + 1))

Submission status: Accepted

Solution code:
import collections

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

c=collections.Counter(a)
s=0

for i in c.values():
  s+=i*(i-1)//2 #ボールを抜かない場合に2つのボールを選ぶ方法の数

for j in a:
  f=c[j]
  print(s-(f-1)) #下記メモ参照

'''
複数個のボールから1個のボールが抜かれる場合、i*(i-1)//2が(i-1)*(i-2)//2となる。

i*(i-1)//2-(i-1)*(i-2)//2
=i-1

よって、2つのボールを選ぶ方法が(i-1)だけ減る。
'''

Submission status: Accepted

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

cnt = Counter(A)
for i in cnt:
    total+=int(((cnt[i])*(cnt[i]-1))/2)

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

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
from functools import reduce
import sys
input = sys.stdin.readline

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

    ans = [0] * N
    cnt = Counter(A)
    s = 0
    for key, value in cnt.items():
        if value < 2:
            continue
        s += combinations_count(value, 2)
    
    for i in range(N):
        if cnt[A[i]]-1 == 0:
            ans[i] = s
        elif cnt[A[i]]-1 == 1:
            ans[i] = s - combinations_count(cnt[A[i]], 2)
        else:
            ans[i] = s - combinations_count(cnt[A[i]], 2) + combinations_count(cnt[A[i]]-1, 2)
    
    print(*ans, sep="\n")

main()

Submission status: Accepted

Solution code:
from collections import Counter

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

count = [0]*(N+1)

for a in A:
    count[a] += 1

sum_ = sum(map(lambda x: int(x*(x-1)/2), count))

ans = []

for a in A:
    temp = sum_ - (count[a] - 1)
    ans.append(temp)

print(*ans, sep='\n')

Submission status: Accepted

Solution code:
#import math
import collections
#n,k = map(int, input().split( ))
n = int(input())
A = list(map(int, input().split( )))
c = collections.Counter(A)
s = 0
for a in c:
  if c[a] >= 2:
    s += c[a]*(c[a]-1)//2
#print(s)  
for a in A:
  if c[a] == 1:
    print(s)
  else:
    print(s - c[a] + 1)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(A_list)
c_value_list = list(c.values())
#print(c_value_list)

ans = 0

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

for k in range(N):
    ans_temp = ans - c[A_list[k]] + 1
    print(ans_temp)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
count = collections.Counter(A) #こいつは辞書型みたいなもん
sum = 0
for x in count.values():
    sum += x*(x-1)//2
for x in A:
    print(sum-(count[x]-1))

Submission status: Accepted

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