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:
from collections import defaultdict
from itertools import permutations, combinations
N = int(input())
List = list(map(int,input().split()))
dic = defaultdict(int)
dic2 = {}
for item in List:
  dic[item] += 1
for item in List:
  if item in dic2:
    print(dic2[item])
    continue
  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)
  dic2[item] = Answer

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
#answer = [-1] * N
#p = [0] * N
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[i] = answer[A[i] - 1]
    #print(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[i] = s
  print(s)
  #answer[A[i] - 1] = s
#for i in p:
  #print(i)

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 set(tmp):
        l = tmp.count(j)
        res += len(list(itertools.combinations(list(range(l)),2)))

    print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
def combination(a, b):
    tmp = 1
    for i in range(a - b + 1, a + 1):
        tmp *= i
    return tmp // math.factorial(b)

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
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 = list(map(int, input().split()))
aa_dict = {}
a_dict = {}
for a in a_list:
    if a in aa_dict:
        aa_dict[a] += 1
        if aa_dict[a] == 2:
            a_dict[a] = 2
        else:
            a_dict[a] += 1
    else:
        aa_dict[a] = 1
a_cal_dict = {}
cal_dict = {}
for a in a_list:
    if a in a_cal_dict:
        print(a_cal_dict[a])
    else:
        ans = 0
        for key in a_dict:
            n = 0
            if key == a:
                n = a_dict[key] - 1
            else:
                n = a_dict[key]
            if n < 2:
                continue
            if n in cal_dict:
                ans += cal_dict[n]
            else:
                cal_dict[n] = cmb(n,2)
                ans += cal_dict[n]
        print(ans)
        a_cal_dict[a] = ans

Submission status: Time Limit Exceeded

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

hako = [0 for i in range(N)]

for i in A:
    hako[i-1] += 1

for i in range(N):
    sum = 0
    tmp = A[i] - 1
    hako[tmp] -= 1

    for j in hako:
        if j > 1:
            sum += int(j * (j-1) / 2)
    
    print(sum)

    hako[tmp] += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input().rstrip())
line = input().rstrip().split(' ')

a = []
d = [0] * n
for i in range(n):
    num = int(line[i])
    a.append(num)
    d[num - 1] += 1

ans_list = [0] * n
for i in range(n):
    ans_list[i] = int(d[i] * (d[i] - 1) / 2)

for i in range(n):
    ans = 0
    for j in range(n):
        if a[i] == j + 1:
            num = d[j] - 1
            ans += num * (num - 1) / 2
        else:
            ans += ans_list[j]
    
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import Counter

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

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

for k in range(N):
    sum = 0
    dele = A.pop(k)
    counter = Counter(A)
    for value in counter.values():
        sum = sum + combinations_count(value,2)
    print(sum)
    A.insert(k,dele)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from collections import Counter
import sys

input = sys.stdin.readline

def main():
    n = int(input().rstrip())
    l = list(input().rstrip().split())

    c = Counter(l)
    for item in l:
        ans = 0
        for key in c:
            tmp = c[key]
            if key == item:
                tmp -= 1
            ans += tmp * (tmp - 1) // 2
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#%%
import copy, math, collections

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

result = []

num_dict = dict(collections.Counter(a))

def calc(x):
    if x >= 2:
        return math.factorial(x) / (math.factorial(x - 2) * 2)
    else:
        return 0

result_dict = {}
result_minus1_dict = {}

for key in num_dict.keys():
    result_dict[key] = calc(num_dict[key])
    result_minus1_dict[key] = calc(num_dict[key] - 1)

for i in range(len(a)):
    result = 0

    for key, value in result_dict.items():
        if key == a[i]:
            result += result_minus1_dict[a[i]]
        else:
            result += value

    print(int(result))


# %%

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=(input().split())
for i in range(n):
    x=0
    for j in range(n):
        for k in range(n):
            if j==i:
                pass
            elif k==i or j==k:
                pass       
            elif j>k and a[j]==a[k] and j!=i and k!=i:
                x=x+1
    print(x)

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 = []
for i in range(M):
  t.append(A.count(C[i]))


s = []  
for i in range(M):
  ans = 0
  for j in range(M):
    if i == j:
      p = t[j]-1
    else:
      p = t[j]
    if p >=2:
      ans += math.factorial(p)/(math.factorial(p-2)*2)
  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 math
n=int(input())
a=list(map(int, input().split()))
def comb(n,r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))

for i in range(n):
    a_ = a.copy()
    del a_[i]
    anc = 0
    for u in set(a_):
        if a_.count(u) > 1:
            anc += comb(a_.count(u),2)
    print(anc)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
