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
from collections import Counter
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))

n = INT()
a = LIST()
c = list(set(a))
b = [0 for _ in range(n + 1)]
for i in range(n):
    b[a[i]] += 1
d = [0 for _ in range(n + 1)]
e = [0 for _ in range(n + 1)]
for i in range(n + 1):
    if b[i] >= 2:
        d[i] = b[i] * (b[i] - 1) // 2
    if b[i] >= 3:
        e[i] = (b[i] - 1) * (b[i] - 2) // 2
sd = sum(d)
se = sum(e)
for k in range(n):
    ans = sd - d[a[k]] + e[a[k]]
    print(ans)

Submission status: Accepted

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

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

for i in range(len(a)):
    p=dic[a[i]]
    print(s-p*(p-1)//2+(p-1)*(p-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter

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

ans = 0
for i in b:
    ans += b[i] * (b[i]-1) // 2
    
for i in a:
    if b[i] < 2:
        sa = 0
    else:
        sa = b[i] - 1
    print(ans - sa)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import Counter
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
c = Counter(A)

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

for i in A:
    cnt = c[i]
    ans = s
    if cnt != 1:
        ans -= (cnt*(cnt-1)//2)
        ans += ((cnt-1)*(cnt-2)//2)
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
balls = defaultdict(int)
default_pattern = defaultdict(int)
pick_pattern = defaultdict(int)

for ai in A:
    balls[ai] += 1

total = 0
for k, v in balls.items():
    remain_balls = v - 1
    if v <= 1:
        default_pattern[k] = 0
    else:
        default_pattern[k] = (v * (v - 1)) // 2
        total += (v * (v - 1)) // 2

    if remain_balls <= 1:
        pick_pattern[k] = 0
    else:
        pick_pattern[k] = (remain_balls * (remain_balls - 1) // 2)

for ai in A:
    ans = total
    print(ans - default_pattern[ai] + pick_pattern[ai])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
d=dict()
for i in range(n):
  if a[i] not in d:
    d[a[i]]=1
  else:
    d[a[i]]+=1
s=0
l=list(d.items())
for i in range(len(l)):
  if l[i][1]>=2:
    s+=l[i][1]*(l[i][1]-1)//2
for i in range(n):
  if d[a[i]]<2:
    print(s)
  else:
    print(s-(d[a[i]]-1))

Submission status: Accepted

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

ans = 0

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

B = sorted(A)

cnt = 1
for i in range(len(B)-1):
    if(B[i] == B[i+1]):
        cnt += 1
    else:
        ans += cnt * (cnt - 1) // 2
        cnt = 1
ans += cnt * (cnt - 1) // 2


for a in A:
    ansi = ans
    num = h[a]
    ansi -= num * (num - 1) // 2
    num -= 1
    ansi += num * (num - 1) // 2
    print(ansi)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
for ai in a:
    d[ai] += 1
s = 0
for v in d.values():
    s += v * (v - 1) // 2
c = {}
for k, v in d.items():
    c[k] = s - v * (v - 1) // 2 + max(0, (v - 1) * (v - 2) // 2)
for ai in a:
    print(c[ai])

Submission status: Accepted

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

C = [0 for _ in range(N)]
for a in A:
  a -= 1
  C[a] += 1
D = [0 for _ in range(N)]
for i in range(N):
  if C[i] >= 2:
    D[i] = C[i]*(C[i]-1)//2
s = sum(D)
for a in A:
  a -= 1
  if C[a] <= 2:
    print(s-D[a])
  else:
    print(s-D[a]+(C[a]-1)*(C[a]-2)//2)

Submission status: Accepted

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

D = dd(int)
for i in range(len(A)):
  D[A[i]] += 1
def C(n):
  if n <= 1:
    return 0
  else:
    return n*(n-1)//2
  
ans = 0
for k,v in D.items():
  ans += C(v)
for i in range(N):
#  print (A[i], D[A[i]])
  print (ans - C(D[A[i]]) + C(D[A[i]]-1))

Submission status: Accepted

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

def main():
    N = int(input())
    A = list(map(int, input().split()))
    counter = Counter(A)
    combs = {i: n*(n-1)//2 for i, n in counter.items()}
    total = sum(combs.values())

    dp = [None] * (N+1)
    for i in A:
        if dp[i] is None:
            tmp = total - combs[i]
            new = counter[i] - 1
            dp[i] = tmp + (new * (new-1) // 2)
            yield str(dp[i])
        else:
            yield str(dp[i])


print("\n".join(main()))

Submission status: Accepted

Solution code:
def comb2(num):
    return num * (num - 1) // 2


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

# create hash table
table = {}
for i in range(n):
    if a[i] not in table:
        table[a[i]] = 1
    else:
        table[a[i]] += 1

total = 0
for idx in table:
    total += comb2(table[idx])

for i in range(n):
    sub = comb2(table[a[i]])
    add = comb2(table[a[i]] - 1)
    print(total - sub + add)

Submission status: Accepted

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

count_dict = {}
for i in range(N):
    now_num = A_list[i]
    count_dict[now_num] = count_dict.get(now_num, 0) + 1

base_ans = 0
for _, v in count_dict.items():
    base_ans += (v * (v - 1)) // 2
    
for i in range(N):
    now_num = count_dict[A_list[i]]
    print(base_ans - (now_num * (now_num - 1) - ((now_num - 1) * (now_num - 2))) // 2)

Submission status: Accepted

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

counts = {}

for a in A:
    if a not in counts:
        counts[a] = 1
    else:
        counts[a] += 1

alls = 0
for k, v in counts.items():
    if v >= 2:
        alls += v * (v - 1) // 2
for a in A:
    acc = counts[a]
    print(alls - (acc - 1))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = [int(x) for x in input().split()]
d = Counter(a)
ans = 0
for i in d.values():
    ans += i * (i-1) // 2
for i in range(n):
    ans_m = ans
    z = a[i]
    zz = d[z]
    ans_m -= zz-1
    print(ans_m)

Submission status: Accepted

Solution code:


def cmb(n,r):#r==2固定
    return n*(n-1)//2

def main():
    import sys
    from collections import defaultdict

    N=int(sys.stdin.readline())
    AL=list(map(int,sys.stdin.readline().split()))
    dic=defaultdict(int)
    for a in AL:
        dic[a]+=1


    K=[cmb(v,2) for k,v in dic.items()]
    total=sum(K)

    for i in range(N):
        a=dic[AL[i]]
        print(total-cmb(a,2)+cmb(a-1,2))

main()

Submission status: Accepted

Solution code:
import sys
import math
import collections
from collections import deque
n = int(input())
#n, m = map(int, sys.stdin.readline().split())
#s = input()
a = list(map(int, sys.stdin.readline().split()))

def com(m):
    return math.factorial(m) // (math.factorial(m - 2) * 2)



c = collections.Counter(a)
d = c.most_common()
max = 0
for i in range(len(d)):
    if d[i][1] < 2:
        break

    max += com(d[i][1])


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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = {}
aa = [0,0]
for i in range(1,n):
	aa.append(aa[-1]+i)
for i in a:
	if i not in d:
		d[i] = 1
	else:
		d[i]+=1
s = 0
for i in d:
	s+=aa[d[i]]
dd = {}
for i in d:
	if i not in dd:
		dd[i] = (s-aa[d[i]]+aa[d[i]-1])
for i in a:
	print(dd[i])

Submission status: Accepted

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

from math import factorial
from operator import mul
from functools import reduce

def comb(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

dicts = {}

N=one_int()

A = many_int()

# 辞書作成
for a in A:
    if a in dicts:
        dicts[a] += 1
    else:
        dicts[a] = 1

# 2個選ぶ組み合わせ計算
dicts_less ={}
sums = 0
for k, v in dicts.items():
    if v>=2:
        sums += comb(v,2)
    if v<2:
        dicts_less[k]=0
    elif v==2:
        dicts_less[k]=comb(v,2) - 0
    else:
        dicts_less[k]=comb(v,2) - comb(v-1,2)

for a in A:
    print(int(sums - dicts_less[a]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(An)

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

for a in An:
    print(res - (c[a]-1))

Submission status: Accepted

Solution code:
from itertools import groupby
n = int(input())
A = list(map(int, input().split()))
B = A[:]
B.sort()
GB = {key: len(list(group)) for key, group in groupby(B)}
# print(GA)
# print(GA[1])
sums = 0
for i, v in GB.items():
  sums += v * (v - 1) // 2
# print(sums)
for a in A:
  print(sums - (GB[a] * (GB[a] - 1) // 2) + ((GB[a] - 1) * (GB[a] - 2) // 2))

Submission status: Accepted

Solution code:
def RA():
    l = input().split()
    return list(map(lambda x: int(x), l))

n = int(input())
a = RA()
m = {}
for ai in a:
    if not ai in m.keys():
        m[ai] = 1
    else:
        m[ai] += 1
total = 0
for ai in m.keys():
    c = m[ai]
    total += int(c * (c - 1) / 2)
    m[ai] = int((c * (c - 1) - (c - 1) * (c - 2)) / 2)
for ai in a:
    print(total - m[ai])

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)
S = sum([n * (n - 1) // 2 for n in c.values()])
for i in range(N):
    print(S - (c[A[i]] - 1))

Submission status: Accepted

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

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

for key, val in c.items():
    d[key] = fac2(val)

sum_val = sum(d.values())
for a in A:
    ans = sum_val - d[a] + fac2(c[a]-1)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

def f(x):
    res = x * (x - 1) // 2

    return max(0, res)


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

if n == 1:
    print(0)
    exit()

counts = [0] * (n + 1)

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

s = sorted(counts)
m1 = s[-1]
m2 = s[-2]


max_result = 0
for k in range(1, n + 1):
    max_result += f(counts[k])

for k in range(1, n + 1):
    kk = a[k - 1]
    print(max_result - f(counts[kk]) + f(counts[kk] - 1))

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split(' ')))
A_count = Counter(A)
S=sum([i*(i-1)//2 for i in A_count.values()])
A_dic = {k:v*(v-1)//2-(v-1)*(v-2)//2 for k,v in A_count.items()}
for i in A:
    print(S-A_dic[i])

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
ac = Counter(A)
comblist = [0 for i in range(max(ac.values())+1)]
if max(ac.values()) == 1:
    for i in range(N):
        print(0)
else:
    comblist[2] = 1
    for i in range(3,max(4,max(ac.values())+1)):
        comblist[i] = comblist[i-1]*i//(i-2)
    ans = {}
    anssum = 0
    for k,v in ac.items():
        if v > 1:
            anssum += comblist[v]
        if k not in ans:
            if v == 2:
                ans[k] = -1
            elif v == 1:
                ans[k] = 0
            else:
                ans[k] = comblist[v-1] - comblist[v]
    for i in A:
        print(anssum+ans[i])

Submission status: Accepted

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

arr = [0 for i in range(N+1)]

for i in range(len(List)):
  a = List[i]
  arr[a] += 1
  
ans1 = [0 for i in range(N+1)]
ans2 = [0 for i in range(N+1)]

for i in range(len(arr)):
  a = arr[i]
  b = int(a*(a-1)/2)
  if a == 0:
    c = 0
  else:
    c = int((a-1)*(a-2)/2)
    
  ans1[i] = b
  ans2[i] = c
    
total = sum(ans1)

for i in range(len(List)):
  a = List[i]
  print(int(total - ans1[a] + ans2[a]))

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)
# print('c', c)


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


combi = {key: f(value) for key, value in c.items()}
# print('combi', combi)
sum_combi = sum(combi.values())
# print('sum_combi', sum_combi)

for i in range(N):
    v = c[A[i]]
    diff = f(v) - f(v - 1)
    print(sum_combi - diff)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
dic = {}
for A in As:
    if A in dic.keys():
        dic[A] += 1
    else:
        dic[A] = 1
total = 0
for A in dic.keys():
    a = dic[A]
    if a > 1:
        temp = a*(a-1)/2
        total += temp
for i in range(N):
    Ai = As[i]
    if dic[Ai] == 2:
        print(int(total-1))
    elif dic[Ai] > 2:
        a = dic[Ai]
        diff = (a*(a-1) - (a-1)*(a-2))/2
        print(int(total - diff)) 
    else:
        print(int(total))

Submission status: Accepted

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

from collections import Counter
c = Counter(a)

ans0 = sum(cc*(cc-1)//2 for cc in c.values())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f(x):
	return x * (x-1) // 2

n = int(input())
a = list(map(int, input().split()))
b = [0] * n
c = [0] * n
d = [0] * n
e = [0] * n
s = 0
for aa in a:
	b[aa-1] += 1
for i in range(n):
	c[i] = f(b[i])
	d[i] = f(b[i] - 1)
	s += c[i]
for i in range(n):
	e[i] = s - c[i] + d[i]
# print(b,c,d,e)
for i in range(n):
	print(e[a[i]-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
r = [0 for i in range(n+1)]
t = [0 for i in range(n+1)]
for ai in a:
    r[ai] += 1
def c2(n):
    return n * (n-1) // 2
t = list(map(lambda i:c2(i-1), r))
r = list(map(c2, r))
s = sum(r)
for i in range(n):
    l = a[i]
    print(s - r[l] + t[l])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ball = dict()
    for i in range(n):
        if a[i] in ball:
            ball[a[i]] += 1
        else:
            ball[a[i]] = 1

    ans = 0
    for b in ball:
        if ball[b] > 1:
            ans += ball[b] * (ball[b]-1) // 2

    for i in range(n):
        if a[i] in ball and ball[a[i]] > 1:
            tmp = ans
            tmp -= ball[a[i]] * (ball[a[i]]-1) // 2
            tmp += (ball[a[i]]-1) * (ball[a[i]]-2) // 2
            print(tmp)
        else:
            print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  la = list(map(int, input().split(' ')))
  counts = [0] * (n+1)
  
  def f(n):
    return int((n * (n-1)) / (2))
  
  for a in la:
    counts[a] += 1
    
  ans = 0
  for count in counts:
    if count >= 2:
      ans += f(count)
      
  for a in la:
    if counts[a] >= 2:
      fc = f(counts[a])
      count = counts[a]
      print(ans - fc + int(fc * ((count-2) / count)))
    else:
      print(ans)    

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def cmb(n, r):
    p = 1
    c = 1
    for i in range(r):
        p *= (n - i)
        c *= (i + 1)
    return p // c


def main():
    N = I()
    A = LI()
    a = Counter(A)
    c = 0
    org = {}
    one = {}
    org_tital = 0
    for k, v in a.items():
        org[k] = cmb(v, 2)
        one[k] = cmb(v-1, 2)
        org_tital += org[k]

    # print(org, one, org_tital)

    for i in range(N):
        diff = org[A[i]] - one[A[i]]
        print(org_tital - diff)
    # print()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
x=int(input())
y=list(map(int, input().split()))
z=copy.deepcopy(y)
y.sort()
n=[0]*(len(y)+1)
t=0
ans=0
for i in range(len(y)):
	t=y[i]
	n[t]=n[t]+1

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

for i in range (len(z)):
	t=z[i]
	k=n[t]
	print(ans-((k*(k-1))//2)+(((k-1)*(k-2))//2))

Submission status: Accepted

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

tmp = {}
for i, a in enumerate(A):
  if a not in tmp: tmp[a] = 0
  tmp[a] += 1

total = 0
map = {}
for a, v in tmp.items():
  n = v * (v - 1) // 2
  total += n
  map[a] = (n, n - max(0, (v - 1) * (v - 2) // 2))

for k in range(N):
  a = A[k]
  print(total - map[a][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())

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

c = Counter(la)

total = 0
for i, num in c.most_common():
  if num > 1:
    total += num * (num-1) // 2
  
for ai in la:
  n = c[ai]
  print(total-n+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

def n_combi_2(n):
    if n < 2:
        return 0
    return n * (n - 1) // 2
  
N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)

patterns_by_number = {key: n_combi_2(counter[key]) for key in counter.keys()}
total = sum(patterns_by_number.values())
res_dict = {}
for a in A:
    if a in res_dict.keys():
        print(res_dict[a])
    else:
        res = total - patterns_by_number[a] * (2) // counter[a]
        res_dict[a] = res
        print(res)

Submission status: Accepted

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

dic = {}
for v in a:
  tmp = dic.get(v, 0) + 1
  dic[v] = tmp
  
ans_dic1 = {}
ans_dic2 = {}
for k in dic.keys():
  tmp = dic.get(k)
  ans_dic1[k] = tmp * (tmp-1) // 2
  ans_dic2[k] = (tmp-1) * (tmp-2) // 2
  
plus = sum(ans_dic1.values())


for i in range(n):
  ans = plus - ans_dic1[a[i]] + ans_dic2[a[i]]
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
num_list = [0]*(2*(10**5)+10)       
  
for i in A:
  num_list[i] += 1

sum_all = 0
for i in range(2*(10**5)+10):
  n = num_list[i]          
  sum_all += n*(n-1)//2 
  
for i in A:  
  n = num_list[i]          
  ans = sum_all - n*(n-1)//2 + (n-1)*(n-2)//2
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

def solve(N, A):
    counter = Counter(A)
    nc2_sum = sum([nC2(n) for _, n in counter.items()])
    for k in range(N):
        n = counter[A[k]]
        u = nc2_sum - nC2(n) + nC2(n-1)
        print(u)


if __name__ == '__main__':
    inputs = read()
    solve(*inputs)

Submission status: Accepted

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


for x in a:
  z=cnt[x-1]
  if z==1:
    print(prm)
  else:
    print(prm-z+1)

Submission status: Accepted

Solution code:
N = int(input())

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

dic = {}
for A in As:
    if A in dic.keys():
        dic[A] += 1
    else:
        dic[A] = 1

total = 0
for A in dic.keys():
    a = dic[A]
    if a > 1:
        temp = a*(a-1)/2
        total += temp

for i in range(N):
    Ai = As[i]
    if dic[Ai] == 2:
        print(int(total-1))
    elif dic[Ai] > 2:
        a = dic[Ai]
        diff = (a*(a-1) - (a-1)*(a-2))/2
        print(int(total - diff)) 
    else:
        print(int(total))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

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

for a in A:
    ans = res
    num = cnt[a]
    ans -= num * (num - 1) // 2
    ans += max((num - 1) * (num - 2) // 2, 0)
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
num = {}
for a in A:
    if a in num:
        num[a] += 1
    else:
        num[a] = 1
total = 0
for a in num:
    n = num[a]
    total += int(n * (n-1) / 2)
for a in A:
    print(total - num[a] + 1)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

c = Counter(A)
c_c = defaultdict(int)
for key, value in c.items():
    c_c[key] = value * (value - 1) // 2
wa = 0
for key, value in c_c.items():
    wa += value

ruiseki = [0] * N

for i in range(N):
    new_value = c[A[i]] - 1    
    ruiseki[i] = wa - c_c[A[i]] + new_value * (new_value - 1) // 2
    # value * (value - 1) // 2
    # c_c[A[i]]
print(*ruiseki, sep= "\n")

Submission status: Accepted

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

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

s0 = sum([d[k] * (d[k] - 1) // 2 for k in d.keys()])

def c2(i):
    if i == 1:
        return 0
    else:
        return i * (i-1) // 2


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

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    A = tuple(iim())

    d1 = dict()
    for ai in A:
        if ai in d1:
            d1[ai] += 1
        else:
            d1[ai] = 1

    aa = []
    cnt = 0
    d2 = dict()
    for k, v in d1.items():
        v = v * (v-1) // 2
        cnt += v
        d2[k] = v

    for ak in A:
        ans = 0
        v = d1[ak] - 1
        v = v * (v-1) // 2
        aa.append(cnt-d2[ak]+v)
    print(*aa, sep="\n")


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
C = Counter(A)
t = {k: int(v*(v-1)/2) for k, v in C.items()}
s = sum(t.values())

for a in A:
    res = s - t[a] + int((C[a]-1)/2*(C[a]-2))
    print(res)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
A = LI()

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

def cnt_ans(ball_sets):
    cnt = 0
    for ball_set in ball_sets:
        if ball_set > 1:
            cnt += ball_set * (ball_set - 1) // 2
    return cnt
    
basic_ans = cnt_ans(ball_dic.values())

for i in range(N):
    n = ball_dic[A[i]]
    if n > 1:
        ans = basic_ans - (ball_dic[A[i]] - 1)
    else:
        ans = basic_ans
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict



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

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

sel = defaultdict(int)
total = 0
for k, v in cnt.items():
    total += v*(v-1)//2

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

Submission status: Accepted

Solution code:
#abc159 d

import itertools
from math import factorial
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7

#ここから書き始める
n = int(input())
cnt = [0] * n
a = list(map(int, input().split()))
for i in a:
    cnt[i - 1] += 1
total = 0
for i in cnt:
    total += i * (i - 1) // 2
for i in a:
    total2 = total
    if cnt[i - 1] >= 1:
        total2 -= cnt[i - 1] * (cnt[i - 1] - 1) // 2
        total2 += (cnt[i - 1] - 1) * (cnt[i - 1] - 2) // 2
    print(total2)

Submission status: Accepted

Solution code:
def main():
    import collections

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

    balls_dict = collections.Counter(balls)
    num_patterns = 0
    for value in balls_dict.values():
        num_patterns += value * (value-1) // 2

    for ball in balls:
        print(num_patterns - balls_dict[ball] + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = {}
for ai in a:
    if ai in cnt:
        cnt[ai] += 1
    else:
        cnt[ai] = 1
s = sum([v * (v - 1) // 2 for _, v in cnt.items()])
dic = { k: s - (v - 1) for k, v in cnt.items() }
ans = "\n".join(map(lambda ai: str(dic[ai]), a))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().strip().split()))
C = Counter(A)
ans = 0
for k, v in C.items():
    ans += v*(v-1)//2

for a in A:
    count = C[a]
    if count < 2:
        print(ans)
    else:
        print(ans-count+1)

Submission status: Accepted

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

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

cnt = 0

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

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