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:
# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

N = INT()
A = LIST()

C = Counter(A)
total = 0
for k, v in C.items():
    total += nC2(v)

ans = [0] * N
for i, a in enumerate(A):
    ans[i] = total - nC2(C[a]) + nC2(C[a]-1)
[print(a) for a in ans]

Submission status: Accepted

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

cnt=[0]*(n+1)
s=0

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

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

for k in range(1,n+1):
    if cnt[a[k-1]]>=2:
        print(s-cnt[a[k-1]]+1)
    else:
        print(s)

Submission status: Accepted

Solution code:
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


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


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    N = get_input(int)
    A = get_inputs(int)

    counter = Counter(A)

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

def solve():
    N = int(input())
    A = [int(x) for x in input().split()]
    C = Counter(A)
    #3 <= N <= 2 * 10**5

    cache = {}
    D = {}

    total = 0
    for a, c in C.items():
        if c == 1:
            D[a] = 0
            continue
        choose = c * (c-1) // 2
        D[a] = choose
        total += choose

    for k in range(N):
        a = A[k]
        if a in cache:
            print(cache[a])
            continue
        
        c = C[a]
        x = total
        x -= D[a]
        if c >= 3:
            x += (c-1) * (c-2) // 2

        cache[a] = x
        print(x)
solve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = dict()
c = dict()
d = dict()
for i in range(n):
    if a[i] not in b:
        b[a[i]] = 0
    b[a[i]] += 1
b = list(b.items())
for i in range(len(b)):
    c[b[i][0]] = b[i][1] * (b[i][1]-1) // 2
    d[b[i][0]] = (b[i][1]-1) * (b[i][1]-2) // 2
c_sum = sum(list(c.values()))
for i in range(n):
    num = a[i]
    print(c_sum - c[num] + d[num])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

from collections import defaultdict
def main():
    N = int(input())
    A = list(map(int, input().split()))
    counter = defaultdict(int)
    for a in A:
        counter[a] += 1

    total = 0
    for key, ct in counter.items():
        if ct > 1:
            total += cmb(ct, 2)
    
    for a in A:
        ans = total
        ct = counter[a]
        if ct > 1:
            before = cmb(ct, 2)
            after  = before * (ct-2) // ct
            ans -= (before-after)
        print(ans)

    

main()

Submission status: Accepted

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

import collections

c = collections.Counter(A)

