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

def reduce_tmp(tmp):
  if tmp % 2 == 0:
    return (1 + tmp) * (tmp / 2)
  else:
    return (1 + (tmp-1)) * ((tmp-1) / 2) + tmp

def tmp2(tmplist):
  return sum([reduce_tmp(len(tmplist.count(ai))) for ai in set(tmplist)])

i = 0

[print(tmp2(a[0:k] + a[k+1:])) for k in range(n)]

Submission status: Runtime Error

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


for i in range(len(a)):
    rem_k = a[:i] + a[i + 1:]
    # print(len(set(rem_k))) # 同一が2個以上のものを抽出してset
    cho = []
    for j in rem_k:
        if rem_k.count(j) >= 2:
            cho.append(j)
    # print(rem_k)
    for i in cho:
        if rem_k.count(i) - 2 < 0:
            ans = 0
        else:
            ans =  math.factorial(rem_k.count(i)) // (math.factorial(rem_k.count(i) - 2) * math.factorial(2))

    print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=[input() for i in range(N)]
A=int(A)
if (3<=N<=2*10**5)and(1<=A<=N):
    i=0
    for i in range(0,N):
        B=A[0:i]+A[i+1:]
        j=0
        k=0
        for j in range(0,N-2):
            l=j+1
            for l in range(j+1,N-1):
                if B[j]==B[l]:
                    k+=1
                l+=1
            j+=1
        print(k)
        i+=1

Submission status: Runtime Error

Solution code:
from math import factorial

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

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

nums = [0] * N
for i, v in enumerate(A):
    nums[v] += 1
combs = list(map(lambda n: int(comb(n, 2)), nums))
total = sum(combs)

for k, val in enumerate(A):
    count = comb(nums[val] - 1, 2)
    print(total - combs[val] + count)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input().split()))
s = list(set(l))
all_sum = 0
count = {}
for i in range(len(s)):
    v = l.count(s[i])
    if v > 1:
        all_sum += v * (v - 1) // 2
        count[s[i]] = v
    else:
        count[l[i]] = 1
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: Runtime Error

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

for i in range(N):
    B = A
    del B[i]
    nums = {}
    for j in B:
        if not j in nums:
            nums[j] = 1
        else:
            nums[j] += 1
    count = 0
    for j in nums.values():
        count += j * (j - 1) // 2
    print(count)

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)
for i in range(2, len(list_comb)):
  list_comb[i] = i*(i-1) // 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 A[i]-1 == 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 itertools
import copy
N = int(input())
A = list(map(int, input().split()))
A_list = []
for i in range(N):
    A_copy = copy.copy(A)
    A_list.append(A_copy)

result = []
count = 0
for k in range(N):
    A_list[k].pop(A_list[k][k])
    count = 0
    comb = itertools.combinations(A_list[k], 2)
    for i in comb:
        if k == N:
            print(i)
        if i[0] == i[1]:
            count += 1
    result.append(count)
for i in result:
    print(i)

Submission status: Runtime Error

Solution code:
import math
import sys

import numpy as np
from scipy.misc import factorial


scan = sys.stdin.readline


def main():
    n = int(scan().rstrip())  # type: int
    a = [int(_) for _ in scan().rstrip().split()]

    bucket = np.zeros(n)
    for ball in a:
        bucket[ball] += 1

    for ball in a:
        bucket[ball] -= 1
        candidates = bucket[bucket > 1]
        combs = factorial(candidates) // (factorial(candidates - 2) * 2)
        print(int(np.sum(combs)))
        bucket[ball] += 1


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from scipy.special import comb

def resolve():
    n = int(input())
    a = [int(x) for x in input().split()]

    for k in range(n):
        # 初期化
        ak = a[:] # deep copy
        del ak[k]
        kumi = 0
        ended = set()

        # loop
        for a0 in ak:
            if a0 in ended:
                continue
            cnt = ak.count(a0)
            if cnt >= 2:
                kumi += int(comb(cnt, 2, exact=False))
            ended.add(a0)
        print(kumi)

