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 collections
from collections import defaultdict
from math import factorial
n = int(input())
l = list(map(int, input().split()))

dict = defaultdict(int)
for i in l:
    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:
import copy
import collections
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
result = 0
n = int(input())
ary = list(map(int, input().split()))
ary_org = ary
counta = 0
for i in ary:
    result = 0
    # k番目除去
    ary_ = copy.copy(ary_org)
    del ary_[counta:counta+1]
    #print(ary_)
    counta += 1
    #ary_ = set(ary_)
    # 重複検索
    c = collections.Counter(ary_)
    for j in c.keys():
        # 2件以上あれば加算
        if c[j] > 1:
            result += combinations_count(c[1],2)
    print(result)

Submission status: Runtime Error

Solution code:
import collections
def count_defaultdict(it):
    counter = collections.defaultdict(int)
    for x in it:
        counter[x] += 1
    return dict(counter)
n = int(input())
l = list(map(int, input().split()))
all_sum = 0
count = count_defaultdict(l)
for k, v in count:
    all_sum += v * (v - 1) // 2
for i in range(len(l)):
    if count[l[i]] > 1:
        v = count[l[i]]
        print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
    else:
        print(all_sum)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
An = [int(x) for x in input().split()]
 
L = [0,0]
for i in range(N):
    if i > 1:
        L.append(int(math.factorial(i) / (math.factorial(i-2) * 2)))

Cn = [An.count(i) for i in range(1,N+1)]
s = sum([L[Cn[i]] for i in range(N)])

for k in range(N):
    print(sum(Dn) - (Cn[An[k]-1]-1))

Submission status: Runtime Error

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

dict = defaultdict(int)
for i in l:
    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:
N = int(input())
A = list(map(int, input().split()))

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


for i in range(len(A)):
    B = A[:]
    B.pop(i)
    B.sort()
    sum = 0
    prev = -1
    sub_cnt = 1
    for i in range(len(B)):
        if prev == B[i]:
            sub_cnt += 1
        else:
            if prev != -1 and sub_cnt >= 2:
                if sub_cnt >= 2:
                    sum += comb(sub_cnt, 2)
            sub_cnt = 1
        prev = B[i]
    if sub_cnt >= 2:
        sum += comb(sub_cnt, 2)
    print(sum)

Submission status: Runtime Error

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

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

mem_cnt = [0] * N
set_a = set(A)
for c in list(set_a):
    mem_cnt[c] = A.count(c)

for k in A:
    ret = 0
    for v in mem_cnt:
        x = v
        ret += cmb(x,2)
    print(ret)

Submission status: Runtime Error

Solution code:
N = int(input())
An = [int(x) for x in input().split()]
 
L = [0,0]
for i in range(N):
    if i > 1:
        L.append(int(math.factorial(i) / (math.factorial(i-2) * 2)))

Cn = [An.count(i) for i in range(1,N+1)]
s = sum([L[Cn[i]] for i in range(N)])

for k in range(N):
    print(sum(Dn) - (Cn[An[k]-1]-1))

Submission status: Runtime Error

Solution code:
import copy

#N = int(input())
#A = list(map(int,input().split()))
N = 5
A = [3,3,3,3,3]


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

mem_cnt = [0] * N
set_a = set(A)
for c in list(set_a):
    mem_cnt[c] = A.count(c)

for k in A:
    ret = 0
    for v in mem_cnt:
        x = v
        ret += cmb(x,2)
    print(ret)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
num_sum = 0
for i in range(1, N+1):
    c = A.count(i)
    num_sum += int(c * (c - 1) / 2)

for j in A:
    if A.count(j) == 1:
        print(sum(num_sum))
    else:
        print(sum(num_sum) - (A.count(j) - 1))

Submission status: Runtime Error

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

m = int(input())
a = list(map(int,input().split()))[:m]
c = collections.Counter(a)

memo = {}
rmemo = {}

for i in a:
    if not rmemo.get(i):
        print(rmemo[i])
        continue

    res = 0
    for k,v in c.items():
        if i == k: v -= 1
        
        if v >= 2:
            if not memo.get(v):
                memo[v] = combinations_count(v, 2)

            res += memo[v]
    rmemo[i] = res
    print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
num_sum = 0
for i in range(1, N+1):
    c = A.count(i)
    num_sum += int(c * (c - 1) / 2)

for j in A:
    if A.count(j) == 1:
        print(sum(num_list))
    else:
        print(sum(num_list) - (A.count(j) - 1))

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 += comb(cnt, 2, exact=True)
            ended.add(a0)
        print(kumi)

