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: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000000000)

from operator import mul
from functools import reduce
from functools import lru_cache

N = int(input())
A_list = [int(e) for e in input().split()]

#ある番号のボールが何個あるかをまとめる
number_dict = dict()

for i in range(N):
    ball_number = A_list[i]
    if ball_number not in number_dict:
        number_dict[ball_number] = 0
        
    number_dict[ball_number] += 1

#それぞれの番号のボールから2個選ぶ組み合わせをカウントする。
comb_dict = dict()

@lru_cache(maxsize=10000000000000)
def combinations_count(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
    
@lru_cache(maxsize=10000000000000)
def two_choice(N):
    if N == 2:
        return 1
    elif N <= 1:
        return 0
    elif N == 3:
        return 3
    else:
        return combinations_count(N,2)*combinations_count(N-2,2)

all = 0

for in_dict in number_dict.items():
    ball_number = in_dict[0]
    balls = in_dict[1]
    comb_dict[ball_number] = two_choice(balls)
    all += comb_dict[ball_number]

#回答作成
for i in range(1,N+1):
    ball_number = A_list[i-1]
    now_balls = number_dict[ball_number] - 1
    print(int(all-comb_dict[ball_number]+two_choice(now_balls)))

Submission status: Runtime Error

Solution code:
import math

def combinations_count(n, r):
    if r>n:
        return 0
    if n==0 or n==1:
        return 0
    if n==r:
        return 1
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

num_list = [0] * n
com_list1 = [0] * n
com_list2 = [0] * n

for i in range(n):
    num_list[a[i]] += 1

for i in range(n):
    num = num_list[i]
    if num>=2:
        com_list1[i] = combinations_count(num, 2)

for i in range(n):
    num = num_list[i]-1
    if num>=2:
        com_list2[i] = combinations_count(num, 2)


total = sum(com_list1)

for i in range(n):
    num = a[i]
    print(total - com_list1[num] + com_list2[num])

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop
from scipy.special import comb

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def cmb(n, r):
    if n < 2: 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 main():
    N = INT()
    A = LIST()

    c = Counter(A)
    l = [0 for _ in range(2 * (10**5) + 7)]
    h = [0 for _ in range(2 * (10**5) + 7)]

    for k, v in c.items():
        l[k] = cmb(v-1, 2)
        h[k] = cmb(v, 2)

    for a in A:
        ans = sum(h) - h[a] + l[a]
        print(ans) 


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
import collections

def combinations_count(n, r):
	if n < 2:
		return 0
	return int(math.factorial(n) / math.factorial(r) /  math.factorial(n-r))

_ = int(input())
ns = input().split(" ")
cs = collections.Counter(ns)

c = 0
for n in ns:
	for k, v in cs.items():
		if v==1:
			c += 0
		elif n==k:
			c += combinations_count(v-1, 2)
		else:
			c += combinations_count(v, 2)
	print(c)
	c = 0

Submission status: Runtime Error

Solution code:
import heapdict

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: Runtime Error

Solution code:
from math import factorial

n = int(input())
a = list(map(int,input().split()))
s = []
total = 0

u = list(set(a))

for i in u:
    if a.count(i) >= 2:
        sub = int(factorial(a.count(i)) / factorial(2) / factorial(a.count(i) - 2))
        total = sub + total
        s.append([i,a.count(i),sub])

for i in a:
    res = 0
    if s != []:
        for j in range(len(s)):
            if s[j][0] == i:
                b = s[j][1] - 1
                if b >= 2:
                    res = total - s[j][2] + int(factorial(b) / factorial(2) / factorial(b - 2))
                else:
                    res = total - s[j][2]
                break
    print(res)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import math
import numpy as np
import collections
from scipy.special import comb
n = int(input()) 
a = np.array(list(map(int, input().split())))
ans = {}
for i in range(n):   
    if a[i] in ans.keys():
        print(ans[a[i]])
        continue
    ans[a[i]] = 0
    ind = np.ones(n, dtype=bool)
    ind[i] = False
    c = collections.Counter(a[ind])
    for k in c.keys():
        if c[k] >= 2:
            ans[a[i]] += comb(c[k], 2, exact=True)
    print(ans[a[i]])

Submission status: Runtime Error

Solution code:
import collections

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

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

for i in range(len(x)):
    sum = 0
    t = x.pop(0)
    c = collections.Counter(x)
    for k in list(set(x)):#count:
        if c[k] != 0 and c[k] != 1:
            sum += combinations_count(c[k],2)
    print(sum)
    
    x.append(t)

Submission status: Runtime Error

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


nCr = {}


def comb(n, r):
    if r == 0 or r == n:
        return 1
    if r == 1:
        return n
    if (n, r) in nCr:
        return nCr[(n, r)]
    if n < 3:
        return 0
    nCr[(n, r)] = comb(n-1, r) + comb(n-1, r-1)
    return nCr[(n, r)]


memo = {}
for i in range(n):
    a_list = nums[:i] + nums[i+1:]
    cnt = 0
    for j in set(a_list):
        u_num = a_list.count(j)
        if u_num in memo:
            cnt += memo[u_num]
        else:
            calc = comb(u_num, 2)
            cnt += calc
            memo[u_num] = calc

    print(cnt)

Submission status: Runtime Error

Solution code:
from collections import Counter


from operator import mul
from functools import reduce
import sys
sys.setrecursionlimit(300000)


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


cmb = []
for i in range(0, 200001):
    cmb.append(cmbf(i, 2))

N = int(input())
A = list(map(int, input().split()))
c = Counter()
for i in A:
    c.update(str(i))
cmbL = {}
gsum = 0

for i in c.keys():
    tmp1 = cmb[int(c[i])]
    tmp2 = tmp1 - (tmp1 * (1 - 2/c[i]))
    cmbL[i] = [tmp1, tmp2]
    gsum += tmp1

for i in A:
    print(int(gsum - cmbL[str(i)][1]))

Submission status: Runtime Error

Solution code:
N = int(input())
import sys
sys.setrecursionlimit(100000)
nCr = {}
def cmb(n, r):
    if n >= 2:
        if r == 0 or r == n: return 1
        if r == 1: return n
        if (n, r) in nCr: return nCr[(n, r)]
        nCr[(n, r)] = cmb(n-1, r) + cmb(n-1, r-1)
        return nCr[(n, r)]
    else:
        return 0

A = input().split()
len(set(A))
B = {}
C = {}
for i in set(A):
    B[i] = cmb(A.count(i) - 1, 2)
    C[i] = cmb(A.count(i), 2)
sC = sum(C.values())
for i in A:
    print(B[i] - C[i] + sC)

Submission status: Runtime Error

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

m = [0] * N
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: Runtime Error

Solution code:
from collections import Counter

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

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


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

for k in range(N):
  sum_k = 0
  temp_c = c.copy()
  temp_c[A[k]] -= 1
  for v in temp_c.values():
    if v > 1:
      sum_k += cmb(v,2)
  print(int(sum_k))

Submission status: Runtime Error

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

num_list = [0] * max(A)
for i in range(max(A)):
  num_list[i] = A.count(i+1)
  
max_A = max(num_list)
list_comb = [0] * (max_A+1)
list_comb[2], list_comb[3] = 2, 3
for i in range(4, len(list_comb)):
  list_comb[i] = list_comb[i-2] * 2

#print(num_list)
#print(list_comb)
  
list_ans = [-1] * max(A)
for i in range(N):
  if list_ans[A[i]-1] > 0:
    print(list_ans[A[i]-1])
  else:
    ans = 0
    for j in range(max(A)):
      if i == j:
        ans += list_comb[num_list[j]-1]
      else:
        ans += list_comb[num_list[j]]
    list_ans[i] = ans
    print(ans)

Submission status: Runtime Error

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

B=sorted(set(A))
sa = []
gokei = 0
for i in range(len(B)):
    x = A.count(B[i])
    if x == 1:
        continue
    elif x ==2:
        gokei += 1
        sa.append(1)
    else:
        z = math.factorial(x)//(math.factorial(x-2)*math.factorial(2))
        gokei += z
        sa.append(z-z*(x-2)//x)

ans = [0]*(N+1)
for i in range(N):
    z = A[i]
    if ans[z] == 0:
        e = gokei - sa[B.index(z)]
        ans.append(e)
        print(e)
    else:
        print(ans[z])

Submission status: Runtime Error

Solution code:
import collections
from math import factorial

def comb(n,r):
	_a = factorial(n) / factorial(r) / factorial(n - r)
	return int(_a)


N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
#
for a in A:
	c[a]-=1
	#print(c)
	sum=0
	for b in set(c):
		if c[b]>=2:
			sum+=comb(c[b],2)
	print(sum)
	c[a]+=1

Submission status: Runtime Error

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

for i in A[:]:
    A.remove(i)
    ans = 0
    for s in range(N -1):
        M = A.count(A[s])
        print(M // 2)

Submission status: Runtime Error

Solution code:
import copy

N=int(input())

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

#print(A)

mama=dict()
papa=dict()
papa[0]=0
papa[1]=0
papa[2]=1
for i in range(3,int(N/2)):
    papa[i]=int(i*papa[i-1]/2)
for i in range(N):

    if(A[i] in mama):
        print(mama[A[i]])
        continue
    
    AA=copy.deepcopy(A)
    ai=A[i]
    del AA[i]
    #print(AA)
    Aset=set(AA)
    count=0
    
    for i in Aset:
        aa=AA.count(i)
        if(aa in papa):
            count+=papa[aa]
            continue
        #print(i,aa)
        aiu=int((aa*(aa-1))/2)
        count+=aiu
        papa[aa]=aiu
    print(count)
    mama[ai]=count

Submission status: Runtime Error

Solution code:
from itertools import combinations
import sys
sys.setrecursionlimit(100000)

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


nCr = {}


def comb(n, r):
    if r == 0 or r == n:
        return 1
    if r == 1:
        return n
    if (n, r) in nCr:
        return nCr[(n, r)]
    if n < 3:
        return 0
    nCr[(n, r)] = comb(n-1, r) + comb(n-1, r-1)
    return nCr[(n, r)]


memo = {}
for i in range(n):
    a_list = nums[:i] + nums[i+1:]
    cnt = 0
    for j in set(a_list):
        u_num = a_list.count(j)
        if u_num in memo:
            cnt += memo[u_num]
        else:
            calc = comb(u_num, 2)
            cnt += calc
            memo[u_num] = calc

    print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np 
import collections

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

for i in range(n):
    _a = np.delete(a, i)
    c_dict = collections.Counter(_a)
    total = 0
    for v in c_dict.values():
        if v >= 2:
            total += v * (v - 1) / 2
            
    flag[i] = 1
    print(int(total))

Submission status: Runtime Error

Solution code:
import collections
import copy
import math
from scipy.special import comb

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

n = int(input())
nums = [ int(s) for s in input().split() ]


for i in range(n):
    ans = 0

    cpl = copy.copy(nums)
    cpl.pop(i)
    ct = collections.Counter(cpl)

    for key, val in ct.items():
        if val > 1:
            ans += comb(val, 2, exact=True)
    
    print(ans)

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}
#print(d)
all_sum = sum(d.values())
#print(all_sum)

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
        sum_d[a[k]] = all_sum  - d[a[k]] + tmp
        print(sum_d[a[k]])
    else:
        print(sum_d[a[k]])

Submission status: Runtime Error

Solution code:
import copy

N=int(input())

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

#print(A)

mama=dict()
papa=dict()
papa[0]=0
papa[1]=0
papa[2]=1
for i in range(3,N/2):
    papa[i]=int(i*papa[i-1]/2)
for i in range(N):

    if(A[i] in mama):
        print(mama[A[i]])
        continue
    
    AA=copy.deepcopy(A)
    ai=A[i]
    del AA[i]
    #print(AA)
    Aset=set(AA)
    count=0
    
    for i in Aset:
        aa=AA.count(i)
        if(aa in papa):
            count+=papa[aa]
            continue
        #print(i,aa)
        aiu=int((aa*(aa-1))/2)
        count+=aiu
        papa[aa]=aiu
    print(count)
    mama[ai]=count

Submission status: Runtime Error

Solution code:
import sys
import copy
from math import factorial
input = sys.stdin.readline

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

for i in range(len(a)):
    ans = 0

    # 1つ１つ削除したときのリストを作る
    adel = copy.deepcopy(a)
    del adel[i]

    # setにして、重複を削除しどんな要素があるかを確認
    adelset = list(set(adel))
    # print(adel)
    # print(adelset)

    for j in range(len(adelset)):
        cnt = adel.count(adelset[j])
        # print("{}は{}個".format(j,cnt))
        if cnt > 2:
            ans += factorial(cnt) / factorial(2) / factorial(cnt - 2)
        elif cnt == 2:
            ans += 1
        elif cnt <= 1:
            ans += 0
    print(int(ans))

Submission status: Runtime Error

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

def cmb(n,r):
    if r > n:
        return 0
    if r == 0 or r == n:
        return 1
    if r == 1:
        return n
    return cmb(n-1,r) + cmb(n-1,r-1)

mem_cnt = {}
set_a = set(A)
for c in list(set_a):
    mem_cnt[str(c)] = A.count(c)

for k in A:
    ret = 0
    for key,v in mem_cnt.items():
        x = v
        if str(key) == str(k):
            x = v-1
        ret += cmb(x,2)
    print(ret)

Submission status: Runtime Error

Solution code:
import collections
from math import factorial

def comb(n,r):
	_a = factorial(n) / factorial(r) / factorial(n - r)
	return int(_a)


N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
#
for a in A:
	c[a]-=1
	#print(c)
	sum=0
	for b in set(c):
		if c[b]>=2:
			sum+=comb(c[b],2)
	print(sum)
	c[a]+=1

Submission status: Runtime Error

Solution code:
import collections

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

# N = 5
# A = [1, 1, 2, 1, 2]

c = collections.Counter(A)
d = {}
for i in c:
    # val = uni_vals[0]
    ans = 0
    for j in c:
        n = c[j]
        if j == A[i]:
            n -= 1
        ans += (n*(n-1))/2
    d[i] = ans

for i in range(N):
    print(int(d[A[i]]))

Submission status: Runtime Error

Solution code:
import collections
import copy
import math
import scipy.misc import comb

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

n = int(input())
nums = [ int(s) for s in input().split() ]


for i in range(n):
    ans = 0

    cpl = copy.copy(nums)
    cpl.pop(i)
    ct = collections.Counter(cpl)

    for key, val in ct.items():
        if val > 1:
            ans += comb(val, 2, exact=True)
    
    print(ans)

Submission status: Runtime Error

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


cmb = []
for i in range(0, 200001):
    cmb.append(cmbf(i, 2))

N = int(input())
A = list(map(int, input().split()))
c = Counter()
for i in A:
    c.update(str(i))
cmbL = {}
gsum = 0

for i in c.keys():
    tmp1 = cmb[int(c[i])]
    tmp2 = tmp1 - (tmp1 * (1 - 2/c[i]))
    cmbL[i] = [tmp1, tmp2]
    gsum += tmp1

for i in A:
    print(int(gsum - cmbL[str(i)][1]))

Submission status: Runtime Error

Solution code:
n = int(input())
def make(k,l):
    return([k,l,0])
ls = list(map(int,input().split()))
for i in range(n):
    k = ls[i]
    ls[i] = make(k,i)

li = sorted(ls, key = lambda x: x[0])

nls = [1]
for i in range(1,n):
    if li[i-1][0] == li[i][0]:
        nls[-1] += 1
    else:
        nls.append(1)

als = []
bls = []
for i in range(len(nls)):
    als.append(int(nls[i]*(nls[i]-1)/2))
    bls.append(int((nls[i]-1)*(nls[i]-2)/2))

g = sum(als)

ansls = []
for i in range(len(nls)):
    ansls.append(g-als[i]+bls[i])

if len(nls) == 1:
    for i in range(n):
        li[i][2] = ansls[0]
else:
    t = 0
    for i in range(len(als)):
        for j in range(als[i]):
            li[t][2] = ansls[i]
            t += 1
li[-1][2] = ansls[-1]

lis = sorted(li, key = lambda x: x[1])

for i in range(n):
    print(lis[i][2])

Submission status: Runtime Error

Solution code:
input()
aa = list(map(int, input().split()))
uu = list(set(aa))
cc = uu.copy()
for i in range(len(uu)):
    cc[i] = aa.count(uu[i])
    total += cc[i] * (cc[i]-1) / 2

dic = dict(zip(uu,cc))

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

Submission status: Runtime Error

Solution code:
import collections
import copy
import math
import scipy.special import comb

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

n = int(input())
nums = [ int(s) for s in input().split() ]


for i in range(n):
    ans = 0

    cpl = copy.copy(nums)
    cpl.pop(i)
    ct = collections.Counter(cpl)

    for key, val in ct.items():
        if val > 1:
            ans += comb(val, 2, exact=True)
    
    print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
 
a=list(map(int, input().split()))
 
import math
import numpy as np
 
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
base = 0
for i in range(len(a)):
    base = 0
    a_del = np.delete(a, int(i), 0)
    set_1 = list(set(a_del.tolist()))
    for j in range(len(set_1)):
        cnt = set_.count(set_1[j])
        if 2 <= cnt:
            comb_cnt = comb(cnt,2)
            base = base + comb_cnt
    print(base)

Submission status: Runtime Error

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

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )


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

S = Counter(A)

for i in A:
  tmp = []
  S[i] -= 1
  for j in S.keys():
    tmp.append(cmb(S[j],2,mod))
  S[i] += 1
  print(sum(tmp))

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
A = input().split()
c = Counter(A)
a = list(c.values())
b = len(a)
AA = A
for i in range(N):
    r = 0
    x = AA.pop(i)
    c = Counter(AA)
    a = list(c.values())
    b = len(a)
    for j in range(b):
        r += (a[j]*(a[j]-1))/2

Submission status: Runtime Error

Solution code:
import math

def combinations_count(n):
    return math.factorial(n) // (math.factorial(n - 2) * 2)
  
N = int(input())
A = list(map(int, input().split()))

num_list = [0] * max(A)
for i in range(max(A)):
  num_list[i] = A.count(i+1)
  
max_A = max(num_list)
list_comb = [0] * (max_A+1)
for i in range(2, len(list_comb)):
  list_comb[i] = combinations_count(i)

#print(num_list)
#print(list_comb)
  
list_ans = [-1] * max(A)
for i in range(N):
  if list_ans[A[i]-1] > 0:
    print(list_ans[A[i]-1])
  else:
    ans = 0
    for j in range(max(A)):
      if i == j:
        ans += list_comb[num_list[j]-1]
      else:
        ans += list_comb[num_list[j]]
    list_ans[i] = ans
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
# sys.stdin = open('input.txt')

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]


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

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

