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 math import factorial as step
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]] += 1
  else:
    dic[a[i]] = 1

def ncr(n, r):
    if n < 0 or r < 0 or n - r < 0:
        return 0
    return step(n) // (step(r) * step(n-r))

# valueのncrのリストを作っておく
all = sum([ncr(v,2) for v in dic.values()])
for i in range(n):
  print(all-dic[a[i]]+1)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

counter = defaultdict(list)
for i in range(len(nums)):
  counter[nums[i]].append(i)

total = 0
for _,v in counter.items():
  v = len(v)
  total += (v * (v-1)) // 2

for k in range(len(nums)):
  t = len(counter[nums[k]])
  print(total - t*(t-1)//2 + (t-1)*(t-2)//2)

Submission status: Accepted

Solution code:
import math
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
data_cnt=collections.Counter(data)
count=0
ans=collections.Counter()
for i in data_cnt.values():
    count+=calc_combi2(i)
for a,b in data_cnt.items():
    if 2<=b:
        ans[a]=int(count-b+1)
    else:
        ans[a]=int(count)
for i in data:
    print(ans[i])

Submission status: Accepted

Solution code:
import math
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
data_cnt=collections.Counter(data)
count=0
for i in data_cnt.values():
    count+=calc_combi2(i)
for i in data:
    ans=count-data_cnt[i]+1
    print(int(ans))

Submission status: Accepted

Solution code:
import collections
import math

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

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

c = collections.Counter(A)
sum_values = sum(map(nC2, c.values()))

for n in range(N):
    if c[A[n]] == 1:
        print(int(sum_values))
    else:
        print(int(sum_values - (c[A[n]] - 1)))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
alist = list(map(int,input().split()))
aalist = [0]*n
aset = set(alist)
adic = {}
for a in alist:
    if a not in adic:
        adic[a] = 0
    adic[a] += 1
a_counter = list(adic.items())

sigma = 0
for counter in a_counter:
    sigma += counter[1] * (counter[1] - 1) // 2

for i in range(n):
    aalist[i] = adic[alist[i]]-1

anslist = []
for a in aalist:
    anslist.append(sigma - a)

for ans in anslist:
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
from collections import Counter
Acount=Counter(A)
L=[[] for i in range(n)]
s=0
for i in Acount.keys():
    k=Acount[i]
    L[i-1].append(k*(k-1)//2)
    L[i-1].append((k-2)*(k-1)//2)
    s+=k*(k-1)//2
for i in range(n):
    b=A[i]
    print(s-L[b-1][0]+L[b-1][1])

Submission status: Accepted

Solution code:
import collections

def combinations_count(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

if len(c) == 1:
    ans = [combinations_count(n-1, 2)]*n
    print(*ans)
    exit()

for i in c.items():
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c.items():
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
values, counts = zip(*c.most_common())

ans = 0

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

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int,input().split())))
L = [0]*(N+2)
L = np.array(L)

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

LL  = np.multiply(L,L-1)/2

gros = int(sum(LL))

for k in A:
    print(gros +1 - L[k])

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


import collections
def main():
    N = int(input())
    A = list(map(int,input().split()))
    c = collections.Counter(A)
    total = 0
    for j in c.values():
        total += j * (j - 1) / 2
    for i in range(N):
        k = c[A[i]]
        before = k * (k - 1) / 2
        after = (k -1) * (k - 2) / 2
        ans = total - before  + after
        print(int(ans))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
for i in range(N):
    S[L[i]-1]+=1

init=0
for j in range(N):
    tmp=S
    if tmp[j]>=2:
        init+=(tmp[j]*(tmp[j]-1))//2




for i in range(N):
    ans=init
    R=S[L[i]-1]
    if R>=2:
        print(ans-(R-1))
    else:
        print(ans)

Submission status: Accepted

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

s = {}
for x in a:
    if x in s:
        s[x] += 1
    else:
        s[x] = 1

sum = 0
for k in s:
    v = s[k]
    sum += v*(v-1)//2
for x in a:
    print(sum - (s[x]-1))

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial
N=int(input())
A=list(map(int,input().split()))
B=Counter(A)
count=0
def combination(x,y):
    return factorial(x) // (factorial(x - y) * factorial(y))
for key,b in B.items():
    if b>=2:
        count+=combination(b,2)
for a in A:
    ans=count
    ans-=B[a]-1
    print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

counter = [0 for _ in range(n + 1)]
for i in a:
    counter[i] += 1

total = 0 
diff = [0 for _ in range(n + 1)]
for i, c in enumerate(counter):
    x = c * (c - 1) // 2
    total += x
    if c - 1 <= 0:
        diff[i] = x
    else:
        y = (c - 1) * (c - 2) // 2
        diff[i] = x - y

for i in range(n):
    print(total - diff[a[i]])

Submission status: Accepted

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


def solve(N, As):
    A_map = [0] * (N + 1)

    # make map
    for A in As:
        A_map[A] += 1

    # calc 'not remove' cmb
    g = 0
    for am in A_map:
        g += cmb(am, 2)

    # print result
    for A in As:
        if A > 0:
            print(g - (A_map[A] - 1))


if __name__ == '__main__':
    N = int(input())
    As = [int(i) for i in input().split()]
    solve(N, As)

Submission status: Accepted

Solution code:
from collections import Counter 
N = int(input())
A = list(map(int, input().split()))
c_A = Counter(A)
all =sum( [i*(i-1)//2 for i in c_A.values()] )

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

Submission status: Accepted

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

from collections import Counter
acnt = dict(Counter(a))
value = list(acnt.values())
sv = 0
for i in range(len(value)):
    if value[i] > 1:
        sv += (value[i]*(value[i]-1))//2
for i in range(n):
    cur = a[i]
    cursv = sv
    if acnt[cur] > 2:
        cursv = cursv - acnt[cur] + 1
    elif acnt[cur] == 2:
        cursv -= 1
    print(cursv)

Submission status: Accepted

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

def factorial(n):
    v = 1
    for i in range(1, n + 1):
        v *= i
    return v

def comb(a, b):
    return factorial(a) // (factorial(b) * factorial(a - b))

from collections import Counter
acnt = dict(Counter(a))
value = list(acnt.values())
sv = 0
for i in range(len(value)):
    if value[i] > 1:
        sv += (value[i]*(value[i]-1))//2
for i in range(n):
    cur = a[i]
    cursv = sv
    if acnt[cur] > 2:
        cursv = cursv - acnt[cur] + 1
    elif acnt[cur] == 2:
        cursv -= 1
    print(cursv)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
ca = Counter(a)
ca_va = list(ca.values())
c_sum = sum([(i*(i-1))//2 for i in ca_va])
ans = 1
for j in range(n):
    x = ca[a[j]]
    ans += c_sum + ((ca[a[j]]-1)*ca[a[j]])//2 - (ca[a[j]]*(ca[a[j]]+1))//2
    print(ans)
    ans = 1

Submission status: Accepted

Solution code:
from collections import defaultdict
def main():
    N = int(input())
    A = [ a for a in input().split()]
    pair = defaultdict(int)
    for i in range(N):
        pair[A[i]] += 1

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

    for i in range(len(A)):
        v = pair[A[i]]
        ans = total - (v*(v-1)//2) + (v-1)*(v-2)//2
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def Dict_Choice(D):
    S=0
    for k in D:
        x=D[k]
        S+=(x*(x-1))//2
    return S

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

D={}
for i in range(N):
    if A[i] in D:
        D[A[i]]+=1
    else:
        D[A[i]]=1

E={}
for k in D:
    x=D[k]
    E[k]=(x*(x-1))//2

S=0
for k in E:
    S+=E[k]

for i in range(N):
    print(S-(D[A[i]]-1))

Submission status: Accepted

Solution code:
import collections
N = int(input())
li = list(map(int, input().split()))
cn = collections.Counter(li)
sumC = sum([n*(n-1)//2 for n in cn.values()])
for k in range(N):
    print(sumC - (cn[li[k]]-1))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
x = [0] * max(a)
for i in a:
    x[i - 1] += 1
all = 0


def nc2(n):
    return int(n * (n - 1) / 2)


for j in x:
    if j >= 2:
        all += nc2(j)

for i in range(n):
    if x[a[i]-1] >= 2:
        print(all-x[a[i]-1]+1)
    else:
        print(all)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int, input().split()))
a=[0]*(N+1)
b=[0]*(N+1)
for i in List:
    a[i]+=1
    
#print(a)
def fun(x):
    num=x*(x-1)/2

    return int(num)
sum=0
for i in range(0,N+1):
    b[i]=int(a[i]*(a[i]-1)/2)
    sum+=b[i]
#print(b,sum)
for k in range(1,N+1):
    q=b[List[k-1]]-(a[List[k-1]]-2)*(a[List[k-1]]-1)/2
    c=sum-q
    print(int(c))

Submission status: Accepted

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

aa = [0]*n
bb = [0]*n

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

for i in range(n):
    bb[i] = (aa[i]-1) * aa[i] // 2

sums = sum(bb)
for i in range(n):
    a_a = a[i] - 1
    b = bb[a_a]
    c = (aa[a_a]-1) * (aa[a_a]-2) // 2
    print(sums - b + c)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():

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

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

    # bannしない場合のパターン数を得る。
    all_cnt = 0
    for val in numdict.values():
        all_cnt += val*(val-1) / 2

    # k番目を除いた場合のパターン数を出力していく。
    for a in a_s:
        # k番目が存在しない場合の影響数を取得
        val = numdict[a]
        work = (val-1)*(val-1-1) / 2 - val*(val-1) / 2
        print(int(all_cnt + work))



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
lis = list(map(int,input().split()))
c = [i for i in lis]
c.sort()
num = [0 for i in range(2 * 10 ** 5+2)]
for i in c:
  num[i] += 1
ans = 0
for i in num:
    ans += i * (i-1) // 2
for i in lis:
  t = num[i]
  print(ans - t + 1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
x = [0]*max(a)
for i in a:
    x[i-1]+=1
# print(x)
count = 0
# x_ = [(k*(k-1))/2 for k in x if k>=2]
# count = sum(x_)
for j in x:
    if j>=2:
        count += int((j*(j-1)/2))
for i in range(n):
    if x[a[i]-1]>=2:
        print(count-x[a[i]-1]+1)
    else:
        print(count)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, A: "List[int]"):
    counts = {}
    for v in A:
        if not v in counts:
            counts[v] = 0
        counts[v] += 1
    cnts = list(counts.values())
    s = 0
    for c in cnts:
        s += (c * (c - 1)) // 2
    for v in A:
        ret = s
        if counts[v] > 1:
            ret -= counts[v] - 1
        print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)

n=l()[0]
A=l()
d={}
for i in range(n):
    if(A[i] not in d):
        d[A[i]]=0
    d[A[i]]+=1
total=0
for ks in d:
    total+=(d[ks]*(d[ks]-1)//2)
for i in range(n):
    cur_tot=total-(d[A[i]]*(d[A[i]]-1)//2)+((d[A[i]]-1)*(d[A[i]]-2)//2)
    print(cur_tot)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for a in A:
    try:
        memo[a] += 1
    except:
        memo[a] = 1

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

sum_memo = 0

for key in memo.keys():
    sum_memo += combinations_count(memo[key], 2)

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

Submission status: Accepted

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

c = {}

for i in a:
    c[i] = c.get(i, 0) + 1

ways = 0
for i in c:
    ways += c[i]*(c[i]-1)//2

for k in a:
    ans = ways-c[k]+1
    print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A = list(map(int,input().split()))
cn = collections.Counter(A)
sumC=sum([n*(n-1)//2 for n in cn.values()])
for k in range(N):
    print(sumC-cn[A[k]]+1)

Submission status: Accepted

Solution code:
import collections
N = int(input())
li = list(map(int, input().split()))
cn = collections.Counter(li)
sumC = sum([n*(n-1)//2 for n in cn.values()])
for k in range(N):
    print(sumC-cn[li[k]] + 1)

Submission status: Accepted

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

c = {}

for i in a:
    c[i] = c.get(i, 0) + 1

ways = 0
for i in c:
    ways += c[i]*(c[i]-1)//2

d = {}
for k in a:
    if d.get(k, "hello") == "hello":
        ans = ways-c[k]+1
        d[k] = ans
    else:
        ans = d[k]
    print(ans)

Submission status: Accepted

Solution code:
def count(l):
    res = {}
    for d in l:
        if d in res:
            res[d] += 1
        else:
            res[d] = 1
    return res
n = int(input())
data = list(map(int, input().split()))
d = count(data)
ans = sum(v*(v-1)//2 for v in d.values())
for a in data:
    banned = d[a] - 1
    print(ans-banned)

Submission status: Accepted

Solution code:
from math import factorial
def comb(n,r):
  return factorial(n)//factorial(n-r)//factorial(r)
N=int(input())
A=list(map(int,input().split()))
ANS=[0 for i in range(N)]
for i in range(N):
  a=A[i]-1
  ANS[a]+=1
ans=0  
for i in range(N):
  if ANS[i]>=2:
    ans+=comb(ANS[i],2)
for i in range(N):
  a=A[i]-1
  ans2=ans-ANS[a]+1
  print(ans2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
dict = {}
sumcon = 0
for i in a:
  if i not in dict:
    dict[i] = 1
  else:
    dict[i] += 1
    
for i in dict:
  sumcon += (dict[i]*(dict[i]-1))//2
  
for j in a:
  print(sumcon + 1 - dict[j])

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
al = list(map(int, input().split()))
C = Counter(al)
# print(C)
# print(C.values())
ans = 0
for i in C.values():
    ans += i * (i - 1) / 2

for a in al:
    k = C[a]
    print(int(ans - (k * (k - 1) / 2) + (k - 1) * (k - 2) / 2))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
cnt=[0 for i in range(n)]
for i in a:
  cnt[i-1]+=1

nc2=[(i*(i-1))//2 for i in cnt]

All=sum(nc2)
for i in range(n):
  now=cnt[a[i]-1]-1
  print(All-nc2[a[i]-1]+((now)*(now-1))//2)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
cnt=[0 for i in range(n)]
for i in a:
  cnt[i-1]+=1

nc2=[]
for i in cnt:
  nc2.append(((i)*(i-1))//2)

All=sum(nc2)
for i in range(n):
  now=cnt[a[i]-1]-1
  print(All-nc2[a[i]-1]+((now)*(now-1))//2)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
C = collections.Counter(A)
comb = 0
output = []

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

for a in A:
  output.append(comb - (C[a]-1))

print(*output, sep="\n")

Submission status: Accepted

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

Submission status: Accepted

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

n = 0
B = set(A)

C = Counter(A)

for i in C.keys():
    n += C[i] * (C[i]-1) // 2

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

Submission status: Accepted

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

for i in a:
    print(sums-s[i]+1)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

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

count = {}
for x in A:
    if count.get(x):
        count[x] += 1
    else:
        count[x] = 1

sub = {}
s = 0
for i, x in count.items():
    count[i] = x * (x - 1) // 2
    s += count[i]
    sub[i] = (x-1) * (x - 2) // 2

for i in range(N):
    print(s - count[A[i]] + sub[A[i]])

Submission status: Accepted

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

d = {}


for i in range(N):
    if l[i] in d:
        d[l[i]] += 1
    else:
        d[l[i]] = 1

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

def get_n(x):

    return int(max_comb - d[x] +1)


for e in l:
    print(get_n(e))

Submission status: Accepted

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

Submission status: Accepted

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

for i in a:
  print(sums-s[i]+1)

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.items():
    cnt += i[1]*(i[1]-1)//2
for j in a:
    k = A[j]
    before = k*(k-1)//2 
    after = (k-1)*(k-2)//2
    print(cnt -before +after)

Submission status: Accepted

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

import collections
 
c = collections.Counter(l)
ans = 0

for i in c.values():
    ans += i * (i - 1) // 2
    
for i in l:
    print(ans - c[i] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
D = {}
E = {}
nn = 0
for i in C.keys():
    n = C[i]
    if n == 1:
        D[i] = 0
        E[i] = 0
    else:
        D[i] = int(n * (n - 1) / 2)
        nn += D[i]
        E[i] = int((n - 1) * (n - 2) / 2)
for k in range(N):
    n = A[k]
    print(nn - D[n] + E[n])

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=[int(i) for i in input().split()]
kw = Counter(A)
ans = sum([x * (x - 1) // 2 for x in kw.values()])
for i in A:
    print(ans-kw[i]+1)

Submission status: Accepted

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

c = [0]*n

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

sum_c = 0
for j in range(n):
    sum_c += (c[j]*(c[j]-1))//2

for k in range(n):
    print(sum_c - c[a[k]] + 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [0]*N
for i in range(N):
  j = A[i] - 1
  B[j] += 1
C = [ B[i]*(B[i] - 1)/2 for i in range(N) ]
S = sum(C)
for i in range(N):
  j = A[i] - 1
  ans = S - B[j] + 1
  print(int(ans))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

#print(Counter(A).keys())
#print(Counter(A).values())
#print(Counter(A).items())

lis = [0]*(N+1)
ans = 0
for key, value in Counter(A).items():
	lis[key] = value
	ans += value*(value-1)//2

#print(lis)

for i in range(N):
	print(ans-lis[A[i]]+1)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

dic = defaultdict(int)
for x in A:
	dic[x] += 1

total = 0
for x in dic:
	total += dic[x]*(dic[x]-1)//2

for x in A:
	print(total-dic[x]+1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    b = {}
    c = set()
    for i in a:
        if i not in b:
            b[i] = 1
        else:
            b[i] += 1
            c.add(i)
    d = 0
    for i in c:
        d += (b[i] * (b[i] - 1)) // 2
    for i in range(n):
        e = d - ((b[a[i]] * (b[a[i]] - 1)) // 2) + (((b[a[i]] - 1) * (b[a[i]] - 2)) // 2)
        print(e)

main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A)
SUM = 0
for key in C.keys():
    num = C[key]
    SUM += (num*(num-1))//2
for a in A:
    if C[a] <= 1:
        print(SUM)
    else:
        num = C[a]
        A = num*(num-1)//2
        B = (num-1)*(num-2)//2
        print(SUM-A+B)

Submission status: Accepted

Solution code:
n = int(input())
number = list(map(int,input().split()))
num = sorted(number)
num.append(0)
p = num[0]
combi = dict()
u = 0

for i in range (n+1) :
    q = num[i]
    if p != q :
        combi[p] = u
        u = 0
    u += 1
    p = q

t = 0

for j in combi.values() :
    t += j*(j-1)//2

for k in range (n) :
    x = number[k]
    print(t + (combi[x]-1)*(combi[x]-2)//2 - combi[x]*(combi[x]-1)//2)

Submission status: Accepted

Solution code:
def combi(count):
  if n==1:return 0
  elif n==2:return 1
  else:return int(count*(count-1)/2)
 
 
n=int(input())
alis=list(map(int,input().split()))
dick={st:0 for st in set(alis)}
for i in range(n):
  p=alis[i]
  dick[p]+=1
#print(dick)
point=0
for v in dick.values():
  point+=combi(v)
 
 
for i in range(n):
  idx=alis[i]
  minus=dick[idx]-1
  print(point-minus)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections

ni = lambda: int(sys.stdin.readline())
nl = lambda: list(map(int, sys.stdin.readline().split()))

N = ni()
A = nl()
counter = collections.Counter(A)
total = 0
for n in counter.values():
    total += n * (n - 1) // 2
for i in range(N):
    n = counter[A[i]]
    k = total - (n * (n - 1) // 2) + (n - 1) * (n - 2) // 2
    print(k)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
se_ = Counter(A)
key = se_.keys()
 
 
all = 0
for i in key:
    all += se_[i]*(se_[i]-1)//2
    
 
for i in A:
    final=0
    final = all -se_[i]+1
    print(final)

Submission status: Accepted

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

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

for i in range(N):
    counts[A[i]] += 1

S = 0

for v in counts.values():
    S += v*(v-1)//2

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