resolve()

Submission status: Runtime Error

Solution code:
import math

N=int(input())
A = list(map(int,input().split()))
cnt=[]
B=[]
for i in range(1,N):
	cnt.append(A.count(i))
	
for i in range(1,N):
	B.append((cnt[i-1]*(cnt[i-1]-1))//2)
	if cnt[i-1]==0:
		B[i-1]=0
		
ans=0
for i in range(1,N):
	ans+=B[i-1]

for i in range(0,N):
	point=A[i]
	deff=(cnt[point-1]*(cnt[point-1]-1))//2-((cnt[point-1]-1)*(cnt[point-1]-2))//2
	print(ans-deff)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
using namespace std;

long long facto(long long n)
{
long long i,ans;
ans=1;
for(i=1;i<=n;i++) {
ans=ans*i;
}
return ans;
}


long long com(int n , int r){
    return ( facto(n) / ( facto(n-r) * facto(r) ) );
}

void comb(vector<vector <long long int> > &v){
  for(int i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(int k = 1;k <v.size();k++){
    for(int j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

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

    long long data[N+1];
    for(int i = 0 ; i<N+1 ;i++){
        data[i] = 0;
    }

    for(int i = 0;i<N;i++){
        data[A[i]] += 1;
    }

    long long total = 0;

    for(int i =0;i<N;i++){
        if( data[i] >=2 ){
            total += com(data[i],2 );
        }
    }

    
    for(int i=0 ; i<N;i++){
        if(data[A[i]]-1>0){
            cout << total - com(data[A[i]]-1,1) << endl;
        }else{
            cout << total << endl;
        }
    
    }

    return 0;
}

Submission status: Runtime Error

Solution code:

n = int(input())
a = list(map(int,input().split()))
b = a.copy()
b = set(b)
b = list(b)
c = []
ans = []

for i in range(len(b)):
    c.append(a.count(b[i]))
print(b)
print(c)
d = []
for i in range(len(b)):
    num = 0
    for j in range(len(b)):
        if i == j:
            num += int(((c[j]-1)*(c[j]-2))/2)
        else:
            num += int(((c[j])*(c[j]-1))/2)
    d.append(num)
print(d)

for i in range(n):
    for j in range(len(b)):
        if a[i] == b[j]:
            print(d[i])

Submission status: Runtime Error

Solution code:
n = int(input())
s = input().split()
for i in tange(n):
  j=0
  count=0
  text=""
  for s0 in s:
    text= s[:j] + s[j+1:]
    if i==j:
      j+=1
    else:
      j=j+1
      i0=len(text)-len(text.replace(s0))
      if i0>1:
        count+= i0-1
        
  print(count)

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))
            ended.add(a0)
        print(kumi)

resolve()

Submission status: Runtime Error

Solution code:
        print(goukei - int(perm_cnt(jisyo[a[i]],2)/2) + int(perm_cnt(jisyo[a[i]]-1,2)/2))    
import collections
import itertools
import math
def perm_cnt(n, r):
    return math.factorial(n) // math.factorial(n - r)

n = int(input())
a = list(map(int,input().split()))
jisyo = collections.Counter(a)
jisyo_value = list(jisyo.values())

array = []
for i in range(len(jisyo_value)):
    if jisyo_value[i] >= 2:
        array.append(int(perm_cnt(jisyo_value[i],2)/2))

for i in range(len(a)):
    goukei = sum(array)
    #print(jisyo[a[i]])
    if jisyo[a[i]] > 2:
        print(goukei - int(perm_cnt(jisyo[a[i]],2)/2) + int(perm_cnt(jisyo[a[i]]-1,2)/2))    
    elif jisyo[a[i]] <= 2:
        print(goukei -1)    


#print(array)
#print(jisyo[0])

Submission status: Runtime Error

Solution code:
import copy
import collections
import math

def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))
result = 0
n = int(input())
ary = list(map(int, input().split()))
ary_org = ary
counta = 0
for i in ary:
    result = 0
    # k番目除去
    ary_ = copy.copy(ary_org)
    del ary_[counta:counta+1]
    #print(ary_)
    counta += 1
    #ary_ = set(ary_)
    # 重複検索
    c = collections.Counter(ary_)
    for j in c.keys():
        # 2件以上あれば加算
        if c[j] > 1:
            result += combinations_count(c[1],2)
    print(result)

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 scipy.misc import comb
from collections import defaultdict, 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()
  a_.remove(i)
  b = 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:
import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

# kごとに、
# 異なる数がいくつあるかを調べる
# (2以上要素が存在する)異なる数ごとに、
# (要素数)C2を解く。

