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: Time Limit Exceeded

Solution code:
import math

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

def comb(x):
  if x <= 1:
    return 0
  return math.factorial(x) // (math.factorial(x - 2) * math.factorial(2))

dic = {}
#まず最初に、グルーピング(グルーピングの時点で辞書化が必要)
for a in A:
  if a in dic:
    dic[a][0] += 1
  else:
    dic[a] = [1]

#辞書にコンビネーションをvalueとして書き込んでいく
for v in dic.values():
  v.append(comb(v[0]))
  v.append(comb(v[0]-1))

#各グループのlen(g)C2を総計して変数grandとして保持しておく
grand = 0
for v in dic.values():
  grand += v[1]
  
for a in A:
  output = grand - dic[a][1] + dic[a][2]
  print(output)

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools

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

for i in range(N):
    tmp = copy.copy(A)
    del tmp[i]
    #tmp.pop(i)

    res = 0
    for j in list(set(tmp)):
        l = tmp.count(j)
        res += len(list(itertools.combinations(list(range(l)),2)))

    print(res)

Submission status: Time Limit Exceeded

Solution code:
def comb(n,k):
    nCk = 1
    MOD = 10**9+7
    # X
    for i in range(n-k+1, n+1):
        nCk *= i
        nCk %= MOD

    # Y
    for i in range(1,k+1):
        nCk *= pow(i,MOD-2,MOD)
        nCk %= MOD
    return nCk

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

cnt = [0]*(n+1)
ans = 0

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

for k in range(n):
    cnt[a[k]] -= 1
    ans = 0
    for j in range(1, n+1):
        if cnt[j] >= 2:
            ans += comb(cnt[j], 2)
    print(ans)
    cnt[a[k]] += 1

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import math
import collections

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

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

#    for key, value in count_dict.items():
#        if value == 1:
#            count_dict.pop(key)

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = int(comb(value, 2))
        sum_all += int(comb(value, 2))


    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + int(comb(count_dict[A[k]]-1, 2))
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int, input().split()))
set_of_numbers = set(P)
num_dict = {}
num_dict_k = {}
for num in set_of_numbers:
    num_of_num = P.count(num)
    pair_set = int(num_of_num*(num_of_num-1)/2)
    pair_set_k = int((num_of_num-1)*(num_of_num-2)/2)
    # num_dict[num] = P.count(num)
    num_dict[num] = int(pair_set)
    num_dict_k[num] = int(pair_set_k)
    # print(pair_set, pair_set_k)


for k in range(N):
    # 取り除く数字はP[k]
    result = 0
    for num in set_of_numbers:
        # numbers = num_dict[num]
        if (num == P[k]):
            result += num_dict_k[num]
        else:
            result += num_dict[num]
    print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
num = [0]*n
for i in A:
  num[i-1] += 1
frac = [1]
for i in range(max(num)):
    frac.append(frac[i]*(i+1))
