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:
def f(x):
    return x * (x - 1) // 2


n = int(input())
a = list(map(int, input().split()))
counter = [0] * n

for ai in a:
    ai -= 1
    counter[ai] += 1

m = 0
for c in counter:
    m += f(c)

for i, ai in enumerate(a):
    ai -= 1
    c = counter[ai]
    if c == 0:
        print(m)
    else:
        print(m - f(c) + f(c - 1))

Submission status: Accepted

Solution code:
from collections import Counter

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

count_a = Counter(A)
ans_list = [0] * (N+1)
for k, v in count_a.items():
    ans_list[k] = int(v * (v-1) / 2)

all_sum = sum(ans_list)
for i in range(N):
    print(all_sum - count_a[A[i]] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(A)

patterns = 0
for value in counter.values():
    if value >= 2:
        patterns += value * (value - 1) // 2

for a in A:
    ans = patterns
    if counter[a] >= 2:
        ans -= counter[a] * (counter[a] - 1) // 2
    if counter[a] >= 3:
        ans += (counter[a] - 1) * (counter[a] - 2) // 2
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)

# 何も除いていない状態で、全体で可能な組み合わせ
comb = 0

# c[i](C)2のcombination計算
for i in c:
    comb += (c[i]) * (c[i] - 1) // 2

# 同じ数のボールがn個あったら組み合わせはn(n-1)/2
# 一つ取り除いた後の組み合わせは(n-1)(n-2)/2
# この差を計算するとn-1
# 除いたボールを使って可能にしていた組み合わせn-1通りが不可能になる、と直接考えてもOK
for i in A:
    ans = comb - (c[i] - 1)
    print(ans)

Submission status: Accepted

Solution code:
from copy import copy
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
a_counter=Counter(a)
ans=0
for i in a_counter:
    ans+=(a_counter[i])*(a_counter[i]-1)//2
