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()))
import math
for k in range(N):
    ans = 0
    B = [i for i in A]
    B.pop(k)
    C = list(set(B))
    for i in C:
        n = B.count(i)
        if n >= 2:
            ans += (math.factorial(n)//(2*(math.factorial(n - 2))))
    print(ans)

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:
from collections import Counter

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

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

nag = len(t)
x = 0
rt = [0 for i in range(nag)]

for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
for f in range(nag):
    rt[f] = x - (t[f][1] -1)
  
for j in range(n):
    for k in range(nag):
        if c[j] == t[k][0]:
            print(rt[k])
            break

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools
n = int(input())
a = list(map(int,input().split()))
for i in range(len(a)):
  o = 0
  b = a[:]
  del b[i]
  c = collections.Counter(b)
  for i in c.values():
    o += len(list(itertools.permutations([ii for ii in range(i)], 2)))/2
  print(int(o))

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import math

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

n = int(input())
nums = [ int(s) for s in input().split() ]


for i in range(n):
    ans = 0

    cpl = copy.copy(nums)
    cpl.pop(i)
    ct = collections.Counter(cpl)

    for key, val in ct.items():
        if val > 1:
            ans += combinations_count(val, 2)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=[];G=[];p=0
import collections
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
  c = collections.Counter(A)
  G=c.most_common()
  #print(len(G))
  for a,b in G:
    #print(G)
    #print("a,b",a,b,i)
    if a==A[i]:
      p=p+((b-1)*(b-2))//2
    else:
      p=p+(b*(b-1))//2
  print(p)
  p=0

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
An = [int(x) for x in input().split()]
 
L = [0,0]
for i in range(N):
    if i > 1:
        L.append(int(math.factorial(i) / (math.factorial(i-2) * 2)))

for k in range(N):
    Bn = [An[i] for i in range(N) if not i == k]
    Cn = [Bn.count(i) for i in range(N)]
    Dn = [Cn.count(i)*L[i] for i in range(N)] 
    
    print(sum(Dn))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c=collections.Counter(A)
kazu=list(c.values())
kazu1=[i*(i-1)/2 for i in kazu]

for k in range(N):
    sub=c[A[k]]-1
    print(int(sum(kazu1)-sub))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

num = input()
num = int(num)

str = input()
list_in = str.split()

count_arr = np.zeros((num+1))
sum_arr = np.zeros((num+1))

for item in list_in:
    count_arr[int(item)] += 1

sum = 0
for i in range(len(count_arr)):
    tmp = count_arr[i]
    tmp = (tmp * (tmp -1)) / 2
    sum += tmp
    sum_arr[i] = tmp

for item in list_in:
    tmp = int(item)
    old_sum = sum_arr[tmp]
    tmp = count_arr[tmp] - 1
    tmp = (tmp * (tmp -1)) / 2
    result = sum - old_sum + tmp
    print(int(result))

Submission status: Time Limit Exceeded

Solution code:
import math
 
def resolve():
    n = int(input())
    a = [int(x) for x in input().split()]
 
    for k in range(n):
        # 初期化
        ak = [x for x in a] # deep copy
        del ak[k]
        kumi = 0
        ended = []
 
        # loop
        for i in range(n - 1):
            a0 = ak[i] # i番目の要素を検査
            if a0 in ended:
                continue
            cnt = ak.count(a0)
            if cnt >= 2:
                kumi += kumiawase(cnt ,2)
            ended.append(a0)
        print(kumi)
 
def kumiawase(n, r):
    return math.factorial(n) // (r * math.factorial(n - r))
 
resolve()

Submission status: Time Limit Exceeded

Solution code:
import collections
N = float(input())
Ai = list(map(int, input().split()))

dic = collections.Counter(Ai)
diccomb={}
dicselected={}
for k in dic:
    num = dic[k]
    diccomb[k] = int(num*(num-1)/2)
    dicselected[k] = int((num-1)*(num-2)/2)

count = {}
for k in dic:
    count[k] = sum(diccomb.values())-diccomb[k]

for k in Ai:
    print(dicselected[k]+count[k])

Submission status: Time Limit Exceeded

Solution code:
import math

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


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


memo = {}
for i in range(n):
    a_list = nums[:i] + nums[i+1:]
    cnt = 0
    for j in set(a_list):
        u_num = a_list.count(j)
        if u_num in memo:
            cnt += memo[u_num]
        else:
            calc = comb(u_num, 2)
            cnt += calc
            memo[u_num] = calc

    print(cnt)

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import deque
  from collections import defaultdict
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = defaultdict(int)
  e = defaultdict(int)
  
  for x in a:
    d[x] += 1
  
  func = lambda x: x*(x-1)//2 if x>=2 else 0
  
  for x in a:
    get = e.get(x)
    if get == None:
      g = d[x] - 1
      h = [v for k,v in d.items() if (k != x and v>=2)]
      h.append(g)
      i = list(map(func, h))
      ans = sum(i)
      e[x] = ans
      print(ans)
    else:
      print(get)
    
      
 
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

N = input()
L = input().split()
C = collections.Counter(L)
I = list(C.keys())
V = list(C.values())
count = []
count2 = []
result = []

for i in range(len(I)):
    count.append(V[i] * (V[i] - 1) / 2)
    count2.append((V[i] - 1) * (V[i] - 2) / 2)

S = sum(count)

for i in range(len(I)):
    result.append(int(S - count[i] + count2[i]))

for l in L:
    print(result[I.index(l)])

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(str,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
import math

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

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

c = collections.Counter(A)
dict_ans = {}

for i in c.keys():
    target = c.copy()
    target[i] -= 1
    ans = 0
    for k in target.values():
        ans += comb(k, 2)
    dict_ans[i] = ans

for i in A:
    print(dict_ans[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())

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

for c in range(N):
    a = A[:]
    count = 0
    del a[c]
    k = list(itertools.combinations(a, 2))
    for c in range(len(k)):
        if k[c][0] == k[c][1]:
            count += 1
        else:
            pass
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
cnt = Counter(a for a in A)
for i in range(N):
  ans = 0
  for a, c in cnt.items():
    if a == A[i]:
      c -= 1
    if c >= 2:
      ans += int(c * (c - 1) / 2)
  print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for i in range(N):
    L = A[0:i]+A[i+1:N]
    c = collections.Counter(L)
    l = [i for i in c.values() if i != 1]
    ans = 0
    for j in l:
        ans += combinations_count(j,2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

    cnt = Counter(A)
    # print(cnt)
    def comb(n, r):
        if n - r < 0:
            return 0
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    cnt_comb = {}
    sm = 0
    for key, val in cnt.items():
        tmp = comb(val, 2)
        cnt_comb[key] = tmp
        sm = sm + tmp
    # print(cnt_comb)

    # print(list(A))
    for a in A:
        comb_val = cnt_comb.get(a)
        cmb = comb(cnt.get(a) - 1, 2)
        print(sm - comb_val + cmb)



if __name__ == '__main__':
    solver()

Submission status: Time Limit Exceeded

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

import math
def combinations_count(n, r):
    ret = 0
    if n >= r :
        ret = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    return ret

dupl = [x for x in set(A) if A.count(x) > 1]
for i in range(N):
    ans = 0
    l = A[:]
    l.pop(i) 
    for j in dupl:
        p = combinations_count(l.count(j),2) 
        if p > 0:
            ans = ans + p
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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


def main(n: int, a: list):
    counter = collections.Counter(a)
    pairs = {k: v * (v - 1) / 2 for k, v in counter.items()}

    counted = {}

    for i in range(N):

        target = a[i]

        if target not in counted.keys():
            temp = pairs.copy()

            v = counter[target] - 1
            temp[target] = v * (v - 1) / 2

            pair_cnt = sum(temp.values())
            counted.update({target: pair_cnt})
        else:
            pair_cnt = counted[target]

        print(int(pair_cnt))


main(N, A)

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 += math.factorial(j) // (math.factorial(j - 2) * math.factorial(2))
    
    print(sum)

    hako[tmp] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import math

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

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby
import copy

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

for k in range(1,N+1):
    n = 0
    B_list = copy.copy(A_list)
    del B_list[(k-1)]
    n_list = sorted(B_list)
    for key, group in groupby(n_list):
        Len = len(list(group))
        if Len >= 2:
            n += Len*(Len-1)/2
    print(int(n))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import add

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

i = 0
for k in range(n):
  atmp = a[0:k] + a[k+1:]
  i = 0
  for ai in set(atmp):
    i += reduce(add, range(atmp.count(ai)))
  print(i)

Submission status: Time Limit Exceeded

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

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

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

    count_dict = collections.Counter(A)

    del_list = []
    for key, value in list(count_dict.items()):
        if value == 1:
            count_dict.pop(key)
            del_list.append(key)


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


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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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


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


c = collections.Counter(aa)

for a in aa:
    cc = c.copy()
    cc[a] -= 1
    ans = 0
    for i in cc.values():
        if i >= 2:
            ans += combinations_count(i, 2)
    print(ans)

Submission status: Time Limit Exceeded

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

C = collections.Counter(L)
for i in range(N):
    del_target = L[i]
#     print("deltarget:{}".format(del_target))
    ans = 0
    for i in C.items():
        if i[0]== del_target:
            n = i[1]-1
#             print(int((n*(n-1)/2)))
            ans += int((n*(n-1)/2))
        else:
#             print(int((i[1]*(i[1]-1))/2))
            ans += int((i[1]*(i[1]-1))/2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

N = int(input())
A = list(map(int, input().split(' ')))
master = {}
result = {}

for c in set(A):
  master[c] = A.count(c)
  

for k in range(N):
  count = 0
  master_ = master.copy()
  master_[A[k]] -= 1
  for v in master_.values():
    if v > 1:
      count += combinations_count(v, 2)
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
l = list(input().split())
ans = 0
for k in range(N):
  a = l[0]
  l.pop(0)
  for x in set(l):
    ans += l.count(x)*(l.count(x)-1)/2
  print(int(ans))
  ans = 0
  l.append(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
alist = list(map(int,input().split()))
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

adic = {}
ans = 0
for i in alist:
  if i not in adic:
    adic[i] = 1
  else:
    adic[i] += 1
for i in range(n):
  for j in adic:
    if j == alist[i]:
      if adic[j] >= 3:
        ans += cmb(adic[j]-1,2)
      else:
        ans += 0
    else:
      if adic[j] >= 2:
        ans += cmb(adic[j],2)
      else:
        ans += 0
  print(ans)
  ans = 0

Submission status: Time Limit Exceeded

Solution code:
import itertools


def main():
    n = int(input())
    a = [int(i) for i in input().split(" ")]
    for i, j in enumerate(a):
        print(len([k for k in itertools.combinations([l for k, l in enumerate(a) if k != i], 2) if k[0] == k[1]]))


main()

Submission status: Time Limit Exceeded

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

N = int(input())
A = list(map(int, input().split()))
#dp = [0]*(N+1)
dp = [int(n*(n-1)/2) for n in range(N+1)]
cnt_org = {}

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
 
for i in range(n):
  b = a[:]
  del b[i]
  k = 0
  c = set(b)
  for t in c:
    num = b.count(t)
    for i in range(num):
      k += i
  print(k)

Submission status: Time Limit Exceeded

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

A_set = set(A)

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

ans_d = {}
for a_s in A_set:
	#print(A.count(a))
	ans_d[str(a_s)] = A.count(a_s)

for a in A:
    A_new = ans_d.copy()
    A_new[str(a)] -= 1
    ans = 0
    #print(A_new)
    #print("="*10)
    for a_n in A_new.values():
        if a_n <= 1:
            continue
        else:
            ans += cmb(a_n, 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
init = [int(s) for s in input().rstrip().split(' ')]

for i in range(N):
    count = 0
    il = init[:]
    il.pop(i)
    for j in range(N-1):
        for k in range(j+1,N-1):
            if il[j] == il[k]:
                count +=1

    print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

Y = 0

for i in range(N):
    B = A[:]
    del B[i]
    c = collections.Counter(B)

    for d in c.values():
        Y = Y + d * (d-1) / 2

    print(int(Y))

    Y = 0

Submission status: Time Limit Exceeded

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

for i in range(size):
    if str(values[i]) in skip_map:
        print(skip_map[str(values[i])])
        continue

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

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

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

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

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

# output
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
# bin/env/pypy3
import sys
import collections
import heapq
import math
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
c = collections.Counter(a)
#print(n,a,c)
memo_f = {}
memo_c = {}
def combinations_count(n, r):
    s = str([n,r])
    if s in memo_c:
        return memo_c[s]
    if not n in memo_f: memo_f[n] = math.factorial(n)
    b = max(n - r,0)
    if not b in memo_f: memo_f[b] = math.factorial(b)
    if not r in memo_f: memo_f[r] = math.factorial(r)
    memo_c[s] = memo_f[n] // (memo_f[b] * memo_f[r])
    return memo_c[s]
for e in a:
    c[e] -= 1
    b = [combinations_count(v,2) for v in c.values() if v > 1]
    print(sum(b))
    c[e] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
ans = {}

cal = {}
for a, b in C.items():
    cal[a] = b*(b-1)//2
for i in C.keys():
    ans[i] = sum(cal.values()) - (C[i] - 1)
for a in A:
    print(ans[a])

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

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

    Cou = collections.Counter(A)

    C_d = {}

    '''
    for k, c in Cou.items():
        if c > 1:
            C_d[k] = comb(c, 2)
        else:
            C_d[k] = 0
    print(C_d)
    '''
    ans = 0
    for k in range(1, N+1):
        tmp = Cou.copy()
        tmp[A[k-1]] -= 1
        ans = 0
        #print(tmp)
        for c in tmp.values():
            if c > 1:
                ans += comb(c, 2)

        print(ans)
        #tmp[A[k-1]] = max(0, tmp[A[k-1]]-2)
        #print(sum(tmp.values()))


    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

# 各数値の重複数辞書
countdic = dict()
for el in a:
    if el in countdic:
        countdic[el] += 1
    else:
        countdic[el] = 1

# 組み合わせ数
def combi(num):
    if num < 2:
        pair = 0
    else:
        pair = int(num * (num-1) / 2)
    return pair

# 各数字を抽出しない時と抽出した時の組み合わせ数
fulldic = dict()
reducedic = dict()
for k, v in countdic.items():
    fulldic[k] = combi(v)
    reducedic[k] = combi(v-1)
    
for el in a:
    print(sum(fulldic.values()) - (fulldic[el] - reducedic[el]))

Submission status: Time Limit Exceeded

Solution code:

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

from collections import Counter
import math
elem_dict = dict(Counter(a_n))
def get_n_combination(n, m):
    if n - m < 0:
        output = 0
    else:
        output = math.factorial(n)//math.factorial(n-m)//math.factorial(m)
#     print("output", output, "m",m, "n", n)
    return output


for a in a_n:
    sum_ = 0
    for k,v in elem_dict.items():
        if k == a:
            sum_ += get_n_combination(v - 1, 2)
        else:
            sum_ += get_n_combination(v, 2)
#         print(sum_)
    print(sum_)

Submission status: Time Limit Exceeded

Solution code:
def count(l,n):
    s=set(l)
    s1=0
    for i in s:
        if(i==n):
            s1+=(pow(l.count(i)-1,2)-l.count(i)+1)//2
        else:
            s1+=(pow(l.count(i),2)-l.count(i))//2
    return s1
n=int(input())
l=[int(i) for i in input().split()]
l1=[]
l2=[]
for i in range(n):
    if(l[i] in l1):
        print(l2[l1.index(l[i])])
    else:
        k=count(l,l[i])
        l1.append(l[i])
        l2.append(k)
        print(k)

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
    return list(map(int, stdin.readline().split()))

def In():
    return map(int, stdin.readline().split())

def I():
    return int(stdin.readline())

P = 1000000007
n = I()
lis = L()
s = list(set(lis))
s. sort()
if len(s) == n:
    for i in range(n):
        print(0)
else:
    arr = [0 for i in range(len(s))]
    for i in lis:
        x = s.index(i)
        arr[x] += 1
    sm = 0
    for i in arr:
        sm += (((i)*(i-1))//2)
    arr2 = []
    for i in range(len(s)):
        v = arr[i]
        val = sm
        if v > 1:
            val = sm - ((((v)*(v-1))//2)) + (((v-2)*(v-1))//2)
        arr2.append(val)
        
    for i in lis:
        x = s.index(i)
        v = arr2[x]
        print(v)

Submission status: Time Limit Exceeded

Solution code:
import math

def resolve():
    n = int(input())
    a = [int(x) for x in input().split()]

    for k in range(n):
        # 初期化
        ak = [x for x in a] # deep copy
        del ak[k]
        kumi = 0
        ended = []

        # loop
        for i in range(n - 1):
            a0 = ak[i] # i番目の要素を検査
            if a0 in ended:
                continue
            cnt = ak.count(a0)
            if cnt >= 2:
                kumi += kumiawase(cnt ,2)
            ended.append(a0)
        print(kumi)

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

resolve()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
import collections
import math
def combinations_count2(n):
    if(n<=1):
        answer=0
    else:
        answer=n*(n-1)/2
    return answer

table=collections.Counter(a)
keytable=collections.Counter(a).keys()
c=[]

for key in keytable:
    c.append(table[key])
#print(c)
ca=[]
for j in range(len(c)):
    ca.append(combinations_count2(c[j]))
#print(ca)
sums=sum(ca)
#print(sum)
#print(keytable)
#print(table)
for i in range(len(a)):
    keynum=list(keytable).index(a[i])
    answer=sums-ca[keynum]+combinations_count2(c[keynum]-1)
    print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import numpy as np

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 = np.zeros(len(A_unique))
for _,A_i in enumerate(A):
  counts[A_i] += 1

for k in range(N):
  counts[A[k]] -= 1 # herasu
  print(int(np.sum(counts*(counts-1)/2)))
  counts[A[k]] += 1 # modosu

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

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

ans_dict = {}
ans_keys = []
value_dict = {1: 0}
value_keys = [1]
for i in range(n):
    if i in ans_keys:
        print(ans_dict[i])
    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_keys:
                tmp_ans += value_dict[value]
            else:
                tmp = (value * (value - 1))/2
                tmp_ans += tmp
                value_dict[value] = tmp
                value_keys.append(value)
        ans_dict[i] = tmp_ans
        ans_keys.append(i)
        print(int(tmp_ans))

Submission status: Time Limit Exceeded

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

for k in range(n):
  a_k = sorted(a[:k] + a[k+1:])
  count = a_k.count(1)
  way = (count*(count-1)//2) if count > 1 else 0
  elements = count
  while True:
    if elements >= n-1:
      break
    new_num = a_k[elements]
    count = a_k.count(new_num)
    way += (count*(count-1)//2) if count > 1 else 0
    elements += count
  print(way)

Submission status: Time Limit Exceeded

Solution code:


def main():
    import math
    import collections
    import numpy as np

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

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

    # As = np.tile(A, (N, 1))
    # As = [A]*N
    # print(As)
    for i in range(N):
        x = A[i]
        A[i] = 0
        tmp_A = collections.Counter(A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                su += combinations_count(pairs, 2)
            else:
                break
        A[i] = x
        print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split(" ")))
import copy
for v in range(n):
    a_ = copy.copy(a)
    del a_[v]
    count = 0
    for s in range(n-1):
        first = a_[s]
        count -=1
        for t in range(n-1):
            second = a_[t]
            if first == second:
                count += 1
    count = int(count/2)
    print(count)

Submission status: Time Limit Exceeded

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

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

d = {}
l = []
for i in a:
  if i not in d:
    d[i] = 1
    l.append(i)
  else:
    d[i] += 1
for i in a:
  s = 0
  for j in l:
    if i == j:
      if d[j]-1 >= 2:
        s += combinations_count(d[j]-1, 2)
    else:
      if d[j] >= 2:
        s += combinations_count(d[j], 2)      
  print(s)