Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

Solution code:
# def cmb(n, k, mod):
#     numerator = 1
#     for i in range(k):
#         numerator = (numerator * (n - i)) % mod

#     denominator = 1
#     for i in range(1, k + 1):
#         denominator = (denominator * i) % mod

#     return (numerator * pow(denominator, mod - 2, mod)) % mod


def ncr(n, r):
    r = min(r, n - r)
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + i + 1 for i in range(r)]
    denominator = [i + 1 for i 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 *= numerator[k]
    return int(result)


import math


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


import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
freq = {}
for i in a:
    if i in freq:
        freq[i] += 1
    else:
        freq[i] = 1

inf = 1001001001
dp = [inf for _ in range(200005)]
dp2 = [inf for _ in range(200005)]
mod = 10**13 + 3

base = 0
for k, v in freq.items():
    if v > 2:
        dp[k] = ncr(v, 2)
        dp2[k] = ncr(v - 1, 2)
    elif v == 2:
        dp[k] = 1
        dp2[k] = 0
    else:
        dp[k] = 0
        dp2[k] = 0
    base += dp[k]

for i in a:
    print(base - dp[i] + dp2[i])

Submission status: Accepted

Solution code:
N=int(input())
la=list(map(int,input().split()))
lc=[0 for i in range(N+1)]
for a in la:
    lc[a]+=1
#print("lc",lc)
"""
lsum=[0 for i in range(N+1)]
for i in range(N):
    lsum[i+1]=lsum[i]+lc[i+1]*(lc[i+1]-1)//2
"""
sum=0
for i in range(N):
    sum+=lc[i+1]*(lc[i+1]-1)//2
#print("sum",sum)
for i in range(1,N+1):
    #print("kosuu_mae",lc[la[i-1]])
    print(sum-lc[la[i-1]]*(lc[la[i-1]]-1)//2+(lc[la[i-1]]-2)*(lc[la[i-1]]-1)//2)

Submission status: Accepted

Solution code:
import collections
N = int(input())
li = list(map(int, input().split()))
cnLi = collections.Counter(li)
CcnLi = [n*(n-1)//2 for n in cnLi.values()]
sumC = sum(CcnLi)
for k in range(N):
    print(sumC-cnLi[li[k]] + 1)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
S=sorted(L)
D={}
c=1
t=0
for i in range(N-1):
  if S[i]==S[i+1]:
    c+=1
  else:
    D[S[i]]=c
    c=1
D[S[N-1]]=c

for i in D.values():
   t+=i*(i-1)//2 
for i in range(N):
  print(t-(D[L[i]]-1))

Submission status: Accepted

Solution code:
def Main(N, A):
    d1 = {}
    for a in A:
        if a not in d1:
            d1[a] = 1
        else:
            d1[a] += 1
    full = 0
    for k in d1:
        full += d1[k]*(d1[k]-1)//2
    ans = []
    for a in A:
        ans.append(full-d1[a]+1)
    return ans 
    

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=(int)(input())
As=(input()).split(" ")

A_num = {}

for A in As:
    if A in A_num.keys():
        A_num[A] += 1
    else:
        A_num[A] = 1


total = 0
for num in A_num.values():
    total+=num*(num-1)//2
#print(A_num)
for A in As:
    print(total-A_num[A]+1)

Submission status: Accepted

Solution code:
import copy

N = int(input())

As = list(map(lambda i: int(i), input().split()))

ns = [0 for i in range(N)]

for a in As:
    ns[a-1] += 1

def get_comb(arr):
    res = 0
    for a in arr:
        if a == 0:
            continue
        res += a*(a-1)/2
    return int(res)

ba = get_comb(ns)

for a in As:
    print(ba - get_comb([ns[a-1]]) + get_comb([ns[a-1]-1]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
lis = [0 for _  in range(N+1)]
lis1 = [0 for _ in range(N+1)]
lis2 = [0 for _ in range(N+1)]
for i in range(N):
    lis[A[i]] += 1

for i in range(N+1):
    num = lis[i]
    if num != 0:
        lis1[i] = int(num*(num-1)/2)
        lis2[i] = int((num-1)*(num-2)/2) - lis1[i]

total = sum(lis1)

for i in range(N):
    val = A[i]
    print(total + lis2[val])

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = input_list()
  ac = collections.Counter(a)
  f_list = {}
  f_sum = 0
  for k, c in ac.items():
    if c <= 1:
      f_list[k] = (c, 0)
    else:
      cc = combinations_count(c, 2)
      f_list[k] = (c, cc)
      f_sum += cc
  for i in a:
    t = f_list[i]
    tv = t[1] - (t[0] - 1)
    print(f_sum - t[1] + tv)
          
  
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split(" ")))
a_list = [[i, 0] for i in range(N+1)]
a_comb = [[i, 0] for i in range(N+1)]
a_comb_sub = [[i, 0] for i in range(N+1)]

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

for i in a_list:
    a_comb[i[0]][1] = i[1]*(i[1]-1) // 2

ans =0
for i in a_comb:
    ans+= i[1]


for i in a_list:
    if i[1] >=2:
        a_comb_sub[i[0]] = i[1]-1
    else:
        a_comb_sub[i[0]] = 0

for i in a:
    print(ans - a_comb_sub[i])

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split(' ')]
c = collections.Counter(a)
counts = {}
all_sum = 0
for k, v in c.items():
    val = v * (v-1) / 2
    counts[k] = val
    all_sum += val

for i in range(n):
    k = a[i]
    count = c[k] - 1
    val = counts[k]
    print(int(all_sum - val + count * (count - 1) / 2))

Submission status: Accepted

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

for j in a:
    d[j]-=1
    
    if ans>0:
        ans-=d[j]
        print(ans)
        ans+=d[j]
        d[j]+=1
    elif ans==0:
        print(0)

Submission status: Accepted

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

import collections

e = collections.Counter(a)

count = 0
for i in e.keys():
    m = e[i]
    count += m*(m-1)//2

for k in range(n):
    b = a[k]
    c = e[b] - 1
    ans = count - c
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
N = int(input())
A = list(map(int,input().split()))

def comb(n):
    if n > 1:
        return n * (n-1) // 2
    else:
        return 0

dict = {}

for i in range(N):
    if A[i] in dict.keys():
        dict[A[i]] = dict[A[i]] + 1
    else:
        dict[A[i]] = 1

combs = {}

for k in dict:
    combs[k] = comb(dict[k])


total = sum(combs.values())

for i in range(N):
    diff = combs[A[i]] - comb(dict[A[i]]-1)
    print(total - diff)

Submission status: Accepted

Solution code:
import collections
N = int(input())
arr = list(map(int, input().split()))
count = collections.Counter(arr)
total = 0
for i in count.values():
    total += (i*(i-1)/2)
for i in arr:
    print(int(total- count[i]+1))

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

import math

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

n = int(input())
a = input().split()

dic = {}
for i in range(len(a)) :
    if a[i] in dic :
        dic[a[i]] = dic[a[i]] + 1
    else :
        dic[a[i]] = 1

#print(dic)
dicSum = {}
total = 0

for key in dic:
    dicSum[key] = cmb(dic[key], 2)
    total += dicSum[key]

#print(dicSum, total)

for i in range(n) :
    #print(total, dicSum[a[i]], combinations_count(dic[a[i]]-1, 2))
    print(total - dicSum[a[i]] + cmb(dic[a[i]]-1, 2))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
all_comb = 0
for v in c.values():
    all_comb += v*(v-1)/2

for i in range(n):
    tmp = c[a[i]]
    print(int(all_comb-tmp+1))

Submission status: Accepted

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

count_A = [0]*N
for a in A:
    count_A[a-1] += 1

ret = 0
for ca in count_A:
    if ca > 0:
        ret += int((ca * (ca - 1)) / 2)

for a in A:
    ca = count_A[a-1]
    print(ret-ca+1)

Submission status: Accepted

Solution code:
import sys, math

sys.setrecursionlimit(10 ** 9)
from collections import defaultdict

IA = lambda: map(int, input().split())
IAS = lambda: map(str, input().split())

n = int(input())
N = 200005
def f(n):
    if n < 2: return 0
    return n * (n - 1) // 2

a = list(IA())
vis = [0 for i in range(0, n + 1)]
for it in a:
    vis[it] += 1

sum1 = 0
sumsum=0
for i in range(1,n):
    sumsum+=1
for i in range(1, n + 1):
    sum1 += f(vis[i])
ans = [int(-1) for i in range(0, n + 1)]
for it in a:
    if ans[it] == -1:
        ans[it] = sum1 - f(vis[it]) + f(vis[it] - 1)
    print(ans[it])

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(input().split())
b = collections.Counter(a)
num = list(b.values())
k = len(num)
an = 0

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

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
c = dict(Counter(a))
ans = 0
for v in c.values():
    ans += v*(v-1)//2
for i in range(n):
    print(ans-c[a[i]]+1)

Submission status: Accepted

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

for a in A:
  if not a in b:
    b[a] = 1
  else:
    b[a] += 1
    
for v in b.values():
  p += v * (v - 1) // 2
  
for a in A:
  d = b[a]
  ans = p - (d * (d - 1) // 2) + ((d - 1) * (d - 2) // 2)
  print(ans)

Submission status: Accepted

Solution code:
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


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

tot = 0
for key in d:
    if d[key] == 1: continue
    tot += cmb(d[key], 2)

#print(d)
for i in range(n):
    if d[a[i]] == 1:
        print(tot)
    elif d[a[i]] == 2:
        print(tot-1)
    else:
        print(tot - (d[a[i]] - 1))

Submission status: Accepted

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

import collections
count_dic = collections.Counter(a_list)

count_dic_new = {}
for key, value in count_dic.items():
    if value == 1:
        count_dic_new[key] = 0
    elif value == 2:
        count_dic_new[key] = 1
    else: 
        count_dic_new[key] = (value * (value - 1)) // 2

fund_values = sum(list(count_dic_new.values()))

for a in a_list:
    if count_dic[a] == 1:
        a_value = fund_values
    elif count_dic[a] == 2:
        a_value = fund_values - 1
    else:
        n = count_dic[a]
        a_value = fund_values + (((n-1) * (n-2)) // 2) - (((n) * (n-1)) // 2) 

    print(a_value)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
    if a[i] in dic:
        dic[a[i]]+=1
    else:
        dic.setdefault(a[i],1)
comb={}
banned={}
for i in dic.keys():
    if dic[i]>1:
        comb.setdefault(i,dic[i]*(dic[i]-1)//2)
        banned.setdefault(i,(dic[i]-1)*(dic[i]-2)//2)
    else:
        comb.setdefault(i,0)
        banned.setdefault(i,0)
sumComb=sum(comb.values())
for i in range(n):
    print(sumComb-comb[a[i]]+banned[a[i]])

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)
dic = {}

for x in a:
    if x not in dic:
        ex = (c[x] - 1) * (c[x] - 2) / 2
        no = (c[x]) * (c[x] - 1) / 2
        dic[x] = (int(no), int(ex))

su = sum([int(x[0]) for x in dic.values()])

for i,x in enumerate(a):
    print(su - (dic[x][0] - dic[x][1]))

Submission status: Accepted

Solution code:
import collections
import math
N = int(input())
A = list(map(int, input().split(' ')))
c = collections.Counter(A)
d1 = {}
d2 = {}
for i in c:
    d1[str(i)] = (c[i]*(c[i]-1))/2
    d2[str(i)] = ((c[i]-1)*(c[i]-2))/2

s = sum(d1.values())

for i in range(N):
    ans = s - d1[str(A[i])]+d2[str(A[i])]
    print(int(ans))

Submission status: Accepted

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

nums = {}
for i in range(n):
    if A[i] not in nums.keys():
        nums[A[i]] = 1
    else:
        nums[A[i]] += 1

calc_list = {}
for key, v in nums.items():
    calc_list[key] = int(v*(v-1)/2)
s = sum(calc_list.values())
# print(calc_list,s)

for i in range(n):
    tmp = nums[A[i]]-1
    diff = int(nums[A[i]]*(nums[A[i]]-1)/2 - tmp*(tmp-1)/2)
    print(s-diff)

# print(nums)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)
all_c = 0
for k,v in c.items():  
  all_c += v*(v-1)//2
    
for a in A:  
  print(all_c - (c[a]-1))

Submission status: Accepted

Solution code:
import collections

n=int(input())
As=list(map(int,input().split()))
ls=collections.Counter(As)
subd={}
an=ls.values()
ans=0
for k in an:
  ans+=k*(k-1)//2
sans=ans

for i in As:
  ans=sans+1-ls[i]
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
D = [0]*(N+1)
for a in A:
  D[a] += 1
tot = sum([D[i]*(D[i]-1)//2 for i in range(1,N+1)])
#print(D)
#print([D[i]*(D[i]-1)//2 for i in range(N)])

#print([(D[i]-1)*(D[i]-2)//2 for i in range(N)])
#print(tot)
for a in A:
  print(tot - D[a]+1)

Submission status: Accepted

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

for i in range(N):
    if(size[i] >= 2):
        ans += int(size[i] * (size[i]-1) / 2)

for k in range(N):
    if(size[A[k]-1] <= 1):
        print(ans)
    else:
        print(ans - (size[A[k]-1]-1))

Submission status: Accepted

Solution code:
# D
import collections

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

N = int(input())
A = list( map(int,input().split()))
c = dict(collections.Counter(A))
su = 0
for i in c.values():
    if (i >= 2):
        su += combinations_count(i, 2)

for i in A:
    if (c[i]  >= 3):
        print(su - combinations_count(c[i],2) + combinations_count(c[i]-1,2))
    elif (c[i] == 2):
        print(su - 1)
    else:
        print(su)

Submission status: Accepted

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

    A_product = [0] * N
    A_product_1 = [0] * N
    for i in range(N):
        a = A_num[i]
        A_product[i] = int(a * (a-1) / 2)
        if a == 0:
            A_product_1[i] = 0
        else:
            A_product_1[i] = int((a-1) * (a-2) / 2)


    su = sum(A_product)
    for k in A:
        print(su - A_product[k-1] + A_product_1[k-1])


if __name__ == "__main__":
    main()

Submission status: Accepted

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

cnt = [0] * N
for i in range(N):
	cnt[A[i] - 1] += 1

cmbr2 = [0] * (N + 1)
for i in range(1, N + 1):
	cmbr2[i] = cmbr2[i - 1] + i - 1

cmb = [0] * N
for i in range(N):
	cmb[i] = cmbr2[cnt[i]]

asum = sum(cmb)
for i in A:
	print(asum - cmbr2[cnt[i - 1]] + cmbr2[cnt[i - 1] - 1])

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

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

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

    cmb = 0

    for i in range(N):
        M = count[i]
        cmb += M * (M-1) // 2

    for i in A:
        s = count[i-1]
        if s <= 1:
            dec = 0
        else:
            dec = s - 1
        temp = cmb - dec
        print(temp)



main()

Submission status: Accepted

Solution code:
from collections import Counter

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

#table = set(a)
con = Counter(a)
dic = {}
#for i in table:
#    con[i] = a.count(i)
#    dic[i] = -1

for x in a:
    if x not in dic:
        ex = (con[x] - 1) * (con[x] - 2) / 2
        no = (con[x]) * (con[x] - 1) / 2
        dic[x] = (int(no), int(ex))

su = sum([int(x[0]) for x in dic.values()])

for i,x in enumerate(a):
    print(su - (dic[x][0] - dic[x][1]))

Submission status: Accepted

Solution code:
import math
import collections


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


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

cnt = collections.Counter(a)
each_ans = {}
for key, value in cnt.items():
    t = nc2(value)
    each_ans[key] = (t, t-value+1)
all_sum = sum(value[0] for _, value in each_ans.items())
ans = {}
for key in cnt.keys():
    ans[key] = all_sum - each_ans[key][0] + each_ans[key][1]
for i in a:
    print(ans[i])

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(lambda: 0)

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

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

comp = 0
for k,v in d.items():
    comp += comb2(v)


for i in A:
    ans = comp - comb2(d[i]) + comb2(d[i] - 1)
    print(ans)

Submission status: Accepted

Solution code:
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict

def main():
    N = int(input())
    d = defaultdict(int)
    As = list(map(int, input().split()))
    for i in range(N):
        d[As[i]] += 1
    
    ans = 0
    for k in d.keys():
        j = d[k]
        if j > 1:
            ans += j*(j-1)//2

    for i in range(N):
        j = d[As[i]]
        print(ans+1-j)

# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc159","d",main)

Submission status: Accepted

Solution code:
def binom(n,r):
    if n<r or n<0:
        return 0

    result=1
    for i in range(r):
        result*=(n-i)
        result//=(i+1)
    return result

n=int(input())
an=[int(c) for c in input().split()]

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

sum_=0
minus=[0 for _ in range(n)]
for i in range(n):
    sum_+=binom(count[i],2)
    minus[i]=binom(count[i]-1,1)

for i in range(n):
    print(sum_-minus[an[i]-1])

Submission status: Accepted

Solution code:
import math
import collections

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


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

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)
from collections import defaultdict
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()))
md={}
for a in A:
  if a in md:
    md[a]+=1
  else:
    md[a]=1
md2={}
md3={}
for key,value in md.items():
  if value>1:
    md2[key]=combinations_count(value,2)
  else:
    md2[key]=0
  value-=1
  if value>1:
    md3[key]=combinations_count(value,2)
  else:
    md3[key]=0
s_md2=sum(md2.values())
for a in A:
  print(s_md2-md2[a]+md3[a])

Submission status: Accepted

Solution code:
n = int(input())
balls = list(map(int, input().split()))
num_dict = {}
total_pairs = 0

for b in balls:
    if b not in num_dict.keys():
        num_dict[b] = [1, 0]
    else:
        num_dict[b][0] += 1
        num_dict[b][1] = (num_dict[b][0] * (num_dict[b][0]-1) - (num_dict[b][0]-1) * (num_dict[b][0]-2))//2

for val in num_dict.values():
    v = val[0]
    total_pairs += v*(v-1)//2
    
for i in range(n):
    ball_val = balls[i]
    print(total_pairs - num_dict[ball_val][1])

Submission status: Accepted

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

for i in range(N):
  if A[i] in cnt2:
    cnt[A[i]] += cnt2[A[i]]
    cnt2[A[i]] += 1
  else:
    cnt[A[i]] = 0
    cnt2[A[i]] = 1
ans = sum(cnt.values())
#print(ans, cnt, cnt2)
for i in range(N):
  print(ans - cnt2[A[i]]+1)

Submission status: Accepted

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

A_sort = sorted(A)
before = A_sort[0]
num = 1
jisho = {}
for i in range(1,N):
    if before == A_sort[i]:
        num += 1
    else:
        jisho[before] = num
        num = 1
        before = A_sort[i]
jisho[before] = num

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

for i in range(N):
    n = jisho[A[i]]
    print(total-n+1)

Submission status: Accepted

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

c = dict(collections.Counter(a))

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

for i in a:
    temp = c[i] - 1
    ans = (temp * (temp - 1)) // 2
    ans += sum
    ans -= (c[i] * (c[i] - 1)) // 2
    print(ans)

Submission status: Accepted

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

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())
M = input().split()

c = collections.Counter(M)

A = 0

for i in c.values():
    if i >= 2:
        A += cmb(i, 2)
    
for j in M:
    print(A - (c[j]-1))

Submission status: Accepted

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

s_set = set(s)

counted = Counter(s)
ans = sum(i*(i-1)/2 for i in counted.values())

for i in s:
    x = counted[i]
    print(int(ans - x + 1))

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    N = int(input())
    As = list(map(int,input().split()))
    Cntr = Counter(As)

    ans0 = 0
    for i in Cntr:
        n = Cntr[i]
        ans0 += n*(n-1)//2
    
    for ai in As:
        ni = Cntr[ai]
        print(ans0-ni+1)

    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


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


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    t = 0
    for i in c:
        t += combinations_count(c[i])

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

Submission status: Accepted

Solution code:
#D
n = int(input())

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

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

index = {}

for i in range(0,  len(line)):
    if line[i] in index.keys():
        index[line[i]] = index[line[i]] + 1
    else:
        index[line[i]] = 1

total = 0

for key in index:
    total = total + c2(index[key])

for u in range(0, len(line)):
    print(total-(index[line[u]]-1))

Submission status: Accepted

Solution code:
from collections import Counter
from functools import lru_cache

@lru_cache(maxsize=20000)
def combi2(n):
  return n * (n-1) // 2

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

# print(c)

base = 0
for value in c.values():
  base += combi2(value)

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

Submission status: Accepted

Solution code:
n = int(input())
a_ls = list(map(int, input().split())) 
# キーはa_lsのユニークな値、valueは出現回数
a_dict = {}
for a in a_ls:
    if not a in a_dict.keys():
        a_dict[a] = 0
    a_dict[a] += 1   

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

# キーはa_lsのset要素, 値はそれで何通りできるか
every_dict = {}
for i in a_dict.keys():
    if a_dict[i] > 1:
        every_dict[i] = comb(a_dict[i],2)
    else:
        every_dict[i] = 0

values_ls = every_dict.values()
_sum = sum(values_ls)
ans_dict = {}
for i in set(a_ls):
    if a_dict[i] - 2 > 0:
        ans_dict[i] = _sum - every_dict[i] + (every_dict[i] *  (a_dict[i] - 2)) // a_dict[i]
    else:
        ans_dict[i] = _sum - every_dict[i]

for i in a_ls:
    print(ans_dict[i])

Submission status: Accepted

Solution code:
import collections
from sys import stdin
n = int(stdin.readline().rstrip())
ass = list(map(int, stdin.readline().rstrip().split()))

c = collections.Counter(ass)

cnum =[0]*len(c)
keys = c.keys()

cs = {}
for i in keys:
    key = c[i]
    combi = key *(key-1) / 2
    cs[i] = combi

sum = sum(cs.values())

for i in range(n):
    add = c[ass[i]]-1
    adds = add *(add-1) / 2
    ans = sum - cs[ass[i]] + adds
    print(int(ans))

Submission status: Accepted

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

sa=set(A)
ca=col.Counter(A)
num=0
for i in sa:
    if ca[i]==1:
        continue
    else:
        num += (ca[i]*(ca[i]-1))//2
for i in A:
    ans = num - (ca[i]-1)
    print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

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 main():
    n = ni()
    A = []
    Onaji = {}
    for i in na():
        i -= 1
        A.append(i)
        if i in Onaji:
            Onaji[i] += 1
        else:
            Onaji[i] = 1

    kumi = {}
    minuskumi = {}
    Sum = 0
    for a in A:
        if Onaji[a] <= 1:
            continue
        if a in kumi:
            continue
        kumi[a] = cmb(Onaji[a], 2)
        if Onaji[a] - 1 <= 1:
            minuskumi[a] = 0
        else:
            minuskumi[a] = cmb(Onaji[a] - 1, 2)
        Sum += kumi[a]
    for a in A:
        if a in minuskumi and a in kumi:
            print(minuskumi[a] + Sum - kumi[a])
        else:
            print(Sum)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [[] for i in range(N)]
ans = [0]*N
for k, a in enumerate(A):
    a = a - 1
    B[a].append(k)


S = 0
for b in B:
    lenb = len(b)
    if lenb >= 1:
        S += lenb * (lenb-1) / 2

for i, b in enumerate(B):
    for j, k in enumerate(b):
        ans[k] = int(S - (len(b) - 1))
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
def parm(a):
    return(int(a*(a-1)/2))

n=int(input())
a=list(input().split())
dic={}
ans=[0]*n
sumans=0
for i in range(n):
    if a[i] in dic.keys():
        dic[a[i]]+=1
    else:
        dic[a[i]]=1
for i in dic.values():
    sumans+=parm(i)
for i in range(n):
    print(sumans-parm(dic[a[i]])+parm(dic[a[i]]-1))

Submission status: Accepted

Solution code:
from copy import copy
n = int(input())
a = [int(i) for i in input().split()]
count_a = {}
diff_a = [0] * n
for ai in a:
    try:
        count_a[ai] += 1
    except KeyError:
        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])