Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

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

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

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Accepted

Solution code:
from collections import Counter

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

S = Counter(l)

ans = 0
for i in S.values():
    ans += int(i * (i - 1) / 2)

for i in range(n):
    val = S[l[i]]
    print(ans - val + 1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


############ ---- Input Functions ---- ############
def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


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




n = in_int()

a = in_list()

m = {}
max_1 = 0
val_1 = -1
max_2 = 0
val_2 = -1
for xx in a:
    if xx in m:
        m[xx]+=1
    else:
        m[xx] = 1


ans = 0

for xx in m:
    tt = m[xx]
    ans += tt*(tt-1)//2



for xx in a:
    tt = m[xx]

    print(ans - (tt)*(tt-1)//2 + (tt-1)*(tt-2)//2)

Submission status: Accepted

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

sub = collections.Counter(a)
ans = 0
for i in sub:
    ans += sub[i] * (sub[i] - 1) // 2
for i in range(n):
    ad = sub[a[i]] * (sub[a[i]] - 1) // 2 - ((sub[a[i]] - 1) * (sub[a[i]] - 2) // 2)
    print(ans - ad)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
li=[0 for i in range(n+1)]
for i in a:
  li[i]+=1
co=[]
cok=[]
for i in li:
  co.append(int(i*(i-1)/2))
  cok.append(int((i-1)*(i-2)/2))
su=sum(co)
for i in a:
  print(su-co[i]+cok[i])

Submission status: Accepted

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


a_ol = Counter(a)
def_count = 0
for item in a_ol.items():
    a_co = item[1]
    def_count += a_co * (a_co - 1)

def_count //= 2

for k in a:
    print(def_count - a_ol[k] + 1)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
d=Counter(A)
dic={}
total=0
for item in d.items():
    counter=item[1]
    s1=(counter - 1)*(counter - 2)/2 #1個少ない
    s2=(counter)*(counter - 1)/2
    dic[item[0]]=int(s1 - s2)
    total+=int(s2)

for a in A:
    print(total + dic[a])

Submission status: Accepted

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

import collections

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

c = collections.Counter(A)
values = c.keys()
counts = c.values()

subs = {}
a_sum = 0
for v, c in zip(values, counts):
    subs[v] = c
    a_sum += (c * (c-1)) // 2

for i in range(N):
    print(a_sum - subs[A[i]] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

for i in range(n):
    ball = balls[i]
    num = c.get(ball)
    ans = total - nC2(num) + nC2(num - 1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

    c = Counter(A)
    vals = c.keys()
    cnts = c.values()

    subs = {}
    a_sum = 0
    for v, c in zip(vals, cnts):
        subs[v] = c - 1
        a_sum += (c * (c-1)) // 2
    
    for a in A:
        print(a_sum - subs[a])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

    for i in range(N):
        count[A[i]] += 1
    
    for i in range(1,N+1):
        if count[i] >= 2:
            num[i] = int(count[i]*(count[i]-1)/2)
            sum_all += num[i]

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

main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
import collections
import math
def combinations_count2(n):
    if(n<=1):
        answer=0
    else:
        answer=n*(n-1)/2
    return answer

table=collections.Counter(a)
keytable=table.keys()
sums=0
for value in table.values():
    sums+=combinations_count2(value)
#print(ca)
#print(sum)
#print(keytable)
#print(table)
for i in range(len(a)):
    answer=sums-table[a[i]]+1
    print(int(answer))

Submission status: Accepted

Solution code:
#159 d bannedK
n=int(input())
a=list(map(int,input().split()))

bunpu=dict()
for x in a:
    if x not in bunpu:
        bunpu[x]=1
    else:
        bunpu[x]+=1
    
def choose2(num):
    return num*(num-1)//2

total=0
for value in bunpu.values():
    total+=choose2(value)
for x in a:
    ans=total
    ans-=choose2(bunpu[x])
    ans+=choose2(bunpu[x]-1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=[int(item)for item in input().split()]
c=Counter(A)
full = {i: c[i] * (c[i] - 1) / 2 for i in c.keys()}
#full = [full[item-1] for item in A]
#print(full)
#ichiheri = [c[i]-1 * (c[i]-1) / 2 for i in range(1,1+len(c))]
sumA = sum(full.values())
val = {i:int(sumA-full[i]+(c[i]-2) * (c[i]-1) / 2 )for i in c.keys()}
#print(ichiheri)



for a in A:
    print(val[a])

Submission status: Accepted

Solution code:
import collections

n = int(input())

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

cnt = collections.Counter(nums)

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

total = 0

for c in cnt.values():
    total += c2(c)

for i in range(n):
    d = cnt[nums[i]]
    diff = c2(d) - c2(d-1)
    print(total - diff)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

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

for k in range(len(nums)):
  print(total - (len(counter[nums[k]])-1))

Submission status: Accepted

Solution code:
from functools import reduce
n=int(input())
a=list(map(int,input().split()))
c=list(range(1,n+1))
num=[0]*n
for i in range(n):
    num[a[i]-1]=num[a[i]-1]+1
preans=reduce(lambda x,y:x+y*(y-1)/2,num,0)

for i in range(n):
    print(int(preans-num[a[i]-1]+1))

Submission status: Accepted

Solution code:
import math

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

L = [[] for _ in range(N)]
for i in range(N):
    L[A[i]-1].append(i)

com = []
for i in range(N):
    n = len(L[i])
    com.append(n * (n - 1) // 2)
s = sum(com)

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

Submission status: Accepted

Solution code:
n=int(input());arr = list(map(int,input().split()))
from collections import Counter
cnt = Counter(arr)
total = 0
for i in cnt:
  total += (cnt[i]**2 - cnt[i])//2
for i in arr:
  print(total-(cnt[i]**2-cnt[i])//2 + (cnt[i]-1)*(cnt[i]-2)//2)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a_lst = [int(elem) for elem in input().split()]
c = collections.Counter(a_lst)
res1 = 0
for k in c.keys():
  res1 += c[k] * (c[k] - 1) // 2

for i in range(n):
  res = res1
  res -= c[a_lst[i]] - 1
  print(res)

Submission status: Accepted

Solution code:
import numpy as np
N = input()
As = np.array(input().split(), dtype=int)

# 集計
from collections import defaultdict
d = defaultdict(int)
for a in As:
    d[a] += 1
    
# 全組み合わせ計算
patterns = sum([v * (v - 1) // 2 for v in d.values()])

# 出力
for num in As:
    times = d[num]
    print(patterns - times + 1)

Submission status: Accepted

Solution code:
def main():
  from collections import Counter
  n=int(input())
  a,b=tuple(map(int,input().split())),[0]*n
  c=Counter(a)
  x=0
  for i in set(a):
    b[i-1]=c[i]
    x+=(b[i-1]*(b[i-1]-1))//2
  b=tuple(b)
  for i in a:
    y=b[i-1]-1
    print(x-y if x>=y else 0)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
C = [0] * (N+1)
sum = 0

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

# print(N,"\n",A)
# print(C)

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

sum = int(sum)
# print(sum)

#   a(a-1)/2 - (a-1)(a-2)/2
# = a-1
for i in range(0,N):
    # print("A[i]" ,A[i])
    print(sum - (C[A[i]] -1))

Submission status: Accepted

Solution code:
# D

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

from collections import Counter

pairs = {}

ca = Counter(A)

for k,v in ca.items():
  #print(k,v)
  if v>1:
    pairs[k] = int(v*(v-1)/2)
  else:
    pairs[k] = 0

pairs_sum = int(sum(pairs.values()))

for k in range(N):
  t = ca[A[k]]
  print(pairs_sum-(t-1))

Submission status: Accepted

Solution code:
import collections

n = int(input())
data = list(map(int, input().split()))
c = collections.Counter(data)

# 合計値
sum = 0
for key in c.keys():
  sum += int( c[key] * (c[key]-1) / 2 )

# print("sum:", sum)

for i in range(n):
  out = sum
  out -= int( c[data[i]] * (c[data[i]]-1) / 2 )
  # print("-out:", out)
  out += int( (c[data[i]]-1) * (c[data[i]]-2) / 2)
  # print("+out:", out)
  print(out)

Submission status: Accepted

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

b = [0]*n
c = 0
d = 0

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

for j in range(n):
    d = d + b[j]*(b[j]-1)/2

for k in range(n):
    c = a[k] - 1
    print(int(d-b[c]+1))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
B = np.zeros(N+1)

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

all_c = 0
for i in range(1,N+1):
  all_c += int(B[i]*(B[i]-1)/2)			# 同一の整数を持つボールの組合せ数
for i in range(N):
  if B[A[i]] != 0:
    print(int(all_c-(B[A[i]]-1)))

Submission status: Accepted

Solution code:
import numpy as np
from sys import stdin
input = stdin.readline
N = int(input())
A = list(map(int, input().split()))
number = np.zeros(N, dtype = int)
total = 0

dic = {}
a = sorted(A)
now_key = a[0]
value = 1
dic[now_key] = 1
for i in range(1,N):
  if a[i] == now_key:
    value += 1
  else:
    dic[now_key] = value
    if value >= 2:
      total += value * (value-1) // 2
    now_key = a[i]
    value = 1
dic[now_key] = value
if value >= 2:
  total += value * (value-1)//2
for j in A:
  print(total - dic[j] + 1)

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

pairs = 0

b = []
for i in range(n+1):
    c = cnt[i]*(cnt[i]-1)/2
    b.append(c)
    pairs += c

for i in a:
    print(int(pairs - b[i] +(cnt[i]-1)*(cnt[i]-2)/2))

Submission status: Accepted

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

c = Counter(A)
# print(c)
cou = 0
for x in c:
    # print(x,c[x])
    if c[x]>1:
        y = c[x]
        cou+=y*(y-1)//2



for i in A:
    X = c[i]
    ans = cou - X*(X-1)//2 + (X-1)*(X-2)//2
    print(ans)

Submission status: Accepted

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

dic = {}
for i in range(n):
  if an[i] not in dic.keys():
    dic[an[i]] = 1
  else:
    dic[an[i]] += 1

ans = {}
all_pattern = 0
for k,v in dic.items():
    if v >= 2:
        all_pattern += v*(v-1)//2
for k,v in dic.items():
    ans[k] = all_pattern - (v-1)
for v in an:
    print(ans[v])

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int,input().split()))
c = [0]*N
p = 0


c = collections.Counter(A)

for i in range(N):
  p += (c[i+1])*(c[i+1]-1)//2

for i in range(N):
  print(p-c[A[i]]+1)

Submission status: Accepted

Solution code:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    ac = collections.Counter(a)
    t = 0
    for k, v in ac.items():
        t += v * (v - 1) // 2
    for v in a:
        print(t - (ac[v] * (ac[v] - 1) // 2) + ((ac[v] - 1) * (ac[v] - 2) // 2))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def comb_2(a):
    return int(a*(a-1)/2)

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

sum_all = 0
for i in a_cnt:
    sum_all += comb_2(i)

for i in a:
    print(sum_all - comb_2(a_cnt[i-1]) + comb_2(a_cnt[i-1] - 1))

Submission status: Accepted

Solution code:

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

def com(x):
    return int(x * (x - 1) / 2)

data_dict = {}
for x in A:
    if x not in data_dict:
        data_dict[x] = 1
    else:
        data_dict[x] += 1
# print(data_dict)

com_dict = {}
com_sum = 0
for x in data_dict.keys():
    com_dict[x] = com(data_dict[x])
    com_sum += com_dict[x]
# print(com_dict)
# print(com_sum)

result_dict = {}
for x in A:
    if x not in result_dict:
        result_dict[x] = com_sum - com_dict[x] + com(data_dict[x] - 1)
    print(result_dict[x])

Submission status: Accepted

Solution code:
from collections import Counter
import math

def main():
    r=2
    def comb2(n):
        if n < 2:
            return 0
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    
    all_c = sum([comb2(count[i]) for i in count])
    
    for b in A:
        print(all_c - (count[b]-1))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
 
from collections import Counter
 
N = int(input())
 
A = list(map(int, input().split()))
 
c = Counter(A) #１、２などの数の書かれたボールが何個あるかについてのdict生成
 
s = 0
for key in c:
    s += c[key] * (c[key] - 1) // 2  #数列から何も除かなかった時の各ボールの選び方　の和
 
for num in A:
    print(s - c[num] * (c[num]-1) // 2 + (c[num]-1) * (c[num]-2) // 2)
    
#k番目のボールを除いたN-1個のボールから、k番目のボールと同じ整数が書かれたボールを選び出す方法の数
#をsから引くと各kに対する答えになる。

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
a=sorted(A)
counter=1
ans=0
value=a[0]
dict={}
for i in range(1,n):
    if a[i]==value:
        counter+=1
    else:
        dict[value]=counter
        if counter<2:
            pass
        else:
            ans+=counter*(counter-1)//2
        counter=1
        value=a[i]
dict[value]=counter
if counter<2:
    pass
else:
    ans+=counter*(counter-1)//2
for j in range(n):
    count=dict[A[j]]-1
    if count==0:
        print(ans)
    elif count==1:
        print(ans-1)
    else:
        c=ans-(count+1)*count//2+count*(count-1)//2
        print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
import math

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

c = collections.Counter(a)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

all_comb = 0
for v in c.values():
    if v != 1:
        all_comb += combinations_count(v,2)

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

Submission status: Accepted

Solution code:
from collections import defaultdict


ball_num = int(input().rstrip())
points = [int(n) for n in input().rstrip().split()]
point_dict=defaultdict(int)
total_pattern=0

for i in range(ball_num):
    point_dict[points[i]]+=1
for j in point_dict:
    total_pattern += point_dict[j]*(point_dict[j]-1)//2
    
total_pattern0=total_pattern
for ban_i in range(ball_num):
    total_pattern0 -= point_dict[points[ban_i]]*(point_dict[points[ban_i]]-1)//2         
    total_pattern0 += (point_dict[points[ban_i]]-1)*(point_dict[points[ban_i]]-2)//2
        
    print(total_pattern0)
    total_pattern0=total_pattern

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
tot=0
dic={}
for i in a:
  if i not in dic:
    dic[i]=1
  else:
    dic[i]+=1
for i in dic:
  tot+=int((dic[i]*(dic[i]-1))/2)

for i in range(N):
  c=dic[a[i]]-1
  print(tot-c)

Submission status: Accepted

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

cnt=[0]*(n+1)
for a in A:
    cnt[a]+=1

total=0
for c in cnt:
    total+=c*(c-1)
total//=2

for k in range(n):
    print(total-cnt[A[k]]+1)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    for x in a:
        d[x] += 1
    diff = {}
    sum_ = 0
    for key, value in d.items():
        num = value * (value - 1) // 2
        sum_ += num
        diff[key] = num - (value - 1) * (value - 2) // 2
    for x in a:
        print(sum_ - diff[x])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    for x in a:
        d[x] += 1
    diff = {}
    sum_ = 0
    for key, value in d.items():
        num = value * (value - 1) // 2
        sum_ += num
        diff[key] = num - (value - 1) * (value - 2) // 2
    for x in a:
        print(sum_ - diff[x])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# number of balls
N = input()
# ball values
K = list(map(int, input().split()))

# get ball counts
# ball_counts = {}
# for i in range(len(K)):
#     ball_value = K[i]
#     if ball_value in ball_counts:
#         ball_counts[ball_value] += 1
#     else:
#         ball_counts[ball_value] = 1

# for i in range(len(K)):
#     ball_value = K[i]
#     two_equal_values_count = 0
#     for value, count in ball_counts.items():
#         # check if we can even come up with a pair
#         if count > 1:
#             if value == ball_value:
#                 # if we have three balls [1, 1, 1] and the current ball is 1, we want to remove our current 1 and only look at [1, 1]
#                 two_equal_values_count += (count - 1) * (count - 2) / 2
#             else:
#                 two_equal_values_count += count * (count - 1) / 2

# get ball counts
ball_counts = {}
for i in range(len(K)):
    ball_value = K[i]
    if ball_value in ball_counts:
        ball_counts[ball_value] += 1
    else:
        ball_counts[ball_value] = 1

# get sums
total_sum = 0
for value, count in ball_counts.items():
    total_sum += (count * (count - 1)) / 2

# remove extra
for i in range(len(K)):
    ball_value = K[i]
    print(int(total_sum - (ball_counts[ball_value] - 1)))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

from collections import Counter
countdic = Counter(arr)
sumdic = dict()
sum = 0

for i in countdic:
    sumdic[i] = (countdic[i]**2-countdic[i])//2
    sum += (countdic[i]**2-countdic[i])//2

for i in arr:
    print(sum-sumdic[i]+(countdic[i]-1)*(countdic[i]-2)//2)

Submission status: Accepted

Solution code:
n = int(input())
inp = list(map(int,input().split()))
d = {}
for x in inp:
    d[x] = d.get(x, 0) + 1
v = [0 for _ in range(n+1)]
for x, y in d.items():
    v[x] = (y)*(y-1)//2
tot = sum(v)
for i in range(n):
    print(tot-v[inp[i]]+((d[inp[i]]-1)*(d[inp[i]]-2)//2))

Submission status: Accepted

Solution code:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
a=n1()

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

from collections import Counter
c=Counter(a)
# print(c)
d={i:comb(j) for i,j in c.items()}
# print(d)
s=sum(d.values())
# print(s)

for i in a:
    print(s-d[i]+comb(c[i]-1))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = input().split()
freq = Counter(a)
#print(freq)
total=0
    
for key,val in freq.items():
    total+=(val*(val-1))//2

for k in range(n): 

    kth = (freq[a[k]]*(freq[a[k]]-1))//2
    newkth = ((freq[a[k]]-2)*(freq[a[k]]-1))//2  

    total -= kth
    total += newkth
    
    print(total)
    
    total -=newkth
    total +=kth

Submission status: Accepted

Solution code:
def choose2(number):
	if number>=2:
		return (number*(number-1))/2
	else:
		return 0
N = int(input())
k = list(map(int,input().split()))
valArr = list(set(k))
occurence = []
occurenceIndex = []
mx = 0
for l in k:
	if l>mx:
		mx=l
for i in range(mx+1):
	occurence.append(0)
for j in range(N):
	occurence[k[j]]+=1
	
			
'''6*5/2
5*4/2
N*(N-1)/2
(N-2)*(N-1)/2
N*(N-1)/2*(N-2)/N
occurence = [1,2,3]
ans = 0+2*1/2+3*2/2
k val = 3
correct = 0+2*1/2+2*1/2

'''
ans = 0
for time in range(len(occurence)):
	ans+=choose2(occurence[time])
for ball in range(N):
	kans = ans-choose2(occurence[k[ball]])+choose2(occurence[k[ball]]-1)
	
	print(int(kans))

Submission status: Accepted

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

def ch_2(n):
	# print(n)
	return n*(n-1)/2

count=Counter(A)

ans=0
for i in count.values():
	ans+=ch_2(i)

for j in range(N):
	ttl=ans
	ttl-=ch_2(count[A[j]])
	ttl+=ch_2(count[A[j]]-1)
	print(int(ttl))

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
l = Counter(A)
al = 0
for i in l:
  al += l[i]*(l[i]-1)//2
for i in range(N):
    print(al - (l[A[i]]-1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
l = Counter(A)
al = sum([x*(x-1)//2 for x in l.values()])
for i in range(N):
    print(al - (l[A[i]]-1))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
l = collections.Counter(A)
al = sum([x*(x-1)//2 for x in l.values()])
for i in range(N):
    print(al - (l[A[i]]-1))

Submission status: Accepted

Solution code:
#!/usr/bin/python3

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

counts = {}

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

tbcomb = 0
for k in counts:
    c = counts[k]
    if c >= 2:
        tbcomb += c * (c - 1) // 2

for i in range(n):
    c = counts[a[i]]
    if c == 1:
        print(tbcomb)
    elif c == 2:
        print(tbcomb - 1)
    else:
        print(tbcomb - (c * (c - 1) // 2) + ((c - 1) * (c - 2) // 2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

'''
5
1 1 2 1 2
'''


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

import collections

a_0 = a[1:]
first_val = a[0]
# print(a_0)

c = collections.Counter(a_0)
# print(c)

# a_0の同じ数の組み合わせを求める
cnt_0 = 0
for val in c.values():
    cnt_0 += int(val * (val-1) /2)
print(cnt_0)

# 減る分：もともとnだったのはn-1減って、
# 増える分：もともとNだったのはn増える

for i in a_0:
    if i == first_val:
        print(cnt_0)
    else:
        print(cnt_0 + c[first_val] - (c[i]-1))

Submission status: Accepted

Solution code:
import math
inp = [input() for i in range(2)]
N = int(inp[0])
A = list(map(int, inp[1].split()))
# B = 0
d = {}
for k in A:
    # a = A.count(k)
    # B += (a - 1) / 2
    if k in d.keys():
        d[k] += 1
    else:
        d[k] = 1

# for k in range(N):
#     c = A.count(A[k]) - 1
#     counter = B - c
#     print(int(counter))

p = {}
for k in d.keys():
    cnt = d[k]
    p[k] = cnt * (cnt-1) / 2

total = sum(p.values())

for item in A:
    print(int(total - d[item] + 1))

Submission status: Accepted

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

d = dict()
for key in a:
  if key in d:
    d[key] += 1
  else:
    d[key] = 1

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

for key in a:
  print(sm-d[key]+1)

Submission status: Accepted

Solution code:
import math
input()
items = list(map(int,input().split()))
d = {}
for i in items:
    if i in d.keys():
        d[i] = d[i]+1
    else:
        d[i] = 1

p = {}
for k in d.keys():
    cnt = d[k]
    p[k] = (cnt*(cnt-1))/2

total = sum(p.values())

for item in items:
    print(int(total - d[item] + 1))

Submission status: Accepted

Solution code:
from math import factorial
from collections import defaultdict


def cmb(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


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

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

sm = 0

for i in dA.values():
    if i >= 2:
        sm += cmb(i, 2)
    else:
        sm += 0

for j in A:
    print(sm - (dA[j] - 1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
d = {}
for a in A:
    d[a] = d.get(a, 0) + 1
ans = sum([v * (v - 1) // 2 for v in d.values()])
for a in A:
    print(ans - d[a] + 1)

Submission status: Accepted

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

num = [0]*(N)
for i in range(N):
    num[A[i]-1]+=1
Ans_ = 0
for i in range(N):
    Ans_ += num[i]*(num[i]-1)//2
    A[i]-=1
for i in range(N):
    print(Ans_-num[A[i]]+1)

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    c = collections.Counter(a)
    s = 0
    for v in c.values():
        s += v * (v-1) // 2
    r = []
    for i in range(n):
        v = c[a[i]]
        r.append(s - v*(v-1)//2 + (v-1)*(v-2)//2)

    return JA(r, "\n")


print(main())

Submission status: Accepted

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

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

cs = Counter(ls)
s = 0

for v in cs.values():
    s += cmb(v)

for a in ls:
    print(s - cmb(cs[a]) + cmb(cs[a] - 1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 19:37:51 2020

@author: Kanaru Sato
"""

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

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

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

for i in range(n):
    print(count-(dic[a[i]]-1))

Submission status: Accepted

Solution code:
import sys
from collections import Counter

def cmb2(n):
	if n < 2:
		return 0

	return n * (n-1) // 2

if __name__ == "__main__":

	N = int(input())
	A = [int(i) for i in input().split()]
	# A = list(map(int, input().split()))

	c = Counter(A)
	s = 0
	for v in c.values():
		s += cmb2(v)

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

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int, input().split(" ")))
count = [0]*(n+1)
for i in range(n):
    count[data[i]] = count[data[i]]+1

val = [0]*(n+1)
sumval = 0
for i in range(n+1):
    if(count[i] >= 2):
        val[i] = count[i]-1
        sumval = sumval + int(count[i]*(count[i]-1)/2)

for i in range(n):
    print(sumval - val[data[i]])

Submission status: Accepted

Solution code:
from collections import Counter


def comb_2(x):
    if x < 2:
        return 0
    return x * (x - 1) // 2


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

c = Counter(arr)
s = 0
for item, cnt in c.items():
    s += comb_2(cnt)

for a in arr:
    print(s - comb_2(c[a]) + comb_2(c[a] - 1))

Submission status: Accepted

Solution code:
def main():
    dates = int(input())
    numbers = list(map(int, input().split()))
    count = {i: 0 for i in range(1, dates + 1)}
    for n in numbers:
        count[n] += 1
    answer = 0
    for c in count.values():
        answer += c * (c - 1) // 2
    for n in numbers:
        print(answer - count[n] + 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter

N = int(input())

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

c = Counter(A)

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

for num in A:
    print(s - c[num] * (c[num]-1) // 2 + (c[num]-1) * (c[num]-2) // 2)

Submission status: Accepted

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

# 各整数がいくつあるかのリスト
L = [0]*N
for i in A:
    L[i-1] += 1

# 全体から選んだ場合の数を求める
# 各整数で２つ選ぶ組み合わせの合計
SumAll = 0
for i in range(N):
    x = L[i]
    if x >= 2:
        SumAll += int((x * (x-1))/2)

# k番目がない場合の数
# kが影響する分のもとの場合の数を引いて、ｋを抜いたあとの場合の数を足す
for k in range(N):
    i = A[k] - 1
    Sub = 0
    x = L[i]
    if x >= 2:
        Sub += int((x * (x-1)/2))
    if x >= 3:
        Sub -= int((x-1)*(x-2)/2)
    print(SumAll - Sub)

Submission status: Accepted

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

Anum = [0] * N
for i in A:
    Anum[i-1] += 1

cnt = 0
for i in Anum:
    cnt += i*(i-1)//2

for k in range(N):
    ans = cnt - (Anum[A[k]-1]-1)
    print(ans)