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
input = sys.stdin.readline

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

count = Counter(A)
sm = {}
sm2 = {}
for key, value in count.items():
    sm[key] = value*(value-1)//2
    sm2[key] = (value-1)*(value-2)//2
total = sum(sm.values())

for num in A:
    print(total - sm[num] + sm2[num])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
re = [0] * (n+1)

c = Counter(a)

for k,v in c.items():
    re[k] = v*(v-1) // 2

s = sum(re)
for i in a:
    print(s - re[i] + (c[i]-1)*(c[i]-2) // 2)

Submission status: Accepted

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

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

cdict = dict(c)

# cdict = dict((key, value) for (key, value) in old_cdict.items() if value >= 2)

# print(cdict)

maxi = 0

for k, v in cdict.items():
    maxi += v * (v - 1) // 2

for a in alist:
    num = cdict[a]
    if num in resdict:
        # print('used')
        print(resdict[num])
        continue
    # print('not used')
    if a in cdict:
        cdict[a] -= 1
        temp = 0
        for k,v in cdict.items():
            temp += v*(v-1)//2
        print(temp)
        resdict[num] = temp
        cdict[a] += 1
    else:
        print(maxi)

Submission status: Accepted

Solution code:
import copy
import collections
import math
 
N = int(input())
A = [int(i) for i in input().split()] 
 
maxnum = max(A)
lis = [-1] * maxnum
bigC = collections.Counter(A)
for i, a in enumerate(A):
  if lis[a-1] < 0:
    newA = copy.deepcopy(A)
    newA.pop(i)
    c = collections.Counter(newA)
    total = 0
    for idx in c:
      num = c[idx]
      total += num * (num-1) // 2
      
    lis[a-1] = total
    for x in bigC:
      if bigC[x] == bigC[a]:
        lis[x-1] = total
      
    print(total)
  else:
    print(lis[a-1])

Submission status: Accepted

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

ans = 0

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

for i in range(n):
    k[l[i]] += 1
    
ll = len(k)
for i in range(ll):
    a = k[i]
    if a <= 1:
        pass
    else:
        ans += (a * (a - 1))/2
        
for i in range(n):
    a = k[l[i]]
    b = (a * (a - 1))/2
    c = a - 1
    if a < 1:
        print(int(ans))
    else:
        pans = ans - b
        c = (c * (c - 1))/2
        pans = pans + c
        print(int(pans))

Submission status: Accepted

Solution code:
import collections 
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
c=defaultdict(int)
for s in a:
    c[s]+=1

a1=list(set(a))

total=0
for j in range(len(a1)):
    x=c[a1[j]]
    total+=x*(x-1)/2

for i in range(n):
    ans=total-c[a[i]]+1
    print(int(ans))

Submission status: Accepted

Solution code:
import operator as op
from functools import reduce
from collections import defaultdict
from math import ceil
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom

n = int(input().strip())
nums = [int(i) for i in input().strip().split()]
ans = [0 for i in range(n)]
num_d = defaultdict(int)
for i in nums:
    num_d[i] += 1

ans_d = defaultdict(int)
s = 0
for each_num in num_d:
    if num_d[each_num] < 2:
        ans_d[each_num] = 0
        continue
    res = ncr(num_d[each_num],2)
    s += res
    ans_d[each_num] = res

for index,i in enumerate(nums):
    if num_d[i] - 1 < 2:
        ans[index] = s - ans_d[i]
    else:
        ans[index] = s - ans_d[i] + ncr(num_d[i] - 1,2)

for each in ans:
    print(each)

Submission status: Accepted

Solution code:
import collections
import math
def combination(a, b):
    tmp = 1
    for i in range(a - b + 1, a + 1):
        tmp *= i
    return tmp // math.factorial(b)
 
N = int(input())
listA = list(map(int, input().split()))
# 各数字の出現回数をカウント
countA = dict(collections.Counter(listA).most_common())
countA_calc = {k: v * (v -1) // 2 for (k, v) in countA.items()}
countA_calc2 = {k: (v - 1) * (v -2) // 2 for (k, v) in countA.items()}
summery = sum(countA_calc.values())
for i in listA:
    print(summery - countA_calc[i] + countA_calc2[i])

Submission status: Accepted

Solution code:
def fun(n):
    return (n*(n+1))//2
n = int(input())
a = list(map(int,input().split()))
d = {}
for i in range(len(a)):
    d[a[i]] = 0
for i in range(len(a)):
    d[a[i]] += 1
total = 0
for i in d:
    total += fun(d[i]-1)
for i in d:
    d[i] = total - (d[i] -1)
for i in range(len(a)):
    print(d[a[i]])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

list1 = [ 0 for a in range(N)]

for i in An:
    list1[i-1] += 1

list2 = [int((a-1)*a/2) for a in list1]
sum1 = sum(list2)
# print(list2)
# print(An)
for i in An:
    if list1[i-1] != 2:
        a = sum1 - list1[i-1] + 1
        print(a)
    else:
        a = sum1 - 1
        print(a)
# print(list2)

Submission status: Accepted

Solution code:
import collections
def cal(n):
    ans = int(n*(n-1)/2)
    return ans 
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
temp = list(c.values())
t_val = list(map(cal,c.values()))
t_key = list(c.keys())
t_cal = sum(t_val)
# print(t_key)
# print(t_cal)
# print(temp)
for i in range(n):
    ans1 = c[a[i]]-1
    print(t_cal - ans1)

Submission status: Accepted

Solution code:
m = int(input())
q_list = list(map(int, input().strip().split()))
n = [0] * m
k = [0] * m

for i in q_list:
    n[i - 1] += 1
for j, l in zip(range(m), n):
    k[j] = int(l * (l - 1)/2)
sum_n = sum(k)

for j in q_list:
    print(sum_n - n[j - 1] + 1)

Submission status: Accepted

Solution code:
import collections

n=int(input())
arr=list(map(int,input().split()))
cnt=collections.Counter(arr)
total=0
for key in cnt.keys():
  total+=cnt[key]*(cnt[key]-1)//2
for i in range(n):
  tmp=total
  tmp-=cnt[arr[i]]*(cnt[arr[i]]-1)//2
  tmp+=(cnt[arr[i]]-1)*(cnt[arr[i]]-2)//2
  print(tmp)

Submission status: Accepted

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

Submission status: Accepted

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

cnt=[0]*1000000
for i in a:
    cnt[i]+=1
for i in cnt:
    ans+=i*(i-1)/2
for i in a:
    
    print(int(ans-cnt[i]*(cnt[i]-1)/2+(cnt[i]-1)*(cnt[i]-2)/2))

Submission status: Accepted

Solution code:
n = int(input().rstrip())
line = input().rstrip().split(' ')

a = []
d = [0] * n
for i in range(n):
    num = int(line[i])
    a.append(num)
    d[num - 1] += 1

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

total = 0
for i in range(n):
    total += ans_list[i]

for i in range(n):
    ans = total

    ans -= ans_list[a[i] - 1]
    num = d[a[i] - 1] - 1
    ans += num * (num - 1) / 2

    print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter
from operator import mul
from functools import reduce

def combo(n):
    if n<1:
        return 0
    r = min(n-2,2)
    if r == 0: return 1
    over = reduce(mul, range(n, n - 2, -1))
    under = reduce(mul, range(1,2 + 1))
    return over // under

N=int(input())
A=list(map(int,input().split(" ")))
count=Counter(A)
com={}

for k,v in count.items():
    com[k]=combo(v)
s=sum(com.values())
for i in range(N):
    su=s-combo(count[A[i]])+combo(count[A[i]]-1)
    print(su)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)

r = 0
for j in c:
    if c[j] > 1:
        r = int(r + (c[j] * (c[j] - 1) / 2))

tmp = r 
for i in range(n):
    r = int(r - ((c[a[i]] * (c[a[i]] - 1) / 2)) + (((c[a[i]] - 1) * (c[a[i]] - 2) / 2)))
    print(r)
    r = tmp

Submission status: Accepted

Solution code:
import collections

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

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

#print(c)

c_value_list = list(c.values())

#print(c_value_list)

sum = 0
for i in range (len(c_value_list)):
  sum += int((c_value_list[i]*(c_value_list[i]-1))/2)

#print(sum)

for i in range(len(a)):
  if c[a[i]] == 1:
    print(sum)
  else:
    diff = int((c[a[i]] * (c[a[i]]-1)) / 2)
    diff_a = int(((c[a[i]]-1) * (c[a[i]]-2)) / 2)
    print(sum - diff + diff_a)

Submission status: Accepted

Solution code:
import math

def combinations_count(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n=int(input())
a=list(map(int,input().split()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if not c in comb:
            comb[c]=combinations_count(c,2)
        pans+=comb[c]
    print(pans)

Submission status: Accepted

Solution code:
import math

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

n = int(input())
a = list(map(int,input().split()))
a1 = sorted(a)
cnt = {}
cnt1 = 1
for i in range(1,n):
    if a1[i] == a1[i-1]:
        cnt1 += 1
        if i == n-1:
            cnt[a1[i]] = cnt1
    else:
        cnt[a1[i-1]] = cnt1
        cnt1 = 1
        if i == n-1:
            cnt[a1[i]] = cnt1


cmb = {}
cmb[1] = 0
cmb_all = 0
for i in cnt.values():
    if i == 1:
        continue
    else:
        cmb1 = combinations_count(i,2)
        cmb_all += cmb1
        cmb[i] = cmb1

for i in a:
    cnt1 = cnt[i]
    if cnt1 == 1:
        print(cmb_all)
    else:
        ans = cmb_all + cmb[cnt1] * ((cnt1-2)/cnt1 - 1)
        print(int(ans))

Submission status: Accepted

Solution code:
import collections
import math

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

    Cou = collections.Counter(A)
    ans = 0

    oo = 0
    for c in Cou.values():
        oo += c * (c-1) // 2
    #print(Cou)
    for k in range(1, N+1):
        c = Cou[A[k-1]] - 1
        print(oo - c)
    return 0


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
c = list(map(int,input().split()))
s = Counter(c)
t = list(s.items())
nag = len(t)
x = 0
ans = 0

for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
    
for j in range(n):
    ans = x - s[c[j]] + 1 
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(l)
ans_dict = {}

l1 = list(c.values())
l2 = [(i*(i-1))/2 if i >= 2 else 0 for i in l1]
sum_l2 = sum(l2)

for i in l:
    b = c[str(i)]
    if b < 2:
        print(int(sum_l2))
    elif b == 2:
        print(int(sum_l2 - 1))
    elif b > 2:
        print(int(sum_l2 - ((b*(b-1))/2) + (((b-1) *(b-2))/2)))

Submission status: Accepted

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

se = set()
dic = dict()

for i in range(N):
    if A[i] in se:
        dic[A[i]] += 1
    else:
        se.add(A[i])
        dic.update({A[i]: 1})

c = 0
for k in dic.keys():
    c += (dic[k])*(dic[k]-1)//2

result = [0]*N
for i in range(N):
    result[i] = c - (dic[A[i]])*(dic[A[i]]-1)//2 + (dic[A[i]]-1)*(dic[A[i]]-2)//2

print("\n".join(list(map(str, result))))

Submission status: Accepted

Solution code:
import collections

n = input()
al = tuple(x for x in input().split())
aset = set(al)

# {number: count}
ccc = collections.Counter
counts_d = ccc(al)

# Count of count
v_counts = ccc(v for v in counts_d.values())

whole_patterns = sum(
    (int(k * (k - 1) / 2)) * v for k, v in v_counts.items() if k > 1
)

[print(whole_patterns - counts_d[a] + 1) for a in al]

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import Counter
    
    N = int(input())
    A = list(map(int, sys.stdin.readline().split()))
    
    count = Counter(A)
    
    d = {}
    comb_sum = 0
    
    for i, n in count.items():
        if n == 1:
            comb = 0
            comb_ex = 0
        else:
            comb = n * (n - 1) / 2
            comb_ex = (n - 1) * (n - 2) / 2
        comb_sum += comb
        d[i] = comb_ex - comb
     
    for n in A:
        ans = int(d[n] + comb_sum)
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(str,input().split()))
ls = [i for i in range(2*10**5)]
c = collections.Counter(a)
comon = c.most_common()
ans = 0

for i in comon:
    if i != 1:
      ls[int(i[0])] = i[1]
      ans += (i[1]*(i[1]-1)//2)

for i in a:
    co = ls[int(i)]
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

Submission status: Accepted

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

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

for i in range(n):
   if (seen[i] == 0) or (seen[i] == 1):
      continue
   count = count +seen[i]*(seen[i]-1)//2

for i in a:
  ans = count - (seen[i-1]*(seen[i-1]-1))//2 + ((seen[i-1]-2)*(seen[i-1]-1))//2
    
  print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
num = list(map(int, input().split()))
dict = {}
for i in range(0, N):
    if num[i] in dict:
        dict[num[i]] += 1
    else:
        dict[num[i]] = 1
totalNum = 0
for i in range(1, N + 1):
    if i in dict and dict[i] > 1:
        totalNum += ((dict[i] * (dict[i] - 1)) / 2)
for i in range(0, N):
    if dict[num[i]] > 1:
        print(int(totalNum - dict[num[i]] + 1))
    else:
        print(int(totalNum))

Submission status: Accepted

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

b=[0]*N
for a in A:
    b[a-1]+=1
    
c=[0]*N
tmp=b
for i in range(N):
    c[i]+=tmp[i]*(tmp[i]-1)//2
sum_c=sum(c)

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

Submission status: Accepted

Solution code:
import collections
import math
 
def combinations_count(n, r):
  if n == r:
    return 1
  elif n < r:
    return 0
  else:
    return n * (n-1) // 2 
    #math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

combi = collections.Counter(A)
combi_count = {}
calc_dict = {}
total = 0
for k, v in combi.items():
  if v in calc_dict:
    combi_count[k] = calc_dict[v]
  else:
    combi_count[k] = combinations_count(v, 2)
    calc_dict[v] = combi_count[k]
  total += combi_count[k]

for i in range(N):
  ans = total - combi_count[A[i]] + combinations_count(combi[A[i]]-1, 2)
  print(ans)

Submission status: Accepted

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

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

for a in aa:
    print(int(total - dic[a] + 1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import combinations
from collections import Counter
import math

N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#-----

A_cnt = Counter(A_list)

A_sum_of_combination={}
A_sum_of_combination_minus1={}
for k,v in A_cnt.items():
    # 個数2で場合分け
    if v >= 2:
        tmp_sum=1
        for i in range(2):
            tmp_sum *= (v-i)/(i+1)
        
        A_sum_of_combination[k] = int(tmp_sum)
        
    else:
        A_sum_of_combination[k] = 0
    
    if (v-1) >= 2:
        tmp_sum=1
        for i in range(2):
            tmp_sum *= ((v-1)-i)/(i+1)
        
        A_sum_of_combination_minus1[k] = int(tmp_sum)
        
    else:
        A_sum_of_combination_minus1[k] = 0

sum_all = sum( [ v for k,v in A_sum_of_combination.items()] )

for i in A_list:
    num_selected = A_cnt[i] - 1
    
    if num_selected >= 2:
        ans = sum_all - A_sum_of_combination[i] + A_sum_of_combination_minus1[i]
        print(ans)
    else:
        ans = sum_all - A_sum_of_combination[i]
        print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A_list = list(map(int, input().split(" ")))
C_dic = collections.Counter(A_list)
ans_dic = {}
sum_cdic = 0
temp_sum = 0
for i in C_dic:
    temp = C_dic[i]
    temp = temp*(temp-1)//2
    temp_sum += temp
sum_cdic = temp_sum

for i in A_list:
    if i in ans_dic:
        print(ans_dic[i])
    else:
        temp_sum = sum_cdic
        temp_sum -= (C_dic[i]*(C_dic[i]-1)//2)
        temp_sum += ((C_dic[i]-1)*(C_dic[i]-2)//2)
        ans_dic[i] = temp_sum
        print(temp_sum)

Submission status: Accepted

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

def comb(n, r):
    if n ==0 or n == 1: return 0
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = comb_list[value]
        sum_all += comb_list[value]

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter as C
n=int(input())
a=list(map(int,input().split()))
b=C(a)
s=sum(i*(i-1)//2for i in b.values())
t={}
for i,j in b.items():
    t[i]=s-(j*(j-1)//2-(j-1)*(j-2)//2)
for i in a:
    print(t[i])

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int,input().split()))
C = [0]*(N+1)
s=0
ds = [0]*N
def c(n,r):
    if(n<r): return 0
    return math.factorial(n)//(r*math.factorial(n-r))

for i in range(N):
    C[A[i]-1] += 1
for i in range(N):
    s +=c(C[i],2)
for i in range(N):
   print(s-C[A[i]-1]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))
ball_count = []
for i in range(0, N):
	ball_count.append(0)

for i in A:
	ball_count[i - 1] += 1

all_pair = 0
for i in ball_count:
	all_pair += i * (i - 1) // 2

for i in A:
	print(all_pair - (ball_count[i - 1] - 1))

Submission status: Accepted

Solution code:
import collections
import math

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

c = collections.Counter(l)

all_ans = 0
for n in c.values():
    if n<=1:
        continue
    all_ans+=math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
for n in l:
    print(all_ans-(c[n]-1))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(l)
ans_dict = {}

hoge = list(c.values())
full_fuga = [(i*(i-1))/2 if i >= 2 else 0 for i in hoge]
full_ans = sum(full_fuga)

for i in l:
    v = c[str(i)]
    if v < 2:
        print(int(full_ans))
    elif v == 2:
        print(int(full_ans - 1))
    elif v > 2:
        print(int(full_ans - ((v*(v-1))/2) + (((v-1) *(v-2))/2)))

Submission status: Accepted

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

sum = sum(c)
for i in range(n):
    d = int(a[i]-1)
    if c[d]>2:
        e = sum - c[d] + int( (c[d])*(b[d+1]-2) /(b[d+1]) )
    else:
        e = sum - c[d]
    print(e)

Submission status: Accepted

Solution code:
import collections

n=int(input())
li = list(map(int,input().split()))
cli = collections.Counter(li)
cli_values = tuple(cli.values())

count = 0

for cli_value in cli_values:
    if cli_value > 1:
        count += (cli_value * (cli_value-1))//2

for k in range(n):
    tmp = cli[li[k]]
    answer = count - (tmp * (tmp-1))//2 + ((tmp-1) * (tmp-2))//2
    print(answer)

Submission status: Accepted

Solution code:
import collections as cl
import numpy as np
n=int(input())
a=list(map(int,input().split()))
b=cl.Counter(a)
a_=np.array(list(b.values()))
c=np.sum(a_*(a_-1)/2)
for k in range(n):
    x=b[a[k]]
    if x==2:
        print(int(c-1))
    elif x==1:
        print(int(c))
    else:
        print(int(c-(x-1)))

Submission status: Accepted

Solution code:
import collections
import math

def func(n):
    ans = math.factorial(n)//((math.factorial(n-2))*math.factorial(2))
    return ans

N = int(input())
a = input().split()
ans = dict(collections.Counter(a))
sums = 0
for i in ans.values():
        if i < 2:
            continue
        else:
            sums += func(i)
            
for j in range(N):  
    print(sums-(ans[a[j]]-1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
ans = {}
cal = sum([b*(b-1)//2 for b in C.values()])
for a, b in C.items():
    ans[a] = cal - (b-1)
for a in A:
    print(ans[a])

Submission status: Accepted

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

A_num = collections.Counter(A)
values, counts = zip(*A_num.most_common())

ans = 0

for i in values :
  num = int(A_num[i])
  ans += num * (num - 1 ) // 2

for j in A :
  print(ans - int(A_num[j]) + 1 )

Submission status: Accepted

Solution code:
from scipy.misc import comb

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

dic = dict()
memo = [-1] * (N+1)
ans_vec = [-1] * (N+1) 

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

provisional = 0
for d in dic.values():
    if d >= 2:
        memo[d] = comb(d,2,exact=True)  
        memo[d-1] = comb(d-1,2,exact=True)   
        provisional += memo[d] 

for a in A:
    ans = 0
    t = dic[a]
    if ans_vec[t] != -1:
        print(ans_vec[t])
    else:
        if t < 2:
            print(provisional)
        else:
            ans = provisional - memo[t] 
            if t-1 >= 2:
                ans += memo[t-1]
            ans_vec[t] = ans
            print(ans)

Submission status: Accepted

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

C = collections.Counter(A)
#print(C)
calc = {}
for i,cnt in C.items():
    calc[i] = cnt*(cnt-1)//2
#print("calc:",calc)
calc_sum = sum(calc.values())
for i in range(N):
    ans = calc_sum - calc[A[i]]
#    print("ans",ans)
#    print("A[i]",A[i],"calc[A[i]]",calc[A[i]])
    if calc[A[i]] != 0:
        ans += (C[A[i]]-1)*(C[A[i]]-2)//2
#        print((calc[A[i]]-2)*(calc[A[i]]-1)//2)
#        ans += (calc[A[i]]-2)*(calc[A[i]]-1)//2
    print(ans)

Submission status: Accepted

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


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

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

    total = 0
    normal = dict()
    target = dict()
    for k, v in d.items():
        if v == 1:
            normal[k] = 0
            target[k] = 0
        else:
            normal[k] = (v-1) * ((v-1) + 1) // 2
            target[k] = normal[k] - (v-1)
            total += normal[k]

    for i in range(n):
        print(total - normal[a[i]] + target[a[i]])


main()

Submission status: Accepted

Solution code:
s = input()
n = int(s)
s2 = input().split()
k2 = [int(el) for el in s2]
a_dic = {}
cnt_dic = {}
for i in range(len(k2)):
  key = k2[i]
  if key in a_dic:
    a_dic[key] = a_dic[key] + 1
  else:
    a_dic[key] = 1

cnt = 0
for el in a_dic.items():
  if el[1] > 1:
    wk = (el[1] * (el[1] - 1 )) /2
    cnt += wk
    cnt_dic[el[0]] = wk
  else:
    cnt_dic[el[0]] = 0
    
for k in range(len(k2)):
  k_cnt = cnt_dic[k2[k]]
  wk2 = 0
  if k_cnt > 0:
    nn = a_dic[k2[k]] - 1
    wk2 = (nn * (nn - 1 )) /2
  print( int(cnt - cnt_dic[k2[k]] + wk2))

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

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

N = I()
A = LI()

c = collections.Counter(A)

tmp = 0
for i in c.values():
    tmp += comb(i)

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

Submission status: Accepted

Solution code:
import copy

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

B = copy.deepcopy(A)
B.sort()

#print A,B

C = [-1 for i in range(N)]
D = [-1 for i in range(N)]

n = -1
p = -1
for i in range(N):
    if n != B[i]:
        p+=1
        if (p > 0 and D[p-1] == 1): p-=1
        C[p] = B[i]
        D[p] = 1
        n = B[i]
    else:
        D[p] += 1
if D[p] == 1:
    C[p] = -1
    D[p] = -1
    p -= 1

#print C, D
#print "p=",p
p += 1

E = [-1 for i in range(p)]
F = [-1 for i in range(p)]

sum = 0
for i in range(p):
    E[i] = D[i]*(D[i]-1)/2
    sum += E[i]
    F[i] = E[i] - (D[i]-1)*(D[i]-2)/2

#print "C=",C
#print E, F, sum

def binsearch(k, s, e):
    #print k,s,e
    if s == e or s + 1 == e:
        if C[s] == k:
            return s
        elif C[e] == k:
            return e
        else:
            return -1
    elif (C[(s+e)/2] < k):
        return binsearch(k, (s+e)/2, e)
    else:
        return binsearch(k, s, (s+e)/2)

for i in range(N):
    pos = binsearch(A[i], 0, p-1)
    #print "pos = ", pos
    if pos >= 0:
        print sum - F[pos]
    else:
        print sum

Submission status: Accepted

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

ct = [0] * (2*10**5+1)
ans = [0] * (n+1)
ld = [0] * (n+1)

for i in range(len(la)):
    ct[la[i]] += 1

sa = set(la)

for j in sa:
    ans[j] = ct[j] * (ct[j]-1) // 2
    ld[j] = ans[j] - ((ct[j]-2) * (ct[j]-1) // 2)

total = sum(ans)

# print(ct[0:10])
# print(ans)

for k in range(n):
    knum = la[k]
    print(total - ld[knum])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

dp = [int(n*(n-1)/2) for n in range(N+1)]
cnt_org = [0]*(N+1)

for a in A:
  key = a
  cnt_org[a] += 1 

base = sum([dp[c] for c in cnt_org])

for k in range(N):
  diff = dp[cnt_org[A[k]]] - dp[cnt_org[A[k]]-1]
  print(base - diff)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
import collections
n=int(input())
A=list(map(int,input().split()))

def comb(n,r):
    r = min(n-r,r)
    if n<=1: return 0
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
c=collections.Counter(A)
combs,total=[0]*n,0
for a in set(A):
  tmp=comb(c[a],2)
  combs[a-1]=tmp
  total+=tmp
for a in A:
  print(total-combs[a-1]+comb(c[a]-1,2))

Submission status: Accepted

Solution code:
import collections

N = int(input())

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

c = collections.Counter(A)
# print(c)
# dic = {}
ans = 0
# for k,v in c.items():
#     dic[k] = int(v*(v-1)/2)
# print(dic)
for v in c.values():
    ans += int(v*(v-1)/2)

for k in range(N):
    Ak = A[k]
    cAk = c[Ak]
    ansk = ans - cAk + 1
    # ans[Ak] = int((cAk-1)*(cAk-2)/2)
    # print(dic,ans)
    print(ansk)