for j in a:
    true_ans=copy(ans)
    true_ans=true_ans-((a_counter[j])*(a_counter[j]-1)//2-(a_counter[j]-1)*(a_counter[j]-2)//2)
    print(true_ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    d = defaultdict(int)
    for e in a:
        d[e] += 1
    sm = 0
    xd = defaultdict(int)
    for k in d:
        if d[k] >= 2:
            xd[k] = (d[k] * (d[k] - 1)) // 2
        sm += xd[k]
    for e in a:
        d[e] -= 1
        sm -= xd[e]
        x = 0
        if d[e] >= 2:
            x = (d[e] * (d[e] - 1)) // 2
        print(sm + x)
        sm += xd[e]
        d[e] += 1


    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""


"""

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def nc2(n):
  if n > 1:
    val = n * (n - 1) // 2
  else:
    val = 0
  return val

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

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

all_sum = 0
for num, count in num_dict.items():
  all_sum += nc2(count)
  
for a in A:
  org_count = num_dict[a]
  print(all_sum - (nc2(org_count) - nc2(org_count - 1)))

Submission status: Accepted

Solution code:

memo = {}

def nc2(n):
  # メモ化不要説
  if n in memo:
    return memo[n]
  if n > 1:
    val = n * (n - 1) // 2
  else:
    val = 0
  memo[n] = val
  return val

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

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

all_sum = 0
for num, count in num_dict.items():
  all_sum += nc2(count)
  
for a in A:
  org_count = num_dict[a]
  print(all_sum - (nc2(org_count) - nc2(org_count - 1)))

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    cnt = Counter(a)
    comb = 0
    for v in cnt.values():
        if v >= 2:
            comb += v * (v - 1) // 2
    for k in range(n):
        if cnt[a[k]] >= 2:
            print(comb - (cnt[a[k]] - 1))
        else:
            print(comb)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

def num_combi2(n):
    combi = n*(n-1)//2
    return combi

def sub_combi2(n):
    c_before = n*(n-1)//2
    c_after =(n-1)*(n-2)//2
    return c_after - c_before

all_combi = 0
for val in Ac.values():
    all_combi += num_combi2(val)

for i in range(N):
    print(all_combi + sub_combi2(Ac[A[i]]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    n = int(input())
    balls = [int(i) for i in input().split()]

    # v - value written on ball
    # b - ball index in [0, n)
    # ct - number of times a value occurs

    # hist[v] = ct
    hist = Counter(balls)
    diff_counts = set(hist.values())
    combos1 = { ct: 0 if ct == 1 else (ct-1) * (ct-2) // 2 for ct in
            diff_counts }
    combos = { ct: ct * (ct-1) // 2 for ct in diff_counts  }

    tot = sum(combos[hist[v2]] for v2 in hist.keys())

    for b in range(n):
        v = balls[b]
        ct = hist[v]
        print(tot - combos[ct] + combos1[ct])

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

deta = [0] * (n+1)

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

ans = [0] * (n+1)

for j in range(n+1):
    if deta[j] > 1:
        ans[j] = deta[j]*(deta[j]-1)/2
    else :
        ans[j] = 0

add = sum(ans)

for k in a:
    print(int(add - deta[k]*(deta[k]-1)/2 + (deta[k]-1)*(deta[k]-2)/2))

Submission status: Accepted

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

c=collections.Counter(b)

n,m=zip(*c.most_common())
n,m=list(n),list(m)
result=0
for i in range(len(m)):
    result+=(m[i]*(m[i]-1))//2

for i in b:
    print(result-c[i]+1)

Submission status: Accepted

Solution code:
from collections import Counter

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


kosu = Counter(A)

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



for a in A:
    print(tempSum - kosu[a]+1)

Submission status: Accepted

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

cnt = Counter(A)

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

for k in A:
    # n*(n-1)//2 - (n-1)*(n-2)//2 = n - 1
    print(ans - (cnt[k] - 1))

Submission status: Accepted

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

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

s = 0

for i in list(c.values()):
    s += i * (i - 1) / 2
    
for i in range(n):
    ca = c[a[i]]
    print(int(s - (ca * (ca - 1) // 2) + ((ca - 1) * (ca - 2) // 2)))

Submission status: Accepted

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

#%%
d = defaultdict(lambda: 0)
for key in A:
    d[key] += 1

#%%
# それぞれのkeyのnC2
cmb = dict()
cmb2 = dict()
for k in d.keys():
    cmb[k] = d[k] * (d[k] - 1) // 2
    cmb2[k] = (d[k] - 1) * (d[k] - 2) // 2

#%%
ans_d = dict()
s = sum(cmb.values())
for k in d.keys():
    ans_d[k] = s - cmb[k] + cmb2[k]
#%%
for a in A:
    print(ans_d[a])

Submission status: Accepted

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


cnt = Counter(A)

ans = 0
for value in cnt.values():
    ans +=int(value*(value-1)/2)

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

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
from collections import*
C=Counter(A)
S=sum(v*(v-1)//2 for v in C.values())
for a in A:
    print(S-C[a]+1)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    C = Counter(A)

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

    ans = [0] * N
    for i, a in enumerate(A):
        ans[i] = total - C[a] + 1

    print('\n'.join(map(str, ans)))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    B = [0] * (N + 1)
    for a in A:
        B[a] += 1

    total = 0
    for b in B:
        total += b * (b - 1) // 2

    ans = [0] * N
    for i, a in enumerate(A):
        ans[i] = total - (B[a] - 1)

    print('\n'.join(map(str, ans)))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int,input().split()))
s = np.zeros(n+1)
for i in a:
    s[i]+=1
al = sum((s*(s-1))/2)
for i in range(n):
    ans=al-(s[a[i]]-1)
    print(int(ans))

Submission status: Accepted

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

count = Counter(A)
vals = count.values()
sum_ = 0
for v in vals:
  if v >= 2: sum_ += v*(v-1)
sum_ //= 2

for k in range(N):
  if count[A[k]] < 2: print(sum_)
  else:print(sum_ + 1 - count[A[k]])

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
base_ans = 0
for i in c.values():
    base_ans += (i)*(i-1)//2
for a in A:
    i = c[a]
    diff_ans = max(0, i-1)
    print(base_ans - diff_ans)

Submission status: Accepted

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

ns = [0] * (n + 1)
for i in a:
    ns[i] += 1
ans = 0
for i in ns:
    if i > 1:
        ans += i * (i - 1) // 2
for i in a:
    print(min(ans - (ns[i] - 1), ans))

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

c = Counter(a)
c2 = [i[1] for i in c.items() if i[1] >= 2]
sum = 0
for i in c2:
    sum += i * (i - 1) // 2

for i in a:
    if c[i] - 1 >= 2:
        tmp = sum - c[i] * (c[i] - 1) // 2
        tmp += (c[i] - 1) * (c[i] - 2) // 2
        print(tmp)
    else:
        tmp = sum - c[i] * (c[i] - 1) // 2
        print(tmp)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A = list(map(int,input().split()))
ans = [0] * N
B = Counter(A)
for i in range(N):
    ans[i] = B[A[i]]-1

total=sum(ans)
C=[]
for i in range(N):
    c = (total-2*ans[i])//2
    C.append(c)

for num in C:
    print(num)

Submission status: Accepted

Solution code:
import collections

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

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

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

Submission status: Accepted

Solution code:
import sys
import collections
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
c = collections.Counter(a)
t = 0
for v in c.values():
    t += v*(v-1)//2

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter

_, *a = map(int, open(0).read().split())
c = Counter(a)
s = sum(i * (i - 1) // 2 for i in c.values())
for i in a:
    print(s - c[i] + 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter

from scipy.special import comb

_, *a = map(int, open(0).read().split())
c = Counter(a)
s = sum(comb(i, 2, exact=True) for i in c.values())
for i in a:
    print(s - c[i] + 1)

Submission status: Accepted

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

c = [0 for i in range(n+1)]
comb = [0 for i in range(n+1)]

for i in range(n):
    c[a[i]] += 1
for i in range(1, n+1):
    if c[i] >= 2:
        comb[i] = int(c[i]*(c[i]-1)/2)

s = sum(comb)
for i in range(n):
    tmp = s - comb[a[i]]
    if c[a[i]]-1 <= 1:
        print(tmp)
    else:
        print(tmp+int((c[a[i]]-1)*(c[a[i]]-2)/2))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from functools import reduce, lru_cache
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline().rstrip())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    N = I()
    A = LI()
    counter = Counter(A)

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

    for ai in A:
        tmp = counter[ai]
        now = tmp * (tmp - 1) // 2
        print(comsum - now + (tmp - 1) * (tmp - 2) // 2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = [int(_) for _ in input().split()]
C = collections.Counter(A)
d = sum([C[i] * (C[i] - 1) // 2 for i in range(N + 1)])
for a in A:
    print(d - C[a] + 1)

Submission status: Accepted

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

n_dic = {}
for a in Alist:
    if a in n_dic.keys():
        n_dic[a] += 1
    else:
        n_dic[a] = 1

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


for k in range(N):
    n = n_dic[Alist[k]] - 1
    print(base - n)

Submission status: Accepted

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

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

al = [0 for i in range(n)]
c = 0
for i in a_2:
    if i > 1:
        al[c] += i * (i-1) / 2
    c += 1

sine = sum(al)
for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sine-(al[f-1]-t*(t-1)/2)))

Submission status: Accepted

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

count = Counter(a)
score = 0

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

for i in a:
    ans = score - (count[i]-1)
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
unique_num, counts =np.unique(A,return_counts=True) #Aから、各要素の出現回数を取得する。

# print(unique_num)
# print(counts)
dic0={} #数字と出現回数の組合わせ辞書
dic1={} #数字と組合わせ個数の組合わせ辞書
dic2={} #回答用の辞書

Combination_pattern = []
for i in range(len(counts)):
    Combination = counts[i] * (counts[i]-1) // 2 #出現回数から、組み合わせを計算する。
    dic0[unique_num[i]]=counts[i]
    dic1[unique_num[i]]=Combination
    dic2.setdefault(unique_num[i])
    Combination_pattern.append(Combination)

totalcombination=sum(Combination_pattern)

# print(dic0)
# print(dic1)
# print(dic2)
# print(Combination_pattern)

'''
以下方針
・Aから数字を取得
・その数字の出現回数を1減らした状態の組み合わせの数を調べる。
・その数字に対応する元の組み合わせの数を減らし、1個前で計算した組合わせを足す。
・同じ数字を引いたときように答えを辞書に登録しておく。
・もし、過去に同じ数字を引いていたら、登録しておいた答えを回答する。
'''

for j in range (N):
    n=A[j] #Aから数字を取得
    if dic2[n]==None:
        reduced_pattern = (dic0[n]-1) * (dic0[n]-2) // 2 #その数字の出現回数を1減らした状態の組み合わせの数を調べる。
        ans=totalcombination-dic1[n]+reduced_pattern #その数字に対応する元の組み合わせの数を減らし、1個前で計算した組合わせを足す。
        dic2[n] = ans #同じ数字を引いたときように答えを辞書に登録しておく。
        print(ans)
    else:
        print(dic2[n]) #もし、過去に同じ数字を引いていたら、登録しておいた答えを回答する。

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)

# print(uni)
# print(cou)
dic0={}
dic1={}
dic={} #回答用

com=[]
for i in range(len(cou)):
    c=cou[i]*(cou[i]-1)//2
    dic0[uni[i]]=cou[i]
    dic1[uni[i]]=c
    com.append(c)
    dic.setdefault(uni[i])

# print(com)
totalcombination=sum(com)

# print(com)
# print(dic1)

'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''

for j in range (N):
    n=A[j] #nはAの数列の数字
    if dic[n]==None:
        e=(dic0[n]-1)*(dic0[n]-2)//2
        t=totalcombination-dic1[n]+e
        dic[n]=t
        print(t)
    else:
        print(dic[n])

Submission status: Accepted

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

d = [0]*n
for i in a:
    d[i-1] += 1

s = 0
for i in range(n):
    s += int(d[i]*(d[i]-1)/2)

for i in range(n):
    print(s-(d[a[i]-1]-1))

Submission status: Accepted

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

nums = [0]*N

for val in A:
    nums[val-1] += 1

choices = 0
for num in nums:
    choices += int(num * (num - 1) / 2)

for val in A:
    print(choices - nums[val - 1] + 1)

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split()))
ls = [0] * (n+1)
for i in an:
    ls[i] += 1
ms = [i*(i-1)/2 for i in ls]
kei = sum(ms)
for j in an:
    j=ls[j]
    ans = kei - j*(j-1)/2 + (j-2)*(j-1)/2
    print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
c=collections.Counter(a)
s=0
for cc in c:
  s+=c[cc]*(c[cc]-1)//2
for aa in a:
  sa=c[aa]*(c[aa]-1)//2-(c[aa]-1)*(c[aa]-2)//2
  print(s-sa)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for key in A:
    d[key] += 1
    #print(d)

Sum = 0
for i in d:
    n = d[i]
    Sum += (n * (n - 1)) // 2
    #print((n * (n - 1)) // 2)
    #print(d[A[i]],i)
    #print(d)

#print(Sum)

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

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(A)

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

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

Submission status: Accepted

Solution code:
# https://drken1215.hatenablog.com/entry/2020/03/23/004100

import collections
import math

N = int(input())
a = list(map(int,input().split()))
a_counter = collections.Counter(a)

res = 0

for v in a_counter: # 全通り
    res += a_counter[v]*(a_counter[v]-1)//2

for v in a:
    befor = a_counter[v]*(a_counter[v]-1)//2
    after = (a_counter[v]-1)*(a_counter[v]-2)//2
    print(res-befor+after)

Submission status: Accepted

Solution code:
import collections
import math

N = int(input())
a = list(map(int,input().split()))
a_counter = collections.Counter(a)

res = 0

for v in a_counter: # 全通り
    res += a_counter[v]*(a_counter[v]-1)//2

for v in a:
    befor = a_counter[v]*(a_counter[v]-1)//2
    after = (a_counter[v]-1)*(a_counter[v]-2)//2
    print(res-befor+after)

Submission status: Accepted

Solution code:
n=int(input())

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

memo = [0]*(2*10**5+1)

for i in t:
    memo[i] += 1

ans = 0

for i in memo:
    ans += i*(i-1)//2

for i in t:
    print(ans-(memo[i]*(memo[i]-1)//2)+((memo[i]-1)*(memo[i]-2)//2))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
m=list(map(int,input().split()))
d=Counter(m)
k=0
for c in d:
    if d[c]>=2:
        k+=d[c]*(d[c]-1)//2
for i in range(n):
    l=d[m[i]]
    if l>=2:
        z=k
        z+=(l-1)*(l-2)//2-l*(l-1)//2
        print(z)
    else:
        print(k)

Submission status: Accepted

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


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

for i in A:
    try:
        combi[i] += 1
    except KeyError:
        combi[i] = 1

total = sum(nC2(i) for i in combi.values())

for i in range(N):
    ans = total - nC2(combi[A[i]])
    ans += nC2(combi[A[i]] - 1)
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
counter_a = Counter(A)
choice_total = sum([(count*(count-1)//2) for count in counter_a.values() if count > 1])

for i in range(0, N):
    sub_num = A[i]
    count_sub_num = counter_a[sub_num]
    print(choice_total - ((count_sub_num*(count_sub_num-1) - (count_sub_num-1)*(count_sub_num-2)) // 2))

Submission status: Accepted

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

  counts = [0] * N

  for v in A:
    counts[v-1] += 1

  total = 0

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

  for v in A:
    n = counts[v-1]

    diff = n * (n - 1) // 2 - (n-1) * (n-2) // 2

    print(total - diff)


if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from collections import Counter

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

A=Counter(a)
B=list(A.keys())
C=list(A.values())

ans=sum(c*(c-1)//2 for c in C)

for k in range(1,N+1):
    print(ans-A[a[k-1]]+1)

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

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

combs = {}
dic = Counter(A)
for k, v in dic.items():
    combs[k] = combinations_count(v)
ans = sum(combs.values())

for i in range(N):
    a = A[i]
    ans_i = ans
    if combs[a] < 2:
        ans_i -= combs[a]
        print(ans_i)
    else:
        ans_i -= dic[a] - 1
        print(ans_i)

Submission status: Accepted

Solution code:
from collections import Counter
def f(n):
    if n<2:
        return 0
    return n*(n-1)//2
N = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
ans = 0
for c in cnt.values():
    ans += f(c)

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

Submission status: Accepted

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

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

m = Counter(A)
s = 0
ls = []
for a, b in m.items():
    s += comb(b)
for a in A:
    if m[a] == 1:
        ls.append(s)
    elif m[a] == 2:
        ls.append(s-1)
    else:
        ls.append(s-comb(m[a])+comb(m[a]-1))
print(*ls, sep='\n')

Submission status: Accepted

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

for num in A:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

total = 0
for value in dic.values():
    total += value * (value-1) / 2   # 使えないボールがない場合のtotal

for num in A:
    print(int(total - dic[num] + 1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
# D - Banned K
from collections import Counter


def main():
    N, *A = map(int, open(0).read().split())
    cnt = Counter(A)
    total = sum(v * (v - 1) // 2 for v in cnt.values())
    res = [total - (cnt[a] - 1) for a in A]
    print("\n".join(map(str, res)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

from collections import defaultdict, Counter

counted_a = Counter(A)
total = 0

for key, v in counted_a.items():
    total += v * (v-1) // 2

ans = []
for a in A:
    v = counted_a[a]
    t = total - v * (v-1) // 2 + (v-1) * (v-2) // 2
    ans.append(t)

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ch=[0]*(n+1)
kai=[0]
for i in a:
  ch[i]+=1
for i in range(n):
  kai.append(kai[i]+i+1)
ans=0
for i in ch:
  if i==0:
    continue
  ans+=kai[i-1]
for i in a:
  print(ans-(kai[ch[i]]-kai[ch[i]-1])+1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[35]:


import collections


# In[31]:


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


# In[36]:


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

for i in A:
    print(numsum - (count[i]-1))


# In[ ]:

Submission status: Accepted

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

count = Counter(a)
numSum = 0
for i in count.values():
  numSum += i*(i-1)//2

for i in a:
  print(numSum - (count[i]-1))

Submission status: Accepted

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

l=[0 for n in range(N)]

for e in A:
    l[e-1]+=1

count=0
for n in range(N):
    if l[n]>=2:
        count+=l[n]*(l[n]-1)//2
            
for n in range(N):
    print(count-l[A[n]-1]+1)

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
from functools import reduce

N = int(input())

line = [int(x) for x in input().split()]
counter = Counter(line)

collect = {key: int(value * (value - 1) / 2)
           for key, value in counter.items()}
min_collect = {key: int((value - 1) * (value - 2) / 2)
               for key, value in counter.items()}

max_value = sum(collect.values())

for i in line:
    print(min_collect[i] + max_value - collect[i])

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial
N = int(input())
A = list(map(int, input().split()))
Acount = Counter(A)
Chose = {}
total = 0
for k, v in Acount.items():
    if Acount[k] >= 2:
        x = int(v*(v-1)/2)
        Chose[k] = x
    else:
        x = 0
        Chose[k] = x
    total = total+x
for i in range(N):
    ans=total-Chose[A[i]]
    if Acount[A[i]]-1>=2:
        ans=ans+(Acount[A[i]]-1)*(Acount[A[i]]-2)/2
    print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
num=[0]*n
ans=0
for i in range(n):
    num[a[i]-1]+=1
def cul(num1):
    if num1>=2:
        return ((num1-1)*num1)//2
    else:
        return 0
for i in range(n):
    ans=ans+cul(num[i])
for i in range(n):
    print(ans-num[a[i]-1]+1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=max(a)
b=[0]
b*=m
for i in range(n):
    b[a[i]-1]+=1
c=[]
for i in range(m):
    if b[i]>=2:
        c.append(int(b[i]*(b[i]-1)/2))
    else:
        c.append(0)
d=sum(c)
for i in range(n):
    if c[a[i]-1]>=1:
        e=c[a[i]-1]-int((b[a[i]-1]-1)*(b[a[i]-1]-2)/2)
        print(d-e)
    else:
        print(d)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
Alist = list(map(int,input().split()))
numcntlist = [0] * N 
numCounter = Counter(Alist)

for i in range(1,N+1):
    numcntlist[i-1] = numCounter[i]

sumcomb = 0
for i in numcntlist:
    sumcomb += int(i*(i-1)/2)

for i in Alist:
    ans = sumcomb -(numCounter[i] - 1)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
dic = {}
allsum = 0
for i in range(n):
  dic.setdefault(lst[i], 0)
  dic[lst[i]] += 1
  if dic[lst[i]] >= 2:
    allsum += (dic[lst[i]] - 1)
for i in range(n):
  a = dic[lst[i]]
  temp = allsum - ((a * (a - 1)) // 2)
  if a - 1 >= 2:
    b = a - 1
    temp += ((b * (b - 1)) // 2)
  print(temp)