num_list = list(map(int, input().split()))
index = 0
targets = copy.copy(num_list)
for num in 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
	targets.insert(index,num)
	index = index+1
	print(r)

Submission status: Time Limit Exceeded

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

N = int(input())
A = input().split()

ans = {}
ans_all = 0
for x in set(A):
    m = A.count(x)
    ans_all += m * (m - 1) // 2


for x in A:
    m = A.count(x) - 1
    print(ans_all - (m + 1) * m // 2 + m * (m - 1) // 2)

Submission status: Time Limit Exceeded

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

for k in range(n):
    count = 0
    for i in range(n):
        for j in range(i+1,n):
            if i != k and j != k:
                if a[i] == a[j]:
                    count += 1
    print(count)

Submission status: Time Limit Exceeded

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

import math
import numpy as np

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
base = 0
for i in range(len(a)):
    base = 0
    a_del = np.delete(a, int(i), 0)
    set_ = a_del.tolist()
    set_1 = list(set(set_))
    for j in range(len(set_1)):
        cnt = set_.count(set_1[j])
        if 2 <= cnt:
            comb_cnt = comb(cnt,2)
            base = base + comb_cnt
    print(base)

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)]
s = 0

for val in c:
  s = s + val * (val - 1) // 2

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

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 = np.array(list(c.values()))
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:
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 = []
for i in range(len(c_lst)):
    cnt.append((c_lst[i][1] * (c_lst[i][1] - 1)) // 2)
    
for i in a:
    ans = 0
    if i in values:
        ans += ((c_lst[values.index(i)][1] - 1) * (c_lst[values.index(i)][1] - 2)) // 2
        ans += sum(cnt[:values.index(i)]) + sum(cnt[values.index(i) + 1:])
    else:
        ans = sum(cnt)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n=int(input())
li = list(map(int,input().split()))
cli = collections.Counter(li)
cli_values = tuple(cli.values())

count = 0

for cli_value in cli_values:
    if cli_value > 1:
        count += (cli_value * (cli_value-1))//2

for k in range(n):
    tmp = li.count(li[k])
    answer = count - (tmp * (tmp-1))//2 + ((tmp-1) * (tmp-2))//2
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
from collections import OrderedDict
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

d = OrderedDict()

n = int(input())
nums = list(map(int, input().split()))
for num in nums:
    if not d.get(num):
        d[num] = 1
    else:
        d[num] += 1

combination_d = {}

for num in nums:
    counter = 0
    if combination_d.get(num):
        print(combination_d[num])
        continue

    for key in d.keys():
        if key == num:
            if d[key]-1 >= 2:
                counter += cmb(d[key]-1, 2)
        else:
            if d[key] >= 2:
                counter += cmb(d[key], 2)
    
    combination_d[num] = counter
    print(counter)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
setA=set(A)
number1={}#-1
number2={}
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 A:
    total=number1[a]
    total+=t
    total-=number2[a]
    print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

def nCr(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()))

counter = Counter(A)

mapping = {}
for k, v in counter.items():
    mapping[k] = nCr(v, 2)

all_sum = 0
for v in mapping.values():
    all_sum += v

for i in range(n):
    num = A[i]
    t = counter[num]
    ans = all_sum - nCr(t, 2) + nCr(t - 1, 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
dic = {}
ans = 0
a = 0

for i in range(n):
  dic[i+1] = l.count(i+1)
  x = l.count(i+1)
  ans += int(x*(x-1)/2)
  
for i in range(n):
  a = dic[l[i]]
  print(ans-a+1)

Submission status: Time Limit Exceeded

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

for i in range(N):
  ans = 0
  tmp = A[i]
  A[i] = 0
  for j in set(A):
    count = A.count(j)
    ans += count * (count - 1) / 2
  print(int(ans))
  A[i] = tmp

Submission status: Time Limit Exceeded

Solution code:
import copy

N=int(input())

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

#print(A)

mama=dict()
papa=dict()

for i in range(N):

    if(A[i] in mama):
        print(mama[A[i]])
        continue
    
    AA=copy.deepcopy(A)
    ai=A[i]
    del AA[i]
    #print(AA)
    Aset=set(AA)
    count=0
    
    for i in Aset:
        aa=AA.count(i)
        if(aa in papa):
            count+=papa[aa]
            continue
        #print(i,aa)
        aiu=int((aa*(aa-1))/2)
        count+=aiu
        papa[aa]=aiu
    print(count)
    mama[ai]=count

Submission status: Time Limit Exceeded

Solution code:
import itertools
import collections

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

divided = collections.Counter(A_list)
ans = 0
for _, v in divided.items():
    ans += len(list(itertools.combinations(range(v), 2)))

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

    print(int(ans_k))

Submission status: Time Limit Exceeded

Solution code:

N=int(input())
A=list(map(int,input().split()))
B=list(set(A))
C=[]
D=[]
rireki=[]
for i in B:
    k=A.count(i)
    if  not i in rireki:
        C.append(int((k-1)*(k-2)/2))
        D.append(int(k*(k-1)/2))
        rireki.append(i)

Dsum=sum(D)
rireki=[]
rirekinum=[]
for i in A:
    if not i in rireki:
        iindex=B.index(i)
        iindexnum=C[iindex]
        num=Dsum-D[iindex]
        ans=iindexnum+num
        rireki.append(i)
        rirekinum.append(ans)
        print(ans)
    else:
        num=rireki.index(i)
        print(rirekinum[num])

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)):
    m=c.count(d[i])
    if m>1:
      b=b+m*(m-1)/2
  return b