resolve()

Submission status: Runtime Error

Solution code:
from scipy.misc import comb
from collections import defaultdict
from collections import deque
d = defaultdict(lambda: -1)
d_ = defaultdict(lambda: -1)
n = int(input())
a = deque([int(x) for x in input().split()])

for i in a:
  a_ = a.copy()
  b = a_.remove(i)
  if d[str(b)] != -1:
    print(d[str(b)])
    continue
  A = list(set(a_))
  ans = 0
  for j in A:
    c = a_.count(j)
    if d_[str(c)] != -1:
      ans += d_[str(c)]
    else:
      e = comb(c, 2, exact=True)
      ans += e
      d_[str(c)] = e

  d[str(b)] = ans
  print(ans)

Submission status: Runtime Error

Solution code:
from math import factorial

n = int(input())
a = list(map(int,input().split()))
a_s = set(a)
for i in range(n):
    l = a[i]
    a[i] = 'X'
    ans = 0
    for j in a_s:
        c = a.count(j)
        if c < 2:
            continue
        ans += factorial(c) / factorial(2) / factorial(c - 2)
    print(int(ans))
    a[i] = l

Submission status: Runtime Error

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

for i in range(N):
    flag = 0
    for j in range(len(T)):
        if A[i] == T[j][0]:
            T[j][1] += 1
            flag = 1
            break
    if flag == 0:
        T.append([A[i],1])

T2 = []

for i in range(len(T)):
    T2.append(T[i][1])


def hoge(List):
    ans = 0
    for i in range(len(List)):
        ans += (List[i]-1)*List[i]//2
    return ans

T3 = []
A = hoge(T2)

print(T)

for i in range(len(T)):
    T3.append(A-((T2[i]-1)*(T2[i]-1)//2))

for i in range(N):
    for j in range(len(T)):
        if T[j][0] == A[i]:
            print(T3[j])
            break

Submission status: Runtime Error

Solution code:
import math

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

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

def combinations_count_fast(n):
    return math.factorial(n) // (math.factorial(n - 2) * 2)


def solve(N, S):
    
    hoge = list(set(S))
    hoge_count = len(hoge)
    ct = 0
    if hoge_count == N:
        while ct < N:
            print(0)
            ct+=1
    elif hoge_count == 1:
        out = N+1
        while ct < N:
            print(out)
            ct+=1
    
    else:
        c = Counter(S)
        sums = 0
        for i in c.values():
            sums += i

        for i in range(N):
            ind = c[S[i]]
            if ind >= 3:
                cal = sums - combinations_count_fast(ind) + combinations_count_fast(ind-1)
            else:
                cal = sums - combinations_count_fast(ind)
            print(cal)
solve(N, S)

Submission status: Runtime Error

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 i in c.keys():
    target = c.copy()
    target[i] -= 1
    if target[i] == 0:
        dict_ans[i] = full_ans
    else:
        dict_ans[i] = sum(list(map(comb, target.values())))

for i in A:
    print(dict_ans[i])

Submission status: Runtime Error

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

def checkPatternNum(inA):
    totalCount = 0
    while(len(inA) > 0):
        targetData = inA[0]
        if(not targetData in countDict):
            targetCount = inA.count(targetData)
            countDict[targetData] = targetCount
            if(2 <= targetCount):
                for n in range(targetCount+1):
                    if(n > 1):
                        totalCount += (n-1)
    return totalCount

for index in range(N):
    targetA = A.copy()
    del targetA[index]
    print(checkPatternNum(targetA))

Submission status: Runtime Error

Solution code:
import collections
from math import factorial
from scipy.special import comb
# a = comb(n, r)
a = comb(n, r, exact=True)

def comb(n,r):
	#_a = factorial(n) / factorial(r) / factorial(n - r)
	a = comb(n, r, exact=True)
	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)
	c[a]+=1
	print(sum)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main() {

    int N; cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    vector<int> uniqueA = A;
    sort(uniqueA.begin(), uniqueA.end());
    uniqueA.erase(unique(uniqueA.begin(), uniqueA.end()), uniqueA.end());

    int sum_comb_all = 0;
    for (int i = 0; i < uniqueA.size(); i++) {
        int num_A = count(A.begin(), A.end(), uniqueA[i]);
        int comb  = (num_A * (num_A-1)) / 2;
        sum_comb_all += comb;
    }


    for (int i = 0; i < N; i++) {

        int num_A     = count(A.begin(), A.end(), A[i]);
        int prev_comb = (num_A * (num_A-1)) / 2;
        int new_comb  = ((num_A-1) * (num_A-2)) / 2;

        int sum_comb  = sum_comb_all - prev_comb + new_comb;
        cout << sum_comb << endl;

    }

}

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
import sys
from operator import mul
from functools import reduce

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

n = int(input())
l = [int(x) for x in input().split()]

# cl = [0 for i in range(n)]
cl = []
ll = [x for x in set(l) if l.count(x) > 1]

# print(ll)

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (m - 2) / m))

