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:
from collections import Counter

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

dic1=Counter(list1)

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

sum=0
for key in dic1:
    sum+=choose2(dic1[key])

for i in list1:
    print(int(sum-dic1[i]+1))

Submission status: Accepted

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

value = [0]*n

for i in range(n):
  value[A[i]-1]+=1
 
sum = 0
for i in range(n):
  sum += value[i]*(value[i]-1)//2

for i in A:
  print(sum - value[i-1]+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]


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

C = [0]*(2*10**5+1)

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

ans = sum(i*(i-1)//2 for i in C)

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

Submission status: Accepted

Solution code:
n = int(input())

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

dic = {}

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

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

for i in a:
    answer = s - (((dic[i] - 1) * (dic[i] - (dic[i]-2))) // 2)
    print(answer)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
 
N = int(input())
A = list(map(int,input().split()))
 
def cmb(n, r):
    if n < r:  return 0
    r = min(n - r, r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

dic = {}
for i in range(N):
    if A[i] in dic:  dic[A[i]] += 1
    else:  dic[A[i]] = 1
        
sum_ = 0
for i in dic: sum_ += cmb(dic[i], 2)

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

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

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

dic = {}
ma = max(A)
for i in range(N):
    if A[i] in dic:
        dic[A[i]] += 1
    else:
        dic[A[i]] = 1
        
c = [0 for _ in range(ma + 1)]
for i in dic:
    c[i] = cmb(dic[i], 2)
sum_ = sum(c)

for i in range(N):
    ans = sum_ - c[A[i]] + cmb(dic[A[i]] - 1, 2)
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

d = Counter(a)

dict1 = {}
dict2 = {}
for key in d:
  m = d[key]
  if m == 1:
    dict1[key] = 0
    dict2[key] = 0
  elif m == 2:
    dict1[key] = 1
    dict2[key] = 0
  else:
    dict1[key] = m*(m-1)//2
    dict2[key] = (m-1)*(m-2)//2

s = sum(dict1.values())
for each in a:
  tmp = s - dict1[each] + dict2[each]
  print(tmp)

Submission status: Accepted

Solution code:
from collections import Counter
import time

N = int(input())
line = list(map(int, input().split()))
dic = Counter(line)
nums = 0
for num, cnt in dic.items():
    if cnt > 1:
        nums += (cnt * (cnt - 1)) // 2
for i in line:
    print(nums - (dic[i] - 1))

Submission status: Accepted

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    N=int(input())
    A=list(map(int,input().split()))
    Ac=[0]*(N+1)
    for i in range(N):
        Ac[A[i]]+=1
    sumc=0
    iset=set()
    for i in range(1,N+1):
        if Ac[i]>=2:
            sumc+=Ac[i]*(Ac[i]-1)//2
            iset.add(i)
    for i in range(N):
        v=A[i]
        if v in iset:
            print(sumc-Ac[v]+1)
        else:
            print(sumc)
    
if __name__=="__main__":
    main()

Submission status: Accepted

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

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

m = Counter(A)
s = 0
for x, y in m.items():
    if y != 1:
        s += c(y)
for a in A:
    print(s - (m[a] - 1))

Submission status: Accepted

Solution code:

import collections as c

n = int(input())

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

x = c.Counter(a)

cnt = {}

for k,v in x.items():
    cnt[k] = v*(v-1)//2
    
s = sum(cnt.values())
    
mcnt = {}

for k,v in x.items():
    mcnt[k] = cnt[k]-(v-2)*(v-1)//2
    
for i in a:
    print(s-mcnt[i])

Submission status: Accepted

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

x = list(map(lambda x: x*(x-1)//2 , box))
ans = sum(x)
for j in range(N) :
    print(ans-(box[A[j]] - 1))

Submission status: Accepted

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations

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

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

C=Counter(A)

C1=[0]*(N+1)
C2=[0]*(N+1)

sum = 0
for i in range(0,2*10**5):
    if C[i] > 1:
        n = C[i]
        r = 2
        sum += combinations_count(n,r)

for i in range(1,N+1):
    s=A[i-1]
    n=C[s]-1
    print(sum-n)

Submission status: Accepted

Solution code:
import math
from collections import Counter
import itertools
n = int(input())
al = list(map(int,input().split()))
C_al = Counter(al)

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

for i in al:
    k = C_al[i]
    print(int(all - (k * (k - 1) / 2) + (k - 1) * (k - 2) / 2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import Counter
ctr = Counter(A)
full = sum(v*(v-1)//2 for v in ctr.values())

ans = []
for a in A:
    ans.append(full - ctr[a] + 1)
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from collections import Counter

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

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

for i in A:
    b = ans
    b -= c[i]*(c[i]-1)//2
    b += (c[i]-1)*(c[i]-2)//2
    print(b)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [0]*(n+1)
cnt = 0

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())

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

A = Counter(a)

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

for i in a:
    ans = 0
    before = (A[i]*(A[i]-1))//2
    after = ((A[i]-1)*(A[i]-2))//2
    
    ans = cnt - before + after
    print(ans)

Submission status: Accepted

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

CNT = [0] * (n + 1)
for a in A:
    CNT[a] += 1

x = sum(cnt * (cnt - 1) // 2 for cnt in CNT if cnt)

for a in A:
    print(x - (CNT[a] - 1))

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(A)

all_num = 0
for value in counter.values():
    all_num += value * (value - 1) // 2


for a in A:
    value = counter[a]
    before = value * (value - 1) // 2
    after = (value - 1) * (value -2) // 2
    print(all_num + after - before)

Submission status: Accepted

Solution code:
import math
import collections

N = input()
nums = list(map(int, input().split()))
counter = collections.Counter(nums)
patterns = {}
for k, v in counter.items():
    patterns[k] = math.comb(v, 2)

baseline = sum(v for v in patterns.values())

cache = {}
for n in nums:
    if n in cache:
        print(cache[n])
    else:
        val = baseline - patterns[n] + math.comb(counter[n] - 1, 2)
        cache[n] = val
        print(val)

Submission status: Accepted

Solution code:
from collections import Counter

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

A_counter = Counter(A)
full_score = 0
for k, v in A_counter.items():
    full_score += v*(v-1)//2

for a in A:
    print(full_score - (A_counter[a]-1))

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split()))
l = [0]*(2*10**5+1)
for i in A:
    l[i] += 1

ans = sum(i*(i-1)//2 for i in l)
for i in A:
    print(ans-l[i]+1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = {}
for x in a:
  b.setdefault(x, 0)
  b[x] += 1
ans = 0
for x in b.values():
  ans += x * (x - 1) // 2
for x in a:
  print(ans - b[x] + 1)

Submission status: Accepted

Solution code:
n=int(input())
from collections import defaultdict
import math
a=list(map(int,input().split()))
d=defaultdict(int)
for m in a:
  d[m]+=1
sm=0
for m in d:
  if d[m]>1:
    sm+=d[m]*(d[m]-1)//2
for m in a:
  if d[m]>2:
    print(sm-d[m]+1)
  elif d[m]==2:
    print(sm-1)
  else:
    print(sm)

Submission status: Accepted

Solution code:
import sys
import time
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))
# ------------------------------

N = int(input())
A = inpl()

s = set(A)
table = [0] * (N+1)
for i in A:
    table[i] += 1

comb = 0
for i in s:
    comb += table[i] * (table[i]-1) // 2

for i in A:
    print(comb - (table[i]-1))


# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Accepted

Solution code:
import math
import collections

N = input()
nums = list(map(int, input().split()))
counter = collections.Counter(nums)
patterns = {}
for k, v in counter.items():
    patterns[k] = math.comb(v, 2)

baseline = sum(v for v in patterns.values())

cache = {}
for n in nums:
    if n in cache:
        print(cache[n])
    else:
        val = baseline - patterns[n] + math.comb(counter[n] - 1, 2)
        cache[n] = val
        print(val)

Submission status: Accepted

Solution code:
import math
import collections

N = input()
nums = list(map(int, input().split()))
counter = collections.Counter(nums)
patterns = {}
for k, v in counter.items():
    patterns[k] = math.comb(v, 2)

baseline = sum(v for v in patterns.values())

for n in nums:
    # todo : memo
    print(baseline - patterns[n] + math.comb(counter[n] - 1, 2))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
count = Counter(A)
total = 0

for c in list(count.values()):
    total += c*(c-1)/2

for a in A:
    n = count[a]
    before = n*(n-1)/2
    after = (n-1)*(n-2)/2
    ans = total + after - before
    print(int(ans))

Submission status: Accepted

Solution code:
from scipy.special import comb

N = int(input())
index = list(map(int, input().split()))
val = [0]*max(index)
for i in index:
    val[i-1] += 1
total = 0
for i in val:
    if i >= 2:
        total += comb(i, 2, exact=True)
for i in index:
    if val[i-1] >= 2:
        print(total-(comb(val[i-1], 2, exact=True)-comb(val[i-1]-1, 2, exact=True)))
    else:
        print(total)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
from collections import Counter
def main():
    n = int(input())
    a = tuple(map(int, input().split()))
    ac = Counter(a)
    com_base = {}
    for i in ac:
        com_base[i] = ac[i] * (ac[i] - 1) / 2
    sum_combase = sum(com_base.values())
    for ae in a:
        r = sum_combase - com_base[ae] + ((ac[ae] - 1) * (ac[ae] - 2) / 2)
        print(int(r))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = input()
    a = input().split()
    ans_base = 0
    counter = Counter(a)
    for v in counter.values():
        ans_base += v * (v-1) // 2

    for v in a:
        cnt = counter[v]
        if 2 <= cnt:
            print(ans_base - cnt * (cnt - 1) // 2 + (cnt - 1) * (cnt-2) // 2)
        else:
            print(ans_base)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

from collections import Counter
count = Counter(a)

def choose2(x):
    return int(x*(x-1)/2)
    
total = 0
for i in list(count.values()):
    total += choose2(i)

for i in a:
    s = count[i]
    print(total - choose2(s) + choose2(s-1))

Submission status: Accepted

Solution code:

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

ban = [0] * n

tmp = 0
for i in lis:
    ban[i-1] += 1

for i in ban:
    tmp += i*(i-1)//2
    
for i in lis:
    print(tmp+(1-ban[i-1]))

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

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

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

if len(d) == n:
    for i in range(n):
        print(0)
    exit()

d1 = dict()
for a in d.keys():
    d1[a] = combinations_count(d[a], 2)

d2 = dict()
for a in d.keys():
    d2[a] = combinations_count(d[a] - 1, 2)

ans = []
d3 = dict()
s = sum(d1.values())
for a in A:
    if a not in d3.keys():
        ans.append(s - d1[a] + d2[a])
    else:
        ans.append(d3[a])

for a in ans:
    print(a)

Submission status: Accepted

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

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0
    
for i in range(N):
    lst[A[i]] += 1

c = dict(lst) # c は keyの値である場合の組み合わせの数。
              # c = dict(lst)で異なるオブジェクトとして等価なcを定義
              # c = lst だと、同一のオブジェクトとなってしまう？
c_all = 0
for j in lst:
    c_all += lst[j]*(lst[j]-1)//2

for i in lst:
    c[i] = c_all
    c[i] = c[i] - lst[i]*(lst[i]-1)//2 + (lst[i]-1)*(lst[i]-2)//2
        
for i in range(N):
    print(c[A[i]])

Submission status: Accepted

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

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0
    
for i in range(N):
    lst[A[i]] += 1

llst = dict(lst) # llst は、valueが1のものを消したリスト
for i in lst:
    if lst[i] == 1:
        llst.pop(i)

c = dict(lst) # c は keyの値である場合の組み合わせの数。
              # c = dict(lst)で異なるオブジェクトとして等価なcを定義
              # c = lst だと、同一のオブジェクトとなってしまう？
c_all = 0
for j in llst:
    c_all += llst[j]*(llst[j]-1)//2

# print('c_all:{}'.format(c_all))
for i in lst:
#     print('i:{}'.format(i))
#     print('lst[i]:{}'.format(lst[i]))
    c[i] = c_all
    c[i] = c[i] - lst[i]*(lst[i]-1)//2 + (lst[i]-1)*(lst[i]-2)//2
        
for i in range(N):
    print(c[A[i]])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
dp = [0] * (n+1)

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

ans = 0
for i in dp:
    if i > 1:
        l = list(range(i))
        ans += sum(l)

for i in a:
    if dp[i] > 1:
        print(ans - dp[i] +1)
    else:
        print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

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

A_count = Counter(A)


allsum = 0

for i in (A_count.values()):
    allsum += (i*(i-1))//2


for j in A:
    counter_num = A_count[j]
    difference = (((counter_num)*(counter_num-1))//2) - (((counter_num-1)*(counter_num-2))//2)
    print(allsum - difference)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
a = np.array(a)
counter = np.bincount(a)
total = (counter*(counter-1)).sum()//2
ans = (total - counter + 1)[a]
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
# D - Banned K
N = int(input())
A = list(map(int,input().split()))
lis = [0]*(N+1)
for x in A:
    lis[x] += 1
ans = 0
for x in lis:
    ans += x*(x-1)//2
for k in range(N):
    print(ans-lis[A[k]]+1)

Submission status: Accepted

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

from collections import Counter
count = Counter(A)
 
s = 0
for c in list(count.values()):
    s += c*(c-1)/2

ans = 0
for a in A:
    n = count[a]
    ans = s - n*(n-1)/2 + (n-1)*(n-2)/2
    print(int(ans))

Submission status: Accepted

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

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

    from collections import Counter

    C = Counter(A)

    sum = 0
    for val in list(C.values()):
        sum += val * (val - 1) // 2

    ans = [None] * N
    for i in range(N):
        p = C[A[i]]
        ans[i] = sum - p * (p - 1) // 2 + (p - 1) * (p - 2) // 2

    print('\n'.join(map(str, ans)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

num_list=[0]*n
for i in range(n):
    num_list[a[i]-1]+=1

sm_all=0
for i in range(n):
    k=num_list[i]
    sm_all+=(k*(k-1))//2

for i in range(n):
    print(sm_all-(num_list[a[i]-1]-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(a)

for r in list(c.values()):
    total += (r*(r-1))/2

#print(l)
#print(total)

for i in a:
    score = total - (c[i]-1)
    print(int(score))

Submission status: Accepted

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

nums = [0]*(n+1)
for i in a: nums[i] += 1

t = 0
for i in nums: t += i*(i-1)//2

for i in a: print(t+1-nums[i])

Submission status: Accepted

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

for n in c.items():
  total += n[1]*(n[1]-1)//2

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

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
C=Counter(a)
#print(C)
total=0
for k,v in C.items():
    if v>=2:
        total+=v*(v-1)//2
#print(total)
for i in range(n):
    V=C[a[i]]
    ans=total-(V*(V-1))//2+((V-1)*(V-2))//2
    print(ans)

Submission status: Accepted

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

c = collections.Counter(A)
cc = c.items()

f_ans = 0
for m,n in cc:
  f_ans += n*(n-1)/2

for i in range(N):
  a = A[i]
  b = c[a]
  sa = b*(b-1)/2 - (b-1)*(b-2)/2

  print(int(f_ans - sa))

Submission status: Accepted

Solution code:
def main():
    import collections
    
    N = int(input())
    num_list = [int(i) for i in input().split()]

    count_list = collections.Counter(num_list)
    calc_d = collections.defaultdict(int)
    
    for i in set(count_list.values()):
        calc_d[i] = int(i * (i - 1) / 2 - (i - 1) * (i - 2) / 2)
    
    ans = int(sum([0 if i == 1 else i * (i - 1) / 2 for i in count_list.values()]))

    for i in num_list:
        print(ans - calc_d[count_list[i]])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
balls = list(map(int, input().split()))
 
list_a = [0 for i in range(n)]
 
# 同じ数の個数を導出
for e in balls:
    list_a[e-1] += 1
# 同じ数の組合せを導出    
list_b = list(map(lambda x: x *(x-1) // 2, list_a))
# N個すべての組合せ
c = sum(list_b)

for e in balls:
  print(c - list_a[e-1] + 1)

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    from collections import defaultdict
    input = stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    h = defaultdict(int)
    for x in a:
        h[x] += 1
    ans = 0
    for _, v in h.items():
        ans += v * (v - 1) // 2
    for x in a:
        print(ans - h[x] * (h[x] - 1) // 2 + (h[x] - 1) * (h[x] - 2) // 2)
main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
C=Counter(L)
x=0
for a in C.values():
  x += int(a*(a-1)/2)  
for K in range(N):
  print(x-C[L[K]]+1)

Submission status: Accepted

Solution code:
from collections import Counter as ct
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    a=LIST()
    a_ct=ct(a)
    ans=0
    #print(a_ct)
    for i in a_ct:
        t=a_ct[i]
        #print(a_ct[i])
        if t>=2:
            ans+=(t*(t-1))//2
    
    for i in a:
        t=a_ct[i]
        temp=ans
        if t>=2:
            temp=temp-(t*(t-1))//2+((t-2)*(t-1))//2
        print(temp)
    
if __name__ == '__main__':
    do()

Submission status: Accepted

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

c = Counter(a)
key = c.keys()
comb = 0
for i in key:
    comb += (c[i]) * (c[i]-1) // 2 #禁止されていない状態の組み合わせを調べる

for i in a:
    ans = comb - (c[i]-1) #a[i]が禁止されている状態の組み合わせ
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

a = [0 for i in range(n)]

for ball in balls:
    a[ball-1] += 1

b = list(map(lambda x: x *(x-1) // 2, a))

c = sum(b)

for ball in balls:
    print(c - a[ball-1] + 1)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
 
#N = 200000
#A = [200000]*N
 
count = [0]*(N+2)
for i in range(N):
    id = A[i]
    count[id] = count[id]+1
 
sumN = long(0)
for i in range(N+1):
    sumN = sumN + long(long(count[i]*(count[i]-1))/2)
 
#print answer
for i in range(N):
    id = A[i]
    sumz = sumN - (count[id] - 1)
    print(sumz)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter

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

C = Counter(A)

k = 0
d = defaultdict(int)

for i,j in C.items():
  if j == 1:
    k += 0
    d[i] = 0
  else:
    k += j*(j-1)//2
    d[i] = j-1

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter

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

#要素数の確認
C = Counter(A)

# 総パターンは普遍
k = 0
for i,j in C.items():
  if j == 1:
    k += 0
  else:
    k += j*(j-1)//2
# 自分の数字のパターンがへる
# delta = j(j-1)/2 - (j-1)(j-2)/2 = j - 1
d = defaultdict(int)
for i,j in C.items():
  if j == 1:
    d[i] = 0
  else:
    d[i] = j-1
    
for i in A:
  print(k-d[i])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = list(map(int,input().split()))
c = Counter(s)
ls = c.most_common()
di = [0]*(max(s))
dis = [0]*(max(s))
def combinations_count(n, r): #組み合わせ(nCr)
    import math
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(len(ls)):
    if ls[i][1] > 2:
        di[ls[i][0]-1] = combinations_count(ls[i][1], 2)
        dis[ls[i][0]-1] = combinations_count(ls[i][1]-1, 2)
    elif ls[i][1] == 2:
        di[ls[i][0]-1] = combinations_count(ls[i][1], 2)
mx = sum(di)
for j in range(n):
    a = s[j]
    if di[a-1] != 0:
        print(mx-di[a-1]+dis[a-1])
    else:
        print(mx)

Submission status: Accepted

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

A_count = Counter(A)
combs = {}

ans = 0
for a in A:
    if a not in combs:
        combs[a] = A_count[a]*(A_count[a]-1)//2
        ans += combs[a]
    else:
        pass
for a in A:
    print(ans - combs[a] + combs[a]*(A_count[a]-2)//A_count[a])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
import copy
n, *a = map(int, open(0).read().split())
b = dict(Counter(a))
d = copy.deepcopy(b)
for i in b:
    b[i] = b[i] * (b[i]-1)//2
c = sum(b.values())
for k in a:
    print(c - d[k] + 1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
tempa=Counter(a)
temp=[0]*n
for i in range(n):
  temp[i]=tempa[a[i]]-1
ans=sum(temp)//2
for i in range(n):
  print(ans-temp[i])

Submission status: Accepted

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


def nCr(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())
a = list(map(int, input().split()))

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0)
    else:
        c = nCr(v, 2)
        ac[k] = (v, c)
        ans += c

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + nCr(v[0] - 1, 2))

Submission status: Accepted

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


def nCr(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())
a = list(map(int, input().split()))

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0, 0)
    else:
        c = nCr(v, 2)
        ans += c
        if v - 1 < 2:
            ac[k] = (v, c, 0)
        else:
            ac[k] = (v, c, nCr(v - 1, 2))

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + v[2])

Submission status: Accepted

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


def main():
    # input
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    B = [0] * n

    for a in A:
        B[a - 1] += 1

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

    C = [0] * n
    for i, b in enumerate(B):
        C[i] = total - b + 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    # input
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    B = [0] * n

    for a in A:
        B[a - 1] += 1

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

    C = [0] * n
    for i, b in enumerate(B):
        C[i] = total + (b - 1) * (b - 2) // 2 - (b) * (b - 1) // 2

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = [0]*n

for a in A:
    B[a-1] += 1

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

C = [0]*n
for i,b in enumerate(B):
    if b == 2:
        C[i] = total - 1
    elif b > 2:
         C[i] = total + (b-1)*(b-2)//2 - (b)*(b-1)//2 
    else:
        C[i] = total

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

Submission status: Accepted

Solution code:
from collections import defaultdict
import math
def comb(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

count = defaultdict(int)
for a in A:
    count[a] += 1

combi = defaultdict(int)
for a in count:
    combi[a] = comb(count[a],2)

total = sum(list(combi.values()))
for i in range(n):
    print(total - (count[A[i]] - 1))

Submission status: Accepted

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

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

for a in As:
  cnt[a] += 1

for a in cnt:
  tmp += a*(a-1)//2

for a in As:
  if cnt[a] < 2:
    print(tmp)
  else:
    print(tmp - cnt[a] + 1)

Submission status: Accepted

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

from collections import Counter
c = Counter(a)
combi = {}
for k, v in c.items():
   combi[k] = v*(v-1)//2
sumc = sum(list(combi.values()))

for i in range(n):
   x = c[a[i]] - 1
   ans = sumc - combi[a[i]] + x*(x-1)//2
   print(ans)