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 bisect,collections,copy,itertools,math,string
import sys
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())
def main():
    n = I()
    a = LI()
    dic = collections.Counter(a)
    als = 0
    exl = [0 for _ in range(n+1)]
    for key, value in dic.items():
        if value >= 3:
            choice = (value)*(value-1)//2
            choice1 = (value-1)*(value-2)//2
            als += choice
            exl[key] = choice1 - choice
        elif value == 2:
            choice = (value)*(value-1)//2
            als += choice
            exl[key] = -choice
    
    for ball in a:
        choices = als + exl[ball]
        print(choices)
main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    from scipy.special import comb

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

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

    ans = 0
    for num in num_lst:
        ans += comb(num, 2, exact=True)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    import time

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

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

    ans = 0
    for num in num_lst:
        # ans += comb(num, 2, exact=True)
        ans += num * (num - 1) // 2

    start = time.time()
    for a in A:
        print(ans - (num_lst[a] - 1))
    end = time.time() - start
    # print(f'{end:6f}', 's')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

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

cnt = [0] * (N + 1)

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

tot = 0
for i in cnt:
    if i > 1:
        tot += nC2(i)

for i in A:
    j = cnt[i]
    if j < 2:
        print(tot)
    else:
        print(tot - (j - 1))

Submission status: Accepted

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

count=[0 for i in range(N)]
for i in A:
    count[i-1]+=1
    
def chose(n):
    return int(n*(n-1)/2)

total=0
for i in count:
    total+=chose(i)

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    import collections
    counter = collections.Counter(A)
    total = 0
    for k, v in counter.items():
        total += v*(v-1)//2
    
    for a in A:
        cnt = counter[a]
        cntm1 = cnt - 1
        print(total - cnt*(cnt-1)//2 + cntm1*(cntm1-1)//2)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

cnt = {}
for i in A:
    if i in cnt:
        cnt[i] += 1
    else:
        cnt[i] = 1
total = 0
for i in cnt:
    if cnt[i]>=2:
        total += cnt[i] * (cnt[i]-1) // 2
        
for i in A:
    if cnt[i] == 1:
        print(total)
    else:
        #print(total - cnt[i]* (cnt[i]-1) // 2 + (cnt[i]-1) * (cnt[i]-2) // 2)
        print(total - (cnt[i]-1))

Submission status: Accepted

Solution code:
def main():
  N=int(input())
  A=[int(a) for a in input().split()]
  cnt={}
  for a in A:
    cnt[a] = cnt.get(a, 0) + 1
  prob=0
  for a in cnt.values():
    prob+=a*(a-1)//2
    
  for a in A:
    print(prob-(cnt[a]-1))
  
main()

Submission status: Accepted

Solution code:
def main():
  N=int(input())
  A=[int(a) for a in input().split()]
  Acnt, Aprob={}, {}
  for a in A:
    Acnt[a] = Acnt.get(a, 0) + 1
  sumA=0
  for k, a in Acnt.items():
    Aprob[k]=a*(a-1)//2
    sumA+=Aprob[k]
  #Acnt=[A.count(n) for n in range(N+1)]
  #Aprob=[Acnt[n]*(Acnt[n]-1)//2 for n in range(N+1)]
  #sumA=sum(Aprob)

  for a in A:
    ans=(Acnt[a]-1)*(Acnt[a]-2)//2
    print(ans+sumA-Aprob[a])
  
main()

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
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()

c = collections.Counter(A)


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

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

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
alst = list(map(int, input().split()))
ans = 0
dd = defaultdict(int)
for a in alst:
    dd[a] += 1
for _, num in dd.items():
    ans += num * (num - 1) // 2
for a in alst:
    num = dd[a]
    ret = ans - num * (num - 1) // 2 + (num - 1) * (num - 2) // 2
    print(ret)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
d=dict([])
ans=0
for a in A:
  if a in d:
    ans+=d[a]
    d[a]+=1
  else:
    d[a]=1
for a in A:
  print(ans-d[a]+1)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int, input().split()))
