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


from collections import Counter
import math 

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


#def combinations_count(n):
#    return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
def combinations_count2(l):
    return sum([cmb(x, 2) for x in l if x > 1])
    

L = [combinations_count2(l)for l in [Counter(A[:i]+A[i+1:]).values() for i in range(N)]]
print(*L, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
s = list(set(l))
all_sum = 0
count = {}
for i in range(len(s)):
    v = l.count(s[i])
    all_sum += v * (v - 1) // 2
    count[s[i]] = v
for i in range(len(l)):
    if count[l[i]] > 1:
        v = count[l[i]]
        print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
    else:
        print(all_sum)

Submission status: Time Limit Exceeded

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

for k in range(N):
    # 取り除く数字はP[k]
    print(result_sum_all + diff[P[k]])

Submission status: Time Limit Exceeded

Solution code:
import copy
N=int(input())
s=input().split()
list2=list(map(int,s))
for y in range(N):
    list1=copy.copy(list2)
    list1.pop(y)
    c=list(set(list1))
    a=0
    for f in c:
        h=list1.count(f)
        a+=(h*(h-1))
    print(int(a/2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
set_a = set(a_list)              

for i in range(n):
  ans = 0
  temp = a_list[0:i]
  temp  += a_list[i+1:]
  for a in set_a:
    cnt = temp.count(a)
    ans += cnt*(cnt-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

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

duplication = {}
ans = 0
for x in A:
    if x in duplication:
        continue
    count = A.count(x)
    if count > 1:
        duplication[x] = count
        for i in range(1,count):
            ans += i
for x in A:
    if x not in duplication:
        print(ans)
    else:
        print(ans -duplication[x]+1)

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:
    idx.append(A[i]-1)

from operator import mul
from functools import reduce

def nCr(n,r):
    if n == 1:
        return 0
    r = min(n-r,r)
    if r == 0:
        return 1
    ov = reduce(mul, range(n, n-r, -1))
    un = reduce(mul, range(1, r+1))
    return ov // un

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:
n=int(input())
a=list(map(int,input().split()))
counter=[]
comb=0
for k in range(n):
  counter.append(a.count(k+1))
  comb+=counter[k]*(counter[k]-1)//2
for k in range(n):
  ans=comb-counter[a[k]-1]*(counter[a[k]-1]-1)//2+(counter[a[k]-1]-1)*(counter[a[k]-1]-2)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

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

    return ret

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

unique_a = set(a)

cnt_all_conbi = 0
a_cnt_dict = {}

for i in unique_a:
    x = a.count(i)
    a_cnt_dict[i] = x
    cnt_all_conbi += permutation_count(x)

for i in a:
    x = a_cnt_dict[i]
    print(cnt_all_conbi - (x-1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools
n = int(input())
li_mas = list(map(int,input().split()))
li = np.array(li_mas)

for i in range(n):
    ind = np.ones(n, dtype = bool)
    ind[i] = False
    con = list(itertools.combinations(li[ind],2))
    res = 0
    for j in con:
        if j[0] == j[1]:
            res += 1
    print(res)

Submission status: Time Limit Exceeded

Solution code:


def main():
    import collections

    N = int(input())
    A = list(map(int, input().split()))
    cc = collections.Counter
    # c = collections.Counter(A)
    # iglist = [x for x in c if c[x] == 1]

    # for x in iglist:
    #     A.remove(x)

    for i in range(N):
        x = A[i]
        A[i] = 0
        tmp_A = cc(A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                su += pairs*(pairs-1)//2
            else:
                break
        A[i] = x
        print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

c = Counter(a).most_common()

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

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

dir = {}

for i in range(n):
  if c[i] in dir:
    dir[c[i]] += 1
  else:
    dir[c[i]] = 1

for j in range(n):
  ans = 0
  dir2 = dir.copy()
  dir2[c[j]] = dir2[c[j]] - 1
  for key,value in dir2.items():
    ans += value*(value-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nCr = {}
def cmb(n):
    if n >= 2:
        return int(n * (n-1) / 2)
    else:
        return 0

A = input().split()
len(set(A))
B = {}
C = {}
for i in set(A):
    B[i] = cmb(A.count(i) - 1)
    C[i] = cmb(A.count(i))
sC = sum(C.values())
for i in A:
    print(B[i] - C[i] + sC)

Submission status: Time Limit Exceeded

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

def solve():
  n = int(input())
  a_list = list(map(int, input().split()))
  for i in range(1, n + 1):
    all = cmb(n - 1, 2)
    ans = 0
    ball_list = collections.Counter(a_list[0:i-1] + a_list[i:n])
    key_list = list(ball_list.keys())
    for i in key_list:
      if ball_list[i] > 1:
        ans += cmb(ball_list[i], 2)
    print(ans)
  return 0

if __name__ == "__main__":
  solve()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections
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()))
ccc=[]
for i in range(n):
  ccc.append(cmb(i,2))

c=collections.Counter(a)
for aa in a:
  o=0
  for k,v in c.items():
    if aa==k:
      v-=1
    o+=ccc[v]
  print(o)

Submission status: Time Limit Exceeded

Solution code:
import collections
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
 
n = int(input())
num = list(input().split(' '))
dic = collections.Counter(num)
g = set(num)
ans = [0] * n

for i in g:
  tmp = my_index_multi(num, i)
  s = 0
  for j in dic:
    if i == j:
      s += (dic[j] - 1) * (dic[j] - 2) / 2
    else:
      s += dic[j] * (dic[j] - 1) / 2
  for j in tmp:
    ans[j] = int(s)
for r in ans:
  print(r)

Submission status: Time Limit Exceeded

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

kazuset=list(set(kazu))
atai=0
for i in kazuset:
    kosuu=kazu.count(i)
    atai=atai+kosuu*(kosuu-1)/2
   
for i in range(n):
    kotae=atai
    kosuu=kazu.count(kazu[i])
    if (kosuu >= 1):
        kotae=atai-kosuu*(kosuu-1)/2+(kosuu-1)*(kosuu-2)/2
    print(int(kotae))

Submission status: Time Limit Exceeded

Solution code:
import collections

def check(A,num):
    count = 0
    for j in A:
        n = A[j]
        if n > 1:
            if j == num:
                n -= 1
#             print("{} {}".format(j,n))

            count += n * (n - 1) / 2
    return int(count)

N = int(input())
A = list(map(int, input().split(" ")))
A2 = collections.Counter(A)
# print(A2)
for i in range(0,N):
    num = A[i]
    out = check(A2,num)
    print(out)

Submission status: Time Limit Exceeded

Solution code:
import math

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

for i in range(n):
  A=0
  latmp = la[:]
  del latmp[i]
  lai = [x for x in set(latmp) if latmp.count(x) > 1]
  for j in lai:
    m = latmp.count(j)
    A += int(m*(m-1)/2)
  print(A)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
N=int(input())
s=input().split()
list2=list(map(int,s))
for y in range(N):
    list1=copy.copy(list2)
    list1.pop(y)
    c=list(set(list1))
    a=0
    for f in c:
        h=list1.count(f)
        a+=(h*(h-1))
    print(int(a/2))

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
A = input().split(" ")

unique = set(A)

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

def caluculate(string):
  if memo[string] <= 1:
    return(0)
  else:
    return(memo[string] - 1) 
  
total = 0
memo = {}
for i in unique:
  times = A.count(i)
  memo[i] = times
  total += combination(times)

# print(memo)
# print(memo.keys())
# print(total)

for i in A:
  #  print(caluculate(i))
  print(total - caluculate(i))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
import math
import collections
def combinations_count(n):
    return math.factorial(n) // (math.factorial(n - 2) * 2)
def get_result(key, c):
    ans = 0
    for d in c.items():
        if d[0] == key:
            value = d[1]-1
        else:
            value = d[1]
        if value > 1:
            ans += combinations_count(value,)
    return int(ans)
ans = {}
tmp = a.copy()
tmp.sort()
c = collections.Counter(tmp)
for k in range(n):
    key = a[k]
    if key in ans:
        print(ans[key])
    else:
        ans[key] = get_result(key, c)
        print(ans[key])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
p = list(map(int, input().split()))
a=[0]*n
for i in range (n):
    a[(p[i]-1)]+=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:
import collections

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

setB = list(set(B))
n = len(setB)
Num = [0]*n
total = 0
for j in range(n):
  Num[j] = B.count(setB[j])
  total += Num[j]*(Num[j]-1)/2
  
for i in range(N):
  print(int(total)-Num[setB.index(B[i])]+1)

Submission status: Time Limit Exceeded

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

N = int(input())
nums = list(map(int,input().split()))
M = max(nums)
counts = [0] * (M + 1)
ser = [0] * (M + 1)
for i in range(1,M+1):
    a = nums.count(i)
    ser[i] = a
    counts[i] = int(a*(a-1)/2)

ans = [0] * M
#print(ser)
for i in range(1,M+1):
    copy = counts[:]
    copy[i] = 0
    #print(ser[i])
    c = ser[i] - 1
    S = sum(copy) + int(c*(c-1)/2)
    ans[i-1] = S

#print(ans)
for j in range(N):
    print(ans[nums[j]-1])

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.misc import comb

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

countDict = {}
minusCountDict = {}
count = 0

for key, value in tempList.items():
    countDict[key] = comb(value, 2, exact=True)
    minusCountDict[key] = comb(value - 1, 2, exact=True)

for k in range(N):
    count = 0
    for key, value in countDict.items():
        if key != AList[k]:
            count += value
        else:
            count += minusCountDict[key]
    
    print(count)

Submission status: Time Limit Exceeded

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

num_list = [0] * max(A)
for i in range(max(A)):
  num_list[i] = A.count(i+1)
  
max_A = max(num_list)
list_comb = [0] * (max_A+1)
for i in range(2, len(list_comb)):
  list_comb[i] = i*(i-1) // 2

#print(num_list)
#print(list_comb)
  
list_ans = [-1] * max(A)
for i in range(N):
  if list_ans[A[i]-1] > 0:
    print(list_ans[A[i]-1])
  else:
    ans = 0
    for j in range(max(A)):
      if A[i]-1 == j:
        ans += list_comb[num_list[j]-1]
      else:
        ans += list_comb[num_list[j]]
    list_ans[A[i]-1] = ans
    print(ans)

Submission status: Time Limit Exceeded

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

if True:
    n = int(input())
    an= list(map(int, input().split()))

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

ans = 0
c = collections.Counter(an)
datas = c.most_common()
ans = 0

for k in range(0,n):
    # an2.sort()
    # print(an2)
    ans=0
    for data in datas:
        if data[0] == an[k]:
            if data[1] > 1:
                ans += (data[1]-1) * (data[1]-2)/2
        else:
            ans += data[1] * (data[1]-1)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools

if __name__ == "__main__":
    N = int(input())
    A = input().split()
    for x in range(N):
        tmp = tuple([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:
n = int(input())
inp = list(map(int, input().split()))
l = [0] * n

for i in range(n):   #k除外
    a = inp.copy()
    a.pop(i)
    l = [0] * n


    j = -1
    while(len(a) > 0):
        j += 1
        target = a[0]
        l[j] = 1
        count = 0
        a.pop(0)
        flag = 0
        num = -1
        for k in range(len(a)):
            if len(a) == 0:
                break
                
            if flag:
                flag = 0
            else :
                num += 1 

            #print(num)
            #print(a[num], target)
            #print(a)
            if a[num] == target:
                l[j] += 1
                a.pop(num)
                flag = 1
                #print("flag = ", flag)
                count += 1

    sum = 0
    max = 0
    for m in range(n - 1):
        if l[m] == 0:
            break
        sum += l[m] * (l[m]-1) / 2

        if sum > max :
            max = sum

    print(int(sum))

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))

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

full_s = {i: combinations_count(s.count(i), 2) for i in set_s}
down_s = {i: combinations_count(s.count(i)-1, 2) for i in set_s}


for i in s:
    print(sum([full_s[k] if i != k else down_s[k] for k in set_s]))

Submission status: Time Limit Exceeded

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

from operator import mul
from functools import reduce
from collections import Counter

def cmb(n,r):
    if r > n:
        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

dict = Counter(A)

dict_cmb = {}
for k, v in dict.items():
    dict_cmb[k] = (cmb(v, 2), cmb(v-1, 2))

for a in A:
    ans = 0
    for k, v in dict_cmb.items():
        if a != k:
            ans += v[0]
        else:
            ans += v[1]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math


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


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

cnt = 0
ansx = []
ansy = []


for i in Ay:
    for j in Ay:
        if i == j and A.count(j) >= 3:
            cnt += combinations_count(A.count(j)-1, 2)
        elif i != j and A.count(j) >= 2:
            cnt += combinations_count(A.count(j), 2)
    ansx.append(i)
    ansy.append(cnt)
    cnt = 0

for i in A:
    print(ansy[ansx.index(i)])

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.buffer.readline().rstrip())
    A = list(map(int, sys.stdin.buffer.readline().split()))
    P = set(A)

    for n in range(N):
        ary = A[n + 1:] + A[:-len(A) + n]
        tmp = 0
        for p in P:
            t = ary.count(p)
            tmp += (t * (t - 1)) // 2

        print(tmp)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
data = [input() for i in range(2)]
N = int(data[0])
ball = data[1].split(" ")
for i in range(N):
  origin = ball[i]
  ball[i] = 0
  counter = 0
  for j in range(N):#数字照合
    counter_com = 0
    for k in range(N):#文字列照合
      if int(ball[k]) == j+1:
        counter_com += 1
    mth = int((int(counter_com)*int(counter_com -1))/2)
    counter += mth
  print(counter)
  ball[i] = origin

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
#A = [1, 2, 1, 4, 2, 1, 4, 1]

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

count_dic = {}
for i in c.keys():
    count = 0
    c_copy = c.copy()
    c_copy[i] = c[i]-1
    #print(c_copy)
    for k in c.keys():
        count += int(c_copy[k]*(c_copy[k]-1)/2)
        count_dic[i] = count
        #print(count)
    #print(count_dic)

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

Submission status: Time Limit Exceeded

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

#print(answer)
for i in range(n):
    print(answer-ans[s[i]]+sub[s[i]])

Submission status: Time Limit Exceeded

Solution code:
def aC(a):
  if a == 0 or a == 1:
    return 0
  return a*(a-1)/2
 
N = input()
N = int(N)
A = list(map(int, input().split()))
all = 0
count_list = []

for i in range(1,N+1):
  aa = A.count(i)
  all = all + aC(aa)
  count_list.append(aa)
    
for a in A:
  ab = count_list[a-1]
  print(int(all-aC(ab)+aC(ab-1)))

Submission status: Time Limit Exceeded

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

from collections import Counter 
import math 

res = [] # solution for each key in A
res_list = {} # solution for each value in A

# counter ver
A_c = Counter(A)
l = len(A_c)

def ncr(j):
  return math.factorial(j) // math.factorial(2) // math.factorial(j - 2) if j >= 2 else 0

# store ncr for each value 
combs = {k: ncr(v) for k, v in A_c.items()}

s = sum(combs.values())
  
for m in A_c.keys():
  res_list[m] = s - combs[m] + ncr(A_c[m] - 1)

for k in range(N):
  res.append(res_list[A[k]])
print('\n'.join([str(s) for s in res]))

Submission status: Time Limit Exceeded

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

m = int(input())
a = list(map(int,input().split()))[:m]
c = collections.Counter(a)

memo = {}

for i in a:
    res = 0
    for k,v in c.items():
        if i == k: v -= 1
        
        if v >= 2:
            if not memo.get(v):
                memo[v] = combinations_count(v, 2)

            res += memo[v]
    print(res)

Submission status: Time Limit Exceeded

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

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())
al = list(map(int, input().split()))
al_cnt = [0] * (n + 1)

for i in range(1, n + 1):
    al_cnt[i] = al.count(i)
max_cnt = 0

for j in range(1, n + 1):
    if al_cnt[j] >= 2:
        max_cnt += cmb(al_cnt[j], 2)

for l in range(n):
    if al_cnt[al[l]] >= 3:
        ans = max_cnt - cmb(al_cnt[al[l]], 2) + cmb(al_cnt[al[l]] - 1, 2)
        print(ans if ans > 0 else 0)
    else:
        ans = max_cnt - cmb(al_cnt[al[l]], 2)
        print(ans if ans > 0 else 0)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
sys.setrecursionlimit(10**8)

fact_table = [-1] * (2*10**5)

def factorial(n):
    #fact_tableが機能してない！？

    if n == 1:
        return 1
    if fact_table[n] != -1:
        return fact_table[n]
    else:
        temp = 1
        append_counter = 0
        for i in range(n,0,-1):
            if fact_table[i] != -1:
                temp = temp * fact_table[i]
                for j in range(1,append_counter+1):
                    fact_table[i+j] = fact_table[i+j-1] * (i+j)

                break
            else:
                append_counter += 1
                temp = temp * i

        fact_table[n] = temp
        return fact_table[n]


def combi(n,r):
    #階乗が多すぎる！　階乗テーブルだ！　×××メモリーエラーになる 違う！階乗の再帰によるメモリ不足だ！
    if n < r:
        return 0
    return factorial(n) // (factorial(r) * factorial(n-r))

def main():
    #2*10**5程度なら全てリストに収まる！
    #各数値の数をカウント！　k番目の数値だけ-1！
    #そして組み合わせ計算だ！nC2！
    n = int(input())
    num_counter = [0] + [0]*n #1-indexed!
    lst = [0]
    for i in [int(l) for l in input().split()]:
        t = i
        lst.append(t)
        num_counter[t] += 1

    answer_lst = [0]+[0] * n
    #even_flag = [False]+[False] * n

    #answer_lst
    for i in range(1,n+1):
        answer_lst[i] = combi(num_counter[i],2)
        #even_flag[i] = (num_counter[i] % 2 == 0) and (num_counter[i] != 0)

    tmp = sum(answer_lst)

    for i in range(1,n+1):
        k = lst[i]
        answer = tmp
        answer -= answer_lst[k]
        answer += combi(num_counter[k]-1,2)
        print(answer)



if __name__ == '__main__':
    main()

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():
    _ = 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

        count = 0
        ttt = collections.Counter(alist).items()
        for k, v in ttt:
            if k == alist[i]:
                count += cmb(v-1, 2)
            else:
                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 math

n = int(input())
a = list(map(int, input().split()))
a_ = set(a)
dic = {x:{} for x in a_}
lis = {i:a.count(i) for i in a_}
for i in a_:
    res =0
    for j in a_:
        k = lis[j]
        if i == j:
            k -= 1
            if k > 1:
                x = math.factorial(k) // (math.factorial(k-2) * math.factorial(2))
                res += x
        else:
            if k > 1:
                x = math.factorial(k) // (math.factorial(k-2) * math.factorial(2))
                res += x
    dic[i] = res
for i in a:
    print(dic[i])

Submission status: Time Limit Exceeded

Solution code:
def count_check(N):
    tmp = 0
    for i in reversed(range(1, N)):
        tmp += i
    return tmp

N = int(input())
a_list = list(map(int, input().split()))
a_unique = list(set(a_list))

for i in range(len(a_list)):
    count = 0
    check_list = a_list[:i] + a_list[i+1:]
    for j in a_unique:
        tmp = check_list.count(j)
        count += count_check(tmp)
    print(count)

Submission status: Time Limit Exceeded

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

def func01(S):
    sum_count=0
    B = set(S)
    if len(B) == N:
        sum_count = 0
    else:
        for s in B:
            count_s = S.count(s)
            sum_count += (count_s * (count_s - 1) // 2)
    print(sum_count)

for i in range(N):
    X = A[0:i] + A[i + 1:N]
    func01(X)

Submission status: Time Limit Exceeded

Solution code:
def combination(n):
    return (n * (n-1)) // 2

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

balls = dict()

for b in A:
    if b in balls:
        balls[b] += 1
    else:
        balls[b] = 1

balls_combi = dict()
balls_combi_1minus = dict()

for k, v in balls.items():
    balls_combi[k] = combination(v)
    balls_combi_1minus[k] = combination(v-1)

for b in A:
    ans = sum(balls_combi.values()) - balls_combi[b]
    ans += balls_combi_1minus[b]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def print_num(a):

    set_a = set(a)
    num = 0
    for targ_value in set_a:
        num_targ = a.count(targ_value)
        num += int(((num_targ)*(num_targ-1))/2)
    print(num)

if __name__ == "__main__":
    n = int(input())
    a = tuple(input().split())
    set_a = set(a)
    tuple_set = tuple(set_a)
    tuple_num_targ = [a.count(targ_value) for targ_value in set_a]
    tuple_c = [int(((num_targ)*(num_targ-1))/2) for num_targ in tuple_num_targ]
    all = sum(tuple_c)
    for targ_value in a:
        targ_index = tuple_set.index(targ_value)
        kazu = tuple_num_targ[targ_index]
        targ_sum = int((tuple_c[targ_index] * (kazu-2) / kazu))
        print(all - tuple_c[targ_index] + targ_sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = [x for x in a if cc[x] > 1]
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

# for k in A:
#     if k in dup:
#         print(int(al-cc[k]+1))
#     else :
#         print(int(al))
[print(int(al-cc[k]+1)) if k in dup else print(int(al)) for k in A]

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 = dict()
sumc = 0
que = deque([])
seen = set()
for i in items:
    if i not in com:
        nn = items.count(i)
        com[i] = nn

    else:

        nn = com[i]
    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:
from collections import defaultdict

mem = {}


def binomialCoef(n, k):
    if n < k:
        return 0
    if n in mem.keys():
        return mem[n]
    C = [[0 for x in range(k + 1)] for x in range(n + 1)]

    # Calculate value of Binomial Coefficient in bottom up manner
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
            # Base Cases
            if j == 0 or j == i:
                C[i][j] = 1

            # Calculate value using previously stored values
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]

    mem[n] = C[n][k]
    return mem[n]


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)
    xs_res = defaultdict(int)

    for n in xs:
        freq[n] += 1
        if freq[n] > 2:
            xs_res[n] *= 2
        if freq[n] > 1:
            xs_res[n] += 1

    res = 0

    for k in range(len(xs)):
        res = 0
        for num in freq.keys():
            res += binomialCoef(freq[num] if num != xs[k] else freq[num] - 1, 2)
        print(res)