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

num_list = [0]* (len(a)+1)

for i in range(len(a)):
#    num_list[i] = a.count(i)
    num_list[a[i]] += 1

total = 0

for j in range(len(num_list)):
    total += (num_list[j]* (num_list[j]-1))/2

#print(total)

for k in range(len(a)):
#    print(num_list[a[k]]) 
    print(int(total-(num_list[a[k]])+1))
"""
import math

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

cnt = flaot(0.0)

for i in range(1, len(a)+1):
    b = a[0:i-1]+a[i:len(a)]
    
    cnt = 0.0
    for j in range (1, len(a)):
        cnt += b.count(j)*(b.count(j)-1)/2
    print(int(cnt))
"""

Submission status: Accepted

Solution code:
N=int(input())
m=list(map(int,input().split()))
t=[]
for i in range(N):
  t.append(0)
for i in range(N):
  t[m[i]-1]+=1
total=0
for i in range(N):
  total+=(t[i]*(t[i]-1))//2
for i in range(N):
  if t[m[i]-1]<2:
    print(total)
  else:
    print(total-(t[m[i]-1]-1))

Submission status: Accepted

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

Submission status: Accepted

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

from collections import Counter

count_dic = Counter(a)


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

for k in a:
    print(int(all_count - (count_dic[k] - 1)))

Submission status: Accepted

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

def comb(a):
    return a*(a-1)//2

for i in range(n):
    try:
        l[d[a[i]]]+=1
    except:
        d[a[i]]=ind
        l.append(1)
        ind+=1

for i in range(len(l)):
    total+=comb(l[i])

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

Submission status: Accepted

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

for i in range(len(a)):
  comb_list[i+1] = count_list[i+1]*(count_list[i+1]-1)//2

wa = sum(comb_list)

for i in range(n):
  print(wa-(count_list[a[i]]-1))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
from collections import Counter
count = Counter(a)
allcnt = 0
for i in set(a):
    allcnt+= count[i]*(count[i]-1)/2
for i in range(n):
    print(int(allcnt-count[a[i]]+1))

Submission status: Accepted

Solution code:
def combi(x):
    return x * (x-1) //2

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

#print(a)    #思い出すときに使おう　動きが分かるよ

for i in range(len(a)):
    a[i] -= 1

#print(a)

cnt = [0] * n

for i in range(len(a)):
    cnt[a[i]] += 1

#print(cnt)

total = 0

for i in cnt:
    total += combi(i)

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

Submission status: Accepted

Solution code:
def combi(x):
    return x * (x-1) //2

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

#print(a)    #思い出すときに使おう　動きが分かるよ

for i in range(len(a)):
    a[i] -= 1

#print(a)

cnt = [0] * n

for i in range(len(a)):
    cnt[a[i]] += 1

#print(cnt)

total = 0

for i in cnt:
    total += combi(i)

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

Submission status: Accepted

Solution code:
from collections import Counter
import math

def ncr(n, r):
  if r > n:
    return 0
  return math.factorial(n) // math.factorial(n-r) // math.factorial(r)