C=Counter(A)
# print(C)
ans=0
for k in C:
    n=C.get(k)
    ans+=n*(n-1)//2
# print(ans)

for i in range(N):
    k=A[i]
    n=C.get(k)
    print(ans-n+1)

Submission status: Accepted

Solution code:
from collections import Counter

if __name__ == '__main__':

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

    #全体の数
    d = Counter(A)
    
    d2 = dict()
    #この時の組み合わせの合計
    for k,v in d.items():
        d2[k] = (v*(v-1)) // 2
    ans_sum = sum(d2.values())

    #結果表示
    for i in A:
        ans = 0
        if i in d2:
            #あるならば
            tmp = d[i] - 1
            before= d2[i]
            after = (tmp*(tmp-1)) //2
            ans = ans_sum - (before-after)
            print(ans)
        else:
            #ないならば
            print(ans_sum)

Submission status: Accepted

Solution code:
import math

def comb(n):
    if n-2<0:
        return 0
    return math.factorial(n)//(math.factorial(2)*math.factorial(n-2))
    
N=int(input())
A=list(map(int,input().split()))

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

num=0
for i in range(len(nums)):
    num+=comb(nums[i])

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

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
Cn_A = collections.Counter(A)
ans_list = [0]*len(Cn_A)
ans = 0
for i,j in enumerate(Cn_A.values()):
    ans+=j*(j-1)//2
