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:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect
import heapq


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

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


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#V, E, r = map(int, input().split())
#INF = V * 10001
#A = [int(i) for i in input().split()]


N = int(input())
A = [int(i) for i in input().split()]
dp = [0]*(N+1)
ans = 0
a = [0]*N
minus = 0

for i in range(N):
  a[i] = A.count(A[i])
  if(a[i]<2):
    dp[A[i]] = 0
  else:
    dp[A[i]] = combinations_count(a[i], 2)

ans = sum(dp)

#print(dp)
for i in range(N):
  temp = dp[A[i]]
  if(a[i]==2):
    minus = temp-1
  elif(a[i]<2):
    minus = temp
  else:
    minus = combinations_count(a[i]-1, 2)
  print(int(ans-(temp-minus)))

#print(ans)

Submission status: Time Limit Exceeded

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

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

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

    ans_dict = {}
    for k in ball_dict:
        ans = 0
        for l in ball_dict:
            if k == l:
                ans += (ball_dict[l] - 1) * (ball_dict[l] - 2) // 2
            else:
                ans += ball_dict[l] * (ball_dict[l] - 1) // 2
        ans_dict.setdefault(k, ans)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys


import sys
import itertools
input = sys.stdin.readline

N = int(input())
A=list(map(int, input().split()))
ans = 0
for i in range(N):
    S = A[:]
    del(S[i])
    for j,k in itertools.combinations(S,2):
        if (j == k):
            ans += 1
        else:
            pass
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

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

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

dic = {}

for i in A:
    if dic.get(i) == None:
        dic[i] = 0
    dic[i] += 1
for i in range(N):
    cnt = 0
    for item in dic.keys():
        if item == A[i]:
            cnt += (dic[item]-1)*(dic[item]-2)/2
        else:
            cnt += (dic[item])*(dic[item]-1)/2
    print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
import itertools

if __name__ == "__main__":
    N = int(input())
    A = input().split()
    for x in range(N):
        tmp = [y for idx, y in enumerate(A) if idx != x]
        result = tuple(filter(lambda t: t[0] == t[1], itertools.combinations(tmp, 2)))
        print(len(result))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
A = list(map(int, input().strip().split()))
U = max(A) + 3
c = np.zeros((U),dtype=np.int64)
for a in A:
    c[a] += 1
#print(c)
x = np.array(range(1,c.max() + 3))
y = np.array(range(0,c.max() + 2))
q = x * y / 2
q = q.astype(np.int64)
#print(q)

s = 0
dex = np.where(c>0)
for i in dex[0]:
    s += q[c[i]-1]
#print(s)
for i in range(n):
    #print(A[i],q[max(0,c[A[i]]-1)],q[max(0,c[A[i]]-2)])
    print(s - q[max(0,c[A[i]]-1)] + q[max(0,c[A[i]]-2)])

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())

A_list = list(map(int,input().split()))
#print(A_list)

c = collections.Counter(A_list)
#print(c)

for k in range(N):
    #temp = c[A_list[k]]
    temp = A_list[k]
    count = 0
    #print(temp)
    #print("k ", k, " temp ", temp)
    for key in c.keys():
        v = c[key]
        #print("key ", key)
        if key != temp:
            count += v * (v-1)/2
            #print("key!=temp ", v, count)
        elif key == temp and v > 1:
            count += (v-1) * (v-2)/2
            #print("key==temp ", v, count)
    
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

n = int(input())

an = list(map(int, input().split()))

an_np = np.array(an)

sum = 0
for i in range(n):
    an_cp = an_np.copy()
    an_cp[i] = -1
#     u, count = np.unique(an_cp, return_counts=True)
    u = np.unique(an_cp)
    cn = 0
    for un in u:
#         print(un)
        uni = len(np.where(an_cp == un)[0])
#         print(uni)
        if (uni > 1):
            cn += uni * (uni - 1) / 2
    print(int(cn))

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)

        if elements == newList:
            print(result)
            continue
        elif len(elements) == 1:
            print(combinations_count(len(newList), 2))
            continue

        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 copy
import collections
import numpy as np

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)