# print(sum)
# print(cl)
for i in l:
    # print(sum - cl[i-1])
    print(sum - cl[ll.index(i)])

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)
for i in range(2, len(list_comb)):
  list_comb[i] = i*(i-1) // 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 A[i]-1 == 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:
N = int(input())
A = list(map(int, input().split()))
cnt = [0 for _ in range(N)]
for i in range(N):
    cnt[A[i]-1] += 1
S = 0
for i in range(N):
    S += cnt[i]*(cnt[i]-1)/2
for i in range(N):
    if cnt[A[i]] == 1:
        print(S)
    else:
        print(int(S+(3-2*cnt[A[i]-1])/2))

Submission status: Runtime Error

Solution code:
import math
import collections
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


def combinations_count(n, r):
	if n < 2:
		return 0
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(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)
			c += cmb(v-1, 2)
		else:
#			c += combinations_count(v, 2)
			c += cmb(v, 2)
	print(c)
	c = 0

Submission status: Runtime Error

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

n = int(input())
a = list(map(int, input().split()))
ab = sorted(a)
memo = collections.Counter(ab)
memo_key = list(memo.keys())
memo_v = list(memo.values())
len_v = len(memo_v)
max = max(memo_v)
cnt = [0] * max
cnt_sum = 0
ans_memo = [0] * len_v
for i in range(1, max):
    cnt[i] = cnt[i - 1] + i
for i in range(len_v):
    cnt_sum += cnt[memo_v[i] - 1]
for i in range(len_v):
    ans_memo[i] = cnt[memo_v[i] - 2] + cnt_sum - cnt[memo_v[i] - 1]
for i in range(n):
    ans = ans_memo[bisect.bisect_left(memo_key, a[i])]
    print(ans)

Submission status: Runtime Error

Solution code:


def main():
    import collections

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

    c = collections.Counter(A)
    iglist = [x for x in c if c[x] == 1]

    for x in iglist:
        A.remove(x)

    for i in range(N):
        x = A[i]
        A[i] = 0
        tmp_A = collections.Counter(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: Runtime Error

Solution code:
#D
from collections import Counter
from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
def x(i):
    if i < 0:
        return 0
    if i == 1:
        return 1
    else:
        return x(i - 1) + i

for j in range(n):
    b=deepcopy(a)
    b.pop(j)
    # print(a,b)
    ans = 0
    # print(b)
    for i in range(1, max(b) + 1):
        # print(b[i])
        ans += x(b.count(i)-1)
    print(ans)

Submission status: Runtime Error

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

def cmb(n,r):
    if n == 1: 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

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

    ans = {}
    for i in range(len(alist)):
        if alist[i] in ans:
            if i == len(alist)-1:
                print(ans[alist[i]], end='')
            else:
                print(ans[alist[i]])
            continue

        count = 0
        for k, v in collections.Counter(alist).items():
            if k == alist[i]:
                count += cmb(v-1, 2)
            else:
                count += cmb(v, 2)

        if i == len(alist)-1:
            print(count, end='')
        else:
            print(count)
        ans[alist[i]] = count


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

super_list = [0,1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190,210,231,253,276,300,325,351,378,406,435,465,496,528,561,595,630,666,703,741,780,820,861,903,946,990,1035,1081,1128,1176,1225,1275,1326,1378,1431,1485,1540,1596,1653,1711,1770,1830,1891,1953,2016,2080,2145,2211,2278,2346,2415,2485,2556,2628,2701,2775,2850,2926,3003,3081,3160,3240,3321,3403,3486,3570,3655,3741,3828,3916,4005,4095,4186,4278,4371,4465,4560,4656,4753,4851,4950,5050]

def balls(t):
  if t > len(super_list):
    for i in range(len(super_list,t)):
      super_kist.append(super_list[-1]+len(super_list))
  else:
    return super_list[t]
  return super_list[t]

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

unique_A_list = list(set(A_list))
unique_A = np.c_[list(set(A_list))]
ones_minus = np.c_[-1 * np.ones((len(unique_A)), int)]

np_A = np.array(unique_A)
ans_dict = dict(np.concatenate([np_A, ones_minus], 1))
num_dict = dict(np.concatenate([np_A, ones_minus], 1))

for i in unique_A_list:
  num_dict[i] = sum(x == i for x in A_list)
  
for i in unique_A_list:
  num_dict[i] = num_dict[i] - 1
  hoge = 0
  for j in unique_A_list:
    hoge += balls(num_dict[j]-1)
  ans_dict[i] = hoge
  num_dict[i] = num_dict[i] + 1
  
for i in A_list:
  print(ans_dict[i])

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
nCr = {}
def cmb(n, r):
    if n == 0:
        return 0
    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)]
mydict = {}
for i in range(N):
    a = A[i]
    if a in mydict.keys():
        mydict[a] += 1
    else:
        mydict[a] = 1
for i in range(N):
    s = 0
    a = A[i]
    mydict[a] -= 1
    for j in mydict.values():
        s += cmb(j,2)
    print(s)
    mydict[a] += 1

Submission status: Runtime Error

Solution code:
import math

dic = {}

def combinations_count(n, r):
    if n in dic:
        return dic[n]
    else:
        if r>n:
            re = 0
        if n==0 or n==1:
            re = 0
        if n==r:
            re = 1
        re = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
        dic[n] = re
        return re


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:
%%time
import math
import numpy as np
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
num_list =  input().split(' ')
num_list2 = [int(n) for n in num_list]
num_list3 = [0]*n
num_list4 = [0]*n
num_list5 = [0]*n
for i in range(len(num_list2)):
    num_list3[num_list2[i]-1] += 1
for i in range(len(num_list2)):
    if num_list4[num_list2[i]-1] == 0:
        size = max(0 , num_list3[num_list2[i]-1])
        if size >= 2:
            ans = comb(size,2)
        elif size == 1:
            ans = 0
        else :
            ans = 0 
        num_list4[num_list2[i]-1] = ans

    if num_list5[num_list2[i]-1] == 0:
        size = max(0 , num_list3[num_list2[i]-1]-1)
        if size >= 2:
            ans = comb(size,2)
        elif size == 1:
            ans = 0
        else :
            ans = 0 
        num_list5[num_list2[i]-1] = ans
for i in range(n):
    print(num_list5[num_list2[i]-1]-num_list4[num_list2[i]-1]+np.sum(num_list4))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
checklist = list(set(A))
dic = {}
nCrdic = {}
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)]

for i in checklist:
    dic[i] = A.count(i)