for i in A:
    j = Cn_A[i]
    print(ans-(j*(j-1)//2)+((j-1)*(j-2)//2))

Submission status: Accepted

Solution code:
from collections import Counter
 
def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    a_c = Counter(A)
    res = [int(v*(v-1)/2) for v in a_c.values()]
    cnt = sum(res)
    for a in A:
        ans = (cnt - (a_c[a]-1))
        print(ans)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from scipy.special import comb
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
x = 0

for c in C.values():
    x += comb(c, 2, exact=True)

for i in range(N):
    y = x - comb(C[A[i]], 2, exact=True) + comb(C[A[i]] - 1, 2, exact=True)
    print(y)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    C = Counter(A)

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

    for i in range(N):
        print(X - C[A[i]] + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

N = INT()
A = LIST()

A_c = Counter(A)

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

for k in range(N):
    ans = A_c_c - (A_c[A[k]] - 1)
    print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

C = Counter(A)
S = set(A)

# l = list(combinations())

# for s in S:
#     C[s]
total = 0
for s in S:
    total += C[s] * (C[s] - 1) // 2
# print(total)

for i, a in enumerate(A):
    # before = C[a]
    exc = total - C[a] * (C[a] - 1) // 2
    a_num = C[a] - 1
    now = exc + a_num * (a_num - 1) // 2
    print(now)

Submission status: Accepted

Solution code:
import math
def combination(n,r):
      return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
ls = [0 for i in range(N)]
for i in range(N):
  ls[A[i]-1] += 1
summation = 0
for i in range(N):
  if ls[i] >= 2:
    summation += combination(ls[i],2)
for i in range(N):
  if ls[A[i]-1] >= 2:
    print(summation - (ls[A[i]-1] -1))
  else:
    print(summation)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

N = k()
A = l()
Cnt = [0] * (N+1)
for a in A:
    Cnt[a] += 1
for c in Cnt:
    ans += c * (c-1) // 2
Ans = [ans - Cnt[a] + 1 for a in A]
print("\n".join(map(str, Ans)))

Submission status: Accepted

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

num = {}


# def xC2
def calc(x):
    return x*(x-1)//2


for a in A[1:]:
    if a not in num:
        num[a] = 0
    num[a] += 1

pattern = 0

for n in num:
    pattern += calc(num[n])

for i in range(1, N+1):
    n = num[A[i]]
    ans = pattern - calc(n) + calc(n-1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
from functools import lru_cache
from functools import reduce
from operator import mul
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


@lru_cache(None)
def cmb(n: int, r: int):
    if n < r:
        return 0
    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


def main():
    _ = int(input().strip())
    A = [int(i) for i in input().strip().split()]
    cnts = Counter(A)
    d = {}
    _sum = 0
    for k, v in cnts.items():
        d[k] = (cmb(v, 2), cmb(v - 1, 2))
        _sum += d[k][0]
    for a in A:
        dif = d[a][0] - d[a][1]
        print(_sum - dif)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

input()
a = list(map(int, input().split()))
c = Counter(a)
d = {i: (i * (i - 1)) // 2 for i in set(c.values())}
s = sum(d[i] for i in c.values())
for k in d:
    d[k] = s - d[k] + ((k - 1) * (k - 2) // 2)
d[1] = s
for i in a:
    print(d[c[i]])

Submission status: Accepted

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


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


N = int(input())
A = list(map(int, input().split()))
dp = [0] * (N + 1)
ans = 0
l = Counter(A)
#print(l)
for sa in set(A):
    if l[sa] < 2:
        continue
    dp[sa] = combinations_count(l[sa], 2)
sum_dp = sum(dp)
#print(dp)
for a in A:
    ans = dp[a] * (l[a]-2)
    ans //= l[a]
    ans += sum_dp - dp[a]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
N = int(input())
A = list(map(int, input().split()))
n = {}
s = 0
for i in range(N):
    if A[i] not in n:
        n[A[i]] = 1
    else:
        n[A[i]] += 1
for j in n:
    if n[j] > 1:
        s += factorial(n[j]) // 2 // factorial(n[j]-2)
for i in range(N):
    print(s-n[A[i]]+1)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
c = collections.Counter(A)
x = 0
#print(c)
for i in c.values():
    x += i*(i-1)//2
#print(x)
for i in range(N):
    print(x-c[A[i]]+1)

Submission status: Accepted

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

S={}
for i in range(n):
    a = A[i]
    if a in S :
        S[a]+=1
    else:
        S[a]=1

V=list(S.values())
c = sum([v*(v-1)//2 for v in V])

cdiff=0
K=[]
for i in range(n):
    cdiff=0
    a = A[i]
    s=S[a]
    if s>=2 :
        cdiff=(s*(s-1)-(s-1)*(s-2))//2
    print(c-cdiff)

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_lst = list(map(int,input().split()))
S = Counter(A_lst)
sums = 0
#for i in S.keys():
#    if S[i] > 1:
#        sums += combinations_count(S[i], 2)
for c, i in S.items():
    sums += i*(i-1)//2

for j in range(1, N + 1):
    if S[A_lst[j-1]] == 2:
        print(sums - 1)
    elif S[A_lst[j-1]] > 2:
        print(int(sums - S[A_lst[j-1]] * (S[A_lst[j-1]]-1) * 0.5 + (S[A_lst[j-1]] - 1) * (S[A_lst[j-1]] - 2) * 0.5))
    else:
        print(sums)

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_lst = list(map(int,input().split()))
S = Counter(A_lst)
sums = 0
#for i in S.keys():
#    if S[i] > 1:
#        sums += combinations_count(S[i], 2)

for c, i in S.items():
    sums += i*(i-1)//2

for j in range(1, N + 1):
    if S[A_lst[j-1]] == 2:
        print(sums - 1)
    elif S[A_lst[j-1]] > 2:
        print(int(sums - S[A_lst[j-1]] * (S[A_lst[j-1]]-1) * 0.5 + (S[A_lst[j-1]] - 1) * (S[A_lst[j-1]] - 2) * 0.5))
    else:
        print(sums)

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_lst = list(map(int,input().split()))
S = Counter(A_lst)
sums = 0
for i in S.keys():
    if S[i] > 1:
        sums += combinations_count(S[i], 2)

for j in range(1, N + 1):
    if S[A_lst[j-1]] == 2:
        print(sums - 1)
    elif S[A_lst[j-1]] > 2:
        print(int(sums - S[A_lst[j-1]] * (S[A_lst[j-1]]-1) * 0.5 + (S[A_lst[j-1]] - 1) * (S[A_lst[j-1]] - 2) * 0.5))
    else:
        print(sums)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int,input().split()))
# print('A:',A)
#まず、一気に数えてしまおう！
num = [0]*N
for i in range(N):
    num[A[i]-1]+=1
# print('num:',num)


#その数のペアはいくつ？
C = [0]*N
cnt=0 #１つも抜かさない時の選び出す場合の数

for i in range(N):
    if num[i]!=0 and num[i]!=1:
        dammy = (num[i] * (num[i]-1))//2
        cnt+=dammy
        C[i] = dammy
# print('cnt,C:',cnt,C)


#実際に計算していく
for i in range(N):
    # print('A[i]',A[i])
    print(cnt - C[A[i]-1] + ((num[A[i]-1]-1)*(num[A[i]-1]-2)) //2)
        # print('ue')

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
def solve():
  N = int(input())
  A = list(map(int, input().split()))
  c = Counter(A)
  cnt = 0
  for k,v in c.items():
    cnt += v*(v-1)//2
  ans = [cnt]*N
  for i in range(N):
    ans[i] -= c[A[i]]*(c[A[i]]-1)//2
    ans[i] += (c[A[i]]-1)*(c[A[i]]-2)//2
  
  return ans
print(*solve(),sep='\n')

Submission status: Accepted

Solution code:
import copy
from collections import Counter

N = int(input())
K = list(map(int,input().split()))
"""
N = 4
K = [1, 2 ,3 ,4]
"""
a = list(set(K))
#print(a)
a_count = {}



a_count = Counter(K)
#print(c)

#print(a_count)

ans_dict = 0
for l in a_count.values() :
        
    ans_dict += l * (l-1)  // 2

#print(ans_dict)

"""
for i in a_count :

    a_count_2 = copy.deepcopy(a_count)
    
    a_count_2[i] -= 1

    #print(a_count_2)
    
    ans_dict[i] = 0
    for l in a_count_2.values() :
        
        ans_dict[i] += l * (l-1)  // 2

#print(ans_dict)
"""

for i in K:
    print(ans_dict - (a_count[i]-1))
    pass

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Cnt = [0] * (N+1)
for a in A:
    Cnt[a] += 1
ans0 = 0
for c in Cnt:
    ans0 += c * (c-1) // 2
Ans = [ans0 - Cnt[a] + 1 for a in A]
print("\n".join(map(str, Ans)))

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  if lst[i] not in dic:
    dic[lst[i]] = [1, 0, 0]
  else:
    dic[lst[i]][0] += 1
    dic[lst[i]][1] = dic[lst[i]][0] * (dic[lst[i]][0] - 1) // 2
    dic[lst[i]][2] = (dic[lst[i]][0] - 1) * (dic[lst[i]][0] - 2) // 2
#print(dic)
count = 0
for value in dic.values():
  count += value[1]
for i in range(n):
  m = lst[i]
  count -= dic[m][1]
  count += dic[m][2]
  print(count)
  count += dic[m][1]
  count -= dic[m][2]

Submission status: Accepted

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

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

allc = 0
for a in range(1, N + 1):
    c = dupcount[a]
    if c > 1:
        allc += (c * (c - 1) // 2)

for a in A:
    ans = allc
    c = dupcount[a]
    if c > 0:
        ans -= (c - 1)
    print(ans)

Submission status: Accepted

Solution code:
import math

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

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

ans = 0
for a in dic:
    if dic[a] > 1:
         ans += dic[a] * (dic[a] - 1) // 2

ansdic = {}

for a in dic:
    ansdic[a] = ans
    if dic[a] > 1:
        ansdic[a] = ans - (dic[a] - 1)

for a in A:
    print(ansdic[a])

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)
total = sum([v*(v-1)//2 for v in c.values()])
for i in a:
    print(total-(c[i]-1))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import sys
from collections import Counter
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = [int(x) for x in input().split()]
c = Counter(a)
def c2(n):
    return n * (n - 1) // 2

ans = sum(c2(v) for v in c.values())

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import sys
from collections import Counter
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = [int(x) for x in input().split()]
c = Counter(a)
def c2(n):
    return n * (n - 1) // 2

ans = sum(c2(v) for v in c.values())

def add(x):
    global ans
    ans -= c2(c[x])
    c[x] += 1
    ans += c2(c[x])

def remove(x):
    global ans
    ans -= c2(c[x])
    c[x] -= 1
    ans += c2(c[x])

for x in a:
    remove(x)
    print(ans)
    add(x)

Submission status: Accepted

Solution code:
def combination(n,r):
    n_=1
    r_=1
    for i in range(n,n-r,-1):
        n_*=i
    for i in range(1,r+1):
        r_*=i

    return n_//r_

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

    ball_dic={}

    for a in A:
        if a in ball_dic.keys():
            ball_dic[a]+=1
        else:
            ball_dic[a]=1


    all_patern=0

    combination_dic={}

    for key in ball_dic.keys():
        if ball_dic[key]>=2:
            if (ball_dic[key] in combination_dic.keys()) is False:
                combination_dic[ball_dic[key]] = combination(ball_dic[key], 2)
            all_patern += combination_dic[ball_dic[key]]

    for a in A:
        if ball_dic[a]-1<2:
            if ball_dic[a] in combination_dic.keys():
                res=all_patern-combination_dic[ball_dic[a]]
            else:
                res=all_patern
        else:
            com=combination_dic[ball_dic[a]]
            res=all_patern-com+(com*(ball_dic[a]-2)//ball_dic[a])

        print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [0]+[int(x) for x in input().split()]

kosuu=[0]*(n+10)
kumiawase=[0]*(n+10)


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

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

ans=sum(kumiawase)
for i in range(1,n+1):
    print(int(ans-(kosuu[a[i]]-1)))

Submission status: Accepted

Solution code:
import collections
import math

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

def com(n, r):
    if n > 1:
        ann = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        ann = 0
    return ann

b = collections.Counter(a)
ans = []
for i in b:
    ans.append(com(b[i],2))
anss = sum(ans)

for i in range(n):
    t = b[a[i]]
    print(anss - (t-1))

Submission status: Accepted

Solution code:
def solve():
    import collections
    N = int(input())
    A = [int(i) for i in input().split()]
    counter = collections.Counter(A)
    combis = {}
    total = 0
    for k,v in counter.items():
        combi = v * (v-1) // 2
        combis[k] = combi
        total += combi
    for i in range(N):
        cnt = counter[A[i]]
        if cnt > 1:
            combi = (cnt-1) * (cnt-2) // 2
        else:
            combi = 0
        print(total - combis[A[i]] + combi)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

a_dict = {}
for i in a:
    if a_dict.get(i, False):
        a_dict[i] += 1
    else:
        a_dict[i] = 1

def mC2(m):
    return m * (m - 1) // 2

sum = 0
for v in a_dict.values():
    sum += mC2(v)

for i, k in enumerate(a):
    ans = sum - (a_dict[k] - 1)
    print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
c=collections.Counter(A)
s=0
def f(n):
    return n*(n-1)//2
for i in c:
    c[i]=(f(c[i]),(f(c[i]-1)))
    s+=c[i][0]
for j in A:
    print(s-c[j][0]+c[j][1])

Submission status: Accepted

Solution code:
from collections import Counter

n= int(input())
a=list(map(int,input().split()))
ca = Counter(a)
sa = set(a)
sum_ans=0
for i in sa:
  sum_ans+=(ca[i]*(ca[i]-1))//2

for i in a:
  print(sum_ans-(ca[i]*(ca[i]-1))//2+((ca[i]-1)*(ca[i]-2))//2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
d={}
for b in a:
  if b not in d:
    d[b]=0
  d[b]+=1

ans=0
for c in d:
  ans+=(d[c]*(d[c]-1))//2
  
for k in range(n):
  e=a[k]
  print(ans-(d[e]*(d[e]-1))//2+(d[e]-1)*(d[e]-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter
def comb(a, b):
    if b > a:
        return 0
    tmp1 = 1
    tmp2 = 1
    for i in range(b):
        tmp1 *= (a-i)
        tmp2 *= (b-i)
    return tmp1//tmp2

N = int(input())
A = [int(i) for i in input().split()]
count_dict = Counter(A)
# いったん引かなかった場合の総和を計算しておいて、あとで一回分引いて足すをする
comp_sum = 0
for key in count_dict.keys():
    comp_sum += comb(count_dict[key], 2)

# 引かれるやつとそうじゃないのをそれぞれメモしとく
for i in range(N):
    print(comp_sum + comb(count_dict[A[i]]-1, 2) -  comb(count_dict[A[i]], 2))

Submission status: Accepted

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

import collections
C = collections.Counter(a)

#logging.debug(C)

sum=0
for v in list(C.values()):
	sum+=v*(v-1)//2
 
for k in range(n):
	x = C[a[k]]
	print(sum-(x*(x-1)//2)+((x-1)*(x-2)//2))

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
c={}
for i in set(L):
  c[i]=0
for i in L:
  c[i]+=1
s=sum([(i*(i-1))//2 for i in c.values()])
for i in L:
  print(s-c[i]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
D = {a:0 for a in set(A)}
for a in A:
  D[a] += 1
S = 0
for v in D.values():
  if v>=2:
    S += v*(v-1)//2
for a in A:
  d = D[a]
  if d==1:
    print(S)
  else:
    print(S-d*(d-1)//2+(d-1)*(d-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))        
C = lambda x: x*(x-1)//2
s = 0
D = Counter(A)
for i,j in D.items():
  s += C(j)
for i in A:
  print(s-D[i]+1)

Submission status: Accepted

Solution code:
import collections

N=int(input())
A=list(map(int,input().split()))
B=collections.Counter(A)
C=sum([x*(x-1)//2 for x in B.values()])

for i in A:
  temp=B[i]
  print(C-(temp*(temp-1)//2-(temp-1)*(temp-2)//2))

Submission status: Accepted

Solution code:
from math import factorial
from collections import Counter


def combinations_count(n, r):
    if n >= r:
        return factorial(n) // (factorial(n - r) * factorial(r))
    else:
        return 0

c = Counter()
n = int(input())
A = list(map(int, input().split()))
for i in A:
    c[i] += 1
ans = 0
for k, v in c.items():
    ans += combinations_count(v, 2)

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = defaultdict(int)
for v in a:
  cnt[v] += 1

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

for v in a:
  print(ans + (cnt[v]-1)*(cnt[v]-2)//2 - cnt[v]*(cnt[v]-1)//2)

Submission status: Accepted

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

for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1

cnt = sum([value*(value-1)//2 for value in d.values()])

for i in range(n):
    val = d[a[i]]
    res = cnt - val*(val-1)//2

    if val >= 3:
        res += (val-1)*(val-2)//2

    print(res)

Submission status: Accepted

Solution code:
import math

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

nn = int(input())
aa = list(map(int,input().split()))

ne = [0]*(nn+1)
cn = [0]*(nn+1)
cnminus1 = [0]*(nn+1)
#print(nn,aa)
#print(len(ne))
for i in range(nn):
    ne[aa[i]] += 1

for i in range(nn+1):
 #   print(ne[i])
    if ne[i] == 0:
        cn[i] = 0
        cnminus1[i] = 0
    elif ne[i] == 1:
        cn[i] = 0
        cnminus1[i] = 0
    elif ne[i] == 2:
        cn[i] = 1
        cnminus1[i] = 0
    else:
        cn[i] = combinations_count(ne[i],2)
        cnminus1[i] = combinations_count(ne[i]-1,2)

sum_cn = sum(cn)

for i in range(nn):
    print(sum_cn - cn[aa[i]] + cnminus1[aa[i]])