sum_all = sum([v * max(v - 1, 0) // 2 for k, v in c.items()])

ans = []
for a in A:
  b = c[a]
  ans_a = sum_all - (b * max(b - 1, 0) // 2) + (max(b - 1, 0) * max(b - 2, 0) // 2)
  ans.append(ans_a)
print(*ans, sep='\n')

Submission status: Accepted

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

NormalAns = [i*(i-1)//2 for i in Nums]
AnotherAns = [((i-1)*(i-2)//2 if i-2>=0 else 0) for i in Nums]
Ans = sum(NormalAns)
for i in A:
    print(Ans-NormalAns[i-1]+AnotherAns[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

d2 = {}
d3 = {}
for k, v in d.items():
    if v < 2:
        d2[k] = 0
    else:
        d2[k] = v*(v-1)//2
    if v-1 < 2:
        d3[k] = 0
    else:
        d3[k] = (v-1)*(v-2)//2
s = sum(list(d2.values()))

for i in range(n):
    print(s-d2[A[i]]+d3[A[i]])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
k_list = [int(w) for w in input().split()]
c = Counter(k_list)


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

for i in k_list:
    cnt = c[i]
    tmp = max_pat

    if cnt >= 2:
        tmp -= (cnt * (cnt - 1)) // 2
        tmp += ((cnt - 1) * (cnt - 2)) // 2

    print(tmp)

Submission status: Accepted

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

#print(A, B, C)
def n2(n):
  if n == 0:
    return 0
  elif n == 1:
    return 0
  else:
    return int(n * (n - 1) / 2)

all_ans = 0
x = -1
count = 0
for i in range(N):
  if B[i] == x:
    count += 1
  else:
    all_ans += n2(count)
    x = B[i]
    count = 1
  #print(count, x, all_ans) 
  
all_ans += n2(count)    
#print(all_ans)
    
for i in range(N):
  y = C[A[i]]
  print(all_ans - y + 1)

Submission status: Accepted

Solution code:
n = int(input())

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

import collections

C = collections.Counter(A)

t = 0

for i in C.values():
    t += i * (i - 1) //2

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

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
nums = [int(i) for i in input().split()]

d = defaultdict(int)
for num in nums:
    d[num] += 1

all_sum = 0
diff_d = {}
for num, freq in d.items():
    # 一回しか出てきてなければ0がたされる
    all_sum += freq * (freq-1) // 2
    # tmp = (freq - 1) * (freq - 2) // 2
    tmp = freq - 1
    diff_d[num] = tmp

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

Submission status: Accepted

Solution code:
from collections import defaultdict
def comb(n,k):
    n1, k = n+1, min(k, n-k)
    numer = denom = 1
    for i in range(1,k+1):
        numer = numer*(n1-i)
        denom = denom*i
    return numer//denom
def main():
    N = int(input())
    a = list(map(int,input().split()))
    d = defaultdict(int)
    for A in a:
        d[A] += 1
    S = defaultdict(int)
    total = 0
    for v in d.items():
        key = v[0]
        n = v[1]
        if n < 2:S[key] = 0
        else:S[key] = comb(n,2)
        total += S[key]
    for A in a:
        m = d[A]-1
        if m < 2:
            print(total-S[A])
        else:
            t = comb(m,2)
            print(total-S[A]+t)
        
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    def comb(n, k):
        a = n * (n - 1)
        return a // 2
    n = int(input())
    a = list(map(int, input().split()))

    count = {}
    # 10 ** 5
    for i in a:
        count[i] = count.get(i, 0) + 1

    all_sum = 0
    # 10 ** 5
    for i in count.values():
        all_sum += comb(i, 2)

    # 10 ** 5
    for i in a:
        ans = all_sum - comb(count[i], 2) + comb(count[i] - 1, 2)
        print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import Counter
n=int(input())
a=[int(i) for i in input().split()]
d=Counter(a) ;ans=0
for i in d:
    ans+=(d[i]*(d[i]-1))//2
for i in a:
    print(ans-(d[i]-1))

Submission status: Accepted

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

counts = [0 for _ in range(N)]
for a in An:
  counts[a-1] += 1
  
total = 0
for c in counts:
  if c >= 2:
    total += c * (c - 1) // 2

for a in An:
  c = counts[a-1]
  if c >= 2:
    minus = c * (c-1)//2  - (c-1)*(c-2)//2
  else:
    minus = 0
  print(total - minus)

Submission status: Accepted

Solution code:
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)

def main():
    N = int(input())
    A = list(map(int,input().split()))
    A_sorted = sorted(A)
    count = [0] * (N+1)

    pre = A_sorted[0]
    count[pre] = 1
    for i in range(1,N):
        if pre == A_sorted[i]:
            count[pre] += 1
        else:
            pre = A_sorted[i]
            count[pre] = 1

    sum = 0
    for i in range(len(count)):
        sum += count[i] * (count[i]-1) // 2

    for a in A:
        minus = count[a] * (count[a]-1) // 2
        plus = (count[a]-1) * (count[a]-2) // 2
        print(sum + plus - minus)




if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A_set = list(set(A))
l = [0]*(N+2)
ans = [0]*(N+2)
for a in A:
  l[a]+=1
comb = [0]*(N+2)
for i in range(2,N+1):
  comb[i] = i*(i-1)//2
  
for i in range(2,N+1):
  ans[i]=comb[i]-comb[i-1]
  
total = 0
for a in A_set:
  total += comb[l[a]]

for a in A:
  print(int(total-ans[l[a]]))

Submission status: Accepted

Solution code:
import collections
n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
d = c.copy()
ans_1 = 0
for v in d.values():
  v = int(v)
  ans_1 += v*(v-1)//2
for k in li:
  print(ans_1 - (c[k]-1))

Submission status: Accepted

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

for i in range(N):
    if L[i] != 0:
        S += (L[i] * (L[i]-1)) // 2

for i in range(N):
    n = A[i]
    S2 = S
    S2 = S2 - ((L[A[i]-1] * (L[A[i]-1]-1)) // 2)
    if L[A[i]-1] - 1 > 0:
        S2 += (((L[A[i]-1]-1) * (L[A[i]-1]-2)) // 2)
    print(S2)

Submission status: Accepted

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

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

def solve(N: int, A: "List[int]"):
    counter = dict(Counter(A))

    MAX = 0
    for _,value in counter.items():
        MAX += comb(value,2)

    for i in range(N):
        value = counter[A[i]]

        print(MAX-comb(value,2)+comb(value-1,2))



    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:
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    total = 0
    for i in C.values():
        if i == 1:
            continue
        total += i*(i-1)//2
    
    for i in range(N):
        a = C[A[i]] - 1
        print(total - a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
def com(x):
    return(x*(x-1)//2)
temp = []
for i in range(0, 2*10**5 + 1):
    temp.append(com(c[i]))
temp_sum = sum(temp)
for i in A:
    print(temp_sum-temp[i]+com(c[i]-1))

Submission status: Accepted

Solution code:
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
cnt = 0
for key in dic:
    cnt += dic[key]*(dic[key]-1)//2
for i in range(N):
    print (cnt-dic[A[i]]+1)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

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

counter = Counter(a)
c = [0] * n
for k, v in counter.items():
    c[k-1] = v


def comb(n):
    r = 2
    numerator = denominator = 1
    for i in range(r):
        numerator = numerator * (n - i)
        denominator = denominator * (i + 1)
    return numerator // denominator


combination = tuple(map(comb, c))
comb_sum = sum(combination)
for i in a:
    i -= 1
    if c[i] > 0:
        print(comb_sum - (c[i] - 1))
    else:
        print(comb_sum)

Submission status: Accepted

Solution code:
import math
from collections import deque
def main():
    n = int(input())
    a = [int(t)for t in input().split()]

    vote = {}
    for a_ in a:
        if a_ in vote.keys():
            vote[a_] += 1
        else:
            vote[a_] = 1

    combi = {}

    result = 0
    for a_,num in vote.items():
        # combi[a_] = num*(num-1)//2
        result += num*(num-1)//2

    for a_ in a:
        res = result-(vote[a_]-1)
        print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

dic2 = {}
maxi = 0
for key, num in dic.items():
    temp = 0
    if num>=2:
        temp = (num)*(num-1)//2
    dic2[key] = temp
    maxi += temp

for k in range(n):
    temp = maxi
    num = dic[str(a[k])]
    nCr = 0
    if num-1>=2:
        nCr = (num-1)*(num-2)//2
    print(temp - dic2[str(a[k])] + nCr)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
    
fac = [-1] * (10**7+1)
inv = [-1] * (10**7+1)
finv = [-1] * (10**7+1)

fac[0] = fac[1] = 1
inv[1] = 1
finv[0] = finv[1] = 1

def initNCMMod(limit):
    for i in range(2, limit):
        fac[i] = fac[i-1] * i

def NCM(n):
    return n * (n-1) // 2
def main():
    N = I()
    A = LI()
    vals = [0 for _ in range(N+1)]
    pick = [0 for _ in range(N+1)]
    for a in A:
        vals[a] += 1
    for i, val in enumerate(vals):
        pick[i] = NCM(val)
    picksum = sum(pick)
    for a in A:
        tmp_picksum = picksum - NCM(vals[a])
        tmp_picksum += NCM(vals[a]-1)
        print(tmp_picksum)


main()

Submission status: Accepted

Solution code:
import collections

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

S = 0
c = collections.Counter(A)
for v in c.values():
    S = S + v*(v-1)//2
for i in range(len(A)):
    print(S-c[A[i]]+1)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ans = [0 for _ in range(N+1)]
dic = defaultdict(int)
for a in A:
    dic[a]+=1
for k in range(N+1):
    ans[k] = dic[k]*(dic[k]-1)//2

s = sum(ans)
for i in range(N):
    a = A[i]
    print(s-ans[a]+(dic[a]-1)*(dic[a]-2)//2)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,=pin()
    A=list(pin())
    from collections import Counter as ct
    a=ct(A)
    t=(a.items())
    #print(t)
    
    Sum=0
    for l in a.values():
        Sum+=(l*(l-1))//2 
    ans=[0]*N
    for j in a.items():
        x,y=j
        ans[x-1]=Sum-((y*(y-1))//2)+(((y-2)*(y-1))//2)     
    for i in A:
        print(ans[i-1])
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


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__":resolve()

Submission status: Accepted

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

def solve(N: int, A: "List[int]"):
    dictionary = collections.defaultdict(int)
    for a in A:
        dictionary[a] += 1

    counted = collections.defaultdict(int)
    for k, v in dictionary.items():
        counted[k] = v*(v-1)//2
    summarized = sum(counted.values())

    for a in A:
        v = dictionary[a]
        result = summarized - v*(v-1)//2 + (v-1)*(v-2)//2
        print(result)
    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
n = int(input())
A = list(map(int, input().split()))
Ca = {k:v for k, v in Counter(A).items() if v>=2}
ans = {}
al = 0
for k, v in Ca.items():
    al += v*(v-1)//2
for k, v in Ca.items():
    ans[k] = al - v*(v-1)//2 + (v-1)*(v-2)//2
for a in A:
    if a in ans:
        print(ans[a])
    else:
        print(al)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import Counter

n = int(sys.stdin.readline().strip())
a = [int(s) for s in sys.stdin.readline().strip().split()]
c = Counter(a)

total = 0
for key, val in c.items():
    total += val * (val-1) // 2

for i in range(n):
    ai = a[i]
    ai_count = c[ai]

    original = ai_count * (ai_count-1) // 2
    updated = (ai_count-1) * (ai_count-2) // 2
    print(total - original + updated)

Submission status: Accepted

Solution code:
N = int(input())
A_dic = {}
A_lis = [int(x) for x in input().split()]
sum = 0

for n in range(N):
    if A_lis[n] in A_dic.keys():
        A_dic[A_lis[n]] += 1
    else:
        A_dic[A_lis[n]] = 1


for key,val in A_dic.items():
    sum += val*(val-1)

sum = sum//2



for n in range(N):
    print(sum - (A_dic[A_lis[n]] - 1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
H = Counter(A)
K = {}

for k,v in H.items():
    K[k] = v*(v-1)//2
total = sum(K.values())

for i in A:
    print(total - K[i] + (H[i] - 1)*(H[i]-2)//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

c=collections.Counter(a)

su=0
an={}
for k in c.keys():
    tmp=c[k]*(c[k]-1)//2
    su+=tmp
    an[k]=(c[k]*(c[k]-1)-(c[k]-1)*(c[k]-2))//2

for o in a:
    print(su-an[o])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
all = 0
for i in c.values():
    all += (i*(i-1))//2
for j in range(n):
    print(all-(c[a[j]]-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):
    t = A[i]
    result = s
    result -= d[t] * (d[t] - 1) // 2
    result += (d[t] - 1) * (d[t] - 2) // 2
    print(result)

Submission status: Accepted

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

def comb(x):
    if x > 1:
        return int(x*(x-1)/2)
    return 0

M = [0 for _ in range(N+1)]
C = [0 for _ in range(N+1)]
_C = [0 for _ in range(N+1)]

for i in range(N):
    M[A[i]] += 1
for i in range(N):
    cnt = M[A[i]]
    C[A[i]] = comb(cnt)
    _C[A[i]] = comb(cnt-1)

S = sum(C)
for i in range(N):
    print(S - C[A[i]] + _C[A[i]])

Submission status: Accepted

Solution code:
from collections import defaultdict
n, *A = map(int, open(0).read().split())
d = defaultdict(int)
for a in A:
    d[a] += 1
s = 0
for v in d.values():
    s += v*(v-1)//2
for a in A:
    print(s-d[a]*(d[a]-1)//2+(d[a]-1)*(d[a]-2)//2)

Submission status: Accepted

Solution code:

import collections

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

ct = collections.Counter(A)

ret = []
total = 0
for k,count in ct.items():
    if count > 1:
        total += count * (count-1) // 2

for i,v in enumerate(A):
    if ct[v] > 1:
        count = ct[v]
        adjust = (count-1) * (count-2) // 2 - (count-1) * count // 2
        val = total + adjust
    else:
        val = total
    print(val)

Submission status: Accepted

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

def solve(N: int, A: "List[int]"):
    dictionary = collections.defaultdict(int)
    for a in A:
        dictionary[a] += 1

    counted = collections.defaultdict(int)
    for k, v in dictionary.items():
        counted[k] = v*(v-1)//2
    summarized = sum(counted.values())

    #print(counted)
    for a in A:
        v = dictionary[a]
        #print(summarized, v, v*(v-1)//2,  (v-1)*(v-2)//2)
        result = summarized - v*(v-1)//2 + (v-1)*(v-2)//2
        print(result)
    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:
L = int(input())
nums = list(map(int,input().split(" ")))
di = {}
for num in nums:
    if num not in di:
        di[num] = 1
    else:
        di[num] += 1
total = 0
minus = {}
for k,v in di.items():
    total += v * (v-1) //2
    minus[k] = v * (v-1) //2 -  max( (v - 2) * (v-1)//2,0)
for num in nums:
    print(total - minus[num])

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def conb(n,r): 
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = inp()
a = inpl()
c = Counter(a)
res = 0
d = dict()
for key in list(c):
    # print(c[key])
    if c[key] > 1:
        tmp = conb(c[key], 2)
        res += tmp
        d[key] = tmp
    else:
        d[key] = 0
for i,t in enumerate(a):
    if d[t] == 0:
        print(res)
        continue
    now = d[t] * max(0,c[t]-2) // c[t]
    # print(now)
    print(res - d[t] + now)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
l = nl()
d = Counter(l)
ans = sum(x*(x-1)//2 for x in d.values())
for x in l:
    print(ans - d[x] + 1)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    ns = [0 for _ in range(N)]
    for a in A:
        ns[a - 1] += 1
    
    ls = [int(ns[i] * (ns[i] - 1) / 2) for i in range(N)]
    sm = sum(ls)

    for a in A:
        print(int(sm - ls[a - 1] + (ns[a - 1] - 1) * (ns[a - 1] - 2) / 2))



# 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:
def fun(a):
    return a*(a-1)//2
import sys
def main():
    n = int(input())
    l = [int(j) for j in sys.stdin.readline().split()]
    d = dict()
    for i in range(n):
        if l[i] in d:
            d[l[i]]+=1
        else:
            d[l[i]]=1
    # print(d)
    ans = 0
    for key in d:
        ans+=fun(d[key])
    # a = len(d)*(len(d)-1)//2
    # b = (len(d)-1)*(len(d)-2)//2
    for i in range(n):
        if d[l[i]]==1:
            print(ans)
        else:
            print(ans - fun(d[l[i]]) + fun(d[l[i]]-1))
main()

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
seq = [int(x) for x in input().split()]
c = Counter(seq)
d = dict()
total = 0
for val in c:
    d[(val,0)] = ((c[val]-1) * (c[val]-1- 1)) >> 1
    d[(val,1)] = (c[val] * (c[val]- 1)) >> 1
    total += d[(val,1)]

for i in range(n):
    val = seq[i]
    print(total - d[(val,1)] + d[(val,0)])

Submission status: Accepted

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

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

c = {}

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


ansall = 0
for _, z in buk.items():
  if z >= 2:
    ansall += cmb(z, 2)

for x in a:
  if buk[x] - 1 >= 2:
    print(ansall - cmb(buk[x], 2) + cmb(buk[x] - 1, 2))
  elif buk[x] == 2:
    print(ansall - 1)
  else:
    print(ansall)

Submission status: Accepted

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

a_dict = dict()
for i in a:
    if i not in a_dict:
        a_dict[i] = 1
    else:
        a_dict[i] += 1

total = 0
for k, v in a_dict.items():
    if v > 1:
        total += int(v * (v - 1) / 2)

for i in a:
    print(total - a_dict[i] + 1)

Submission status: Accepted

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter
setrecursionlimit(1000000)

# INF = float('inf')
INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    cA = Counter(A)

    diff = {}
    total = 0
    for key in cA:
        v = cA[key]
        total += (v * (v - 1)) // 2
        diff[key] = (v - 1) * (v - 2) // 2 - (v * (v - 1)) // 2

    for ai in A:
        print(total + diff[ai])
        # cA[ai] -= 1
        # count = 0
        # for key in cA:
        #     v = cA[key]
        #     count += (v * (v - 1)) // 2
        # cA[ai] += 1
        # print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

num_count = {}
for num in nums:
    num_count[num] = num_count.get(num, 0) + 1

total = 0
for num, ele in num_count.items():
    total += (ele * (ele - 1)) // 2

ans = []

for num in nums:
    ans.append(total - num_count[num] + 1)
print("\n".join(list(map(str, ans))))