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 heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

N=2*10**5+10

n=I()
a=LI()
m=[0]*N
ans=0
for i in a:
    m[i]+=1
for i in m:
    ans+=i*(i-1)//2

for i in a:
    print(ans-(m[i]-1))

Submission status: Accepted

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

#aに含まれる要素とその個数を取得する
d = {}
for i in range(n):
	d[a[i]] = d.get(a[i], 0) + 1

# print(d)

cnt = 0
for v in d.values():
	# val = d.get(a[k])
	# print(v)
	cnt += v * (v - 1) // 2


for i in range(n):
	print(cnt - d.get(a[i]) + 1)

Submission status: Accepted

Solution code:
n = int(input())
list_A = list(map(int, input().split()))
list_B = list(set(list_A))
list_M = [0 for _ in range(n+1)]

for s in list_A:
    list_M[s] += 1
total = 0

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

for s in list_A:
    ans = total - list_M[s] + 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=  int( input())
a = list( map( int , input().split()))
d ={}
for item in a:
	if item  in d:
		d[item]+=1
	else:
		d[item] =1;
ans =0
for val in d.values():
	ans += (val * (val-1)) //2
for item in a:
	temp =ans
	temp -= ( (d[item] * (d[item] -1) ) //2 )
	temp += ( (max(d[item]-2 ,0) * (d[item] -1) ) //2 )
	print(temp)

Submission status: Accepted

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

Submission status: Accepted

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

def group_by_val(list_):
    res = dict()
    for v in list_:
        if v not in res:
            res[v] = [v]
            continue
        res[v].append(v)
    return res

def count_leng(dict_):
    res = dict()
    for k in dict_:
        res[k] = len(dict_[k])
    return res

def comb2(list_):
    leng = len(list_)
    if leng == 1:
        return 0
    return (leng*(leng-1)) // 2

grouped_dict = group_by_val(A)
group_leng = count_leng(grouped_dict)

sum_val = 0
for k in grouped_dict:
    sum_val += comb2(grouped_dict[k])

for a in A:
    print(sum_val-(group_leng[a]-1))

Submission status: Accepted

Solution code:
from itertools import groupby
import copy

def equalfind(A):
    A.sort()
    s=1
    count=0
    if len(A)==1:
        return count
    else:
        for i in range(0,len(A)-1):
            if A[i]==A[i+1]:
                s+=1
            else:
                count+=s*(s-1)//2
                s=1
        return count+s*(s-1)//2


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

B=copy.copy(A)
s=equalfind(B)
data=groupby(B)
ans=[0 for i in range(0,N+1)]
for key,group in data:
    g=len(list(group))
    ans[key]=g

for i in range(0,N):
    minus=ans[A[i]]-1
    print(s-minus)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
 
import collections
count_dic = collections.Counter(a_list)
 
count_dic_new = {}
for key, value in count_dic.items():
    if value == 1:
        count_dic_new[key] = 0
    elif value == 2:
        count_dic_new[key] = 1
    else: 
        count_dic_new[key] = (value * (value - 1)) // 2
fund_values = sum(list(count_dic_new.values()))
 
for a in a_list:
    if count_dic[a] == 1:
        a_value = fund_values
    elif count_dic[a] == 2:
        a_value = fund_values - 1
    else:
        n = count_dic[a]
        a_value = fund_values + (((n-1) * (n-2)) // 2) - (((n) * (n-1)) // 2) 
 
    print(a_value)

Submission status: Accepted

Solution code:
from collections import Counter
from sys import exit, stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
CA = Counter(A)
d = dict()
for k, v in CA.items():
    d[k] = v*(v-1)/2
ans = sum(d.values())
for i in range(N):
    v, a = CA[A[i]], A[i]
    print(int(ans - d[a] + (v-1)*(v-2)/2))

Submission status: Accepted

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

counter = {}
for b in balls:
  counter[b] = counter.get(b, 0) + 1

sum = 0
for v in counter.values():
  sum += v * (v-1) // 2
  
for b in balls:
  n_old = counter[b]
  c_old = n_old * (n_old - 1) // 2
  c_new = (n_old - 1) * (n_old - 2) // 2
  
  result = sum - c_old + c_new
  print(result)

Submission status: Accepted

Solution code:
N = int(input())
K = list(map(int, input().split()))
counter = dict()
for i in range(N):
  if K[i] not in counter:
  	counter[K[i]] = 1
  else:
   	counter[K[i]] += 1

results = 0
remove_counter = dict()
for key, value in counter.items():
  remove_counter[key] = (value * (value - 1) / 2) - ((value - 1) * (value - 2) / 2)
  results += value * (value - 1) / 2
for i in range(N):
  print(int(results - remove_counter[K[i]]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
import collections
A_count = collections.Counter(A)
 
import math
sum_ = 0
for ai in A_count:
    sum_ += (A_count[ai] * (A_count[ai] - 1)) // 2
 
for ai in A:
    #print(sum_ - (A_count[ai] * (A_count[ai] - 1)) // 2 + (A_count[ai] - 1) * (A_count[ai] - 2)) // 2
    l = A_count[ai] * (A_count[ai] - 1) // 2
    m = (A_count[ai] - 1) * (A_count[ai] - 2) // 2
    print(sum_ - l + m)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num_dic = {}
for i in a:
    if i in num_dic:
        num_dic[i]+=1
    else:
        num_dic[i] = 1
pre_ans = 0
for k,v in num_dic.items():
    pre_ans+=v*(v-1)//2
for num in a:
    tmp = num_dic[num]
    ans = pre_ans - (tmp-1)
    print(ans)

Submission status: Accepted

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

import collections
A_count = collections.Counter(A)

import math
sum_ = 0
for ai in A_count:
    sum_ += (A_count[ai] * (A_count[ai] - 1)) // 2

for ai in A:
    #print(sum_ - (A_count[ai] * (A_count[ai] - 1)) // 2 + (A_count[ai] - 1) * (A_count[ai] - 2)) // 2
    l = A_count[ai] * (A_count[ai] - 1) // 2
    m = (A_count[ai] - 1) * (A_count[ai] - 2) // 2
    print(sum_ - l + m)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc159/tasks/abc159_d

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

m = {}
for a in A:
    m[a] = m.get(a, 0) + 1

max_value = 0
for value in m.values():
    # nC2
    max_value += (value * (value - 1)) // 2

for a in A:
    value = m[a]
    old_value = (value * (value - 1)) // 2
    new_value = ((value - 1) * (value - 2)) // 2
    print(max_value + new_value - old_value)

Submission status: Accepted

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

hako = [0 for i in range(N)]
sum = 0

c = collections.Counter(A)

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


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

Submission status: Accepted

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

ans_l=[0]*(n+1)
ans=0
def con(a):
    return int(a*(a-1)*0.5)
for i in l:
    ans_l[i]+=1
#print("ans_l",ans_l)
#print("l",l)
for j in ans_l:
    ans+=con(j)
# print(ans)
for i in l:
    ans_2=ans
    # print("ans_2_0:",ans_2)
    #print("ans_l[i]",ans_l[i])
    ans_2-=con(ans_l[i])
    #print("ans_2_1:",ans_2)
    
    #print("ans_l[i]:",ans_l[i]-1)   
    ans_2+=con(ans_l[i]-1)
    print(ans_2)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    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 key, value in dic.items():
        ans += (value * (value-1)) // 2
    
    for a in A:
        print(ans - dic[a] + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = [int(i) for i in input().split()]

C = collections.Counter(A)
ans = 0
for k, v in C.items():
    ans += v*(v-1)//2

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

Submission status: Accepted

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

#%%
C = {}
for i in A:
    if i in C:
        C[i] += 1
    else:
        C[i] = 1

ans = 0    
for i in C.keys():
    ans += C[i] * (C[i]-1) // 2
    
for i in A:
    dec =  C[i] * (C[i]-1) // 2 - (C[i]-1) * (C[i]-2) //2
    print(ans - dec)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
c = [0 for _ in range(max(a))]
for k, v in d.items():
    c[k - 1] = v * (v - 1) / 2

ans = sum(c)
for i in range(n):
    p = a[i]
    res = c[p - 1] * (d[p] - 2) / d[p]
    ans -= c[p - 1] - res
    print(int(ans))
    ans += c[p - 1] - res

Submission status: Accepted

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

B=Counter(A)

total=0

for v in B.values():
    total+=v*(v-1)//2

for i in range(N):
    ans=total
    ans-=B[A[i]]-1
    print(ans)

Submission status: Accepted

Solution code:
d={}
s=[]

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

for a in nums:    
    if not a in d:
        d[a]=1
    else:
        d[a]+=1
    s.append(a)
    
allcount=0

for count in d.values():
    allcount+=int(count*(count-1)/2)

for num in s:
    print(allcount-d[num]+1)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
from collections import Counter
c = Counter(l)
def motomeru(i):
  return int(c[i]*(c[i]-1)/2)
def herasu(i):
  if c[i] == 0:
    return 0
  else:
    return int((c[i]-1)*(c[i]-2)/2)
zenbu = 0
for i in range(n):
  zenbu = zenbu + motomeru(i+1)
for i in l:
  a = zenbu - motomeru(i) + herasu(i)
  print(a)

Submission status: Accepted

Solution code:
s = input()
n=int(s[0])
s = input().split()
d={}
dc={}
summ=0
for i in s:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d:
    n = d[i]
    if n>1:
        e=n*(n-1)/2
        summ+=e
        dc[i]=e
    else:
        dc[i]=0
for i in s:
    n = d[i]-1
    e = n*(n-1)/2
    print(int(summ-dc[i]+e))

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
from collections import defaultdict
d = defaultdict(int)
for a in l:
  d[a] += 1
def motomeru(i):
  return int(d[i]*(d[i]-1)/2)
def herasu(i):
  if d[i] == 0:
    return 0
  else:
    return int((d[i]-1)*(d[i]-2)/2)
zenbu = 0
for i in range(n):
  zenbu = zenbu + motomeru(i+1)
for i in l:
  a = zenbu - motomeru(i) + herasu(i)
  print(a)

Submission status: Accepted

Solution code:
from collections import Counter # 重複個数のカウント: Counter(num_list)

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    ans_list = [0] * n
    cnt_list = Counter(num_list)
    for k, v in cnt_list.items():
        if v == 1:
            ans_list[k-1] = 0
            continue
        ans_list[k-1] = v * (v-1) // 2
        
    sum_num = sum(ans_list)

    ans = sum_num

    for i in range(n):
        ans -= ans_list[num_list[i]-1]
        if cnt_list[num_list[i]]-1 != 1:
            ans += (cnt_list[num_list[i]]-1) * (cnt_list[num_list[i]]-2) // 2
        print(ans)
        ans = sum_num

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())

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

num=[0]*(N+1) #それぞれの数の個数

cnb=[-1]*(N+1) #それぞれの球を抜いた時のコンビネーション

for i in range(len(ball)):
  num[ball[i]]+=1


allcnb=0
for i in num:
  if i>=2:
    allcnb+=int(i*(i-1)/2)

for i in ball:
  cnb=0

  if num[i]>=2:
    cnb=num[i]-1

  print(allcnb-cnb)

Submission status: Accepted

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

ans_l=[0]*(n+1)
ans=0
def con(a):
    return int(a*(a-1)*0.5)
for i in range(n):
    ans_l[l[i]]+=1
#print("ans_l",ans_l)
#print("l",l)
for j in ans_l:
    ans+=con(j)
# print(ans)
for i in range(n):
    ans_2=ans
    # print("ans_2_0:",ans_2)
    #print("ans_l[l[i]]",ans_l[l[i]])
    ans_2-=con(ans_l[l[i]])
    #print("ans_2_1:",ans_2)
    
    #print("ans_l[l[i]]:",ans_l[l[i]]-1)
    ans_2+=con(ans_l[l[i]]-1)
    print(ans_2)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_c=collections.Counter(A)
cnt=0

for i in A_c.values():
  cnt+=(i*(i-1))//2


for i in A:
  x=A_c[i]
  if x==1:
    print(cnt)
  else:
    print(cnt-x+1)

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline())
a_dict = {}
A = list(map(int, stdin.readline().split()))
for a in A:
    if a in a_dict:
        a_dict[a] += 1
    else:
        a_dict[a] = 1

sum_comb = 0
B = set(A)
for b in B:
    n = a_dict[b]
    sum_comb += n * (n - 1) // 2

for a in A:
    ans = sum_comb
    n = a_dict[a]
    ans -= n * (n - 1) // 2
    ans += (n - 1) * (n - 2) // 2
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

total = 0

for i in C.keys():
   if C[i]>1:
        total += (C[i]*(C[i]-1))//2

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

Submission status: Accepted

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

count_dict = {}
result_dict = {}

for a in a_list:
    if a not in count_dict:
        count_dict[a] = 1
    else:
        count_dict[a] += 1

# default count
count = 0
for k,v in count_dict.items():
    if v >= 2:
        count += v*(v-1)//2

for a in a_list:
    num = count_dict[a]
    if num >= 3:
        print(count-num*(num-1)//2+(num-1)*(num-2)//2)
    elif num == 2:
        print(count-1)
    else:
        print(count)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a_list = [int(num) for num in input().split(" ")]
c_dict = collections.Counter(a_list)

total_comb = 0
for i in c_dict:
    i_count = c_dict[i]
    total_comb += int(i_count*(i_count-1)/2)
for k in range(n):
    except_num = a_list[k]
    print(total_comb - (c_dict[except_num]-1))

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
d=collections.Counter(a)
total=sum([(v-1)*v//2 for v in d.values()])
for x in a:
    print(total-d[x]+1)

Submission status: Accepted

Solution code:
from collections import *

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

c = Counter(A)
t = {key : (value*(value-1)//2) for key, value in c.items()}
s = sum(t.values())
ans = {a : s-t[a] + ((c[a]-1)*(c[a]-2)//2) for a in A }

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
 
N = int(input())
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
    D[a] += 1
 
memo = defaultdict(lambda: -1)
conv_D = defaultdict(int)
SUM = 0
for d in D:
    conv_D[d] = D[d] * (D[d] - 1) // 2
    SUM += conv_D[d]
 
for i in range(N):
    if memo[A[i]] == -1:
        tmp = SUM - conv_D[A[i]] + (D[A[i]] - 1)*(D[A[i]] - 2)//2
        print(tmp)
        memo[A[i]] = tmp
    else:
        print(memo[A[i]])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
d={}
for x in a:
    if d.get(x) is None:
        d[x]=1
        continue
    d[x]+=1
total=0
for v in d.values():
    total+=(v-1)*v//2
for x in a:
    print(total-d[x]+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter

n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
dic = defaultdict(int)
for i in range(n):
    dic[a[i]] += 1

sum = 0
for v in dic.values():
    sum += v * (v - 1) / 2
    
for i in range(n):
    print(int(sum - (dic[a[i]] - 1)))

Submission status: Accepted

Solution code:
from collections import Counter
_, *a = map(int, open(0).read().split())
c = Counter(a)
s = sum(c*~-c // 2 for c in c.values())
for k in a:
    print(s - c[k] + 1)

Submission status: Accepted

Solution code:
import itertools
import math 
def C(n,r): return n*(n-1)//r

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

c = 0
cc = {}
for key, val in d.items():
    temp = C(val,2)
    c += temp
    if cc.get(key) is None:
        cc[key] = temp - C(val-1,2)
for i in a:
    print(c - cc[i])

Submission status: Accepted

Solution code:

from collections import Counter

def C(n):
    if n < 2: return 0
    return n * (n-1) // 2    
    
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)

count = 0

for i, j in c.items():
    count += C(j)

for i in a:
    m = c[i]
    print(count - m + 1)

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = Counter(A)
    comb = 0
    for i in c.values():
        if i >= 2:
            comb += i*(i-1)//2
    for i in range(N):
        print(comb - (c[A[i]] - 1))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

dic = {}
ans = 0
# for t in A:
#     if t in dic.keys():
#         continue
#     tmp = A.count(t)
#     dic[t] = tmp
#     ans += tmp*(tmp-1)//2

for a in A:
    tmp = dic.get(a)
    if tmp == None:
        dic[a] = 1
    else:
        dic[a] = tmp + 1

for key in list(dic.keys()):
    tmp = dic.get(key)
    ans += tmp * (tmp - 1) // 2
    

for a in A:
    val = dic.get(a)
    print(ans-val+1)

Submission status: Accepted

Solution code:
n = int(input())
numlist = [int(_) for _ in input().split()]
numdic = {}
nsum = 0

for i in range(n):
    if numlist[i] in numdic:
        numdic[numlist[i]] += 1
    else:
        numdic[numlist[i]] = 1

for j in numdic:
    nsum += numdic[j] * (numdic[j] - 1) // 2

for k in range(n):
    ans = nsum
    if numdic[numlist[k]] >= 2:
        ans -= (numdic[numlist[k]]-1)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
numlist = [int(_) for _ in input().split()]
numdic = {}
nsum = 0

for i in range(n):
    if numlist[i] in numdic:
        numdic[numlist[i]] += 1
    else:
        numdic[numlist[i]] = 1

for k in numdic:
    nsum += numdic[k] * (numdic[k] - 1) // 2

for j in range(n):
    ans = nsum
    cal = numdic[numlist[j]]
    if cal >= 2:
        ans = ans - (cal-1)
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
N = II()
A = list(MI())
ans = 0
cum = defaultdict(int)
def couple_combination(n):
    if n >= 2:
        return int(n * (n - 1) / 2)
    else:
        return 0
for v in A:
    cum[v] += 1
for v in cum.values():
    ans += couple_combination(v)
for v in A:
    print(max(0,ans - cum[v] + 1))

Submission status: Accepted

Solution code:
n=int(input())
#a,b=map(int,input().split())
al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

dic={}

for a in al:
    dic[a]=dic.get(a,0)+1
total=0
for k in dic.keys():
    total+=dic[k]*(dic[k]-1)//2

for k in range(n):
    count=dic[al[k]]
    ans=total-count*(count-1)//2+(count-1)*(count-2)//2
    print(ans)

Submission status: Accepted

Solution code:
import collections


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

    counters = collections.Counter(A)
    ans = 0
    for _, c in counters.items():
        ans += c*(c-1)//2

    for K in range(N):
        if A[K] in counters:
            c = counters[A[K]]
            if counters[A[K]] > 2:
                print(ans - (c*(c-1)//2) + ((c-1)*(c-2)//2))
            else:
                print(ans - (c*(c-1)//2))
        else:
            print(ans)


# Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

n = int(input())
A = list(map(int, input().split()))
A = [a-1 for a in A]
  
Cnt = [0]*n
for i in range(n):
  Cnt[A[i]] += 1
    
tot = 0
for i in range(n):
    tot += choose2(Cnt[i])

for i in range(n):
    ans = tot
    ans -= choose2(Cnt[A[i]])
    ans += choose2(Cnt[A[i]]-1)
    print(ans)

Submission status: Accepted

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

total = 0
used = set([])
for ball in balls:
    if not ball in used:
        total += counts[ball] * (counts[ball] - 1) // 2
        used.add(ball)

for ball in balls:
    removed = counts[ball] * (counts[ball] - 1) // 2
    added = (counts[ball] - 2) * (counts[ball] - 1) // 2
    print(total - removed + added)

Submission status: Accepted

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

for k in range(n):
  num_ak = hist_a[a[k]-1]
  sum_val = tmp_sum - num_ak * ( num_ak - 1 ) // 2 + ( num_ak - 1 ) * ( num_ak - 2 ) // 2
  print(sum_val)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
from collections import Counter
LL = Counter(L)
cnt = 0
sum1 = 0
for v in LL.values():
    a = v*(v-1)//2
    sum1 +=a

li = []
for i in range(n):
    li.append((L[i],LL[L[i]]))

for i in range(n):
    c = li[i][1]
    c = c*(c-1)//2
    b = li[i][1]-1
    b = b*(b-1)//2
    print(sum1-(c-b))

Submission status: Accepted

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

for v in a:
  if d[v]>1:
  	print(tmp-d[v]+1)
  else:
    print(tmp)

Submission status: Accepted

Solution code:
from collections import Counter
N, li = int(input()), list(map(int, input().split()))
cnt_dict, cnt_total = Counter(li), 0
cnt_total =sum([i[1] * (i[1]-1) // 2 for i in cnt_dict.items()])
for k in range(N):
    print(cnt_total - cnt_dict[li[k]]*(cnt_dict[li[k]]-1)//2 + (cnt_dict[li[k]]-1)*(cnt_dict[li[k]]-2)//2)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
cnt=[0]*(N+1)
for i in range(N):
    cnt[A[i]]+=1
res=0
for i in cnt:
    res+=i*(i-1)//2
for i in range(N):
    print(res-(cnt[A[i]]-1))

Submission status: Accepted

Solution code:
from collections import Counter
N, li = int(input()), list(map(int, input().split()))
cnt_dict, cnt_total = Counter(li), 0
for i in cnt_dict.items():
    cnt_total += i[1] * (i[1]-1) // 2
for k in range(N):
    buf = cnt_dict[li[k]]
    print(cnt_total - buf*(buf-1)//2 + (buf-1)*(buf-2)//2)

Submission status: Accepted

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

B = [0] * N

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

ans = 0

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

for i in range(N):
	res = ans
	res -= (B[A[i]-1] * (B[A[i]-1] - 1)) // 2
	if B[A[i]-1] >= 1:
		res += ((B[A[i]-1] - 1) * (B[A[i]-1] - 2)) // 2
	print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#abc159d
import collections
n=int(input())
a=list(map(int,input().split()))
c=collections.Counter(a)
res=sum([v*(v-1)//2 for v in c.values()])
for x in a:
 print(res-c[x]+1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split() ))
from collections import Counter
d=dict(Counter(A))
#print(d)
cnt=0
for item in d:
    cnt += (d[item])*(d[item]-1)//2

#print("cnt",cnt)
   
for num in A:
    new = max(0, (d[num]-1)*(d[num]-2)//2)
    print(cnt- d[num]*(d[num]-1)//2 + new)

Submission status: Accepted

Solution code:
from collections import Counter


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


c = Counter(A)
ans = []



# 全体でできる、各整数のペアの数から、 i番目の数を使ってできる、A[i]のペアを取り除くでOKのはず
total = 0
for n in c.values():
    total += n * (n - 1) // 2

ans = []
for a in A:
    # A[i]を使ってできるペアの数（A[i]以外の同じ整数とペアになるので、全体のaの個数-1）
    tmp = c[a]-1
    ans.append(total - tmp)

for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
#create date: 2020-03-23 11:51

import sys
stdin = sys.stdin

import numpy as np

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = np.array(na())
    counter = np.bincount(a)
    total = np.sum(counter*(counter-1))//2
    answer = (total - (counter-1))[a]
    print(*answer, sep="\n")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split(' '))))
num_sum = 0
remove_array = np.array([], int)
for i in range(N):
    c1 = np.count_nonzero(A == i + 1)
    c2 = np.count_nonzero(A == A[i])
    if c2 <= 1:
        remove_array = np.append(remove_array, 0) ←　appendがネックになってる？
    else:
        remove_array = np.append(remove_array, c2 - 1)
    num_sum += int(c1 * (c1 - 1) / 2)
print(*(num_sum - remove_array), sep='\n')
'''
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A) # リストやイテレータから値の出現回数をカウントできる（出力は辞書型）
total = 0
for key in cnt.keys():
    total += cnt[key] * (cnt[key] - 1) // 2
for i in range(N):
    tmp = total
    print(tmp - (cnt[A[i]] - 1))
#    tmp -= cnt[A[i]] * (cnt[A[i]] - 1) // 2
#    tmp += (cnt[A[i]] - 1) * (cnt[A[i]] - 2) // 2

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
nums = list(map(int,input().split(" ")))
 
c = Counter(nums)
a = 0
for k,v in c.items():
    if v >=2:
        a+=(v-1)*v//2
 
for num in nums:
    curr = -1
    if c[num] >= 2:
        curr = a - (c[num]-1)
    else:
        curr = a
    print(curr)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=[int(A) for A in input().split()]

cnt=Counter(a)
s=sum([c*(c-1)//2 for c in cnt.values()])

for k in a:
    print(s-(cnt[k]-1))