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 collections
import math

def comb(n, r):
  if n < r:
      return 0
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

ans = sum([comb(v, 2) for v in c.values()])
for ai in a:
    print(ans - c[ai] + 1)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
import collections
import sys
input = sys.stdin.readline
 
def combinations(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom
 
N = int(input())
 
AList = list(map(int, input().split()))

NumberList = set(AList)
CountList = collections.Counter(AList)

result = 0
for i in NumberList:
    if CountList[i] >= 2:
        result += combinations(CountList[i], 2)


for i in AList:
    print(result - (CountList[i] - 1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
v = {}
c = {}
for i in range(N):
  if A[i] in v:
    v[A[i]] += 1
  else:
    v[A[i]] = 1
x = 0
for i in range(1, N+1):
  _l = v.get(i, 0)
  c[i] = _l * (_l - 1) // 2
  x += c[i]
for i in range(N):
  print(x - (v[A[i]] - 1))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
l = list(map(int, input().split()))
set_l = set(l)

dictionary = Counter(l)
ll_dict = {}

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

for n in range(N):
  print(cnt-(dictionary[l[n]]-1))

Submission status: Accepted

Solution code:
import collections

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

u = collections.Counter(A)
sum = 0
for j, c in u.items():
    sum += int(c * (c - 1) / 2)

s = ""
for k in A:
    s += str(sum - u[k] + 1) + "\n"
print(s)

Submission status: Accepted

Solution code:
import collections

def solve(N, A):
    c_dict = collections.Counter(A)
    originals = [0 for i in range(N + 1)]
    minus = [0 for i in range(N + 1)]
    
    for (k, v) in c_dict.items():
        originals[k] = v * (v - 1) // 2
        minus[k] = ((v * (v - 1)) - (v - 1) * (v - 2)) // 2
        
    total = sum(originals)
    
    ans = []
    for a in A:
        ans.append(str(total - minus[a]))
    return '\n'.join(ans)
    
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    print(solve(N, A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import math
import itertools
import statistics
import collections

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

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

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

#print(total)
s = ""
for i in range(n):
    s = l[a[i]]
    ans = total-s+1
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    total = 0
    for k, v in C.items():
        if v > 1:
            total += v * (v - 1) // 2

    for a in A:
        v = C[a]
        if v > 1:
            print(total - v * (v - 1) // 2 + (v - 1) * (v - 2) // 2)
        else:
            print(total)


main()

Submission status: Accepted

Solution code:
from collections import Counter

def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
A=list(map(int,input().split()))
C=Counter()
C.update(A)
ans=0
L=[0]*N
for i in range(N):
    L[A[i]-1]+=1
Most=C.most_common()
for i in range(len(C)):
    ans+=Comb(Most[i][1],2)
for i in range(N):
    pri=ans
    pri=ans-Comb(L[A[i]-1],2)+Comb(L[A[i]-1]-1,2)
    print(pri)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
 
from collections import Counter
c=Counter(a)
 
max_c=0			# max combination
for i in c.values():
    max_c+=i*(i-1)//2     # 6+1+1 = 8
 
for num in a:
    k=c[num]      # 4 2 4 2 2 4 2 4
 
    print(max_c-(k*(k-1)//2)+((k-1)*(k-2)//2)) # different from max combination
                # 8 - 6 + 3
                # 5

Submission status: Accepted

Solution code:
import collections
import math


def comb(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

summ = sum([comb(v, 2) for v in c.values()])
for ai in a:
    print(summ - c[ai] + 1)

Submission status: Accepted

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

from collections import Counter 
import math 

res = [] # solution for each key in A
res_list = {} # solution for each value in A

# counter ver
A_c = Counter(A)
l = len(A_c)

def ncr(j):
  return int(j * (j-1) / 2) if j >= 2 else 0

# store ncr for each value 
combs = {k: ncr(v) for k, v in A_c.items()}

s = sum(combs.values())
  
for m in A_c.keys():
  res_list[m] = s - combs[m] + ncr(A_c[m] - 1)

for k in range(N):
  res.append(res_list[A[k]])
print('\n'.join([str(s) for s in res]))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
cnt=[0 for _ in range(N+1)]
for i in range(N):
    cnt[A[i]]+=1
cmb=[cnt[k]*(cnt[k]-1)//2 for k in range(N+1)]
SUM=sum(cmb)
for j in range(N):
    n=A[j]
    v=cnt[A[j]]
    print(SUM-cmb[n]+(v-1)*(v-2)//2)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
d = defaultdict(lambda: 0)
for i in range(len(A)):
    d[A[i]] += 1
total = 0
for k,v in d.items():
    total += v*(v-1)//2
for a in A:
    print(total - (d[a]-1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from scipy.misc import comb

def main():
     n = int(input())
     a = list(map(int, input().split()))
     num_a = dict()
     com_a = dict()

     for i in a:
          if not i in num_a.keys():
               num_a[i] = 1
          else:
               num_a[i] += 1

     for i in num_a.keys():
          value = num_a[i]
          com_a[i] = com(value)

     # print(num_a)
     # print(com_a)

     pre_ans = sum(com_a.values())
     # print('pre_ans:{}'.format(pre_ans))

     for i in range(n):
          k = a[i]
          ans = pre_ans - com_a[k] + com(num_a[k] - 1)
          print(ans)

def com(n):
     return comb(n, 2, exact=True)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
B = [0 for i in range(N)]
def nC2(n):
  return n*(n-1)//2
for i in range(N):
  B[A[i]-1] += 1
Ctot = 0
for i in range(N):
  Ctot += nC2(B[i])
for i in range(N):
  current_idx = A[i]-1
  if B[current_idx] == 1:
    count = Ctot
  else:
    count = Ctot + nC2(B[current_idx]-1) - nC2(B[current_idx])
  print(count)

Submission status: Accepted

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

#print(N)
#print(A)
all = {}
for a in A:
  if a not in all:
    all[a] = 0
  all[a] +=1

#print(all)
total = 0
for key, v in all.items():
  total += int(v*(v-1)/2)

for k in range(len(A)):
  ans = total - (all[A[k]] -1)
  print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
D={}
x=0
lis=[]
for a in A:
	try:
		D[a]=D[a]+((D[a]*2)**0.5//1)+1
	except:
		D[a]=0

z=sum(D.values())
for i in range(len(A)):
	x=0
	y=((D[A[i]]*2)**0.5)//1
	x=x+z-y
	print(int(x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())

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

c = Counter(a_list)

all_count = 0
for value in c.values():
    if value == 1:
        continue
    else:
        tmp = int((value * (value - 1))/2)
        all_count += tmp

for i in a_list:
    print(all_count - c[i] + 1)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7




n = I()
L = LI()
cnt = Counter(L)
ret = 0

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

for i in cnt.values():
    if i >= 2:
        ret += comb(i)

for l in L:
    if cnt[l] < 2:
        print(ret)
    else:
        print(ret - comb(cnt[l]) + comb(cnt[l] - 1))

Submission status: Accepted

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

for i,j in c.items():
    all_ans+=(j*(j-1))//2

for k in a:
    print(all_ans-((c[k]*(c[k]-1))//2)+(((c[k]-1)*(c[k]-2))//2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A1=sorted(A)
cnt=[0 for _ in range(N+1)] #cnt_table[i]: 数字iの要素の個数

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

cmb=[cnt[k]*(cnt[k]-1)//2 for k in range(N+1)]
SUM=sum(cmb)

for j in range(N):
    n=A[j]
    v=cnt[A[j]]
    print(SUM-cmb[n]+(v-1)*(v-2)//2)

Submission status: Accepted

Solution code:
def main():
  from collections import defaultdict
  from collections import Counter
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = Counter(a)
  e = defaultdict(int)
  
  func = lambda x: x*(x-1)//2 if x>=2 else 0
  v = list(d.values())
  base = sum(list(map(func, v)))
  
  for k,v in d.items():
    if v<=1:
      e[k] = base - 0
    else:
      e[k] = base - ( v*(v-1)//2 - (v-1)*(v-2)//2 )
    
  p = [str(e[x])+'\n' for x in a]
  q = ''.join(p)
  print(q)
      
 
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

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

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

Submission status: Accepted

Solution code:
from collections import Counter


from operator import mul
from functools import reduce


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


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

for A_i in A:
    c[A_i] += 1

gsum = 0
for i in range(1, N + 1):
    if (c[i] != 0):
        gsum += cmbf(c[i], 2)

for i in A:
    print(gsum-(c[i]-1))

Submission status: Accepted

Solution code:
# 解き直し
import collections

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

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

for k_ball in A:
    cnt = c[k_ball]
    print(total - (cnt * (cnt - 1) // 2) + ((cnt - 1) * (cnt - 2) // 2))

Submission status: Accepted

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

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

for i in range(n):
  print(ans-c[k[i]]+1)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a_list = list(map(int,input().split()))
c = collections.Counter(a_list)
result = 0
for i in c.values():
  result += i * (i - 1)//2

for a in a_list:
  print(result - c[a] + 1)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    d = defaultdict(lambda : 0)
    for i in a:
        d[i] += 1
    s = 0
    for i,j in d.items():
        s += j*(j-1)>>1
    for i in a:
        j = d[i]
        print(s-(j*(j-1)>>1)+((j-1)*(j-2)>>1))
    return

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

Submission status: Accepted

Solution code:
import collections

def check(A):
    count = 0
    for j in A:
        n = A[j]
        if n > 1:
            count += n * (n - 1) / 2
            
    return int(count)


N = int(input())
A = list(map(int, input().split(" ")))
A2 = collections.Counter(A)
# print(A2)
Nsum = check(A2)
# print(Nsum)
for i in range(0,N):
    num = A[i]
    count = A2[num]
    out = Nsum - ( count - 1 )
    print(out)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int,input().split()))
d = collections.Counter(a)
s = sum([i*(i-1)//2 for i in d.values()])

for j in a:
    print(s-d[j]+1)

Submission status: Accepted

Solution code:
import collections
from sys import stdin

# # not AC
# def counter(num_arr):
#     uniq_dict = {}
#     for val in num_arr:
#         uniq_dict[val] = uniq_dict.get(val, 0) + 1
#     return uniq_dict


# def get_result(data):
#     N = data[0][0]
#     A = data[1]
#     uniq_cnt = counter(A)
#     ans = {}
#     for key in uniq_cnt.keys():
#         tmp = uniq_cnt.copy()
#         tmp[key] -= 1
#         ans[key] = int(sum([val * (val-1) / 2 for val in tmp.values()]))

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

def get_result(data):
    N = data[0][0]
    A = data[1]
    uniq_cnt = collections.Counter(A)
    all_selection = 0
    for val in uniq_cnt.values():
        all_selection += int(val * (val-1) / 2)

    for i in range(N):
        print(all_selection - uniq_cnt[A[i]] + 1)


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    get_result(data)

Submission status: Accepted

Solution code:
n=int(input())
su=0
a = list(map(int, input().split())) 
da={}
for i in range(len(a)):
    if a[i] in da:
        da[a[i]]+=1
    else:
        da[a[i]]=1
 
for p in da.keys():
    su+=da[p]*(da[p]-1)/2

for k in range(len(a)):
    sut=su-(da[a[k]]-1)
    print(int(sut))

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]*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())
l=[int(i) for i in input().split()]
c=[0 for i in range(n)]
for i in l:
    c[i-1]+=1
res=0
for i in range(n):
    res+=c[i]*(c[i]-1)//2
for i in range(n):
    j=l[i]-1
    print(res-c[j]*(c[j]-1)//2+(c[j]-1)*(c[j]-2)//2)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
d={}
ans=0
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d.keys():
    x=d[i]*(d[i]-1)//2
    ans+=x
for i in l:
    if d[i]>2:
        print(ans-(d[i]*(d[i]-1)//2)+((d[i]-1)*(d[i]-2)//2))
    elif d[i]==2:
        print(ans-1)
    else:
        print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = set([x for x in a if cc[x] > 1])
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

for k in A:
    if k in dup:
        print(int(al-cc[k]+1))
    else :
        print(int(al))

Submission status: Accepted

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

sums = 0

for i in a.values():
    sums +=  i * (i - 1) // 2

for j in res:
    print(sums - a[j] * (a[j] -1) //2 + (a[j]-1) * (a[j] -2 ) // 2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
c = collections.Counter(a)
val_list=list(c.values())
v_count=map(lambda x: x * (x-1), val_list)
total_count=int(sum(v_count)/2)

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

Submission status: Accepted

Solution code:
import math
n=int(input())
A = list(map(int, input().split()))
res=[0]*n
import collections
def com(n, r):
    if n<r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

af=collections.Counter(A)
res2=list(af.items())
res3=[0]*n
s=0
for i in range(0,len(res2)):
    s=s+com(res2[i][1],2)
for j in range(0,len(res2)):
    res3[res2[j][0]-1]=s-res2[j][1]+1
for i in range(n):
    res[i]=res3[A[i]-1]

for j in range(n):
    print(res[j])

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb

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

num = 0
for v in c.values():
  if v > 1:
      num += comb(v, 2,exact=True)
for k in range(N):
  print(int(num - (c[A[k]]-1)))

Submission status: Accepted

Solution code:
import collections

def count_defaultdict(it):
    counter = collections.defaultdict(int)
    for x in it:
        counter[x] += 1
    return dict(counter)

_ = int(input())
l = list(map(int, input().split()))
all_sum = 0
count = count_defaultdict(l)
for _, v in count.items():
    all_sum += v * (v - 1) // 2
for i in range(len(l)):
    if count[l[i]] > 1:
        v = count[l[i]]
        print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
    else:
        print(all_sum)

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
count = collections.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:
import collections

def main(): 
    N = int(input())
    A = list(map(int, input().split()))
    cnt = [0]*(N+1)
    C = collections.Counter(A)
    
    for k, v in C.items():
        cnt[k] = v*(v-1)//2
    S = sum(cnt)
    
    for i in A:
        print(S - cnt[i] + (C[i]-1)*(C[i]-2)//2)
        
main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
total = 0
los = [0]*n
for x, y in c.items():
    if y >= 2:
        total += y * (y-1) // 2
    if y >= 2:
        los[x-1] = y*(y-1)//2 - (y-1)*(y-2)//2

for i in range(n):
    print(total - los[a[i]-1])

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

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

# 各数値の重複数辞書
counter = collections.Counter(a)

# 組み合わせ数
def combi(num):
    if num < 2:
        pair = 0
    else:
        pair = int(num * (num-1) / 2)
    return pair


total = sum([combi(v) for v in counter.values()])
for el in a:
    print(total - (counter[el] - 1))

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)   # c: Counter({1: 3, 2: 2})など
for k, v in c.items():
    re[k] = v*(v-1) // 2

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = set([x for x in a if cc[x] > 1])
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

# for k in A:
#     if k in dup:
#         print(int(al-cc[k]+1))
#     else :
#         print(int(al))
[print(int(al-cc[k]+1)) if k in dup else print(int(al)) for k in A]

Submission status: Accepted

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

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

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

c = Counter(k)

li = c.most_common()

a_li = [x for x in li if x[1] != 1]

max = 0

for i in a_li:
    num = i[1]
    max = max + cmb(num,2)
    
#print(max)

for i in k:
    num = c[i]
    if num == 1:
        ans = max
    elif num == 2:
        ans = max - 1
    else:
      pre = cmb(num,2)
      cur = cmb(num-1,2)
      ans = max - pre + cur
    print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

cnt = [0] * N

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

combination = [0] * N

for a in A:
    combination[a - 1] = cnt[a - 1] * (cnt[a - 1] - 1) // 2

total = sum(combination)

for a in A:
    c = combination[a - 1]
    b = cnt[a - 1]
    ans = total - c + c * (b - 2) // b
    print(ans)

Submission status: Accepted

Solution code:


def main():
    import collections

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

    allc = collections.Counter(A)
    # all_list = [0]*N
    allsu = 0
    for pairs in sorted(allc.values(), reverse=True):
        allsu += pairs*(pairs-1)//2

    for a in A:
        num = allc[a]
        print(allsu - (num-1))

    # for i in range(N):
    #     x = A[i]
    #     A[i] = 0
    #     tmp_A = cc(A)
    #     su = 0
    #     for pairs in sorted(tmp_A.values(), reverse=True):
    #         if pairs >= 2:
    #             su += pairs*(pairs-1)//2
    #         else:
    #             break
    #     A[i] = x
    #     print(su)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

counters = {}
cevap_list = {}
sayilar = set(numbers)
for i in numbers:
    if i in counters:
        counters[i] += 1
    else:
        counters[i] = 1

sonuc = 0
for sayi in counters:
    count = counters[sayi]
    sonuc += int(count * (count - 1) / 2)


for i in range(n):
    print(sonuc-counters[numbers[i]]+1)

Submission status: Accepted

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

def main():
    n = int(input())
    lis = list(map(int,input().split()))
    d = Counter(lis)
    su = 0
    for e in d:
        su += (d[e] - 1) * d[e] // 2

    for e in lis:
        if d[e] <= 1:
            print(su)
        else:
            k = d[e]
            res = su - k*(k-1)//2 + (k-1)*(k-2)//2
            print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
import math

#****************************************************************
def comb(n):
    if n < 2:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
#****************************************************************

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

c = collections.Counter(A)
full_ans = sum(list(map(comb, c.values())))

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

Submission status: Accepted

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

c=Counter(a)

al=0
for i in c.values():
    al+=(i*(i-1))//2
for i in range(n):
    print(al - (c[a[i]]-1))

Submission status: Accepted

Solution code:
sel='D'
#A
if sel=='A':
    N,M=map(int,input().split())
    ans=0
    ans+=M*(M-1)//2
    ans+=N*(N-1)//2
    print(ans)

#B
if sel=='B':
    def ispal(s):
        for i in range(len(s)//2+1):
            if s[i]!=s[-(i+1)]:
                return False
        return True

    S=input()
    if ispal(S) and ispal(S[:(N-1)//2]) and ispal(S[(N+3)//2-1:]):
        print('Yes')
    else:
        print('No')

#C
if sel=='C':
    L=int(input())
    print((L**3)/27)

#D
if sel=='D':    
    N=int(input())
    A=[int(i) for i in input().split()]
    kin=list(set(A))
    cnt={}
    for k in kin:
        cnt[k]=0
    for a in A:
        cnt[a]+=1
    SUM=0
    for k in kin:
        SUM+=cnt[k]*(cnt[k]-1)//2
    for a in A:
        if cnt[a]>=2:
            print(SUM-cnt[a]+1)
        else:
            print(SUM)
        
#E
if sel=='E':
    a=0

#F
if sel=='F':
    N,S=map(int,input().split())
    A=[int(i) for i in input().split()]
    
    for l in range(N):
        for r in range(l,N):
            print(tab[l][r])

Submission status: Accepted

Solution code:
import sys
import numpy as np


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


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    count = np.bincount(A)
    # print(count)
    total = (count * (count - 1)).sum() // 2
    answer = (total - (count - 1))[A]
    print("\n".join(answer.astype(str)))


main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def calc(x):
    return x*(x-1)//2


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

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

count = 0
for k, v in dd.items():
    if v >= 2:
        count += calc(v)
for i in range(N):
    ans = 0
    if dd[A[i]] >= 3:
        ans = count - calc(dd[A[i]]) + calc(dd[A[i]]-1)
    elif dd[A[i]] == 2:
        ans = count - 1
    elif dd[A[i]] == 1:
        ans = count
    print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = input().split()

c = collections.Counter(A)
cou = list(c.values())

ans = 0
for j in range(len(cou)):
	m = cou[j]
	k = m*(m-1)/2
	ans += int(k)


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

Submission status: Accepted

Solution code:
from collections import Counter
def num_to_comb(val):
    return val*(val-1)//2

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

if len(set(ball_list)) == num_:
    for i in range(num_):
        print(0)

elif len(set(ball_list)) == 1:
    for i in range(num_):
        print((num_ - 1)*(num_ - 2) // 2)

else:
    temp = Counter(ball_list)
    ans_dict={}
    temp_ans_dict = {}
    
    for num in set(ball_list):
        if temp_ans_dict.get(temp[num], 0) == 0:
            temp_dict = temp.copy()
            temp_dict[num] = temp_dict[num]-1
            ans_dict[num] = sum(list(map(num_to_comb, temp_dict.values())))
            temp_ans_dict[temp[num]] = ans_dict[num]
        else:
            ans_dict[num] =temp_ans_dict[temp[num]]
            
    for i in ball_list:
        print(ans_dict[i])

Submission status: Accepted

Solution code:
import collections

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

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

counts = collections.Counter(A_list)
ans = 0
for _, v in counts.items():
    ans += v * (v - 1) / 2

for k in range(1, N + 1):
    n = counts[A_list[k - 1]]
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

Submission status: Accepted

Solution code:
import collections
 
def LI():
	return [ int(s) for s in input().split() ]
 
N = int(input())
A = LI()
 
c_counter = [0]*(2*(10**5))
 
for a in A:
  c_counter[a]+=1
 
total = 0
total_dict = [0]*(2*(10**5))
 
for a in range(len(c_counter)):
  v = c_counter[a]
  if v >= 2:
    comb = v*(v-1)//2
    total_dict[a] = comb
    total += comb
 
for a in A:
  if total == 0:
    print(0)
  else:
    print(total - total_dict[a] + (c_counter[a]-1)*(c_counter[a]-2)//2)

Submission status: Accepted

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

b = [0]*(a[n-1])
d = [0]*(a[n-1])

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        l = ((k*(k-1))//2)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)