np_A = np.array(A)

# 辞書作成
dic = {}

for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)
    # print("tmp_k", tmp_k)   # k以上
    # print(np.unique(np_A))

    # 同じ組み合わせのケースを排除する。
    # (num, 要素数)の組み合わせを辞書かで作る

    k_unique = np.unique(tmp_k)
    ans = 0

    # k_uniqueをstr化
    # str_unique = [str(i) for i in k_unique]
    maped_num = map(str, k_unique) #格納される数値を文字列にする
    str_unique = "".join(maped_num.tolist())
    # print("str_unique", str_unique)

    # 要素数リストを作成
    count_u_list = []
    for u in k_unique:
        count_u = np.count_nonzero(tmp_k==u)
        count_u_list.append(count_u)
    # 要素数リストをstr化
    maped_num = map(str, count_u_list) #格納される数値を文字列にする
    str_count = "".join(maped_num.tolist())
    # str_count = "".join(count_u_list)
    # print("str_count", str_count)

    key = str_unique+str_count

    if key in dic.keys():
        print(dic[key])
        continue
    else:
        for count_u in count_u_list:
            if count_u < 2:
                continue
            # (count_u) C 2
            tmp = comb(count_u,2)
            ans += tmp

    # 値の更新
    dic[key] = ans
    print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

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

a_set = {}
for i in range(N):
    if i in a_set.keys():
        a_set[i] += 1
    else:
        a_set[i] = 1

for i in range(N):
    a_subset = a_set.copy()
    a_subset[A[i]] -= 1
    s = 0
    for key in a_subset.keys():
        if A[key] >= 2:
            for k in range(1, A[key]):
                s += k
    print(s)

Submission status: Runtime Error

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

int main() {
    ios_base::sync_with_stdio();
    cin.tie(NULL);
    cout.tie(NULL);
    int n,i;
    scanf("%d",&n);
    int a[n+1];
    for(i=1;i<=n;i++)
    scanf("%d",&a[i]);
    int maxi=0;
    for(i=1;i<=n;i++)
    {
        maxi=max(maxi,a[i]);
    }
    
    int cnt[maxi+1]={0};
    for(i=1;i<=n;i++)
    {
        cnt[a[i]]++;
    }
    
    int che[maxi+1]={0};
//    for(i=1;i<=maxi;i++)
  //  cout<<cnt[i]<<" ";
    int sumis,j;
    for(i=1;i<=n;i++)
    {
        sumis=0;
        for(j=1;j<=maxi;j++)
        {
            
            if(cnt[j]==1)
            {
            che[j]=0;
            break;
            }
            else if(che[j]==0)
            {
            if(j==a[i])
            sumis+=((cnt[j]-1)*(cnt[j]-2))/2;
            else
            sumis+=(cnt[j]*(cnt[j]-1))/2;
            }
            else
            break;
        }
        che[j]=sumis;
        printf("%d",che[j]);
    }
    
}

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=[]*N
for i in range(N):
	for j in range(i+1,N):
		if a[i]==a[j]:
			b[i]+=1
for i in range(N):
	ans=0
	for j in range(N):
		if i==j:
			pass
		else:
			ans+=b[j]
	print(ans)

Submission status: Runtime Error

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

for i in range(N):
  c=0
  A.pop(i)
  for j in range(N-1):
    for k in range(j+1,N-1):
      if A[j]==A[k]:
        c+=1
  print(c)

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))
if len(unique_A_list) != len(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])
else:
  for i in A_list:
    print(0)

Submission status: Runtime Error

Solution code:
# coding:utf-8
from math import factorial


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

    a_uniq = list(set(a))
    for v in a_uniq:
        if int(a.count(v)) > 1:
            temp  = factorial(a.count(v)) / factorial(2) / factorial(a.count(v) - 2)
        else: temp = 0
        count = count + temp

    for i in range(n):
        print(count - a.count(a[i])+1)
    
if __name__ == '__main__' :
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
int main(){
	ll n;
	cin >> n;
	vector<int> a(n);
	map<ll, ll>mp;
	for(int i=0; i<n; ++i){
		cin >> a[i];
		++mp[a[i]];
	}
	
	ll msum =0;
	for(auto m : mp){
		ll t=m.second;
		ll tmp = t(t-1)/2;
		if(t<2)tmp = 0;
		msum += tmp;
	}
	
	
	for(int i=0;i <n; ++i){
		ll t= mp[a[i]];
		ll tmp = t(t-1)/2;
		if(t<2)tmp = 0;
		--t;
		ll tmp2 = t(t-1)/2;
		if(t<2)tmp = 0;
		ll ans = msum - tmp + tmp2;
		cout << ans << endl;
	}
}

