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:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

b = [0]*n
d = [0]*n

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

def re(n):
  if n < 2:
    return 1
  return n*re(n-1)

def c(n,r):
  return re(n)//re(n-r)//2

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        l = c(k,2)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    if (d[j-1] == 0) or d[j-1] == 1:
        continue
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = [int(i) for i in input().split()]


def solve(a):
    ar = np.array(a)
    u, counts = np.unique(ar, return_counts=True)
    c = 0
    for v in counts:
        c += int(v*(v-1)/2)
    print(c)

for k in range(N):
    p_A = A[:k] + A[k+1:]
    solve(p_A)

Submission status: Runtime Error

Solution code:
import math
import numpy as np

N = int(input())
arr = np.array(map(int, input().split(" ")))
#val, count = np.unique(arr, return_counts=True)
count = np.bincount(arr)

for i in range(len(arr)):
    k_val = arr[i]
    count[k_val] -= 1
    conv = (count * (count - 1) / 2).astype(int)
    print(np.sum(conv))

Submission status: Runtime Error

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

b = [0]*n
d = [0]*n

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

def re(n):
  if n < 2:
    return 1
  return n*re(n-1)

def c(n,r):
  return re(n)//re(n-r)//2

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        l = c(k,2)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    if d[j-1] == 0:
        continue
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)

Submission status: Runtime Error

Solution code:
import copy
import numpy as np
from math import factorial

def num_choice_2(N):
    if N > 1:
        return factorial(N) // (factorial(N - 2)* 2)
    else:
        return 0

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

nums = np.zeros(N, dtype=int)
for a_i in A:
    nums[a_i - 1] += 1

for i in range(N):
    nums[A[i] - 1] -= 1
    ans = np.sum([num_choice_2(x) for x in tmp_nums])
    nums[A[i] - 1] += 1
    print(ans)

Submission status: Runtime Error

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

from collections import Counter
import copy
from scipy.special import comb

c = Counter(A)
keys, values = zip(*c.most_common())
ans = 0
for i in range(len(values)):
    ans += values[i]*(values[i]-1)//2


for i in range(N):
    l = keys.index(A[i])
    print(ans - comb(values[l],2,exact=True) + comb(values[l]-1,2,exact=True))

Submission status: Runtime Error

Solution code:
import collections

n=int(input())
As=list(map(int,input().split()))
ls=collections.Counter(As)
subd={}
an=ls.values()
for k in l:
  ans+=k*(k-1)//2
sans=ans

for i in As:
  ans=sans+1-ls[i]
  print(ans)

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
a = list(map(int, input()))
for i in range(n):
    b = a[i]
    a[i] = 0
    x = 0
    for v in collections.Counter(a).values():
        x += v*(v-1)//2
    a[i] = b
    print(x)

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

for i in range(N):
    # i = 2
    A_sub = A.copy()
    A_sub.pop(i)

    A_sub = np.array(A_sub)

    ans = 0
    u, counts = np.unique(A_sub, return_counts=True)
    for n in counts:
        # val = uni_vals[0]
        ans += (n*(n-1))/2
    # dic = {}
    # for val in A_sub:
    #     if val in dic.keys():
    #         dic[val] += 1
    #     else:
    #         dic[val] = 1
    # ans = 0
    # for val in dic.values():
    #     ans += (val*(val-1)/2)

    print(int(ans))

Submission status: Runtime Error

Solution code:
from collections import Counter

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

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

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


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

S = Counter(A)

for i in range(l):
  ans = 0
  S[A[i]] -= 1
  for j in S.keys():
    ans += cmb(S[j],2,mod)
  S[A[i]] += 1
  print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

table = set(a)

con = {}
dic = {}
ans = []

con = Counter(a)
dic = {}
#for i in table:
#    con[i] = a.count(i)
#    dic[i] = -1

for x in a:
    if dic[x] == None:
        ex = (con[x] - 1) * (con[x] - 2) / 2
        no = (con[x]) * (con[x] - 1) / 2
        dic[x] = (int(no), int(ex))

su = sum([int(x[0]) for x in dic.values()])

for i,x in enumerate(a):
    print(su - (dic[x][0] - dic[x][1]))

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
List=[int(l) for l in input().split()]
a = [0]*20000
for j in range(n):
    a[List[j]] += 1
