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
import bisect
n = int(input())
a = list(map(int,input().split()))
A = collections.Counter(a)
Key = []
Val = []
for key,val in A.items():
    Key.append(key)
    Val.append(val)
ans = 0
for i in range(len(Val)):
    ans+= ((Val[i])*(Val[i]-1))//2
for i in range(n):
    s = bisect.bisect_left(Key,a[i])
    d1 = (Val[s]*(Val[s]-1))//2
    Val[s]-=1
    d2 = (Val[s]*(Val[s]-1))//2
    ans1 = ans - (d1-d2)
    print(ans1)
    Val[s]+=1

Submission status: Runtime Error

Solution code:
import collections
import bisect
n = int(input())
a = list(map(int,input().split()))
A = collections.Counter(a)
Key = set(a)
Key = list(Key)
Val = []
for val in A.values():
    Val.append(val)
ans = 0
for i in range(len(Val)):
    ans+= ((Val[i])*(Val[i]-1))//2
for i in range(n):
    s = bisect.bisect_left(Key,a[i])
    d1 = (Val[s]*(Val[s]-1))//2
    Val[s]-=1
    d2 = (Val[s]*(Val[s]-1))//2
    ans1 = ans - (d1-d2)
    print(ans1)
    Val[s]+=1

Submission status: Runtime Error

Solution code:
import numpy
import copy
N = int(input())
E= list(map(int,input().split())
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(unique[t]))
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(int(E[k]))]
for i in range(N):
    print(int(ans[i]))

Submission status: Runtime Error

Solution code:
import sys
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
 
n = int(input())
l = [int(x) for x in input().split()]
c = collections.Counter(l)
 
# cl = [0 for i in range(n)]
cl = []
 
# print(ll)
 
if not ll:
    for i in range(n):
        print(0)
    sys.exit()
 
sum = 0
for a, v in c:
    nc = combinations_count(v, 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (v - 2) / v))
 
# print(sum)
# print(cl)
 
def my_index(l, x, default=n):
    if x in l:
        return l.index(x)
    else:
        return default
 
for i in l:
    # print(sum - cl[i-1])
    idx = my_index(ll, i)
    if idx == n:
        print(sum)
    else:
        print(sum - cl[idx])

Submission status: Runtime Error

Solution code:
import sys
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
 
n = int(input())
l = [int(x) for x in input().split()]
c = collections.Counter(l)
 
# 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:
    nc = combinations_count(c[i], 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)
 
def my_index(l, x, default=n):
    if x in l:
        return l.index(x)
    else:
        return default
 
for i in l:
    # print(sum - cl[i-1])
    idx = my_index(ll, i)
    if idx == n:
        print(sum)
    else:
        print(sum - cl[idx])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
#各番号がいくつずつあるのかを整理
#keyに番号、valueにその番号が書かれてあるボールの数
for i in range(n):
    if not　s[i] in ans.keys():
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1
#iC2の総和
answer = 0
for i in ans.values():
    answer += i
#answer-nC2+(n-1)C2 = answer-n+1
#nはans[s[i]],よって以下の出力で求められる
for i in range(n):
    print(answer-ans[s[i]]+1)

Submission status: Runtime Error

Solution code:
def fac(x):
    if x==1:return 0
    return fac(x-1)+x-1

from collections import Counter
c=Counter()
N=int(input())
A=list(map(int,input().split()))
l=len(A)
t=[]
d={}
for i in range(l):
    t=A.copy()
    del t[i]
    d=dict(Counter(t))
    s=0
    for value in d.values():
        if value>=2:
            s=s+fac(value)
    print(s)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
#各番号がいくつずつあるのかを整理
#keyに番号、valueにその番号が書かれてあるボールの数
for i in range(n):
    if s[i] in ans.keys():
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1
#iC2の総和 
answer = 0
for i in ans.values():
    answer += i
#answer-nC2+(n-1)C2 = answer-n+1
#nはans[s[i]],よって以下の出力で求められる
for i in range(n):
    print(answer-ans[s[i]]+1)

Submission status: Runtime Error

Solution code:
from collections import Collection
import math

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