Submission status: Runtime Error

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

shim0=[-1 for i in range(N)]
real_ans=0
ans1=list(set(A))
for k in range(len(ans1)):
    num_k=A.count(ans1[k])
    real_ans+=(num_k*(num_k-1))//2
    
    
for i in range(N):
    if shim0[A[i]]!=-1:
        print(shim0[A[i]])
    else:
        num=A.count(A[i])
        re_ans=real_ans-(num-1)
        shim0[A[i]]=re_ans
        print(shim0[A[i]])

Submission status: Runtime Error

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

nums = []
count = {}
for a_i in a:
    if not a_i in nums:
        nums.append(a_i)
        count[a_i] = 1
    else:
        count[a_i] = count[a_i] + 1

count = {k:v for k, v in count.items() if v > 1}

pre_cal = {}
for k, v in count.items():
    result = 0
    for key in count.keys():
        tmp_v = count[key] if not key == k else count[key] - 1
        if tmp_v > 1:
            result += (tmp_v * (tmp_v - 1)/2)
    pre_cal[k] = result

ans = ""
for i, a_i in enumerate(a):
#    choice = 0
#    for k, v in count.items():
#        tmp_v = v if not k == a_i else v - 1
#        if tmp_v > 1:
#            choice += (tmp_v * (tmp_v - 1)/2)
    choice = pre_cal[a_i]
    if i == (len(a) - 1):
        ans += str(int(choice))
    else:
        ans += str(int(choice)) + "\n"

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

b=np.zeros(N)

