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:
#!/usr/bin/env pypy3

import sys
sys.setrecursionlimit(10 ** 6)
memo = [1, 1]
def fact(n):
	if len(memo) > n:
		return memo[n]
	else:
		for i in range(len(memo), n+1):
			memo.append(memo[i-1] * i)
		return memo[n]
def nCr(n, r):
	a = 1
	for i in range(r):
		a *= n - i
	return a //fact(r)
def main():
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	cnt = {}
	sumcase = 0
	for i in range(n):
		cnt[a[i]] = 0
	for i in range(n):
		cnt[a[i]] += 1
	# print(a, cnt)
	for ai in cnt:
		if cnt[ai] >= 2:
			sumcase += nCr(cnt[ai], 2)
	for i in range(n):
		if cnt[a[i]] == 2:
			print(sumcase - nCr(cnt[a[i]], 2))
		elif cnt[a[i]] > 2:
			#print("hoge", a,  cnt[a[i]], sumcase, cnt)
			print(sumcase - nCr(cnt[a[i]], 2) + nCr(cnt[a[i]]-1, 2))
		else:
			print(sumcase)
main()

Submission status: Accepted

Solution code:
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}
#print(d)
all_sum = sum(d.values())
#print(all_sum)

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
            sum_d[a[k]] = all_sum  - d[a[k]] + tmp
            print(sum_d[a[k]])
        else:
            print(all_sum)
    else:
        print(sum_d[a[k]])

Submission status: Accepted

Solution code:
import collections

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

# 数値Aの頻度
ndic = collections.Counter(A)

mem = {}
tcmb = 0
for j in ndic:
    if ndic[j] == 1:
        continue
    
    if ndic[j] in mem:
        tcmb += mem[ndic[j]]
    else:
        mem[ndic[j]] = int((ndic[j] * (ndic[j] -1)) / 2)
        tcmb += mem[ndic[j]]
        
for i in A:
    cmb  = tcmb - (ndic[i] - 1)
    print(cmb)

Submission status: Accepted

Solution code:
import collections

n, *l = map(int, open(0).read().split())

c = collections.Counter(l)

values = list(c.values())

cnt = 0
for num in values:
    cnt += num * (num - 1) // 2
    
for num in l:
    print(cnt - (c[num] - 1))

Submission status: Accepted

Solution code:
import collections
N=int(input())
a=0
list1 = list(map(int, input().split()))
c = collections.Counter(list1)
for i in range(N):
    a+=int(c[i+1]*(c[i+1]-1)/2)
for i in range(N):
    if c[list1[i]]!=0:
        print(a-c[list1[i]]+1)
    else:
        print(a)

Submission status: Accepted

Solution code:
import collections
from functools import lru_cache
from scipy.misc import comb


results = {}


@lru_cache(maxsize=None)
def get_combination(n):
    if n < 2:
        return 0
    else:
        return comb(n, 2, exact=True)


n = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
S = 0
for k, v in c.items():
    S += get_combination(v)
for a in A:
    if results.get(a):
        print(results[a])
    else:
        results[a] = S - get_combination(c[a]) + get_combination(c[a] - 1)
        print(results[a])

Submission status: Accepted

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

c = collections.Counter(an)
allcount = 0

for i in c.values():
    allcount += i*(i-1)//2

for i in an:
    print(allcount - (c[i]-1))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
# 除かずに数える
cnt = {}
for i in a:
  cnt[i] = cnt.get(i, 0) + 1
ans = 0
for i in cnt.values():
  ans += i * (i - 1) // 2
