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()))

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

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

for i in a:
  print(m-adict[i]+1)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

sum = 0

for key, value in dic.items():
    sum += value * (value - 1) // 2

for a in A:
    val = dic[a]
    pre_sum = sum - val * (val - 1) // 2
    ans = pre_sum + (val - 1) * (val - 2) //2
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
C = [0 for i in range(N + 1)]

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

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

Submission status: Accepted

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

dp = [0] * (N+1)

for i_A in A_list:
    dp[i_A] += 1

total = 0
for i_dp in dp:
    total += (i_dp*(i_dp-1)) // 2

for i_A in A_list:
    diff = (dp[i_A]*(dp[i_A]-1))//2 - ((dp[i_A]-1)*(dp[i_A]-2))//2
    print(total - diff)

Submission status: Accepted

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

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

for i in range(n):
    print(ans - cnt[a[i]] + 1)

Submission status: Accepted

Solution code:
import math

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

n= int(input())
a=list(map(int, input().split()))
b = [0]*(n+1)
c = [0]*(n+1)
su = 0

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

for y in range(len(b)):
    if b[y] >=2:
        c[y]=combinations_count(b[y], 2)
    else:
        c[y]=0
    su += c[y]

for z in range(n):
    if c[a[z]] > 0:
        ans = su - (b[a[z]] - 1)
    else:
        ans = su

    print(str(ans))

Submission status: Accepted

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

a_dict = {}
for num in a_l:
    if num in a_dict:
        a_dict[num] += 1
    else:
        a_dict[num] = 1

comb_dict = {}
comb_tot = 0
for num in a_dict:
    comb_dict[num] = a_dict[num]*(a_dict[num]-1)//2
    comb_tot += comb_dict[num]

for num in a_l:
    ans = comb_tot - (a_dict[num]-1)
    print(ans)

Submission status: Accepted

Solution code:
import itertools

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

gr=itertools.groupby(sorted(A))
s=0
dic={}
for x,group in gr:
  l=len(list(group))
  if l >= 2:
    s+=l*(l-1)/2
  dic[x]=l

for k in range(N):
  l=dic[A[k]]
  if l >= 2:
    print(int(s-l*(l-1)/2+(l-1)*(l-2)/2))
  else:
    print(int(s))

Submission status: Accepted

Solution code:
from math import factorial
def comb(n):
    if n == 1 or n == 0:
        return 0
    return int(n * (n-1) / 2 )

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

count = {}
ans = 0

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

for v in count.values():
    ans += comb(v)


for i in range(n):
    v = count[a[i]]
    print(ans - comb(v) + comb(v-1))

Submission status: Accepted

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

count={}
for ak in a:
    if ak not in count:
        count[ak] = 0
    count[ak] += 1

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