input()
As = list(map(int, input().split()))
counter = Counter(As)
res = {}
total = 0
for k, v in counter.items():
  x = ncr(v, 2)
  res[k] = (x, (x * (v-2) // (v)))
  total += x
for a in As:
  v1, v2 = res[a]
  print(total - v1 + v2)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
L = list(map(int, input().split()))
A = Counter(L)
C = {0:0}

for i in range(1, max(A.values())+1):
    C[i] = C[i-1]+(i-1)
ans = 0
for a in A.keys():
    key = A[a]
    ans += C[key]

for l in L:
    key = A[l]-1
    tmp = ans - C[A[l]]
    print(tmp+C[key])

Submission status: Accepted

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

u = collections.Counter(a)

sume = 0
for i in u.values():
    sume += int(i*(i-1)/2)

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

Submission status: Accepted

Solution code:
import collections
import copy
N = int(input())
A = list(map(int,input().split()))
AA = collections.Counter(A)
ans = sum([i*(i-1)//2 for i in AA.values()])

for i in range(N):
    num = ans
    num -= AA[A[i]]-1
    print(num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
s = sum(x * (x - 1) // 2 for x in counter.values())

# (n-1)(n-2)/2 - n(n-1)/2 = 1 - n
print('\n'.join(str(s + 1 - counter[x]) for x in A))

Submission status: Accepted

Solution code:
# import numpy as np


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

    nums_dict = dict()
    for a in As:
        if a in nums_dict:
            nums_dict[a] += 1
        else:
            nums_dict[a] = 1

    combs = dict()
    total = 0
    for key, value in nums_dict.items():
        comb = (value * (value-1)) // 2
        combs[key] = comb
        total += comb

    for a in As:
        v = nums_dict[a]
        if v > 1:
            c = (v-1) * (v-2) // 2
            print(total - combs[a] + c)
        else:
            print(total)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    A = [int(_) for _ in input().split()]
    B = Counter(A)
    cc = 0
    for i in B.keys():
        cc += B[i] * (B[i]-1) // 2
    for a in A:
        print(cc + 1 - B[a])
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
Adic = [0]*N
for i in range(N):
    Adic[A[i]-1] += 1
counter = 0
for i in range(N):
    num = Adic[i]
    counter += num*(num-1)/2
    
for k in range(N):
    num = Adic[A[k]-1]
    plusminus = (num-1)*(num-2)/2-num*(num-1)/2
    print(int(counter+plusminus))

Submission status: Accepted

Solution code:
import math
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
m = len(set(a))
nans = 0
tbl = list(Counter(a).values())
tbl2 = Counter(a)
for i in range(m):
    if tbl[i] >= 2:
        nans += math.factorial(tbl[i])//(math.factorial(tbl[i]-2)*math.factorial(2))
for i in range(n):
    num = a[i]
    numcnt = tbl2[num]
    print(nans-(numcnt-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

s = 0
for k in d:
    s += d[k] * (d[k] - 1) // 2

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
import copy
from operator import mul
from functools import reduce

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


s = [0 for i in range(N)]
for i in range(N):
    s[A[i]-1] += 1
    
s1 = copy.copy(s)
for i in range(N):
    if s[i] == 0:
        s1[i] = 0
    else:
        s1[i] -= 1

amount = 0
for i in range(N):
    a = cmb(s[i],2)
    amount += a

for i in range(N):
    print(amount-s1[A[i]-1])

Submission status: Accepted

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

dic = dict(Counter(a_list))

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

for j in a_list:
    n = dic[j]
    if n == 1:
        print(ret)
    else:
        print(ret - (n * (n - 1) // 2) + ((n - 1) * (n - 2) // 2))

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(a)

cnt_items = list(cnt.items())

cnt_values = [i[1] for i in cnt_items]

sum = sum([j*(j-1)//2 for j in cnt_values])

for k in a:
    p = cnt[k]
    print(sum- p*(p-1)//2 + (p-1)*(p-2)//2)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Combination:
    def __init__(self):
        self.fact = [0 for _ in range(100001)]
        self.init_fact()
    
    def init_fact(self):
        self.fact[0] = 1
        self.fact[1] = 1
        for i in range(2,100001):
            self.fact[i] = self.fact[i-1] * i
            print(self.fact[i])

    def calc(self,n,k):
        return int(self.fact[n] / (self.fact[k] * self.fact[n-k]))

N = int(input())

A_num = [0 for _ in range(N + 1)]
A = []
ans = 0

for i in input().split(" "):
    A_num[int(i)] += 1
    A.append(int(i))

for i in A_num:
    if i < 2:
         continue
    else:
        ans += i * (i - 1) // 2

for i in A:
    x = A_num[i]
    ans_tmp = ans - (x * (x - 1) // 2 )
    if (A_num[i] - 1) < 2:
        print(int(ans_tmp))
        continue
    else:
        ans_tmp += ((x - 1) * (x - 2) // 2 )
        print(int(ans_tmp))
        continue

    print(int(ans))
    A_num[i] += 1

Submission status: Accepted

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

    q = Counter(a)
    a_cn = sum(x * (x - 1) // 2 for x in q.values())

    ans = []
    for x in a:
        ans.append(a_cn - q[x] + 1)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    ctr = Counter(a)
    frm_n = sum(x * (x - 1) // 2 for x in ctr.values())

    ans = []
    for x in a:
        ans.append(frm_n - ctr[x] + 1)

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


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Submission status: Accepted

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

    ctr = Counter(a)
    frm_n = sum(x * (x - 1) // 2 for x in ctr.values())

    ans = []
    for x in a:
        ans.append(frm_n - ctr[x] + 1)

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


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Submission status: Accepted

Solution code:
import math
import collections

n = int(input())
a = list(map(int, input().split()))
num_list = []
ans_total = 0

c = collections.Counter(a)

for d in c:
    if not c[d] == 1:
        ans_total += int(math.factorial(c[d])/(2*math.factorial(c[d]-2)))

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

Submission status: Accepted

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

d=collections.Counter(A)
e=0
f=list(d.values())

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

for j in range(N):
  if d[A[j]]==1:
    print(int(e))
  else:
    print(int(e-d[A[j]]+1))

Submission status: Accepted

Solution code:
from collections import Counter

def myAnswer(N:int,A:list)-> None:
   dic = Counter(A)
   total =0
   for value in dic.values():
      total += value * (value -1) //2
   for a in A:
      print(total - (dic[a]-1))


def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   myAnswer(N,A[:])
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
import collections
def myAnswer(N:int,A:list)-> None:
   dic = collections.Counter(A)
   total =0
   for value in dic.values():
      total += value * (value -1) //2
   for a in A:
      print(total - (dic[a]-1))


def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   myAnswer(N,A[:])
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
lst = [int(x) for x in input().split()]
dic = dict(Counter(lst))

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


for x in lst:
    n = dic[x]
    if n == 1:
        print(ret)
    else:
        print(ret - (n * (n - 1) // 2) + ((n - 1) * (n - 2) // 2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = [int(x) for x in input().split()]
s = 0
c = Counter(a)
# print(c)
for i in c.values():
    s += i*(i-1)// 2
    
# print(s)
for j in range(n):
    t = c[a[j]]
    print(s-t+1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ls=[0 for _ in range(N)]
for i in range(N):
  ls[A[i]-1]+=1
s=0
for i in range(len(ls)):
    m=ls[i]*(ls[i]-1)//2
    s+=m
for j in range(N):
  print(s-(ls[A[j]-1]-1))

Submission status: Accepted

Solution code:
n = int(input())
ai = list(map(int,input().split()))
dic = {}
su = 0
for a in ai:
    dic[a] = 0
for a in ai:
    dic[a] += 1
for i , x in enumerate(dic):
    su += dic[x]*(dic[x]-1)/2
for a in ai:
    print(int(su-dic[a]+1))

Submission status: Accepted

Solution code:
#coding:utf-8

n = int(input())
a = input().split()

list_n = [0 for i in range(n)]
basic = 0

for i in a:
    aa = int(i)
    count = list_n[aa - 1]
    basic += count
    list_n[aa - 1] += 1

ans = 0
for i in range(n):
    ans = 0
    a1 = int(a[i])
    ans = basic - list_n[a1 - 1] + 1
    print("{}".format(int(ans)))

Submission status: Accepted

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

# n(n-1)/2 - (n-1)(n-2)/2 = n-1
dic = {}
su = 0
m = 0
for a in An:
  dic[a] = 0
for a in An:
  dic[a] += 1 
for d,i in enumerate(dic):
  su += dic[i] * (dic[i]-1) / 2
for a in An:
  print(int(su - dic[a] + 1))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
sys.setrecursionlimit(10**8)
INF = float("inf")


def solve(N: int, A: "List[int]"):
    c = Counter(A)
    each = [c[key]*(c[key]-1)//2 for key in c]
    # print(c)
    # print(each)
    tot = sum(each)

    for i in range(N):
        print(tot - (c[A[i]]-1))
    return


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]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int,input().split()))
p = [0 for i in range(N)]
for i in range(len(Alist)):
    p[Alist[i]-1] = p[Alist[i]-1] + 1
q = 0
for i in range(len(p)):
    q = q + p[i]*(p[i]-1)/2
for i in range(N):
    print(int(q-p[Alist[i]-1]+1))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
dic = {}
for i in a:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
point = 0
for j,k in dic.items():
    point += k*(k-1)//2

for i in a:
    j = point
    j -= dic[i]*(dic[i]-1)//2
    j += (dic[i]-1)*(dic[i]-2)//2
    print(j)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N = int(input())
A_list = list(map(int, input().split()))
A_collection=collections.Counter(A_list)
total=0
for i in A_collection.values():
    total+=i*(i-1)//2
for k in A_list:
    print(total-(A_collection[k]-1))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
CA = collections.Counter(A)
S = 0
for i in CA.values():
    S += i*(i-1)//2
for x in A:
    print(S - CA[x] + 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

s = 0
for k in d:
    s += d[k] * (d[k] - 1) // 2

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

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

for i in range(n):
    ai = A[i]
    cnt = d[ai]
    diff = (cnt * (cnt - 1)) // 2 - ((cnt - 1) * (cnt - 2)) // 2
    print(ans - diff)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
n = int(readline())
lst1 = list(map(int,readline().split()))
dic1 = defaultdict(int)

for i in lst1:
    dic1[i] += 1



al = 0
for i in dic1.values():
    if i >= 2:
        al += i*(i-1)//2

for i in lst1:
    res = dic1[i]
    if res >= 2:
        print(al-res*(res-1)//2+(res-1)*(res-2)//2)
    else:
        print(al)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int,input().split()))
    conb = [0]*N
    ans = 0
    for i in A:
        conb[i-1] += 1
    for j in conb:
        if j != 0:
            ans += j*(j-1)//2
    for k in range(N):
        print(ans- conb[A[k]-1]+1)



if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

X = 0
for x, y in A.items():
    X += y * (y-1) // 2

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

Submission status: Accepted

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


def main():
    n = int(input())
    A = list(map(int, input().split()))
    cnt = Counter(A)
    cnt_common = cnt.most_common()
    ans = 0
    for i in range(len(cnt_common)):
        num, val = cnt_common[i]
        ans += val * (val - 1) // 2
    for i in A:
        c = cnt[i]
        print(ans - c * (c - 1) // 2 + (c-1) * (c-2) // 2 )

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
def resolve():
    N=int(input())
    A=tupin()

    from collections import Counter
    c=Counter(A)
    total=0
    
    for t in c.items():
        ag,b=t
        total+=(b*(b-1))//2
    #print(total)
    for m in A:
        a=c[m]
        print(total-((a*(a-1))//2)+((a-1)*(a-2))//2)
#%%submit!
resolve()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
C = Counter(A)
# print(C)
# combinations()
al = 0
for ball, num in C.items():
    al += num * (num - 1) // 2
# print(al)
for a in A:
    ball = a
    num = C[a]
    ans = al - C[a] * (C[a] - 1) // 2 + (C[a] - 1) * (C[a] - 2) // 2
    print(ans)

# for a in A:
#     c[a]

Submission status: Accepted

Solution code:
import collections
N=int(input())
L=list(map(int,input().split()))
S=collections.Counter(L)
K={}
ans=0
for x,y in S.items():
	K[x]=(y*(y-1)//2,(y-1)*(y-2)//2)
	ans+=K[x][0]
for i in range(N):
	print(ans-K[L[i]][0]+K[L[i]][1])

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
c = list(c.items())
num = 0
nc2 = dict()
n_1c2 = dict()
for i in range(len(c)):
    nc2[c[i][0]] = c[i][1] * (c[i][1]-1) // 2
    num += c[i][1] * (c[i][1]-1) // 2
    n_1c2[c[i][0]] = (c[i][1]-1)*(c[i][1]-2) // 2
for i in range(n):
    tar = a[i]
    print(num-nc2[tar]+n_1c2[tar])

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    dict = {}
    for i in range(n):
        if a[i] in dict:
            dict[a[i]] += 1
        else:
            dict[a[i]] = 1
    dval = list(dict.values())
    total = 0
    for i in range(len(dval)):
        total += (dval[i] * (dval[i]-1))/2
    for i in range(n):
        test = a[i]
        print(int(total-dict[test]+1))
resolve()

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    ans = 0
    for v in c.values():
        if v > 1:
            ans += v*(v-1)//2
    for k, a in enumerate(A):
        cnt = 0
        add = 0
        if c[a] > 1:
            cnt = c[a]*(c[a]-1)//2
        if c[a] > 2:
            add = (c[a]-1)*(c[a]-2)//2
        print(ans - cnt + add)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
t = collections.defaultdict(int)
for x in A:
    t[x] += 1
ans = 0
for x in t:
    ans += t[x]*(t[x]-1) // 2
for x in A:
    tmp = ans
    tmp -= (t[x]*(t[x] - 1)) // 2
    print(tmp + ((t[x]-1)*(t[x]-2))//2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right


# https://atcoder.jp/contests/abc159/tasks/abc159_d
# Kを抜かないときは簡単
# その数字のカウントをnとするとnC2通り
# k番目の数字を抜く kが減る→ n-1 C 2通りになる

# カウントn1,n2...とすると sum (n1,n2...) nC2 で
# kのカウントにnkについて通りを引いて新しい通りに更新してやれば良い

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

for a in A:
    n = cnt[a]
    ans = s - (n * (n - 1) // 2) + ((n - 1) * (n - 2) // 2)
    print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    count = {}
    for aa in a:
        if aa in count:
            count[aa] += 1
        else:
            count[aa] = 1
    all_way = 0
    for c in count.values():
        all_way += c * (c - 1) // 2
    for i in range(n):
        if a[i] in count:
            print(all_way - count[a[i]] + 1)
        else:
            print(all_way)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = ((N+1)//2)/N
    print(ans)
    return

def examB():
    N, K = LI()
    H = [I()for _ in range(N)]
    H.sort()
    ans = inf
    for i in range(N-K+1):
        cur = H[i+K-1]-H[i]
        if ans>cur:
            ans = cur
    print(ans)
    return

def examC():
    N = I()
    A = LI()
    ans = [0]*N
    C = Counter(A)
    base = 0
    for c in C.values():
        base += c*(c-1)//2
    #print(C)
    #print(base)
    for i in range(N):
        ans[i] = base - (C[A[i]]-1)
    for v in ans:
        print(v)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
l = [0]*N
ans = 0
for a in A:
    l[a-1] += 1
for j in l:
    ans += int(j*(j-1)/2)
for k in range(N):
    print(ans - l[A[k]-1] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
count=dict(Counter(a))
ans=0
dic={}
for j,k in count.items():
    ans=k*(k-1)/2
    dic[j]=ans
s=sum(dic.values())
for i in range(n):
    v=count[a[i]]
    if v==0:
        print(0)
    else:
        print(int(s-v+1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(sep=" ")))
S = [0 for i in range(0,N+1,1)]
for i in range(0,N,1):
    S[A[i]] += 1
Sum = 0
for i in range(1,N+1,1):
    Sum += S[i]*(S[i]-1)//2
for k in range(0, N ,1):
    print(Sum - S[A[k]] +1)

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int,input().split()))
x = [0] * N
ans = 0
for i in range(N):
    x[nums[i] - 1] += 1
for i in range(N):
    ans += (x[i]*(x[i]-1))/2
ans = int(ans)    
for i in range(N):
    if x[nums[i] - 1] == 0 or x[nums[i] - 1] ==1:
        print(ans)
    else:
        print(ans - x[nums[i]-1] + 1)