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: Wrong Answer

Solution code:

#####

import numpy as np
# from numpy import int32, int8
from collections import Counter

#####

def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs


def unique(x):
	count=Counter()
	for xi in x:
		count[xi]+=1
	u,c=zip(*count.items())
	u=np.array(u)
	c=np.array(c)
	return u,c


def main(inputs):
	n=int(inputs[0][0])
	aa=getIntList(inputs[1])

# 	n=20000
# 	aa=np.random.randint(1,n+1,n)
	
	aa=np.array(aa)
# 	aUnique, count=np.unique(aa, return_counts=True)
	aUnique, count=unique(aa)
	
# 	print(n)
# 	print("aa",aa)
# 	print("aUnique",aUnique)
# 	print("count  ",count)
	
# 	countUnique,countCount=np.unique(count, return_counts=True)
	countUnique,countCount=unique(count)
	
	pat={}
	countAns=np.empty(len(countUnique), np.int32)
	countAns1=np.empty(len(countUnique), np.int32)
	for ci,c in enumerate(countUnique):
		if c not in pat:
			ni=c*(c-1)//2
			pat[c]=ni
		countAns[ci]=pat[c]
		
		c-=1
		if c not in pat:
			ni=c*(c-1)//2
			pat[c]=ni
		countAns1[ci]=pat[c]
	
# 	print("countUnique",countUnique)
# 	print("countCount ",countCount)
# 	print("countAns ",countAns)
# 	print("countAns1",countAns1)
	
	countUniqueIndex=dict([(c,i) for i,c in enumerate(countUnique)])
	
	uniNums=np.empty(len(aUnique), np.int32)
	for cui,cu in enumerate(countUnique):
		countCount[cui]-=1
		num0=(countAns*countCount).sum()
		countCount[cui]+=1
		num1=countAns1[cui]
# 		print(cu,num0+num1)
		countMask=count==cu
		uniNums[countMask]=num0+num1
	
# 	print("uniNums", uniNums)
	
	nums=np.empty(n, np.int32)
	for ai,a in enumerate(aUnique):
		mask=aa==a
		nums[mask]=uniNums[ai]
		
	print("\n".join(map(str,nums)))
	

if __name__=="__main__":
	inputs=getInputs()
# 	inputs=simInputs()
	main(inputs)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
#L = [0 for i in range(N)]
dict = {}

for i in A:
  if i in dict:
    dict[i] = dict[i] + 1
  else:
    dict[i] = 1

print(dict)
total = 0;
for i in dict:
#  print("i:" + str(i) + " " + str(dict[i]))
  total += dict[i]*(dict[i]-1) // 2

#print("T:" + str(total))

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

Submission status: Wrong Answer

Solution code:
def aC(a):
  if a == 0:
    return 0
  return a*(a-1)/2

N = input()
N = int(N)
B = input().split(" ")
A = []
for a in B:
  A.append(int(a))
  
for i in range(0,N):
  pre_A = A[:]
  pre_A.pop(i)
  count_list = []
  for i in range(1,N+1):
    count_list.append(pre_A.count(i))

  print(sum(map(aC,count_list)))

Submission status: Wrong Answer

Solution code:
import math
N = (int(input()))
items = list(map(int,input().split()))
for i in range(len(items)):    
    rest = items[:i] + items[i+1:]
    rest_unique = list(set(rest))
    out = 0
    for j in rest_unique:
        cnt = sum([1 for x in rest if x == j])
        if cnt < 2:
            out = out
        else:
            out = out + math.factorial(cnt)/math.factorial(cnt-2)/2
    print(out)

Submission status: Wrong Answer

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


def ncr(n, r, mod=10**9+7):
    x = 1
    y = 1
    for i in range(1, r+1):
        x = x * (n-i+1) % mod
        y = y * i % mod
    return (x*pow(y, mod-2, mod)) % mod

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

cmemo = {}
for k, v in dmemo.items():
    if v in cmemo:
        continue

    val = ncr(v, 2)
    cmemo[v] = val

for a in A:
    ans = 0
    for dk, dv in dmemo.items():
        if dk == a:
            dv -= 1
        
        if dv not in cmemo:
            val = ncr(dv, 2)
            cmemo[dv] = val
        ans += cmemo[dv]
    print(ans)