c = Collection(A)

all_ans = 0
for n in c.values():
    if n <= 1:
        continue
    all_ans += math.factorial(n)//(math.factorial(n-2)*math.factorial(2))

for n in A:
    print(all_ans - (c[n]-1))

Submission status: Runtime Error

Solution code:
def main():
  H,W,K = map(int,input().split())
  import numpy as np
  from collections import deque
  ans = 10000
  S = np.array([list(map(int,list(input()))) for _ in range(H)])
  cum = [[0]*W for _ in range(H+1)]
  for j in range(W):
    for i in range(1,H+1):
      cum[i][j] = cum[i-1][j]+S[i-1][j]

  cum = np.array(cum)
  for i in range(2**(H-1)):
    q = deque([0])
    for h in range(H):
      if (i>>h)&1:
        q.append(h+1)
    q.append(H)
    p = deque()
    judge = False
    for a in range(len(q)-1):
      x = cum[q[a+1]]-cum[q[a]]
      if (x>K).any():
        judge = True
        break
      else:
        p.append(x)

    if judge:
      continue

    t_a = len(q)-2
    l = [0]*len(p)
    for w in range(W):
      for x,a in enumerate(p):
        l[x]+=a[w]
        if l[x]>K:
          t_a+=1
          l = [0]*len(p)
          for z,b in enumerate(p):
            l[z] = b[w]
          break
    ans = min(ans,t_a)

  print(ans)
  
main()

Submission status: Runtime Error

Solution code:
#!/user/bin/env python3
# -*- conding: utf-8 -*-

import collections as cl


def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = cl.Counter(A)

    count = 0

    for j in c.values():
        count += int(j * (j-1) / 2)

    for i in range(N):
        print(count - c.values(A[i]) + 1)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
dA = {}
for a in input().split():
    if a in dA:
        dA[a] += 1
    else:
        dA[a] = 1

cA = {}
for a in dA:
    cA[a] = dA[a] * (dA[a] - 1) // 2

for a in A:
    print(sum(cA.values()) - dA[a] + 1)

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 = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	if x <= 1:
		dic[newdata[i]] = 0
	else:
		dic[newdata[i]] = x * (x - 1) // 2
	if x - 1 <= 1:
		after[newdata[i]] = 0
	else:
		after[newdata[i]] = (x - 1) * (x - 2) // 2

numsum = 0
for dicv in dic.values():
	numsum += dicv 

ans = {}
for key in dic.keys():
	ans[key] = numsum - dic[A[key]] + after[A[key]]

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

Submission status: Runtime Error

Solution code:
from scipy.misc import comb

N = int(input())
num_list = list(map(int, input().split()))
num_set = set(num_list)
num_dict = {}

for i in num_set:
  if num_list.count(i) == 1:
    pass
  else:
    num_dict[i] = num_list.count(i)

# 全数あるときの組み合わせ
ans_dict_1 = {}
# 1個抜いたときの組み合わせ
ans_dict_2 = {}

for k, v in num_dict.items():
  ans_dict_1[k] = comb(v,2,exact=True)
  ans_dict_2[k] = comb(v-1,2,exact=True)

total = sum(ans_dict_1.values())

for i in num_list:
  print(total - ans_dict_1[i] + ans_dict_2[i])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# input
n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    if a_dic[key] == 1:
        del a_dic[key]
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    if a_dic[ak] <= 2:
        pass
    else:
        sum_select += int((a_dic[ak]-1) * (a_dic[ak]-2) / 2)
    for key in a_solution_dic:
        if key != ak:
            sum_select += a_solution_dic[key]


    print(sum_select)

Submission status: Runtime Error

Solution code:
n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
d=[0]*(n+1)
for i in l:
    d[i]+=1
ans=0
for i in d:
    ans+=i*(i-1)//2
for i in range(n):
    print(ans-d[l[i]]-1)

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 = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	if x <= 1:
		dic[newdata[i]] = 0
	else:
		dic[newdata[i]] = factorial(x) / factorial(2) / factorial(x - 2)

	if x - 1 <= 1:
		after[newdata[i]] = 0
	else:
		after[newdata[i]] = factorial(x - 1) / factorial(2) / factorial(x - 1 - 2)