total = 0

for i in cnt.values():
    if i >= 2:
        total += cmb(i, 2)

ansDic = {}

for i in range(n):
    if a[i] in ansDic.keys():
        print(ansDic[a[i]])
    else:
        x = cnt[a[i]]
        ans = total
        if x >= 2:
            ans = total - cmb(x, 2)
        if x-1 >= 2:
            ans += cmb(x-1, 2)
        ansDic[a[i]] = ans
        print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

cnt = Counter(O)
cnt = dict(cnt)

for k in cnt:
    if cnt[k] == 1:
        del cnt[k]

for i in O:
    cnt[i] = cnt.get(i, 1) - 1
    res = 0

    for k in cnt:
        if cnt[k] >=2:
            res += (cnt[k]*(cnt[k]-1))/2
    print(res)

    cnt[i] += 1

Submission status: Runtime Error

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

import math

def product(iterable):
    prod = 1
    for n in iterable:
        prod *= n
    return prod

def npr(n, r):
    return product(range(n - r + 1, n + 1))

def ncr(n,r):
    if r > n // 2:
        r = n - r
    return npr(n, r) // math.factorial(r)

a = list(set(A))
a_n = []
c = []
new = []
if len(a) != N:
  for i in range(len(a)):
    x = A.count(a[i])
    a_n.append(x)
    c.append(ncr(x,2))
    if x > 2: new.append(ncr(x-1,2))
    else: new.append(0)

  ans = sum(c)
  for i in range(N):
    n = a.index(A[i])
    print(ans-c[n]+new[n])