Submission status: Wrong Answer

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 = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)

pre = 0
for i in range(n):
    if i == 0:
        c[a[i]] -= 1
    else:
        c[pre] += 1
        c[a[i]] -= 1
        print(sum(comb(v, 2) for v in c.values()))
    pre = a[i]

Submission status: Wrong Answer

Solution code:
from collections import Counter

def nCk(n, k, m=10**9+7):
    a = b = 1
    for i in range(k):
        a = (a * (n-i)) % m
        b = (b * (i+1)) % m
    return (a * pow(b, m-2, m)) % m

n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
c = {}
d = {}
for k, v in b.items():
    c[k] = nCk(v, 2)
    d[k] = nCk(v-1, 2)
total = sum(c.values())
for i in range(n):
    ans = total - c[a[i]] + d[a[i]]
    print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
for k in range(N):
	s=0
	for i in range(N):
		for j in range(i+1,N):
			if A[i]==A[j]:
				s=s+1
	if A[i]==A[k]:
		s=s-1
	print(s)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

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

def main():
    n = ni()
    A = []
    Onaji = {}
    for i in na():
        i -= 1
        A.append(i)
        if i in Onaji:
            Onaji[i] += 1
        else:
            Onaji[i] = 1

    kumi = {}
    minuskumi = {}
    Sum = 0
    for a in A:
        if Onaji[a] <= 1:
            continue
        if a in kumi:
            continue
        kumi[a] = cmb(Onaji[a], 2)
        if Onaji[a] - 1 <= 1:
            minuskumi[a] = 0
        else:
            minuskumi[a] = cmb(Onaji[a] - 1, 2)
        Sum += kumi[a]
    for a in A:
        if a in minuskumi and a in kumi:
            print(minuskumi[a] + Sum - kumi[a])
        else:
            print(0)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3


def nC2(x):
    if x == 1 or x == 0:
        return 0
    else:
        return x * (x - 1) // 2


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

C = [0] * (N + 1)
C2 = [0] * (N + 1)
for i in A:
    C[i] += 1
for i in range(N):
    C2[i] = nC2(C[i])
sumup = sum(C2)

for k in A:
    print(sumup - C2[k] + nC2(C[k] - 1))

Submission status: Wrong Answer

Solution code:
import collections
from sys import stdin
n = int(stdin.readline().rstrip())
ass = list(map(int, stdin.readline().rstrip().split()))

c = collections.Counter(ass)

cnum =[0]*len(c)
keys = c.keys()

cs = {}
for i in keys:
    key = c[i]
    combi = key *(key-1) / 2
    cs[i] = combi

sum = sum(cs.values())

for i in range(n):
    add = c[ass[i]]-1
    adds = add *(add-1) / 2
    ans = sum - cs[ass[i]] + adds
    print(ans)

Submission status: Wrong Answer

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

import sys
sys.setrecursionlimit(10 ** 6)
memo = [1, 1]
def fact(n):
	if len(memo) > n:
		return memo[n]
	else:
		for i in range(len(memo), n+1):
			memo.append(memo[i-1] * i)
		return memo[n]
