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:
from collections import Counter
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

cntA = Counter(a)
all = 0
for i in cntA:
    if cntA[i] > 1:
        all += combinations_count(cntA[i], 2)
for i in range(len(a)):
    if cntA[a[i]] > 1:
        print(all - (cntA[a[i]] -1))
    else:
        print(all)

Submission status: Accepted

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

ls = [0]*N
for i in range(N):
    ls[A[i]-1] += 1
s = 0
for i in range(N):
    s += ls[i]*(ls[i]-1)//2
ans = [0]*N
for i in range(N):
    ans[i] = s-ls[A[i]-1] + 1

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

Submission status: Accepted

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

ball = [0 for _ in range(N)]

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

all = 0
for i in range(N):
    all += ball[i]*(ball[i] - 1)//2


#print("ball", ball)
for k in range(N):
    ans = all
    ball_new = ball[A[k] - 1] - 1
    #ball_new[A[k] - 1] -= 1

    ans -= ball[A[k] - 1]*(ball[A[k] - 1] - 1)//2
    ans += ball_new*(ball_new - 1)//2

    print(ans)

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
total = 0
for i in range(N):
  s = count[i]
  total += s*(s-1)/2
for i in range(N):
  j = count[A[i]-1]
  ans = total-j*(j-1)/2+(j-1)*(j-2)/2
  print(int(ans))

Submission status: Accepted

Solution code:
import math

def combinations_count(n):
    return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))

n = int(input())
a = list(map(int,input().split()))
n_list = [0 for i in range(n)]
len_list = [0 for i in range(n)]

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

sum_len = 0

for i in range(n):
    if n_list[i] == 0:
        len_list[i] == 0
    elif n_list[i] == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(n_list[i])
    sum_len += len_list[i]

for i in range(n):
    if len_list[a[i]-1] == 0:
        print(sum_len)
    elif len_list[a[i]-1] == 1 or len_list[a[i]-1] == 2:
        print(sum_len - 1)
    else:
        print(sum_len - (n_list[a[i]-1] - 1))

Submission status: Accepted

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

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

norm = []
ans = []