c = 0
for i in range(len(a)):
    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:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

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

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

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

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
import numpy as np

A = list(map(int, input().split()))
dp = np.zeros((N,N))
for i in range(N):
  tmp = [0]*(A[i]-1)+[1]+[0]*(N-A[i])
  tmp = np.array(tmp)
  dp[i] = tmp+dp[i-1]

dp = np.array(dp)
ans = []
for i in range(N):
  if i > 0 and i < N-1:
    tmp = dp[N-1] - dp[i] + dp[i-1]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum())
  if i == 0:
    tmp = dp[N-1] - dp[i]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum())
  if i == N-1:
    tmp = dp[N-2]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum())
for i in ans:
  print(int(i))

Submission status: Runtime Error

Solution code:
from math import factorial
import collections

memo={1:0,2:1}
def ajaja(n):
  if n in memo:
    return memo[n]
  if n >=2:
    memo[n]=factorial(n) / factorial(2) / factorial(n - 2)
  else:
    memo[n]=0
  return memo[n]
  
N=int(input())
A=list(map(int,input().split()))
B=set(A)
C=collections.Counter(A)
for i in range(N):
  C[A[i]]-=1
  ans=0
  for j in B:
    ans+=ajaja(C[j])
  print(int(ans))  
  C[A[i]]+=1

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
d = defaultdict(lambda: 0)
for i in range(len(A)):
    d[A[i]] += 1

ans1 = 0
ans2 = 0
r = 2
calc_dict1 = {}
calc_dict2 = {}
for k, v in zip(d.keys(), d.values()):
    if v > 1:
        ans1 = math.factorial(v) // (math.factorial(v - r) * math.factorial(r))
    else:
        ans1 = 0
    if v > 2:
        ans2 = math.factorial(v - 1) // (math.factorial(v - 1 - r) * math.factorial(r))
    else:
        ans2 = 0
    calc_dict1[k] = ans1
    calc_dict2[k] = ans1 - ans2
total = sum(calc_dict1.values())
for i in range(N):
    print(total - calc_dict2[A[i]])

Submission status: Runtime Error

Solution code:
n=int(input())
li=[int(ele)for ele in input().split()]
d={}
def f(d,ele):
  int cnt=0;
  for x in d:
    if(x!=ele):
      if(d[x]>1):
        cnt+=1
  return cnt
      
for ele in li:
  d[ele]=d.get(ele,0)+1
for ele in li:
  print(f(d,ele))

Submission status: Runtime Error

Solution code:
from scipy.misc import comb
from math import factorial
import collections

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

COUNTS = collections.Counter(BALLS)


MEMO = {}
def fact(n):
    if MEMO.get(n):
        return MEMO[n]
    r = 2
    if n - r < 0:
        return 0
    MEMO[n] = int(comb(n, r))
    return MEMO[n]

MEMO2 = {}
def solve(n):
    if MEMO2.get(n):
        return MEMO2[n]

    MEMO2[n] = sum(fact(v if k != n else v - 1) for (k, v) in COUNTS.items())
    return MEMO2[n]

for i in range(N):
    print(solve(BALLS[i]))

Submission status: Runtime Error

Solution code:
import itertools,sys,collections,math,random,fractions,bisect;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]

N, = I()
A = I()
d = dict()
def dd(n):
    if n in d:
        return d[n]
    else:
        d[n] = math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
        return d[n]
c = collections.Counter(A)
total = 0
for e in c.items():
    total += dd(e[1])
for e in A:
    print(total -  dd(c[e]) + dd(c[e]-1))

Submission status: Runtime Error

Solution code:
import sys
import collections
from math import factorial
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()

C = collections.Counter(A)
factorial(N) / factorial(2) / factorial(N - 2)

for x in A:
    ans = 0
    tmp = C[x] - 1
    if tmp >= 2:
        ans += factorial(tmp) / factorial(2) / factorial(tmp - 2)
    for c in C.keys():
        if x != c:
            v = C[c]
            if v >= 2:
                ans += factorial(v) / factorial(2) / factorial(v - 2)
    print(int(ans))

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)
    count = [j for j in set(x) if x.count(j) > 1]
    c = collections.Counter(x)
    for k in count:
        sum += combinations_count(c[k],2)
    print(sum)
    
    x.append(t)