def main():
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	cnt = {}
	sumcase = 0
	for i in range(n):
		cnt[a[i]] = cnt.get(a[i], 0) + 1
	for ai in cnt:
		if cnt[ai] >= 2:
			sumcase += fact(cnt[ai]) // fact(2) // fact(cnt[ai]-2)
	for i in range(n):
		if cnt[a[i]] == 2:
			print(sumcase - fact(cnt[a[i]]) // fact(2) // fact(cnt[ai]-2))
		elif cnt[a[i]] > 2:
			#print("hoge", a,  cnt[a[i]], sumcase, cnt)
			print(sumcase - fact(cnt[a[i]]) // fact(2) // fact(cnt[a[i]]-2) + fact(cnt[a[i]]-1) // fact(2) // fact(cnt[a[i]]-3))
		else:
			print(sumcase)
main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
D = [0]*(N+1)
for a in A:
  D[a] += 1
tot = sum([D[i]*(D[i]-1)//2 for i in range(N)])
#print(D)
#print([D[i]*(D[i]-1)//2 for i in range(N)])

#print([(D[i]-1)*(D[i]-2)//2 for i in range(N)])
#print(tot)
for a in A:
  print(tot - D[a]+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
S=[0 for j in range(N+1)]
count=0
for i in range(N):
  S[A[i]]+=1
for i in range(N):
  count+=S[i]*(S[i]-1)//2
for i in range(N):
  print(count-(S[A[i]]-1))

Submission status: Wrong Answer

Solution code:
n=int(input())
S=list(map(int,input().split()))
for i in S:
  print(len(S)-S.count(i)+1)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

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

for i in range(len(a)):
  aa = copy.copy(a)
  del aa[i]
  for av in aa:
    if av in matches:
      matches[av] += 1
    else:
      matches[av] = 1
  combi_cnt = 0
  for v in matches:
    combi_cnt += len(list(itertools.combinations(range(v), 2)))
  print(combi_cnt)

Submission status: Wrong Answer

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

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

for i in range(1,n+1):
  cmb1[i]=(l[i]*(l[i]-1))//2
  cmb2[i]=((l[i]-1)*(l[i]-2))//2

kei1=0
for i in range(1,n):
  kei1+=cmb1[i]

for i in range(1,n+1):
  print(kei1-cmb1[a[i-1]]+cmb2[a[i-1]])

Submission status: Wrong Answer

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    n=int(input())
    a=list(map(int,input().split()))
    from collections import Counter
    ss=Counter(a)
    s = [i for i in ss.most_common() if i[1] > 1]
    if len(s) == 0:
        for i in a:
            print(0)
        return 0
    b=list(set(a))
    dp = [0]*len(b)
    s1,s2 = zip(*s)
    cnt=0
    for i in a:
        if dp[b.index(i)] == 0:
            cnt = 0
            if i in s1:
                for j in range(len(s1)):
                    if i == s1[j]:
                        cnt += (s2[j]-1)*(s2[j]-2)//2
                    else:
                        cnt += s2[j]*(s2[j]-1)//2
                dp[b.index(i)] = cnt
            else:
                dp[b.index(i)] = [i[1]*(i[1]-1)//2 for i in s]
        print(dp[b.index(i)])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from scipy.misc import comb

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

dic = dict()

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

tmp = 0
for d in dic.values():
    if d >= 2:
        tmp += comb(d,2,exact=True) 
        #print(f'{tmp=}')

for a in A:
    ans = 0
    t = dic[a]
    if t >= 2:
        ans = tmp - comb(t,2,exact=True) 
    if t-1 >= 2:
        ans += comb(t-1,2,exact=True) 

    print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	ap = {}
	for i in A:
		if i in ap:
			ap[i] += 1
		else:
			ap[i] = 1

	for i in A:
		ap_tmp = ap.copy()
		ap_tmp[i] -= 1
		#print(ap_tmp)
		ans = 0
		for k, v in ap_tmp.items():
			if v > 1:
				ans += combination(v, 2)
		print(ans)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def cmb(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

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
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

print(d)
a = defaultdict(int)
b = defaultdict(int)
for key, value in d.items():
    a[key] = value * (value - 1) //2
    b[key] = value * (value - 1) //2 - (value - 1) * (value - 2) // 2
#print(a)
#print(b)
total = 0
for key, value in a.items():
    total += value
#print(total)
for i in range(N):
    ans = total - b[A[i]]
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n=ii()
    a=lmis()
    nlis=[0]*(n+1)
    cnt=0
    for i in range(n): nlis[a[i]]+=1
    for i in range(n): cnt+=nlis[i]*(nlis[i]-1)//2
    for i in range(n): print(cnt+1-nlis[a[i]])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def cmb(n, k, mod):
    numerator = 1
    for i in range(k):
        numerator = (numerator * (n - i)) % mod

    denominator = 1
    for i in range(1, k + 1):
        denominator = (denominator * i) % mod

    return (numerator * pow(denominator, mod - 2, mod)) % mod


import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
freq = {}
for i in a:
    if i in freq:
        freq[i] += 1
    else:
        freq[i] = 1

inf = 1001001001
dp = [inf for _ in range(200005)]
dp2 = [inf for _ in range(200005)]
mod = 10**9 + 7

base = 0
for k, v in freq.items():
    dp[k] = cmb(v, 2, mod)
    dp2[k] = cmb(v - 1, 2, mod)
    base += dp[k]

# print(dp)

for i in a:
    print(base - dp[i] + dp2[i])

Submission status: Wrong Answer

Solution code:
def cmb(n):
    return n * (n-1) / 2

def main():

    N = int(input())

    a_l = [0] * N

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

    for i in nums:
        a_l[i-1] += 1

    for i in nums:
        a_l[i-1] -= 1
        total = 0
        for j in a_l:
            if j > 1:
                total += cmb(j)
        print(total)
        a_l[i-1] += 1


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
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()))

a_cnt = Counter(a)

a_comb = {}
for key, val in a_cnt.items():
    if val >= 2:
        a_comb[key] = combinations_count(val, 2)
    else:
        a_comb[key] = 0
print(a_cnt)
print(a_comb)

for num in a:
    dic = a_comb.copy()
    if dic[num] <= 1:
        dic[num] = 0
    else: 
        dic[num] = combinations_count(a_cnt[num]-1, 2)
    print(int(sum(dic.values())))

Submission status: Wrong Answer

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

from collections import defaultdict
c = defaultdict(int)
for ai in a:
    c[ai] += 1

class Combination:
    def __init__(self, N=2*10**5, mod=10**9 + 7):
        self.mod = mod
        self.N = N
        self.fact = [1, 1]  # fact[n] = (n! mod p)
        self.factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
        self.inv = [0, 1]  # factinv 計算用
        for i in range(2, N + 1):
            self.fact.append((self.fact[-1] * i) % mod)
            self.inv.append((-self.inv[mod % i] * (mod // i)) % mod)
            self.factinv.append((self.factinv[-1] * self.inv[-1]) % mod)

    def nCr(self, n, r):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return self.fact[n] * self.factinv[r] * self.factinv[n-r] % self.mod
comb = Combination()

d = {}
t = 0
for ci in c:
    #print(ci, c[ci])
    t += comb.nCr(c[ci],2)
#print(t)
for ai in a:
    print(t - comb.nCr(c[ai],2) + comb.nCr(c[ai]-1,2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = [0 for _ in range(n)]
r = []
for i in range(n):
    c[a[i-1]-1] += 1
for i in range(n):
    r.append(c[i-1])
kai = []
v = []
for i in range(len(r)):
    kai.append((r[i]*(r[i]-1)//2))
    v.append(kai[i] - max(((r[i]-1)*(r[i]-2)//2),0))
#print(v,kai)
ans = sum(kai)
for i in range(len(v)):
    for j in range(v[i]+1):
        print(ans - v[i])

Submission status: Wrong Answer

Solution code:
import copy
import itertools

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

for i in range(N):
    tmp = copy.deepcopy(A)
    tmp.pop(i)

    res = 0
    for j in list(set(tmp)):
        l = tmp.count(j)
        if(l<2):
            break
        res += len(list(itertools.combinations(list(range(l)),2)))

    print(res)

Submission status: Wrong Answer

Solution code:
from collections import OrderedDict
N = int(input())
A = list(map(int, input().split()))
# maybe use a dictionary
ball_values = OrderedDict()
for val in A:
  if val not in ball_values:
    ball_values[val] = 1
  else:
    ball_values[val] += 1
# get total pairs of all balls
total_possible_combos = sum(item[1]*(item[1]-1)//2 for item in ball_values.items())
# for each k, take away the loss of pairs due to removing it
k_outputs = [total_possible_combos-(ball_values[val]-1) for val in A]
print(k_outputs)

Submission status: Wrong Answer

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

for i in range(N):
    num = lis[i]
    if num != 0:
        lis1[i] = int(num*(num-1)/2)
        lis2[i] = int((num-1)*(num-2)/2) - lis1[i]

total = sum(lis1)

for i in range(N):
    val = A[i]
    print(total + lis2[val])

Submission status: Wrong Answer

Solution code:
def nCr(n, r):
    import math
    # nCrのr>nは組み合わせが存在しないので0を返す
    # raiseすべきのこともあるかも
    if r > n:
        return 0
    return math.factorial(n) // ((math.factorial(n - r) * math.factorial(r)))

memo = [-1] * 300000
memo2 = [-1] * 300000
memo3 = [-1] * 300000

n = int(input())
dat = list(map(int, input().split()))
from collections import Counter
c = Counter(dat)
total = 0
for k in c:
    if c[k] > 1:
        memo[k] = nCr(c[k], 2)
        memo2[k] = nCr(c[k]-1, 2)
        memo3[k] = memo[k] - memo2[k]
        total += memo[k]
#print(total)
#print(memo[:10])
#print(memo2[:10])
#print(memo3[:10])
for i in range(n):
    if memo3[dat[i]] == -1:
        print(0)
    else:
        print(total - memo3[dat[i]])

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    cnt=[0]*n
    for i in range(n):
        cnt[a[i]-1]+=1
    res=0
    for i in range(n):
        if cnt[i]>1:
            res+=cnt[i]*(cnt[i]-1)//2
    ans=[0]*n
    for i in range(n):
        if cnt[i]>=1:
            ans[i]=res-(cnt[i]-1)
    print(cnt)
    print(ans)
    for i in range(n):
        print(ans[a[i]-1])
        
if __name__=="__main__":
    main()

Submission status: Wrong Answer

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


count = [0]*(N+2)
for i in range(N):
    id = A[i]
    count[id] = count[id]+1

sumN = 0
for i in range(N):
    sumN = sumN + long(count[i]*(count[i]-1)/2)

#print answer
for i in range(N):
    id = A[i]
    sumz = sumN - long(count[id]*(count[id]-1)/2)
    sumz = sumz + long((count[id] - 1)*(count[id] - 2)/2)
    print(sumz)

Submission status: Wrong Answer

Solution code:

def power(x, y, mod):
    """
    https://qiita.com/Yaruki00/items/fd1fc269ff7fe40d09a6
    二分累乗法でx ** yを求める
    
    2 ** n = 4 ** (n//2) = 16 ** (n//4)
    みたいなことをする
    """

    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y//2, mod)**2 % mod
    else            : return power(x, y//2, mod)**2 * x % mod


def mul(a, b, mod):
    """
    https://qiita.com/Yaruki00/items/fd1fc269ff7fe40d09a6
    modを加味したかけ算を行う
    """
    return ((a % mod) * (b % mod)) % mod

def div(a, b, mod):
    """
    https://qiita.com/Yaruki00/items/fd1fc269ff7fe40d09a6
    modを加味した割り算を行う    
    """
    if a == b:
        return 1
    return mul(a, power(b, mod-2, mod), mod)


def factorial_part(start, last, mod):
    """
    startからlastまでのかけ算を行う
    
    factorial_part(2, 4, mod) = 2 * 3 * 4 = 24    
    """
    assert(start <= last)
    x = 1
    for k in range(start, last+1):
        x *= k
        x %= mod
    return x


def comb(n, a):
    mod = int(1e9) + 7
    return div(factorial_part(n - a + 1, n, mod), factorial_part(1, a, mod), mod)

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


from collections import Counter

c = Counter(a)

s = set(c.values())

dic = {}
for ss in s:
    if ss not in dic.keys():
        dic[ss] = int(comb(ss, 2))
for ss in s:
    ss -= 1
    if ss not in dic.keys():
        dic[ss] = int(comb(ss, 2))

base = 0
for k in c.keys():
    base += dic[c[k]]

for k in range(len(a)):
    print(int(base - dic[c[a[k]]] + dic[c[a[k]] - 1]))

Submission status: Wrong Answer

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

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

for i in range(n):
  cmb1[i]=(l[i]*(l[i]-1))//2
  cmb2[i]=((l[i]-1)*(l[i]-2))//2

kei1=0
for i in range(n):
  kei1+=cmb1[i]

for i in range(n):
  print(kei1-cmb1[a[i]]+cmb2[a[i]])

Submission status: Wrong Answer

Solution code:
import math

 
IA = lambda: map(int,input().split())
IAS= lambda: map(str,input().split())

n=int(input())
def f1(n):
    res=1
    for i in range(1,n+1):
        res*=i
    return res
def f(n):
    if n<2:return 0
    return n*(n-1)//2

a=list(IA())
vis=[0 for i in range(0,n+1)]
for it in a:
    vis[it]+=1
sum1=0

for i in range(1,n):
    sum1+=f(vis[i])
ans=[int(-1) for i in range(0,n+1)]
for it in a:
    if ans[it]==-1:
        ans[it]=sum1-f(vis[it])+f(vis[it]-1)
    print(ans[it])

Submission status: Wrong Answer

Solution code:
def aC(a):
  if a == 0:
    return 0
  return a*(a-1)/2

N = input()
N = int(N)
B = input().split(" ")
A = []
for a in B:
  A.append(int(a))
  
for i in range(0,N):
  pre_A = A[:]
  pre_A.pop(i)
  count_list = []
  for i in range(1,N+1):
    count_list.append(pre_A.count(i))

  ans = 0
  for i in count_list:
    ans = ans + aC(i)

  print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
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 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)]
from functools import lru_cache

N = I()
A = LI()

@lru_cache(maxsize=None)
def ncr(n, r, p=MOD):
    a = 1
    b = 1
    for i in range(r):
        a = (a * (n - i) % p)
        b = (b * (r - i) % p)

    return a * pow(b, p - 2, p) % p

tmp = 0
AA = collections.Counter(A)
As = AA.items()
for n, t in As:
    tmp += ncr(t, 2)

for a in A:
    an = AA[a]
    print(tmp - ncr(an, 2) + ncr(an - 1, 2))

Submission status: Wrong Answer

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

N = int(input().rstrip())
As = [int(x) for x in input().rstrip().split()]
print(As)
d = {}
for n in As:
    if n not in d.keys():
        d[n] = 1
    else:
        d[n] += 1
for n in As:
    d[n] -= 1
    ret = 0
    for val in d.values():
        if val > 1:
            ret += combinations_count(val, 2)
    print(ret)
    d[n] += 1

Submission status: Wrong Answer

Solution code:

n=int(input())
 
A=list(map(int,input().split()))
 
X=[0]*(n+1)
 
for i in range(n):
    X[A[i]]+=1
 
ans=0
 
for i in range(n):
    if X[i]>1:
        ans+=(X[i]*(X[i]-1))//2      
for i in range(n):
    if X[A[i]]==1:
        print(ans)
    elif X[A[i]]>1:
        print(ans-(X[A[i]]-1))

Submission status: Wrong Answer

Solution code:
def combination(n,r,mod=10**9+7):
  n1,r = n+1, min(r,n-r)
  numer = denom = 1
  for i in range(1, r+1):
    numer = (numer*(n1-i)) % mod
    denom = (denom*i) % mod
  return numer * pow(denom, mod-2, mod) % mod

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

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

total = 0
for i in d:
  if d[i] > 1:
    total += combination(d[i],2)

for i in a:
  ans = total - (d[i]-1)
  print(ans)

Submission status: Wrong Answer

Solution code:
import math

num = int(input())
a_i = list(map(int, input().split()))
anslist = [0] * (num + 1)
for x in range(num):
    val = x + 1
    cnt = a_i.count(val)
    if cnt <= 1: anslist[val] = 0
    else: anslist[val] = math.factorial(cnt - 1)

ans = sum(anslist)
for x in range(num):
    val = a_i[x]
    cnt = a_i.count(val)
    if cnt <= 2: print(ans)
    else: 
        sa = anslist[val] - math.factorial(cnt - 2)
        print(ans - sa)

Submission status: Wrong Answer

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


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


for i in range(n):
    bb = balls[b[i]]
    print(su - bb+1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from copy import deepcopy

n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
all_comb = 0
for v in c.values():
    all_comb += v*(v-1)/2

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

Submission status: Wrong Answer

Solution code:
def main():
    N=int(input())
    L=[0]*(N+1)
    A=[int(_) for _ in input().split()]
    P=[0]*(N+1)
    ans=[0]*(N+1)
    
    for i in A:
        L[i]+=1
    
    a=set(A)
    
    for i in a:
        P[i]=L[i]*(L[i]-1)//2
    
    sums=sum(P)
    
    for i in a:
        if L[i]>=2: ans[i]=sums-P[i]+(L[i]-1)*(L[i]-2)//2
    
    for i in A:
        print(ans[i])

    
if __name__ == '__main__':
    main()