else:
  for _ in range(N):
    print(0)

Submission status: Runtime Error

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


for j in range(n):
    print((nsum+1-b[a[j]]))

Submission status: Runtime Error

Solution code:
import numpy as np
from math import factorial

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

dic = {}
newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	dic[newdata[i]] = x

for i in range(N):
	dic[A[i]] -= 1
	ans = 0
	for num in dic.values():
		if num <= 1:
			pass
		else:
			ans += factorial(num) / factorial(2) / factorial(num- 2)
	print(int(ans))
	dic[A[i]] += 1

Submission status: Runtime Error

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  a=list(map(int,input().split()))
  dic={}
  for i in a:
    if i in dic:
      dic[i]+=1
    else:
      dic[i]=1
  for i in list(dic):
    if dic[i]==1:
      dic.pop(i)
  sum=0
  if not dic:
    sum=0
  else:
    for j in dic:
      k=dic[j]
      if k>1:
        sum+=k*(k-1)//2
  for i in range(n):
    ans=0
    if not dic:
      ans=0
    else:
      k=dic[a[i]]
      l=k-1
      if k>1:
        ans=sum-k*(k-1)//2+l*(l-1)//2
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
#from collections import Counter

N = int(input())
A_list = [int(_) for _ in input().split()]

ans = 0
for i in set(A_list):
    v = A_list.cont(i)
    if v < 2:
        break
    ans += v*(v-1)/2