for ak in a:
    print(sum-(count[ak]-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 key in c:
    s+=c[key]*(c[key]-1)//2
for item in a:
    print(s-c[item]+1)

Submission status: Accepted

Solution code:
import math
from collections import Counter

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

c=Counter(a)
s=0
for key in c:
    s+=c[key]*(c[key]-1)//2
for item in a:
    print(s-c[item]+1)

Submission status: Accepted

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

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

x, y = [0]*(n+1), [0]*(n+1)
for i in range(n+1):
    x[i] = ct[i]*(ct[i]-1)//2
    y[i] = (ct[i]-1)*(ct[i]-2)//2
s = sum(x)

for i in a:
    print(s-x[i]+y[i])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 01:30:29 2020

@author: shinba
"""
import collections

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

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

B = collections.Counter(A)

W = 0
for i in B.values():
    W += conbination(i)

for i in A:
    print(W-B[i]+1)

Submission status: Accepted

Solution code:
import copy
# In=[list(map(int,i.split())) for i in Input.split('\n')]
# N=In[0][0]
# A=In[1]
N=int(input())
A=list(map(int,input().split()))
counter=[0 for i in range(N+1)]
ans2=[0 for i in range(N+1)]
B=[]
for i in A:
    counter[i]+=1
ans=[0 for i in range(N+1)]
for i,value in enumerate(counter):
    if value>0:
        ans[i]=(value*(value-1)//2)
        ans2[i]=((value-1)*(value-2)//2)
total=sum(ans)
for i in A:
    temp=total
    temp-=ans[i]
    temp+=ans2[i]
    print(temp)

Submission status: Accepted

Solution code:
stdin =open(0).read().split('\n')
N=int(stdin[0])
A=list(map(int,stdin[1].split()))
from collections import Counter
C=Counter(A)
ans=0
for i in C.values():
    ans+=i*(i-1)//2
for a in A:
    num = C[a]
    print(ans-num*(num-1)//2+(num-1)*(num-2)//2)

Submission status: Accepted

Solution code:
import collections

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

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

for i in A:
    n = C[i]
    if n >= 2:
        print(count - n  + 1)
    else:
        print(count)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
dic={}
for a in A:
  if a in dic:
    dic[a]+=1
  else:
    dic[a]=1
   

c=0
for i in dic.values():
  c+=i*(i-1)/2
for j in range(1,N+1):
  d=c-dic[A[j-1]]+1
  print(int(d))

Submission status: Accepted

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

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

for k in range(n):
    num = a[k]
    if tmp[num] == 0:
        print(ans)
        continue
    num = tmp[num]
    print(ans - num*(num-1)//2 + (num-1)*(num-2)//2)

Submission status: Accepted

Solution code:
n=int(input())
al=[int(a) for a in input().split()]
cnt = {}
total=0

for a in al:
	if a not in cnt:
		cnt[a] = 0
	cnt[a]+=1
#print(cnt)

for i in cnt.values():
	total += i*(i-1)//2
	#print("i: ",i)
#print(cnt.values())
for a in al:
	print(total - (cnt[a]-1))

Submission status: Accepted

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

c = collections.Counter(a)

nc=[-1]*(n+1)
def nConb(n):
  if nc[n] != -1:
    return nc[n]
  nc[n]=n*(n-1)//2
  return nc[n]

temp=0
for k,v in c.items():
  temp+=nConb(v)

for i in range(n):
  if c[a[i]] == 1:
    print(temp)
  else:
    print(temp-(c[a[i]]-1))

Submission status: Accepted

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

B = {}
s = 0
for a in A:
    if a not in B:
        B[a] = 0
    B[a] += 1
    s += (B[a] - 1)

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

Submission status: Accepted

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

B = {}
for a in A:
    if a not in B:
        B[a] = 0
    B[a] += 1

sum = 0
for b in B:
    sum += B[b] * (B[b] - 1) // 2

for a in A:
    print(sum - (B[a] - 1))

Submission status: Accepted

Solution code:
n=int(input())
alist=list(map(int,input().split()))
adic={}

for a in alist:
    if a in adic:
        adic[a]=adic[a]+1
    else:
        adic[a]=1

kumisum=0
for tmp in adic.values():
    kumisum += int(tmp*(tmp-1)/2)

for i in range(n):
    print(kumisum-(adic[alist[i]]-1))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
ans=0
for i in d:
    ans+=d[i]*(d[i]-1)//2
for i in a:
    print(ans-(d[i]*(d[i]-1))//2+((d[i]-1)*(d[i]-2)//2))

Submission status: Accepted

Solution code:
# inputを高速化する。
import sys
input = sys.stdin.readline

n = int(input())

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

cnt = {}

for ball in balls:
    if ball not in cnt:
        cnt[ball] = 0
    cnt[ball] += 1

def combination(nxt, num = 0):
    return (cnt[nxt]-num) * (cnt[nxt]-1- num) // 2

total = 0
for nxt in cnt:
    total += combination(nxt)


for ball in balls:
    ans = total -  combination(ball) + combination(ball,1)
    print(ans)

Submission status: Accepted

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

a_types = set(A)

count_dict = [0] * N
for each in A:
    count_dict[each - 1] += 1

all_types = 0
for value in count_dict:
    all_types += value * (value - 1) // 2

# result_dict = {}
# for each in a_types:
#     result_dict[each] = all_types - count_dict[each] + 1

for a in A:
    print(all_types - count_dict[a - 1] + 1)

Submission status: Accepted

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

count_dict = [0] * N
for each in A:
    count_dict[each - 1] += 1

all_types = 0
for value in count_dict:
    all_types += value * (value - 1) // 2

# result_dict = {}
# for each in a_types:
#     result_dict[each] = all_types - count_dict[each] + 1

for a in A:
    print(all_types - count_dict[a - 1] + 1)

Submission status: Accepted

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

c = [0]*n

for i in range(n):
    c[a[i]-1] = c[a[i]-1] + 1
    
total = 0
for x in c:
    total = total + x*(x-1)//2

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

Submission status: Accepted

Solution code:
from collections import defaultdict
num = defaultdict(lambda : 0)
n = int(input())
a = [int(i) for i in input().split()]
for i in a:
    num[i] += 1
memo = {i: 0 for i in num.keys()}
total = 0

for i, j in num.items():
    x = int(j * (j - 1) / 2 )
    memo[i] = x
    total += x

for i, j in enumerate(a):
    y = num[j]
    z = memo[j]

    if y == 1:
        print(total)
    else:
        print(total - z + int((y - 1) * (y - 2) / 2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
s = sum(x * (x - 1) // 2 for x in cnt.values())
for x in a:
    print(max(0, s - cnt[x] + 1))

Submission status: Accepted

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

dic = {}

for A in As:
	if A not in dic.keys():
		dic[A] = 1
	else:
		dic[A] = dic[A] + 1

cnt = 0

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

for A in As:
	print(cnt - (dic[A]-1) )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
from collections import Counter

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
c = Counter

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

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

count = 0
C = Counter(A)
for num in C.values():
    count += num*(num-1)//2
#print(count)

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

Submission status: Accepted

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

dic = dict()
for i in a:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

cnt = 0
dic_v = list(dic.values())
for i in range(len(dic_v)):
    x = dic_v[i]
    cnt += int(x*(x-1)/2)
    
for k in range(n):
    print(cnt - (dic[a[k]]-1))

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
num={i:0 for i in range(N)}
for i in A:
    num[i-1]+=1
def set(k):
    if k >= 2:
        return (k)*(k-1)/2
    else:
        return 0
def selfset(k):
    if k >= 2:
        return (k-1)*(k-2)/2
    else:
        return 0

setnum={i:set(num[i]) for i in range(N)}
a=sum(setnum.values())

selfsetnum={i:selfset(num[i]) for i in range(N)}


for i in range(N):
    print(int(a-setnum[A[i]-1]+selfsetnum[A[i]-1]))

Submission status: Accepted

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

from collections import Counter

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

#
C=Counter()
for i in range(n):
    C[a[i]] += 1
xsum=0
for key, value in C.items():
    xsum += value*(value-1)//2

#
for k in range(n):
    print( xsum - (C[a[k]] - 1) )

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

input = sys.stdin.readline

from collections import Counter

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

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

#
C=Counter()
for i in range(n):
    C[a[i]] += 1
xsum=0
for key, value in C.items():
    xsum += value*(value-1)//2
    
#
for k in range(n):
    print( xsum - (C[a[k]] - 1) )

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 = 0

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

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

Submission status: Accepted

Solution code:
import math

#要素の数を数えたい場合（key:数）
n=int(input())
li = list(map(int,input().split()))

dic={}
for i in li:
    if i not in dic:
        dic[i] = 1
    elif dic[i] >= 1:
        dic[i] += 1
        
#print(dic)


cnt = 0
for j in dic:
    cnt += dic[j]*(dic[j]-1)/2
    #print(math.floor(cnt))

for i in range(n):
    print(math.floor(cnt - dic[li[i]]+1))

Submission status: Accepted

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

for i in a:
    if c[i] > 0:
        print(sum-(c[i]-1))

Submission status: Accepted

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

for i in a:
    num[i-1]+=1

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
from collections import Counter
def comb(n):
    if n==1:
        return 0
    else:
        return n*(n-1)/2
list=Counter(A)
sum=0
for number in list:
    sum=sum+comb(list[number])

for i in A:
    print(int(sum-comb(list[i])+comb(list[i]-1)))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n = II()
    A = LI()

    memo = {}
    for a in A:
        # memo[a] = memo.get(a, 0) + 1
        memo[a] = memo.setdefault(a, 0) + 1

    def choose2(a):
        if a == 0: return 0
        return a * (a-1) // 2

    # print(memo)
    ans = 0
    for k, v in memo.items():
        ans = ans + choose2(v)
    # print(ans)

    for a in A:
        print(ans - choose2(memo[a]) + choose2(memo[a] - 1))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n = II()
    A = LI()

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

    def choose2(a):
        if a == 0: return 0
        return a * (a-1) // 2

    # print(memo)
    ans = 0
    for k, v in memo.items():
        ans = ans + choose2(v)
    # print(ans)

    for a in A:
        print(ans - choose2(memo[a]) + choose2(memo[a] - 1))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

n = I()
A = LI()
c = [0]*(n+1)
d = [0]*(n+1)
for a in A:
    c[a-1] += 1
# print(c)
def nc2(num):  # num >= 0
    return int(num*(num-1)/2)
for i in range(n):
    d[i] = nc2(c[i])
# print(d)
dsum = sum(d)
for i in range(n):
    a = A[i] - 1
    print(dsum - nc2(c[a]) + nc2(c[a]-1))

Submission status: Accepted

Solution code:
import collections

N = int(input())
As = list(map(int,input().split()))
Cs = collections.Counter(As)

s = 0
for key in Cs:
  s = s + Cs[key]*(Cs[key] - 1)//2
  
for i in range(N):
  rlt = s - Cs[As[i]]*(Cs[As[i]] - 1)//2 + (Cs[As[i]] - 1)*(Cs[As[i]] - 2)//2
  print(rlt)

Submission status: Accepted

Solution code:
from __future__ import print_function

import array
import bisect
import collections
import copy
import fractions
import functools
import heapq
import itertools
import math
import operator
import random
import re
import string
import sys
import time
from collections import Counter, deque
from fractions import Fraction
from functools import reduce
from heapq import heapify, heappop, heappush, heappushpop, heapreplace
from itertools import accumulate
from operator import mul
from queue import PriorityQueue as pq
from queue import Queue

sys.setrecursionlimit(500000)









input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    if r>n:
        return 0
    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().strip())
    a = list(map(int, input().strip().split()))
    C=Counter()
    for i in range(n):
        C[a[i]]+=1
    ans_temp=0
    X=[0 for _ in range(n+1)]
    for key, value in C.items():
        X[key] = combinations_count(value, 2)
        ans_temp += X[key]
    for k in range(n):
        print( ans_temp - X[a[k]] + combinations_count(C[a[k]]-1,2) )


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

a = LIST()

occ = defaultdict(int)

for el in a:
    occ[el]+=1

tot = 0

for key in occ:
    tot+=occ[key]*(occ[key] - 1)//2

for el in a:
    print(tot - occ[el] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
 
n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
total = sum(i*(i - 1)//2 for i in b.values()) 

for i in a:

    print(total - (b[i] - 1))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B={}
for i in A:
  if i not in B:
    B[i]=0
  B[i]+=1
c=0
for v in B.values():
    c+=v*(v-1)/2
for i in range(N):
  print(int(c-B[A[i]]+1))

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

n = readInt()
a = readInts()

d = defaultdict(int)

ans = 0

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

maxv = 0
for i in d:
	maxv+=comb(d[i],2)

for k in range(0,n):
	b = d[a[k]]
	delta = comb(b,2)-comb(b-1,2)
	ans = maxv-delta
	print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
cnt = {}
total = 0

for a in A:
    if a not in cnt:
        cnt[a] = 0
    cnt[a] += 1

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
total = 0

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

ans = [total - (d[i] - 1) for i in a]
print("\n".join(map(str,ans)))

Submission status: Accepted

Solution code:
def ban():
    n = int(input())
    a = list(map(int, input().split()))
    d = {}
    total = 0

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

    ans = [total - (d[i] - 1) for i in a]
    return "\n".join(map(str, ans))

print(ban())

Submission status: Accepted

Solution code:
def ban():
    from collections import Counter
    n = int(input())
    a = list(map(int, input().split()))
    b = Counter(a)
    total = sum(i*(i - 1)//2 for i in b.values())

    ans = [(total - (b[i] - 1)) for i in a]
    return "\n".join(map(str, ans))

print(ban())

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
total = sum(i*(i - 1)//2 for i in b.values())

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
ans = 0

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

for i in a:
    print(ans - (b[i] - 1))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
D = Counter(A)
L = list(D.values())

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

for k in range(n):
    print(ans-(D[A[k]]-1))

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 k,v in d.items():
    t += (v * (v-1)) // 2
for i in range(n):
    v = d[a[i]]
    s = t - (v * (v-1)) // 2 + ((v-1) * (v-2))//2
    print(s)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
total = 0

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
total = 0

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve_iter(N: int, A: "List[int]"):
    from collections import Counter
    def f(n):
        return n * (n - 1) // 2
    c = Counter(A)
    base = sum(map(f, c.values()))
    for a in A:
        yield base - f(c[a]) + f(c[a]-1)

def solve(N: int, A: "List[int]"):
    return list(solve_iter(N, A))

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(*solve(N, A), sep='\n')

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
max_two = [0]*(N+1)
one_minus = [0]*(N+1)
A_cnt = Counter(A)
A_cnt = A_cnt.most_common()
for a in A_cnt:
    max_two[a[0]] = (a[1]*(a[1]-1))//2
    one_minus[a[0]] = ((a[1]-1)*(a[1]-2))//2
max_sum = sum(max_two)

for i in range(N):
    print(max_sum-max_two[A[i]]+one_minus[A[i]])

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
ans=0
for i in list(set(a)):
  ans+=d[i]*(d[i]-1)//2
for i in a:
  kari=ans
  kari-=d[i]*(d[i]-1)//2
  kari+=(d[i]-1)*(d[i]-2)//2
  print(kari)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter

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

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

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

all = int(all)
val_cnt = Counter(A)
for a in A:
    print(all - (val_cnt[a] - 1))

Submission status: Accepted

Solution code:
n = int(input())
alist = list(map(int, input().split(" ")))
blist = [0]*n

for i in range(n):
  blist[alist[i]-1] += 1

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

for i in range(n):
  print(int(total-(blist[alist[i]-1]-1)))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
m = list(map(int,input().split()))   
ctr = Counter(m)
all = 0
def f(n):
    return n*(n-1)//2

for i in ctr.values():
    all += f(i)

ans = []
for j in m:
    ans.append(all - ctr[j] +1)

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)

total = 0
for i in c.items():
    x = i[1]
    total += x*(x-1)//2

for i in a:
    ans = total
    ans -= (c[i] - 1)
    print(ans)

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    assert len(A) == N
    ball = collections.Counter(A)
    total = 0
    for i in ball.values():
        if i >= 2:
            total += combinations_count(i, 2)
    for k in range(N):
        if ball[A[k]] == 2:
            print(total - 1)
        elif ball[A[k]] == 1:
            print(total)
        else:
            print(total - ball[A[k]] + 1)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc159/tasks/abc159_d

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

box = [0] * (N + 1)
box_2 = [0] * (N + 1)
box_3 = [0] * (N + 1)

for i in range(N):
    box[A_list[i]] += 1

for i in range(N + 1):
    if box[i] > 1:
        box_2[i] = (box[i] * (box[i] - 1)) / 2
        box_3[i] = ((box[i] - 1) * (box[i] - 2)) / 2

ans_base = sum(box_2)

for i in range(N):

    if box[A_list[i]] > 0:
        diff = box_2[A_list[i]] - box_3[A_list[i]]
        print(int(ans_base - diff))
    else:
        print(int(ans_base))

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    # *** Sample ***
    # num: count -> 1: 1, 2: 5, 3: 3
    # base     = 1_C_2 + 5_C_2 + 3_C_2
    # remove-1 = 0_C_2 + 5_C_2 + 3_C_2 = base - 0_C_1 (0_C_1 + 0_C_2 = 1_C_2)
    # remove-2 = 1_C_2 + 4_C_2 + 3_C_2 = base - 4_C_1 (4_C_1 + 4_C_2 = 5_C_2)
    # remove-3 = 1_C_2 + 5_C_2 + 2_C_2 = base - 2_C_1 (2_C_1 + 2_C_2 = 3_C_2)
    N = int(input())
    A = list(map(int, input().split(' ')))
    counter = Counter(A)
    base_num = sum([n * (n - 1) // 2 for n in counter.values()])
    for k in range(N):
        print(base_num - (counter[A[k]] - 1))


if __name__ == '__main__':
    main()