numsum = 0
for dicv in dic.values():
	numsum += dicv 


for i in range(N):
	print(int(numsum - dic[A[i]] + after[A[i]]))

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 = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	if x <= 1:
		dic[newdata[i]] = 0
	else:
		dic[newdata[i]] = factorial(x) / factorial(2) / factorial(x - 2)

	if x - 1 <= 1:
		after[newdata[i]] = 0
	else:
		after[newdata[i]] = factorial(x - 1) / factorial(2) / factorial(x - 1 - 2)

	print(dic, after)

numsum = 0
for dicv in dic.values():
	numsum += dicv 


for i in range(N):
	print(int(numsum - dic[A[i]] + after[A[i]]))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
na=a
for i in range(1,n+1):
	hm=[0]*(200000)
	del a[i-1]
	for j in range(len(a)):
		hm[i]+=1
	a=na
	ans=hm.count(2)
	print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
# TLE
duplication = {}
ans = 0
for x in A:
    if x in duplication:
        continue
    count = A.count(x)
    if count > 1:
        duplication[x] = count
        ans += count*(count-1)//2
for x in A:
    # if x not in duplication:
    #     print(ans)
    # else:
    print(ans -(duplication[x]-1))

Submission status: Runtime Error

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

import math

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


count = 0
kaisuu = []
sum = 0
kaisuu = [0 for _ in range(n)]
for num in range(1,max(a)+1):
    count = 0
    for i in range(n):
        if a[i] == num:
            count += 1
    #kaisuu.append(count)
    kaisuu[num] = count
    sum += count * (count - 1)/2

#print('kaisuu',kaisuu)

for k in range(n):
    ans = sum - kaisuu[a[k]] + 1
    print(int(ans))

Submission status: Runtime Error

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

import math

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


count = 0
kaisuu = []
sum = 0
kaisuu = [0 for _ in range(n)]
for num in range(1,max(a)+1):
    count = 0
    for i in range(n):
        if a[i] == num:
            count += 1
    #kaisuu.append(count)
    kaisuu[num] = count
    sum += count + (count - 1)//2

#print('kaisuu',kaisu

for k in range(n):
    ans = sum - kaisuu[a[k]] + 1
    print(ans)

Submission status: Runtime Error

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

import math

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


count = 0
kaisuu = []
sum = 0
kaisuu = [0 for _ in range(n)]
for num in range(1,max(a)+1):
    count = 0
    for i in range(n):
        if a[i] == num:
            count += 1
    #kaisuu.append(count)
    kaisuu[num] = count
    sum += comb(count,2)

#print('kaisuu',kaisuu)

for k in range(n):
    ans = sum - kaisuu[a[k]] + 1
    print(ans)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
A = list(map(int , input().split()))
a = collections.Counter(A)
S = len(a)
k = 0
for j in range (S):
    b = a.most_common()[j][1]
    if b > 1:
        k += b * (b-1)/2
    

for i in range(N):
    s = A[i]
    p = a[s]
    print(int(k - q)

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)

jjj1=[]
jjj2=[]
for k, v in cs.items():
	cs[k]
	jjj1.append(cmb(v-1, 2))
	jjj2.append(cmb(v, 2))

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:
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()]

ll = [x for x in set(l) if l.count(x) > 1]

sm = 0
m = [0 for x in range(n)]
for i in ll:
    t = l.count(i)
    sm += combinations_count(t, 2)
    m[i] = t

for i in l:
    print(sm-(m[i]-1))

Submission status: Runtime Error

Solution code:
from scipy.misc import comb

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

print(int(comb(N, 2) + comb(M, 2)))

Submission status: Runtime Error

Solution code:
import collections
import math

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
cnt = 0
ansx = []
ansy = []
ansy2 = []

