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 sys
import numpy as np

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n = I[0]
a = I[1:]

def main():
    c = np.bincount(a, minlength=np.amax(a)+1) 
    tot = np.sum(c * (c - 1) // 2)
    res = tot - (c[a] - 1)
    return res
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

#input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
    n = int(input())
    k = list(map(int,input().split()))
    l = Counter(k)
    ans_ = sum([i*(i-1)//2 for i in l.values()])
    for i_n in range(n):
        d = k[i_n]
        print(ans_ - l[d]*(l[d]-1)//2 + (l[d]-1)*(l[d]-2)//2)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
# import time
#start = time.time()



x=int(input())
a=list(map(int,input().split()))
 
c=collections.Counter(a)
#print(c)

num=0
for i in c.values():
    num += i*(i-1)/2
    
for i in range(x):
    print(int(num-c[a[i]]+1))

Submission status: Accepted

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

c = collections.Counter(a)
ans = 0

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

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

Submission status: Accepted

Solution code:
import collections
_ = int(input())
l = list(map(int, input().split()))
c = collections.Counter(l)
M=0
for v in c.values():
    if v>1:
        M +=  v*(v-1)/2

for k in l:
    print(int(M-c[k]+1))

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    func = lambda x: x * (x - 1) // 2 if x >= 2 else 0
    v = tuple(d.values())
    base = sum(tuple(map(func, v)))

    for k, v in d.items():
        if v <= 1:
            e[k] = base - 0
        else:
            e[k] = base - (v - 1)

    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n = I[0]
a = I[1:]

def main():
    c = np.bincount(a, minlength=np.amax(a)+1) 
    tot = comb(c).sum()
    res = tot - comb(c[a]) + comb(c[a]-1)
    return res
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter


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

# 全体を求める
tmp_all = np.array(A)
unique = np.unique(tmp_all)
# print("unique:", unique)
ans_all = 0
for u in unique:
    count_u = c[u]
    # print(f"u:{u}, count_u:{count_u}")
    if count_u < 2:
        continue
    # (count_u) C 2
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp
# print("all", ans_all)


# kごとのケースを求めて、全体から引く
for i in A:
    print(ans_all - c[i] + 1)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

import collections
c = collections.Counter(a)

# print(n)
# print(a)

# l = [0] * (n + 1)
# ll  = [0] * (n + 1)

l = {}
ll = {}

# for i in range(n):
#     l[a[i]] += 1
#     ll[a[i]] = int((l[a[i]] * (l[a[i]] - 1)) / 2)
    
# print(c)
    
for key, value in c.items():
    l[key] = value
    ll[key] = int((value * (value - 1)) / 2)
    
# print(l)
# print(ll)
    
# print(l)
# print(ll)

c_sum = sum(ll.values())
for i in range(n):
    v = l[a[i]]
    vv = ll[a[i]]
    vvv = int(((v - 1) * (v - 2)) / 2)
    # print(vvv)
    print(c_sum - (vv - vvv))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
A=list(map(int,input().split()))
C=Counter(A)
ans=0
for i in C.values():
    ans+=Comb(i,2)
for i in A:
    print(ans-C[i]+1)

Submission status: Accepted

Solution code:
import math

N=int(input())
A = list(map(int,input().split()))
cnt=[0]*(N+1)
for i in range(0,N):
	cnt[A[i]-1]+=1
ans=0
for i in range(0,N+1):
	if cnt[i]==0:
		ans+=0
	else:
		ans+=cnt[i]*(cnt[i]-1)//2

for i in range(0,N):
	point=A[i]
	deff=(cnt[point-1]*(cnt[point-1]-1))//2-((cnt[point-1]-1)*(cnt[point-1]-2))//2
	print(ans-deff)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
k = [0]*n
for i in range(n):
  k[l[i] - 1] = k[l[i] - 1] + 1
def motomeru(i):
  return int((k[i-1])*(k[i-1]-1)/2)
def herasu(i):
  if k[i-1] == 0 or k[i-1] == 1:
    return 0
  else:
    return int((k[i-1]-1)*(k[i-1]-2)/2)
zenbu = 0
for i in range(n):
  zenbu = zenbu + motomeru(i+1)
for i in range(n):
  a = zenbu - motomeru(l[i]) + herasu(l[i])
  print(a)

Submission status: Accepted

Solution code:
import collections


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

    counter = collections.Counter(A)
    ans = 0

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

    for a in A:
        count = counter[a]
        print(ans - (count * (count - 1) // 2) +
              ((count - 1) * (count - 2) // 2))


resolve()

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    from collections import Counter
    import sys

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

    d = Counter(a)
    e = defaultdict(int)

    func = lambda x: x * (x - 1) // 2 if x >= 2 else 0
    v = list(d.values())
    base = sum(list(map(func, v)))

    for k, v in d.items():
        if v <= 1:
            e[k] = base - 0
        else:
            e[k] = base - (v * (v - 1) // 2 - (v - 1) * (v - 2) // 2)

    p = [str(e[x]) + '\n' for x in a]
    q = ''.join(p)
    print(q)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

def main(N, lst_A):

    dct_cntA = Counter(lst_A)
    sum_comb = 0

    for cntA in dct_cntA.values():
        comb = (cntA * (cntA - 1)) // 2
        sum_comb += comb

    for A in lst_A:
        print(sum_comb - (dct_cntA[A] - 1))

    return 0

if __name__ == "__main__":

    main(N, lst_A)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)

from operator import mul
from functools import reduce
from functools import lru_cache

N = int(input())
A_list = [int(e) for e in input().split()]

#ある番号のボールが何個あるかをまとめる
number_dict = dict()

for i in range(N):
    ball_number = A_list[i]
    if ball_number not in number_dict:
        number_dict[ball_number] = 0
        
    number_dict[ball_number] += 1

#それぞれの番号のボールから2個選ぶ組み合わせをカウントする。
comb_dict = dict()

@lru_cache(maxsize=10000000000000)
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
    
@lru_cache(maxsize=10000000000000)
def two_choice(N):
    if N == 2:
        return 1
    elif N <= 1:
        return 0
    elif N == 3:
        return 3
    else:
        return combinations_count(N,2)

all = 0

for in_dict in number_dict.items():
    ball_number = in_dict[0]
    balls = in_dict[1]
    comb_dict[ball_number] = two_choice(balls)
    all += comb_dict[ball_number]

#回答作成
for i in range(1,N+1):
    ball_number = A_list[i-1]
    now_balls = number_dict[ball_number] - 1
    print(int(all-comb_dict[ball_number]+two_choice(now_balls)))

Submission status: Accepted

Solution code:
import collections
from collections import defaultdict

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

dict = defaultdict(int)
for i in l:
    dict[i]+=1

ans = 0
for v in dict.values():
    ans += int((v*(v-1)) // 2)

for k in l:
    print(ans - (dict[k] - 1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
countdic = Counter(A)
goukei = 0
ans = ''
for key,value in countdic.items():
    goukei += (value*(value-1))//2
for i in A:
    ans = goukei -(countdic[i]*(countdic[i]-1))//2 + ((countdic[i]-1)*(countdic[i]-2))//2
    print(ans)

Submission status: Accepted

Solution code:
import math

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

C = list(set(A))
M = len(C)
t = []
anst = 0
dic = {}
for i in A:
  if not i in dic:
    dic[i] = 0
  dic[i] += 1

jisyo = {}
for i in range(M):
  temp = dic[C[i]]
  jisyo[C[i]] = temp
  #t.append(A.count(C[i]))
  if temp >=2:
    anst += temp*(temp-1)/2

#jisyo = dict(zip(C, t))
#print(jisyo)   
for i in range(N):
  #print(t[C.index(A[i])])
  print(int(anst-jisyo[A[i]]+1))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import factorial

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


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


counts = [0] * (N + 1)
for a in A:
    counts[a] += 1
total = sum(map(lambda n: comb(n, 2), filter(lambda n: n > 1, counts)))

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

Submission status: Accepted

Solution code:
import collections

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

Ad = dict(collections.Counter(A))
# A_set = list(set(A))
# Ad = {a_set:A.count(a_set) for a_set in A_set}
# Ad = {}
# for i in A:
#   if not i in Ad:
#     Ad[i]=0
#   Ad[i] += 1

score_sum = sum([x*(x-1)/2 for x in list(Ad.values())])

for a in A:
    print(int(score_sum - (Ad[a] - 1)))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
S=list(map(int,input().split()))
number=Counter(S)
comb=0
stock={}
for i in list(number.values()):
  comb += i*(i-1)/2
for i in S:
  ans = int(comb - number[i]*(number[i]-1)/2 + (number[i]-1)*(number[i]-2)/2)
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(balls)

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

for b in balls:
    k = c[b]
    print(sum_ball_combinations- (k*(k-1)//2)+ (k-1)*(k-2)//2)

Submission status: Accepted

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

    count = Counter(A)
    total = sum(c * (c - 1) // 2 for c in count.values())

    ans = [total - (count[a] - 1) for a in A]
    return ' '.join(map(str, ans))

print(d_banned_k())

Submission status: Accepted

Solution code:
import collections
import math

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


sumall = 0
for x, y in c.items():
    sumall += (y*(y-1)//2)

for i in range(N):
    print(sumall - c[A[i]] + 1)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
d = {}
for a in A:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1
total = 0
for k in d.keys():
    total += (d[k] * (d[k]-1)) // 2
# print(total)
for i in range(N):
    v = total
    t = A[i]
    v -= ((d[t]) * (d[t]-1)) // 2
    v += ((d[t]-1) * (d[t]-2)) // 2
    print(v)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

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

n, *a = map(int, sys.stdin.read().split())

def main():
    c = Counter(a)
    res = 0
    for v in c.values():
        res += comb(v)

    for i in a:
        yield res - comb(c[i]) + comb(c[i]-1)
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
L = [int(i) for i in input().split()]
C = [0 for i in range(N)]
for i in L:
    C[i-1] += 1
res = 0

for i in range(N):
    res += C[i]*(C[i]-1)//2
# 合計最初に出す
for i in range(N):
    j = L[i]-1
    print(res - C[j]*(C[j]-1)//2 + (C[j]-1)*(C[j]-2)//2)

Submission status: Accepted

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

# A_set = list(set(A))
# Ad = {a_set:A.count(a_set) for a_set in A_set}
Ad = {}
for i in A:
  if not i in Ad:
    Ad[i]=0
  Ad[i] += 1

score_sum = sum([x*(x-1)/2 for x in list(Ad.values())])

for a in A:
    print(int(score_sum - (Ad[a] - 1)))

Submission status: Accepted

Solution code:
# bin/env/pypy3
import sys
import collections
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
c = collections.Counter(a)
#print(n,a,c)
memo_e = {}
s = sum(v*(v-1)//2 for v in c.values() if v > 1)
for e in a:
    if not e in memo_e:
        #c[e] -= 1
        if 1 < c[e]:
            memo_e[e] = s - (c[e]*(c[e]-1)//2 - (c[e]-1)*(c[e]-2)//2)
        else:
            memo_e[e] = s
    print(memo_e[e])

Submission status: Accepted

Solution code:
from math import factorial

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

def comb(n, r):
    if n - r < 0:
        return 0
    else:
        return factorial(n) // (factorial(n - r) * factorial(r))

nums = [0] * (N + 1)
for i, v in enumerate(A):
    nums[v] += 1
combs = list(map(lambda n: comb(n, 2), nums))
total = sum(combs)

for k, val in enumerate(A):
    print(total - (nums[val] - 1))

Submission status: Accepted

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

parent=dict()
An=set(A)
parecou=0
aaa=Counter(A)
for i in aaa.values():
    parecou+=int((i*(i-1)/2))
"""
for i in An:
    f=A.count(i)
    parent[i]=f
    parecou+=int((f*(f-1)/2))
"""


#print(parecou)
for i in range(N):
    anko=aaa[A[i]]
    print(parecou-int((anko*(anko-1)/2))+int(((anko-2)*(anko-1)/2)))
    #print(parecou-anko+1)

Submission status: Accepted

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

for i in B.values():
  ans += i*(i-1)//2
for a in A:
  k = B[a]
  print(ans - (k*(k-1)//2)+((k-1)*(k-2)//2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
C = [0 for i in range(N)]
for value in A:
    C[value - 1] += 1
ways_all = 0
for i in range(N):
    ways_all += (C[i] * (C[i] - 1)) // 2
for value in A:
    print(ways_all - C[value - 1] + 1)

Submission status: Accepted

Solution code:
import numpy as np
import collections

n = int(input())
A = list(map(int, input().strip().split()))
C = collections.Counter(A)
#print(C)
s = 0
for v in C.values():
    s += v * (v-1) // 2
#print(s)
#print()
for i in range(n):
    print(s - C[A[i]] + 1)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

a = np.array(list(map(int, input().split())))
d = [0] * (n+1)
s = {}

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

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

Submission status: Accepted

Solution code:
import collections

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

cnt = collections.Counter(As)
sm = 0 
#print(cnt)

for k in cnt:
  sm += cnt[k]*(cnt[k]-1)//2
  
for i in range(N):
  print(sm -(cnt[As[i]]-1))

Submission status: Accepted

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())

b=Counter(a)

ans=sum(v*(v-1)//2 for k,v in b.items())

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

Submission status: Accepted

Solution code:
import sys
import itertools
from collections import Counter
sys.setrecursionlimit(10**8)
if __name__ == "__main__":
    N = int(input())
    A = input().split()
    counter = Counter(A)
    result = 0
    for c in counter.values():
        result += c*(c-1)//2
    for a in A:
        k = counter[a]
        print(result - k*(k-1)//2 + (k-1)*(k-2)//2)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

a = np.array(list(map(int, input().split())))
d = [0] * (n+1)
s = {}

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

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

Submission status: Accepted

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

from math import factorial
from itertools import combinations
from collections import Counter
from copy import deepcopy
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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_c = Counter(a)
    a_c_sum = 0
    for c in a_c:
        if a_c[c] >= 2:
            a_c_sum += cmb(a_c[c], 2)

    for ea in a:
        if a_c[ea] <= 1:
            sa = 0
        elif a_c[ea] == 2:
            sa = 1
        else:
            sa = cmb(a_c[ea], 2) - cmb(a_c[ea] - 1, 2)
        print(a_c_sum - sa)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def solve(N: int, A: "List[int]"):
    if len(set(A))==len(A):
        for i in range(N):
            print(0)
        exit()
    q=0
    p=cs.Counter(A)
    for o in p.values():
        q+=o*(o-1)//2
    for i in range(N):
        print(q-p[A[i]]+1)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

c=collections.Counter(a)
num=0
for i in c.values():
    num+=i*(i-1)/2
    
for i in range(x):
    print(int(num-c[a[i]]+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict


def num_combinations(_n):
    return _n * (_n - 1) // 2


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)

    for n in xs:
        freq[n] += 1

    total = sum([num_combinations(n) for n in freq.values()])
    for x in xs:
        print(total - (freq[x] - 1))

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

n = int(input())
a = [[] for i in range(n)]

a = [int(i) for i in input().split()]
b = [0]*(n+1)

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

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

def ans(N):
    if N >= 2:
        return(cmb(N,2))
    else: return(0)

cnt=0
for j in b: cnt += ans(j)
for i in a:
    temp = cnt
    temp = temp - (ans(b[i])- ans(b[i]-1))
    print(temp)

Submission status: Accepted

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

dct = {}
for i in A:
  if not i in dct:
    dct[i]=0
  dct[i] += 1

anst=0
for (i,cnt) in dct.items():
  if cnt >= 2:
    anst += cnt * (cnt-1)/2

for i in A:
  print(int(anst-dct[i]+1))

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter(A)
    sum_ = sum([v*(v-1)/2 for v in count.values()])

    for i in range(len(A)):
        took_count = count[A[i]]
        sub = took_count - 1
        print(int(sum_ - sub))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
from collections import deque
import numpy as np

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

ca = collections.Counter(A)
cakeys = list(ca.keys())
cavalues = np.array(list(ca.values()))

#print(cakeys)
#print(np.array(list(ca.values())))

cavaluesmax = cavalues*(cavalues-1)//2
valuesum = np.sum(cavaluesmax)
#cavaluesmin = (cavalues-1)*(cavalues-2)//2


for a in A:
    #print(cakeys[index])
    v = ca[a]
    ans = valuesum - v + 1
    #ans = cavaluesmin[index].sum() + cavaluesmax[:index].sum() + cavaluesmax[index+1:].sum()
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
D={}
x=0
for a in A:
	try:
		D[a]=D[a]+((D[a]*2)**0.5//1)+1
	except:
		D[a]=0

y=sum(D.values())
for i in range(len(A)):
	x=0
	z=((D[A[i]]*2)**0.5)//1
	x=y-z
	print(int(x))

Submission status: Accepted

Solution code:

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

    # print(N)
    # print(A)
    d = {i:0 for i in range(1,N+1)}
    d2 = {}
    d3 = {}
    # print(d)
    # return


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

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

    # print(d2)

    for i in range(1, N+1):
        if d[i] == 0: d3[i] = 0
        else: d3[i] = int((d[i]-1) * (d[i]-2) / 2)

    # print(d3)


    for i in A:
        print(sum - d2[i] + d3[i])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math

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

# def findsame(aa):
#     s = 0
#     for a in aa:
#         s += (len([x for x in aa if x == a]) - 1)
#     return int(s / 2)

# for k in range(len(aa) - 1):
#     print(findsame(aa[:k] + aa[k+1:]))
# print(findsame(aa[:len(aa)-1]))

# aaa = dict()
# for a in aa:
#     if a not in aaa:
#         aaa[a] = len([x for x in aa if x == a])

# sum = 0
# for k in aaa:
#     sum += int(aaa[k] * (aaa[k] - 1) / 2)

# for a in aa:
#     print(sum - (aaa[a] - 1))

aaa = dict()
for a in aa:
    if a not in aaa:
        aaa[a] = 1
    else:
        aaa[a] += 1

sum = 0
for k in aaa:
    sum += int(aaa[k] * (aaa[k] - 1) / 2)

for a in aa:
    print(sum - (aaa[a] - 1))

Submission status: Accepted

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

a = list(set(A))
a_c = {}
for v in A:
  if v in a_c:
    a_c[v] += 1
  else:
    a_c[v] = 1

def cmb(n):
  if n < 2: return 0
  else:
    return n * (n-1)//2

S = 0
for k in a_c:
  S += cmb(a_c[k])

for i in range(N):
  print(S - cmb(a_c[A[i]]) + cmb(a_c[A[i]]-1))

Submission status: Accepted

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

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

s = 0
for k in d:
    s += d[k] * (d[k] - 1) // 2

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

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
lin = [0]*(n+1)
for i in li:
    lin[i] += 1
liv = [0]*(n+1)
point = 0
for j,k in enumerate(lin):
    point += k*(k-1)//2
    liv[j] += k*(k-1)//2
for i in li:
    print(point-liv[i]+(lin[i]-1)*(lin[i]-2)//2)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    C = collections.Counter(A)
    S = sum([int(i * (i-1)) / 2 for i in C.values()])
    for k in range(len(A)):
        c = C[A[k]]
        print(int(S - (c * (c-1) / 2) + ((c-1) * (c-2) / 2)))
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
import math

N = int(input())
Al = list(map(int, input().split()))
d_di = {}
#d_in = {}
counter = Counter(Al)
As_l = list(set(Al))
for n in As_l:
    cnt = int(counter[n])
    if cnt > 1:
        d_di[n] = (math.factorial(cnt) // (2 * math.factorial(cnt - 2)))
    else:
        pass
#    if cnt > 2:
#        d_in[n] = (math.factorial(cnt-1) // (2 * math.factorial(cnt - 3)))
#    else:
#        d_in[n] = 1
# print(d_di)
# print(d_in)
total = sum(v for v in d_di.values())
for k in Al:
    ans = total - (counter[k]-1)
    if ans < 0:
        ans = 0
    else:
        pass
    print(ans)

Submission status: Accepted

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_sort = sorted(a)

    ball_dict = {}
    count = 1
    for i in range(1, n):
        if a_sort[i] == a_sort[i - 1]:
            count += 1
        else:
            ball_dict.setdefault(a_sort[i - 1], count)
            count = 1
    ball_dict.setdefault(a_sort[n - 1], count)

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

    ans_dict = {}
    for v in ball_dict.values():
        if v not in ans_dict:
            ans_dict.setdefault(v, ans - (v - 1))

    for i in range(n):
        print(ans_dict[ball_dict[a[i]]])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for a in A:
    k = B[a]
    print(ans - (k*(k-1)//2)+((k-1)*(k-2)//2))

Submission status: Accepted

Solution code:
import collections
import math

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


N=input()
A = list(map(int, input().split()))
c = collections.Counter(A)
#print(c.values())

ans = sum([comb(v, 2) for v in c.values()])
for ai in A:
    print(ans - c[ai] + 1)

Submission status: Accepted

Solution code:
import collections
def resolve():
    N = int(input())
    A = tuple(map(int, input().split()))
    C = collections.Counter(A)
    ans = 0
    for i in C.values():
        ans += i * (i-1) // 2

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
 
count = [0]*(N+1)
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)
 
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()))

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

allct = 0
for i in range(N):
    allct += ct[i]*(ct[i]-1)//2

for i in range(N):
    print(allct - ct[A[i]] + 1)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
 
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)
 
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()))
数え=[0]*N
for i in A:
  数え[i-1]+=1
前者=0
for i in 数え:
  前者+=i*(i-1)//2
for i in A:
  後者=数え[i-1]-1
  print(前者-後者)

Submission status: Accepted

Solution code:
import collections

N = int(input())
num_list = list(map(int,input().split()))
memo = [0]*(N+1)
memo_flg = [False]*(N+1)
counter = [0] * (N+1)
counter_ans = [0] * (N+1)

for i in num_list:
    counter[i] += 1
for i in range(N+1):
    counter_ans[i] = (counter[i]*(counter[i]-1))//2

all_sum = sum(counter_ans)
def calc(k):
    num = num_list[k]
    
    if(memo_flg[num] is True):
        return memo[num]
    ans = 0
    
    wrong = counter_ans[num]
    c = counter[num]-1
    right = (c*(c-1))//2
    
    ans = all_sum - wrong + right
    memo[num] = ans
    memo_flg[num] = True
    return ans

[print(calc(i)) for i in range(N)]

Submission status: Accepted

Solution code:
import collections
import math


n = int(input())
aa = list(map(int, input().split()))
ls = [0] * (n+1)

c = collections.Counter(aa)

for i, j in c.items():
    ls[i] = j*(j-1)//2

total = sum(ls)

for i in aa:
    before = (c[i]-1)*(c[i]-2) // 2
    print(total - ls[i] + before)

Submission status: Accepted

Solution code:
from collections import Counter

def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
A=list(map(int,input().split()))
C=Counter()
C.update(A)
ans=0
L=[0]*N
for i in range(N):
    L[A[i]-1]+=1
Most=C.most_common()
#print(Most)
for i in range(len(C)):
    ans+=Comb(Most[i][1],2)
for i in range(N):
    pri=ans
    pri=ans-L[A[i]-1]+1
    print(pri)