Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
from copy import copy, deepcopy
from operator import itemgetter

from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
#dequeを使うときはpython3を使う、pypyはダメ
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds

#mod = 10**9+7
#w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

N = int(input())
#N, K = map(int, input().split())
#L = [int(input()) for i in range(N)]
A = list(map(int, input().split()))
#S = [list(map(int, input().split())) for i in range(N)]

l=[0]*N
ans=0
for a in A:
    ans+=l[a-1]
    l[a-1]+=1


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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
import collections

def combinations_count(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    counter = 0
    a_c = collections.Counter(A)
    for v in a_c.values():
        counter += combinations_count(v,2)

    for a in A:
        print(counter-a_c[a]+1)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

total = 0
for key, val in new_a.items():
    if val > 1:
        total += (val * (val - 1) // 2)


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

Submission status: Accepted

Solution code:
from collections import Counter


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

ans1 = 0
counter = Counter(a)
items = list(counter.items())
counter_dict = dict(items)

for i in range(len(items)):
    c_i = items[i][1]
    ans1 += c_i * (c_i - 1) // 2

for k in range(n):
    ans2 = counter_dict.get(a[k]) - 1
    print(ans1 - ans2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [0]*N
for n in range(N):
  B[A[n]-1] +=1
max_val = 0
for m in range(N):
  max_val+= B[m-1]*(B[m-1]-1)//2
result = [max_val]*N
for n in range(N):
  result[n] -= B[A[n]-1]-1
for n in range(N):
  print(result[n])

Submission status: Accepted

Solution code:
import math

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

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

memo = [0 for i in range(10 ** 6)]
ans = 0
for k in dic:
  tmp  =dic[k]
  if tmp >= 2:
    if memo[tmp] == 0:
      memo[tmp] = tmp * (tmp - 1) // 2
      ans += memo[tmp]
    else:
      ans += memo[tmp]

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  elif tmp == 2:
    print(ans - 1)
  else:
    print(ans - tmp * (tmp - 1) // 2 + (tmp - 1) * (tmp - 2) // 2)

Submission status: Accepted

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

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

sumA = 0

for key, value in d.items():
    ncr1[key] = value * (value-1) // 2
    ncr2[key] = (value-1)*(value-2) // 2
    sumA += ncr1[key]

for i in range(N):
    a = A[i]
    ans = sumA - ncr1[a] + ncr2[a]
    print(ans)

Submission status: Accepted

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

from collections import Counter

cnt = Counter(a)
b = set(a)
c = 0
for i in b:
    c += cnt[i]*(cnt[i]-1)//2

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

Submission status: Accepted

Solution code:
def cmb(n, r):
    if r > n: return 0
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

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

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

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

    return result

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

c = collections.Counter(A)
keys = c.keys()
ans_dict = {}
ans_dict2 = {}
for key in keys:
    ans_dict[key] = cmb(c[key]-1,2)
    ans_dict2[key] = cmb(c[key],2)

SUM  = sum(ans_dict2.values())

# print(ans_dict)
# print(ans_dict2)

for key in A:
    print(SUM-ans_dict2[key]+ans_dict[key])

Submission status: Accepted

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

for a in K:
    e = C[a]
    print(ans-e*(e-1)//2+(e-1)*(e-2)//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    A = read_ints()
    counter = Counter(A)
    total = 0
    for count in counter.values():
        total += count*(count-1)//2
    for a in A:
        if counter[a] > 1:
            print(total-(counter[a]-1))
        else:
            print(total)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

counts = [0] * (N+1)

for i in range(1, N+1):
    counts[A[i-1]] += 1

sum_ = 0
for k in range(1, N+1):
    sum_ += counts[k] * (counts[k]-1) // 2

for k in range(1, N+1):
    # A[k]を抜くことで減る組み合わせを引く
    print(sum_ - (counts[A[k-1]] - 1))

Submission status: Accepted

Solution code:
def main():
	import collections
	s = int(input())
	a = list(map(int,input().split()))
	ac = collections.Counter(a)
	ans = 0
	for i in set(ac):
		at = ac[i]
		ans += (at * (at-1))/2
	for t in a:
		ans_print = ans -((ac[t] * (ac[t] -1))/2) + (((ac[t]-1) * ((ac[t]-1)-1))/2)
		print(int(ans_print))
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# a/b の切り上げ


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


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

    nums = defaultdict(int)
    ans = 0
    for i in range(n):
        nums[a[i]] += 1
    for i in nums:
        ans += nums[i] * (nums[i]-1)//2
    """
    for v in a:
        ans += nums[v]
        nums[v] += 1
    """

    for v in a:
        print(ans - (nums[v]-1))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
A = int(input())
import collections
total = 0
listN = list(map(int,input().split()))
dic = collections.Counter(listN)
listB = list(dic.values())
for i in listB:
    total += i * (i-1) // 2
dic_keys = list(dic.keys())
dic_values = list(dic.values())
for i in range(len(dic_values)):
    B = dic_values[i]
    dic_values[i] = total-(B-1)
dic = {}
for i in range(len(dic_values)):
    dic[dic_keys[i]] = dic_values[i]
for i in listN:
    print(dic[i])

Submission status: Accepted

Solution code:
n = int(input())
L = [int(i) for i in input().split()]
t = set(L)
dn = {}
dc = {}
All = 0
for i in t:
  dn[i] = 0
for i in L:
  dn[i] += 1
for i in t:
  dc[i] = int(dn[i]*(dn[i]-1)/2)
  All += dc[i]
for i in range(n):
  print(All-dc[L[i]] + int((dn[L[i]]-1)*(dn[L[i]]-2)/2))

Submission status: Accepted

Solution code:
M=int(input())
N=list(input().split())
check=[0]*M
for i in range(M):
    check[int(N[i])-1]+=1
num=0
for i in range(M):
    num+=check[i]*(check[i]-1)//2
for i in range(M):
    #if check[int(N[i])-1]!=2:
    print(num-(check[int(N[i])-1]-1))
    #else:
       # print(num-1)

Submission status: Accepted

Solution code:
M=int(input())
N=list(input().split())
check=[0]*M
for i in range(M):
    check[int(N[i])-1]+=1
num=0
for i in range(M):
    num+=check[i]*(check[i]-1)//2
for i in range(M):
    if check[int(N[i])-1]!=2:
        print(num-(check[int(N[i])-1]-1))
    else:
        print(num-1)

Submission status: Accepted

Solution code:
M=int(input()) 
N=list(input().split()) 
check=[0]*M           
for i in range(M):    
    check[int(N[i])-1]+=1   
num=0
for i in range(M):
    num+=check[i]*(check[i]-1)//2
for i in range(M):
    if check[int(N[i])-1]!=2:
        print(num-(check[int(N[i])-1]-1))
    else:
        print(num-1)

Submission status: Accepted

Solution code:
import copy
from scipy.misc import comb
n = int(input())
#x, y, a, b, c = map(int, input().split())
#s= list(input())
a = list(map(int, input().split()))
an = [0] * n
for i in range(n):
    an[a[i]-1] += 1

ac2 = []
ac2_diff = []
for i in range(n):
    ac2.append(comb(an[i], 2, exact=True))
    ac2_diff.append(ac2[i]-comb(an[i]-1, 2, exact=True))
sum = sum(ac2)
for i in range(n):
    print(sum - ac2_diff[a[i]-1])

Submission status: Accepted

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

import collections
c = collections.Counter(A)
a = 0

for i,j in c.items():
    a += j *(j-1) //2
#print('===', a)
for k in range(n):
    nn = c[A[k]]
    ans = a -(nn-1)
    print(ans)

Submission status: Accepted

Solution code:
import sys

#from scipy.misc import comb #scipy.special

#import numpy as np

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

n = int(input())

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

bunpu = [0]*(n+1)

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


sum = 0
for i in bunpu:
    sum+=choose(i)
i = 0
for i in a:
    print(int(sum-choose(bunpu[i])+choose(bunpu[i]-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

ans_all = 0
ans_list = {}
for k, v in d.items():
    if v == 1:
        ans_list[k] = 0
        continue
    if v == 2:
        ans_all += 1
        ans_list[k] = 1
        continue
    ans_all += v*(v-1)/2
    ans_list[k] = v*(v-1)/2 - (v-1)*(v-2)/2

for a in A:
    print(int(ans_all-ans_list[a]))

Submission status: Accepted

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

data = [0]*(n+1)

for aa in a:
  data[aa] += 1

allans = 0
for d in data:
  if d >= 2:
    allans += (d*(d-1))//2
    
for aa in a:
  if data[aa] == 2:
    print(allans - (data[aa]*(data[aa]-1))//2)
  elif data[aa] > 2:
    print(allans - (data[aa]-1))
  else:
    print(allans)

Submission status: Accepted

Solution code:
N = int(input())
src = list(map(int,input().split()))
dic = {}
for n in range(N):
  if dic.get(src[n]) == None:
    dic[src[n]] = 1
  else:
    dic[src[n]] += 1
a = 0
for key, value in dic.items():
  a += value*(value-1)//2
ans = []
for s in src:
  ans.append(a - dic[s]*(dic[s]-1)//2 + (dic[s]-1)*(dic[s]-2)//2)
for b in ans:
  print(b)

Submission status: Accepted

Solution code:
N = int(input())
Alist=list(map(int, input().split()))
numdic = {}
for i in Alist:
    if i in numdic:
        numdic[i] += 1
    else:
        numdic[i] = 1
Apple = 0
for i in numdic.values():
    Apple += i*(i-1)//2
for i in Alist:
    tmp = Apple
    tmp -= (numdic[i]-1)
    print(tmp)

Submission status: Accepted

Solution code:
n=int(input())
li=list(map(int,input().split()))
d={}
dif={-1}
ans={}
for i in range(n):
    if li[i] not in dif:
        dif|={li[i]}
    if d.get(li[i])!=None:
        d[li[i]]+=1
    else:
        d[li[i]]=1
dif.remove(-1)
g=0
for i in dif:
    g+=d[i]*(d[i]-1)//2
for i in dif:
    ans[i]=g-(d[i]-1)*d[i]//2+(d[i]-2)*(d[i]-1)//2
for i in li:
    print(ans[i])

Submission status: Accepted

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

b = []
total = 0

for i in range(n):
    b.append(0)

for i in range(n):
    b[a[i]-1] += 1
for i in range(n):
    if b[i] >= 2:   
        total += b[i]*(b[i]-1)//2



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

Submission status: Accepted

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

l1sum=sum(l1)
for i in range(n):
    if l[a[i]-1]-1>=2:print(l1sum-l1[a[i]-1]+ (l[a[i]-1]-1)*(l[a[i]-1]-2)//2)
    else:print(l1sum-l1[a[i]-1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
ans = {}
for k in cnt.keys():
    ans[k] = (cnt[k] * (cnt[k]-1)) // 2
s = sum(list(ans.values()))
for ai in a:
    if cnt[ai] == 1:
        print(s)
    else:
        print(s - ans[ai] + int((ans[ai] / cnt[ai]) * (cnt[ai] - 2)))

Submission status: Accepted

Solution code:
import collections

def cmb(n, r):
  #MOD = 10**9+7
  if r > n: return 0
  if n - r < r: r = n - r
  if r == 0: return 1
  if r == 1: return n

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

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

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

  return result

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

count = collections.Counter(a)

l1 = [0 for _ in range(n)]
l2 = [0 for _ in range(n)]

for i,v in count.items():
	if v != 1:
		l1[i-1] = cmb(v,2)
		l2[i-1] = cmb(v-1,2)

sum_l1 = sum(l1)

for i in range(n):
	#print(l2[a[i]-1], sum_l1, l1[a[i]-1])
	print(l2[a[i]-1] + sum_l1 - l1[a[i]-1])

Submission status: Accepted

Solution code:
n=int(input())
inp=list(map(int,input().split()))
hoge=[0 for i in range(n)]
huga=[0 for i in range(n)]
for i in inp:
  huga[i-1]+=1
  hoge[i-1]=(huga[i-1]-1)*huga[i-1]
some=sum(hoge)//2
for i in inp:
  if(huga[i-1]==2):
    print(some-1)
  else:
    print(some-int(huga[i-1]-1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = tuple(map(int, input().split()))
A_count = Counter(A)
ans = 0
for i, j in A_count.items():
    ans += j * (j - 1) // 2
for k in range(1, N+1):
    n = A_count[A[k-1]]
    ans_ = ans - (n - 1)
    print(ans_)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(str, input().split()))
nums = {}
for num in a_list:
    if num in nums:
        nums[num][0] += 1
    else:
        nums[num] = [1]
total = 0
for key, value in nums.items():
    subtotal = (value[0] * (value[0] - 1)) // 2
    value.append(subtotal)
    total += subtotal
for i in range(n):
    val = a_list[i]
    subtotal = ((nums[val][0]-1)*(nums[val][0]-2))//2
    res = total - nums[val][1] + subtotal
    print(res)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
a = list(map(int, input().split()))
A = sorted(a)
s = [0]*(n+1)
b = [0]*(n+1)
c = []
for i in range(1, n+1):
    x = a[i-1]
    s[i] = bisect.bisect_right(A, x) - bisect.bisect_left(A, x)
    if b[a[i-1]] == 0:
        c.append(s[i]*(s[i]-1)//2)
        b[a[i-1]] = 1
S = sum(c)
# print(c)
for i in range(1, n+1):
    print(S - s[i] + 1)

Submission status: Accepted

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

a_count=collections.Counter(a)

nctwo=[0,0,1]

def nc2(i):
    if(len(nctwo)>i):
        return nctwo[i]
    else:
        while(len(nctwo)<=i):
            nctwo.append(nctwo[-1] * len(nctwo) / (len(nctwo)-2))
        return nctwo[i]

allsum=0
for v in a_count.values():
    allsum+=nc2(v)

for i in range(n):
    ai_count=a_count[a[i]]
    print(int(allsum-(nc2(ai_count) - nc2(ai_count-1))))

Submission status: Accepted

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

import collections

numcount=collections.Counter(numlist)


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

for i in numlist:
    print(sum - numcount[i] + 1)

Submission status: Accepted

Solution code:
import collections

n = int(input())
list1 = list(map(int,input().split()))
list2 = collections.Counter(list1)

kei1=0

for j in list2.values():
    kei1 += j * (j-1) // 2

for x in list1:
    print(kei1 - (list2[x] -1))

Submission status: Accepted

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

Submission status: Accepted

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

from collections import Counter
cnt_A = Counter(A)

All = 0
cnt = 0
for a in B:
    cnt = cnt_A[a]
    All +=  cnt*(cnt-1)//2

for k in range(N):
    ans_k = 0
    cnt = cnt_A[A[k]]
    ans_k = cnt - 1
    Ans = All - ans_k
    print(Ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

def input():
    return sys.stdin.readline().strip()
def combi(num):
    return num * (num - 1) // 2
def main():
    N = int(input())
    A = list(map(int,input().split()))
    counterdic = Counter(A)
    ma = 0
    for value in counterdic.values():
        ma += combi(value)
    for i in A:
        print(ma-combi(counterdic[i])+combi(counterdic[i]-1))
if __name__ == "__main__":
    main()

Submission status: Accepted

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

    cnts = Counter(As)
    ns = [cnts.get(i,0)*(cnts.get(i,0)-1)/2 for i in range(1,n+1)]
    sumn = sum(ns)
    for i in As:
        k = cnts.get(i,0)
        ct = sumn - k*(k-1)/2 + (k-1)*(k-2)/2
        print(int(ct))
main()

Submission status: Accepted

Solution code:
n = raw_input()
a = map(int, raw_input().split(' '))
import collections
q = 0
cc = collections.Counter(a)
for k in cc:
	q += cc[k] * (cc[k]-1) /2


for e in a:
	print q - cc[e] * (cc[e]-1) /2 + (cc[e]-1)*(cc[e]-2)/2

Submission status: Accepted

Solution code:
n = raw_input()
a = map(int, raw_input().split(' '))
import collections
q = 0
cc = collections.Counter(a)
for k in cc:
	q += cc[k] * (cc[k]-1) /2


for e in a:
	q -= cc[e] * (cc[e]-1) /2
	cc[e]-=1 
	q += cc[e] * (cc[e]-1) /2
	print q
	q -= cc[e] * (cc[e]-1) /2
	cc[e]+=1 
	q += cc[e] * (cc[e]-1) /2

Submission status: Accepted

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

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

for n in range(N):
  sum += list[n]*(list[n]-1)//2
  

for n in range(N):
  if list[n]>0:
  	list2[n]=sum-list[n]*(list[n]-1)//2+(list[n]-1)*(list[n]-2)//2
  
for a in A:
  print(list2[a-1])

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    arr = [int(x) for x in input().split()]
    freq = {}
    for elem in arr:
        if elem not in freq:
            freq[elem] = 0
        freq[elem] += 1
    total = 0
    for elem, f in freq.items():
        total += (f * (f - 1)) / 2
    for elem in arr:
        print(int(total - freq[elem] + 1))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
d=Counter(a)
import math
def cmb(n,r):
    f = math.factorial
    return int(f(n) // f(r) // f(n-r))
def combination(n, r):
    a = 1
    b = 1
    for i in range(r):
        a *= (n - i)
        b *= (i + 1)
    return a // b
e={}
dd=0
for i,j in d.items():
    dd+=combination(j, 2)
    e[i]=combination(j-1, 1)

for i in a:
    print(dd-e[i])

Submission status: Accepted

Solution code:
def resolve():
    import collections

    N = int(input())
    A_list = [int(item) for item in input().split()]
    origin_dict = collections.Counter(A_list)

    twopair_in_N = 0
    for i in origin_dict.values():
        twopair_in_N += i*(i-1)//2

    for j in range(N):
        deff = origin_dict[A_list[j]] -1

        print(twopair_in_N - deff)
    

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

c=collections.Counter(a)
s=0
for i in c.values():
    s+=i*(i-1)//2

for i in a:
    cnt=c[i]
    print(int(s-cnt+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    d = defaultdict(int)
    nums = []
    _ = input()
    nums = list(map(int, input().split()))
    for n in nums:
        d[n] += 1
    
    total_combs = 0
    for k in d.keys():
        total_combs += int((d[k]*(d[k]-1))/2)
    
    for n in nums:
        print(total_combs - (d[n] - 1))
        
 
if __name__ == "__main__":
    main()

Submission status: Accepted

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

    # print(N)
    # print(A)
    d = {i:0 for i in range(1,N+1)}
    d2 = {}
    d3 = {}
    # print(d)
    # return


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

    # print(d)
    sum = 0
    for i in range(1,N+1):
        d2[i] = int(d[i] * (d[i]-1) / 2)
        sum += d2[i]

    # print(d2)

    for i in range(1, N+1):
        if d[i] == 0: d3[i] = 0
        else: d3[i] = int((d[i]-1) * (d[i]-2) / 2)

    # print(d3)


    for i in A:
        print(sum - d2[i] + d3[i])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

sum = [0]*(max(A)+1)
for a in A:sum[a] += 1
total = 0
for s in sum:total += combi(s)
for a in A:
    print(total - combi(sum[a]) + combi(sum[a]-1))

Submission status: Accepted

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

data = [0]*(N+1)
for num in S:
    data[num] += 1

ans = 0
for num in data:
    if num < 2:
        continue
    ans += num*(num-1)//2

for num in S:
    if data[num] < 2:
        print(ans)
    else:
        print(ans-data[num]+1)

Submission status: Accepted

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

K = {}
for row in A:
  if row in K:
    K[row] += 1
  else:
    K[row] = 1
        
# 最大数を計算
max = 0
for i in K:
  max += int(K[i]*(K[i]-1)/2)

# 辞書Kに関して全探索
for i in A:
  print(max - int(K[i]*(K[i]-1)/2) + int((K[i]-1)*(K[i]-2)/2) )

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
c_values = list(c.values())
sum_all = 0
for c_value in c_values:
    sum_all += c_value*(c_value-1)//2
for i in range(n):
    sum = c[a[i]] - 1 
    print(sum_all - sum)

Submission status: Accepted

Solution code:
n=int(input())
a=[0 for i in range(n)]
b=[0 for i in range(n)]
c=[0 for i in range(n)]
r=0
count=0
for i in input().split():
  a[int(i)-1]+=1
  b[count]=int(i)
  count+=1
for i in range(n):
  m=a[b[i]-1]
  c[i]=2*m-2
for i in range(n):
  r+=a[i]*(a[i]-1)
for i in range(n):
  print((r-c[i])//2)

Submission status: Accepted

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

C = {}
for a in A:
    if not(a in C):
        C[a] = 0

    C[a] += 1

s = sum(c*(c-1)//2 for c in C.values())

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

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
sum_A = 0
dict = defaultdict(int)
for i in A:
    dict[i] += 1

for v in dict.values():
    sum_A += v * (v - 1) // 2

for i in A:
    print(sum_A - (dict[i] - 1))

Submission status: Accepted

Solution code:
import collections as cl
import math


def combinations_count(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

cnt = cl.Counter(A)
cnt_l = [0] * 200000
for a in A:
    cnt_l[a] += 1

ans = 0
for v in cnt_l:
    ans += combinations_count(v, 2)

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

Submission status: Accepted

Solution code:
import collections as cl
import math


def combinations_count(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

cnt = cl.Counter(A)
ans = 0
for v in cnt.values():
    ans += combinations_count(v, 2)

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

Submission status: Accepted

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

A_count = collections.Counter(A)

sum = 0
for i in A_count.values():
    sum += i*(i-1) // 2

for i in range(N):
    count = A_count[A[i]]
    ans = sum - count + 1
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

import collections

D = collections.Counter(L)
sum=0
for i in D.values():
	sum+=i*(i-1)//2

for i in range(n):
	m = D[L[i]]
	ans = sum-(m-1)
	print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

S = sum([(i*(i-1))//2 for i in list(A_dic.values()) if i >= 2])

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

Submission status: Accepted

Solution code:
def q4():
    import collections
    N = int(input())
    A = [int(i) for i in input().split()]

    def n_C_2(n):
        """ n個から2個選ぶ組み合わせ総数を計算 """
        ret = 0 if n < 2 else n * (n - 1) / 2
        return ret

    all_sum = 0  # 総数
    counter = collections.Counter(A)
    # all_sumを作成
    for v in counter.values():
        all_sum += n_C_2(v)

    for i in range(N):
        cnt = counter[A[i]]  # i番目の値の出現回数を取得
        ans = all_sum - n_C_2(cnt) + n_C_2(cnt - 1)
        print(int(ans))


if __name__ == '__main__':
    q4()

Submission status: Accepted

Solution code:
import math
from collections import Counter

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

def combinations_count(n, r):
    if n==1:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
c = Counter(Balls)
d={}
ALL_SUM=0
for i,j in c.most_common():
    d[i]=j
    ALL_SUM+=combinations_count(j,2)
for k in Balls:
    print(ALL_SUM-(d[k] - 1))

# for k in range(N):
#     tmp=Balls[k]
#     array_k = Balls
#     sum_k=0
#     c= Counter(array_k)


#     for j in list(c.values()):
#         sum_k += combinations_count(j,2)
    
#     Balls.insert(k,tmp)
#     print(sum_k)

Submission status: Accepted

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

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

max_pattern = 0
for ball in A_ball:
    max_pattern += ball * (ball - 1) /2

for num in A:
    print(int(max_pattern - (A_ball[num-1]-1)))