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:
N = int(input())
A = list(map(int, input().split()))
dictA = {}
for x in A:
    dictA[x] = 0
for x1 in A:
    dictA[x1] += 1

combisum = 0
for y in dictA.values():
    combisum += y * (y - 1) // 2

for z in range(N):
    print(combisum - dictA[A[z]] + 1)

Submission status: Accepted

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


a

import collections
pair_list=collections.Counter(a).most_common()

mass=0
for pair in pair_list:
        mass+=pair[1]*(pair[1]-1)/2

d={str(s[0]): s[1]-1  for s in pair_list}

for  k in range(1,n+1):
    print(int(mass-d[str(a[k-1])]))

Submission status: Accepted

Solution code:
import math
import sys 
import collections
n = int(input())
a = list(map(int,input().split()))
tmp = 0
# comb = [tmp+=i for i in range(1,n+1)]
comb = []
diff = []
su = 0
for i in range(0,n):
    su = su+i
    comb.append(su)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
c = collections.Counter(a)
total = 0
for i in c.values():
    total += comb[i-1]
for i in a:
    print(total - (c[i]-1))

Submission status: Accepted

Solution code:
# import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # queue
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n = int(input())
    a = list(map(int,input().split()))
    mp = {}
    for i in range(n):
        if a[i] in mp.keys():
            mp[a[i]] += 1
        else:
            mp[a[i]] = 1
    res = 0
    b = [0] * (n + 1)
    for k in mp.keys():
       x = mp[k] * (mp[k] - 1) // 2
       res += x
       b[k] = x - (mp[k] - 1) * (mp[k] - 2) // 2
    for i in range(n):
        print(res - b[a[i]])
    return

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
N=int(input())
A=list(map(int,input().split()))

l=[0]*(N+1)
cnt=0

for i in A:
		l[i]+=1
		
for j in l:
		cnt+=(j*(j-1))//2