Submission status: Runtime Error

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

for i in range(max(a)):
  if a.count(i + 1) > 1:
    answer += a.count(i + 1) * (a.count(i + 1) -1) // 2

for k in range(n):
  if a.count(k + 1) > 2:
    answer1 = answer - (a.count(k + 1) - 1)
  if a.count(k + 1) == 2:
    answer1 = answer - 1
  print(answer1)

Submission status: Runtime Error

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

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

divided = []
for i in range(N):
    divided.append(A_list.count(i + 1))

ans = 0
for j in divided:
    ans += combinations_count(j, 2)

# print(divided)

for k in range(1, N + 1):
    n = divided[A_list[k - 1] - 1]
    # print(n)
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

Submission status: Runtime Error

Solution code:
import math
import numpy as np

N = int(input())
arr = np.array(input().split(" "))
val, count = np.unique(arr, return_counts=True)

for i in range(len(arr)):
    k_val = arr[i]
    count[np.where(val == k_val)] -= 1
    conv = (count * (count - 1) / 2).astype(int)
    print(np.sum(conv))

Submission status: Runtime Error

Solution code:
N = int(input())
import numpy as np

A = list(map(int, input().split()))
dp = np.zeros((N,9))
for i in range(N):
  tmp = [0]*(A[i]-1)+[1]+[0]*(9-A[i])
  tmp = np.array(tmp)
  dp[i] = tmp+dp[i-1]

dp = np.array(dp)
ans = []
for i in range(N):
  if i > 0 and i < N-1:
    tmp = dp[N-1] - dp[i] + dp[i-1]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum())
  if i == 0:
    tmp = dp[N-1] - dp[i]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum())
  if i == N-1:
    tmp = dp[N-2]
    tmp = tmp*(tmp-1)/2
    tmp = tmp[tmp>0]
    ans.append(tmp.sum())
for i in ans:
  print(int(i))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

A_=Counter(A)
D=[0]*N
S=0
for k,v in A_.items():
    if v<2:
        continue
    a=v*(v-1)//2
    D[k]=a
    S+=a

for a in A:
    x=A_[a]
    y=x*(x-1)//2
    S-=y
    z=(x-1)*(x-2)//2
    print(S+z)
    S+=y

Submission status: Runtime Error

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

from collections import defaultdict

cnt = defaultdict(int)
for i in range(n):
    cnt[a[i]] += 1
# print(cnt)

for key, val in list(cnt.items()):
    if val < 2:
        cnt.pop(key)
print(cnt)

total = 0
for val in cnt.values():
        if val >= 2:
            total += val*(val-1)//2

for i in range(n):
    if a[i] in cnt:
        y = (cnt[a[i]]-1)*cnt[a[i]]//2
        x = (cnt[a[i]]-1)*(cnt[a[i]]-2)//2
        diff = y - x
    print(total - diff)
        
        

# for i in range(n):
#     flag = False
#     if a[i] in cnt:
#         cnt[a[i]] -= 1
#         flag = True
#     # print(cnt)
#     ans = 0
#     for val in cnt.values():
#         if val >= 2:
#             ans += val*(val-1)//2
#     print(ans)
#     if flag:
#         cnt[a[i]] += 1

Submission status: Runtime Error

Solution code:
import collections
import math
 
def combinations_count(n, r):
    return 0 if n < r else math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

combi = collections.Counter(A)
for k, v in combi.items():
  if v <= 1:
    combi.pop(k)
#print(combi)

for i in range(N):
  ans = 0
  
  # i番目の要素をcombiからdecrement
  combi[A[i]] -= 1
  
  #print(combi)
  
  for v in combi.values():
    #print(v)
    ans += combinations_count(v, 2)
  
  print(ans)

  # i番目の要素をcombiからincrement
  combi[A[i]] += 1

Submission status: Runtime Error

Solution code:
from functools import lru_cache

N = int(input().strip())
A = [int(v) for v in input().strip().split()]
C = {}