for i in a:
  # i を除くと cnt[i] - 1 個減る
  print(ans - cnt[i] + 1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
numlist = list(map(int, input().split()))
dic = {}
for num in numlist:
  if num not in dic:
    dic[num] = 1
  else:
    dic[num] += 1
    
tmpsum = 0
for value in dic.values():
  tmpsum += int(value*(value-1)/2)
ansdic = {}
for key in dic.keys():
  n = dic[key]
  ansdic[key] = tmpsum - n + 1      
    
for num in numlist:
  print(ansdic[num])

Submission status: Accepted

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

for j in range(n):
    print((nsum+1-b[a[j]]))

Submission status: Accepted

Solution code:
# from sys import stdin
# import math
from functools import lru_cache


@lru_cache(maxsize=1000)
def combinations_count(n, r):
    if n < 2:
        return 0
    return n*(n-1)//2


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

d = {}
cnt = 0
for i in range(N):
    d[A[i]] = d.get(A[i], 0)+1
# print(d)
sum1 = 0
for key, value in enumerate(d):
    # print(key,value)
    sum1 = sum1+combinations_count(d.get(value, 0), 2)

for l in range(N):

    # print(d)
    # print(len(d))
    sum = 0
    sum = sum1-combinations_count(d.get(A[cnt], 0), 2)
    d[A[cnt]] = d.get(A[cnt], 0)-1
    sum = sum+combinations_count(d.get(A[cnt], 0), 2)
    # for key, value in enumerate(d):
    #     # print(key,value)
    #     sum = sum+combinations_count(d.get(value, 0), 2)
    print(sum)
    d[A[cnt]] = d.get(A[cnt], 0)+1
    # d = {}
    cnt += 1

Submission status: Accepted

Solution code:
import sys
# sys.stdin = open('input.txt')

nCr = {}
def cmb2(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

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


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

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

total = 0

for i in cnt.values():
    if i >= 2:
        total += cmb(i, 2)

ansDic = {}

for i in range(n):
    if a[i] in ansDic.keys():
        print(ansDic[a[i]])
    else:
        x = cnt[a[i]]
        ans = total
        if x >= 2:
            ans = total - cmb(x, 2)
        if x-1 >= 2:
            ans += cmb(x-1, 2)
        ansDic[a[i]] = ans
        print(ans)

Submission status: Accepted

Solution code:
import collections
c = {0: 0, 1: 0, 2: 1}
n = int(input())
sum = 0
for i in range(3, n + 1):
    c[i] = c[i - 1] * i // (i - 2)
cc = collections.defaultdict(int)
a = list(map(int, input().split()))
for i in a:
    cc[i] += 1
for k, v in cc.items():
    sum += c[v]
# print (c)
# print (cc)
# print (sum)
for i in range(n):
    v = cc[a[i]]
    # print (v)
    # print(c[v], c[v - 1])
    print (sum - c[v] + c[v - 1])

Submission status: Accepted

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

aa = [0 for i in range(2*10**5)]
for i in range(n):
  aa[a[i]] += 1
 
ans = 0
for key in range(2*10**5):
  ans += aa[key]*(aa[key]-1)//2
    
for i in range(n):
  tmp1 = aa[a[i]]*(aa[a[i]]-1)//2
  aa[a[i]] -= 1
  tmp2 = aa[a[i]]*(aa[a[i]]-1)//2
  aa[a[i]] += 1
  print(ans-tmp1 + tmp2)

Submission status: Accepted

Solution code:
import collections
import math

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

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

c = collections.Counter(A)
#print(c)

d = {}
csum = 0
for i,j in c.items():
    if j == 1:
        d[i] = 0
        csum += 0
    else:
        d[i] =  combinations_count2(j)
        csum += d[i]

#print(c)
#print(d)
#print(csum)

#print(A)
for i in A:
    #print(c[i], i)
    if c[i] == 1:
        print(csum)
    elif c[i] == 2:
        print(csum-1)
    else:
        print(csum - combinations_count2(c[i]) + combinations_count2(c[i]-1))

Submission status: Accepted

Solution code:
import collections
import numpy as np

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

c=collections.Counter(A)
kazu=list(c.values())
a=np.array(kazu)
sn=a*(a-1)/2
s=sum(sn)
for k in range(N):
    sub=c[A[k]]-1
    print(int(s-sub))

Submission status: Accepted

Solution code:
#%%
import copy, math, collections

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

result = []

num_dict = dict(collections.Counter(a))

def calc(x):
    if x >= 2:
        return math.factorial(x) / (math.factorial(x - 2) * 2)
    else:
        return 0

result_dict = {}
result_minus1_dict = {}

for key, value in num_dict.items():
    result_dict[key] = calc(num_dict[key])
    result_minus1_dict[key] = calc(num_dict[key] - 1)

result_all = sum(result_dict.values())

for i in range(len(a)):
    print(int(result_all - result_dict[a[i]] + result_minus1_dict[a[i]]))

# %%

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
A = input().split()
a = Counter(A)

ans_all = 0
for x in a.keys():
    m = a[x]
    ans_all += m * (m - 1) // 2

for x in A:
    m = a[x] - 1
    print(ans_all - m)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
cnt_list = [0 for i in range(N)]
sum_res = 0

for i in range(N):
  cnt_list[A_list[i] - 1] += 1

for i in range(N):
  sum_res += cnt_list[i] * (cnt_list[i] - 1) // 2

for i in range(N):
  print_res = sum_res
  if cnt_list[A_list[i] - 1] >= 2:
    print_res = sum_res - cnt_list[A_list[i] - 1] * (cnt_list[A_list[i] - 1] - 1) // 2 + (cnt_list[A_list[i] - 1] - 1) * (cnt_list[A_list[i] - 1] - 2) // 2
  
  print(print_res)

Submission status: Accepted

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

a_dict = dict()
for x in a:
    if x not in a_dict:
        a_dict[x] = 1
    else:
        a_dict[x] += 1

sum = 0
for k, v in a_dict.items():
    sum += v*(v-1)//2
    a_dict[k] = (v*(v-1)//2)-((v-1)*(v-2)//2)

for i in a:
    print(int(sum-a_dict[i]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from collections import Counter
from bisect import bisect_left

def f(i, d, dsum, n):
    return func(i, d) + dsum[i] + dsum[n] - dsum[i+1]

def func(i, w):
    return w[i][1] * max(0, w[i][1]-1) // 2

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    d = dict(Counter(v))
    w = []
    for k in sorted(d.keys()):
        w.append([k, d[k]])
    m = len(w)
    c = [0] * (m+1)
    for i in range(1, m+1):
        c[i] = c[i-1] + func(i-1, w)
        # print(func(i-1, w))
    q = [u[0] for u in w]
    # print(c)
    # print(w)
    for i in v:
        g = bisect_left(q, i)
        # print(q)
        # print(i)
        # print('g: ', g)
        w[g][1] -= 1
        print(f(g, w, c, m))
        w[g][1] += 1


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a=int(input())
num=[int(i) for i in input().split()]
num_dict={}
num_min1_dict={}
sum_num=0
for i in num:
    if i not in num_dict:
        num_dict[i]=1
    else:
        num_dict[i]+=1

num_to_sum_dict={}
for key, value in num_dict.items():
    sum_num+=value*(value-1)/2

for key,value in num_dict.items():
    tmp=sum_num-value*(value-1)/2+(value-1)*(value-2)/2
    num_to_sum_dict[key]=tmp

for i in range(a):
    print(int(num_to_sum_dict[num[i]]))

Submission status: Accepted

Solution code:
import collections
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_list = list(map(int, input().split()))
a_col = collections.Counter(a_list)
c_dic = {}
sm = sum([c * (c - 1) for c in a_col.values()]) // 2
for a in a_list:
    if a in c_dic:
        print(c_dic[a])
    else:
        ans = sm - combinations_count(a_col[a], 2) + combinations_count(a_col[a] - 1, 2)
        print(ans)
        c_dic[a] = ans

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
for i in range(N):
  count[A[i] - 1]  += 1
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
MOD=10**9+7

d=Counter(a)
tni=0
dv=d.values()
dv=list(dv)
for i in range(len(dv)):
    tni+=dv[i]*(dv[i]-1)//2

for i in range(n):
    r=d[a[i]]
    print(tni-(r-1))

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
import collections

def comb(a, b):
    result = 1
    for i in range(b):
        result = result * ((a-i)/(b-i))
    return int(result)

N = int(input())
A = list(map(int, input().split()))
all_comb = 0
freq = collections.Counter(A)
for i in freq.values():
    if (i == 1) or (i == 0):
        pass
    else:
        all_comb += comb(i, 2)

for k in range(N):
    result = all_comb
    if freq[A[k]] == 1:
        pass
    else:
        tmp = comb(freq[A[k]], 2)
        result = result - tmp + tmp*((freq[A[k]]-2)/freq[A[k]])
    print(int(result))

Submission status: Accepted

Solution code:
import math

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

lib = [0] * n

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

for i in a:
    lib[i - 1] += 1

count = 0

for i in lib:
    if i >= 2:
        count += comb(i, 2)

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

Submission status: Accepted

Solution code:
from math import factorial
import collections

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

MEMO = {}
def fact(n):
    if MEMO.get(n):
        return MEMO[n]
    r = 2
    if n - r < 0:
        MEMO[n] = 0
        return 0
    MEMO[n] = factorial(n) // factorial(r) // factorial(n - r)
    return MEMO[n]

COUNTS = collections.Counter(BALLS)
TOTAL = sum(fact(n) for n in COUNTS.values())

MEMO2 = {}
def solve(n):
    if MEMO2.get(n):
        return MEMO2[n]

    MEMO2[n] = TOTAL - fact(COUNTS[n]) + fact(COUNTS[n] - 1)
    return MEMO2[n]

for i in range(N):
    print(solve(BALLS[i]))

Submission status: Accepted

Solution code:
from scipy.misc import comb

N = int(input())
A = input().split()

B = {}
for i in range(0, N):
    if not(A[i] in B.keys()):
        B[A[i]] = 1
    else:
        B[A[i]] += 1 

S = 0
for value2 in B.values():
    S = S + comb(int(value2), 2, exact=True)

S0 = 0
for i in range(0, N):
    S0 = S - (comb(int(B[A[i]]), 2, exact=True) - comb(int(B[A[i]])-1, 2, exact=True))
    print(S0)

Submission status: Accepted

Solution code:

import collections

N = int(input())

A = list(map(int, input().split()))
count = collections.Counter(A)
d = dict(count)
total =0
for j in count.items():
    total +=(j[1])*(j[1]-1)/2
    d[j[0]] = (j[1]-1)

for i in A:
    print(int(total - d[i]))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
mylist = np.array(list(map(int, input().split())))
#print(N, mylist)
mydic = {}

mysumpari = 0
for i in mylist:
    if i not in mydic:
        mydic[i] = 1
    else:
        mydic[i] += 1
        mysumpari += 1
#print(mydic, mysumpari)

mysum = 0
for d in mydic:
    if mydic[d] >= 2:
        a = mydic[d]
        b = mydic[d] - 1
        #print(a*b)
        mysum +=  (a*b)//2
    #print(mysum)
    
for i in mylist:
    if mydic[i] >= 2:
        a = mydic[i]
        b = mydic[i] - 1
        
        if b > 1:
            print(mysum - (a*b)//2 + ((a-1)*(b-1))//2)
        else:
            print(mysum - (a*b)//2)
        
        #print(mysum - (a*b)//2)

    else:
        print(mysum)

Submission status: Accepted

Solution code:
import collections
import math

def combinations_count(n, r):
    if n==1 or n==0:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
A = list(map(int, input().split()))
# A = list(range(10**5))
c = collections.Counter(A)

ks=list(c.keys())
vs=list(c.values())
cvs=collections.Counter(vs)
tmp_ans=0
# print('vs',vs)
for k,v in cvs.items():
    # print('kv',k,':',v)
    tmp_ans += v *combinations_count(k, 2)
# print('tmp',tmp_ans)
# tmp_ans = 

# ans_dict={}
# for i,k in enumerate(list(set(vs))):
#     ans = 0
#     for j in range(len(list(set(vs)))):
#         ans += combinations_count(j, 2)

    
#     ans_dict[i] = 

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


# ans_dict={}
# ks=list(c.keys())
# vs=list(c.values())

Submission status: Accepted

Solution code:
n = int(input())
a = [int(c) for c in input().split()]
c = [0 for _ in range(n)]
for i in range(n):
    c[a[i]-1]+=1
count = sum(c[i] * (c[i]-1) / 2 for i in range(n))
for i in range(n):
    pp = c[a[i]-1]
    print(int(count - pp * (pp - 1) / 2 + (pp-1) * (pp-2) / 2))

Submission status: Accepted

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

b = a[:]
b.sort()

c = [[ 0 for _ in range(3)] for _ in range(n+1)]

ct = 1
tmp = b[1]
sum = 0
for j in range(1,n):
  if b[j] == b[j-1]:
    ct += 1
  else :  
    c[tmp][0] = ct
    cm = int(ct*(ct-1)/2)
    c[tmp][1] = cm
    cm1 = int((ct-1)*(ct-2)/2)
    c[tmp][2] = cm1
    sum += cm
    tmp = b[j]
    ct = 1

c[tmp][0] = ct
cm = int(ct*(ct-1)/2)
c[tmp][1] = cm
cm1 = int((ct-1)*(ct-2)/2)
c[tmp][2] = cm1
sum += cm      

for k in a:
  print(sum-c[k][1] + c[k][2])

Submission status: Accepted

Solution code:
n = int(input())
a=[int(i) for i in input().split()]
h = [0]*(n+1)
for i in a:
    h[i]+=1


from operator import mul
from functools import reduce
import copy
hh = copy.deepcopy(h)
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

for index,i in enumerate(hh):

    if i==0 or i==1:
        h[index]=0
        continue
    h[index] = cmb(i,2)

ss = sum(h)

for i,j in zip(a,hh):

    if hh[i]<=2:

        print(ss-h[i])
    else:

        print(ss-h[i]+cmb(hh[i]-1,2))

Submission status: Accepted

Solution code:
k = int(input())
l = list(map(int, input().split()))
import collections
z = []
n = collections.Counter(l)
y = [x* (x -1) / 2 for x in n.values()]
mai = {}
plus = {}
for k in n.keys():
  plus.setdefault(k, ((n[k] - 1) * (n[k]) / 2) )
  mai.setdefault(k, ((n[k] - 1) * (n[k] -2) / 2) )

base = sum(y)
q = [base - (plus[i] - mai[i]) for i in l]
for i in range(len(q)):
  print(int(q[i]))

Submission status: Accepted

Solution code:
from collections import Counter

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

ans_sum = 0

counter = Counter(a_li)

for i in counter.values():
    ans_sum += i*(i - 1) // 2

for a in a_li:
    count = counter[a]
    if count == 1:
        print(ans_sum)
    else:
        print(ans_sum - count*(count - 1) // 2 + (count - 1)*(count - 2) // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
n = int(input())
li = list(map(int,input().split()))
S=Counter(li)
Sum = 0
for k in range(n):
            if S[k+1] > 1:
                Sum += (S[k+1] * (S[k+1] -1)) / 2
            else:
                continue

#k個目が何なのかを考える
for i in range(n):
    if S[li[i]] > 1:
        Sum_2 = Sum
        Sum_2 = Sum_2 - (S[li[i]] * (S[li[i]] -1)) / 2 + ((S[li[i]]-1) * (S[li[i]] -2)) / 2
        print(int(Sum_2))
    else:
        print(int(Sum))

Submission status: Accepted

Solution code:
import copy
import collections
n = int(input())

a_list = list(map(str, input().split()))
c = collections.Counter(a_list)

tmp_list = []

ans = 0
for j in c:
    tmp = c[str(j)]
    #print('tmp')
    #print(tmp)
    ans += (tmp-1)*tmp/2

#print('ttt')
#print(ans)


for k in range(n):
    #ans = 0
    #tmp = c[str(a_list[k])] # a_list[k]の数の個数
    #print('tmp')
    #print(tmp)
    #print(c[str(a_list[k])]-1)
    #ans -= c[str(a_list[k])]-1
    #if str(a_list[k]) == j:
    #    tmp = c[str(a_list[k])]-1
    #else:
    #    tmp = c[str(j)]

    #ans += (tmp-1)*tmp/2

    print(int(ans - (c[str(a_list[k])]-1)))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
sameNumber = collections.Counter(A)
total = 0
for n in sameNumber:
  total += sameNumber[n] * (sameNumber[n]-1)

def count(val):
  if sameNumber[val] > 1:
    return (total -2 * (sameNumber[val]-1)) //2
  return total //2

for n in A:
  print(count(n))

Submission status: Accepted

Solution code:
from collections import defaultdict
n, *D  = [list(map(int, s.split())) for s in open(0)]
D = D[0]
dic = defaultdict(int)
for i in D:
    dic[i] += 1

total_comb = 0
for j in dic:
    if dic[j] >= 2:
        total_comb += dic[j] * (dic[j] - 1) / 2
for k in D:
    if dic[k] >= 2:
        print(int(total_comb - dic[k] + 1))
    else:
        print(int(total_comb))

Submission status: Accepted

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

from collections import Counter
d=Counter(a)
f={}
for k,v in d.items():
    f[k]=v*(v-1)/2

ans={}
s=sum(f.values())
for k in f.keys():
    ans[k]=(d[k]-1)*(d[k]-2)/2 + s- f[k]

for i in range(n):
    print(int(ans[a[i]]))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
count = [0]*(N+1)
for i in range(N):
    count[A[i]] += 1

s = sum([x*(x-1)/2 for x in count])

for k in range(N):
    print(int(s-count[A[k]]+1))

Submission status: Accepted

Solution code:
import collections
import math

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

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

counts = collections.Counter(A_list)
ans = 0
for _, v in counts.items():
    # ans += len(list(itertools.combinations(range(v), 2)))
    if v > 1:
        ans += combinations_count(v, 2)

for k in range(1, N + 1):
    n = counts[A_list[k - 1]]
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

Submission status: Accepted

Solution code:
import collections

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


def main(n: int, a: list):
    counter = collections.Counter(a)
    pairs = {k: v * (v - 1) / 2 for k, v in counter.items()}
    pairs_all = sum(pairs.values())

    for i in range(N):
        k = a[i]
        v = counter[k] - 1
        pair_cnt = pairs_all - pairs[k] + (v * (v - 1) / 2)

        print(int(pair_cnt))


main(N, A)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline


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

    counter = Counter()
    ret = 0
    for num in nums:
        counter[num] += 1
        ret += (counter[num] - 1)

    for num in nums:
        ret -= (counter[num] - 1)
        print(ret)
        ret += (counter[num] - 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections
import math


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

def get_nc2(s):
    if nc2[s] == -1:
        t_nc2 = combinations_count(s, 2)
        nc2[s] = t_nc2
        return t_nc2
    else:
        return nc2[s]


n = int(input())
a = list(map(int, input().split()))
col_a = collections.Counter(a)
res = [-1]*(n+1)
mid = [-1]*(n+1)
mid_sum =0
nc2 = [-1]*(n+1)
nc2[0] = 0
nc2[1] = 0


# print(col_a)
# print("==Create mid table==")
for i in col_a.keys():
    tmp_l = [0]*2
    cnt_i = col_a[i]
    # print("i: " + str(i) + " , cnt_i: " + str(cnt_i))
    if mid[cnt_i] == -1:
        tmp_l[0] = get_nc2(cnt_i)
        tmp_l[1] = get_nc2(cnt_i-1)
        # print("tmp_l: " + str(tmp_l))
        mid[cnt_i] = tmp_l
        mid_sum += tmp_l[0]
    else:
        mid_sum += mid[cnt_i][0]
# print("mid_sum: " + str(mid_sum))

# print("==Create result table==")
for i in col_a.keys():
    cnt_i = col_a[i]
    # print("i: " + str(i) + " , cnt_i: " + str(cnt_i))
    if res[cnt_i] == -1:
        tmp_cnt = mid_sum - mid[cnt_i][0] + mid[cnt_i][1]
        res[cnt_i] = tmp_cnt
        # print("tmp_cnt: " + str(tmp_cnt))

for ai in a:
    print(res[col_a[ai]])

Submission status: Accepted

Solution code:
#!/usr/bin/python

n = int(input())
t = input().split()
a = []
for c in t:
    a.append(int(c))

# hash
h = {}
for v in a:
    if v in h:
        h[v] += 1
    else:
        h[v] = 1
total = 0
for k in h.values():
    if k > 1:
        total += (k*(k-1)//2)

for c in a:
    aa = total
    if h[c] > 1:
        aa -= (h[c]*(h[c]-1)//2)
    if h[c] > 2:
        aa += ((h[c]-1)*(h[c]-2)//2)
    print(aa)

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
arrA = list(map(int,input().split()))

cbdp = [0,0]
cbdp2 = [i*(i-1)//2 for i in range(2,n+1)]
cbdp += cbdp2
# print(cbdp)

ctA = Counter(arrA)
# print(ctA)

totalconb = 0
for v in ctA.values():
    totalconb += cbdp[v]
# print(totalconb)

for a in arrA:
    print(totalconb-cbdp[ctA[a]]+cbdp[ctA[a]-1])

# print(*ans,sep="\n")
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

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

c = collections.Counter(a)
values, counts = zip(*c.most_common())
counts = list(counts)
if counts[-1] == 1:
	k = counts.index(1)
	del counts[k:]


from operator import mul
from functools import reduce

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

from collections import deque
t = deque()
for i in counts:
    t.append(cmb(i,2))

t = list(t)
ans = sum(t)

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

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
a,b,c = [0]*N,[0]*N,[0]*N
for i in As:
  a[i-1] += 1
  b[i-1] = a[i-1]*(a[i-1]-1)//2
  c[i-1] = (a[i-1]-1)*(a[i-1]-2)//2

d = sum(b)
print("\n".join([str(d-b[i-1]+c[i-1]) for i in As]))

Submission status: Accepted

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

import collections

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))
    c = collections.Counter(a_li)
    total = 0
    dict_1 = {}
    dict_2 = {}
    for k, v in c.items():
        if v > 1:
            num = combinations_count(v, 2)
            total += num
            dict_1[k] = num
            if v - 1 > 1:
                dict_2[k] = combinations_count(v-1, 2)
            else:
                dict_2[k] = 0
        else:
            dict_1[k] = 0
            dict_2[k] = 0

    for a in a_li:
        ans = total - dict_1[a] + dict_2[a]
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
def i():return int(sys.stdin.readline().replace("\n",""))
def i2():return map(int,sys.stdin.readline().replace("\n","").split())
def s():return str(sys.stdin.readline().replace("\n",""))
def l():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def strl():return [str(k) for k in sys.stdin.readline().replace("\n","").split()]
def lx():return list(map(lambda x:int(x)*-1,sys.stdin.readline().replace("\n","").split()))
def t():return tuple(map(int,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":pass
n = i()
a = intl()
A = list(set(a))
d = {}
c = 0
def f(c):
    return (c*(c-1))//2
for i in range(n):
    try:
        d[a[i]] += 1
    except KeyError:
        d[a[i]] = 1
for i in A:
    c += f(d[i])
for i in range(n):
    t = d[a[i]]
    if t > 1:
        print(c-t+1)
    else:
        print(c)

Submission status: Accepted

Solution code:
import math
import copy
import numpy as np

N = int(input())
As = input().split()
A = [0 for i in range(N)]
for i in range(N):
  A[i] = int(As[i])

# relabel A's elements
A_unique = list(set(A))
num_unique = len(A_unique)
to_unique = {}
for i,a in enumerate(A_unique):
  to_unique[a] = i
for i in range(N):
  A[i] = to_unique[A[i]]

# count number of each label appearing
counts = [0 for i in range(num_unique)]
for _,A_i in enumerate(A):
  counts[A_i] += 1
answer_total = 0
for count in counts:
  answer_total += int(count*(count-1)/2)

spoiler = [-1 for i in range(num_unique)]
for k in range(N):
  if spoiler[A[k]]<0:
    answer_k = answer_total - (counts[A[k]]-1)
    spoiler[A[k]] = answer_k
  else:
    answer_k = spoiler[A[k]]
  print(answer_k)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=list(map(str,input().split()))
c=collections.Counter(a)
b=0

for i in c.keys():
    b+=(c[i]*(c[i]-1)/2)
for i in a:
    print(int(b-c[i]+1))