Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

Solution code:
import sys 
input = sys.stdin.readline
MOD = 10**9 +7
INF = 10**9
from bisect import bisect_right

def main():
    n = int(input())
    a = list(map(int,input().split()))
    dic = {}
    for num in a:
        if num in dic:
            dic[num] += 1
        else:
            dic[num] = 1
    
    tot = 0
    for v in dic.values():
        if v >= 2:
            tot += v * (v - 1)//2
    
    for num in a:
        if dic[num] == 1:
            print(tot)
        elif dic[num] == 2:
            print(tot - 1)
        else:
            l = dic[num]
            print(tot + 1 - l)        



if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

summary = {}
for a in As:
    if a in summary:
        summary[a] += 1
    else:
        summary[a] = 1

n_comb = {}
for k in summary:
    if 2<=summary[k]:
        n_comb[k] = summary[k]*(summary[k]-1)//2
    else:
        n_comb[k] = 0

x_comb = {}
total = sum([n_comb[k] for k in n_comb])        
for k in summary:
    if 3<=summary[k]:
        x_comb[k] = total - n_comb[k] + (summary[k]-1)*(summary[k]-2)//2
    else:
        x_comb[k] = total - n_comb[k]


for a in As:
    print(x_comb[a])

Submission status: Accepted

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

cnt = [0 for _ in range(n)]
cnt_max = [0 for _ in range(n)]
cnt_mini = [0 for _ in range(n)]

for i in a:
    cnt[i - 1]+=1

for i in range(n):
    cnt_max[i] = cnt[i] * (cnt[i] - 1) // 2
    if cnt[i]!=0:
        cnt_mini[i] = (cnt[i] - 1) * (cnt[i] - 2) // 2

s = sum(cnt_max)
for i in range(n):
    print(s - cnt_max[a[i]-1] + cnt_mini[a[i]-1])

Submission status: Accepted

Solution code:
import unittest


class TestD(unittest.TestCase):
    def test_1(self):
        self.assertEqual(
            think([1, 1, 2, 1, 2]),
            [2, 2, 3, 2, 3]
        )

    def test_2(self):
        self.assertEqual(
            think([1, 2, 3, 4]),
            [0, 0, 0, 0]
        )

    def test_3(self):
        self.assertEqual(
            think([3, 3, 3, 3, 3]),
            [6, 6, 6, 6, 6]
        )

    def test_4(self):
        self.assertEqual(
            think([1, 2, 1, 4, 2, 1, 4, 1]),
            [5, 7, 5, 7, 7, 5, 7, 5]
        )


def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    hist = make_hist(a)
    num_of_pair = calc_num_of_pair(hist)
    sum_of_num_of_pair = 0

    result = [0] * len(a)

    for k, v in num_of_pair.items():
        sum_of_num_of_pair += v

    for i, e in enumerate(a):
        if hist[e] <= 1:
            result[i] = sum_of_num_of_pair
        else:
            n = hist[e]
            result[i] = sum_of_num_of_pair - num_of_pair[e]
            result[i] += ((n - 1) * (n - 2)) // 2
    return result


def make_hist(a):
    hist = {}
    for e in a:
        hist[e] = hist.get(e, 0) + 1
    return hist


def calc_num_of_pair(hist):
    num_of_pair = {}
    for k, v in hist.items():
        if v <= 1:
            num_of_pair[k] = 0
        else:
            num_of_pair[k] = (v * (v - 1)) // 2
    return num_of_pair


