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
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	A = getlist()
	D = defaultdict(int)
	for i in range(N):
		D[A[i]] += 1

	ans_pre = 0
	for i in D:
		var = D[i]
		ans_pre += int(var * (var - 1) // 2)

	for i in range(N):
		ans = ans_pre - D[A[i]] + 1
		print(ans)


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
ar=list(map(int,input().split()))
x=Counter(ar)
dct={}
y=list(set(ar))
pair=0
#print(x)
for i in y:
    ct=x[i]-1
    pair+=(ct*(ct+1))//2
#print(pair)
for i in range(n):
    el=ar[i]
    ct=x[el]-1
    add=((ct*(ct+1))//2)-(((ct-1)*((ct-1)+1))//2)
    #print("fff",add)
    print(pair-add)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

C = collections.Counter(A)

s_all = 0
for c in C:
    s_all += C[c] * (C[c] - 1) // 2

for a in A:
    s = 0
    s += s_all - (C[a] - 1)
    print(s)

Submission status: Accepted

Solution code:
N = int(input())
K = list(map(int, input().split()))
C = [0]*(N+1)
for k in K:
    C[k] += 1
Sc = 0
for c in C:
    Sc += c*(c-1)//2
for k in K:
    print(Sc-(C[k]-1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
*A, = map(int, input().split())
C = Counter(A)
S = 0
for v in C.values():
    S += v*(v-1)//2

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
N = list(map(int, (stdin.readline().strip().split())))[0]
M = list(map(int, (stdin.readline().strip().split())))
num_list=np.array([0]*(2*10**5+1))
ans=0
for i in M:
    num_list[i-1]+=1
ans=sum(num_list*(num_list-1))//2
for k in range(N):
    num=num_list[M[k]-1]-1
    print(ans-num)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
C = Counter(A)
D=dict()
sum_D =0
for key,val in C.items():
    r = val*(val-1)//2
    D[key] =r
    sum_D+=r

for a in A:
    cnt = C[a]
    gain= (cnt-1)*(cnt-2)//2
    less = D[a]
    print(sum_D - less + gain)

Submission status: Accepted

Solution code:
import collections#モジュールをマスタしよう
N=int(input())
A= list(map(int,input().split()))#この入力は基本
cnt=collections.Counter(A)#インスタンスを作るイメージ
TTL=0
for key in cnt.keys():#辞書なのでkeysで持ってくる
  TTL+=cnt[key]*(cnt[key]-1)//2#事前に全体を計算しておく
for i in range(N):
  ans=TTL
  ans-=cnt[A[i]]*(cnt[A[i]]-1)//2#添え字の使い方がポイント
  ans+=(cnt[A[i]]-1)*(cnt[A[i]]-2)//2
  #その字を消した時、1個少ない数で組合せを作る
  print(ans)

Submission status: Accepted

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

from operator import mul
from functools import reduce

def cmb(n, r):
    r = min(n - r, r)
    if r == 0: return 1
    # reduce(mul, 範囲)で範囲までの階乗計算
    # range (n, n-r, -1) -> n から n-rまで，逆順
    # print(range(n, n - r, -1))
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

def main():
    num = [0]*(((10**5) * 2) + 1)
    c_num = [0]*(((10**5) * 2) + 1)

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

    for i in range(0,len(num)):
        if num[i] >= 2:
            c_num[i] = cmb(num[i],2)

    c_sum = sum(c_num)
    # print(c_sum)

    for i in range(N):

        if num[A[i]] >= 2:
            # print(num[A[i]])
            Def = cmb(num[A[i]],2)
            tmp = 0

            if num[A[i]]-1 >= 2:
                tmp = cmb(num[A[i]]-1,2)

            # print("{} {}".format(Def,tmp))
            print(c_sum-(Def-tmp))

        else:
            print(c_sum)
            # g = 0


    # print(num[1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
counter = {}
for ele in input().split():
    ele = int(ele)
    if not ele in counter:
        counter[ele] = 1
    else:
        counter[ele] += 1
    A.append(ele)

variation_count = {}
base_sum = 0
for key in counter.keys():
    count = counter[key]
    variation_count[key] = count*(count-1)//2
    base_sum += count*(count-1)//2

for k in range(N):
    tmp = base_sum
    count = counter[A[k]] - 1
    tmp = tmp - variation_count[A[k]] + count*(count-1)//2
    print(tmp)

Submission status: Accepted

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

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

B_sum = 0
for i in B:
    B_sum += i * (i-1) //2

for i in range(N):
    print(B_sum-B[A[i]-1]+1)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

for i in range(n):
    tmp = num[a[i]-1]
    ans = cnt - ((tmp-(tmp-2)) * (tmp-1) // 2)
    print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
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
ans=0
for i in dic.values():
  ans+=i*(i-1)/2
for i in range(n):
  bk=dic[a[i]]
  print(int(ans-bk+1 if bk>1 else ans))

Submission status: Accepted

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

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

ans = 0
for i in num_A:
    ans += 0.5 * (i - 1) * i
        
for i in range(N):
    print(int(ans - (num_A[A[i]-1] - 1)))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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


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

c = Counter(A_i)


total = sum(list(map(Get_nC2, c.values())))

for a_i in A_i:
    result =  total - Get_nC2(c[a_i]) + Get_nC2(c[a_i]-1)
    print(result)

Submission status: Accepted

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

count =[0 for i in range(N)]
for i in range(N):
    count[A_n[i]-1] +=1
ans =[count[i]*(count[i]-1)//2 for i in range(N)]
a1=sum(ans)
for i in range(N):
    print(a1-count[A_n[i]-1]+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
dic = {}
com = {}
ans = 0
for v in a:
    try:
        t = dic[v]
        ans += t
        com[v] += t
        dic[v] += 1
    except:
        dic[v] = 1
        com[v] = 0

for k in range(n):
    print(ans - (dic[a[k]]-1))

Submission status: Accepted

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

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

for i in range(N):
    # A[i]の箇所だけ減ることを考える
    print(all_selection-(count_number[A[i]]-1))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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


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

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

base = sum(map(lambda x: nC2(x), cnt.values()))

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a0=[ai for ai in a]
a0.sort()
b=[-1]*(n+1)
base=0
i=0
while i < n:
    t=a0[i]
    c=1
    while i < n-1 and a0[i]==a0[i+1]:
        c+=1
        i+=1
    b[t]=c
    base+=c*(c-1)//2
    i+=1
for i in range(n):
    d=b[a[i]]
    print(base-d+1)

Submission status: Accepted

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

Submission status: Accepted

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

counts=[0 for _ in range(n)]

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

total=0
for i in counts:
  if i>=2:
    total += i*(i-1)//2

for i in a:
  c=counts[i-1]
  if c>=2:
  	minus=c*(c-1)//2 - (c-1)*(c-2)//2
  else:
    minus=0
  print(total-minus)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
B = collections.Counter(A)
L = {key:(B[key] * (B[key] - 1) // 2) for key in B}
total = sum(L.values())
#print(f"A = {A}\nB = {B}\nL = {L}\ntotal = {total}")
[print(total if L[i] == 0 else total - L[i] + ((B[i] - 1) * (B[i] - 2) // 2)) for i in A]

Submission status: Accepted

Solution code:
import collections

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

s = 0
for k, i in B.items():
    s += i*(i-1) // 2
for a in A:
    print(s-(B[a]-1))

Submission status: Accepted

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

from collections import Counter

Num=Counter(A)

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=[0]*n
#a[i]は1以上n以下で、cの要素番号は0~n-1(acは1ずれている。)
all=0
for i in range(n):
  c[a[i]-1]+=1
for i in range(n):
  if c[i]>=2:
    d=c[i]*(c[i]-1)/2
    all+=d
for i in range(n):
  print(int(all-(c[a[i]-1]-1)))

Submission status: Accepted

Solution code:
import numpy as np


def solve(a):
    b = np.bincount(a)
    be = b*(b-1)//2
    ans = np.sum(be)
    for i in range(n):
        print(ans-max((b[a[i]]-1),0))
    return





n = int(input())
a = [int(s) for s in input().split(" ")]
solve(a)

Submission status: Accepted

Solution code:
def Main(N, A):
    ans = [0]*N
    hist = [0]*N
    for i in range(N):
        hist[A[i]-1] += 1
    whole = 0
    for i in range(N):
        whole += hist[i]*(hist[i]-1)//2
    for i in range(N):
        ans[i] = whole - hist[A[i]-1] + 1
    return ans

def main():
    N = int(input())
    A = list(map(int, input().split()))
    out = Main(N, A)
    for e in out:
        print(e)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import collections

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

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

counter = collections.Counter(A)
dic = {c: comb(counter[c], 2) for c in counter.keys()}
sum_dic = sum(dic.values())

for i in A:
    print(sum_dic - (counter[i] - 1))

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

def main():
    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
    ret = 0
    for d in D:
        ret += D[d]*(D[d]-1)//2
    for a in A:
        print(ret - D[a]*(D[a]-1)//2 + (D[a]-1)*(D[a]-2)//2 )


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#coding:utf-8


def nC2(n):
    return n * (n-1) // 2
  
def main():
    n = int(input())
    li = list(map(int, input().split()))
    nums = [0] * n
    for num in li:
        nums[num - 1] += 1
        
    sumv = 0
    for num in nums:
        if num == 0:
            continue
        sumv += nC2(num)
    
    for num in li:
        cnt = nums[num - 1]
        v = sumv + nC2(cnt-1) - nC2(cnt)
        print(v)
        
  
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections


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


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

count = collections.Counter(A)

total = 0
for cnt in count.values():
    total += choose2(cnt)

for a in A:
    ans = total
    c = count[a]
    ans -= choose2(c)
    ans += choose2(c-1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
*l, = map(int, input().split())
c = Counter(l)

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

for i in l:
    print(v-(c[i]*(c[i]-1)//2)+((c[i]-1)*(c[i]-2)//2))

Submission status: Accepted

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

num = [0] * (n + 1)
for i in ball:
    num[i] += 1

data = [0] * (n + 1)
sentaku = [0] * (n + 1)

for i in range(n+1):
        data[i] = num[i] * (num[i] - 1) // 2
        sentaku[i] = (num[i] - 1) * (num[i] - 2) // 2
x = sum(data)

for i in ball:
    print(x - data[i] + sentaku[i])

Submission status: Accepted

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

num = [0] * (n + 1)
data = [0] * (n + 1)

for i in ball:
    num[i] += 1

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

x = sum(data)

for i in ball:
    print(x - num[i] + 1)

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split()))
n_list = [0]*n

for i in range(n):
  n_list[an[i]-1] += 1
n_sum = int(sum([x*(x-1)/2 for x in n_list]))

for j in range(n):
  n_remove = n_list[an[j]-1] - 1
  print(n_sum - n_remove)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)

ans = 0

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

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

Submission status: Accepted

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

count = []
ans = 0
pt = [0 for i in range(n)]
for k, v in c.items():
  count.append([k,v])
  if v > 1:
    ans += v*(v-1)//2
    pt[k-1] = v-1
    
for i in a:
  print(ans - pt[i-1])

Submission status: Accepted

Solution code:
n = input()
n = int(n)
lin = input().split()
# 要素の個数を求める
dic_s = {}
for c in lin:
  dic_s.setdefault(c, 0)
  dic_s[c] += 1
# ペアの総数
tot = 0
for key,value in dic_s.items():
  v = value
  tot += v * (v - 1) / 2
# print(int(tot))
# 指定位置のペアを除く
for c in lin:
  if tot > 0:
    v = dic_s[c]
    print(int(tot-(v-1)))
  else:
    print(0)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

A_l = np.array(list(map(int, input().split())))

num_dict = {}

for num in A_l:
    if num not in num_dict:
        num_dict[num] = 1
    else:
        num_dict[num] += 1

sum_all = 0

for num in num_dict.values():
    sum_all += num*(num-1)/2


for i in range(N):
    ans = sum_all - (num_dict[A_l[i]] - 1)
    print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter


N = int(input())
A = list(map(int, input().split()))
count_A = Counter(A)
sum_A = sum([i*(i-1)//2 for i in count_A.values()])

for k in range(N):
    n = count_A[A[k]]
    ans = sum_A - n + 1
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    N = int(input())
    A = list(map(int, input().split()))
    d = defaultdict(int)
    for a in A:
        d[a] += 1

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

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



if __name__ == "__main__":
    main()

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:
N = int(input())
L = list(map(int, input().split()))

from collections import Counter

c = Counter(L)
d = {}
sum = 0

for i in c:
    k = c[i]
    sum += k * (k-1) //2
for i in range(N):
    print(int(sum - c[L[i]] + 1))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
d = {}
sum = 0
for i in c:
    k = c[i]
    sum += k * (k - 1) / 2
for i in range(n):
    print(int(sum - c[a[i]] + 1))

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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


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

    D = [0]*(N+1)
    for i in range(N):
        D[A[i]] += 1
    
    tmp = 0
    for n in range(1, N+1):
        tmp += cmb(D[n])
    for k in range(N):
        res = tmp - cmb(D[A[k]]) + cmb(D[A[k]]-1)
        print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
ans = 0
c = Counter(a)  # aの要素の数と値の辞書を出してくれる
#print(c)
#print(c.keys())
#print(c.values())
for m in c.values():  # Counterで出した辞書の値（values)のリストの中身でfor
    ans += int((m*(m-1))/2)  # mC2をしている


for k in range(n):
    #print(c[a[k]])
    print(ans - (c[a[k]] - 1))
    """
    c[a[k]]-1というのはｋ番目のボールを隠す前と隠した後についての差。
    隠す前、aのリストにA_kがｍ個あったとする。隠した後、aのリストにA_kはｍ－１個
    なので、mC2 - (m-1)C2 = m-1 である。ここでｍとはc[a[k]]なので、上が成り立つ。
    """

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
lst1=[0]*(n+1)
lst2=[0]*(n+1)
for i in a:
  lst1[i]+=1
for i in range(n+1):
  if lst1[i]>0:
    lst2[i]=lst1[i]-1
lst3=list(map(lambda x:x*(x-1)//2,lst1))
lst4=list(map(lambda x:x*(x-1)//2,lst2))
ans=sum(lst3)
for i in a:
  print(ans-(lst3[i]-lst4[i]))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A)
count = []
ans = 0
pt = [0 for i in range(N)]
for k,v in C.items():
    count.append([k,v])
    if v > 1:
        ans += v*(v-1)//2
        pt[k-1] = v-1
for i in A:
    print(ans - pt[i-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
As = [int(n) for n in input().split()]

vals = {}
for A in As:
    if A in vals:
        vals[A] += 1
    else:
        vals[A] = 1

#print(vals)

choose2 = {}
for val in vals:
    num = vals[val]
    choose2[val] = num*(num - 1) // 2

#print(choose2)

total = 0
for k in choose2:
    total += choose2[k]
#print("total: " + str(total))

for Ak in As:
    print(total - (vals[Ak] - 1))

Submission status: Accepted

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

c = collections.Counter(l)
ans=0
for v in c.values():
  ans+=v*(v-1)//2

for x in l:
  print(ans-c[x]+1)

Submission status: Accepted

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

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
#import numpy as np

def main():
	n=II()
	A=LI()
	B=[0]*(n+1)
	for i in A:
		B[i]+=1

	ans=0
	for i in B:
		ans+=i*(i-1)//2
	#print(ans)
	for i in A:
		print(ans-B[i]+1)









if __name__ == "__main__":
	main()

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
#import numpy as np

from collections import deque

def main():
	n=II()
	A=LI()
	B=[0]*(n+1)
	for i in A:
		B[i]+=1

	ans=0
	for i in B:
		ans+=i*(i-1)//2
	#print(ans)
	for i in A:
		print(ans-B[i]+1)









if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = input().split()
C = Counter(A)
s = sum([comb(j, 2, exact=True) for j in C.values()])
for i in range(N):
    print(s-C[A[i]]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
total = 0
def cmb(n):
  return n*(n-1)//2

X = [0]*(max(A)+1)
for a in A:
  X[a] += 1
#print(X)

for x in X:
  total += cmb(x)
#print(total)

for a in A:
  ans = total
  ans -= cmb(X[a])
  ans += cmb(X[a]-1)
  print(ans)

Submission status: Accepted

Solution code:
import math
from collections import Counter

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

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

c = Counter(A)
ans = 0
for m in c.values():
    if m >= 2:
        ans +=comb2(m)

for i in range(N):
    print(ans - c[A[i]] + 1)

Submission status: Accepted

Solution code:
N = int(input())
arr = [0] + list(map(int,input().split()))
cnt_arr = [0]*(N+1)
for i in arr:
  cnt_arr[i] += 1
def cmb2(n):
  if n<2:
    return 0
  else:
    return n*(n-1)//2
cmb_arr = [0]*(N+1)

_sum = 0
for idx,cnt in enumerate(cnt_arr):
  cmb_arr[idx] = cmb2(cnt)
  _sum += cmb_arr[idx]

for k in range(1,N+1):
  print(_sum - cmb2(cnt_arr[arr[k]]) + cmb2(cnt_arr[arr[k]]-1))

Submission status: Accepted

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

    cnt = dict()
    ans = dict()
    for i in a:
        cnt[i] = cnt.get(i, 0) + 1

    for (idx, v) in cnt.items():
        ans[idx] = v * (v - 1) // 2

    sum_ = sum(ans.values())

    for k in range(len(a)):
        res = sum_ - (cnt[a[k]] - 1)
        print(res)

Submission status: Accepted

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

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))

check_list = []
for i in range(N + 1):
  check_list.append([i, 0, 0])

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

count = 0
for check in check_list:
  if (check[1] < 2):
    continue
  else:
    c = comb(check[1], 2)
    check_list[check[0]][2] = c
    count += c

for k in range(1, N + 1):
  rid = A[k - 1]
  if (check_list[rid][1] >= 1):
    minus = check_list[rid][1] - 1
  else:
    minus = 0
  result = count - minus
  print(result)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

cnt = Counter(A)
ans = 0

for c in cnt.values():
    if c >= 2:
        ans += comb2(c)

for i in range(N):
    print(ans - cnt[A[i]] + 1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
m=max(a)
l=[0]*(m+1)
c=[0]*(m+1)
for i in a:
  l[i]+=1
for i in set(a):
  c[i]=l[i]*(l[i]-1)/2
s=sum(c)
for i in range(n):
  print(int(s-c[a[i]]+(l[a[i]]-1)*(l[a[i]]-2)/2))

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
d = defaultdict(int)
for x in a:
  d[x] += 1
  
s = 0
for k,v in d.items():
  s += v * (v-1) // 2
for x in a:
  c = d[x] - 1
  print(s - c)

Submission status: Accepted

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

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

    dic = {}
    array = input().split()
    for k in array:
        if k in dic:
            dic[k] += 1
        else:
            dic[k] = 1

    ans = 0
    for k in dic:
        ans += nc2(dic[k])

    for a in array:
        print(ans - dic[a] + 1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
A = list(map(int, input().split()))
X = [0]*(n+1)
for a in A:
    X[a] += 1
s = 0
for x in X:
    s += x*(x-1)//2
for a in A:
    print(s-X[a]+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def comb(val):
    if(val > 0):
        return int(val*(val-1) / 2)
    else:
        return 0
    
def make_map(balls):
    balls_map = {}
    for ball in balls:
        if(ball in balls_map):
            balls_map[ball] += 1
        else:
            balls_map[ball] = 1
    return balls_map

def main():
    args = input().rstrip().split(' ')
    N = int(args[0])
    balls = [val for val in input().rstrip().split(' ')]
    balls_map = make_map(balls)
    
    sum_ = 0
    for val in balls_map.values():
        sum_ += comb(val)
    for ball in balls:
        print(sum_ - (balls_map[ball]-1))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
from functools import reduce
def combinations_count2(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

#
N=int(input())
A=list(map(int,input().split()))
#
hoge=Counter(A)
ans=0
for i in hoge.values():
    if i>=2:
        ans += combinations_count2(i, 2)
for i in range(N):
    print(ans-hoge[A[i]]+1)

Submission status: Accepted

Solution code:
# from scipy.misc import comb
# from scipy.special import comb
import math

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

def banned(al):
    d = {}
    for a in al:
        if a in d.keys():
            d[a] += 1
        else:
            d[a] = 1
    # print(d)
    ans = 0
    for k, v in d.items():
        if v > 1:
            ans += comb(v, 2)
            # d[k] = [v, comb(v, 2)]
        # else:
            # d[k] = [v, 0]
    # print(ans)
    for a in al:
        # for k, v in d.items():
        #     if k == a:
        #         if v[0] > 2:
        #             ans += comb(v[0]-1, 2)
        #     else:
        #         ans += v[1]
        print(int(ans-(d[a]-1)))

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

banned(al)

Submission status: Accepted

Solution code:
import collections

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

def combdict(dic, dp):
    ans = 0
    for v in dic.values():
        ans += dp[v]
    return ans

def solve(a, dic, dp):
    allsum = combdict(dic,dp)
    for i in a:
        print(allsum - dp[dic[i]] + dp[dic[i]-1])

def main():
    dp = [comb2(i) for i in range(2 * ( 10 ** 5) + 1)]
    n = int(input())
    a = list(map(int,input().split()))
    d = collections.Counter(a)
    solve(a, d, dp)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

num_dic = {}

for x in A:
    if x in num_dic:
        num_dic[x] += 1
    else:
        num_dic[x] = 1

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

for x in A:
    print(tot - num_dic[x] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
def choose2(n):
    return n*(n-1)/2
sum1=0
sum2=0
N = int(input())
A = [int(N) for N in input().split()]
S = Counter(A)
for i in  S.values():
    sum1+=int(choose2(i))
for j in range(N):
    sum2+=int(sum1-choose2(S[A[j]])+choose2(S[A[j]]-1))
    print(sum2)
    sum2=0