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:
n=int(input())
a=list(map(int,input().split()))
x=[0]*(n+1)

for i in a:
    x[i]+=1
s=sum([x[i]*(x[i]-1)//2 for i in range(1,n+1)])


for i in range(n):
    k=a[i]
    p=x[k]*(x[k]-1)//2
    q=(x[k]-1)*(x[k]-2)//2
    print(s-p+q)

Submission status: Accepted

Solution code:
from collections import Counter


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


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    s = sum([cmb(x) for x in c.values()])
    for i in a:
        print(s - c[i] + 1)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def inputs(): return [int(x) for x in input().split()]
from collections import defaultdict
n= int(input())
a = inputs()
#a1 = sorted(a)
ans = 0
l = defaultdict(list)
l1 = defaultdict(int)
for i in range(n):
    l1[a[i]]+=1
for i in l1.keys():
    ans+=(l1[i]*(l1[i]-1))//2
    if 1<l1[i]:
        l[i].append([(l1[i]*(l1[i]-1))//2,((l1[i]-2)*(l1[i]-1))//2])
    else:
        l[i].append([(l1[i]*(l1[i]-1))//2,0])
for i in a:
    #print(l[i])
    f,s = l[i][0]
    print(ans-f+s)

Submission status: Accepted

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

s = 0
for c in count:
    s += c * (c - 1) // 2

for a in A:
    c = count[a]
    x = c * (c - 1) // 2
    c -= 1
    y = c * (c - 1) // 2

    print(s - x + y)

Submission status: Accepted

Solution code:
def comb(n,r):
    if (n<r): return 0
    a,b=1,1
    r = min(r,n-r)
    for i in range(r):
        a = a*(n-i)
        b = b*(r-i)
    return a // b

from collections import Counter

n=int(input())
a=list(map(int,input().split()))
count_a = Counter(a)
ans = 0
for x,y in count_a.items():
    ans += comb(y,2)

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

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
AC=Counter(A)
goukei=0
for i, e in AC.items():
    goukei+=(e*(e-1))//2
for a in A:
    p=AC[a]
    print(goukei - (p*(p-1))//2 + ((p-1)*(p-2))//2)

Submission status: Accepted

Solution code:
#create date: 2020-03-22 21:11

import sys
stdin = sys.stdin
from collections import Counter

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    c = Counter(a)
    s = 0
    for m in c.values():
        s += m*(m-1)//2
    for ai in a:
        print(s-(c[ai]-1))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

s = sum([f(c) for c in C])
for i in range(N):
	a = A[i]
	c = C[a]
	print(s - f(c) + f(c - 1))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
d={};an=0
for i in l:
  d[i]=d.get(i,0)+1
for i in d.values():
  an+=i*(i-1)//2
for j in l:
  i=d[j];print(an+(i-1)*(i-2)//2-i*(i-1)//2)

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    A = list(map(int, input().split()))
    count = dict()
    for v in A:
        count[v] = count.get(v, 0) + 1
    tot = 0
    for v in count:
        tot += count[v] * (count[v] - 1) // 2

    for k in range(N):
        v = A[k]
        u = tot - count[v] * (count[v] -1) // 2
        u += (count[v]-1) * (count[v] - 2) // 2
        print(u)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt = [0]*(n+1)
for i in a:
  cnt[i] += 1
ans = 0
C = [[0,0] for i in range(n+1)]
for i in range(n+1):
  if cnt[i] <= 1:
    continue
  C[i][0] = cnt[i]*(cnt[i]-1)//2
  ans += cnt[i]*(cnt[i]-1)//2
  if cnt[i] > 2:
    C[i][1] = (cnt[i]-1)*(cnt[i]-2)//2
for i in range(n):
  print(ans-C[a[i]][0]+C[a[i]][1])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = {}
for i in range(n):
  if a[i] in d.keys():
    d[a[i]] += 1
  else:
    d[a[i]] = 1
s = 0
for x in d.values():
  s += x*(x-1)//2
for i in range(n):
  print(s - d[a[i]] + 1)

Submission status: Accepted

Solution code:
def examA():
    N, M = LI()
    ans = N*(N-1)//2 + M*(M-1)//2
    print(ans)
    return

def examB():
    def judge(A):
        #print(A)
        n = len(A)
        for i in range(n//2):
            if A[i]!=A[n-i-1]:
                return False
        return True
    S = SI()
    N = len(S)
    if not judge(S):
        print("No")
        return
    if judge(S[:(N-1)//2]) and judge(S[(N+1)//2:]):
        print("Yes")
    else:
        print("No")
    return

def examC():
    L = I()
    ans = (L/3)**3
    print(ans)
    return

def examD():
    N = I()
    A = LI()
    D = defaultdict(bool)
    C = Counter(A)
    M = 0
    for key,c in C.items():
        M += c*(c-1)//2
    #print(C)
    #print(M)
    ans = [0]*N
    for i in range(N):
        a = A[i]
        c = C[a]
        ans[i] = M -(c*(c-1)//2) + ((c-1)*(c-2)//2)
    for v in ans:
        print(v)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD()

"""

"""

Submission status: Accepted

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


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

b = [0 for i in range(n+1)]
for x in a:
    b[x]+=1

total = 0
for i in range(1,n+1):
    if b[i]>=2:
        total += combinations_count(b[i],2)

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

for i in range(N):
    print(s - max(0,C[A[i]]-1))

Submission status: Accepted

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    C = collections.Counter(A)

    ans = 0
    for v in C.keys():
        ans += C[v] * (C[v] - 1) // 2

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
l = inputIL()

count = [0]*(N+1)

for i in l:
  count[i] += 1

res = 0
for i in count:
  res += i*(i-1)//2

for i in l:
  print(res-(count[i]-1))

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    aDict = dict()
    for i, a in enumerate(A):
        if a in aDict: aDict[a] += 1
        else: aDict[a] = 1
    indivisual = dict()
    total = 0
    for key in aDict:
        n = aDict[key]
        total += n * (n - 1) // 2
        indivisual[key] = n * (n - 1) // 2
    for i, a in enumerate(A):
        ans = total - indivisual[a]
        n = aDict[a]
        ans += (n-1) * (n-2) // 2
        print(ans)


    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split(" ")]
ma = max(a)
b = [0 for i in range(ma)]
for i in a:
    b[i-1] += 1
c = [i*(i-1)//2 for i in b]
s = sum(c)
for i in a:
    if c[i-1] != 0:
        print(s-(b[i-1]-1))
    else:
        print(s)

Submission status: Accepted

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

ac = Counter(a)
al = 0
for k in ac:
    al+=ac[k]*(ac[k]-1)//2
for i in range(n):
    ack = ac[a[i]]
    al2 = al-ack*(ack-1)//2
    al2 += (ack-2)*(ack-1)//2
    print(al2)

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)
tmp = 0
for key in C.keys():
    if C[key] >= 2:
        tmp += (C[key] * (C[key]-1))//2

for a in A:
    if C[a] <= 1:
        print(tmp)
    else:
        print(tmp - (C[a] * (C[a]-1))//2 + ((C[a]-1) * (C[a]-2))//2)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    d = {}
    for i in A:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    rt = 0
    for i in d:
        rt += (d[i] * (d[i]-1))//2
    for i in A:
        if d[i] == 1:
            print(rt)
        else:
            print(rt - ((d[i] * (d[i]-1))//2) + (((d[i]-1) * (d[i]-2))//2))
main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
com = {}
ans = 0
for k, v in c.items():
  ans += v*(v-1)//2 

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
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 fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()

    ex_comb =  defaultdict(int)
    all_comb = 0
    cnt = defaultdict(int)
    for a in A:
        cnt[a] += 1
    for a,c in cnt.items():
        val = c*(c-1)//2
        all_comb += val
        ex_comb[a] = val
    for a in A:
        whole = all_comb - ex_comb[a]
        whole += (cnt[a]-2)*(cnt[a]-1)//2
        print(whole)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
nums=[0]*(N+1)

for i in range(N):
  nums[A[i]]+=1
  
tmp=0
for i in range(1, N+1):
  tmp+=nums[i]*(nums[i]-1)//2
  
for i in range(N):
  print(tmp-max(0, nums[A[i]]-1))

Submission status: Accepted

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

lst = [0] *(N + 1)
for a in A:
    lst[a] += 1

ans = 0
for tmp in lst:
    ans += (tmp * (tmp - 1)) // 2

# print (lst)
for a in A:
    print (ans - lst[a] * (lst[a] - 1) // 2 + (lst[a] - 1) * (lst[a] - 2) // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)

ans = 0

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

for x in a:
    if c[x] == 1:
        print(ans)
    else:
        z = c[x]
        print(ans - z*(z-1)//2 + (z-1)*(z-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(As)
B = sum([k*(k-1)//2 for k in cnt.values()])
#print('B:', B)

anss = []
for A in As:
    x = cnt[A]
    ans = B - x*(x-1)//2 + (x-1)*(x-2)//2
    anss.append(ans)

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

Submission status: Accepted

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

mydict = {}

for num in nums:
    if not num in mydict:
        mydict[num] = 1
    else:
        mydict[num] += 1

# print(mydict)

all_sum = 0
for key, value in mydict.items():
    all_sum += value * (value - 1)// 2

for num in nums:
    print(all_sum - (mydict[num] - 1))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

ans = 0

for i in range(n+1):
    ans += cnt[i]*(cnt[i]-1)//2

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

Submission status: Accepted

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

count = [0] * (n+1)
for x in a:
    count[x] += 1

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

s = 0
for x in count:
    s += comb(x)

for x in a:
    print(s - comb(count[x]) + comb(count[x] - 1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
D = {}
for a in A:
  if a not in D: D[a]=0
  D[a]+=1

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

for a in A:
  n = D[a]
  print(S - n*(n-1)//2 +(n-1)*(n-2)//2)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
num=[0]*n
for i in a_list:
    num[i-1]+=1
c=0
for i in num:
    c+=i*(i-1)//2
for i in range(n):
    x=a_list[i]
    ans=c-num[x-1]+1
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(i) for i in input().split()]
from collections import Counter
d=Counter(A)
sum_ = 0
for i in d.values():
    sum_ += i*(i-1)//2
for a in A:
    print(sum_ -(d[a] -1))

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(N):
  if a[i] not in dic.keys():
    dic[a[i]]=1
  else:
    dic[a[i]]+=1
tmp=1
for v in dic.keys():
  cnt=dic[v]
  if cnt>=2:
    tmp+=(cnt*(cnt-1)//2)
for i in range(N):
  print(tmp-dic[a[i]])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    balls = list(map(int,input().split()))
    vals = {}
    for i in balls:
        if i not in vals.keys():
            vals[i] = 0

        vals[i] += 1

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

        
    for i in balls:
        val = vals[i]
        total -= val*(val-1)//2
        val -= 1
        val = max(val,0)
        total += val*(val-1)//2
        print(total)
        total -= val*(val-1)//2
        val += 1
        total += val*(val-1)//2


main()

Submission status: Accepted

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


def comb(n: int, k: int) -> int:
    if n < k or n < 0 or k < 0:
        return 0
    k = min(k, n - k)
    ans = 1
    for i in range(1, k + 1):
        ans *= n - i + 1
        ans //= i
    return ans


d = defaultdict(int)
comb_list = [0] * n
for ai in a:
    d[ai] += 1

ans_sum = 0
for i in d:
    ans_sum += comb(d[i], 2)

for i in range(n):
    ans_i = ans_sum - comb(d[a[i]], 2) + comb(d[a[i]] - 1, 2)
    print(ans_i)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    from collections import Counter

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

    all_sum = 0
    for value in c.values():
        all_sum += int(value*(value-1)/2)

    for a in aa:
        print(all_sum-c[a]+1)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    t = sum(v * (v - 1) // 2 for v in C.values())
    for a in A:
        print(t - (C[a] - 1))

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=[0]*n
D=[0]*n
for i in range(n):
    b=B[i]
    C[i]=b*(b-1)//2
    if b==0:
        D[i]=0
    else:
        D[i]=(b-1)*(b-2)//2
ans=sum(C)
for i in range(n):
    a=A[i]-1
    ans_=ans-C[a]+D[a]
    print(ans_)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
lists=list(map(int,input().split()))
cnt=0
from collections import Counter
d=dict(Counter(lists))
for value in d.values():
    cnt+=value*(value-1)//2

for i in range(n):
    x=lists[i]
    ans=0
    ans+=cnt
    now=d[x]
    ans-=now*(now-1)//2
    ans+=(now-1)*(now-2)//2
    print(ans)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

tmp = [0]*n
for i in range(n):
    tmp[i] = (cnt[i]*(cnt[i]-1))//2
s = sum(tmp)
for i in range(n):
    print(s - cnt[a[i]-1]+1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    count = {}
    for aa in a:
        if aa in count:
            count[aa] += 1
        else:
            count[aa] = 1
    balls = list(count.items())
    all = 0
    for b in balls:
        all += b[1] * (b[1] - 1) // 2
    for aa in a:
        print(all - count[aa] + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
counter = Counter(A)
total = sum(x*(x-1)//2 for x in counter.values())
for a in A:
    x = counter[a]
    temp = total
    temp -= x * (x-1) // 2
    if x > 2:
        temp += (x-1) * (x-2) // 2
    print(temp)

Submission status: Accepted

Solution code:
from collections import Counter
n,*a=map(int,open(0).read().split())
C=Counter(a)
s=sum(x*(x-1)//2 for x in C.values())
ans=[]
for x in a:
	c=C[x]
	t=s-c*(c-1)//2+(c-1)*(c-2)//2
	ans.append(t)
print(*ans,sep="\n")

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    N = int( input())
    A = list( map( int, input().split()))
    C = Counter(A)
    ans = 0
    ANS = []
    for t in C:
        ans += C[t]*(C[t]-1)//2
    for a in A:
        if C[a] == 1:
            ANS.append(ans)
            continue
        ANS.append( ans - C[a]*(C[a]-1)//2 + (C[a]-1)*(C[a]-2)//2)
    print("\n".join( map( str, ANS)))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = {}
for a in arr:
    if a not in cnt:
        cnt[a] = 1
    else:
        cnt[a] += 1

comb = {}

sums = 0
for key in cnt.keys():
    t1 = cnt[key]*(cnt[key]-1)
    t2 = (cnt[key]-2)*(cnt[key]-1)
    comb[key] = [t1//2,t1//2-t2//2]
    sums += t1//2

for i in range(n):
    print(sums-comb[arr[i]][1])

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
Alist = list(map(int, input().split()))
Ac = Counter(Alist)
kaz_d = {}
for k, v in Ac.items():
    kaz_d[k] = (v * (v-1))//2
s = sum(kaz_d.values())
for a in Alist:
    v = Ac[a]
    print(s - ((v*(v-1)//2)) + (((v-1)*(v-2))//2))

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)

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

for i in range(N):
    d = C[A[i]]
    res = tmp - d * (d - 1) // 2 + (d - 1) * (d - 2) // 2
    print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
table = [0] * (N + 1)
for i in range(N):
  table[a[i]] += 1

ttable = [0] * (N + 1)
for i in range(N + 1): ttable[i] = table[i] * (table[i] - 1) // 2
cs = [0] * (N + 2)
for i in range(N + 1): cs[i + 1] = cs[i] + ttable[i]
#print(ttable)
#print(cs)

for x in a:
  t = cs[x] + (cs[-1] - cs[x + 1])
  print(t + (table[x] - 1) * (table[x] - 2) // 2)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd

dic = dd(int)
N = int(input())
a = list(map(int,input().split()))
for i in range(N):
    dic[str(a[i])] += 1

res = 0
# まずN個出す
for v in dic.values():
    res += v*(v-1)//2

res2 = [0]*N
for i in range(N):
    tmp = dic[str(a[i])]
    print(res-(tmp-1))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    num = [0] * (N+1)
    for a in A:
        num[a] += 1
    S = 0
    for i in range(1, N+1):
        S += (num[i] * (num[i]-1)) // 2
    ans = [0] * (N+1)
    for i in range(1, N+1):
        if num[i]:
            ans[i] = S - (num[i] * (num[i]-1)) // 2 + ((num[i]-2) * (num[i]-1)) // 2
        else:
            ans[i] = S
    for i in range(N):
        print(ans[A[i]])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
raw = [0 for _ in range(3*10**5)]
for x in a:
  raw[x] += 1
ans = 0
for y in raw:
  ans += y * (y-1) // 2
for x in a:
  tmp = int(ans)
  tmp -= raw[x] - 1
  print(tmp)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()
a = l()
dp = [[0,0] for i in range(2*(10**5)+1)]
po = s(sorted(a))

su = 0
for i in range(len(po)):
    dp[po[i][0]] = [po[i][1],po[i][1]*(po[i][1]-1)//2]
    su += dp[po[i][0]][1]



for i in range(n):
    ppp = dp[a[i]][1]
    print(su - ppp + (dp[a[i]][0]-1)*(dp[a[i]][0]-2)//2)

Submission status: Accepted

Solution code:
N = int(input())
d = {}
for i in range(1,N+1):
    d[i] = 0
A = list(map(int,input().split()))
for i in range(N):
    a = A[i]
    d[a] += 1
s = 0
for i in range(1,N+1):
    x = d[i]
    s += (x*(x-1))//2
for i in range(N):
    y = A[i]
    z = s + 1 - d[y]
    print(z)

Submission status: Accepted

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

Submission status: Accepted

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

C = {}
for a in A:
    C[a] = C.get(a,0) + 1

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

for a in A:
    n = C[a]
    ans = ans0 - n*(n-1)//2 + (n-1)*(n-2)//2
    print(ans)

Submission status: Accepted

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

from collections import defaultdict

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

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

sum_cnt = 0
for a in cnt.values():
    sum_cnt += a * (a - 1) // 2
#print(cnt)
#print(sum_cnt)

for a in A:
    cnta = cnt[a]
    print(sum_cnt - cnta * (cnta - 1) // 2 + (cnta - 1) * (cnta - 2) // 2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1
from operator import mul
from functools import reduce
def cmb(n, r):
  if not 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


n = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A)
for i in range(n+1):
  ans += cmb(C[i],2)
for i,ai in enumerate(A):
  print(ans-cmb(C[ai],2) + cmb(C[ai]-1,2))

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
x = [0]*n
for i in a: x[i-1] += 1
c = sum([i*(i-1)//2 for i in x])

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
ac = Counter(a).most_common()
all_sum = sum([x * (x-1) // 2 for _, x in ac])
dic = {i: x for i, x in ac}
for i in range(n):
    print(all_sum - dic[a[i]] + 1)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n = inp()
    a = inpl()
    dic = defaultdict(int)
    for i in range(n):
        dic[a[i]] += 1
    ans = 0
    for e in dic:
        num = dic[e]
        ans += (num*(num-1))//2
    for i in range(n):
        print(ans-(dic[a[i]]*(dic[a[i]]-1))//2+((dic[a[i]]-1)*(dic[a[i]]-2))//2)
    
if __name__ == "__main__":
    main()