def write(result):
    for r in result:
        print(r)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    input()
    a = [int(i) for i in input().split()]
    c = Counter(a)
    s = sum(v * (v - 1) // 2 for v in c.values())
    for i in a:
        print(s - (c[i] - 1))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
def comb(n):
    return n*(n-1)//2
def main():
    n = int(input())
    A = list(map(int,input().split()))
    C = Counter(A)
    mx = sum(comb(b) for b in C.values())
    for a in A:
        print(mx-C[a]+1)
main()

Submission status: Accepted

Solution code:
n = int(input())
a_array = [int(x) for x in input().split()]
d = dict()
ans_d = dict()
for a in a_array:
    d.setdefault(a, 0)
    d[a] += 1
for key, value in d.items():
    ans_d[key] = value*(value-1)//2

sum_count = sum(ans_d.values())
ans = []
for a in a_array:
    tmp = d[a] - 1
    tmp_count = sum_count -ans_d[a]
    ans.append(tmp_count + tmp*(tmp-1)//2)

for a in ans:
    print(a)

Submission status: Accepted

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

c = collections.Counter(a)
d = {}
# print(c)
sum_num = 0
for k, v in c.items():
    d[k] = v*(v-1)//2 - (v-1)*(v-2)//2
    sum_num += v*(v-1)//2
# print(sum_num)
# print(d)
for i in a:
    print(sum_num-d[i])

Submission status: Accepted

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


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


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

Submission status: Accepted

Solution code:
from collections import Counter

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

cmbA = {}
for key, value in cntA.items():
    cmbA[key] = value * (value - 1) // 2

sum_cmbA = sum(cmbA.values())

for n in range(N):
    if cntA[A[n]] == 1:
        print(sum_cmbA)
    else:
        print(sum_cmbA - (cntA[A[n]] - 1))

Submission status: Accepted

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

counts = {}
for e in A:
    if e not in counts:
        counts[e] = 0
    counts[e] += 1

ans = 0
for e in counts:
    ans += counts[e] * (counts[e]-1) // 2

for i in range(0, len(A)):
    print(ans - counts[A[i]] + 1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
x=dict()
for i in range(N):
    if A[i] in x:
        x[A[i]]+=1
    else:
        x[A[i]]=1
sm=0
for idx in x:
    sm+=((x[idx]-1)*x[idx])//2
    #y[idx]=((x[idx]-1)*x[idx])//2
ans=[]
#print(x)
for i in range(N):
    a=sm-(x[A[i]]-1)
    ans.append(a)
for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
nums = list(map(int, input().split()))
length = len(nums)
num_dict = Counter(nums)
all_count = 0
for i in num_dict.keys():
    all_count += ((num_dict[i]-1) * num_dict[i]) //2
    
for i in range(length):
    print(all_count - (num_dict[nums[i]]-1))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

yag = defaultdict(int)

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

result = 0
for i in yag.keys():
    t = yag[i]
    result += (t*(t-1))//2

for i in a:
    t = yag[i]
    print(result - (t*(t-1))//2 + ((t-1)*(t-2))//2)

Submission status: Accepted

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

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

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

ans = 0
for _cnt in cnt:
  ans += _cnt * (_cnt - 1) // 2

for _a in a:
  print(ans - cnt[_a] + 1)

Submission status: Accepted

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

freq_dic = Counter(ls)
ans = 0
for key,val in freq_dic.items():
    if val>1:
        ans += (val*(val-1))//2
for val in ls:
    currans = ans
    if freq_dic[val]>1:
        tempval = freq_dic[val]
        tempans = (tempval*(tempval-1))//2
        newans = 0
        if tempval>2:
            newans += ((tempval-1)*(tempval-2))//2
        currans -= tempans
        currans += newans
    print(currans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
for i in a:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

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

for i in a:
    if i in dic:
        print(total-dic[i]+1)
    else:
        print(total)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
numofnum = [0 for i in range(N)]
for i in range(N):
    numofnum[a[i]-1] += 1
k =0
for i in range(N):
    k += numofnum[i]*(numofnum[i]-1)//2

for i in range(N):
    num = numofnum[a[i]-1]
    x =num*(num-1)//2
    y = (num-1)*(num-2)//2
    print(k-x+y)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(a) for a in input().split()]
count = Counter(A)
all_sum = 0
for key, val in count.items():
    all_sum += val * (val - 1)
all_sum //= 2

for k in range(N):
    num = count[A[k]]
    before = num * (num - 1) // 2
    after = (num - 1) * (num - 2) // 2
    print(all_sum - before + after)

Submission status: Accepted

Solution code:
def MI():return map(int,input().split())
def LI():return list(MI())

from collections import Counter

n=int(input())
a=LI()
c=Counter(a)

# print(c)
M=0
for i,j in c.items():
    M+=(j)*(j-1)//2
# print(M)
for i in a:
    t=c[i]
    d=(t-1)
    print(M-d)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

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

n = ni()
a = na()

import collections
c = collections.Counter(a)

k = 0

for i in c.values():
    if i > 1:
        k += i*(i-1)//2

for j in range(n):
    b = a[j]
    d = 0 if c[b] < 2 else 1 if c[b] == 2 else c[b]-1
    print(k-d)

Submission status: Accepted

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

c = Counter(A)
#print(c)
all_comb = 0

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

for a in A:
    print(all_comb - c[a] + 1)

Submission status: Accepted

Solution code:
N = int(input())

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

balls = [0] * (N + 1)

for a in A_list:
	balls[a] += 1

counts = [0] * (N + 1)
total = 0

for i in range(1, N + 1):
	ball = balls[i]
	count = ball * (ball - 1) // 2
	total += count
	counts[i] = count

for i in range(N):
	a = A_list[i]
	ball = balls[a] - 1
	count = ball * (ball - 1) // 2
	print(total - counts[a] + count)

Submission status: Accepted

Solution code:
n=int(input())
ls=list(map(int,input().split()))
dic={}
for i in ls:
    dic.setdefault(i,0)
    dic[i]+=1
org=0
for v in dic.values():
    org += v*(v-1)//2

for i in ls:
    v = dic[i]
    print(org-(v*(v-1)//2)+((v-1)*(v-2)//2))

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import Counter


def solve(*args: str) -> str:
    n = int(args[0])
    A = list(map(int, args[1].split()))
    counts = Counter(A)
    memo = [0]*(n+1)
    total = 0
    for i, c in counts.items():
        memo[i] = c*(c-1)//2
        total += memo[i]

    ret = []
    for a in A:
        tmp = total - memo[a] + (memo[a]*(counts[a]-2)//counts[a])
        ret.append(str(tmp))

    return '\n'.join(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

from collections import Counter
N = int(input())
A = list(map(int, (input().split())))
c = Counter(A)
def nC2(n):
    return n*(n-1)//2

all_cnt = 0
for k, v in c.items():
    if v>1:
        all_cnt += nC2(v)

for i in range(N):
    k = c[A[i]]
    ans = all_cnt - nC2(k) + nC2(k-1)
    print(ans)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res
# Summarize count of factor within list --- END ---

# nCr -- START --
def nCr(n):
  return (n*(n-1))//2
# nCr --- END ---

def main():
  n=I()
  l=LI()
  mp=[0]*(n+1)

  sl=summarize_list(l)
  # print(sl)

  ans=0
  for x,c in sl:
    mp[x]=c
    ans+=nCr(c)

  # print(mp)
  # print(ans)

  for x in l:
    print(ans-nCr(mp[x])+nCr(mp[x]-1))

main()
# print(main())

Submission status: Accepted

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

total = 0
#print(freq)
for v in freq.values():
    total += int((v*(v-1)/2))
#print(total)
for a in A:
    v = freq[a]
    diff = -int(v*(v-1)/2) + int((v-1)*(v-2)/2)
    print(total+diff)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
*A, = map(int, input().split())
C = Counter(A)
D = {}
for key in C.keys():
    D[key] = C[key] * (C[key] - 1) // 2
S = sum(D.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()))

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

ans = 0
ans_li = [0 for i in range(N+1)]
for i in range(N):
    ans += max(0,(cnt[i]*(cnt[i]-1))//2)
    ans_li[i] = max(0,(cnt[i]*(cnt[i]-1))//2)


for i in range(N):
    mm = cnt[A[i]-1]-1
    mm2 = ans-ans_li[A[i]-1]
    mm3 = max(0,mm*(mm-1)//2)
    # print(mm,mm2,mm3,mm2+mm3)
    print(mm2+mm3)


# print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
astr = list(map(int, input().split()))
for i in astr:
    d[i] = d.get(i, 0) + 1

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

s = sum(f(i)  for i in d.values())


for i in astr:
    t = d[i]
    print(s - f(t) + f(t-1))

Submission status: Accepted

Solution code:
n,*a,=map(int,open(0).read().split())
d={}
for i in a:
    d[i]=d.get(i,0)+1
b=0
for i in d.values():
    b+=i*(i-1)//2
for i in a:
    print(b-d[i]+1)

Submission status: Accepted

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

cnt = {}
for i, Ai in enumerate(A):
    key = str(Ai)
    cnt[key] = cnt.setdefault(key, 0) + 1

tab_full = {}
tab_sub  = {}
for key, val in cnt.items():
    if val == 0: continue
    tab_full[key] = val * (val - 1) // 2
    tab_sub[key] = (val - 1) * (val - 2) // 2

ans_base = sum(tab_full.values())
for i, Ai in enumerate(A):
    key = str(Ai)
    ans = ans_base
    ans -= tab_full[key]
    ans += tab_sub[key]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for ai in a:
  if not a_pop.get(ai):
    a_pop[ai] = 1
  else:
    a_pop[ai] += 1

hoge = sum(v * (v - 1) // 2 for v in a_pop.values())

for ai in a:
  ans = hoge
  v = a_pop[ai]
  print(ans - (v - 1))

Submission status: Accepted

Solution code:
import math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
N = int(input())
arr = list(map(int, input().split()))
cnt = [0]*(1+N)
ans = [0]*(1+N)
ans_less = [0]*(1+N)
for i in range(N):
    cnt[arr[i]] += 1
for i in range(1,N+1):
    if cnt[i] == 0 or cnt[i] == 1:
        continue
    ans[i] = C(cnt[i],2)
    if cnt[i] >= 3:
        ans_less[i] = C(cnt[i]-1,2)
    else:
        ans_less[i] = 0
su = sum(ans)
for i in range(N):
    print(su-ans[arr[i]]+ans_less[arr[i]])

Submission status: Accepted

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

import collections
c = collections.Counter(a)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
from collections import Counter
def fac(n):
    return (n*(n-1))/2

cnt = Counter(A)

total = 0
for k,v in cnt.items():
    total += fac(cnt[k])

for a in A:
    print(int(total + fac(cnt[a]-1) - fac(cnt[a])))

Submission status: Accepted

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

l = [0] * (2*10**5)
for i in a:
    l[i-1] += 1

ans = 0
for i in l:
    ans += i*(i-1)//2

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

Submission status: Accepted

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

from collections import Counter
c = Counter(A)
m = {}
total = 0
for k, v in c.items():
    a = v*(v-1)//2
    m[k] = a
    total += a

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

Submission status: Accepted

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

Submission status: Accepted

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

X = [0]*N

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

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

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

Submission status: Accepted

Solution code:
n=int(input())

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

import collections

c = collections.Counter(a)
cc=c.keys()

maxsum=0
for item in cc:
    temp=c[item]
    if temp>1:
        maxsum+=(temp)*(temp-1)//2

for item in a:
    temp=c[item]
    if temp>1:
        t=maxsum-temp*(temp-1)//2+(temp-1)*(temp-2)//2
        print(t)
    else:
        print(maxsum)

Submission status: Accepted

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

c = {}

for i in range(n):
  if a[i] in c:
    c[a[i]] += 1
  else:
    c[a[i]] = 1
  
s = 0
for i in c:
  s += c[i]*(c[i]-1)//2
  
dic = {i: s-c[i]+1 for i in c}
  
for i in range(n):
  print(dic[a[i]])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
count = Counter(a)
kind = list(set(a))
ans_count = dict()
sum_ans = 0
for i in range(len(kind)):
    num = kind[i]
    k = count[num] 
    sum_ans += k*(k-1)//2
for i in range(n):
    ans = sum_ans
    num = a[i]
    k = count[num]
    ans -= k*(k-1) // 2
    ans += (k-1)*(k-2)//2
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
d={}
for i in arr:
	if i in d:
		d[i]+=1
	else:
		d[i]=1
ans=0
for key,value in d.items():
	ans+=(value*(value-1))//2
for i in range(len(arr)):
	print(ans-(d[arr[i]]-1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int,input().split()))
#test
#a = [i for i in range(1,2*10**5+1)]

bucket = [0] * (2*10**5+1)
comb_bucket = [0] * (2*10**5+1)
for i in a:
    bucket[i]+=1

for i,v in enumerate(bucket):
    if v==0:
        continue
    comb_bucket[i] = (v * (v-1))//2
total_comb = sum(comb_bucket)
for ai in a:
    bi = bucket[ai]
    if bi == 1:
        print(total_comb)
    elif bi == 2:
        print(total_comb-1)
    else:
        print(total_comb - (bi-1))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
*arr, = map(int, input().split())


cnt = Counter(arr)

ans = 0

total = 0

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


for v in arr:
    print(total - cnt.get(v, 1) + 1)

Submission status: Accepted

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

for a in A:
    x = c[a]
    ans = s-x*(x-1)//2+(x-1)*(x-2)//2
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
count = {}
for i in a:
    if i in count:
        count[i] += 1
    else:
        count[i] = 1

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

for i in a:
    if count[i] == 1:
        print(ans)
    else:
        o = count[i]
        pi = ans - o*(o-1)//2
        pi += (o-1)*(o-2)//2
        print(pi)

Submission status: Accepted

Solution code:
from collections import Counter


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


def submit():
    n = int(input())
    alist = list(map(int, input().split()))
    a_count = Counter(alist)
    total = sum(comb(v) for k, v in a_count.items())

    for a in alist:
        print(total - (a_count[a] - 1))


submit()

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
h = {}
for x in a:
    if x not in h:
        h[x] = 0
    h[x] += 1
tot = 0
for x in h:
    tot += h[x]*(h[x]-1)//2
for i in range(n):
    y = h[a[i]]
    print(tot-y*(y-1)//2+(y-1)*(y-2)//2)

Submission status: Accepted

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

counter = Counter(A)
cnts = counter.most_common()
total = 0
for cnt in cnts:
    total += cnt[1]*(cnt[1]-1)//2

for ai in A:
    print(total-(counter[ai]-1))

Submission status: Accepted

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

cnt={}
for i in A:
    if i not in cnt:cnt[i]=1
    else:cnt[i] +=1

B={}
C={}
for v,m in cnt.items():
    B[v]=m*(m-1)//2
    C[v]=(m-1)*(m-2)//2

point=sum(B.values())

for i in A:
    print(point-B[i]+C[i])

Submission status: Accepted

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

di = {}
B = set(A)
for b in B:
  di[b] = 0
total = 0
for a in A:
  di[a]+=1
for v in di.values():
  total += v*(v-1)//2
#print(di.values(),total)
ans = []
for a in A:
  ans.append(total-di[a]+1)
print(*ans,sep='\n')

Submission status: Accepted

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

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

d = defaultdict(int)
for i in A :
    d[i] += 1

ans = 0
ans2 = defaultdict(int)
for a in d :
    ans += d.get(a)*(d.get(a)-1)//2
    ans2[a] = d.get(a)*(d.get(a)-1)//2 - (d.get(a)-1)*(d.get(a)-2)//2

for i in A :
    print(ans - ans2[i])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
def comb(num):
    return num*(num-1)//2

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

log=[0]*N
for item in A:
    log[item-1]+=1

sm=[0]*N
for index,item in enumerate(log):
    sm[index]=comb(item)

temp=sum(sm)

for k in range(N):
    ans=temp
    #print(log[A[k]-1])
    ans-=sm[A[k]-1]
    ans+=comb(log[A[k]-1]-1)
    print(ans)

#print(log)
#print(sm)

Submission status: Accepted

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

A_count = collections.Counter(A)
A_count_nCr = {}

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

all_count = 0

for k, v in A_count.items():
    if v >= 2:
        A_count_nCr[k] = cmb(v, 2)
    else:
        A_count_nCr[k] = 0
    all_count += A_count_nCr[k]

for a in A:
    if A_count[a]-1 >= 2:
        print(all_count - A_count_nCr[a] + cmb(A_count[a]-1, 2))
    else:
        print(all_count - A_count_nCr[a])

Submission status: Accepted

Solution code:


def resolve():
    N=int(input())
    A=[int(i)-1 for i in input().split()]

    Atbl=[0]*N
    for i in A:
        Atbl[i]+=1
    cmpsum = 0
    for i in Atbl:
        cmpsum+=i*(i-1)//2
    for i in A:
        print(cmpsum-Atbl[i]+1)






if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

    num_balls = [0 for i in range(N+1)]
    for a_i in A:
        num_balls[a_i] += 1

    comb = 0
    for n in range(N+1):
        comb += num_balls[n] * (num_balls[n]-1) // 2

    for a_i in A:
        ori = num_balls[a_i] * (num_balls[a_i]-1) // 2
        rev = (num_balls[a_i]-1) * (num_balls[a_i]-2) // 2
        print(comb + rev - ori)


solve()

Submission status: Accepted

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

d = defaultdict(int)
c = Counter(A)
total = sum(x * (x - 1) // 2 for x in c.values())

ans = []
for a in A:
    v = c[a]
    subt = v * (v - 1) // 2
    v -= 1
    add = v * (v - 1) // 2
    ans.append(total - subt + add)

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
sumnum = 0
for key,value in c.items():
    if value >= 2:
        sumnum += value*(value-1)//2

for i in range(n):
    key = [a[i]]
    ans = sumnum
    value = c[a[i]]
    if value >= 3:
        ans -= value*(value-1)//2
        ans += (value-2)*(value-1)//2
    if value <= 2:
        ans -= value*(value-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)
x = []
r = 0
for x, y in c.items():
    r += (y-1)*y//2

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

Submission status: Accepted

Solution code:
def nc2(n):
	return (n*(n-1))//2
n=int(input())
arr=[int(x) for x in input().split()]
cnt={}
for x in arr:
	if x in cnt:
		cnt[x]+=1
	else:
		cnt[x]=1
ans=0
for x in cnt:
	ans+=nc2(cnt[x])
for x in arr:
	print(ans+nc2(cnt[x]-1)-nc2(cnt[x]))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

sumans = sum(ans)
for i in a:
    print(sumans - ans[i] + (tmp[i]-1)*(tmp[i]-2)//2)

Submission status: Accepted

Solution code:
import math
from collections import Counter

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

def cmb(r,k):
  if r < k: return 0
  return math.factorial(r) // math.factorial(r-k) // math.factorial(k)

c = Counter()
for a in A:
  c[a] += 1
al = 0
for v in c.values():
  al += cmb(v,2)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
input = raw_input
n = int(input())
nums = map(int, input().split(" "))

counter = collections.Counter(nums)
values = counter.keys()
all_out = 0
for val, num in counter.items():
    if num == 1:
        continue
    else:
        all_out += num * (num-1) // 2

outs = {}
for val, num in counter.items():
    if num == 1:
        outs[val] = all_out
    else:
        outs[val] = all_out - (num-1)

for num in nums:
    print(outs[num])

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

SUM = 0
for c in cnt.values():
    if c >= 2:
        SUM += c * (c - 1) // 2

for a in A:
    c = cnt[a]
    tmp = c * (c - 1) // 2
    if c > 2:
        tmp -= (c - 1) * (c - 2) // 2
    print(SUM - tmp)

Submission status: Accepted

Solution code:
inxx = input()
inxx = inxx.split()

n = int(inxx[0])

inxx = input()
inxx = inxx.split()

arr = []
for x in inxx:
    arr.append(int(x))

import collections
cc = collections.Counter(arr)

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

s = sum(choose2(x) for x in cc.values())

for x in arr:
    sol = s - cc[x] + 1
    print(sol)

Submission status: Accepted

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

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

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

sum_comb = 0
for k, v in cntd.items():
    sum_comb += comb2(v)

#print(cntd)
#print(sum_comb)
for i in range(N):
    k = A[i]
    ans = sum_comb - comb2(cntd[k]) + comb2(cntd[k]-1)
    print(ans)