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

ans = 0
for i in range(N):
    S = A[:]
    del(S[i])
    for j in range(N -1):
        for k in range(j+1,N -1):
            if(S[j] == S[k]):
                ans += 1
            else:
                pass
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(input().split())
for i in range(N):
  po = A.pop(i)
  #print('tmp:',tmp)
  sumcounter = 0
  counter = Counter(A)
  #print('counter:',counter)
  for j in counter.keys():
    n = counter.get(j)
    #print('n:',n,'j:',j)
    sumcounter += n*(n-1)/2
  A.insert(i,po)
  print(int(sumcounter))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections

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)
    c = collections.Counter(A_sub)
    for j in c:
        # val = uni_vals[0]
        n = c[j]
        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: Time Limit Exceeded

Solution code:
def fun(n):
    return (n*(n+1))//2
n = int(input())
a = list(map(int,input().split()))
d = {}
for i in range(len(a)):
    d[a[i]] = 0
for i in range(len(a)):
    d[a[i]] += 1
for i in range(len(a)):
    ans = 0
    d[a[i]] -= 1
    for j in d:
        ans += fun(d[j]-1)
    print(ans)
    d[a[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from itertools import permutations, combinations
N = int(input())
List = list(map(int,input().split()))
dic = defaultdict(int)
for item in List:
  dic[item] += 1
for item in List:
  Answer = 0
  for i in dic:
    if i != item:
      if dic[i] >= 2:
        Answer += len(list(combinations(range(dic[i]),2)))
    else:
      if dic[i] >= 3:
        Answer += len(list(combinations(range(dic[i]-1),2)))
  print(Answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
import math
import collections
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for k in range(n):
    count = 0
    tmp = a.copy()
    tmp.pop(k)
    c = collections.Counter(tmp)
    print(int(sum([combinations_count(i, 2) for i in c.values() if i>1])))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
*a,= map(int, input().split())
count={}
for i in range(N):
  count[a[i]]=0
for i in range(N):
  count[a[i]]+=1

def ncn(n):
  return n*(n-1)//2

def without(a,k):
  cc = 0
  for key in count.keys():
    kc = count[key]
    if key==a[k]:
       kc-=1
    if kc>1:
       cc+=ncn(kc)
  return cc
   
for k in range(N):
  print(without(a,k))

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  a=list(map(int,input().split()))
  dic={}
  for i in a:
    dic[i]=0
  for i in a:
    dic[i]+=1
  for i in list(dic):
    if dic[i]==1:
      dic.pop(i)
  for i in range(n):
    ans=0
    if a[i] in dic:
	    dic[a[i]]-=1
    if not dic:
      ans=0
    else:
      for j in dic:
        k=dic[j]
        if k>1:
          ans+=k*(k-1)//2
    print(ans)
    if a[i] in dic:
	    dic[a[i]]+=1

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
n = int(input())
l = list(map(int,input().split()))
c=Counter(l)
for i in l:
  count=0
  n = c[i]-1
  if n>1:
    count+=math.factorial(n)/(math.factorial(n-2)*2)
  for z in c:
    if z!=i:
        x=c[z]
        if x>1:
            count+=math.factorial(x)/(math.factorial(x-2)*2)
  print(int(count))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
Ss = set(a)
s = list(Ss)
#import collections
#c = collections.Counter
j = 0
for i in range(len(s)):
  j += int(a.count(s[i])*(a.count(s[i])-1)/2)
#print(j)
for i in range(n):
  if a.count(a[i]) >= 2:
    print(int(j-(a.count(a[i])-1)))
  else:
    print(j)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
for i in range(n):
    ans = 0
    #print("a")
    a = []
    for j,k in enumerate(s):
        if j == i:
            continue
        elif not k in a:
            if s[i] == k:
                p = (s.count(k)-2)*(s.count(k)-1)
                ans += p//2
            else:
                p = s.count(k)*(s.count(k)-1)
                ans += p//2
            a.append(k)
            #print(ans,p,j)
#    print(s)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_set = list(set(A))
Ad = {}
for a_set in A_set:
  Ad[a_set] = A.count(a_set)
  
score_sum = 0
for x in list(Ad.values()):
  score_sum += (x>1) * x * (x-1) / 2

Ad_score = {}
for ad_key, ad_value in Ad.items():
  Ad_score[ad_key] = score_sum - (ad_value -1) * (ad_value > 0)

for a in A:
  print(str(int(Ad_score[a])))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import copy

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

for i in range(len(c)):
  ans = 0
  d = copy.copy(c)
  del d[i]
  for j in range(len(d)):
    e = copy.copy(d)
    del e[j]
    for k in range(len(e)):
      if d[j] == e[k]:
        ans += 1
  print(int(ans/2))

Submission status: Time Limit Exceeded

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

for i in range(N):
    ans = 0
    B=A.copy()
    del B[i]
    for b in B:
        ans += (B.count(b)-1)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
# ABC159 D

import math

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

    for i in range(n):
        newList = A[:i] + A[i+1:n]
        result = 0

        elements = set(newList)

        for elem in elements:
            result += combinations_count(newList.count(elem), 2)
        
        print(result)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
N = int(input())
A = list(map(int,input().split()))
A_set = list(set(A)) #重複を削除
A_dict = dict()
for i in range(len(A_set)):
    count = 0
    for j in range(len(A)):
        if A_set[i] == A[j]:
            count += 1
    A_dict[A_set[i]] = count
# 各キーが選ばれたときの答えを求めてdict型で持っておく
ans = dict()
for i in range(len(A_set)):
    count = 0
    A_dict_2 = copy.deepcopy(A_dict) # 辞書をオリジナルからコピー
    A_dict_2[A_set[i]] -= 1 # そのキーを取り出した想定
    # print(A_set[i],':',A_dict_2)
    for k,v in A_dict_2.items():
        if v >= 2:
            count += int(math.factorial(v)/(2*math.factorial(v-2)))
    ans[A_set[i]] = count

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

Submission status: Time Limit Exceeded

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

MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

N = int(input())
A = list(map(int, input().split()))
dic = {}
for a in A:
    if a in dic: dic[a] += 1
    else: dic[a] = 1

C = {0:0}

sum_ni = 0
for i in range(1, 2*10**5+1):
    C[i] = i*(i-1)//2

ans2 = {}
for k, v in dic.items():
    sum_ni += C[v]



ans = []
for i in range(N):
    dic2 = dic.copy()
    dic2[A[i]] -= 1
    a = sum_ni - C[dic[A[i]]] + C[dic[A[i]]-1]

    ans.append(a)


for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
#print(counter)
d = {}
for key, value in counter.items():
    d[key] = (value * (value - 1)) // 2
#print(counter)

#print('d:', d, end='\n\n')

for k in range(n):
    d2 = d.copy()
    '''
    print('d2:', d2)
    print(f'a[{k}]:', a[k], end=', ')
    print(f'd[{a[k]}]:', d[a[k]])
    print('計算')
    print(f'(counter[a[k]] - 1):{(counter[a[k]] - 1)}')
    print(f'(counter[a[k]] - 2): {(counter[a[k]] - 2)}')
    '''
    
    if d[a[k]] > 0: d2[a[k]] = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
    #print(f'd2({a[k]}除く):', d2)
    print(sum(d2.values()))

Submission status: Time Limit Exceeded

Solution code:
import collections
from operator import mul
from functools import reduce
mod = 10**9 + 7
def cmb(n,r,mod):
	a=1
	b=1
	r = min(r,n-r)
	for i in range(r):
		a = a*(n-i)%mod
		b = b*(i+1)%mod
	return a*pow(b,mod-2,mod)%mod

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

for k in range(N):
  A[k] -= (10**6)
  C = collections.Counter(A)
  A[k] += (10**6)
  values, counts = zip(*C.most_common())
  ans = 0
  for i in range(len(counts)):
    if counts[i] == 1:
      pass
    else:
      ans += cmb(counts[i],2,mod)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
A = list(map(int, input().split()))
c = 0
for j in range(N):
  B = copy.copy(A)
  B[j] = 0
  cnt = 0
  for i in range(N):
    c = B.count(i+1)
    if c <= 1:
      cnt += 0
    else:
      cnt += int((c * (c - 1) / 2))
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
def count_ident(v):
    c = collections.Counter(v)
    val_list=c.values()
    v_count=0
    for k in val_list:
        if k>1:
            v_count=v_count+int(k*(k-1)/2)
        else:
            v_count=v_count
    print(v_count)

import copy
c = copy.deepcopy(a)    
for i in range(len(a)):
    tmp = copy.deepcopy(a)    
    tmp.pop(i)
    count_ident(tmp)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
if True:
    n = int(input())
    an= list(map(int, input().split()))

else:
   # n=5
   # an=[1,1,2,1,2]
    n=8
    an=[1,2,1,4,2,1,4,1]


for k in range(0,n):
    an2 = an[:]
    del an2[k]
    an2.sort()
    # print(an2)
    before = ""
    ans = 0
    same_count = 0
    for a in an2:
        if a == before:
            same_count += 1
        elif same_count > 0:
            # print(same_count)
            ans += same_count * (same_count+1)/2
            same_count = 0
        before = a
    if same_count > 0:
        ans += same_count * int((same_count+1)/2)
    print(int(ans))
    an2.clear()

Submission status: Time Limit Exceeded

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

def n_c_2(val):
  ret = 0
  if val > 1:
    ret = (val)*(val-1)//2
  return ret

org_nc2 = {}
org_minus1_nc2 = {}
ans = {}

for k in list(c.keys()):
  org_nc2[k] = n_c_2(c[k])
for k in list(c.keys()):
  org_minus1_nc2[k] = n_c_2(c[k] - 1)
for k in list(c.keys()):
  tmp = 0
  for j in list(c.keys()):
    if j==k:
      tmp += org_minus1_nc2[j]
    else:
      tmp += org_nc2[j]
  ans[k] = tmp

for d in a:
  print (ans[d])

Submission status: Time Limit Exceeded

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

tmp = []
for n in c:
    tmp.append(c[n])
tmp = np.array(tmp)
l1= tmp*(tmp-1)//2
l2= (tmp-1)*((tmp-1)-1)//2

for a in A:
    r = list(c).index(a)
    sum = np.sum(l1) + l2[r] -l1[r]
    print(int(sum))

Submission status: Time Limit Exceeded

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

d = {}
for a in A:
  d[a] = d.get(a, 0) + 1
for a in set(A):
  d[a] = (d[a], int(d[a]*(d[a]-1)/2))
  
for i in range(N):
  tmp_d = d.copy()
  tmp = tmp_d[A[i]][0] - 1
  tmp_d[A[i]] = (tmp, int(tmp*(tmp-1)/2))
  sum = 0
  for v in tmp_d.values():
    sum += v[1]
  print(sum)

Submission status: Time Limit Exceeded

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


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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

a = [i for i in list(map(int, input().split()))]

for i in range(n):
  aa = a[:]
  aa.pop(i)
  ab=np.array(aa)
  num=0
  for j in set(aa):
    ns = len(ab[ab==j])
    if ns>1:
      num += (ns*(ns-1))//2

  print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
a,b = [0]*N,[0]*N
for i in range(N):
  c = A.count(str(i+1))
  a[i] = c*(c-1)//2
  b[i] = (c-1)*(c-2)//2
d = sum(a)
for i in map(int,A.split()): print(d-a[i-1]+b[i-1])

Submission status: Time Limit Exceeded

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

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

T2 = []

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


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

for i in range(N):
    for j in range(len(T)):
        if T[j][0] == A[i]:
            T2[j] -= 1
            print(hoge(T2))
            T2[j] += 1
            break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def make_cmb_table(n):
    fact = [1]
    for i in range(1, n+1):
        fact.append(fact[i-1] * i)

    ifact = [0] * (n+1)
    ifact[n] = pow(fact[n], -1)
    for i in range(n, 0, -1):
        ifact[i-1] = ifact[i] * i
    return (fact, ifact)


def cmb(n, r, table):
    if r > n:
        return 0
    return int(table[0][n] * table[1][n - r] * table[1][r])


def main():
    n = int(input())
    a = list(map(str, input().split()))
    table = make_cmb_table(n)

    d = dict()
    for i in range(n):
        if a[i] in d:
            d[a[i]] = d[a[i]] + 1
        else:
            d[a[i]] = 1

    total = 0
    normal = dict()
    target = dict()
    for k, v in d.items():
        t = cmb(v, 2, table)
        normal[k] = t
        target[k] = cmb(v-1, 2, table)
        total += t

    for i in range(n):
        print(total - normal[a[i]] + target[a[i]])


main()

Submission status: Time Limit Exceeded

Solution code:
import copy

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

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
a=[]
A=list(map(int,input().split()))
c=collections.Counter(A)
d=c.most_common()
Ans=[]
for i in range(N):
    ans=0
    if i>0:
        for l in range(i):
            if A[i]==A[l]:
                ans=Ans[l]
                break
    if ans==0:
        for j in range(len(d)):
            if d[j][0]==A[i]:
                ans+=(d[j][1]-1)*(d[j][1]-2)//2
            else:
                ans+=d[j][1]*(d[j][1]-1)//2
    Ans.append(ans)
for i in range(N):
    print(Ans[i])

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 = {}
result_sum_all = 0
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)
    result_sum_all += int(pair_set)
    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 = result_sum_all - num_dict[P[k]] + num_dict_k[P[k]]
    print(result)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
n=int(input())
retsu=input().split()
item=list(collections.Counter(retsu).values())
key=list(collections.Counter(retsu).keys())
for i in range(len(retsu)) :
  item1=copy.deepcopy(item)
  count=0
  for j in range(len(key)):
    if retsu[i]==key[j] and item1[j]>1:
      item1[j]-=1
      break
 
  
  for youso in item1 :
  	count+=int(youso*(youso-1)/2)
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math


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


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

summ = 0
for v in c.values():
    summ += comb(v, 2)

for i in range(n):
    print(summ - comb(c[a[i]], 2) + comb(c[a[i]] - 1, 2))

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
NC = [0] * N
NC_minus = [0] * N
for i,c in enumerate(counts):
  if c>=2:
    NC[i] = int(c * (c-1) * 0.5)
  if c>=3:
    NC_minus[i] = int((c-1)*(c-2)*0.5)


for i in range(len(A)):
  cnt = 0
  for j in range(len(counts)):
    t = NC[j] if A[i]!=(j+1) else NC_minus[j]
    cnt += t
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

N = int(input())
As = input().split()
A = [0 for i in range(N)]
for i in range(N):
  A[i] = int(As[i])

# relabel A's elements
A_unique = list(set(A))
to_unique = {}
for i,a in enumerate(A_unique):
  to_unique[a] = i
for i in range(N):
  A[i] = to_unique[A[i]]

# count number of each label appearing
counts = [0 for i in range(len(A_unique))]
for i,A_i in enumerate(A):
  counts[A_i] += 1

answer = [-1 for i in range(N)]
spoiler = {}
for k in range(N):
  if A[k] in spoiler:
    answer[k] = spoiler[A[k]]
  else:
    appnums = copy.copy(counts)
    appnums[A[k]] -= 1
    answer[k] = 0
    for appnum in appnums:
      if appnum>1: answer[k] += int(appnum*(appnum-1)/2)
    spoiler[A[k]] = answer[k]

for k in range(N):
  print(answer[k])

Submission status: Time Limit Exceeded

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1


d_c = {}
for i in d:
    d_c[i] = d[i] * (d[i] - 1) // 2


d_3 ={}

for i in range(n):
    b = a[i]
    count = 0
    if b not in d_3:
        for j in d:
            if j != b:
                count += d_c[j]
            else:
                count += d_c[j]*(d[j]-2)//d[j]
        d_3[b] = count
        print(count)
    else:
        print(d_3[b])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = sorted(set(A))
dic = {}
ans = 0

for i in range(len(B)):
    if A.count(B[i]) > 1:
        C = A.count(B[i])
        dic[B[i]] = C
        ans += C*(C-1)//2
    else:
        dic[B[i]] = 1
        
for i in range(len(B)):
    x = dic[B[i]]
    dic[B[i]] = ans + 1 - x

for i in range(n):
    print(dic[A[i]])

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
l=list(map(int,input().split()))
d={}
for i in l:
    if i in d:
        d[i]+=1 
    else:
        d[i]=1 
count=0
for k in d.keys():
        x=d[k]
        if x<2:
            count+=0
        else:
            count+=math.factorial(x)/(math.factorial(x-2)*2)

for i in l:
    x=d[i]
    #print(i, x)
    if x>=2:
        val=math.factorial(x)/(math.factorial(x-2)*2)
        ans=count-val
        if x>=3:
            val1=math.factorial(x-1)/(math.factorial(x-3)*2)
        else:
            val1=0
        ans+=val1
    else:
        if x==2:
            ans=count-1 
        else:
            ans=count 
    print(int(ans))

Submission status: Time Limit Exceeded

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

p = 7+pow(10,9)
def cmb(n,r,p):
    numerator = 1
    for i in range(n-r+1, n+1):
        numerator = numerator * i % p
    denominator = 1
    for i in range(1, r+1):
        denominator = denominator * i % p
    return numerator * pow(denominator, p-2, p) % p


b = []
c = 0
for i in range(n):
    if not a[i] in b:
        b.append(a[i])
        if a.count(a[i]) == 1:
            continue
        else:
            c += cmb(a.count(a[i]), 2, p)

for i in range(n):
    if a.count(a[i]) == 1:
        print(c)
    elif a.count(a[i]) == 2:
        print(c-1)
    else:
        print(c-(a.count(a[i])-1))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
data=list(map(int, input().split(' ')))
A={}
def check(DIC):
    n=0
    for value in DIC.values():
        n+=int(value*(value-1)/2)
    
    return n


for value in data:
    if value in A:
        A[value]+=1
    else:
        A[value]=1

#print(A)
#B=A
#print(check(A))

for value in data:
    A[value] -= 1
    print(check(A))
    A[value] +=1

Submission status: Time Limit Exceeded

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1


d_c = {}
for i in d:
    d_c[i] = d[i] * (d[i] - 1) // 2


d_3 ={}

for i in range(n):
    b = a[i]
    count = 0
    if b not in d_3:
        for j in d:
            if j != b:
                count += d_c[j]
            else:
                count += d_c[j]*(d[j]-2)//d[j]
        d_3[b] = count
        print(count)
    else:
        print(d_3[b])

Submission status: Time Limit Exceeded

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

from collections import defaultdict

adict = defaultdict(int)

resdict = {}

for a in alist:
    adict[a] += 1

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
answer = [-1] * N
p = []
for i in range(N):
  count[i]  = A.count(i + 1)
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  if answer[A[i] - 1] != -1:
    p.append(answer[A[i] - 1])
  else:
    tekito = count[A[i] - 1]
    if tekito == 2:
      s = nanimo - 1
    elif tekito == 1:
      s = nanimo
    else:
      s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
    p.append(s)
    answer[A[i] - 1] = s

for i in p:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import collections
import copy

def calcPair(ballNum):
    if ballNum -1 <= 0:
        return 0
    else:
        return int(ballNum * (ballNum -1) / 2)

def banK(a, k):
    result = 0
    #print(a)
    #print(k)
    a_tmp = copy.deepcopy(a)
    a_tmp.pop(k)
    c = collections.Counter(a_tmp)
    #print(c.values())
    for i in c.values():
        result += calcPair(i)
    print(result)

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

result_sum = 0
for i in range(n):
    banK(a, i)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

n = int(input())
a = list(map(int, 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:
from collections import Counter

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

for b in box:
    c[b] -= 1
    cnt = 0
    for k in c.keys():
        #print('k, c[k]: ',k ,c[k])
        if c[k] != 1:
            cnt += int(1/2 * c[k] * (c[k] - 1))
    print(cnt)
    c[b] += 1

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import mul
import math
 
def cmb(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under
n = int(input())
l = list(map(int, input().split()))
d = {}
s_sum = {}
s_ans = {}
s = list(set(l))
m = 0
for i in range(len(s)):
    v = l.count(s[i])
    d[s[i]] = v
    if v > 1:
        a = cmb(v, 2)
        s_ans[s[i]] = a
        m += a
    else:
        s_ans[s[i]] = 0
for i in range(len(l)):
    if not d[l[i]] > 1:
        print(m)
    elif l[i] in s_sum:
        print(s_sum[l[i]])
    else:
        a = d[l[i]] - 1
        ans = m - s_ans[l[i]]
        if a > 1:
            ans += cmb(a, 2)
        s_sum[l[i]] = ans
        print(ans)

Submission status: Time Limit Exceeded

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

# define combination
fact = [1, 1]
for i in range(2, N+1):
    fact.append(fact[-1]*i)
combs = [0, 0]
for n in range(2, N+1):
    combs.append(fact[n] // (fact[n - 2] * 2))

# process
nums = [0]*(N+1)
for a in A:
    nums[a] += 1

sumall = 0
for n in nums:
    sumall += combs[n]

ans = []
for i, a in enumerate(A):
    ans.append(sumall - combs[nums[a]] + combs[nums[a]-1])

# output
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:

def select2( val ):
  return( val*(val-1) // 2 ) 

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


ielms = []
for elm in elms:
  ielms.append( int(elm ))



test_dict = {}

for ielm in ielms:
  if( ielm in test_dict.keys() ):
    test_dict[ielm] += 1
  else:
    test_dict[ielm] = 1
               
for ielm in ielms:
  total = 0
  for dkey in test_dict.keys():
    if( ielm == dkey ):
      total += select2( test_dict[dkey]-1 )
    else:
      total += select2( test_dict[dkey] )
  print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
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 += int(c * (c - 1) / 2)
  print(ans)
  A.insert(i, tmp)

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,combs,total={},{},0
for a in set(A):
  cnt[a]=A.count(a)
  tmp=comb(cnt[a],2)
  combs[a]=tmp
  total+=tmp
for a in A:
  print(total-combs[a]+comb(cnt[a]-1,2))

Submission status: Time Limit Exceeded

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

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

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

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

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

import collections
for i in range(n):
    s = a[i]
    a.pop(i)
    c =collections.Counter(a)
    ln = [v for k, v in c.items() if v >= 2]
    ans = 0
    for j in ln:
        ans += cmb(j, 2)
    a.insert(i, s)
    print(ans)

Submission status: Time Limit Exceeded

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

#if aa.ndim == 0:
#    aa = [aa]    
    
for a in aa:
    print(int(total - dic[a] + 1))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
c_lst = []
c = collections.Counter(a)
for i in c.most_common()[::-1]:
    if i[1] >= 2:
        c_lst.append(list(i))
if c_lst:
    values, counts = zip(*c_lst)
else:
    values = []

cnt = 0
for i in range(len(c_lst)):
    cnt += (c_lst[i][1] * (c_lst[i][1] - 1)) // 2
    
for i in a:
    ans = 0
    if i in values:
        c_lst[values.index(i)][1] -= 1
        for k in range(len(c_lst)):
            if c_lst[k][1] >= 2:
                ans += (c_lst[k][1] * (c_lst[k][1] - 1)) // 2
        c_lst[values.index(i)][1] += 1
    else:
        ans = cnt
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from sys import stdin
import math


def combinations_count(n, r):
    if n < 2:
        return 0
    return n*(n-1)//2


N = int(input())
A = list(map(int, input().split()))
# print(A)
d = {}
cnt = 0
for l in range(N):
    for i in range(N):
        if i != cnt:
            d[A[i]] = d.get(A[i], 0)+1
    # print(d)
    # print(len(d))
    sum = 0
    for key, value in enumerate(d):
        # print(key,value)
        sum = sum+combinations_count(d.get(value, 0), 2)
    print(sum)
    d = {}
    cnt += 1