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()))
b = [0] * (n + 1)
for i in range(n):
    b[a[i]] += 1
ans = 0
for i in range(len(b)):
    ans += b[i] * (b[i]-1) // 2
for i in range(n):
    print(ans - (b[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:
        d[a[i]]+=1
    else:
        d[a[i]]=1
ans=0
D=list(d.values())
for i in range(len(D)):
    s=D[i]*(D[i]-1)//2
    ans+=s
for i in range(n):
    print(ans-d[a[i]]+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

c = 0
cnt = [0]*n

for i in range(n):
    A[i] -= 1

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

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

for i in range(n):
    c += combinations(cnt[i])

for i in range(n):
    ans = c
    ans -= combinations(cnt[A[i]])
    ans += combinations(cnt[A[i]]-1)
    print(ans)

Submission status: Accepted

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

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

num_dict = {i:0 for i in range(1,N+1)}
for i in range(N):
    num_dict[A[i]] += 1

S = 0
for i in range(1,N+1):
    S += cmb(num_dict[i])

for i in range(N):
    tmp = 0
    S_dif = cmb(num_dict[A[i]]-1) - cmb(num_dict[A[i]])
    tmp = S + S_dif
    print(int(tmp))

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

def cmb(n,r):
    if n < r:
        return 0
    else:
        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()))

num_dict = {i:0 for i in range(1,N+1)}
for i in range(N):
    num_dict[A[i]] += 1

S = 0
for i in range(1,N+1):
    S += cmb(num_dict[i],2)

for i in range(N):
    tmp = 0
    S_dif = cmb(num_dict[A[i]]-1,2) - cmb(num_dict[A[i]],2)
    tmp = S + S_dif
    print(tmp)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
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 TUPLE(): return tuple(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 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
A = LIST()
C = Counter(A)

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

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

Submission status: Accepted

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

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
combo1 = [-1]*(n+1)
combo2 = [-1]*(n+1)
total = 0
for i in range(n):
    if combo1[a[i]] == -1:
        c = cnt[a[i]]
        combo1[a[i]] = (c*(c-1)//2)
        combo2[a[i]] = ((c-1)*(c-2)//2)
        total += combo1[a[i]]
'''
print(combo1)
print(combo2)
print(total)
'''
for i in a:
    ans = total - combo1[i] + combo2[i]
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

c = None

def resolve(in_, out):
    n = int(in_.readline())
    a = tuple(map(int, in_.readline().split()))

    c = dict(Counter(a))

    total = sum(v * (v - 1) // 2 for v in c.values())

    for ai in a:
        ans = total - (c[ai] - 1)
        print(ans, file=out)


def main():
    resolve(sys.stdin.buffer, sys.stdout)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from functools import lru_cache

c = None

@lru_cache(maxsize=200000)
def _num_of_ways_to_choose(x):
    v = c[x]
    return (v * (v - 1)) // 2


def resolve(in_, out):
    n = int(in_.readline())
    a = tuple(map(int, in_.readline().split()))
    global c
    c = dict(Counter(a))

    _num_of_ways_to_choose.cache_clear()
    total = sum(map(_num_of_ways_to_choose, c.keys()))

    for ai in a:
        ans = total - _num_of_ways_to_choose(ai) + ((c[ai] - 1) * (c[ai] - 2)) // 2
        print(ans, file=out)


def main():
    resolve(sys.stdin.buffer, sys.stdout)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(1, N+1):
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Submission status: Accepted

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

d = [0]*n
for i in a:
    d[i-1] += 1

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

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

Submission status: Accepted

Solution code:
from scipy.special import comb
from collections import Counter

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

count = Counter(A)
combs = {k:comb(count[k], 2) for k in count.keys()}
combs_minas_one = {k:comb(count[k]-1, 2) for k in count.keys()}
all = sum(combs.values())

for a in A:
    ans = all - combs[a] + combs_minas_one[a]
    print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
import math

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
answer = 0
for num in c:
  answer += c[num]*(c[num]-1)//2

for k in range(N):
  print(answer - c[A[k]] + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
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 = collections.Counter(A)
num = 0
for i in set(A):
    if B[i] > 1:
        num += combinations_count(B[i], 2)
for i in A:
    print(num-B[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)])

n = inp()
a = inpl()
kosuu = [0]*(200000+1)
cnt = [0]*(200000+1)
ans = [0]*(200000+1)

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

s = 0
for i in kosuu:
  s += i * (i - 1) // 2

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
freq = [0] * (max(A)+1)
for a in A:
    freq[a] += 1
total = 0
for f in freq:
    total += f*(f-1)//2
for a in A:
    print (total - max(freq[a]-1, 0))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(i) for i in input().split()]
ca = Counter(A)
ts = 0
for k in ca:
    ts += (ca[k] * (ca[k] - 1)) // 2
ans = [0]*N
for i,a in enumerate(A):
    ans[i] = ts - (ca[a] - 1)
print("\n".join(map(str, ans)))

Submission status: Accepted

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

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

sum_combinations = 0
for x in lis[1:]:
    if x >= 2:
        sum_combinations += x*(x-1)//2

for i in range(1,N+1):
    ans = sum_combinations
    num = lis[A[i]]
    if num >= 2:
        ans += (num-1)*(num-2)//2 - num*(num-1)//2
    
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

C=Counter(A)

C_={}
sum_ = 0
for k in C.keys():
    c=C[k]
    if c == 1:
        C_[k]=0
    else:
        C_[k]=(c*(c-1))//2
        
    sum_ += C_[k]

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


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

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

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

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


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
d={}
for i in a:
  if d.get(i):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]-1))

Submission status: Accepted

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

def choose2(n):
  if n <= 1:
    return 0
  return n*(n-1)//2

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

total = 0
for i in range(n+1):
  total += choose2(inp[i])

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

Submission status: Accepted

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

nums = [0] * (max(a) + 1)
for i in range(len(a)):
    nums[a[i]] += 1

from scipy.special import comb

all = 0
for i in range(len(nums)):
    all += comb(nums[i], 2, exact=True)

for k in range(n):
    print(all - (nums[a[k]] - 1))

Submission status: Accepted

Solution code:
import collections
import math

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
answer = 0
for num in c:
    answer += c[num] * (c[num] - 1) // 2

for k in range(N):
    print(answer - c[A[k]] + 1)

Submission status: Accepted

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

a_count = Counter(a_input)

score_dic={}
exclude_dic={}

for k,v in a_count.items():
    score_dic[k]=v*(v-1)//2
    exclude_dic[k]=(v-1)*(v-2)//2

score_sum = sum(score_dic.values())

for i in range(n):
    print(score_sum-score_dic[a_input[i]]+exclude_dic[a_input[i]])

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n = int(stdin.readline().rstrip())
a = [x for x in stdin.readline().rstrip().split()]

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

m = sum([v*(v-1) // 2 for v in list(d.values())])

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

Submission status: Accepted

Solution code:
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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

ac = Counter(a)
sm = 0
for v in ac.values():
    if v > 1:
        sm += v * (v - 1) // 2
for v in a:
    print(sm - ac[v] * (ac[v] - 1) // 2 + (ac[v] - 1) * (ac[v] - 2) // 2)

Submission status: Accepted

Solution code:
from collections import Counter

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

ac = Counter(a)
sm = 0
for v in ac.values():
    if v > 1:
        sm += v * (v - 1) // 2
for v in a:
    print(
        sm
        if ac[v] <= 1
        else sm - ac[v] * (ac[v] - 1) // 2 + (ac[v] - 1) * (ac[v] - 2) // 2
    )

Submission status: Accepted

Solution code:
from collections import defaultdict
from sys import stdin
input = stdin.readline


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

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

  C2 = lambda N: int(N * (N-1) / 2)

  sum_ = 0
  for key in dic.keys():
    sum_ += C2(dic[key])

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


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 range(n):
    cnt[a[i]]+=1

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

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

Submission status: Accepted

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

ind_A = {}

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

ans = 0

for (i, v) in ind_A.items():
  ans += (v * v - v) // 2


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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))

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

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

for k in range(N):
  ans = total - s[A[k]-1]*(s[A[k]-1]-1)//2 + (s[A[k]-1]-1)*((s[A[k]-1]-1)-1) // 2
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = int(input())
    A = list(map(int, input().split()))
    a_c = Counter(A)
    a_s = list(set(A))
    tot = 0
    for a in a_s:
        tot += a_c[a] * (a_c[a] - 1) // 2
    ans = list(map(lambda x: tot - (a_c[x] - 1), a_s))
    ans_dic = dict(zip(a_s, ans))
    for a in A:
        print(ans_dic[a])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
cnt = Counter(A)
sum = 0
for n in cnt.values():
    sum += n*(n-1)//2
for a in A:
    print(sum-cnt[a]+1)

Submission status: Accepted

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

for a in A:
    print(sum_comb - dict_A[a] + 1)

Submission status: Accepted

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

c = Counter(a)
#print(c)

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

for i in range(n):
    print(s - c[a[i]] + 1)

Submission status: Accepted

Solution code:
from math import factorial
n=int(input())
cnt=[0]*(n+1)
lst=list(map(int,input().split()))
for i in lst:
    cnt[i]+=1
ans=0
for i in range(n+1):
    p=cnt[i]
    if p>=2:
        ans+=factorial(p)//factorial(p-2)//2
for i in range(n):
    print(ans-cnt[lst[i]]+1)

Submission status: Accepted

Solution code:
from math import factorial
n=int(input())
cnt=[0]*(n+1)
lst=list(map(int,input().split()))
tmp=[0]*(n+1)
change=[0]*(n+1)
for i in lst:
    cnt[i]+=1
ans=0
for i in range(n+1):
    p=cnt[i]
    if p>=2:
        q=factorial(p)//factorial(p-2)//2
        ans+=q
        tmp[i]=q
    if p>=3:
        change[i]=factorial(p-1)//factorial(p-3)//2
for i in range(n):
    print(ans-tmp[lst[i]]+change[lst[i]])

Submission status: Accepted

Solution code:
import collections

n = int(input())
arr = list(map(int,input().split()))
cnt = collections.Counter(arr)
total = 0

for key in cnt.keys():
  total += cnt[key] * (cnt[key] - 1)//2
  
for i in range(n):
  tmp = total
  tmp -= cnt[arr[i]]*(cnt[arr[i]] - 1)//2
  tmp += (cnt[arr[i]] - 1)*(cnt[arr[i]] - 2)//2
  print(tmp)

Submission status: Accepted

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


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

def solve(n,a):
  d = Counter(a)
  patterns = dict((k,combi(v)) for k,v in d.items())
  patterns_sum = sum(patterns.values())
  ans = [0]*n
  for i in range(n):
    ans[i] = patterns_sum - patterns[a[i]] + combi(d[a[i]]-1)
  return ans



def main():
  N = int(input())
  a = list(map(int,input().split()))
  ans = solve(N,a)
  for i in range(len(ans)):
    print(ans[i])
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
from copy import deepcopy

def count(A):
    cnt = 0
    for a in A:
        cnt += (a*(a-1))//2
    return cnt


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    max_A = max(A)

    count_A = [0]*(max_A+1)

    for a in A:
        count_A[a]+= 1

    combi_A = 0

    for c in count_A:
        combi_A += (c*(c-1))//2
    for a in A:
        print(combi_A-(count_A[a]*(count_A[a]-1)-(count_A[a]-1)*(count_A[a]-2))//2)

Submission status: Accepted

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


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


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

ttl = 0
for j in range(l):
    ttl += b[j] * (b[j] - 1) // 2

for k in range(n):
    print(ttl - nC2(b[a[k]]) + nC2(b[a[k]] - 1))

Submission status: Accepted

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

for i in range(n):
    d = c[a[i]]
    x = (d*(d-1))//2 if d > 1 else 0
    y = ((d-1)*(d-2))//2 if d > 2 else 0
    print(s-x+y)

Submission status: Accepted

Solution code:
n=int(input(""))
lista=input("").split(" ")
lisa=[]
listn=[]
for i in range(n+1):
    listn+=[0]
for i in lista:
    lisa+=[int(i)]
    listn[int(i)]+=1
s=0
for i in listn:
    if(i<2):
        continue
    else:
        s+=i*(i-1)/2
for i in lisa:
    print(int(s-listn[i]+1))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
from collections import Counter
cnt = Counter(A)
ans = 0
for key,val in cnt.items():
    if val>=2:
        ans += val*(val-1)//2
for a in A:
    cur = ans
    if cnt[a]>=2:
        cur -= cnt[a]-1
    print(cur)

Submission status: Accepted

Solution code:
import collections
import itertools

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

ac = collections.Counter(A)

dp = [0] * (N+1)
dp2 = [0] * (N+1)
total = 0
for no, num in ac.items():
    dp[no] = int(num*(num-1)/2)
    dp2[no] = dp[no] - (num-1)
    total += dp[no]

for k in range(1,N+1):
    no = A[k-1]
    print(int(total - dp[no] + dp2[no]))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
T = tuple(map(int, input().split()))
C = Counter(T)
d = dict()
sum = 0
for c in C.most_common():
  if c[1] == 0:
    break
  if d.get(c[1]):
    sum += d[c[1]]
  else:
    x = (c[1] * (c[1] - 1)) // 2
    d[c[1]] = x
    sum += x
for t in T:
  x = C[t]
  if x >= 2:
    if d.get(x):
      minus = d[x]
    else:
      minus = (x * (x - 1)) // 2
      d[x] = minus
    if d.get(x-1):
      plus = d[x - 1]
    else:
      plus = ((x - 1) * (x - 2)) // 2
      d[x - 1] = plus
    print(sum - minus + plus)
  else:
    print(sum)

Submission status: Accepted

Solution code:
import math
N = int(input())
K = list(map(int,input().split()))
hashmap = {}
for k in K:
  if k not in hashmap:
    hashmap[k] = 0
  hashmap[k]+=1
total = 0
for v in hashmap.values():
  if v>=2:
  	total+=math.factorial(v) // (math.factorial(v - 2) * 2)

for k in K:
  print(total-(hashmap[k]-1))

Submission status: Accepted

Solution code:
from collections import Counter
N, = list(map(int,input().split()))
A  = list(map(int,input().split()))
c = Counter(A)
p = sum([(c[k]*(c[k]-1)//2) for k in c.keys()])
for ai in A:
    print(p - c[ai] + 1)

Submission status: Accepted

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


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


t = sum(list(map(comb, C)))
for a in A:
    print(t - comb(C[a]) + comb(C[a] - 1))

Submission status: Accepted

Solution code:

from collections import Counter

N = int(input())
X = list(map(int, input().split()))
ctr = Counter(X)

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

for i in range(N):
    val = ctr[X[i]]
    print(ans - val * (val - 1) // 2 + (val - 1) * (val - 2) // 2)

Submission status: Accepted

Solution code:
import sys
import collections
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = collections.Counter(a)
    all_comb = 0
    for i in cnt.keys():
        all_comb += cnt[i] * (cnt[i]-1) // 2
    for i in range(n):
        comb = all_comb
        comb -= cnt[a[i]] * (cnt[a[i]]-1) // 2
        comb += (cnt[a[i]]-1) * (cnt[a[i]]-2) // 2
        print(comb)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter
c = Counter(l)

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

tot = 0
for i,j in c.items():
    if j >= 2:
        tmp1 = j*(j-1)//2
        tot += tmp1
    else:
        tmp1 = 0
    if j >= 3:
        tmp2 = (j-1)*(j-2)//2
    else:
        tmp2 = 0
    num[i] = [tmp1, tmp2]

for k in l:
    print(tot - num[k][0] + num[k][1])

Submission status: Accepted

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

import collections
B = collections.Counter(A)
count = 0
for a in B.values():
    count += (a*(a-1))//2

for a in range(N):
    D = A[a]
    print(count-B[D]+1)

Submission status: Accepted

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


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    A = LI()
    count = [0] * (N + 1)

    for a in A:
        count[a] += 1

    total = 0

    for idx in range(N+1):
        total += count[idx] * (count[idx] - 1) / 2
    for a in A:
        a_count = count[a]
        print(int(total - (a_count * (a_count - 1) /
                           2) + ((a_count - 1)*(a_count - 2)/2)))


main()

Submission status: Accepted

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

from collections import Counter
c = Counter(l)

total = 0
for i in c.values():
    if i != 1:
        total += i*(i-1)//2 # 組み合わせ_重複なしの数の公式を整理

for j in range(n): # 影響する部分のみ再計算(引く→足す)する
    tmp = c[l[j]]
    before = tmp*(tmp-1)//2 
    after = (tmp-1)*(tmp-2)//2
    print(total - before + after)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    ans = 0
    for v in c.values():
        ans += v*(v-1)//2

    for k in range(N):
        v = c[A[k]]
        diff = -v*(v-1)//2 + (v-1)*(v-2)//2
        print(ans + diff)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    ans = 0
    for v in c.values():
        ans += v*(v-1)//2

    for k in range(N):
        v = c[A[k]]
        diff = -v*(v-1)//2 + (v-1)*(v-2)//2
        print(ans + (diff if v != 1 else 0))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
a = {}
ANS = 0
for x,y in Counter(A).items():
    if y >= 2: 
        a[x] = [y, (y*(y-1))//2]
        ANS += (y*(y-1))//2
for i in range(n):
    ans = ANS
    if A[i] in a:
        x = a[A[i]][0]-1
        ans -= a[A[i]][1]
        ans += (x*(x-1))//2
    print(ans)

Submission status: Accepted

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

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

for i in range(n):
    tmp = total
    tmp -= Acount[A[i]]*(Acount[A[i]]-1) // 2
    tmp += (Acount[A[i]]-1)*(Acount[A[i]]-2) // 2
    print(tmp)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()
C = Counter(A)


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


def solve():
    total = 0
    for x, k in C.items():
        total += choose2(k)

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


solve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
C = {i:0 for i in range(1,N+1)}
for i in range(1,N+1):
    C[A[i]] += 1
tot = 0
for i in range(1,N+1):
    tot += (C[i]*(C[i]-1))//2
for i in range(1,N+1):
    c = C[A[i]]-1
    ans = tot-(C[A[i]]*(C[A[i]]-1))//2+(c*(c-1))//2
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
def solve():
  N = int(input())
  A = list(map(int, input().split()))
  c = Counter(A)
  cnt = 0
  for k,v in c.items():
    cnt += v*(v-1)//2
  ans = [cnt]*N
  for i in range(N):
    ans[i] -= c[A[i]]*(c[A[i]]-1)//2
    ans[i] += (c[A[i]]-1)*(c[A[i]]-2)//2
  
  return ans
print(*solve(),sep='\n')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

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

dic = Counter(A)

res = 0
for i, cnt in dic.items():
    res += cnt*(cnt-1)//2


for i in A:
    temp = dic[i]-1
    print(res-temp)

Submission status: Accepted

Solution code:
from collections import Counter

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

cA = Counter(A)

allS = 0
for key in cA:
    allS += cA[key] * (cA[key] - 1) / 2

for i in range(N):
    if A[i] in cA.keys():
        print(int(allS - (cA[A[i]] - 1)))
    else:
        print(int(allS))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
counter = [0 for _ in range(max(A)+1)]
appeard = []
for k in range(N):
  counter[A[k]] += 1
  if counter[A[k]] == 2:
    appeard.append(A[k])

allsum = 0
for item in appeard:
  allsum += counter[item]*(counter[item]-1)//2
for k in range(N):
  if counter[A[k]] > 1:
    print(allsum - (counter[A[k]]-1))
  else:
    print(allsum)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

c = Counter(a)

d = dict(c)
sm = 0
for k, v in c.items():
    mx = (v * (v - 1)) // 2
    nx = ((v - 1) * (v - 2)) // 2
    sm += mx
    d[k] = nx - mx

for v in a:
    print(sm + d[v])

Submission status: Accepted

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

B = [0]*(N+1)

for i in range (0, N):
	B[A[i]]+=1
    
C = 0
import math

for i in range (1, N+1):
	C+=math.comb(B[i],2)
    
for i in range (0, N):
	print(C+math.comb(B[A[i]]-1,2)-math.comb(B[A[i]],2))