for k in range(N):
    tgt = A_list[k]
    n = A_list.count(tgt)
    print(int(ans - n + 1))

Submission status: Runtime Error

Solution code:
from math import factorial

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

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

for k, val in enumerate(A):
    count = 0
    nums = [0] * N
    for i, v in enumerate(A):
        if k == i:
            continue
        nums[v] += 1
    combs = list(map(lambda n: int(comb(n, 2)), filter(lambda n: n > 1, nums)))
    print(sum(combs))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(50000)
n = int(input())
List=[int(l) for l in input().split()]
a = [0]*20001
for j in range(n):
    a[List[j]] += 1
c = 0
for i in range(20001):
    c = c+(a[i]*(a[i]-1))//2
for k in range(n):
    print(c-a[List[k]]+1)

Submission status: Runtime Error

Solution code:
number=[]
result=[]
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
  number.append(0)
  result.append(0)
i=0
for i in A:
    if i in range(N):
      number[i]+=1
    else:
      number[i]=1
i=0
for i in range(N):
  cnt=0
  kie=A.pop(i)
  number[kie]-=1
  if result[kie]==0:
    for j in range(N-1):
      if number[j]>=1:
        cnt+=int((number[j]*(number[j]-1))/2)
    result[kie]=cnt
  A.insert(i,kie)
  number[kie]+=1
