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:
# abc159_d.py
import sys
import time
import unittest
from io import StringIO
from collections import defaultdict


def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    dd = defaultdict(int)
    idx = 0
    cmb = [0] * (N + 1)
    for a in A:
        dd[a] += 1
        idx += 1
        cmb[idx] = cmb[idx - 1] + (idx - 1)
    sum = 0
    for j in dd.keys():
        sum += cmb[dd[j]]
    for i in range(N):
        print(sum - (cmb[dd[A[i]]] - cmb[dd[A[i]] - 1]))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Submission status: Accepted

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

counter=[0]*(n+1)
for a in al:
    counter[a]+=1

sum=0
for c in counter:
    sum+=c*(c-1)//2

for a in al:
    print(sum-(counter[a]-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
conb1, conb2 = [], []
for i in range(n + 1):
    conb1.append(cnt[i] * (cnt[i] - 1) // 2)
    if cnt[i] == 0:
        conb2.append(0)
    else:
        conb2.append((cnt[i] - 2) * (cnt[i] - 1) // 2)
sumall = sum(conb1)
for i in range(n):
    ans = sumall - conb1[a[i]] + conb2[a[i]]
    print(ans)

Submission status: Accepted

Solution code:
n = (int)(input())
a = list(map(int, input().split(" ")))
temp = [0 for i in range(n + 1)]
aaa = sorted(a)
count = 0
for i in range(n - 1):
  if aaa[i] == aaa[i + 1]:
    count+= 1
  else:
    temp[aaa[i]] = count + 1
    count = 0
else:
  if len(a) == 1:
    temp[aaa[0]] = 1
  else:
    temp[aaa[-1]] = count + 1
ans = 0

#---ここまで231ms

#print(temp)#各数字の出現回数
for i in range(n + 1):
  if temp[i] >= 2:
    ans += temp[i] * (temp[i] - 1) / 2
    
#---ここまでで1553ms

#print(ans)#ansは全体の組み合わせの合計。割れない

for i in range(n):
  kotae = ans
  if temp[a[i]] >= 3:
    #print(ans, comb(temp[a[i]], 2), comb(temp[a[i]] - 1, 2))
    #print(temp[a[i]], i)
    kotae = ans - (temp[a[i]] * (temp[a[i]] - 1) / 2) + ((temp[a[i]] - 1) * (temp[a[i]] - 2) / 2)
  elif temp[a[i]] == 2:
    kotae = ans -1
  print((int)(kotae))

Submission status: Accepted

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

balls = {}

for a in A:
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1
res = 0
for b in balls:
  count = balls[b]
  res += count * (count-1) // 2

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

Submission status: Accepted

Solution code:
import math
import sys

n = int(sys.stdin.readline().rstrip())
a = list(map(int, sys.stdin.readline().rstrip().split()))
num = [0] * n
for ai in a:
    num[ai - 1] += 1

c = 0
for n in num:
    c += n * (n - 1) // 2

for ai in a:
    print(c - num[ai-1] + 1)

Submission status: Accepted

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

cnt = [ 0 ] + [ 0 ] * n

for a_i in  a:
    cnt[a_i] += 1

all_combi = 0
for m in cnt:
    all_combi += m * ( m - 1 ) // 2

for k in range( 1, n+1 ):
    print( all_combi - ( cnt[a[k-1]] - 1 ) )

Submission status: Accepted

Solution code:
import math

n = int(input())
a_list = list(map(int,input().split()))
num = [0]*(n+1)
for a in a_list:
  num[a] += 1
c = 0
for n in num:
  if n != 0:
    c += n*(n-1)//2
for a in a_list:
  print(c-num[a]+1)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
su = 0
l = list(map(int,input().split()))
l1 = Counter(l)
for k in l1.values():
  su += k*(k-1)//2
for t in l:
  a = l1[t]
  q = su-a*(a-1)//2+(a-1)*(a-2)//2
  print(q)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

num_dict = {}
for a in a_list:
    num_dict.setdefault(a,0)
    num_dict[a] += 1

all_comb = 0
for num,cnt in num_dict.items():
    comb = cnt*(cnt-1)//2
    all_comb += comb

for a in a_list:
    a_cnt = num_dict[a]
    a_comb_diff = a_cnt*(a_cnt-1)//2 - (a_cnt-1)*(a_cnt-2)//2
    print(all_comb-a_comb_diff)

Submission status: Accepted

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

from collections import Counter		#defaultdictやなくてConter使ってこ

C=Counter(a)

sum=0
for v in list(C.values()):
	sum+=v*(v-1)//2

for k in range(n):
	x=C[a[k]]
	print(sum-(x*(x-1)//2)+((x-1)*(x-2)//2))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr(), dtype=np.int32)
counter = np.bincount(A)
case = (counter * (counter-1) // 2).sum()
answer = case - (counter[A] - 1)
print('\n'.join(answer.astype(str)))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr(), dtype=np.int32)
counter = np.bincount(A)
case = (counter * (counter-1) // 2).sum()
answer = case - (counter[A] - 1)
print('\n'.join(map(str, answer)))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from math import factorial
from collections import Counter, defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def comb(n, r):  # 組み合わせ
    if n <= 0 or r > n: return 0
    return factorial(n) // factorial(n - r) // factorial(r)

N = ir()
A = lr()
counter = Counter(A)
case = 0
dic = defaultdict(int)
for k, v in counter.items():
    dic[k] = v
    case += comb(v, 2)

for a in A:
    num = dic[a]
    answer = case - (num - 1)
    print(answer)

Submission status: Accepted

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

cnt = dict()
for i in range(n):
    try:
        cnt[a[i]] += 1
    except:
        cnt[a[i]] = 1

ansnow=0
for v,c in cnt.items():
    ansnow += c*(c-1)//2

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

Submission status: Accepted

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

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

for i in range(1, n+1):
    print(sumx - x[a[i-1]] * (x[a[i-1]]-1) // 2 + (x[a[i-1]]-1) * (x[a[i-1]]-2) // 2)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
An = list(map(int, input().split()))
counter = Counter(An)
cnt = 0
for c in counter.values():
    cnt += c * (c-1) // 2
for a in An:
    print(cnt - (counter[a] - 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 
aa=0
ans=[]
for i in range(n+1):
    aa+=(cnt[i]*(cnt[i]-1)//2)
for i in range(n):
    print(aa-(cnt[a[i]]*(cnt[a[i]]-1))//2 +((cnt[a[i]]-1)*(cnt[a[i]]-2))//2, end ="\n")

Submission status: Accepted

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

import collections
import math

dicA = collections.Counter(A)

total = 0
for x in list(dicA.values()):
  if x != 1:
    total += math.factorial(x) // (math.factorial(x - 2)*2)

for i in range(N):
  print(total - dicA[A[i]] +1)

Submission status: Accepted

Solution code:
import collections
def C(n):
    return n * (n-1) // 2
N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
d = {}
sm = 0
for k,v in c.most_common():
    d[k] = v
    sm += C(v)
for a in A:
    print (sm - d[a] + 1)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    m = {}

    for i in a:
        if i not in m:
            m[i] = 1
        else:
            m[i] += 1
    
    ans = 0

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

Submission status: Accepted

Solution code:
def possibleCombos(num):
    num2 = num - 1
    return (num2*num2 + num2)//2
N = int(input())
A = [int(i) for i in input().split()]
data = [0] * (N+1)
total = 0
for i in A:

    data[i] +=1

for i in data:
    total += possibleCombos(i)
for i in A:
    result = total - possibleCombos(data[i]) + possibleCombos(data[i]-1)
    print(result)

Submission status: Accepted

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

c = Counter(a)
key = c.keys()
comb = 0
for i in key:
    comb += (c[i]) * (c[i]-1) // 2

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
m = {}
for a in A:
    m.setdefault(a, 0)
    m[a] += 1
# print(m)
all_combination = 0
for v in m.values():
    if v > 1:
        all_combination += int(v * (v - 1) / 2)
for a in A:
    ans = all_combination - (m[a] - 1)
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(int(x) for x in input().split())

nball = [0] * (N+1)
for i in range(N):
    nball[Alist[i]]+=1

sum=0
for i in range(N):
    sum+=nball[i+1]*(nball[i+1]-1)/2
    
for i in range(N):
    if(nball[Alist[i]] > 2):
        print(int(sum-( ((nball[Alist[i]]-1)*2) /2 )))
    elif(nball[Alist[i]] == 2):
        print(int(sum-1))
    else:
        print(int(sum))

Submission status: Accepted

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

allCount = 0
c = Counter(A)

for i, v in c.items():
    if (v == 1):
        continue
    else:
        allCount += v * (v - 1) // 2


if (allCount == 0):
    for i in range(N):
        print(0)
    exit()

for i in range(N):
    a = allCount
    num = c[A[i]] - 1
    if (num == 1 or num == 0):
        print(allCount - num)
        continue
    old = c[A[i]] * (c[A[i]] - 1) // 2
    count = num * (num - 1) // 2
    print(a - old + count)

Submission status: Accepted

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

d = dict()
for a in A:
    d.setdefault(a, 0)
    d[a] += 1

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

for k in range(0, N):
    ak = A[k]
    print(counts - d[ak] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''
import collections

n = int(input())
a = list(map(int, input().split()))
alis = collections.Counter(a)
ss = []
for i in range(1, n+1):
    ss.append(alis[i]*(alis[i]-1)//2)
#print(ss,alis)

chinpo=sum(ss)
#print(chinpo)
for i in range(n):
    #ss[a[i]-1] = (alis[a[i]]-1)*(alis[a[i]]-2)//2
    print(chinpo-ss[a[i]-1]+(alis[a[i]]-1)*(alis[a[i]]-2)//2)

Submission status: Accepted

Solution code:
from collections import defaultdict
import math

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

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

for x in A:
    d[x]+=1
s=0
for v in d.values():
    if v>1:
        s+=comb(v,2)

for j in range(N):
    num=A[j]
    z=d[num]-1
    print(s-z)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
c = [0 for i in range(n+1)]
b = sorted(a)
cnt = 1
for i in range(n-1):
  if b[i]==b[i+1]:
    cnt += 1
  else:
    c[b[i]] = cnt
    cnt = 1
  if i==n-2:
    c[b[n-1]] = cnt
ans = 0
for i in range(n+1):
  ans += int(c[i]*(c[i]-1)/2)
for i in range(n):
  print(ans-(c[a[i]]-1))

Submission status: Accepted

Solution code:
def z(z_dic, v: int):
    if v in z_dic:
        return z_dic[v]
    z = 0
    if v == 2:
        z = 1
    elif v > 2:
        z = int(v*(v-1)/2)
    z_dic.setdefault(v, z)
    return z


def main():
    n = int(input())

    ar = [0] * (n+1)
    ls = []

    for x in map(int, input().split()):
        ls.append(x)
        ar[x] += 1

    zls = []
    zls2 = []

    z_dic = {}

    for i in range(1, n+1):
        v = ar[i]
        zls.append(z(z_dic, v))
        zls2.append(z(z_dic, v-1))
    ss = sum(zls)

    for i in range(1, n+1):
        aa = ls[i-1]
        print(ss - zls[aa-1] + zls2[aa-1])


main()

Submission status: Accepted

Solution code:
def z(z_dic, v: int):
    if v in z_dic:
        return z_dic[v]
    z = 0
    if v == 2:
        z = 1
    elif v > 2:
        z = int(v*(v-1)/2)
    z_dic.setdefault(v, z)
    return z


def main():
    n = int(input())

    ar = [0] * (n+1)
    ls = []

    for x in map(int, input().split()):
        ls.append(x)
        ar[x] += 1

    zls = []
    zls2 = []

    z_dic = {}

    for i in range(1, n+1):
        v = ar[i]
        zls.append(z(z_dic, v))
        zls2.append(z(z_dic, v-1))
    ss = sum(zls)

    for i in range(1, n+1):
        aa = ls[i-1]
        print(ss - zls[aa-1] + zls2[aa-1])
    #
    # itr = map(int, input().split())
    # dic = {}
    # for num in itr:
    #     dic.setdefault(num, 0)
    #     dic[num] += 1
    #     ls.append(num)
    #
    # for i in range(n):
    #     # ls[i]を除く
    #     a = ls[i]
    #     dic[a] -= 1
    #     res = 0
    #     for k, v in dic.items():
    #         if v < 2:
    #             continue
    #         elif v == 2:
    #             res += 1
    #         else:
    #             res += int(v*(v-1)/2)
    #     print(res)
    #     dic[a] += 1


main()

Submission status: Accepted

Solution code:
import numpy as np
def main():
    from collections import Counter
    n = int(input())
    a = tuple(map(int, input().split()))
    ans = 0
    c = Counter(a)
    for i in range(n):
        ans += c[i+1]*(c[i+1]-1)//2
    for j in range(n):
        print(ans - c[a[j]] + 1)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
total=0
A_li=[0]*(1+2*10**5)
for i in range(N):
    A_li[A[i]]+=1
for i in range(1+2*10**5):
    total+=A_li[i]*(A_li[i]-1)//2
for i in range(1,N+1):
    if A_li[A[i-1]]>1:
        print(total-(A_li[A[i-1]]-1))
    else:
        print(total)

Submission status: Accepted

Solution code:
N = int(input())
K = list(map(int,input().split()))
A = [0]*N
for i in range(N):
    A[K[i]-1] += 1
r = 0
for k in range(N):
    r += 0.5*(A[k]*(A[k]-1))
An =[]
for j in range(N):
    D = A[K[j]-1]-1
    An.append(int(r-(0.5*(D+1)*(D))+(0.5*D*(D-1))))
for i in range(len(An)):
    print(An[i])

Submission status: Accepted

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

for k in range(n):
    print(al - (l[a[k]-1] - 1))

Submission status: Accepted

Solution code:
import math

n=int(input())
a=list(map(int,input().split()))
table={}
fact_table={}
total=0
for i in a:
    if i in table:
        table[i]+=1
    else:
        table[i]=1
for key in table:
    if table[key]>=2:
        fact_table[key]=math.factorial(table[key])//(math.factorial(2)*math.factorial(table[key]-2))
        total+=fact_table[key]
    else:
        fact_table[key]=0
for i in a:
    ans=total-fact_table[i]+((table[i]-2)*fact_table[i])//table[i]
    print(ans)

Submission status: Accepted

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

a_count = Counter(a_input)

score_dic={}
exclude_dic={}

for k,v in a_count.items():
    score_dic[k]=v*(v-1)//2
    exclude_dic[k]=(v-1)*(v-2)//2

score_sum = sum(score_dic.values())

for i in range(n):
    print(score_sum-score_dic[a_input[i]]+exclude_dic[a_input[i]])

Submission status: Accepted

Solution code:
from collections import Counter
def comb(x):
    return x*(x-1)//2
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
b = 0
for i in c.values():
    b+=comb(i)
for k in range(n):
    print(b-c[a[k]]+1)

Submission status: Accepted

Solution code:
from collections import Counter

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

l = Counter(A)
ans = sum([v*(v-1)//2 for v in l.values()])

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

Submission status: Accepted

Solution code:
import collections


def permutations_count(n, r):
    return n*(n-1)//2

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

cnt = collections.Counter(A)
per_cnt = {k:permutations_count(v, 2) for k,v in cnt.items()}
pre_total = sum(per_cnt.values())

for a in A:
    print(pre_total-per_cnt[a]+permutations_count(cnt[a]-1, 2))

Submission status: Accepted

Solution code:
N=int(input())
kazu=[0 for i in range(N)]
A=[int(x) for x in input().rstrip().split()]

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

cnt=0
ans=[]
for i in kazu:
  ans.append((i*(i-1))//2)

cnt=sum(ans)
for i in range(N):
  now=kazu[A[i]-1]-1
  print(cnt-ans[A[i]-1]+(now*(now-1))//2)

Submission status: Accepted

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

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

d = {}
for i in a:
    if i not in d.keys():
        d[i] = 1 
    else:
        d[i] += 1

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

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

Submission status: Accepted

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

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

cnt = {}
for a in A:
    if a not in cnt:
        cnt[a] = 1
    else:
        cnt[a] += 1
res = 0
for k, v in cnt.items():
    if v >= 2:
        res += combi2(v)

ans = []
for a in A:
    v = cnt[a]
    if v >= 2:
        ans.append(res-combi2(v)+combi2(v-1))
    else:
        ans.append(res)
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
from collections import defaultdict


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


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

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

total = 0
for k, v in h.items():
    total += nC2(v)

for k in range(N):
    t = A[k]
    print(total - h[t] + 1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N = ii()
A = li()
d = defaultdict(int)

for num in A:
    d[num] += 1

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

for num in A:
    if d[num] == 1:
        print(sum_a)
    elif d[num] == 2:
        print(sum_a-1)
    else:
        print(sum_a-d[num]+1)

Submission status: Accepted

Solution code:
# D - Banned K

from collections import Counter

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

counter = Counter(A)
total = 0
for v in counter.values():
    total += v * (v - 1) // 2

for i in range(N):
    print(total - (counter[A[i]] - 1))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

Solution code:
#  =========     /\       /|    |====/|
#      |        /  \       |    |   / |
#      |       /____\      |    |  /  |
#      |      /      \     |    | /   |
#  ========= /        \  =====  |/====|  
#  code

def c(n):
    return max(0 , (n * (n - 1)) // 2)

def main():
    from collections import Counter
    n = int(input())
    a = list(map(int , input().split()))
    C = Counter(a)
    ans = 0
    for i in C:
        ans += c(C[i])
    
    for i in a:
        pans = ans - c(C[i]) + c(C[i] - 1)
        print(pans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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

import collections

cnt = collections.Counter(s)
ans = sum([(v * (v-1)) // 2 for k, v in cnt.items()])

for i in range(len(s)):
  print(ans - cnt[s[i]] + 1)

Submission status: Accepted

Solution code:
import collections

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

C = collections.Counter(A)
C2 = {}
total = 0
for v in C:
  c = C[v]
  c2 = c*(c-1)//2
  total += c2
  C2[v]=(c2-(c-1)*(c-2)//2)
  
for a in A:
  print(total-C2[a])

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))

n = inp()
a = inpl()
dic = collections.defaultdict(int)
origin=0
for i in a:
    origin+=dic[i]
    dic[i] += 1

for i in a:
    print(origin-dic[i]+1)

Submission status: Accepted

Solution code:
import collections

if __name__ == '__main__':

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

    c = collections.Counter(a)

    all = 0

    for item in c.items():
        all += item[1] * (item[1] - 1) //2

    for i in range(n):
        item = c[a[i]]
        after = item * (item-1) // 2
        before = (item-1) * (item-2) // 2
        #print(after, before)
        ans = all - after + before
        print(ans)

Submission status: Accepted

Solution code:
import math
import collections

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

equal = 0
elements = collections.Counter(A)
equal = sum([v * (v - 1) // 2 for v in elements.values()])

for i in range(n):
    equal_i = elements[A[i]] - 1
    print(equal - equal_i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
c = Counter(A)
cnt = [0]*N
for k,v in c.items():
    cnt[k-1] = v*(v-1)//2
num = sum(cnt)
ans = 0
for a in A:
   print(num-c[a]+1)

Submission status: Accepted

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

for i in range(N):
    count_list[arr[i-1]-1] +=1

sum = 0
for i in range(N):
    if count_list[i] != 0:
        sum += int(count_list[i]*(count_list[i]-1)/2)

for i in range(N):
    print(sum+1-count_list[arr[i]-1])

Submission status: Accepted

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

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

s = 0
for i in range( N ):
  l = L[ i ]
  if l > 0:
    s += ( l - 1 ) * l // 2

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

Submission status: Accepted

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

b = {}
for i in range(N):
    if A[i] not in b:
        b[A[i]] = 1
    else:
        b[A[i]] += 1

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
from math import factorial

def comb(n, r):
    if n >= r:
        return factorial(n) // (factorial(n - r) * factorial(r))
    else:
        return 0
  
n = int(input())
a = list(map(int, input().split()))

clc = cl.Counter(a)

# 1. 数字が等しいペアを選ぶ総数
npair = 0
for (k, v) in clc.items():
    npair += comb(v, 2)

# 2. (取り除いた数字と同じ数字を選ぶ総数) - (1)
for a_ in a:
    print(npair - (clc[a_] - 1))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = [0] * n
res = 0

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

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

for i in a:
    print(res - c[i-1] + 1)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
MOD = 10**9 + 7

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

#ひとつものぞかず、同じ数のボールをとるのは？
c = collections.Counter(a)
maxa = 0
item = [0 for i in range(max(a))]
for key,value in c.items():
    item[key-1] = value
    maxa += (value)*(value-1)//2
for i in range(len(a)):
    d = (item[a[i]-1])*(item[a[i]-1]-1)//2-(item[a[i]-1]-1)*(item[a[i]-1]-2)//2
    print(maxa-d)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
from collections import *
n,*a=map(int,open(0).read().split())
s=set(a)
c=Counter(a)
sm=0
for i in s:
  sm+=c[i]*(c[i]-1)//2
for i in a:
  print(sm-(c[i]*(c[i]-1)//2)+((c[i]-1)*(c[i]-2)//2))

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
#input = sys.stdin.readline
from bisect import bisect_left

N = I()
A = LI()
dic = {}
total_pair = 0
for i in range(N):
    dic[A[i]]  = dic.get(A[i],0) + 1
for value in dic.values():
    total_pair += int(value*(value-1)/2)
for num in A:
    print(total_pair-dic[num]+1)

Submission status: Accepted

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

c = Counter(a)
s = sum([i*(i-1)//2 for i in c.values()])

for k in range(n):
  print(s-c[a[k]] + 1)

Submission status: Accepted

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

nums = [0]*(n+1)
for i in a:
  nums[i] += 1
  
t = 0
for j in nums:
  t += j*(j-1)//2

for k in a:
  print(t+1-nums[k])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    d = na()
    c = collections.Counter(d)

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

    for i in range(n):
        v = c[d[i]]

        print(int(total - v + 1))


if __name__ == '__main__':
    main()