for n in num:
  norm.append(n*(n-1)//2)
nsum = sum(norm)
for i in range(N):
  if num[i]==0:
    ans.append(-1)
  else:
    ans.append(nsum-norm[i]+(norm[i]*(num[i]-2))//num[i])
for a in A:
  print(ans[a-1])

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
al = 0
part = {}
for i in range(N):
    if nums[i] in part:
        al += part[nums[i]]
        part[nums[i]] += 1
    else:
        part[nums[i]] = 1

for i in range(N):
    print(al - part[nums[i]] + 1)

Submission status: Accepted

Solution code:



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

# counter
from collections import Counter
cntr = Counter(lst)

# combination
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

total = 0
# countSameNames
for value in cntr.values():
    if value > 1:
        total += combinations_count( value, 2 )


for l in lst:
    print(total - (cntr[l]-1))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
C = Counter(A)
tot = 0
for key in C.keys():
    tot += C[key] * (C[key] - 1) // 2
[print(tot - (C[a] - 1)) for a in A]

Submission status: Accepted

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

from collections import Counter
counter=Counter(a)

# すべての組み合わせを事前に計算しておく
combi_sum=0
for num,cnt in counter.items():
    combi_sum+=cnt*(cnt-1)//2

for i in range(n):
    print(combi_sum - counter[a[i]] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
参考
https://kakedashi-engineer.appspot.com/2020/03/23/abc159d/
https://qiita.com/c-yan/items/83ae14b2fbf117355586
kを抜かない場合の方法の数を出し、そこからkを選ぶ場合の数を引く
'''
import collections

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

# ボールに書かれた整数と、その個数の辞書を作成
D = collections.Counter(A)
L = {}
total = 0

# 辞書の整数=k、個数=vごとに
# その整数の個数の辞書=Lと
# 組み合わせ総数=totalを出す
for k, v in D.most_common():
    L[k] = v
    total += v * (v-1) // 2

for a in A:
    print(total - (L[a]-1))

Submission status: Accepted

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

time={}
zentai=0
for i in range(n):
  if a[i] in time:
    zentai+=time[a[i]]
    time[a[i]]+=1
  else:
    time[a[i]]=1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# import sys
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()
# sys.setrecursionlimit(100000)
# INF = 1001001001001
# MOD = 1000000007

def main():

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

    count = [0 for _ in range(N + 1)]
    comb = [0 for _ in range(N + 1)]

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

    for i in range(1, N + 1):
        comb[i] = count[i] * (count[i] - 1) * 0.5

    all_comb = sum(comb)
    # print(count)
    # print(comb)
    # print(all_comb)

    for i in range(1, N + 1):
        num = A[i - 1]
        ans = all_comb - comb[num] + (count[num] - 1) * (count[num] - 2) * 0.5
        print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int,input().split()))
mx = max(a)
b=[0] * 2000000
ans = 0
for i in range(len(a)):
    b[a[i]-1] += 1

for i in range(mx):
    if(b[i] == 0 or b[i] == 1):
        ans += 0
    else:
        ans = ans + int(b[i] * (b[i]-1)/2)
ans1 = 0


for i in range(len(a)):
    ans1 = ans
    if(b[a[i]-1] >2):
        ans1 = ans1 -int((b[a[i]-1]*(b[a[i]-1]-1))/2) + int(((b[a[i]-1]-1) * (b[a[i]-1]-2))/2)
        print(int(ans1))
    elif(b[a[i]-1] == 2):
        print(int(ans1-1))
    elif(b[a[i]-1] < 2):
        print(int(ans1))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import Counter 
def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    d = Counter(A)
    pans = 0
    for v in d.values():
        pans += v*(v-1)//2
    for k in A:
        tmp = pans
        v = d[k]
        tmp -= v*(v-1)//2
        v -= 1
        tmp += v*(v-1)//2 
        print(tmp)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)
B.append(0)
p = B[0]
y = dict()
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        y[p] = u
        u = 0
    u += 1
    p = q
All = 0
for l in y.values():
    All +=l*(l-1)//2
for j in range(N):
    x = A[j]
    print(All+(y[x]-1)*(y[x]-2)//2-y[x]*(y[x]-1)//2)

Submission status: Accepted

Solution code:
from collections import defaultdict
from copy import copy
n = int(input())
a = [int(i) for i in input().split()]
count_a = defaultdict(int)
diff_a = [0] * n
for ai in a:
        count_a[ai] += 1

for i in range(n):
    diff_a[i] = (count_a[a[i]] - 1)

count = 0    
for v in count_a.values():
    count += v*(v - 1) // 2
for i in range(n):
    count_copy = copy(count)
    print(count_copy - diff_a[i])

Submission status: Accepted

Solution code:
import math

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

def combi(n):
    if n == 1: return 0
    return math.factorial(n) // (math.factorial(n - 2) * 2)

a2 = sorted(a)
a2.append(n+1)
dic = {}
n = 0
cnt = 1
total = 0
for i in a2:
    if n != i:
        if cnt > 1:
            dic[str(n)] = cnt
            total += combi(cnt)
        n = i
        cnt = 1
    else:
        cnt += 1

for i in a:
    if str(i) in dic:
        cnt = dic[str(i)]
        print(total - cnt + 1)
    else:
        print(total)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Cs = {}
for A in As:
  if A not in Cs:
    Cs[A] = 0
  Cs[A] += 1
#print(Cs)
r0 = 0
for C in Cs.values():
  r0 += C*(C-1)//2
#print(r0)
for A in As:
  C = Cs[A]
  r = r0-C*(C-1)//2+(C-1)*(C-2)//2
  print(r)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
lia = [0]*n
sum1 = 0
for i in range(n):
    lia[li[i]-1] += 1
for i in range(n):
    if lia[i] >= 2:
        lia[i] -= 1
        sum1 += (lia[i]*(lia[i]+1)//2)
    else:
        if lia[i] == 1:
            lia[i] -= 1
for i in range(n):
    if lia[li[i]-1] >= 1:
        a = lia[li[i]-1]*(lia[li[i]-1]+1)//2
        b = (lia[li[i]-1]-1)*lia[li[i]-1]//2
        print(sum1-a+b)
    else:
        print(sum1)

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 += int(0.5* i * (i-1))
for j in A:
    print(ans-C[j]+1)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product#accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
def run():
    N = int(sysread())
    A = list(map(int, sysread().split()))
    count = [0] * (N+1)
    for val in A:
        count[val] += 1
    base_sum = 0
    for val in count:
        if val <= 1:continue
        base_sum += val * (val-1) // 2
    for i in range(N):
        if count[A[i]] == 1:
            print(base_sum)
        else:
            print(base_sum - count[A[i]] + 1)
if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
N = int(input()) #N: ボールの個数

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

from collections import defaultdict

d = defaultdict(int)
for i in A:
    d[i] += 1
max = sum([d[i]*(d[i]-1)//2 for i in range(1,N+1)])
ans = [max - d[i] + 1 for i in A]

for x in ans:
    print(x)

Submission status: Accepted

Solution code:
from collections import defaultdict
def banned_k(n,a):
  d=defaultdict(int)
  for x in a:
    d[x]+=1
  total = sum([d[i]*(d[i]-1)//2 for i in range(1,n+1)])
  ans = [total - d[x] + 1 for x in a]
  return ans

n = int(input())
a = list(map(int,input().split()))
ans = banned_k(n,a)
for x in ans:
  print(x)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A=list(map(int,input().split()))
B=Counter(A)
tmp=0
for i in B.values():
    tmp+=int(0.5*(i)*(i-1))
    #print(tmp)
for i in A:            
    print(tmp-B[i]+1)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

c = Counter(a)
s = 0

for i in c.values():
    s += combination2(i)

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

for v in d.values():
    ans += v*(v-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()))
ans = 0
c = Counter(A)

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

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

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(la)
lv = list(c.values())
lvmin = [i-1 for i in lv]

s = sum(i*(i-1)//2 for i in lv)

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

Submission status: Accepted

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

d = defaultdict(int)
for i in range(N):
    d[A[i]] += 1
# print(d)
ans = 0
for key in d:
    ans += d[key]*(d[key]-1)//2

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

Submission status: Accepted

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

for i in range(n):
  sum = count
  now = a[i]
  now_c = c[now-1]
  sum -= now_c-1
  print(sum)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc159/tasks/abc159_d

import math

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

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

cnb=[0 for i in range(n)]
sum=0
for i in range(n):
    if count[i]>=2:
        cnb[i]=int(count[i]*(count[i]-1)/2)
        sum+=cnb[i]

#print(count)
#print(cnb)
#print(sum)

for k in range(n):
    if count[a[k]-1]<2:
        print(sum)
    else:
        print(sum-cnb[a[k]-1]+int((count[a[k]-1]-1)*(count[a[k]-1]-2)/2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from scipy.misc import comb

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
a = nl()

count = [0 for _ in range(n+1)]
for i in a:
    count[i] += 1
ans_ele = 0
for i in range(1, n+1):
    #print('#', i, count[i])
    if count[i] >= 2:
        ans_ele += comb(count[i], 2, exact=True)
#print(ans_ele)
for i in range(n):
    if count[a[i]] <= 1:
        print(ans_ele)
    elif count[a[i]] == 2:
        print(ans_ele - 1)
    else:
        print(ans_ele - comb(count[a[i]], 2, exact=True) + comb(count[a[i]]-1, 2, exact=True))

Submission status: Accepted

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

from collections import defaultdict
dict = defaultdict(int)
for a in A:
    dict[a] += 1

ans = 0
for key, value in list(dict.items()):
    ans += value*(value-1) // 2

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
d=dict()
for i in range(n):
    if l[i] not in d:
        d.update({l[i]:1})
    else:
        d[l[i]]+=1
ans=0
for j in d:
   ans+=(d[j]*(d[j]-1))//2
for i in range(n):
    k=l[i]
    tot=ans+1-d[k]
    print(tot)

Submission status: Accepted

Solution code:
from collections import defaultdict
from pprint import pprint
#from scipy.special import comb

n = int(input())

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

a_dict = defaultdict(int)

for i in a_list:
    a_dict[i] += 1

#pprint(a_dict)

#総数から(a_dict[k]-1)の値を引いていく。
#例えば5C2のものが一つ減った場合、4C2になる。この差分は5C2-4C2=10-6=4 
#例えば6C2のものが一つ減った場合、5C2になる。この差分は6C2-5C2=15-10=5 
#例えば7C2のものが一つ減った場合、6C2になる。この差分は7C2-6C2=21-15=6

total = 0

#組み合わせの総数を求める。
for j in a_dict.values():
    total += j*(j-1)/2

for k in a_list:
    print(int(total - (a_dict[k]-1)))

Submission status: Accepted

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

#print(C)
ans = 0
for k,v in C.items():
  ans += v * (v-1) // 2
  
for a in A:
  cnt = C[a]
  if cnt > 0:
    print(ans - cnt*(cnt-1)//2 + (cnt-1)*(cnt-2)//2)
  else:
    print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
d={}
for i in a:
  d[i]=d.get(i,0)+1
ans=sum([i*(i-1)//2 for i in d.values()])
for i in a:
    print(ans+1-d[i])

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    nums = [0] * (N+1)
    for i in A:
        nums[i] += 1
    #nums = [A.count(i) for i in range(N+1)]
    comb = sum((i*(i-1)//2 for i in nums))
    for k in range(N):
        print(comb-(nums[A[k]]-1))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = list(map(int,input().split()))
balls = [[] for _ in range(N+1)]
for i in range(N):
  balls[A[i]].append(i)
  
K = 0  
for i in range(N+1):
  if len(balls[i]) < 2:
    continue
  else:
    u = len(balls[i])
    K += int(u*(u-1)/2)
  
t = 0
q = deque()
for i in range(N):
  s = A[i]
  a = len(balls[s])
  if a >=2:
    d = a-1
  else:
    d = 0
  q.append(K-d)


while q:
  v = q.popleft()
  print(v)

Submission status: Accepted

Solution code:
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools

def solve():
  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 a:
    print(s-(c[i]-1))
  return 0
 
if __name__ == "__main__":
  solve()

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import factorial

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

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

def combination(n, r):
    denominator = 1
    for i in range(n, n - r, -1):
        denominator *= i
    numerator = factorial(r)
    return denominator // numerator

combi = {}
combi2 = {}
for k, v in number.items():
    combi[k] = combination(v, 2)
    combi2[k] = combination(v - 1, 2)
combination_all = sum(combi.values())
# print(f"number:{number} combi:{combi} combi2:{combi2} combination_all:{combination_all}", file=sys.stderr)

for a in A:
    print(combination_all - (combi[a] - combi2[a]))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    from collections import Counter
    c=Counter(l)# ()にカウンターの対象のリストの変数名
    cnt=0
    for i in c.values():
        if i>=2:
            cnt+=i*(i-1)//2
    for j in l:
        x=(c[j]-1)*(c[j]-2)//2-c[j]*(c[j]-1)//2
        print(cnt+x)
resolve()

Submission status: Accepted

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

def combinations_count(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

def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = combinations_count(c,2)
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
Data = list(map(int,input().split()))
dic = {}
for d in Data:
    if not d in dic.keys():
        dic[d] = 0
    dic[d]+=1

allsum = 0
for d in dic.values():
    allsum += d*(d-1)//2
for d in Data:
    n = dic[d]
    ans = allsum - n*(n-1)//2
    if n > 1:
        ans += (n-1)*(n-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)
cnt = 0
for i in c.values():
    cnt += i * (i - 1) // 2
for i in a:
    print(cnt - c[i] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
def comb(n):
    return n*(n-1)//2

N = int(input())
A = list(map(int, input().split()))
cnt = [0]*N
for i in range(N):
    cnt[A[i]-1] += 1

normal_cal_cnt = [0] * N
minus_cal_cnt = [0] * N
for i in range(N):
    normal_cal_cnt[i] = comb(cnt[i])
    minus_cal_cnt[i] = comb(cnt[i]-1)
ans = sum(normal_cal_cnt)
for i in range(N):
    tmp = ans
    tmp -= normal_cal_cnt[A[i]-1]
    tmp += minus_cal_cnt[A[i]-1]
    print(tmp)

Submission status: Accepted

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

a_dict = {}

for i in a:
    if i in a_dict:
        a_dict[i] += 1
    else:
        a_dict[i] = 1

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

tmp = 0
for i in a:
    tmp = a_dict[i] - 1
    print(total_com - tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
counter_comb = list(map(lambda y: int(y*(y-1)/2), counter.values()))
sum_counter = sum(counter_comb)
for i in range(n):
    z = counter[A[i]]
    if z>0:
        print(sum_counter-(z-1))
    else:
        print(sum_counter)

Submission status: Accepted

Solution code:
import collections
n = int(input())
A = list(map(int,input().split()))
B = collections.Counter(A)
C = B.copy()
total = 0
for i in B.values():
    total += i*(i-1)//2
for i in A:
    print(total - B[i]+1)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

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

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

def main():
    N = I()
    A_list = LI()
    A_ctr = collections.Counter(A_list)
    base = 0
    for i in range(1,N+1):
        base += comb(A_ctr[i])
    for i in range(1,N+1):
        i_val = A_ctr[A_list[i-1]]
        before = comb(i_val)
        after = comb(i_val-1)
        print(int(base - before + after))
main()

Submission status: Accepted

Solution code:
from collections import defaultdict


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


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

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

whole = sum(list(map(combi, list(d.values()))))

diff = []
for i in range(N):
    diff.append(combi(d[A[i]]) - combi(d[A[i]] - 1))

for x in diff:
    print(whole - x)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
from math import factorial

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

for i,a in enumerate(A):
  if a in dict:
    dict[a].append(i)
  else:
    dict[a]=[i]

ans = 0    
    
for k,v in dict.items():
  if len(v) >= 2:
    ans += len(v) * (len(v)-1) //2
#print(ans)

  
for i in range(n):
  wnum = A[i]
  l=dict[wnum]
#  print('l',l)
  if 2 < len(dict[wnum]):
#    print(ans)
#    print('x',(len(v)-1) * (len(v)-2) // 2 )
#    print('y',len(v) * (len(v)-1) // 2 )
    print(int(ans -(len(l) * (len(l)-1) //2) + (len(l)-1) * (len(l)-2) //2))
  elif 2 == len(dict[wnum]):
    print(int(ans - 1))
  else:
    print(int(ans))

Submission status: Accepted

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
B=sorted(A)
C=[]
D=[]
E=[]
F=[]
C.append(1)
D.append(B[0])
for i in range(1,N):
    if B[i]==B[i-1]:
        C[-1]+=1
    else:
        C.append(1)
        D.append(B[i])
for i in range(len(C)):
    if C[i]==1:
        E.append(0)
        F.append(0)
    else:
        if C[i]==2:
            E.append(1)
            F.append(0)
        else:
            E.append(int(math.factorial(C[i])/(math.factorial(2)*math.factorial(C[i]-2))))
            F.append(int(math.factorial(C[i]-1)/(math.factorial(2)*math.factorial(C[i]-3))))
e=sum(E)
import bisect
for i in range(N):
    s=bisect.bisect_left(D,A[i])
    print(e-E[s]+F[s])

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
CA = Counter(A)
all_count = 0
for ca in CA.values():
  all_count += ca*(ca-1)//2

for a in A:
  print(all_count - (CA[a]-1))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
def fun(x):
    return x*(x-1)/2
d ={}
for i in range(n):
    if l[i] in d:
        d[l[i]] +=1
    else:
        d[l[i]] =1
total = 0
for i in d:
    total += fun(d[i])

for i in range(n):
    print(int(total-fun(d[l[i]])+fun(d[l[i]]-1)))

Submission status: Accepted

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

count_list = [0]*n

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

for i in count_list:
    tmp_ans += math.floor((i*(i-1))/2)

for i in range(n):
    exclution = a[i]
    diff = math.floor((count_list[a[i]-1]*(count_list[a[i]-1]-1))/2) - math.floor(((count_list[a[i]-1]-1)*(count_list[a[i]-1]-2))/2)
    print(tmp_ans-diff)

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())

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

c = Counter(a)


res = []

for i in range(1, n+1):
    m  = c.get(i, 0)
    res.append(m * (m-1) // 2)

s = sum(res)


for i in range(1, n+1):
    x = a[i-1]
    l = c.get(x, 0)


    print(s - res[x-1] + (l-1) * (l-2) // 2)

Submission status: Accepted

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


def main():
    import sys
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    from collections import Counter as cc
    a_ = cc(a)
    sum_ = sum([nc2(k) for k in a_.values() if k > 1])
    for i in range(n):
        if a_[a[i]] < 2:
            print(sum_)
        else:
            print(sum_ - (a_[a[i]] -1))


if __name__ == '__main__':
    main()

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 sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))

    c_n = []
    for i in range(n + 1):
        c_n.append(i * (i - 1) // 2)

    sorted_a = sorted(a)
    g = {}
    count = 1
    tmp = 0
    for ai in sorted_a:
        if tmp == ai:
            count += 1
        else:
            g[tmp] = count
            count = 1
        tmp = ai
    g[tmp] = count

    ans = 0
    for k in g:
        ans += c_n[g[k]]

    for ai in a:
        print(ans - g[ai] + 1)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()