i=0
for i in range(N):
  print(result[A[i]])

Submission status: Runtime Error

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

import math
def combinations_count(n, r):
    ret = 0
    if n >= r :
        ret = math.factorial(n) // (math.factorial(n - r) *2)
    return ret

dupl = [x for x in set(A) if A.count(x) > 1]
for i in range(N):
    ans = 0
    l = A[:]
    l.pop(i) 
    for j in dupl:
        p = combinations_count(l.count(j),2) 
        if p > 0:
            ans = ans + p
    print(ans)

Submission status: Runtime Error

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  a=list(map(int,input().split()))
  dic={}
  for i in a:
    if i in dic:
      dic[i]+=1
    else:
      dic[i]=1
  for i in list(dic):
    if dic[i]==1:
      dic.pop(i)
  sum=0
  for j in dic:
    k=dic[j]
    if k>1:
      sum+=k*(k-1)//2
  for i in range(n):
    ans=0
    if not dic:
      ans=0
    else:
      k=dic[a[i]]
      l=k-1
      if k>1:
        ans=sum-k*(k-1)//2+l*(l-1)//2
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:

import collections

n = int(input())
a = list(map(int, input().split()))
c_lst = []
c = collections.Counter(a)
for i in c.most_common()[::-1]:
    if i[1] >= 2:
        c_lst.append(list(i))