for x, y in c.items():
    sumall += (y*(y-1)//2)
    ansy2.append(y-1)
    ansx.append(x)

for i in A:
    print(sumall - ansy2[ansx.index(i)])

Submission status: Runtime Error

Solution code:
import collections
import math

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
cnt = 0
ansx = []
ansy = []
ansy2 = []

for x, y in c.items():
    sumall += (y*(y-1)//2))
    ansy2.append(y-1)
    ansx.append(x)

for i in A:
    print(sumall - ansy2[ansx.index(i)])

Submission status: Runtime Error

Solution code:
def resolve():
    N = int(input())
    A = sorted(map(int, input().split()))

    counter = collections.Counter(A)
    ans = 0

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

    for a in A:
        count = counter[a]
        print(ans - (count * (count - 1) // 2) +
              ((count - 1) * (count - 2) // 2))

resolve()

Submission status: Runtime Error

Solution code:
/*input
8
1 2 1 4 2 1 4 1
*/

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

ll MOD = 1000000007;
const int SINF = 1000000009;
const ll INF =  1000000000000000018;

#define sp          cout << " "
#define nl          cout << "\n"
#define inv(a)      powr((a), (MOD - 2))
#define all(c)      (c).begin(),(c).end()
#define rep(i,a,b)  for(ll i = (a); i < (b); i ++)
#define prec(n)     cout << fixed << setprecision(n)
#define PI          3.14159265358979323846264338327951
#define ifalse      ios_base::sync_with_stdio(false), cin.tie(NULL)

template<typename T> void toInt(string s, T &a) { istringstream(s) >> a; }
ll gcd(ll a, ll b) { ll t; while(a) { t = a; a = b%a; b = t; } return b; }
ll sub(ll a, ll b) { a -= b; a = (a + MOD) % MOD; return a; }
ll mul(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
ll add(ll a, ll b) { a += b; a %= MOD; return a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll powr(ll a, ll b) {
  ll x = 1LL;
  while(b) {
    if(b & 1) x = mul(x, a);
    a = mul(a, a), b >>= 1;
  }
  return x;
}

bool check(ll n)
{
  if(n == 1) return 0;
  if(n == 2 or n == 3) return 1;
  if(n%2 == 0 or n%3 == 0) return 0;
  for(ll i = 5; i*i <= n; i += 6) if(n%i == 0 or n % (i+2) == 0) return 0;
  return 1;
}

/*********************************START**********************************/
const int N = 100005;

ll func(ll n)
{
  return (n * (n - 1)) / 2;
}

int main()
{
  ifalse;
  int n;
  cin >> n;
  map < int, int > mp;
  vector < int > v(n);
  for(auto &x: v) {
    cin >> x;
    mp[x] ++;
  }

  ll ans = 0;
  for(auto x: mp) {
    if(x.second <= 1) continue;
    ans += func(x.second); 
  }

  for(auto x: v) {
    if(mp[x] <= 1) {
      cout << ans, nl;
    } else if(mp[x] == 2) {
      cout << ans - 1, nl;
    } else {
      cout << ans - func(mp[x]) + func(mp[x] - 1), nl;
    }
    
  }
  return 0;
}

Submission status: Runtime Error

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

import math

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


count = 0
kaisuu = []
sum = 0
kaisuu = [0 for _ in range(n)]
for num in range(1,max(a)+1):
    count = 0
    for i in range(n):
        if a[i] == num:
            count += 1
    #kaisuu.append(count)
    kaisuu[num] = count
    sum += comb(count,2)

#print('kaisuu',kaisuu)

for k in range(n):
    ans = sum - (comb(kaisuu[a[k]],2) - comb(kaisuu[a[k]]-1,2))
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter


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


n = int(input())
a = list(map(lambda i: int(i) - 1, input().split()))
b = Counter(a)
# 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:
import collections
_=int(input())
l = list(map(int, input().split()))
c = collections.Counter(l)
M={}

for i,v in c.items():
    if v>1:
        M[i] =  v*(v-1)/2

for k in l:
    M_ = M.copy()
    M_[k] =  M_[k]*(c[k]-2)/c[k]
    print(int(sum(M_.values())))

Submission status: Runtime Error

Solution code:
import collections
_=int(input())
l = list(map(int, input().split()))
c = collections.Counter(l)
M={}

for i,v in c.items():
    if v>1:
        M[i] =  v*(v-1)/2

for k in l:
    M_ = M.copy()
    M_[k] =  M_[k]*(c[k]-2)/c[k]
    print(sum(M_.values()))

Submission status: Runtime Error

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

from math import factorial
from itertools import combinations
from collections import Counter
from copy import deepcopy
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 main():
    n = int(input())
    a = list(map(int, input().split()))
    a_c = Counter(a)
    a_c_sum = 0
    for c in a_c:
        a_c_sum += cmb(a_c[c], 2)

    for ea in a:
        if a_c[ea] <= 1:
            sa = 0
        elif a_c[ea] == 2:
            sa = 1
        else:
            sa = cmb(a_c[ea], 2) - cmb(a_c[ea] - 1, 2)
        print(a_c_sum - sa)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())

a = np.array(list(map(int, input().split())))
d = [0] * n
s = {}

for i in a:
  d[i] += 1
  
sum = 0
for k in d:
  if k>1:
    sum += (k*(k-1))//2

for i in range(n):
  ans=sum-d[a[i]]+1
  print(ans)

Submission status: Runtime Error

Solution code:
import math
from collections import Counter

def cmb(n,r):
    if n < r:
        return 0
    else:
        math.factorial(n,r)

def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter(A)
    sum_ = sum([cmb(i,2) for i in count.values()])

    for i in range(len(A)):
        remain = A[:i] + A[i+1:]
        took_count = count[A[i]]
        sub = took_count - 1
        print(int(sum_ - sub))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = [[] for i in range(n)]

a = [int(i) for i in input().split()]
b = [0]*(n+1)

for i in a:
    b[i] +=1

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)]

def ans(N):
    if N >= 2:
        return(cmb(N,2))
    else: return(0)

cnt=0
for j in b: cnt += ans(j)
for i in a:
    temp = cnt
    temp = temp - (ans(b[i])- ans(b[i]-1))
    print(temp)

Submission status: Runtime Error

Solution code:
import math
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter(A)
    sum_ = sum([math.factorial(i,2) for i in count.values()])

    for i in range(len(A)):
        remain = A[:i] + A[i+1:]
        took_count = count[A[i]]
        sub = took_count - 1
        print(int(sum_ - sub))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())

a = np.array(list(map(int, input().split())))
d = [0] * n
s = {}

for i in a:
  d[i] += 1
  
sum = 0
for k in d:
  if k>1:
    sum += (k*(k-1))//2

for i in range(n):
  nd = d[a[i]]
  ans = sum-(nd*(nd-1))//2
  nd -= 1
  if nd>1:
    ans += (nd*(nd-1))//2
  print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
 
def comb2(n):
    return 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:
import collections
import math


n = int(input())
aa = list(map(int, input().split()))
ls = [0] * (n-1)

c = collections.Counter(aa)

for i, j in c.items():
    ls[i] = j*(j-1)//2

total = sum(ls)

for i in aa:
    before = (c[i]-1)*(c[i]-2) // 2
    print(total - ls[i] + before)

Submission status: Runtime Error

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

Ad = {a_set:A.count(a_set) for a_set in A_set}

score_sum = sum([x*(x-1)/2 for x in list(Ad.values())]

for a in A:
    print(int(score_sum - (Ad[a] - 1)))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import collections
import math

n = int(input())

m = list(map(str,input().split()))

c = dict(collections.Counter(m))
d = {}
sets = {}
def ans(a):
    if a<2:
        return 0
    else:
        return math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))

for i in m:
    if i in sets:
        print(sets[i])
    else:
        cnt = 0
        for j,k in c.items():
            if j == i:
                if k - 1 >= 2:
                    d.clearsetdefault(j, ans(k-1))
                    cnt += d[j]
                
                else:
                    d.setdefault(j, ans(k))
                    cnt += d[j]
                    sets[i] = cnt
        print(cnt)

Submission status: Runtime Error

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

from collections import Counter
c=Counter(A)

ANS=0	# max combination
for i in C.values():
    ANS+=i*(i-1)//2     # 6+1+1 = 8
 
for a in A:
    k=C[a]      # 4 2 4 2 2 4 2 4
 
    print(ANS-(k*(k-1)//2)+((k-1)*(k-2)//2)) # different from max combination
                # 8 - 6 + 3
                # 5

Submission status: Runtime Error

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_sort = sorted(a)

    ball_dict = {}
    count = 1
    for i in range(1, n):
        if a_sort[i] == a_sort[i - 1]:
            count += 1
        else:
            ball_dict.setdefault(a_sort[i - 1], count)
            count = 1
    ball_dict.setdefault(a_sort[n - 1], count)

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

    ans_dict = {}
    for v in ball_dict.values():
        if v not in ans_dict.values():
            ans_dict.setdefault(v, ans - (v - 1))

    for i in range(n):
        print(ans_dict[ball_dict[a[i]]])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from scipy.special import comb

N = int(input())
An = [int(x) for x in input().split()]  
Cn = [An.count(i) for i in range(1,N+1)]  # どの数字が何個あるか？

# ボールを全部使えるときの合計を求める
s = 0
for i in range(N):
    if Cn[i] > 1:
        s += int(comb(Cn[i],2))

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

Submission status: Runtime Error

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

from math import factorial
from itertools import combinations
from collections import Counter
from copy import deepcopy
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 main():
    n = int(input())
    a = list(map(int, input().split()))
    a_c = Counter(a)
    a_c_all = sum([cmb(a_c[i],2) for i in a_c])

    a_c_dic = dict(a_c)
    #a_c_dic_com = {}
    #for ac,ace in a_c_dic.items():
    #    a_c_dic_com[ac] = cmb(ace,2)

    for i in range(n):
        tmpn = a_c_dic[a[i]]
        #print("tmpn:",tmpn)
        if tmpn <= 1:
            sa = 0
        elif tmpn == 2:
            sa = 1
        else:
            sa = (factorial(tmpn) // (2 * factorial(tmpn - 2))) - (factorial(tmpn-1) // (2 * factorial(tmpn -1 - 2)))
        print(a_c_all - sa)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import collections
import math

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

N = int(input())
L = list(map(int, input().split()))
M = set(L)
K = collections.Counter(L)
ans = 0

for i in M:
    ans += combinations_count(K[i], 2) 

for i in range(N):
    B = K[L[i]]    
    if B == 2:
        print(ans - 1)
    elif B > 2:
        print(ans -combinations_count(B,2) +combinations_count(B-1,2))
    else:
        print(ans)

Submission status: Runtime Error

Solution code:
import math

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

C = list(set(A))
M = len(C)
t = []
anst = 0
for i in range(M):
  t.append(A.count(C[i]))
  temp = A.count(C[i])
  anst += math.factorial(temp)/(math.factorial(temp-2)*2)

#print(t)
#print(anst)
#print(jisyo[1])
for i in range(N):
  #print(t[C.index(A[i])])
  print(int(anst-t[C.index(A[i])]+1))

Submission status: Runtime Error

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

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

N = int(input())
L = list(map(int, input().split()))
M = set(L)
K = collections.Counter(L)
ans = 0

for i in M:
    ans += combinations_count(K[i], 2) 

for i in range(N):
    B = K[L[i]]    
    if B == 2:
        print(ans - 1)
    elif B > 2:
        print(ans -combinations_count(B,2) +combinations_count(B-1,2))
    else:
        print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())

a = np.array(list(map(int, input().split())))
d = [0] * n
s = {}

for i in a:
  d[i-1] += 1
  
sum = 0
for k in d:
  if k>1:
    sum += (k*(k-1))//2

for i in range(n):
  nd = d[a[i]]
  ans = sum-(nd*(nd-1))//2
  nd -= 1
  if nd>1:
    ans += (nd*(nd-1))//2
  print(ans)

Submission status: Runtime Error

Solution code:
import sys
import resource
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:
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:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
re = [0] * (n-1)

c = Counter(a)   # c: Counter({1: 3, 2: 2})など
for k, v in c.items():
    re[k] = v*(v-1) // 2

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