for k in range(n):
  print(int(ans(a,k)))

Submission status: Time Limit Exceeded

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

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

n = int(input())
S = list(input().split(" "))

s = [int(n) for n in S]

dic_l = collections.Counter(s)
factor_list = list(dic_l.keys())
count_list = []
for i in range(n):
  tmp = s[i]
  count = 0
  for m in range(len(factor_list)):
    if tmp == factor_list[m]:
      kazu = dic_l[factor_list[m]] -1
      count = count + cmb(kazu, 2)
    else:
      kazu = dic_l[factor_list[m]]
      count = count + cmb(kazu, 2)
  count_list.append(count)

count_list = [str(n) for n in count_list]
print("\n".join(count_list))

Submission status: Time Limit Exceeded

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

a1=len(list(set(a)))

total=0
for j in range(a1):
    x=c.most_common()[j][1]
    total+=x*(x-1)/2

for i in range(n):
    ans=total-a.count(a[i])+1
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
lst = list(map(int, input().split()))
sett = set(lst)
newLst = []
for x in sett:
    newLst.append(lst.count(x))
dic = dict(zip(sett, newLst))
for i in range(n):
    summ = 0
    dic[lst[i]] = dic[lst[i]]-1
    for x in list(dic.values()):
        summ = summ + len(list(combinations(range(x), 2)))
    dic[lst[i]] = dic[lst[i]]+1
    print(summ)

Submission status: Time Limit Exceeded

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

from math import factorial
from itertools import combinations
from collections import Counter
from copy import deepcopy

def main():
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        tmp_a = deepcopy(a)
        tmp_a[i] = -1
        tmp_a_c = Counter(tmp_a)
        tmp_a_cc = [tmp_a_c[i] for i in tmp_a_c if tmp_a_c[i] > 1]
        r = 0
        for cc in tmp_a_cc:
            r += factorial(cc) // (factorial(2) * factorial(cc-2))
        print(r)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(set(A))
dic = dict()
ans = 0
for i in range(len(B)):
    c = A.count(B[i])
    ans += c*(c-1)/2
    dic[B[i]]=c
#print(dic)
    
for i in range(N):
    d = dic[A[i]]
    print(int(ans -d*(d-1)/2+(d-1)*(d-2)/2))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