if c_lst:
    values, counts = zip(*c_lst)
else:
    values = []

cnt = []
for i in range(len(c_lst)):
    cnt.append((c_lst[i][1] * (c_lst[i][1] - 1)) // 2)
cnt_ = sum(cnt)
    
for i in a:
    ans = 0
    if i in values:
        ans += ((c_lst[values.index(i)][1] - 1) * (c_lst[values.index(i)][1] - 2)) // 2
        ans += cnt_ - cnt[values.index(i)]
    else:
        ans = sum(cnt_)
    print(ans)

Submission status: Runtime Error

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

from collections import deque

As = deque()

N = int(input().rstrip())
As.extend([int(x) for x in input().rstrip().split()])
d = {}  # count each num
for n in As:
    if n not in d.keys():
        d[n] = 1
    else:
        d[n] += 1
# print(f'd: {d}')

calc_combi_count = {}
calc_combi_count[1] = 0
total = 0

for n in d.values():
    if n > 1:
        if n not in calc_combi_count.keys():
            calc_combi_count[n] = combinations_count(n, 2)
        total += calc_combi_count[n]
        if n > 2 and n-1 not in calc_combi_count.keys():
            calc_combi_count[n-1] = combinations_count(n-1, 2)

# print(total)
# print(calc_combi_count)
for num in As:
    if calc_combi_count != {1: 0}:
        ans = total - calc_combi_count[d[num]] + calc_combi_count[d[num]-1]
    else:
        ans = 0
    print(ans)

Submission status: Runtime Error

Solution code:
import math
N = int(input())

nMax = 2 * 10 ** 5
lmemo = [-1] * (nMax + 1)
def fperm(a, b):
  if a < b:
    return 0
  nump = 1
  for i in range(b, a + 1):
    nump *= i
  return nump

def fcomb(a, b):
  if a < b:
    return 0
  if lmemo[a] != -1:
    return lmemo[a]
  else:
    lmemo[a] = fperm(a, b) // math.factorial(b)
    return lmemo[a]
  
l = {}
ldef = {}

ak = input().split()
for num in ak:
  if num in l:
    l[num] += 1
  else:
    l[num] = 1
ctotal = 0
for i in l:
  if l[i] >= 2:
    com = fcomb(l[i], 2)
    ctotal += com
    ldef[i] = fcomb(l[i] - 1, 2) - com
for num in ak:
  total = ctotal + ldef[num]
  print(total)