Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

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

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

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

Solution code:
import copy
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
total = 0
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
    
for v in c.values():
    if v >= 2:
        total += cmb(v, 2)
# print(total)

for ai in a:
    if c[ai] >= 2:
        # print('Y')
        ans = total - cmb(c[ai], 2)
        if c[ai] - 1 >= 2:
            ans += cmb(c[ai] - 1, 2)
    else:
        ans = total
    print(ans)
    # ans = 0
    # temp = copy.copy(c)
    # temp[ai] = temp[ai] - 1

    # for v in temp.values():
    #     # print(v)
    #     if v >= 2:
    #         ans += cmb(v, 2)
    # print(ans)
# temp = copy.copy(c)
# temp[1] = temp[1] - 1
# print(c)
# print(temp)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys


def input():
    return sys.stdin.readline().strip()


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

acc = 0
dic_l = defaultdict(int)
for i in range(n):
    dic_l[A[i]] += 1
    acc += dic_l[A[i]] - 1

for i in range(n):
    print(acc - dic_l[A[i]] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
import math


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


n = int(input())
a = list(map(int, input().split()))
a_cnt = Counter(a).most_common()
a_cnt_dict = dict(a_cnt)
ans = 0
for i in range(len(a_cnt)):
    if a_cnt[i][1] > 1:
        ans += c_count(a_cnt[i][1], 2)
for i in range(n):
    print(ans - (a_cnt_dict[a[i]] - 1))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#D
import math

n = int(input())
a = list(map(int,input().split()))
numli = [0] * (n+1)
for i in range(n):
    numli[a[i]] += 1
numli2 = numli.copy()
numli2.sort(reverse=True)    
i = 0
ans = 0
while numli2[i] > 1:
    ans += numli2[i] * (numli2[i]-1) / 2
    i += 1

ansli = [0] * n
for k in range(n):
    nb = numli[a[k]]
    anssub = nb * (nb - 1) / 2
    anssub = (nb-1) * (nb - 2) / 2
    ansli[k] = int(ans - (nb - 1))
    print(ansli[k])

Submission status: Accepted

Solution code:
def main():
    # initialize
    _ = int(input())
    nums = [int(n) for n in input().split(' ')]

    hash_table = {}
    for n in nums:
        if n in hash_table.keys():
            hash_table[n] += 1
        else:
            hash_table[n] = 1

    ans_cash = {}
    for n in nums:
        num_n = hash_table[n]
        if num_n in ans_cash.keys():
            ans = ans_cash[num_n]
        else:
            ans = 0
            for k, v in hash_table.items():
                if k == n:
                    v -= 1
                ans += v * (v - 1) / 2 if v > 1 else 0
            ans_cash[num_n] = ans
        print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter,mul
from fractions import gcd
from functools import reduce
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

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=ii()
arr=limii()

cou=Counter(arr)
ans=0
for j in cou.values():
    if j>=2:
        ans+=cmb(j,2)

for i in arr:
    if cou[i]>2:
        print(ans-cmb(cou[i],2)+cmb(cou[i]-1,2))
    elif cou[i]==2:
        print(ans-1)
    else:
        print(ans)

Submission status: Accepted

Solution code:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
'''


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

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

n = int(input())

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

num = [0 for i in range(n+1)]

for i in range(n):
    num[a[i]]+=1
    
ans = 0
anslist = []
for i in range(n+1):
    b = num[i]*(num[i]-1)//2
    anslist.append(b)
    ans += b

for i in range(n):
    print(ans - c2(num[a[i]]) + c2(num[a[i]]-1))

Submission status: Accepted

Solution code:
from collections import Counter

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

Counted_A = Counter(a)

comp_dict = {}
not_dict = {}
for i in Counted_A.items():
    comp_dict[i[0]] = i[1]*(i[1]-1)/2
    not_dict[i[0]] = (i[1]-1)*(i[1]-2)/2

ans = 0
for i in comp_dict.items():
    ans += i[-1]


for i in a:
    temp = ans - comp_dict[i] + not_dict[i]
    print(int(temp))

Submission status: Accepted

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

field = {}
for v in a:
    if not v in field:
        field[v] = 1
    else:
        field[v] += 1

all_ans = 0
for k,v in field.items():
    all_ans += ((v*(v-1))/2)


for i in a:
    ans = all_ans
    if i in field:
        n = field[i]
        ans -= (n*(n-1))/2
        n = n - 1
        ans += (n*(n-1))/2
    
    print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import OrderedDict
N = int(input())
A = list(map(int, input().split()))
# maybe use a dictionary
ball_values = OrderedDict()
for val in A:
  if val not in ball_values:
    ball_values[val] = 1
  else:
    ball_values[val] += 1
# get total pairs of all balls
total_possible_combos = sum(item[1]*(item[1]-1)//2 for item in ball_values.items())
# for each k, take away the loss of pairs due to removing it
k_outputs = [total_possible_combos-(ball_values[val]-1) for val in A]
[print(value) for value in k_outputs]

Submission status: Accepted

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

temp=0

s=[0]*n

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

for i in range(n):
    c=s[i]
    temp=temp+int(c*(c-1)/2)

for k in range(n):
    ch=a[k]
    c2=s[ch-1]
    if c2==0 or c2==1:
        print(temp)
    else:
        ans=temp-int(c2*(c2-1)/2)+int((c2-1)*(c2-2)/2)
        print(ans)

Submission status: Accepted

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

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

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

def calcComb(n):
    if n == 0:
        return 0
    return (n*(n-1)) // (2*1)
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

d = {}
for ai in a:
    if d.get(ai):
        d[ai] += 1
    else:
        d[ai] = 1

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

for ai in a:
    print(t - (d[ai]-1))

Submission status: Accepted

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

cnt_set = {}
total = 0
for a in A:
    if a in cnt_set:
        cnt_set[a] += 1
    else:
        cnt_set[a] = 1

    if cnt_set[a] == 2:
        total += 1
    elif cnt_set[a] > 2:
        total += (cnt_set[a]-1)

for a in A:
    if cnt_set[a] == 2:
        print(total - 1)
    elif cnt_set[a] < 2:
        print(total)
    else:
        print(total - (cnt_set[a]-1))

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
A=list(map(int,input().split()))
c = Counter(A)
keys=c.keys()
ans = 0
for key in keys:
    ans+=c[key]*(c[key]-1)/2
for a in A:
    amin = (c[a]*(c[a]-1)/2) - ((c[a]-1)*(c[a]-2)/2)
    
    
    
    
    print(int(ans-amin))

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    cnt=[0]*n
    for i in range(n):
        cnt[a[i]-1]+=1
    res=0
    for i in range(n):
        if cnt[i]>1:
            res+=cnt[i]*(cnt[i]-1)//2
    ans=[0]*n
    for i in range(n):
        if cnt[i]>=1:
            ans[i]=res-(cnt[i]-1)
    for i in range(n):
        print(ans[a[i]-1])
        
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
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= [int(i.strip()) for i in input().split(' ')]

D =defaultdict(int)

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

S = 0
for i in D.values():
    S += combinations_count(i,2)

result_dic ={}
for i in A:
    a = D[i] - 1
    print(S - a)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
C=[0]*N
for i in range(N):
    C[A[i]-1]+=1
K=0
for i in range(N):
    if(C[i]!=0):
        K+=int(C[i]*(C[i]-1)/2)

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
bag = Counter(a)
label2diff = {}
fullcombi = 0
for label, freq in bag.most_common(n):
    #data.append((label, freq * (freq - 1) // 2 - (freq - 1) * (freq - 2) // 2))
    label2diff[label] = freq - 1
    fullcombi += freq * (freq - 1) // 2
for ai in a:
    print(fullcombi - label2diff[ai])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
lst_a = list(map(int, input().split()))
c = Counter(lst_a)
def comb2(n):
    return n*(n-1)/2
s = sum([comb2(x) for x in c.values()])
for i in lst_a:
    print(int(s - c[i] + 1))

Submission status: Accepted

Solution code:
"""def power(x, y): 
    res = 1
    x=x
    while (y > 0): 
        if ((y & 1) == 1) : 
            res = (res * x) 
        y = y >> 1
        x = (x * x)
    return res""" 
"""def fact(n):
    if(n==0):
        return 1
    if(n==1):
        return 1
    return fact(n-1)+fact(n-2)"""

import math
#import combinations
#t=int(input())
#for _ in range(t):
n=int(input())
arr=list(map(int,input().split()))
#n,m=map(int,input().split())
#count=0
d={}
total=0
ans=0

for i in range(n):
    if arr[i] not in d:
        d[arr[i]]=1
    else:
        d[arr[i]]+=1
for i in d:
    total+=(d[i]*(d[i]-1))//2
for i in range(n):
    ans=total-(d[arr[i]]*(d[arr[i]]-1)//2) +((d[arr[i]]-1)*(d[arr[i]]-2)//2)
    print(ans)

Submission status: Accepted

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

import collections
c = collections.Counter(A)
ans = sum([j*(j-1)//2 for j in c.values()])

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

Submission status: Accepted

Solution code:
import math
import collections


f = math.factorial


def comb(n, r, memo):
    if (n, r) in memo:
        return memo[n, r]
    elif n < r:
        memo[n, r] = 0
        return 0
    else:
        res = memo[n, r] = f(n) // f(r) // f(n - r)
        return res


N = int(input())
A = list(map(int, input().split()))
memo = {}
counts = collections.Counter(A)
total = sum(comb(count, 2, memo) for count in counts.values())
for a in A:
    count = counts[a] - 1
    diff = count * (counts[a] - count + 1) // 2
    print(total - diff)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int, input().split()))
s=set(l)
d=dict()

for x in l:
    d[x]=0
for x in l:
    d[x]+=1


sum = 0
for key in d.keys():
    a = d[key]
    sum += (a*(a - 1)) // 2


for x in l:
    print(sum - d[x] +1)

Submission status: Accepted

Solution code:
from math import factorial
def C(n,r):
  if n>=r:
    return factorial(n) // factorial(r) // factorial(n - r)
  else:
    return 0
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
B=Counter(A)
c=[0]*N
for i in range(1,N+1):
  c[i-1]=C(B[i],2)
  
ans=sum(c)
for i in range(N):
  if B[A[i]]>2:
    print(ans-c[A[i]-1]+c[A[i]-1]*(B[A[i]]-2)//B[A[i]])
  else:
    print(ans-c[A[i]-1])

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
combinations = [0] + [c * (c - 1) // 2 for c in range(1, 1 + max(C.values()))]
_all = sum([combinations[c] for c in C.values()])
for a in A:
    print(_all - combinations[C[a]] + combinations[C[a] - 1])

Submission status: Accepted

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

def syori(k):
    return k*(k-1) // 2


a_count = Counter(a)
a_value = a_count.values()
for j in a_value:
    cnt += syori(j)

for i in a:
    jud = a_count[i]
    print(cnt - (jud-1))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
l = list(map(int, input().split()))
c = Counter(l)

t = int(sum([v * (v-1) / 2 for k, v in c.items() if v > 1]))
for k in range(N):
    c_k = c[l[k]]
    if c_k > 1:
        print(t-c_k+1)
    else:
        print(t)

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
L=list(map(int,input().split()))
M=Counter(L)
Z={}
#print(M)
pft=0
for i,j in M.items():
	Z[i]=(j*(j-1)//2,((j-2)*(j-1)//2))
	pft+=Z[i][0]
#print(Z)
for i in range(N):
	print(pft-Z[L[i]][0]+Z[L[i]][1])

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
d=[0]*n
for i in range(n):
 b[a[i]-1]+=1
for i in range(n):
 c[i]=b[i]*(b[i]-1)//2
 if b[i]==0:
  continue
 else:
  d[i]=(b[i]-1)*(b[i]-2)//2
sumc=sum(c)
for i in range(n):
 print(sumc-c[a[i]-1]+d[a[i]-1])
cnt=0
#print(b,c,d)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = list(set(a))
d = {str(si): 0 for si in s}

for ai in a:
    d[str(ai)] += 1

cd = dict()
for k, v in d.items():
    cd[k] = v*(v-1)//2

sm = sum(list(cd.values()))

for ai in a:
    di = d[str(ai)]
    print(sm - cd[str(ai)] + (di-1)*(di-2)//2)

Submission status: Accepted

Solution code:
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(readline())
A = np.array(read().split(), np.int64)
counter = np.bincount(A)

total = (counter * (counter - 1)).sum() // 2
answer = (total - (counter - 1))[A]

print('\n'.join(answer.astype(str)))

Submission status: Accepted

Solution code:
def corp(i):
    return i*(i-1)//2

N=int(input())
A=list(map(int,input().split()))
box=[0]*N
for i in A:
    box[i-1]+=1
sum=0
for i in box:
    if i != 1:
        sum+=corp(i)
for i in A:
    ans=sum
    if box[i-1] != 1:
        ans=ans-corp(box[i-1])+corp(box[i-1]-1)
    print(ans)

Submission status: Accepted

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

count = [0]*n
calc = [0]*n
for i in a:
    count[i-1] +=1
for i in range(n):
    if count[i] ==0 or count[i] ==1:
        continue
    else:
        calc[i] = int(count[i]*(count[i]-1)/2)
ans = sum(calc)

for i in range(n):
    print(ans- int(count[a[i]-1]*((count[a[i]-1]-1)/2)) + int((count[a[i]-1]-1)*((count[a[i]-1]-2)/2)))

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

n = int(input())		# 文字列の入力  abcdefg...

A =        list( map(int, input().split()) )	# 1 2 3 4 5 ...

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

sumA=0
dicSum = defaultdict(int)
for key in A:
	if dicSum[key] > 0: continue
	cnt = dicCount[key]
	dicSum[key] = (cnt*(cnt-1))//2
	sumA += dicSum[key]


#p(dicCount)
#p(dicSum)

for key in A:
	cnt = dicCount[key]
	
	i = (cnt-1)*(cnt-2)//2
	
	ans = sumA - dicSum[key] + i
	
	print( ans )
	
	#p("key,cnt,i,sumA,dicSum[key],ans, ", key,cnt,i,sumA,dicSum[key],ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
li = list(c.values())

ans = 0

for i in li:
    ans += i*(i-1)//2

num = 0

for i in range(n):
    num = c[a[i]]
    if num == 1:
        print(ans)
    else:
        print(ans-(num-1))

Submission status: Accepted

Solution code:
from collections import Counter

def computeComb(C):
  #print(C)
  total = 0
  sub = {}
  for n in C:
    if C[n] >= 2:
      total += C[n] * (C[n] - 1) // 2
      sub[n] = C[n] - 1
    else:  # C[n] < 2
      sub[n] = 0
  return total, sub

if __name__ == '__main__':
  
  N = int(input())
  A = list(map(int, input().split()))
  C = Counter(A)
  
  total, sub = computeComb(C)
  #print(total, sub)

  for i in range(N):
    print(total - sub[A[i]])

Submission status: Accepted

Solution code:
import collections

n, *a = map(int, open(0).read().split())
c = collections.Counter(a)
ans = sum([ val*(val-1)//2 for val in c.values()])
for i in range(n):
    print(ans - (c[a[i]] - 1))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(ai)

nbanned = {}

for it in c.items():
    nbanned[it[0]] = int(it[1] * (it[1] - 1) * 0.5)

s = sum(nbanned.values())

for num in ai:
    if nbanned[num] == 0 or nbanned[num] == 1:
        t = nbanned[num]
    else:
        u = c[num] - 1
        t = nbanned[num] - int(u * (u - 1) * 0.5) 

    print(s - t)

Submission status: Accepted

Solution code:
n = int(input())
book = {}
array = [int(i) for i in input().split()]

for i in array:
    book[i] = book.get(i, 0) + 1

sum_val = 0
delta = {}
for k, v in book.items():
    t = (v * (v - 1)) // 2 if v > 0 else 0
    sum_val += t
    u = v - 1
    delta[k] = t - (u * (u - 1)) // 2 if u > 0 else 0

for i in range(n):
    print(sum_val - delta[array[i]])

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
dict = defaultdict(int)
ans = 0
for k in range(N):
    a = A[k]
    ans += dict[a]
    dict[a] += 1

for k in range(N):
    a = A[k]
    print(ans - dict[a] + 1)

Submission status: Accepted

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

d={}
for Ai in As:
    if Ai in d.keys():
        d[Ai] += 1
    else:
        d[Ai] = 1

sum=0
for k in d.keys():
    n=d[k]
    sum+=n*(n-1)//2

for Ai in As:
    n=d[Ai]
    print(sum-(n-1))

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    A=list(map(int,readline().split()))

    li=[[0,0] for _ in range(N+1)]
    di=dict()
    for i in range(N):
        if A[i] not in di:
            di[A[i]]=1
        else:
            di[A[i]]+=1
    
    s=0
    for k,v in di.items():
        li[k][0]=v*(v-1)//2
        s+=v*(v-1)//2
        li[k][1]=(v-1)*(v-2)//2

    for i in range(N):
        print(s-li[A[i]][0]+li[A[i]][1])

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().strip())
nums = list(map(int, sys.stdin.readline().strip().split()))
# appear times
d = dict()
for one in nums:
    if one in d:
        d[one] += 1
    else:
        d[one] = 1

def C(a):
    return (a*(a-1)//2 )

accum = 0
for k, v in d.items():
    accum += C(v)

res_history = dict()

for one in nums:
    if one in res_history:
        print(res_history[one])
    else:
        if d[one] == 1:
            print(accum)
        else:
            res = accum 
            delta = C(d[one]) - C(d[one] - 1)
            res -= delta
            res_history[one] = res
            print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, A: "List[int]"):
    from collections import Counter
    import math
    from functools import lru_cache

    @lru_cache(None)
    def combination(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    c = Counter(A)
    
    ta = 0
    for k,v in c.items():
        if v >= 2:
            ta += combination(v, 2)
            
    # dp = [-1] * (N+1)
    ans = []
    for i in range(N):
        t = ta
        if c[A[i]] >= 2:
            t -= combination(c[A[i]], 2)
        if c[A[i]]-1 >= 2:
            t += combination(c[A[i]]-1, 2)
        
        ans.append(t)

 
    for i in ans:
        print(i)
    return



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:
N = int(input())
inp = list(map(lambda x: int(x), input().split(" ")))

counts = {}
for i in range(N):
    counts[inp[i]] = 0
for i in range(N):
    counts[inp[i]] += 1

ret = {}

allsum = 0
for k in counts.keys():
    allsum += counts[k]*(counts[k]-1)//2

for i in range(N):
    print(allsum - counts[inp[i]]+1)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
As = list(map(int, input().split()))
c = Counter(As)
ans = 0
for con in c.most_common():
  if con[1]<2:
    break
  ans += (con[1]*(con[1]-1)/2)
for i in range(N):
  print(int(ans-c[As[i]]+1))

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
X = 0
for a in set(A):
  X += d[a]*(d[a]-1)//2
for a in A:
  print(X-(d[a]-1))

Submission status: Accepted

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

D = {}
for i in A:
    if i not in D:
        D[i] = 1
    else:
        D[i] += 1

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

for i in A:
    print(r - (D[i] - 1))

Submission status: Accepted

Solution code:
from collections import defaultdict


def cmb(n, r):
    if n < r:
        return 0
    a = 1
    for i in range(n, n - r, -1):
        a *= i
    b = 1
    for i in range(1, r + 1):
        b *= i
    return int(a / b)


N = int(input())
AList = [int(i) for i in input().split()]

aDict = defaultdict(int)
for a in AList:
    aDict[a] += 1

ans = 0
for key in aDict:
    ans += cmb(aDict[key], 2)

for a in AList:
    print(ans - aDict[a] + 1)

Submission status: Accepted

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

su = 0
for i in range(n+1):
    su += (balls[i]*(balls[i]-1))//2


for i in range(n):
    bb = balls[b[i]]
    print(su - bb+1)

Submission status: Accepted

Solution code:
def c(N):
    return int(N * (N-1) / 2)

N = int(input())
A = [int(s)-1 for s in input().split()]

cnt = [0] * N
for Ai in A:
    cnt[Ai] = cnt[Ai]+1

cmb = [0] * N
cmb_sum = 0
for i in range(0,N):
    cmb_sum = cmb_sum + c(cnt[i])

for i in range(0,N):
    print (cmb_sum - c(cnt[A[i]]) + c(cnt[A[i]]-1) )

Submission status: Accepted

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

ok=[0]*(N+1)

# print(ok)
for i in range(N):
    ok[A[i]]+=1
# print(ok)

ans=0
sum=0
for i in range(N+1):
    if ok[i]>=2:
        sum+=int(ok[i]*(ok[i]-1)/2)
# print("sum ",sum)
for i in range(N):
    # print(i,A[i])
    if ok[A[i]]<=1:
        ans=sum
    elif ok[A[i]]==2:
        ans=sum-1
    else:
        ans=sum-int(ok[A[i]]*(ok[A[i]]-1)/2)+int((ok[A[i]]-1)*(ok[A[i]]-2)/2)
    print(ans)

Submission status: Accepted

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

aset = set(a)
col = collections.Counter(a)
N = len(aset)
s = 0
for i in range(N):
    k = col[aset.pop()]
    if k>1:
        s += k*(k-1)//2
for i in range(len(a)):
    l = col[a[i]]
    if (l-1)>1:
        c1=l*(l-1)//2
        c2=(l-1)*(l-2)//2
        print(s-c1+c2)
    else:
        c1=l*(l-1)//2        
        print(s-c1)

Submission status: Accepted

Solution code:
def sum_solve(d):
    result = 0
    for k, v in d.items():
        result += v * (v-1) / 2
    return int(result)


def solve(sum_d, d, except_num):
    num = d[except_num]
    result = sum_d - num * (num-1)/2 + (num-1) * (num-2) / 2
    return int(result)


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

d = {}

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

sum_d = sum_solve(d)
# print(sum_d)

for i, a in enumerate(A):
    print(solve(sum_d, d, a))

Submission status: Accepted

Solution code:


n = [0]
a = []

def format_input(filename = None):
	global n
	global a
	if filename == None:
		n = int(input())
		a = list(map(int, input().split()))

	elif filename == '__random__':
		from random import randint as rng
		n = rng(3, 2 * 10**5)
		a = [rng(1, n) for i in range(n)]

def get_answer():
	balls = [0] * (n+1)
	for i in a:
		balls[i] += 1

	selection = [0] * (n+1)
	selection_all = 0
	for j in range(n+1):
		selection[j] = balls[j] * (balls[j] - 1) // 2
		selection_all += selection[j]

	for k in range(n):
		correction = (balls[a[k]] - 1) * (balls[a[k]] - 2) // 2 - selection[a[k]]
		answer = selection_all + correction
		print(answer)

if __name__ == '__main__':
	format_input()

	get_answer()

Submission status: Accepted

Solution code:
def d_banned_k():
    from collections import Counter
    from functools import reduce
    N = int(input())
    A = [int(i) for i in input().split()]

    def comb(n, r):
        numerator = reduce(lambda x, y: x * y, range(n, n - r, -1))
        denominator = reduce(lambda x, y: x * y, range(1, r + 1))
        return numerator // denominator

    count = Counter(A)
    total = sum(comb(c, 2) for c in count.values())

    ans = []
    for a in A:
        v = count[a]
        ans.append(total - comb(v, 2) + comb(v - 1, 2))
    return ' '.join(map(str, ans))

print(d_banned_k())

Submission status: Accepted

Solution code:
n = int(input())

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

dic = {}

for i in l:
    if not i in dic:
        dic[i] = 1
    
    else:
        dic[i] += 1

comb = 0

for v in dic.values():
    comb += int(v*(v-1)/2)

for i in l:
    print(comb-dic[i]+1)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
n = input()
d = dd(int)
n = [int(i) for i in input().split()]
for i in n:
  d[i] += 1

base = 0
for k, v in d.items():
  if v > 1:
    base += v * (v-1)/ 2
    
for i in n:
  tmp = d[i]
  base1 = (tmp) * (tmp-1) /2
  base2 = (tmp - 1) * (tmp-2) /2
  print(int(base - base1 + base2))

Submission status: Accepted

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

d=collections.Counter(a)
#print(d)
ans=0
for v in d.values():
    ans+=v*(v-1)//2
#print(ans)

for i in range(n):
    ans2=ans
    v=d[a[i]]
    ans2-=v*(v-1)//2
    v-=1
    ans2+=v*(v-1)//2
    print(ans2)