@lru_cache
def calc(a):
    C[a] -= 1
    sum_ = sum([n * (n - 1) // 2 for n in C.values() if n > 1])
    C[a] += 1
    return sum_

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

for i, a in enumerate(A):
    print(calc(a))

Submission status: Runtime Error

Solution code:
from scipy.special import comb

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

divided = []
for i in range(N):
    divided.append(A_list.count(i + 1))

ans = 0
for j in divided:
    ans += comb(j, 2)

# print(divided)

for k in range(1, N + 1):
    n = divided[A_list[k - 1] - 1]
    # print(n)
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

Submission status: Runtime Error

Solution code:
from math import factorial
import copy
 
n = map(int, input())
a = list(map(int, input().split()))
matches = {}
 
for i in range(len(a)):
  aa = copy.copy(a)
  del aa[i]
  for av in aa:
    if av in matches:
      matches[av] += 1
    else:
      matches[av] = 1
  combi_cnt = 0
  for v in matches:
    combi_cnt += factorial(v) / factorial(v) / factorial(v - 2)
  print(combi_cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from scipy.special import comb
from sys import stdin

N = int(stdin.readline())
count1 = [0] * (N + 1)
count2 = [0] * (N + 1)
A = list(map(int, stdin.readline().split()))

B = list(set(A))

for b in B:
    num_of_b = A.count(b)
    if num_of_b > 2:
        count1[b] = comb(num_of_b, 2, exact=True)
        count2[b] = comb(num_of_b - 1, 2, exact=True)
    elif num_of_b == 2:
        count1[b] = 1


for a in A:
    ans = 0
    ans += sum(count1) - count1[a]
    ans += count2[a]
    print(ans)

Submission status: Runtime Error

Solution code:
def comb(n ,r):
    if n <= 1:
        return 0
    from math import factorial
    return int(factorial(n) / factorial(r) / factorial(n - r))

n = int(input())
ai = [int(each) for each in input().split(' ')]

import copy
for k in range(n):
    newlist = copy.copy(ai)
    del newlist[k]
    newset = set(newlist)
    result = 0
    for i in newset:
        result += comb(newlist.count(i), 2)
    print(result)

Submission status: Runtime Error

Solution code:
import copy
import numpy as np
import collections

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  ans = 0
  b = copy.copy(a)
  b.pop(i)
  c = np.array(list(collections.Counter(b).values()))
  d = int(sum(c*(c-np.ones(len(c)))//2))
  print(d)

Submission status: Runtime Error

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

for i in range(n):
  if a.count(i + 1) > 1:
    answer += a.count(i + 1) * (a.count(i + 1) -1) // 2

for k in range(n):
  if a.count(k + 1) > 2:
    answer1 = answer - (a.count(k + 1) - 1)
  if a.count(k + 1) == 2:
    answer1 = answer - 1
  print(answer1)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
arr = np.array(list(map(int, input().split())))
u, count = np.unique(arr, return_counts=True)
total = 0
for i in count:
    total += (i*(i-1)/2)
for i in arr:
    print(int(total- count[u==i][0]+1))

Submission status: Runtime Error

Solution code:
import math

n = int(input())
aa = list(map(int, input().split(' ')))

counts = {}
for a in aa:
    if a not in counts:
        counts[a] = aa.count(a)

results = {}
for a, c in counts.items():
    count = 0
    for aaa, cc in counts.items():
        if a == aaa:
            cc -= 1
        if cc <= 1:
            continue
        if cc <= 2:
            count += 1
            continue
        count += math.factorial(cc) / 2 / math.factorial(cc - 2)
    results[a] = int(count)

for k in range(n):
    print(str(results[aa[k]]))

Submission status: Runtime Error

Solution code:
from collections import Counter

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

for k in range(N):
    ans = 0
    test = A_list[:k] + A_list[(k+1):]
    temp = Counter(test)
    for k, v in temp.most_common.items():
        if v < 2:
            break
        ans += v*(v-1)/2
    print(int(ans))

Submission status: Runtime Error

Solution code:
from collections import Counter
from scipy.special import comb

N = int(input())
A = list(map(int, input().split()))
l = Counter()
for i in range(N):
    l.update(str(A[i]))

gsum = 0
combD = {}
for key in l.keys():
    tmp = comb(l[key], 2, exact=True)
    gsum += tmp
    combD[str(key)] = int(tmp - (tmp*(1-2/l[key])))

for i in range(N):
    print(gsum - combD[str(A[i])])

Submission status: Runtime Error

Solution code:
N = int(input())
import numpy as np

A = list(map(int, input().split()))
dp = np.zeros((N,9))
for i in range(N):
  tmp = [0]*(A[i]-1)+[1]+[0]*(9-A[i])
  tmp = np.array(tmp)
  dp[i] = tmp+dp[i-1]

dp = np.array(dp)
ans = []
for i in range(N):
  if i > 0 and i < N-1:
    tmp = dp[N-1] - dp[i] + dp[i-1]
    tmp = tmp*(tmp-1)/2
    tmp = [t for t in tmp.tolist() if t > 0]
    tmp = np.array(tmp)
    ans.append(tmp.sum())
  if i == 0:
    tmp = dp[N-1] - dp[i]
    tmp = tmp*(tmp-1)/2
    tmp = np.array([t for t in tmp.tolist() if t>0])
    ans.append(tmp.sum())
  if i == N-1:
    tmp = dp[N-2]
    tmp = tmp*(tmp-1)/2
    tmp = np.array([t for t in tmp.tolist() if t>0])
    ans.append(tmp.sum())
for i in ans:
  print(int(i))

Submission status: Runtime Error

Solution code:
from functools import lru_cache

N = int(input().strip())
A = [int(v) for v in input().strip().split()]
C = {}
S = {}


@lru_cache
def calc(C, a):
    C[a] -= 1
    sum_ = sum([S[n] for n in C.values() if n > 1])
    C[a] += 1
    return sum_

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


for i, a in enumerate(A):
    print(calc(C, a))

Submission status: Runtime Error

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

import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
base = 0
for i in range(len(a)):
    base = 0
    a=list(map(int, input().split()))
    a_ = a.pop(int(i))
    set_ = list(set(a))
    for j in range(len(a_)):
        cnt = a_.count(set_[j])
        if 2 <= cnt:
            comb_cnt = comb(cnt,2)
            base = base + comb_cnt
    print(base)

Submission status: Runtime Error

Solution code:
# D
import collections

def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
def combination_count(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
N = int(input())
A = list( map(int,input().split()))
c = dict(collections.Counter(A))
su = 0
for i in c.values():
    if (i >= 2):
        su += combinations_count(i, 2)

for i in A:
    if (c[i]  >= 3):
        print(su - combinations_count(c[i],2) + combinations_count(c[i]-1,2))
    elif (c[i] == 2):
        print(su - 1)
    else:
        print(su)

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
a=[int(input()) for i in range(n)]
c = 0
c_list = []
for i in range(n):
    s = a.pop(i)
    
    for j in itertools.combinations(a, 2):    
        if(j[0] == j[1]):
            c += 1
        
    a.insert(i,s)
    c_list.append(c)
    c = 0


for i in range(len(c_list)):
    print(c_list[i])

Submission status: Runtime Error

Solution code:
import math

n = int(input())
aa = list(map(int, input().split(' ')))

counts = {}
for a in aa:
    if a not in counts:
        counts[a] = aa.count(a)

results = {}
for a, c in counts.items():
    count = 0
    for aaa, cc in counts.items():
        if a == aaa:
            cc -= 1
        if cc <= 1:
            continue
        count += math.factorial(cc) / 2 / math.factorial(cc - 2)
    results[a] = int(count)

for k in range(n):
    print(str(results[aa[k]]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = np.fromstring(input(), np.uint64, sep=" ")

uniq, counts = np.unique(A, return_counts=True)

p = lambda n: n * (n - 1) // 2

n = p(counts)
m = p(counts-1)

ans = n.sum() - n + m

mapping = dict(zip(uniq, ans))

for a in A:
    print(mapping[a])

Submission status: Runtime Error

Solution code:
import itertools
import sys
n=int(input())
a=[int(input()) for i in range(n)]
c = 0
c_list = []
for i in range(n):
    s = a.pop(i)
    
    for j in itertools.combinations(a, 2):    
        #print(j)
        if(j[0] == j[1]):
            c += 1
        
    a.insert(i,s)
    c_list.append(c)
    c = 0


for i in range(len(c_list)):
    print(c_list[i])

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
arr = np.array(list(map(int, input().split())))
u, count = np.unique(arr, return_counts=True)
comb = []
for i in count:
    comb.append(i*(i-1)/2)
total = np.sum(comb)
comb = np.array(comb)
for i in arr:
    print(int(total- count[u==i][0]+1))

Submission status: Runtime Error

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

int main()
{
  int N;
  cin >> N;
  vector<int> nums(N);
  vector<int> balls(N);
  for (int i = 0; i < N; i++)
  {
    cin >> balls.at(i);
    nums.at(balls.at(i) - 1)++;
  }
  int ans = 0;
  for (int i = 0; i < N; i++)
  {
    ans += nums.at(i) * (nums.at(i) - 1) / 2;
  }
  for (int i = 0; i < N; i++)
  {
    int a = ans - (nums.at(balls.at(i) - 1) - 1);
    cout << a << endl;
  }
}

Submission status: Runtime Error

Solution code:
import java.util.*;

public class Main{

  static int check(int k, int n, int[]a){
    int t=0;

    for(int i=0; i<n-1; i++){
      if(i == k) i++;

      for(int j=i+1; j<n; j++){
        if(j == k) j++;
        if(j == n) break;
        if(a[i]==a[j])
          t++;
      }
    }
    return t;
  }
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int n = Integer.parseInt(sc.next());

    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    for(int i=0; i<n; i++)
      System.out.println(check(i, n, a));

  }
}

Submission status: Runtime Error

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

for i in range(n):
  if a.count(i + 1) > 1:
    answer += a.count(i + 1) * (a.count(i + 1) -1) // 2

for k in range(n):
  if a.count(k + 1) > 2:
    answer1 = answer - (a.count(k + 1) - (a.count(k + 1) - 2)) * (a.count(k + 1) - 1) // 2
  if a.count(k + 1) == 2:
    answer1 = answer - 1
  print(answer1)

Submission status: Runtime Error

Solution code:
from math import factorial
import collections


N=int(input())
A=list(map(int,input().split()))
B=set(A)
C=collections.Counter(A)
for i in range(N):
  D=C.copy()
  #print(C)
  #print(D)
  D[A[i]]-=1
  ans=0
  for j in B:
    if D[j]>=2:
      ans+=factorial(D[j]) / factorial(2) / factorial(D[j] - 2)
  print(int(ans))

Submission status: Runtime Error

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

c = dict(Counter(a))
dp_on = {}
dp_off = {}
for k, v in c.items():
    if v == 1:
        dp_on[k] = 0
        dp_off[k] = 0
    elif v == 2:
        dp_on[k] = 1
        dp_off[k] = 0
    else:
        dp_on[k] = int(v*(v-1)/2)
        dp_off[k] = int((v-1)*(v-2)/2)
    comb_sum = sum([v for v in dp_on.values()])
    for i in range(n):
        print(comb_sum - dp_on[a[i]] + dp_off[a[i]])

Submission status: Runtime Error

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

int main(){
  int n;
  cin >> n;
  int a[n];
  int num[200001]={0};
  for(int i=0; i<n; i++){
    int s;
    scanf("%d",&s);
    a[i] =s;
    num[s]++;
  }
  int sum=0;
  for(int i=1; i<=n; i++){
    if(num[i]>1){
      sum +=num[i]*(num[i]-1)/2;
     }
  }
  for(int i=0; i<n; i++){
    if(num[a[i]]==1){
      cout << sum << endl;
    }else{
      int b = num[a[i]];
      cout << sum - (b-1) << endl;
    }

  }
  return 0;
}

Submission status: Runtime Error

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

import sys
from math import factorial as fact
sys.setrecursionlimit(10 ** 6)


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

Submission status: Runtime Error

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

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

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


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

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

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

# print(dp)

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

Submission status: Runtime Error

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

import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
base = 0
for i in range(len(a)):
    a=list(map(int, input().split()))
    base = 0
    a_ = a.pop(int(i))
    set_ = list(set(a))
    for j in range(len(a_)):
        cnt = a_.count(set_[j])
        if 2 <= cnt:
            comb_cnt = comb(cnt,2)
            base = base + comb_cnt
    print(base)