for j in range(N):
  B = copy.copy(A)
  B[j] = 0
  cnt = 0
  c = collections.Counter(B)
  c = np.array(list(map(int, c.values())))
  c = (c * (c - 1) / 2)
  print(int(np.sum(c)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

N=int(input())
A=deque(map(int,input().split()))
setA=set(A)
number1={}#-1
number2={}
ans={}
if(len(setA)==1):
    answer= len(A) - 1
    q=(answer - 1) * answer //2
    for _ in range(len(A)):
        print(q)
    sys.exit()


if(len(setA)==len(A)):
    for _ in range(len(A)):
        print(0)
    sys.exit()


for i in setA:
    tmp=A.count(i) - 1
    souwa=(tmp - 1)*(tmp)//2
    number1[i]=souwa
    souwa=tmp*(tmp+1)//2
    number2[i]=souwa

t=sum(number2.values())

for a in setA:
    total=number1[a]
    total+=t
    total-=number2[a]
    ans[a]=total

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    nLst = lst.copy()
    nLst.pop(i)
    cnLst = [x for x in set(nLst) if nLst.count(x) > 1]
    cnt = 0
    for cn in cnLst:
        cnnLst = [x for x in nLst if x == cn]
        cnt += len(cnnLst) * (len(cnnLst)-1) / 2
    print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

N = int(input())
A = list(map(int, input().split()))
  
c = collections.Counter(A)
c_keys = list(c.keys())
c_values = list(c.values())
ans_list = []
temp_ans = 0
for i in range(len(c_keys)):
  temp_ans += c_values[i] * (c_values[i]-1)
temp_ans = int(temp_ans/2)
for i in range(len(c_values)):
  ans_list.append(temp_ans - (c_values[i]-1))

for i in range(N):
  print(ans_list[c_keys.index(A[i])])

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
A = [int(s) for s in input().split()]
B = [0 for i in range(N)]
idx = []
for i in range(N):
  B[A[i]-1] += 1
  if B[A[i]-1] >= 2 and not A[i]-1 in idx:
    idx.append(A[i]-1)

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

Cs = []
C = []
Ctot = 0
for j in idx:
  if B[j]-1 >= 2:
    Cs.append(nCr(B[j]-1, 2))
  else:
    Cs.append(0)
  tmp = nCr(B[j], 2)
  C.append(tmp)
  Ctot += tmp
for i in range(N):
  current_idx = A[i]-1
  if current_idx in idx:
    count = Ctot + Cs[idx.index(current_idx)]-C[idx.index(current_idx)]
  else:
    count = Ctot
  print(count)

Submission status: Time Limit Exceeded

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

    for i in range(N):
        ans = 0
        a = A[:i]+A[i+1:]
        C = collections.Counter(a)
        for v in C.values():
            ans += (v*(v-1))//2
        print(ans)
        
main()

Submission status: Time Limit Exceeded

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

l = [0] * n

for i in range(n):
    #print('a=' + str(a[i]))
    l[a[i]-1] += 1

for i in range(n):
    ans = 0
    for j in range(n):
        tmp = l[j]
        #print(tmp)
        if (j + 1) == a[i]:
            tmp -= 1

        ans += int((tmp * (tmp-1))/2)
    #    print('ans=' + str(ans))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

cmb2 = [0] * 200001
for n in range(2, 200001):
    num = 1
    r = 2
    for i in range(1, r + 1):
        num = num * (n - i + 1) // i
    cmb2[n] = num

n = int(input())
a = list(map(int, input().split()))
nn = len(a) - 1
ctr = Counter(a)

for aa in a:
    x = 0
    for k, v in ctr.items():
        if k == aa:
            v -= 1
        if v >= 2:
            x += cmb2[v]
    print(x)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


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

def calc(a, i):
    _a = a[:i] + a[i+1:]
    set_a = set(_a)
    res = sum(map(lambda x: combinations_count(_a.count(x), 2), set_a))
    return res

input()
s = list(map(int, input().split(' ')))

for i in range(len(s)):
    print(calc(s, i))

Submission status: Time Limit Exceeded

Solution code:
import collections
from operator import mul
from functools import reduce
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:
      r = min(2, j[1] - 2)
      numer = reduce(mul, range(j[1], j[1] - r, -1), 1)
      denom = reduce(mul, range(1, r + 1), 1)
      ans += numer // denom
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

C = list(set(A))
M = len(C)
t = []
t1 = []
t2 = []
for i in range(M):
  t.append(A.count(C[i]))
  temp = A.count(C[i])
  if temp >=3:
    t1.append(math.factorial(temp-1)/(math.factorial(temp-3)*2))
    t2.append(math.factorial(temp)/(math.factorial(temp-2)*2))    
  elif temp == 2:
    t1.append(0)
    t2.append(math.factorial(temp)/(math.factorial(temp-2)*2))
  else:
    t1.append(0)
    t2.append(0)

#print(t,t1,t2)


s = []  
for i in range(M):
  ans = 0
  for j in range(M):
    if i == j:
      ans += t1[j]
    else:
      ans += t2[j]
  s.append(int(ans))

jisyo = dict(zip(C,s))
#print(jisyo[1])
for i in range(N):
  print(jisyo[A[i]])

Submission status: Time Limit Exceeded

Solution code:
import collections
from operator import mul
from functools import reduce

def cmb(n,r):
    if n == 1: return 0
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    N = int(input())
    alist = list(map(int, input().split()))

    ans = {}
    for i in range(len(alist)):
        if alist[i] in ans:
            if i == len(alist)-1:
                print(ans[alist[i]], end='')
            else:
                print(ans[alist[i]])
            continue

        tmp = alist[:]
        tmp.pop(i)

        count = 0
        for v in collections.Counter(tmp).values():
            count += cmb(v, 2)

        if i == len(alist)-1:
            print(count, end='')
        else:
            print(count)
        ans[alist[i]] = count

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

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

for i in range(0, len(A)):
    B = copy.copy(A)
    B.pop(i)
    c = collections.Counter(B)
    d = [i for i in c.values()]
    d = [n for n in d if n!=1]
    Z = 0
    for j in d:
        Z += sum([i for i in range(0,j)])
    print(Z)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

for k in range(N):
  sum_k = 0
  temp_c = c.copy()
  temp_c[A[k]] -= 1
  for v in temp_c.values():
    if v > 1:
      sum_k += cmb(v,2)
  print(int(sum_k))

Submission status: Time Limit Exceeded

Solution code:
# 200322 D

import collections

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


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

cmblist = {}
cmblistl = {}
for i in gAc:
    if gAc[i] <= 1:
        cmblist[i] = 0
    else:
        cmblist[i] = cmb(gAc[i],2)
        if gAc[i] > 1:
            cmblistl[i] = cmb(gAc[i]-1,2)
        else:
            cmblistl[i] = 0
        
for i in range(len(A)):
    bAc = {}
    for k in gAc:
        bAc[k] = gAc[k]
    Ac = bAc
    Ac[A[i]] -= 1
    cmbsum = 0
    for j in Ac:
        if Ac[j] > 1:
            if gAc[j] != Ac[j]:
                cmbsum += cmblistl[j]
            else:
                cmbsum += cmblist[j]
    print(cmbsum)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
 
def combinations_count(n, r):
  if n == r:
    return 1
  elif n < r:
    return 0
  else:
    return 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)
combi_count = {}
calc_dict = {}
total = 0
for k, v in combi.items():
  if v in calc_dict:
    combi_count[k] = calc_dict[v]
  else:
    combi_count[k] = combinations_count(v, 2)
    calc_dict[v] = combi_count[k]
  total += combi_count[k]

for i in range(N):
  ans = total - combi_count[A[i]] + combinations_count(combi[A[i]]-1, 2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
import collections

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

for i in range(n):
    _a = np.delete(a, i)
    c_dict = collections.Counter(_a)
    total = 0
    for v in c_dict.values():
        if v >= 2:
            total += v * (v - 1) / 2
            
    print(int(total))

Submission status: Time Limit Exceeded

Solution code:
def cmb(n):
	if n<=1:
		return 0
	else:
		return int(n*(n-1)*0.5)
N = int(input())
A = [int(x) for x in input().split()]
ans=[0]*N
lists={}
k=0
for i in range(N):
  if not A[i] in lists:
    l=cmb(A.count(A[i]))
    m=cmb(A.count(A[i])-1)-l
    k+=l
    lists[A[i]]=m
    ans[i]=m
  else:
    ans[i]=lists[A[i]]
    
for i in range(N):
  ans[i]+=k
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
n = int(input())
items = tuple(map(int, sys.stdin.readline().rstrip().split()))


com = [-1] * (n+1)
sumc = 0
que = deque([])
seen = set()
for i in items:
    if com[i] != -1:
        nn = com[i]
    else:
        nn = items.count(i)
        com[i] = nn
    que.append(nn)
    if i in seen:
        continue
    seen.add(i)
    sumc += nn*(nn-1)//2

while que:
    q = que.popleft()
    if q <2:
        print(sumc-q*(q-1)//2)
    else:
        print(sumc-q+1)

Submission status: Time Limit Exceeded

Solution code:
import collections
#import numpy as np

N = int(input())
arr = list(input().split(" "))
#val, count = np.unique(arr, return_counts=True)
count = collections.Counter(arr)
conv_before = [int(x * (x-1) /2) for x in count.values()]

#conv_before = (count * (count - 1) / 2).astype(int)
conv_before_sum = sum(conv_before)

for i in range(len(arr)):
    k_val = arr[i]
    n_count_k = count[k_val] - 1
    n_conv = int(n_count_k * (n_count_k -1) /2)
    conv = conv_before_sum - conv_before[list(count.keys()).index(k_val)] + n_conv

    #3conv = (count * (count - 1) / 2).astype(int)
    print(conv)

Submission status: Time Limit Exceeded

Solution code:
size = int(input())
values = list(map(int, input().split()))
skip_map = {}
count_map = {}
count_map_2 = {}

# 先に各数値とその存在数をカウントしておく
for i in range(size):
    if str(values[i]) in count_map:
        count_map[str(values[i])] = count_map[str(values[i])] + 1
    else:
        count_map[str(values[i])] = 1

for key in count_map.keys():
    value = count_map[key]
    if str(value) in count_map_2:
        count_map_2[str(value)].append(key)
    else:
        count_map_2[str(value)] = [key]


for i in range(size):
    if str(values[i]) in skip_map:
        print(skip_map[str(values[i])])
        continue
    check_int = count_map[str(values[i])]
    checks = count_map_2[str(check_int)]
    for check in checks:
        if str(values[i]) in skip_map:
            print(skip_map[str(values[i])])
            break
    else:
        # 集計
        calc_map = {}
        for j in range(size):
            if i == j:
                continue
            if str(values[j]) in calc_map:
                calc_map[str(values[j])] = calc_map[str(values[j])] + 1
            else:
                calc_map[str(values[j])] = 1
        # パターン
        result = 0
        for val in calc_map.keys():
            if 1 < calc_map[val]:
                for k in range(calc_map[val]):
                    result = result + k
        print(result)
        skip_map[str(values[i])] = result

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.misc import comb

N = int(input())
AList = input().split(" ")
tempList = collections.Counter(AList)

for k in range(N):
    count = 0

    for key, value in tempList.items():
        if k <= len(AList) and key == AList[k]:
            value -= 1

        count += comb(value, 2, exact=True)
    
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(a)

for i in range(n):
    r = 0
    k = a[i]
    c[k] = c[k]-1
    for j in c:
        if c[j] > 1:
            r = int(r + (c[j] * (c[j] - 1) / 2))
    print(r)
    c[k] = c[k]+1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

a_list = list(map(int, input().split()))

ans_dict = {}
value_dict = {1: 0}
for i in range(n):
    num = a_list[i]
    if num in ans_dict.keys():
        print(ans_dict[num])
    else:
        buff = a_list[:i]
        buff.extend(a_list[i+1:])
        c = Counter(buff)

        tmp_ans = 0
        for value in c.values():
            if value in value_dict.keys():
                tmp_ans += value_dict[value]
            else:
                tmp = int((value * (value - 1))/2)
                tmp_ans += tmp
                value_dict[value] = tmp
        ans_dict[num] = tmp_ans
        print(tmp_ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
#from math import factorial
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())
s = list(map(str, input().split()))
ss = ''.join(s)
#print(ss)
for i in range(n):
  sss = ss[:i] + ss[i+1:]
  c = collections.Counter(sss)
  #print(c)
  count=0
  for v in c.values():
    if v >= 2:
      vv = cmb(v, 2)
      count += vv
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

l = (int)(input())
a = map(int, input().split())
al = list(a)

for i in range(l):
    al2 = copy.deepcopy(al)
    al2.pop(i)
    c = collections.Counter(al2)
    #print("{}".format(c))
    ret = 0
    for j in c.most_common():
       ret = ret + (int)((j[1] * (j[1]-1)) / 2)
    print("{}".format(ret))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
N=int(input())
A=list(map(int,input().split()))
_A=list(set(A))
l_A=len(_A)
for j in range(1,N+1):
    B=A[:j-1]+A[j:]
    print(sum([comb(B.count(_A[i]),2,exact=True) for i in range(l_A)]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
a = []
answer = 0
for i in s:
    if not i in a:
        ans[i] = s.count(i)*(s.count(i)-1)//2
        a.append(i)
        answer += ans[i]
#print(ans)

#print(answer)
for i in range(n):
    print(answer-ans[s[i]]+((ans[s[i]]*2//s.count(s[i]))*(s.count(s[i])-2)//2))

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
listA = list(map(int, input().split()))
# 各数字の出現回数をカウント
countA = collections.Counter(listA)
countA_calc = {}
countA_calc2 = {}
for i in listA:
    count = 0
    for x, y in countA.most_common():
        if x == i:
            if x not in countA_calc2:
                countA_calc2[x] = (y -1) * (y - 2) // 2
            count += countA_calc2[x]
        else:
            if x not in countA_calc:
                countA_calc[x] = y * (y -1) // 2 
            count += countA_calc[x]
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(map(int, input().split()))
# A = [1, 2, 3] * 10000
B = set(A)
d = dict()
al = dict()
ALL = 0
from collections import Counter

counter = Counter(A)
# for b in B:
#     k = A.count(b)
#     d[b] = k
#     al[b] = k * (k - 1) // 2
#     ALL += al[b]
for k, v in counter.items():
    al[k] = v * (v - 1) // 2


kl = dict()
for b in B:
    kl[b] = sum(al.values()) - counter[b] + 1

print("\n".join(list(str(kl[a]) for a in A)))

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
integer = list(map(int, input().split()))
length = len(integer)
count = 0
for i in range(length):
  tmp = copy.copy(integer)
  del(tmp[i])
  for j in range(length-2):
    for k in range(j+1, length-1):
      if tmp[j] == tmp[k]:
        count += 1      
  print(count)
  count = 0

Submission status: Time Limit Exceeded

Solution code:
import math
def comp(n, r):
    return math.factorial(n) // (math.factorial(abs(n-r))*math.factorial(r))
def pp(n, s):
    cnt, cnt2 = 0, 0
    new = s[1:]
    new_2 = sorted(new)
    while (cnt2 < len(new_2)):
        cnt = cnt + comp(new_2.count(new_2[cnt2]), 2)
        cnt2 = cnt2 + new_2.count(new_2[cnt2])
    print(cnt)
    for i in range(1,n):
        cnt = 0
        cnt2 = 0
        new = s[:i] + s[i+1:]
        new_2 = sorted(new)
        while(cnt2 < len(new_2)):
            cnt = cnt + comp(new_2.count(new_2[cnt2]),2)
            cnt2 = cnt2 + new_2.count(new_2[cnt2])

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

Submission status: Time Limit Exceeded

Solution code:
def fact(k):
    val = 1
    for i in range(2, k+1):
        val = val*i
    return val

n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    temp = a[:]
    temp[k:k+1] = []
    ms = set(temp)
    total = 0
    for m in ms:
        times = temp.count(m)
        if times < 2:
            continue
        total += fact(times) / (fact(2) * fact(times-2))
    print(int(total))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
k = list(map(int, input().split()))
d = {}
dd = {}
for i in range(N):
    if k[i] in d:
        d[k[i]] += 1
    else:
        d[k[i]] = 1
for key in d:
    dd[key] = int(d[key]*(d[key]-1)/2)
for i in range(N):
    res = 0
    for key in d:
        if d[key]>1:
            if key == k[i]:
                res += int((d[key]-1)*(d[key]-2)/2)
            else:
                res += dd[key]
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

def main():
    n = int(input())
    num_list = list(map(int, input().split()))  
    for i in range(len(num_list)):
        ansdic = {}
        pop = num_list.pop(i)
        dic = collections.Counter(num_list)
        for k, j in dic.items():
            if j < 2:
              pass
            else:
              ansdic[k] = int(j * (j-1)/2)
        print(sum(ansdic.values()))
        ansdic = {}
        num_list.insert(i, pop)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = input().split()
index = [] # Aに含まれる数の個数
# index.append(0) # 0の数は0

for i in range(len(An) + 1):
    index.append(int(An.count(str(i))))

# result
result=[] # 選び出す方法の個数
# result.append(0)
for j in range(len(An) + 1):
    A = index[j]
    if (A == 0):
        result.append([0, 0])
    else:
        result.append([(A*(A-1))//2, ((A-1)*(A-2))//2])


for k in range(1, len(result)):
    ans = 0 # 答え
    for l in range(1, len(result)):
        if (l == int(An[k-1])):
            ans += result[l][1]
        else:
            ans += result[l][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
res = list(map(int,input().split()))
res2 = [0] * len(res)
a = Counter(res)

for i in a.keys():
    a[i] -= 1
    sums = 0
    for j in a.values():
        sums += j * (j - 1) // 2
    a[i] += 1
    for x in range(len(res)):
        if res[x] == i:
            res2[x] = sums
        else:
            res2[x] = res2[x]
for k in res2:
    print(k)

Submission status: Time Limit Exceeded

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

def comb(n, k):
    p = (10 ** 9) + 7
    """power_funcを用いて(nCk) mod p を求める"""
    from math import factorial
    if n<0 or k<0 or n<k: return 0
    if n==0 or k==0: return 1
    a=factorial(n) %p
    b=factorial(k) %p
    c=factorial(n-k) %p
    return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
    """a^b mod p を求める"""
    if b==0: return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a * power_func(a, b - 1, p)) % p   

memo = {}
for i in range(len(aa)):
    if not aa[i] in memo:
        hu = copy.deepcopy(aa)
        del hu[i]
        a = collections.Counter(hu)
        summ = 0
        for j in a:
            summ += comb(a[j], 2)
        memo[aa[i]] = summ
    print(memo[aa[i]])

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
ls = [int(x) for x in input().split()]
counter = 0
for x in range(len(ls)):
    newls = ls.copy()
    counter = 0
    del newls[x]
    for x in itertools.combinations(newls, 2):
        if x[0] == x[1]:
            counter += 1
    print(counter)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

if N == set(A):
    for _ in range(N):
        print(0)

c2 = math.factorial(2)
umemory = {}
dmemory = {}
memory = {}

count = Counter(A)

for k, v in count.items():
    umemory[k] = 0 if v < 2 else math.factorial(v) // (math.factorial(v - 2) * c2)
    dmemory[k] = 0 if v < 3 else math.factorial(v-1) // (math.factorial(v - 3) * c2)

for k in range(0, N):
    a = A[k]
    if a in memory:
        result = memory[a]
    else:
        memory[a] = sum(map(lambda kv: kv[1], filter(lambda kv: kv[0] != a, umemory.items())))
        memory[a] += dmemory[a]
        result = memory[a]
    print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

for n in range(1, N+1):
    tmp = A[0:n - 1] + A[n:]
    c = Counter(tmp)
    ans = 0
    for i in c.values():
        if i >= 2:
            ans += i * (i - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = input()
L = input().split()

for i in range(len(L)):
    M = L[:]
    M.pop(i)
    C = collections.Counter(M)
    V = list(C.values())
    result = 0
    for c in V:
        result += c * (c - 1) / 2
    print(int(result))