def comb(n, r):
    return 0 if n < r else (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

ans = [-1 for _i in range(n)]
combs = [comb(e, 2) for e in dosu]
total = sum(combs)

for e in nums:
    if ans[e - 1] < 0:
        ans[e - 1] = total - combs[e - 1] + comb(dosu[e - 1] - 1, 2)

    print(ans[e - 1])

Submission status: Time Limit Exceeded

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

c = [A.count(i) for i in uni]
coun = c[:]
c = [int((i*(i-1))/2) for i in c]
# print(c)
l = []
# print("+++++++++++++++")
for i in range(len(uni)):
    c_ = c[:]
    x = ((coun[i]-1)*(coun[i]-2))/2
    c_[i] = x
    # print(c_)
    l.append(int(sum(c_)))
# print(l)
ans = dict([[uni[i],l[i]] for i in range(len(l))])

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

p_dict = {}
def permutation_count(x):
    ret = 0
    if 2 <= x:
        if(not x in p_dict.keys()):
            ret = math.factorial(x) // (2*(math.factorial(x-2)))
        else:
            ret = p_dict[x]

    return ret

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

copy_a = copy.copy(a)
unique_a = set(copy_a)

cnt_all_conbi = 0
for i in unique_a:
    x = a.count(i)
    cnt_all_conbi += permutation_count(x)

for i in a:
    x = a.count(i)
    print(cnt_all_conbi - (x-1))

Submission status: Time Limit Exceeded

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


numlist = []
for i in range(N):
    number = 0
    for j in range(N):
        if A[i] == A[j]:
            number += 1
    numlist.append(number-1)
sum = int(sum(numlist)/2)

for k in range(N):
    print(sum-numlist[k])

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(n):
    l = a[:i]+a[i+1:]
    pattern = 0
    pattern += sum([j*(j-1)//2 for j in collections.Counter(l).values()])
    print(pattern)

Submission status: Time Limit Exceeded

Solution code:
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 = v * (v-1) // 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 += a * (a-1) // 2
        s_sum[l[i]] = ans
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
from scipy.misc import comb
from collections import Counter

N=int(input())
A=list(map(int,input().split() ))
B=[-1]*N   #メモ化。iの時の方法の総数

f_cnt=Counter(A)
f_dic=dict(f_cnt)
#print("first",f_dic)
#f_dic[1] += -1 
#print(f_dic)

for i,item in enumerate(A):
  if B[item-1]>-1:
    #print(i,item,"は計算済み！")
    #print(i,B[item-1],"done")
    print(B[item-1])
  else:
    #C=A[0:i]+A[i+1:] #仮で作るリスト
    #cnt = Counter(C)
    #D=dict(cnt)
    D=copy.deepcopy(f_dic)
    D[item] -= 1
    E=[]
    for item2 in D:
      if D[item2]>=2:
        E.append(D[item2])
    #print(i,f_dic,D,E)
    opt=0
    for item3 in E:
      opt += comb(item3,2,exact=True)
    B[item-1]=opt
    #print(i,opt)
    print(opt)
    #print(i,E,opt)
    #print(C,cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import copy

a = input()
a=int(a)
ball=[]
mydict={}
DPdict={}
temp=input().split(" ")
for i in range(a):
    if not (temp[i] in mydict):
        mydict[temp[i]]=1
    else:
        mydict[temp[i]]+=1
    ball.append(temp[i])

for i in range(len(ball)):
    rettemp=0
    #print(DPdict)
    if ball[i] in DPdict:
        print(int(DPdict[ball[i]]))
        continue
    for j in mydict.keys():

        if ball[i]==j:
            rettemp+=(mydict[j]-1)*(mydict[j]-2)/2
        else :
            rettemp+=(mydict[j]-1)*(mydict[j])/2
    DPdict[ball[i]]=rettemp
    print(int(rettemp))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a2 = sorted(a) 
a3 = list(set(a2))

z = []
for i in range(len(a3)):
     u = a.count(a3[i])
     z.append(u)


for j in range(n):
    count = 1
    for i in z:
        count += i*(i-1)/2
    count -= z[a3.index(a[j])]
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections
import copy
def cmb(n,r):
  if n-r<0:
    return 0
  return factorial(n) // factorial(r) // factorial(n - r)
n=int(input())
a=list(map(int,input().split()))
c=collections.Counter(a)
for aa in a:
  t=copy.copy(c)
  o=0
  t[aa]-=1
  for i in t.values():
    o+=cmb(i,2)
  print(o)

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
d=dict()

for i in range(len(l)):
  d.setdefault(l[i],0)
  d[l[i]] += 1

for i in range(len(l)):
    d[l[i]] -= 1
    result=0
    for key,value in d.items():
        num=(value*(value-1))/2
        result += num
    d[l[i]] += 1
    print(int(result))

Submission status: Time Limit Exceeded

Solution code:
import collections
from math import factorial

N = int(input())
A = list(map(int, input().split()))
A_master = A
for i in range(len(A)):
  if i==0:
    A_temp = A[1:]
  elif i==len(A)-1:
    A_temp = A[0:len(A)-1]
  else:
    A_temp = A[:i] + A[i+1:] 
  c = collections.Counter(A_temp)
  
  co = 0
  for k in c.values():
    if k != 1 and k != 0:
      Mcon = factorial(k) // factorial(2) // factorial(k - 2)
      co += Mcon
  print(co)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

num = int(input())
ball_original = list(map(int, input().split()))
ans = 0

for i in range(num):
    ball = copy.copy(ball_original)
    ball.pop(i)
    c = collections.Counter(ball)
    values, counts = zip(*c.most_common())
    
    for j in range(len(counts)):
        if counts[j] != 1:
            ans = ans + int(counts[j]*(counts[j]-1)/2)
        
    print(ans)
    
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
tmp = {}
for i in range(n):
    a_copy = a[:]
    a_copy.remove(a[i])
    k = a[i]
    if k in tmp:
        print(tmp[k])
    else:
        c = collections.Counter(a_copy)
        cnt = 0
        for i in c.values():
            cnt += i * (i-1) // 2
    
        tmp[k] = cnt
    
        print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math

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

balls = [0]*n
for a in A:
    balls[a-1] += 1

tei = math.factorial(2)
ball_permu = [0]*n
ball_permu_target = [0]*n
fac = [-1]*n
for i in range(n):
    if balls[i] < 2:
        ball_permu[i] = 0
    else:
        if fac[balls[i]-1] == -1:
            ball_permu[i] = math.factorial(balls[i])//(math.factorial(balls[i]-2)*tei)
            fac[balls[i]-1] = ball_permu[i]
        else:
            ball_permu[i]=fac[balls[i]-1]

    ball = balls[i]-1
    if ball < 2:
        ball_permu_target[i] = 0
    else:
        if fac[ball-1] == -1:
            ball_permu_target[i] = math.factorial(ball)//(math.factorial(ball-2)*tei)
            fac[ball-1] = ball_permu_target[i]
        else:
            ball_permu_target[i]=fac[ball-1]

ball_sum = sum(ball_permu)
for a in A:
    ans = ball_sum-ball_permu[a-1]+ball_permu_target[a-1]
    print(ans)

Submission status: Time Limit Exceeded

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

s = collections.Counter(A)

for i in s:
    if s[i] >= 2:
        b = s[i]*(s[i]-1)/2
        cnt += b

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

d = {}
count = {}
s = list(set(a))

for i in s:
    d[i] = 0
    c = a.count(i)
    count[i] = c
    if c>=2:
        d[i] += ((c*(c-1))//2)
    
for i in a:
    temp = 0
    c = 0
    for j in d:
        if j==i:
            temp += ((count[i]-1)*(count[i]-2))//2
        else:
            temp += d[j]
    print(temp)

# * (2/c/2)

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

keys, values = zip(*c.most_common())

ans = 0

for i in values:
  if i != 1:
  	ans += (i*(i-1)//2)

for i in a:
    for j in range(len(keys)):
      if i == keys[j]:
        co = values[j]
        break
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(l)
ans_dict = {}

for i in l:
    if str(i) in ans_dict.keys():
        print(int(ans_dict[str(i)]))
    else:
        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
        ans_dict[str(i)] = ans
        print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())

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

c = collections.Counter(A)
# print(c)
dic = {}
for k,v in c.items():
    dic[k] = int(v*(v-1)/2)
# print(dic)

for k in range(N):
    Ak = A[k]
    ans = dic.copy()
    ans[Ak] = int((c[Ak]-1)*(c[Ak]-2)/2)
    # print(dic,ans)
    print(sum(ans.values()))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = list(map(int, input().split()))
set_N = set(l)

for i in range(N):
  cnt = 0
  for sn in set_N:
    num = l.count(sn)
    if l[i]==sn:
      num -= 1
    cnt += num*(num-1)/2
  print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    D = {}

    for k in range(N):
        if A[k] in D.keys():
            print(D[A[k]])
        else:
            tmp = A.copy()
            del tmp[k]
            ans = 0
            for i in range(len(tmp) - 1):
                for j in range(i + 1, len(tmp)):
                    if tmp[i] == tmp[j]:
                        ans += 1
            print(ans)
            D[A[k]] = ans


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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



count = {}
for i in range(N):
    if values[i] not in count:
        count[values[i]] = 1
    else:
        count[values[i]] = count[values[i]] + 1
time = {}
for i in range(N):
    if(values[i] not in time):
        temp = count.copy()
        temp[values[i]] = temp[values[i]] - 1
        num = 0
        for j in temp:
            num = num + int(temp[j]*(temp[j]-1)/2)
        print(num) 
        time[values[i]] = num
    else:
        print(time[values[i]])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
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 = list(map(int, input().split()))
aa_dict = {}
a_dict = {}
for a in a_list:
    if a in aa_dict:
        aa_dict[a] += 1
        if aa_dict[a] == 2:
            a_dict[a] = 2
        else:
            a_dict[a] += 1
    else:
        aa_dict[a] = 1
a_cal_dict = {}
cal_dict = {}
for a in a_list:
    if a in a_cal_dict:
        print(a_cal_dict[a])
    else:
        ans = 0
        for key in a_dict:
            n = 0
            if key == a:
                n = a_dict[key] - 1
            else:
                n = a_dict[key]
            if n < 2:
                continue
            if n in cal_dict:
                ans += cal_dict[n]
            else:
                cal_dict[n] = cmb(n,2)
                ans += cal_dict[n]
        print(ans)
        a_cal_dict[a] = ans