def comb(n, k):
    a=frac[n]
    b=frac[k]
    c=frac[n-k]
    return (a // (b * c))
ans_sub = 0
for i in num:
  if i < 2:
    pass
  else:
    ans_sub += comb(i, 2)
for i in A:
  if num[i-1] < 2:
    a = 0
  else:
    a = num[i-1]-1
  print(ans_sub-a)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

As = np.asarray(input().split(), dtype=np.uint32)
As_sort = np.sort(As)

numbers = {}

count = 1
search = As_sort[0]
for i in range(n-1):
    if As_sort[i+1] == search:
        count += 1
    else:
        numbers[search] = count
        search = As_sort[i+1]
        count = 1
    if i == n-2:
        numbers[search] = count

values = np.asarray(list(numbers.values()), dtype=np.uint32)

full_ans = 0
for i in range(len(values)):
    full_ans += (values[i] * (values[i] - 1) // 2)

for i in range(n):
    print(full_ans - (numbers[As[i]] - 1))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for i in range(len(a)):
  a_ = a.copy()
  a_.pop(i)
  A = list(set(a_))
  ans = 0
  for j in A:
    ans += comb(a_.count(j), 2, exact=True)

  print(ans)

Submission status: Time Limit Exceeded

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

for i in range(len(a)):
    A = a.copy()
    A.pop(i)
    #print(A)
    dict = {}
    for j in range(len(a)-1):
        if A[j] not in dict:
            dict[A[j]] = A.count(A[j])
#print(dict)
    total = 0
    for k in dict:
        if dict[k] >=2:
            total += dict[k]*(dict[k]-1)/2
    print(int(total))

Submission status: Time Limit Exceeded

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

result = [0]*(N+1)
for i in range(1,N+1):
  cnt = A.count(i)
  result[i] = cnt * (cnt-1) * (1/2)
  
for i in range(1,N+1):
  print(int(sum(result)-(A.count(A[i-1])-1)))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
B = Counter(A)
seen = [-1]*(5*10**5)
nc2 = [-1]*(5*10**5)
for i in range(N):
    if seen[A[i]] != -1:
        print(seen[A[i]])
        continue
    B[A[i]] -= 1
    ans = 0
    for n in B.values():
        if nc2[n] != -1:
            ans += nc2[n]
            continue
        ans += n*(n-1)//2
    print(ans)
    seen[A[i]] = ans
    B[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = input().split()

for i in range(n):
  c = 0
  b = copy.copy(a)
  b.pop(i)
  for j in range(n):
    d = b.count(str(j)) 
    c += int(d*(d-1)/2)
  print(c)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
def main():
    n = int(input())
    As = input().split(" ")
    partial = reversed(list(itertools.combinations(As, r=n-1)))
    for As_tmp in partial:
        #print(As_tmp)
        a = np.array(list(itertools.combinations(As_tmp, 2)))
        print(len(a[:,0][a[:,0] == a[:,1]]))
        #b = map(lambda x: len(set(x))-2, a)
        #ans = - sum(b)
        #print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def abc159d(n, arr):
    dic = {}
    for a in arr:
        if a in dic:
            dic[a] += 1
        else:
            dic[a] = 1
    
    # delete non-paired ball
    # del_list = []
    # for k, val in dic.items():
    #     if val == 1:
    #         del_list.append(k)
    # for item in del_list:
    #     del dic[item]

    ans = []
    for a in arr:
        sum = 0
        for k, val in dic.items():
            n = val
            if k == a:
                n -= 1

            sum += n * (n-1) // 2

        ans.append(sum)

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    arr = input().split(" ")
    
    ans = abc159d(n, arr)
    
    # output
    for a in ans:
        print(a)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
def ans(a,x):
  b=0
  c=a[:x]+a[x+1:]
  d=list(set(c))
  for i in range(len(d)):
    if c.count(d[i])>1:
      b=b+(c.count(d[i])*(c.count(d[i])-1))/2
  return b
for k in range(n):
  print(int(ans(a,k)))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
input()
aa = list(map(int, input().split()))
uu = list(set(aa))
cc = []
for u in list(uu):
    cc += [aa.count(u)]
dic = dict(zip(uu,cc))
total = 0
for v in list(cc):
    total += v * (v-1) / 2
    
for a in aa:
    print(int(total - dic[a] + 1))

Submission status: Time Limit Exceeded

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

c = collections.Counter(A)

for i in range(N):
    target = c.copy()
    target[A[i]] -= 1
    ans = 0
    for k in [j for j in target.values() if j >= 2]:
        ans += comb(k, 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
import itertools

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_original = a.copy()
    count = 0
    
    for i in range(n):
        a.pop(i)
        for v in itertools.combinations(a, 2):
            if v[0] == v[1]:
                count = count + 1
        print(int(count))
        count = 0
        a = a_original.copy()
    
if __name__ == '__main__' :
    main()

Submission status: Time Limit Exceeded

Solution code:
#from scipy.special import comb
#from math import factorial
import collections

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

#a = cmb(n,r)
c = collections.Counter(A)
l1 = []
l2 = []
l3 = []
for i in c.keys():
  l1.append(i)
  if c[i] == 1:
    l2.append(0)
    l3.append(0)
  if c[i] == 2:
    l2.append(1)
    l3.append(0)
  if c[i] >= 3:
    l2.append(cmb(c[i], 2))
    l3.append(cmb(c[i]-1, 2))    

#print(l1)
#print(l2)
#print(l3)

for i in range(N):
  sum = 0
  for j in c.keys():
    var_index = l1.index(j)
    if A[i] != j:
      sum += l2[var_index]
    else:
      sum += l3[var_index]
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np

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

ca = collections.Counter(A)
cakeys = list(ca.keys())
cavalues = np.array(list(ca.values()))

#print(cakeys)
#print(np.array(list(ca.values())))

cavaluesmax = cavalues*(cavalues-1)//2
cavaluesmin = (cavalues-1)*(cavalues-2)//2

#print(cavaluesmax)
#print(cavaluesmin)

ans = 0
for i in range(N):
    #print(cakeys[index])
    index = cakeys.index(A[i])
    ans = cavaluesmin[index].sum() + cavaluesmax[:index].sum() + cavaluesmax[index+1:].sum()
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(len(A)):
  count = 0
  B = A[:]
  B.pop(i)
  for b in set(B):
    l = B.count(b) - 1
    count += (l * (l+1)) // 2
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(N):
	Atmp = A.copy()
	Atmp.pop(i)
	l = collections.Counter(Atmp)

	l_v = l.most_common()
	res = 0
	for j in range(len(l_v)):	
		t = int(l_v[j][1])
		if int(t) >= 2:
			res += t * (t - 1) // 2
	print(res)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
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, exact=True))
    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: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
count = [0]*(N+1)
for i in range(N):
    count[A[i]] += 1

for k in range(N):
    count[A[k]] -= 1
    print(int(sum([x*(x-1)/2 for x in count])))
    count[A[k]] += 1

Submission status: Time Limit Exceeded

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

n = int(input())
a_list = list(map(int, input().split()))
a_list.append(n + 100)

for i in range(len(a_list) - 1):
  tmp_list = a_list[:]
  del tmp_list[i]
  tmp_list.sort()
  count = 0
  tmp_count = 0
  for j in range(1, len(tmp_list)):
    if tmp_list[j] == tmp_list[j - 1]:
      tmp_count += 1
    else:
      if tmp_count >= 1:
      	count += comb(tmp_count + 1, 2)
      tmp_count = 0
  print(count)

Submission status: Time Limit Exceeded

Solution code:
N= int(input())
A = list(map(int, input().split()))
B=[0]*N
C=0
for i in range(N):
    B[i]=A.count(i+1)
    C = C + B[i] * (B[i] - 1) / 2
for i in range(N):
    print(int(C-(B[A[i]-1])+1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
k = []
q = []
for i in range(n):
    t = a[i]
    if t in k:
        q[k.index(t)] += 1
    else:
        k.append(t)
        q.append(1)
M = 0
for i in q:
    if i > 1:
        M += i*(i-1)//2
for i in range(n):
    w = a[i]
    if w in k:
        m = q[k.index(w)]
        print(M-m+1)
    else:
        print(M)

Submission status: Time Limit Exceeded

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

for i in range(n):
    a_copy = a[:]
    a_copy.remove(a[i])
    c = collections.Counter(a_copy)
    cnt = 0
    for i in c.values():
        cnt += i * (i-1) // 2

    
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math

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

N = int(input())
A = list(map(int, input().split()))
dp = [0]*(N+1)
cnt_org = {}

for a in A:
  key = str(a)
  cnt_org[key] = cnt_org.get(key, 0) + 1

for k in range(N):
  cnt = cnt_org.copy()
  cnt[str(A[k])] -= 1
  ans = 0
  for v in cnt.values():
    if dp[v] == 0:
      dp[v] = combinations_count(v)
    ans += dp[v]
  
  print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter
import copy
def cmb(n, r):
    if n < r:
        return 0
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

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 - cmb(values[l],2) + cmb(values[l]-1,2))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
ans = 0
for k in range(N):
    buf = A[k]
    ans = 0
    A[k] = 0
    c = collections.Counter(A)
    for l in c.values():
        ans = ans + l*(l-1)/2
    print(int(ans))
    A[k] = buf

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
Y = set(A)
memo = {}
for k in range(0,N):
    key = A[k] 
    if A.count(key) == 1:
        print(0)
        continue
    if key in memo:
        print(memo[key])
        continue
    c = 0
    for i in Y:
        sc = A[:k].count(i)
        sc += A[k+1:].count(i)
        if sc == 1:
            continue
        #カウント対象
        c += sc * (sc-1)//2
    memo[key] = c
    print(c)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

n = int(input())
a = input().split()

for i in range(n):
  c = 0
  b = copy.copy(a)
  b.pop(i)
  d = collections.Counter(b)
  for j in d.values(): 
    c += int(j*(j-1)/2)
  print(c)

Submission status: Time Limit Exceeded

Solution code:

def main():
  from functools import reduce
  from operator import mul
  
  from collections import deque
  from collections import defaultdict
  
  n = int(input())
  a = list(map(int, input().split()))
  d = defaultdict(int)
  e = defaultdict(int)
  
  for x in a:
    d[x] += 1
  
  for f in d.keys():
    g = d[f] - 1
    h = [v for k,v in d.items() if (k != f and v>=2)]
    h.append(g)
    i = list(map(func, h))
    ans = sum(i)
    
    e[f] = ans
    
  for x in a:
    ans = e[x]
    print(ans)

    
def func(x):
  if x < 2:
    return 0
  else:
    ans = x*(x-1)//2
    return ans 
      
 
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
# print(A)
# N = 4
# A = [1,2,3,4]
counts = [0]*N
for a in A:
  counts[a-1] += 1

for i in range(len(A)):
  cnt = 0
  for j in range(len(counts)):
    t = counts[j] if A[i]!=(j+1) else counts[j] - 1
    if t<2:
      continue
    cnt += int(t * (t-1) * 0.5)
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

s = Counter(c)
t = list(s.items())

nag = len(t)
x = 0
ans = 0

for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
    
for j in range(n):
    hg = c.count(c[j])
    ans = x - (hg-1)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import functools
 

@functools.lru_cache(maxsize=8128)
def nCr(n,r):
    if n<r:
        return 0
    
    result = math.factorial(n)//math.factorial(r)//math.factorial(n-r)
    return result

def compute(count):
    total = 0
    for i in count:
        total += nCr(i,2)
    return total


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

count = [0] * (N+1)

for value in data:
    count[value] += 1
    

for i in range(len(data)):
    count[data[i]] -= 1
    
    total = compute(count)
    print(total)
          
          
    count[data[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
b=collections.Counter(a)
for l in a:
    s=0
    for i in b:
        c=b[i]
        if i==l:
            s+=(c-1)*(c-2)//2
        else:
            s+=c*(c-1)//2
    print(s)

Submission status: Time Limit Exceeded

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

import collections

for i in range(N):
  ans=0
  c = collections.Counter(A)
  c[A[i]]=c[A[i]]-1
  B=list(c.values())
  for k in range(len(B)):
    ans+=B[k]*(B[k]-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
l = [s for s in input().split()]

c = collections.Counter(l)

for i in l:
    c[str(i)] -= 1
    hoge = list(c.values())
    fuga = [(i*(i-1))/2 if i >= 2 else 0 for i in hoge]
    ans = sum(fuga)
    c[str(i)] += 1
    
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

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

dosu = [0 for _i in range(n)]

for e in nums:
    dosu[e - 1] += 1

combs = [-1 for _i in range(n)]

for e in nums:
    if combs[e - 1] < 0:
        dosu[e - 1] -= 1
        combs[e - 1] = sum(map(lambda x: comb(x, 2) if x > 1 else 0, dosu))
        dosu[e - 1] += 1

    print(combs[e - 1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a1 = set(a)
default = 0
for i in a1:
    default += a.count(i) * (a.count(i) - 1) // 2
for k in a:
    print(default - (a.count(k) - 1))

Submission status: Time Limit Exceeded

Solution code:

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

data_elements = list(set(data))

for k in range(n):
    data_k = data[:]
    data_k.pop(k)
    data_k_elements = list(set(data_k))
    answer = 0
    for x in data_k_elements:
        i = data_k.count(x)

        answer += int(i*(i-1)/2)

    print(answer)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for k in range(n):
    test = []
    for j in range(n):
        if j == k: continue
        test.append(a[j])
        
    c = collections.Counter(test)
    ans = 0
    for i in c.values():
        for l in range(1,i):
            ans += l
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
data = list(map(int, input().split()))

for k in range(n):
  data_n = data.copy()
  data_n.pop(k)
  cnt = 0
  for v in itertools.combinations(data_n, 2):
    if v[0] == v[1]:
      cnt += 1
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
c = [A.count(i + 1) for i in range(N)]
ans = [c[i] * (c[i] - 1) // 2 for i in range(N)]
Sum = sum(ans)

for val in A:
  m = c[val - 1] - 1
  print(Sum - m)

Submission status: Time Limit Exceeded

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

for i in range(N):
    B = sorted(A[0:i] + A[i+1:N])
    s = 0
    for j in range(N-1):
        for k in range(j+1,N-1):
            if B[j] == B[k]:
                s += 1
    print(s)

Submission status: Time Limit Exceeded

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

a = list(set(A))
dup = [x for x in a if A.count(x) > 1]
m = len(a)
l = len(A) 
c_l = [(A.count(i)-1)*(A.count(i))/2 for i in a]
al = sum(c_l)

for k in A:
    if k not in dup:
        print(int(al))
    else :
        print(int(al-A.count(k)+1))

Submission status: Time Limit Exceeded

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


dic = {}
ans = 0
for t in A:
    if t in dic.keys():
        continue
    tmp = A.count(t)
    dic[t] = tmp
    ans += tmp*(tmp-1)//2

for i in range(N):
    val = dic.get(A[i])
    print(ans-val+1)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
  ans = 0
  temp = l.copy()
  temp.pop(i)
  c = collections.Counter(temp)
  for j in c.most_common():
    if j[1] > 1:
      ans += math.factorial(j[1]) // (math.factorial(j[1] - 2) * math.factorial(2))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
balls = input().split()
for i in range(n):
  balls[i] = int(balls[i])
for j in range(n):
  ans = 0
  for k in range(n):
    if balls[j] == k:
      ans += (balls.count(k) - 1) * (balls.count(k) - 2) / 2
    else:
      ans += balls.count(k) * (balls.count(k) - 1) / 2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
import math
from collections import Counter
def comb(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i in range(N):
  tmp = A.pop(i)
  cnt = Counter(a for a in A)
  ans = 0
  for a, c in cnt.items():
    if c >= 2:
      ans += comb(c, 2)
  print(ans)
  A.insert(i, tmp)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

D = np.zeros(2*(10**5)+1,dtype=int)


for i in L:
    D[i] += 1

Dcopy = D

D = np.where(D>1,(D-1)*D/2,0)
temp = int(D.sum())

for j in range(len(L)):
    if Dcopy[L[j]] > 1:
        print(temp - Dcopy[L[j]] + 1)
    else:
        print(temp)

Submission status: Time Limit Exceeded

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

def comb(n, r):
  if n<=1:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
cnt={}
for a in set(A):
  cnt[a]=A.count(a)
combs,total={},0
for c in cnt:
  tmp=comb(cnt[c],2)
  combs[c]=tmp
  total+=tmp
for a in A:
  print(total-combs[a]+comb(cnt[a]-1,2))

Submission status: Time Limit Exceeded

Solution code:

#####

import numpy as np
# from numpy import int64, int8
from collections import Counter

#####

def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin
# import sys
# sys.setrecursionlimit(10**8)

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs


def unique(x):
	count=Counter()
	for xi in x:
		count[xi]+=1
	u,c=zip(*count.items())
	return u,c



def main(inputs):
	n=int(inputs[0][0])
	aa=getIntList(inputs[1])

# 	n=200000
# 	aa=np.random.randint(1,n+1,n)
	
# 	print("start")
# 	aa=np.array(aa)
# 	aUnique, count=np.unique(aa, return_counts=True)

	aUnique, count=unique(aa)
	aUnique=np.array(aUnique, np.int64)
	count=np.array(count, np.int64)
	
# 	print(n)
# 	print("aa",aa)
# 	print("aUnique",aUnique)
# 	print("count  ",count)
	
# 	countUnique,countCount=np.unique(count, return_counts=True)
	countUnique,countCount=unique(count)
	countCount=np.array(countCount, np.int64)
	
	pat={}
	countAns=np.empty(len(countUnique), np.int64)
	countAns1=np.empty(len(countUnique), np.int64)
	for ci,c in enumerate(countUnique):
		if c not in pat:
			ni=c*(c-1)//2
			pat[c]=ni
		countAns[ci]=pat[c]
		
		c-=1
		if c not in pat:
			ni=c*(c-1)//2
			pat[c]=ni
		countAns1[ci]=pat[c]
	
# 	print("countUnique",countUnique)
# 	print("countCount ",countCount)
# 	print("countAns ",countAns)
# 	print("countAns1",countAns1)
	
	countUniqueIndex=dict([(c,i) for i,c in enumerate(countUnique)])
	
	uniNums=np.empty(len(aUnique), np.int64)
	for cui,cu in enumerate(countUnique):
		countCount[cui]-=1
		num0=(countAns*countCount).sum()
		countCount[cui]+=1
		num1=countAns1[cui]
# 		print(cu,num0+num1)
		countMask=count==cu
		uniNums[countMask]=num0+num1
	
# 	print("uniNums", uniNums)
	
	nums=np.empty(n, np.int64)
	for ai,a in enumerate(aUnique):
		mask=aa==a
		nums[mask]=uniNums[ai]
	
# 	print("ok")
# 	print((main2(n,aa)==nums).all())
	print("\n".join(map(str,nums)))
	

if __name__=="__main__":
	inputs=getInputs()
# 	inputs=simInputs()
	main(inputs)

Submission status: Time Limit Exceeded

Solution code:
import collections

n  = int(input())
nums = list(map(int, input().split()))
c = collections.Counter(nums)
tap = c.most_common()

cnt = 0
for i in range(len(tap)):
    s = tap[i][1]
    cnt += s*(s-1)/2

for i in range(n):
    num = nums[i]
    a = nums.count(num)
    print(int(cnt - a +1))

Submission status: Time Limit Exceeded

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

def factor(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
memo = {}
a = collections.Counter(aa)
ans = 0
for i in range(len(aa)):
    if not aa[i] in memo:
        aaa = a[aa[i]]
        if aaa - 2 >= 0:
            sub = factor(aaa, 2)
            memo[aa[i]] = sub
        else:
            memo[aa[i]] = 0

memo2 = {}
for i in range(len(aa)):
    if not aa[i] in memo2:
        aaa = a[aa[i]] - 1
        if aaa - 2 >= 0:
            sub = factor(aaa, 2)
            memo2[aa[i]] = sub
        else:
            memo2[aa[i]] = 0

for i in range(len(aa)):
    ans = 0
    ans += memo2[aa[i]]
    for key in memo:
        if aa[i] != key:
            ans += memo[key]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
select_num = [0 for i in range(N+1)]
select_num2 = [0 for i in range(N+1)]
maxim = max(A)
count_num = [0 for i in range(maxim+1)]
for i in range(1,maxim+1):
  count_num[i] = A.count(i)
  select_num[i] = (count_num[i] - 0)*(count_num[i] - 1)//2
  select_num2[i] = count_num[i] - 1
sumS = sum(select_num)
for i in range(N):
  s = select_num2[A[i]] 
  print(sumS -s)

Submission status: Time Limit Exceeded

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

N = int(input())
A = list(map(int, input().split()))
B = A[:]
B.sort()
C = [B[0]]
D = [1]
L = 0
for i in range(N-1):
    if B[i] == B[i+1]:
        D[L] += 1
    else:
        C.append(B[i+1])
        D.append(1)
        L += 1

E = []

for i in range(len(C)):
    t = 0
    for j in range(len(C)):
        if i == j and D[j]>2:
            t += cmb(D[j]-1,2)
        if i != j and D[j]>1:
            t += cmb(D[j],2)
    E.append(t)
for i in range(N):
    print(E[C.index(A[i])])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from itertools import combinations
from collections import Counter
import math

N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#-----

A_cnt = Counter(A_list)

A_sum_of_combination={}
for k,v in A_cnt.items():
    # 個数2で場合分け
    if v >= 2:
        A_sum_of_combination[k] = math.factorial(v) // (math.factorial(v - 2) * math.factorial(2))
    else:
        A_sum_of_combination[k] = 0
    
sum_all = sum( [ v for k,v in A_sum_of_combination.items()] )

for i in A_list:
    num_selected = A_cnt[i] - 1
    
    if num_selected >= 2:
        cnt_selected = math.factorial(num_selected) // (math.factorial(num_selected - 2) * math.factorial(2))
        ans = sum_all - A_sum_of_combination[i] + cnt_selected
        print(ans)
    else:
        ans = sum_all - A_sum_of_combination[i]
        print(ans)