for k in range(N):
	print(cnt-l[A[k]]+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 20:55:58 2020

@author: naoki
"""

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

aC2 = [0] * (N+1)

for i in range(2,N+1):
    aC2[i] = int(i*(i-1)/2)

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

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

all_count = 0
for i in range(N+1):
    all_count += aC2[count[i]]

for i in range(N):
    print(all_count - aC2[count[A[i]]] + aC2[count[A[i]]-1])

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import groupby

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    
    def cmb(n, r):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)

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

    B = sorted(A)
    d = defaultdict(int)
    gr = groupby(B)
    
    s = 0
    for key, group in gr:
        c = len(list(group))
        d[key] = c
        s += cmb(c, 2)
    
    for a in A:
        ans = s - cmb(d[a], 2) + cmb(d[a]-1, 2)
        print(ans)
    
solve()

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
from functools import lru_cache

N = I()
A = LI()

@lru_cache(maxsize=None)
def ncr(n, r):
    return int(n*(n-1)/2)

tmp = 0
AA = collections.Counter(A)
As = AA.items()
for n, t in As:
    tmp += ncr(t, 2)

for a in A:
    an = AA[a]
    print(tmp - ncr(an, 2) + ncr(an - 1, 2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
m = {}
total = 0
diff = [0]*N
for a in c.items():
    m[a[0]] = a[1]
    total += a[1]*(a[1]-1)//2
for i in range(N):
    com1 = (m[A[i]])*(m[A[i]]-1)//2
    com2 = (m[A[i]]-1)*(m[A[i]]-2)//2
    diff[i] = com1-com2
for i in range(N):
    print(total-diff[i])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
a_list=[0]*(n+1)
for i in a:
  a_list[i]+=1
ans1=[0]*(n+1)
ans2=[0]*(n+1)
for i,m in enumerate(a_list):
  if m>=2:
    ans1[i]=m*(m-1)//2
    ans2[i]=(m-2)*(m-1)//2
all=sum(ans1)
for i in a:
  print(all-ans1[i]+ans2[i])

Submission status: Accepted

Solution code:
import collections
import math

def C(n):
    if n-2 < 0:
        return 0
    return n * (n-1) // 2
N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
#d = {}
d = [-1]*200010
Cf = [C(i) for i in range(200010)]
sm = 0
for k,v in c.most_common():

    d[k] = v
    sm += C(v)

for a in A:
    print (sm - Cf[d[a]] + Cf[d[a]-1])

Submission status: Accepted

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

def main():

    N = int(input())

    a_l = [0] * N

    nums = list(map(int, input().split()))
    for i in nums:
        a_l[i-1] += 1

    total = 0
    for j in a_l:
        if j > 1:
            total += cmb(j)

    for k in nums:
        if a_l[k-1] > 1:
            print(total - cmb(a_l[k-1]) + cmb(a_l[k-1] - 1))
        else:
            print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())

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

H=list()
for _ in range(N+1):
    H.append(0)
for i in range(1,N+1):
    H[L[i]]+=1
#print(H)

def culc(x):
    return x*(x-1)//2

A=0
for j in range(1,N+1):
    A+=culc(H[j])
#print(A)


for k in range(1,N+1):
    number=L[k]
    print(A-(H[number]-1))

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in a:
    x = c[i]
    print(sum-x+1)

Submission status: Accepted

Solution code:
N = int(input())
ARR = [int(s) for s in input().split(" ")]
# N = 8
# ARR = [1, 2, 1, 4, 2, 1, 4, 1]

def calculate(n,arr):

    dict = {}
    brr = {}
    for ar in arr:

        if dict.get(ar) == None:
            dict.__setitem__(ar,1)
        else:
            tmp = dict.get(ar)
            tmp = tmp + 1
            brr.__setitem__(ar,(tmp * (tmp - 1)) // 2 )
            dict.__setitem__(ar,tmp)

    sumValue = sum(brr.values())

    for ar in arr:
        tmp = dict.get(ar)

        print(sumValue - (tmp - 1))

calculate(N,ARR)

Submission status: Accepted

Solution code:
import collections
import numpy as np

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

cnt = collections.Counter(A)

B = [cnt[a] for a in A]

def cmb(n):
    return n * (n - 1) // 2
    
ans = int(sum([cmb(k) for k in cnt.values()]))

for b in B:
    print(ans - cmb(b) + cmb(b - 1))

Submission status: Accepted

Solution code:
from collections import defaultdict
import numpy as np

N = int(input())
A = np.fromstring(input(), np.int, sep=" ")

uniq_count = defaultdict(int)
for a in A:
    uniq_count[a] += 1
uniq = np.fromiter((uniq_count.keys()), np.int)
counts = np.fromiter(uniq_count.values(), np.int)

p = lambda n: n * (n - 1) // 2

n = p(counts)
m = p(counts - 1)

ans = n.sum() - n + m

mapping = dict(zip(uniq, ans))

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

Submission status: Accepted

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

d = collections.Counter(A)
su = 0
for val in d.values():
    su += int(val*(val-1)//2)
for i in range(N):
    val = d[A[i]]
    print(su - int(val*(val-1)//2) + int((val-1)*(val-2)//2))

Submission status: Accepted

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

sortedList = sorted(myList)

myDict = {sortedList[0]:1}
for myIndx1 in range(1,N):
    if sortedList[myIndx1] == sortedList[myIndx1-1]:
        myDict[sortedList[myIndx1-1]] = myDict[sortedList[myIndx1-1]] + 1
    else:
        myDict[sortedList[myIndx1]] = 1

total = 0
for myVal in myDict.values():
    total = total + int((myVal*(myVal-1))/2)

for item in myList:
    print(total - (myDict[item]-1))

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
A__=list(set(A))
A_=Counter(A)
S=0
for a in A__:
    k=a
    v=A_[a]
    if v<2:
        continue
    a=v*(v-1)//2
    S+=a

for a in A:
    x=A_[a]
    y=x*(x-1)//2
    S-=y
    z=(x-1)*(x-2)//2
    print(S+z)
    S+=y

Submission status: Accepted

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

d = {}
for a in As:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1

# 全ての組み合わせの総数をcntに入れる
cnt = 0
for i in d.values():
    cnt += (i * (i-1)) // 2

# cntから自分を抜いて組み合わせの総数をprintする
for a in As:
    print(cnt-(d[a]-1))

Submission status: Accepted

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

def cmb(n,r):
    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

N = int(input())
items = list(map(int, input().split()))
counts = Counter(items)
cobinations = {}
count_sum = 0

for key,value in counts.items():
  if value not in cobinations:
    if value > 1:
      count = cmb(value, 2)
      cobinations[value] = count
    else:
      cobinations[value] = 0
  count_sum+=cobinations[value]

for key in items:
  value = counts[key]
  if value - 1 not in cobinations:
    if value - 1 > 1:
      # tmp_key = list(range(value-1))
      # count = len(list(combinations(tmp_key, 2)))
      # cobinations[value-1] = count
      count = cmb(value-1, 2)
      cobinations[value-1] = count
    else:
      cobinations[value-1] = 0
  
  before = cobinations[value]
  after = cobinations[value-1]
  print(count_sum - before + after)

Submission status: Accepted

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

c = collections.Counter(As)

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

sum_ = int(sum_)

for i in range(N):
  print(sum_-(c[As[i]]-1))

Submission status: Accepted

Solution code:
def nCr(n, r):
    import math
    # nCrのr>nは組み合わせが存在しないので0を返す
    # raiseすべきのこともあるかも
    if r > n:
        return 0
    return math.factorial(n) // ((math.factorial(n - r) * math.factorial(r)))

memo = [-1] * 300000
memo2 = [-1] * 300000
memo3 = [-1] * 300000

n = int(input())
dat = list(map(int, input().split()))
from collections import Counter
c = Counter(dat)
total = 0
#print(c)
for k in c:
    if c[k] > 1:
        memo[k] = nCr(c[k], 2)
        memo2[k] = nCr(c[k]-1, 2)
        memo3[k] = memo[k] - memo2[k]
        total += memo[k]
#print(memo[:10])
#print(memo2[:10])
#print(memo3[:10])
#print(total)
for i in range(n):
    if memo3[dat[i]] == -1:
        print(total)
    else:
        print(total - memo3[dat[i]])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    dica = {}
    for i in range(n):
        if a[i] in dica:
            dica[a[i]] += 1
        else:
            dica[a[i]] = 1
    count = list(dica.values())
    total = list(map(lambda x: x*(x-1)/2, count))
    sumtotal = sum(total)

    for i in range(n):
        test = a[i]
        valuetest = dica[test]
        subtract = valuetest*(valuetest-1)/2
        add = (valuetest-1)*(valuetest-2)/2
        print(int((sumtotal-subtract+add)))
resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = [0 for _ in range(n+1)]
r = []
for i in range(n):
    c[a[i-1]-1] += 1
for i in range(n):
    r.append(c[i])
kai = []
v = []
for i in range(len(r)):
    kai.append((r[i]*(r[i]-1)//2))
    v.append(kai[i] - max(((r[i]-1)*(r[i]-2)//2),0))
#print(v,kai)
ans = sum(kai)
for i in range(len(a)):
    print(ans - v[a[i]-1])

Submission status: Accepted

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

value_count = {}
for item in a:
	value_count[item] = value_count.get(item, 0) + 1

all_pairs = 0
for value in value_count:
	all_pairs += (value_count[value] * (value_count[value] -1)) // 2

for i in range(n):
	value = a[i]
	used_to =  (value_count[value] * (value_count[value] -1)) // 2
	now =  ((value_count[value] - 2) * (value_count[value] -1)) // 2
	print(all_pairs - used_to + now)

Submission status: Accepted

Solution code:
import collections
import math

def combinations_count2(n, memo):
    if n <= 1:
        return 0
    if n in memo.keys():
        return memo[n]
    else:
        memo[n]= math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
        return memo[n]


n = input()
li = input().split()
c = collections.Counter(li)
memo = dict()
Sum = 0
for cv in c.values():
    Sum += combinations_count2(cv,memo)
for l in li:
    print(Sum - c[l] +1)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
countA = Counter(A)
ansA = [v*(v-1)//2 for k,v in countA.items()]
sumA = sum(ansA)

for i in range(N):
  t =  countA[A[i]]
  ans = sumA - t*(t-1)//2 + (t-1)*(t-2)//2
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
hoge = input().split()

m = {}
for i in range(len(hoge)):
    if not(hoge[i] in m):
        m[hoge[i]] = 0
    m[hoge[i]] += 1

al = 0
for key in m:
    al += ((m[key] * (m[key] - 1)) / 2)

for i in range(len(hoge)):
    num1 = m[hoge[i]]
    if num1 < 2:
        print(int(al))
    else:
        num2 = (num1 * (num1 -1)/2) - ((num1 - 1) * (num1 -2)/2)
        print(int(al - num2))

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    d = dict()
    for i in A:
        d[i] = 1 + d.get(i, 0)

    d_sel = dict()
    total = 0
    for k,v in d.items():
        sel = v * (v-1) // 2
        d_sel[k] = sel
        total += sel

    for i in range(N):
        print(total - d_sel[A[i]] + (d[A[i]]-1) * (d[A[i]]-2)//2)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

count = defaultdict(int)
for i in balls:
  count[i] += 1

def cmb(M):
  return M*(M-1)//2

sum = 0
for i in count:
   sum += cmb(count[i])
#sum = defaultdict(int)
#for i in count:
#  for j in count:
#    if i==j:
#      sum[i] += cmb(count[j]-1)
#    else:
#      sum[i] += cmb(count[j])
#      
for i in balls:
  print(sum - (count[i]-1))

Submission status: Accepted

Solution code:
import collections as cc
n=int(input())
l=list(map(int,input().split()))
ff=cc.Counter(l)
ff=dict(ff)
ans=0
for i in ff:
    x=ff[i]
    ans+=(x*(x-1))//2
v={}
for i in set(l):
    vv=max(ff[i]-1,0)
    te=ans-(ff[i]*(ff[i]-1))//2+(vv*(vv-1))//2
    v[i]=te
for i in l:
    print(v[i])

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

S = [0]*(N+1)
for key, val in d.items():
  S[key] = (val * (val-1))//2

ans = sum(S)
for k in range(N):
  t = d[A[k]]*(d[A[k]]-1)//2
  add = (d[A[k]]-1)*(d[A[k]]-2)//2
  print(ans - t + add)

Submission status: Accepted

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

from collections import defaultdict

cnt = defaultdict(int)
for i in range(n):
    cnt[a[i]] += 1
# print(cnt)

for key, val in list(cnt.items()):
    if val < 2:
        cnt.pop(key)
# print(cnt)

total = 0
for val in cnt.values():
    if val >= 2:
        total += val*(val-1)//2

for i in range(n):
    diff = 0
    if a[i] in cnt:
        y = (cnt[a[i]]-1)*cnt[a[i]]//2
        x = (cnt[a[i]]-1)*(cnt[a[i]]-2)//2
        diff = y - x
    print(total - diff)
        
        

# for i in range(n):
#     flag = False
#     if a[i] in cnt:
#         cnt[a[i]] -= 1
#         flag = True
#     # print(cnt)
#     ans = 0
#     for val in cnt.values():
#         if val >= 2:
#             ans += val*(val-1)//2
#     print(ans)
#     if flag:
#         cnt[a[i]] += 1

Submission status: Accepted

Solution code:
n=int(input())
 
A=list(map(int,input().split()))
 
X=[0]*(n+1)
 
for i in range(n):
    X[A[i]]+=1
 
ans=0

for i in range(n+1):
    if X[i]>1:
        ans+=X[i]*(X[i]-1)//2      
for i in range(n):
    if X[A[i]]==1:
        print(ans)
    elif X[A[i]]>1:
        print(ans-(X[A[i]]-1))

Submission status: Accepted

Solution code:
#python3
from collections import Counter
def C2(x):
    return x*(x-1)//2

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    d = Counter(a) 
    pans = 0
    for v in d.values():
        pans += C2(v)

    for k in range(n):
        p = pans
        val = d[a[k]]
        p = p - C2(val) + C2(val-1)
        print(p)
main()

Submission status: Accepted

Solution code:
n = input()
n = int(n)
input_list = list(map(int, input().split()))
#print(input_list)
dicts = {}
for i in range(n):
    if input_list[i] not in dicts.keys():
        dicts[input_list[i]] = 0
    dicts[input_list[i]] += 1

sums = 0
dicts2 = {}
for key, value in dicts.items():
    if dicts[key] >= 2:
        dicts2[key] = value * (value - 1) // 2
    else:
        dicts2[key] = 0
    sums += dicts2[key]

#print(dicts, dicts2)
for j in range(n):
    if dicts[input_list[j]] >= 2:
        value1 = dicts[input_list[j]]
        res = sums - dicts2[input_list[j]] + (value1-1) * (value1 - 2) // 2
    else:
        res = sums
    print(str(res))

Submission status: Accepted

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

n, = I()
a = I()

d = dd(int)
for i in a:
    d[i] += 1

def get(x):
    return x * (x - 1) // 2

total = 0
for i in d:
    total += get(d[i])

for i in range(n):
    ans = total - get(d[a[i]]) + get(d[a[i]] - 1)
    print(ans)

Submission status: Accepted

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

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

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

sum = 0
for key in nums:
    num = nums[key]
    sum += cmb2(num)

for k in range(N):
    ans = sum - cmb2( nums[A[k]] ) + cmb2( nums[A[k]]-1 )
    print(ans)

Submission status: Accepted

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

adic = ddic(int)
for a in AA:
    adic[a] += 1
    
patdic = {}
asum = 0
for key in adic.keys():
    num = adic[key]
    seki = num * (num-1)//2
    
    patdic[key] = seki
    asum += seki
    
# print(adic)
# print(adic.keys())
    
# for key in patdic.keys():
#     print(key)
#     print(adic[key])
#     print(patdic[key])
#     print('---')

for a in AA:
    ans = asum - (patdic[a] ) + (patdic[a] - adic[a]+1)
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
#L = [0 for i in range(N)]
dict = {}

for i in A:
  if i in dict:
    dict[i] = dict[i] + 1
  else:
    dict[i] = 1

#print(dict)
total = 0;
for i in dict:
#  print("i:" + str(i) + " " + str(dict[i]))
  total += dict[i]*(dict[i]-1) // 2

#print("T:" + str(total))

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

Submission status: Accepted

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


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

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

c = collections.Counter(A)

comb_sum = sum([comb_count(n) for n in c.values()])
# print(comb_sum)
for a in A:
    print(comb_sum - (c[a] - 1))

Submission status: Accepted

Solution code:
import math
import collections

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

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

C = collections.Counter(A)
csum = 0

for c in C.values():
    if c > 1:
        csum += combinations_count(c, 2)

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

Submission status: Accepted

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

Cntr = collections.Counter(A)
Sum = 0
for c in Cntr.items():
    key = c[0]
    val = c[1]
    if val > 1:
        Sum += val * (val - 1) // 2

for a in A:
    print(Sum-(Cntr[a]-1))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

count_dict = defaultdict(int)
for cal in A:
  count_dict[cal] += 1
    
from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

count_result = defaultdict(list)
for k,v in count_dict.items():
  if v > 2:
    count_result[k] = [combinations_count(v,2), combinations_count(v-1,2)]
  elif v == 2:
    count_result[k] = [1,0]    
  else:
    count_result[k] = [0,0]

all_ans = 0
for v in count_result.values():
  all_ans += v[0]


for _ in A:
  ans = all_ans - count_result[_][0] + count_result[_][1]
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = sorted(A)
C = [0 for i in range(N+1)]

bef = 0
for i in range(N):
  if bef!=B[i]:
    bef = B[i]
    C[B[i]] = 1
  else: C[B[i]]+=1

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

for i in range(N):
  dif = C[A[i]]*(C[A[i]]-1)//2 - (C[A[i]]-1)*(C[A[i]]-2)//2
  print(sum-dif)

#print(N,A,B,C,sum)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
ball = [int(i) for i in input().split()]
counter = Counter(ball)
result = sum([i * (i - 1) // 2 for i in counter.values()])

for i in range(n):
  c = counter[ball[i]]
  print(result - (c - 1))

Submission status: Accepted

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

c = dict(Counter(a))
dp_on = {}
dp_off = {}
for k, v in c.items():
    if v == 1:
        dp_on[k] = 0
        dp_off[k] = 0
    elif v == 2:
        dp_on[k] = 1
        dp_off[k] = 0
    else:
        dp_on[k] = int(v*(v-1)/2)
        dp_off[k] = int((v-1)*(v-2)/2)
comb_sum = sum([v for v in dp_on.values()])
for i in range(n):
    print(comb_sum - dp_on[a[i]] + dp_off[a[i]])

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
ball = [0] * N
ans = 0

for A in A_list:
    ans += ball[A - 1]
    ball[A - 1] += 1
    

[print(ans - (ball[A_list[k] - 1]) + 1) for k in range(N)]

Submission status: Accepted

Solution code:
from collections import Counter
import math

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

# print('input >>')
N = int(input())
A = list(map(int,(input().split())))

Ac = Counter(A)

sumc = 0

for Ae in dict(Ac):
    sumc += comb(Ac[Ae])

# print('-----output-----')

for k in A:
    print(sumc - comb(Ac[k]) + comb(Ac[k]-1))

Submission status: Accepted

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

from collections import Counter

c = Counter(a)

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

base = 0
for aa in c.keys():
    x = c[aa]
    base += comb(x)

for aa in a:
    ans = base - comb(c[aa]) + comb(c[aa] - 1)
    print(ans)

Submission status: Accepted

Solution code:
import collections
def input_int_list():
    return [int(thing) for thing in input().split(" ")]

nCr = {}

def nC2(n):
    if n in nCr:
        return nCr[n]
    else:
        val=n*(n-1)//2
        nCr[n]=val
        return val



N=int(input())
A_list=input_int_list()
int_count=collections.Counter(A_list)
the_sum=sum([nC2(int_count[i]) for i in int_count])
for i in range(N):
    number=A_list[i]
    c=int_count[number]
    print(the_sum-c+1)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = [int(i) for i in input().split()] 

num_dict = collections.Counter(a)
min_dict = {}
max_num = 0
for k,v in num_dict.items():
  keymax = v*(v-1)/2
  max_num += keymax
  if (v == 1):
    min_dict[k] = 0
  else:
    min_dict[k] = keymax - (v-1)*(v-2)/2

for i in a:
  print(int(max_num-min_dict[i]))

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
# print(a_list)
a_set = set(a_list)
length_dict = {}
for elm in a_set:
    length_dict[elm] = 0    

for ind in range(n):
    elm = a_list[ind]
    length_dict[elm] += 1

total_num = 0
for elm in a_set:
    total_num += int(length_dict[elm]*(length_dict[elm]-1)/2)
difference = {}
for elm in a_set:
    difference[elm] = int(length_dict[elm]*(length_dict[elm]-1)/2) - int((length_dict[elm]-1)*(length_dict[elm]-2)/2)


# print(num_dict0, num_dict1)
for ind in range(n):
    elm = a_list[ind]
    print(total_num-difference[elm])

Submission status: Accepted

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

dic = {}
for val in a:
    if not(val in dic.keys()):
        dic[val] = 1
    else:
        dic[val] += 1

sum = 0
for val in dic.keys():
    sum += dic[val]*(dic[val]-1)//2

for k in range(n):
    print(sum - dic[a[k]]*(dic[a[k]]-1)//2 + (dic[a[k]]-1)*(dic[a[k]]-2)//2)

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

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

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

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

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n=int(input())
    a=list(map(int,input().split()))
    b=[0 for i in range(n+1)]
    for i in a:
        b[i]+=1
    total=0
    for i in b:
        total+=i*(i-1)//2

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




if __name__ == "__main__":

    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
C = {}
for a in A:
    if a in C:
        C[a] += 1
    else:
        C[a] = 1

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

Submission status: Accepted

Solution code:
import math

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

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    hist = [0] * 2*pow(10, 5)
    for a in A:
        hist[a-1] += 1

    all = 0
    for h in hist:
        if h >= 2:
            all += combinations_count(h)

    for a in A:
        h = hist[a-1]
        if h >= 2:
            diff = h-1
            print(all - diff)
        else:
            print(all)
    
if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = [0]
a.extend(list(map(int, input().split())))
max_a = max(a)
counter = [0]*(max_a+1)
combi_common = 0
for num in a:
    counter[num] += 1
for i in range(1, max_a+1):
    if counter[i] >= 2:
        combi_common += counter[i]*(counter[i]-1)//2
for j in range(1,n+1):
    cou = counter[a[j]]
    if cou >= 2:
        out = combi_common + ((cou-1)*(cou-2) - cou*(cou-1))//2
    else:
        out = combi_common
    print(out)