for key,value in dic.items():
    nCrdic[key] = cmb(value,2) if value > 1 else 0
for i in A:
    if dic[i] > 2:
        ans = sum(nCrdic.values()) - nCrdic[i] + cmb(dic[i]-1,2)
    else: ans = sum(nCrdic.values()) - nCrdic[i]
    print(ans)

Submission status: Runtime Error

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

import numpy as np

base = 0
a_del = np.delete(a, int(i), 0)
set_ = a_del.tolist()
set_1 = list(set(set_))
for j in range(len(set_1)):
    cnt = set_.count(set_1[j])
    print(cnt)
    if 2 <= cnt:
        base = base + cnt*(cnt-1)/2
print(base)

Submission status: Runtime Error

Solution code:
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()))
ans=0
L=[0]*N
for i in range(N):
    L[A[i]-1]+=1
for i in range(len(C)):
    ans+=Comb(L[A[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: Runtime Error

Solution code:
import numpy as np

def comb2(n):
    return int(n*(n-1)/2)

n = int(input())

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

b = a+a.reshape(n,1)

e = np.ones(n, dtype=int) - np.identity(n, dtype=int)


c = b * e

for nums in c:
    sums = 0
    tmp, counts = np.unique(nums, return_counts=True)
    for num in counts:
        sums += comb2(num)
    print(sums)

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 - cmb[int(c[i])-1]
    cmbL[i] = [tmp1, tmp2]
    gsum += tmp1

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

Submission status: Runtime Error

Solution code:
import collections
import sys
import numpy as np
import copy
import bisect
from operator import mul
from functools import reduce
import itertools
import math
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

sys.setrecursionlimit(1000000000)
n = int(input())
a = list(map(int,input().split()))
A = collections.Counter(a)
li = []
number = set(a)
number = list(number)
counter = []
for val in A.values():
    counter.append(val)
counter2 = []
for i in range(len(counter)):
    counter2.append((counter[i]*(counter[i]-1))//2)

for i in range(n):
    s = bisect.bisect_left(number,a[i])
    counter[s]-=1
    counter2[s] = counter2[s] - counter[s]
    ans = sum(counter2)
    counter2[s] = counter2[s] + counter[s]
    counter[s]+=1
    print(ans)
"""
temp = li[:]
for i in range(n):
    temp = li[:]
    ans = 0
    for j in range(len(temp)):
        if a[i] == temp[j][0]:
            temp[j][1] -= 1
            break
    for j in range(len(temp)):
        ans += ((temp[j][1])*(temp[j][1]-1))//2
    for j in range(len(temp)):
        if a[i] == temp[j][0]:
            temp[j][1] += 1
            break
    print(ans)
allans = 
"""

Submission status: Runtime Error

Solution code:
from math import factorial
n = int(input())
l = list(map(int, input().split()))

dict = {}
for i in l:
    if i not in dict:
        dict[i] = 1
    else:
        dict[i] += 1



for j in l:
    ans = 0
    k = dict.copy()
    k[j] -= 1
    # print(k)
    for v in k.values():
        if v > 1:
            ans += int(factorial(v) / factorial(2) / factorial(v - 2))
    print(ans)

Submission status: Runtime Error

Solution code:
# def c2(x):
#     return x * (x - 1) // 2

n = int(input())
a = list(map(lambda i: int(i) - 1, input().split()))
b = list(a.count(i) for i in range(n))
c2 = list((x * (x - 1) // 2) for x in range(n))
# c = list(c2(b[i]) for i in range(n))
# d = list(c2(b[i] - 1) for i in range(n))
c = list(c2[i] for i in b)
d = list(c2[i - 1] for i in b)

s = sum(c)
# for k in range(n):
#     i = a[k]
#     ans = s - c[i] + d[i]
#     print(ans)
for i in a:
    print(s - c[i] + d[i])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict

mem = {}


def binomialCoeff(n, k):
    if n < k:
        return 0
    if k == 0 or k == n:
        return 1
    if n in mem.keys():
        return mem[n]

    # Recursive Call
    mem[n] = binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k)
    return mem[n]


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)
    xs_res = defaultdict(int)

    for n in xs:
        freq[n] += 1
        if freq[n] > 2:
            xs_res[n] *= 2
        if freq[n] > 1:
            xs_res[n] += 1

    res = 0

    for k in range(len(xs)):
        res = 0
        for num in freq.keys():
            res += binomialCoeff(freq[num] if num != xs[k] else freq[num] - 1, 2)
        print(res)

Submission status: Runtime Error

Solution code:
import copy
from collections import Counter
from math import factorial

def comb(n,r):
  a = factorial(n) / factorial(r) // factorial(n - r)
  return float(a)

N=int(input())
A=list(map(int,input().split() ))
B=[-1]*N   #メモ化。iの時の方法の総数

f_cnt=Counter(A)
f_dic=dict(f_cnt)

for i,item in enumerate(A):
  if B[item-1]>-1:
    print(B[item-1])
  else:
    D=copy.deepcopy(f_dic)
    D[item] -= 1
    opt=0
    for item3 in D:
      if D[item3]>=2:
        opt += comb(D[item3],2)
    B[item-1]=opt
    print(opt)

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] >= 2:
            sum += combinations_count(c[k],2)
    print(sum)
    
    x.append(t)

Submission status: Runtime Error

Solution code:
n=int(input())
k=list(map(int,input().split()))
print(k)
for i in k:
    x=k.pop(i)
    ans=0
    for j in range(n):
        c=k.count(j)
        ans=ans+c*(c-1)//2
    print(ans)
    if i!=n-1:
        k.insert(i,x)
    else:
        k.append(x)

Submission status: Runtime Error

Solution code:
import copy
from collections import Counter
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() ))
B=[-1]*N   #メモ化。iの時の方法の総数

f_cnt=Counter(A)
f_dic=dict(f_cnt)

for i,item in enumerate(A):
  if B[item-1]>-1:
    print(B[item-1])
  else:
    D=copy.deepcopy(f_dic)
    D[item] -= 1
    opt=0
    for item3 in D:
      if D[item3]>=2:
        opt += comb(D[item3],2)
    B[item-1]=opt
    print(opt)

Submission status: Runtime Error

Solution code:
from math import factorial
import itertools

import sys

def input():
    return sys.stdin.readline()[:-1]
  
def get_comb(n, r):
  if n < r:
    return 0
  else:
    return (factorial(n) / factorial(r) / factorial(n - r))
 
def each_check(ls):
  nums = list(set(ls))
  total = 0
  for i in nums:
    count = ls.count(i)
    total += get_comb(count, 2)
  return int(total)
 
N = input()
L = list(map(int, input().split()))
 
for i in L:
  target = L.pop(0)
  print(each_check(L))
  L.append(target)

Submission status: Runtime Error

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


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


@lru_cache(maxsize=1000)
def comb_count2(val):
    return comb(val, 2, exact=True)

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_count2(val)
    
    print(ans)

Submission status: Runtime Error

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)
dict_ans = {i:0 for i in c.keys()}
full_ans = sum(list(map(comb, c.values())))

for i in c.keys():
    target = c.copy()
    target[i] -= 1
    if target[i] == 0:
        dict_ans[i] = full_ans
    else:
        for k in target.values():
            dict_ans[i] += comb(k, 2)

for i in A:
    print(dict_ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np

def comb2(n):
    return int(n*(n-1)/2)

n = int(input())

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

b = a+a.reshape(n,1)

e = np.ones(n, dtype=int) - np.identity(n, dtype=int)


c = b * e

for i in range(n):
    sums = 0
    tmp, counts = np.unique(c[i], return_counts=True)
    for num in counts:
        sums += comb2(num)
    print(sums)