for i in range(N):
	b=np.zeros(N)
    tmp=0
    for j in range(N):
      if(not(j==i)):
        b[int(A[j])]=b[int(A[j])]+1
    for j in range(N):
      if(not(b[j]==0):
         tmp=tmp+b[j]*(b[j]-1)/2
  	print(int(tmp))

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:
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
cnt = Counter(a for a in A)
cnt = {x : cnt[x] for x in cnt if cnt[x] >= 2}
for i in range(N):
  ans = 0
  if cnt[A[i]] > 0:
    cnt[A[i]] -= 1
  print(sum(int(c * (c - 1) / 2) for c in cnt.values()))
  cnt[A[i]] += 1

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
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



for k in range(N):
    cnt=0
    L=A
    L.pop(k)
    aK=L
    lK=list(set(aK))
    for i in lK:
        if aK.count(i)>=2:
            cnt+=cmb(N-1,aK.count(i))
    print(cnt)

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 = []
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 i[0] == i[1]:
            count += 1
    result.append(count)
for i in result:
    print(i)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
# A.sort()
# sum = 0
# prev = -1
# sub_cnt = 1

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


for i in range(len(A)):
    B = A[:]
    B.pop(i)
    B.sort()
    sum = 0
    prev = -1
    sub_cnt = 1
    for i in range(len(B)):
        if prev == B[i]:
            sub_cnt += 1
        else:
            if prev != -1:
                sum += comb(sub_cnt, 2)
            sub_cnt = 1
        prev = B[i]
    print(sum)

Submission status: Runtime Error

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

for k in A:
    cnt = 0
    del A[k]
    for l in A:
        for m in A:
            if l == m:
                cnt += 0
            elif A[l] == A[m]:
                cnt += 1
            else:
                cnt += 0
    print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
a = sorted(A)
answer = [0]*N
r,l = 0,0
for i in range(N):
  b = a.pop(A[i])
  A.remove(A[i])
  ans = 0
  con = 0
  while r < N-1:
    if r == l:
      r += 1
    elif a[r] == a[l]:
      con += 1
      r += 1
      if r == N-1:
        ans += (con*(con+1)//2)

    else:
      l += 1
      ans += (con*(con+1)//2)
      con = 0
  r,l = 0,0
      
  answer[i] = ans
  A.insert(i,b)
  a.append(b)
  a.sort()
for i in range(N):
  print(answer[i])

Submission status: Runtime Error

Solution code:
import collections
from math import factorial

N = int(input())
number = input().split(" ")

a = collections.Counter(number)
b = list(a.keys())
c = list(a.values())

for i in number:
    ans = 0
    for j,num in enumerate(b):
        if i == num:
            if c[j] > 2:
                ans += factorial(c[j]-1) / factorial(2) / factorial((c[j]-1) - 2)
            else:
                pass
        else:
            if c[j] > 1:
                ans += factorial(c[j]) / factorial(2) / factorial(c[j] - 2)
            else:
                pass
    print(int(ans))

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
    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:
N=int(input())
A = [int(x) for x in input().split()]

for i in range(N):
  c=0
  A.pop(i)
  for j in range(N-1):
    for k in range(j+1,N+1):
      if A[j]==A[k]:
        c+=1
  print(c)

Submission status: Runtime Error

Solution code:
import math
s = input()
n=int(s[0])
s = input().split()
d={}
dc={}
summ=0
for i in s:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d:
    n = d[i]
    if n>1:
        a = math.factorial(n)
        b = math.factorial(2)
        c = math.factorial(n-2)
        e=a/b/c
        summ+=e
        dc[i]=e
    else:
        dc[i]=0
for i in s:
    print(int(summ-dc[i]+(dc[i]/d[i]*(d[i]-2))))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
IND = list(set(A))
# CNT = [A.count(x) for x in IND]
# print(IND)
# print(CNT)
CONB = [A.count(x)*(A.count(x)-1)//2 for x in IND]
# print(CONB)
SUM = sum(CONB)
CALC = [-1]*(N+1)
for i in A:
    if CALC[i] != -1:
        print(CALC[i])
    else:
        # n = CNT[IND.index(i)]
        # ans = SUM - CONB[IND.index(i)] + (n-1)*(n-2)//2
        ans = SUM - CONB[IND.index(i)] + CONB[IND.index(i-1)]
        CALC[i] = ans
        print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
from math import factorial


n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
c = [0 for _ in range(max(a))]
for k, v in d.items():
    if v > 2:
        c[k - 1] = factorial(v) / factorial(2) / factorial(v - 2)
    else:
        c[k - 1] = 0
        
for i in range(n):
    ans = sum(c)
    p = a[i]
    res = c[p - 1] * (d[p] - 2) / d[p]
    ans -= c[p - 1] - res
    print(int(ans))

Submission status: Runtime Error

Solution code:
import sys, re
import math
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

# -*- coding: utf-8 -*-

def A(i, mat):
    if i == n:
        return
    for x in range(x, n - 1):
        if a[i] == a[x]:
            match[a[i]] += 1
        A(i + 1, match)

def B(value, count):
    if value <= count:
        return count
    B(value - count, count + 1)



n = int(input())
a = input().split('')
match = []

A(0, match)

for x in range(0, n - 1):
    print(B(match[a[x]]))

Submission status: Runtime Error

Solution code:

N = int(input())
A = list(map(int, input().split()))
count = collections.Counter(A)
ans = [0]*N
for i in range(2,N):
    ans[i] = comb(i, 2)
#print(ans)
for i in range(N):
    arr = copy.copy(count)
    arr[A[i]] -= 1
    cnt = 0
    #print(count)
    for num in count:
        cnt += ans[arr[num]]
    #print(cnt)

Submission status: Runtime Error

Solution code:
import copy
n = int(input())
num_list = list(map(int, input().split()))
index = 0
targets = copy.copy(num_list)
for num in num_list:
	targets.pop(index)
	set_list = list(set(targets))
	r = 0
	a = list(map(lambda s:targets.count(s), set_list))
	b = list(map(lambda s:(s * (s -1))//2, a))
	for s in b:
		r = r + b
	targets.insert(index,num)
	index = index+1
	print(r)

Submission status: Runtime Error

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

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

mem_cnt = {}
set_a = set(A)
for c in list(set_a):
    if str(c) not in mem_cnt.keys():
        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:
n = int(input())
alist = list(map(int,input().split(' ')))

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

old_cdict = dict(c)

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

# print(cdict)

maxi = 0

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

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

Submission status: Runtime Error

Solution code:
D
N = int(input())
A = list(map(int, input().split()))
count = collections.Counter(A)
ans = [0]*N
for i in range(2,N):
    ans[i] = comb(i, 2)
#print(ans)
for i in range(N):
    arr = copy.copy(count)
    arr[A[i]] -= 1
    cnt = 0
    #print(count)
    for num in count:
        cnt += ans[arr[num]]
    #print(cnt)

Submission status: Runtime Error

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

def main():
    n = int(input())
    lis = list(map(int,input().split()))
    ans = Counter(lis)
    for j in lis:
        su = 0
        for k, v in ans.items():
            if k == j:
                su += (v - 2) * (v - 1) // 2
            else:
                su += (v - 1) * v // 2
        print(su)

if __name__ == '__main__':
    main()

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])
    idx = ll.index(i)
    if idx:
        print(0)
    else:
        print(sum - cl[ll.index(i)])