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:
# import math
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())
An = [int(x) for x in input().split()]  
Cn = [An.count(i) for i in range(1,N+1)]  # どの数字が何個あるか？

# ボールを全部使えるときの合計を求める
s = 0
for i in range(N):
    if Cn[i] > 1:
        # s += int(math.factorial(Cn[i]) / (math.factorial(Cn[i]-2) * 2))
        s += int(cmb(Cn[i],2))

for k in range(N):
    print(s - (Cn[An[k]-1]-1))

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 = []
anst = 0
for i in range(M):
  t.append(A.count(C[i]))
  temp = A.count(C[i])
  if temp >=2:
    anst += temp*(temp-1)/2

jisyo = dict(zip(C, t))
    
for i in range(N):
  #print(t[C.index(A[i])])
  print(int(anst-jisyo[A[i]]+1))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000)

n = int(input())
a = [[] for i in range(n)]

a = [int(i) for i in input().split()]
b = [0]*(n+1)

for i in a:
    b[i] +=1

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

def ans(N):
    if N >= 2:
        return(cmb(N,2))
    else: return(0)

cnt=0
for j in b: cnt += ans(j)
for i in a:
    temp = cnt
    temp = temp - (ans(b[i])- ans(b[i]-1))
    print(temp)

Submission status: Time Limit Exceeded

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

parent=dict()
An=set(A)
parecou=0

for i in An:
    f=A.count(i)
    parent[i]=f
    parecou+=int((f*(f-1)/2))



#print(parecou)
for i in range(N):
    anko=parent[A[i]]
    #print(parecou-int((anko*(anko-1)/2))+int(((anko-2)*(anko-1)/2)))
    print(parecou-anko+1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ua=list(set(a))
total=sum([sum(range(a.count(x))) for x in ua])
for i in range(n):
    print(total-a.count(a[i])+1)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
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()))
c = collections.Counter(a)
val = list(c.values())
key = list(c.keys())
comb = [combinations_count(i,2) if not i < 2 else 0 for i in val]
cosum = sum(comb)
for i in range(n):
    ind = key.index(a[i])
    count = sum(comb)
    count -= val[ind]-1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def num_combinations(_n):
    return _n * (_n - 1) // 2


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

    for n in xs:
        freq[n] += 1

    res = 0

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

N = int(input())
Al = list(map(int, input().split()))
d_di = {}
d_in = {}

counter = Counter(Al)

As_l = list(set(Al))

for n in As_l:
    cnt = int(counter[n])
    if cnt > 1:
        d_di[n] = (math.factorial(cnt) // (2 * math.factorial(cnt - 2)))
    else:
        pass
    if cnt > 2:
        d_in[n] = (math.factorial(cnt-1) // (2 * math.factorial(cnt - 3)))
    else:
        d_in[n] = 1
# print(d_di)
# print(d_in)
total = sum(v for v in d_di.values())
for k in Al:
    ans = total - (counter[k]-1)
    if ans < 0:
        ans = 0
    else:
        pass
    print(ans)

Submission status: Time Limit Exceeded

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

def calc_comb(lst_A):

    elem_A = list(set(lst_A))
    sum_comb = 0
    dct_A = {}

    for A in elem_A:
        numA  = lst_A.count(A)
        combA = (numA * (numA-1)) // 2
        dct_A[A] = numA
        sum_comb += combA

    return sum_comb, dct_A



def main(N, lst_A):

    sum_comb_all, dct_A = calc_comb(lst_A)

    for A in lst_A:
        numA = dct_A[A]
        prev_combA = (numA * (numA-1)) // 2
        new_combA  = ((numA-1) * (numA-2)) // 2

        sum_comb = sum_comb_all - prev_combA + new_combA
        print(sum_comb)

    return 0

if __name__ == "__main__":

    main(N, lst_A)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math

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

# def findsame(aa):
#     s = 0
#     for a in aa:
#         s += (len([x for x in aa if x == a]) - 1)
#     return int(s / 2)

# for k in range(len(aa) - 1):
#     print(findsame(aa[:k] + aa[k+1:]))
# print(findsame(aa[:len(aa)-1]))

aaa = dict()
for a in aa:
    if a not in aaa:
        aaa[a] = len([x for x in aa if x == a])

sum = 0
for k in aaa:
    sum += int(aaa[k] * (aaa[k] - 1) / 2)

for a in aa:
    print(sum - (aaa[a] - 1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(num) for num in input().split(" ")]
a_set = set(a_list)
total_comb = 0
for i in a_set:
    i_count = a_list.count(i)
    total_comb += i_count*(i_count-1)
total_comb = int(total_comb/2)
for k in range(n):
    except_num = a_list[k]
    print(total_comb - (a_list.count(except_num)-1))

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
ai=[int(i) for i in 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
c=[ai.count(x) for x in set(ai) if ai.count(x)>1]
val=sum([cmb(x,2) for x in c])
for i in range(len(ai)):
    b=ai.count(ai[i])
    print(val-b+1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

a_count_list = []

a_dic = {}

for i in range(len(a_list)):
    key = a_list[i]
    if key in a_dic:
        a_dic[key] += 1
    else:
        a_dic[key] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    if key in a_solution_dic:
        continue
    else:
        a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
        a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    for key in a_dic:
        if ak == key:
            sum_select += a_solution_dic_at_k[key]
        else:
            sum_select += a_solution_dic[key]

    print(sum_select)

Submission status: Time Limit Exceeded

Solution code:

import collections

n = int(input())
a = list(map(int, input().split()))
c_lst = []
c = collections.Counter(a)
c_lst = c.most_common()

values, counts = zip(*c_lst)

cnt = []
for i in range(len(c_lst)):
    cnt.append((c_lst[i][1] * (c_lst[i][1] - 1)) // 2)
cnt_ = sum(cnt)
    
for i in a:
    ans = 0
    m = values.index(i)
    ans += ((c_lst[m][1] - 1) * (c_lst[m][1] - 2)) // 2
    ans += cnt_ - cnt[m]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
from collections import deque
import numpy as np

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

ca = collections.Counter(A)
cakeys = list(ca.keys())
cavalues = np.array(list(ca.values()))

#print(cakeys)
#print(np.array(list(ca.values())))

cavaluesmax = cavalues*(cavalues-1)//2
valuesum = np.sum(cavaluesmax)
#cavaluesmin = (cavalues-1)*(cavalues-2)//2
diff = cavalues-1

ans = valuesum - diff

#print(cavaluesmax)
#print(cavaluesmin)

for i in range(N):
    #print(cakeys[index])
    index = cakeys.index(A[i])
    #ans = cavaluesmin[index].sum() + cavaluesmax[:index].sum() + cavaluesmax[index+1:].sum()
    print(ans[index])

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)]
def nCr(n, r):
  if n <= 1:
    return 0
  else:
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
for i in range(N):
  B[A[i]-1] += 1
Ctot = 0
for i in range(N):
  Ctot += nCr(B[i], 2)
for i in range(N):
  current_idx = A[i]-1
  if B[current_idx] == 1:
    count = Ctot
  else:
    count = Ctot + nCr(B[current_idx]-1, 2) - nCr(B[current_idx], 2)
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
 
N = int(input())
A = list(map(int, input().split()))
 
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 = int(k*(k-1)/2)
      co += Mcon
  print(co)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
An = [int(x) for x in input().split()]  
Cn = [An.count(i) for i in range(1,N+1)]  # どの数字が何個あるか？

# ボールを全部使えるときの合計を求める
s = 0
for i in range(N):
    if Cn[i] > 1:
        s += int(math.factorial(Cn[i]) / (math.factorial(Cn[i]-2) * 2))
        # s += int(fact(Cn[i]) / (fact(Cn[i]-2) * 2))

for k in range(N):
    print(s - (Cn[An[k]-1]-1))

Submission status: Time Limit Exceeded

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

parent=dict()
An=set(A)
parecou=0

for i in An:
    f=A.count(i)
    parecou+=int((f*(f-1)/2))



#print(parecou)
for i in range(N):
    anko=A.count(A[i])
    #print(parecou-int((anko*(anko-1)/2))+int(((anko-2)*(anko-1)/2)))
    print(parecou-anko+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = []
for i in a:
    c = ao.count(i)
    ac += sum([j for j in range(1, c)])
    acount.append(c-1)

for i in ao:
    ind = a.index(i)
    print(ac-acount[ind])

Submission status: Time Limit Exceeded

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
b=Counter(a)
for i in range(n):
    c=(b-Counter([a[i]]))
    print(sum([v*(v-1)//2 for k,v in c.items()if v>=2]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
A = list(map(int, input().strip().split()))
l = max(A) + 1
c = np.zeros(max(A) + 1,dtype=np.int64)
for a in A:
    c[a] += 1
#print('c',c)
q = np.zeros(c.max() + 1,dtype=np.int64)
s = 0
for ci in c:
    if ci > 0:
        if q[ci] <= 0:
            q[ci] = ci * (ci - 1) // 2
        s += q[ci]
        if q[ci-1] <= 0:
            q[ci-1] = (ci - 1) * (ci - 2) // 2
    #print(ci,q)
#print(q)
        
#print(s)
for i in range(n):
    #print(A[i],q[max(0,c[A[i]]-1)],q[max(0,c[A[i]]-2)])
    print(s - q[max(0,c[A[i]])] + q[max(0,c[A[i]]-1)])

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 = []
anst = 0
for i in range(M):
  t.append(A.count(C[i]))
  temp = A.count(C[i])
  if temp >=2:
    anst += temp*(temp-1)/2

#print(t)
#print(anst)
#print(jisyo[1])
for i in range(N):
  #print(t[C.index(A[i])])
  print(int(anst-t[C.index(A[i])]+1))

Submission status: Time Limit Exceeded

Solution code:
import collections
_=int(input())
l = list(map(int, input().split()))
c = collections.Counter(l)
M={}
for i,v in c.items():
    M[i] =  v*(v-1)/2

for k in l:
    M_ = M.copy()
    M_[k] =  M_[k]*(c[k]-2)/c[k]
    print(int(sum(M_.values())))

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:
    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()))
A_set = list(set(A))

Ad = {a_set:A.count(a_set) for a_set in A_set}

score_sum = sum([x*(x-1)/2 for x in list(Ad.values())])

for a in A:
    print(int(score_sum - (Ad[a] - 1)))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
A=list(map(int,input().split()))
C=Counter()
C.update(A)
ans=0
L=[0]*N
for i in range(N):
    L[A[i]-1]+=1
for i in range(len(C)):
    ans+=Comb(C.most_common()[i][1],2)
for i in range(N):
    pri=ans
    pri=ans-L[A[i]-1]+1
    print(pri)

Submission status: Time Limit Exceeded

Solution code:
# bin/env/pypy3
import sys
import collections
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
c = collections.Counter(a)
#print(n,a,c)
memo_e = {}
for e in a:
    if not e in memo_e:
        c[e] -= 1
        memo_e[e] = sum(v*(v-1)//2 for v in c.values() if v > 1)
        c[e] += 1
    print(memo_e[e])

Submission status: Time Limit Exceeded

Solution code:
import math
def comb(n,r):
    if n<2:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
a = list(map(int, input().split()))
all=0
for i in range(N):
    all += comb(a.count(i+1),2)
for i in range(N):
    print(all-(a.count(a[i])-1))

Submission status: Time Limit Exceeded

Solution code:

import collections
_=int(input())
l = list(map(int, input().split()))
c = collections.Counter(l)
M={}
for i,v in c.items():
    M[i] =  v*(v-1)/2
M
for k in l:
    M_ = M.copy()
    M_[k] =  M_[k]*(c[k]-2)/c[k]
    print(int(sum(M_.values())))

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 = []
anst = 0
for i in range(M):
  t.append(A.count(C[i]))
  temp = A.count(C[i])
  if temp >=2:
    anst += math.factorial(temp)/(math.factorial(temp-2)*2)

#print(t)
#print(anst)
#print(jisyo[1])
for i in range(N):
  #print(t[C.index(A[i])])
  print(int(anst-t[C.index(A[i])]+1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
# TLE
duplication = {}
ans = 0
for x in A:
    if x in duplication:
        continue
    count = A.count(x)
    if count > 1:
        duplication[x] = count
        ans += count*(count-1)//2
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 sys
import itertools
sys.setrecursionlimit(10**8)
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:
import copy
N = int(input())
a_list = list(map(int,input().split()))

b_list = sorted(a_list)

for i in range(N):
    c_list = copy.copy(b_list)
    c_list.remove(a_list[i])

    ans = 0
    t = 0
    count = 0

    for j in range(N-1):
        if t == c_list[j]:
            count += 1
        else:
            t = c_list[j]
            ans += int(count*(count-1)/2)
            count = 1
    ans += int(count*(count-1)/2)

    print(ans)

Submission status: Time Limit Exceeded

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

dictionary = {ll: l.count(ll) for ll in set_l}
ll_dict = {}

cnt = 0
for k, v in dictionary.items():
  cnt += v*(v-1)//2

for n in range(N):
  print(cnt-(dictionary[l[n]]-1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_set = list(set(A))
Ad = {}
for a_set in A_set:
    Ad[a_set] = A.count(a_set)

score_sum = 0
for x in list(Ad.values()):
    score_sum += (x > 1) * x * (x - 1) / 2

for a in A:
    print(int(score_sum - (Ad[a] - 1)))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

# print(n)
# print(a)

l = [0] * (n + 1)
ll  = [0] * (n + 1)
for i in range(n):
    l[a[i]] += 1
    ll[a[i]] = int((l[a[i]] * (l[a[i]] - 1)) / 2)
    
# print(l)
# print(ll)
    
for i in range(n):
    v = l[a[i]]
    vv = ll[a[i]]
    vvv = int(((v - 1) * (v - 2)) / 2)
    # print(vvv)
    print(sum(ll) - (vv - vvv))

Submission status: Time Limit Exceeded

Solution code:
import math
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()))
for val in range(N):
    l = [k for k in A]
    del l[val]
    l_set = set(l)
    cnt = 0
    for i in l_set:
        x = l.count(i)
        if x > 1:
            cnt += combinations_count(x, 2)
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

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

N = int(input())
balls = [int(i) for i in input().split()]

c = Counter(balls)
combi_counter ={}
for k, v in c.items():
    if k in combi_counter.keys():
        pass
    else :
        combi_counter[v] = combination_count(v, 2)

sum_ball_combinations = 0
for k, v in c.items():
    sum_ball_combinations += combi_counter[v]

for k in balls:
    diff = combination_count(c[k], 2) - combination_count(c[k]-1, 2)
    print(sum_ball_combinations-diff)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
 
def combinations_count(n, r):
    if n < 2:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
 
n = int(input())
a_list = list(map(int, input().split()))
a_col = collections.Counter(a_list)
c_dic = {}
sm = 0
for c in a_col:
    sm += combinations_count(a_col[c], 2)
 
for a in a_list:
    if a in c_dic:
        print(c_dic[a])
    else:
        ans = sm - combinations_count(a_col[a], 2) + combinations_count(a_col[a] - 1, 2)
        print(ans)
        c_dic[a] = ans

Submission status: Time Limit Exceeded

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

ans1=0
s=0
for i in range(1,N+1):
    a=A.count(i)
    C.append(a)
for j in range(N):
    c=C[j]
    if c>1:
        s=c*(c-1)/2
        ans1=ans1+s
        
for k in range(N):
    b=A[k]-1
    d=C[b]
    if d<2:
        ans=ans1
    else:
        ans=ans1-(d-1)
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy

def check(li):
	count = 0
	cli = copy.copy(li)
	for l in li:
		del cli[0]
		for i in cli:
			if l == i:
				count += 1
	return count		 

def main():
	N = int(input())
	Aj = list(map(int, input().split()))
	for i in range(len(Aj)):
		li = copy.copy(Aj)
		del li[i]
		print(check(li))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

#Input
n = int(input())
l_a = list(map(int, input().split()))

#Process
for i in range(n):
    l_o = copy.copy(l_a)
    del l_o[i]
    l_c = list(collections.Counter(l_o).values())
    Ans = 0
    for j in range(len(l_c)):
        if(l_c[j] != 1):
            Ans += int((l_c[j] - 1) * l_c[j] / 2)
    print(Ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
D={}
x=0
lis=[]
for a in A:
	if a in D.keys():
		D[a]=D[a]+((D[a]*2)**0.5//1)+1
	else:
		D[a]=0

z=sum(D.values())
for i in range(len(A)):
	x=0
	y=((D[A[i]]*2)**0.5)//1
	x=x+z-y
	print(int(x))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split(" ")))
count = 0
import itertools
x = list(itertools.combinations(a, 2))
for v in range(len(x)):
    if x[v][0] == x[v][1]:
        count += 1
for t in range(n):
    b = a[t]
    c = a.count(b)
    print(count - c + 1)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
# A = [int(x) for x in input().split()]

for i in range(N):
    # nums = {}
    B = A[:i] + A[i + 1:]
    # for j in B:
        # if not j in nums:
            # nums[j] = 1
        # else:
            # nums[j] += 1
    nums = collections.Counter(B)
    count = 0
    for j in nums.values():
        count += j * (j - 1) // 2
    print(count)

Submission status: Time Limit Exceeded

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

parent=dict()
An=set(A)

for i in An:
    parent[i]=A.count(i)
parecou=0

#print(parent)
for i in parent:
    f=parent[i]
    parecou+=int((f*(f-1)/2))

#print(parecou)
for i in range(N):
    anko=parent[A[i]]
    #print(parecou-int((anko*(anko-1)/2))+int(((anko-2)*(anko-1)/2)))
    print(parecou-anko+1)

Submission status: Time Limit Exceeded

Solution code:
import sys
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())
l = [int(x) for x in input().split()]

cl = [0 for i in range(n)]
ll = [x for x in set(l) if l.count(x) > 1]

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    cl[i - 1] = nc - int(nc * (m - 2) / m)

# print(sum)
# print(cl)
for i in l:
    print(sum - cl[i - 1])

Submission status: Time Limit Exceeded

Solution code:
import math
import string
import collections
from collections import Counter


def readints():
    return list(map(int, input().split()))


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


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]


n = int(input())
a = readints()
# print(a)
aa = a[1:]
so_aa = sorted(aa)
c = Counter(so_aa)
d = c.most_common()
# print(d)
sum = 0
for i in range(len(d)):
    if d[0+i][1] != 1:
        sum += nCr(d[0+i][1], 2)
print(sum)
for j in range(1, n):
    aaa = a[:j]+a[j+1:]
    so_aaa = sorted(aaa)
    cc = Counter(so_aaa)
    dd = cc.most_common()
    # print(dd)
    sumsum = 0
    for k in range(len(dd)):
        # sumsum = 0
        if dd[0+k][1] != 1:
            sumsum += nCr(dd[0+k][1], 2)
    print(sumsum)

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):
    return n*(n-1)//2
    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]
memo_e = {}
for e in a:
    if not e in memo_e:
        c[e] -= 1
        b = [combinations_count(v,2) for v in c.values() if v > 1]
        memo_e[e] = sum(b)
        c[e] += 1
    print(memo_e[e])

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)

for i,item in enumerate(A):
  if B[item-1]>-1:
    print(B[item-1])
  else:
    D=copy.deepcopy(f_dic)
    D[item] -= 1
    opt=0
    for item3 in D:
      if D[item3]>=2:
        opt += D[item3]*(D[item3]-1)//2
    B[item-1]=opt
    print(opt)

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
a= list(map(int, input().split()))
s = collections.Counter(a)
ans={}
temp=0
for i in s.keys():
    s[i]=s[i]-1
    for j in s.keys():
        temp+=int(s[j]*(s[j]-1)/2)
    s[i]=s[i]+1
    ans[i]=temp
    temp=0
    
for i in range(n):
    print(ans[a[i]])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
countdic = Counter(A)
nCrdic = {}

for key,value in countdic.items():
    nCrdic[key] = (value*(value-1))//2 if value > 1 else 0
for i in A:
    if countdic[i] > 2:
        ans = sum(nCrdic.values()) - nCrdic[i] + ((countdic[i]-1)*(countdic[i]-2))//2
    else: ans = sum(nCrdic.values()) - nCrdic[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
A = list(map(int,input().split()))
d = set(A)
def comb(n,r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
if d == None:
    for _ in range(N):
        print(0)
    exit()
for i in range(0,N):
    ans = 0
    tmp = A[i]
    A[i] = None
    for j in d:
        num = A.count(j)
        if num >= 2:
            ans = ans + comb(num,2)
    A[i] = tmp
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
An = [int(x) for x in input().split()]  
Cn = [An.count(i) for i in range(1,N+1)]  # どの数字が何個あるか？

# ボールを全部使えるときの合計を求める
s = 0
for i in range(N):
    if Cn[i] > 1:
        s += int(math.factorial(Cn[i]) / (math.factorial(Cn[i]-2) * 2))

for k in range(N):
    print(s - (Cn[An[k]-1]-1))

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
from math import factorial


def main():
    n = int(input())
    a = list(map(int, input().split()))
    count = 0

    a_uniq = list(set(a))
    for v in a_uniq:
        if int(a.count(v)) > 1:
            temp =  a.count(v) * (a.count(v)-1) // 2
        else: temp = 0
        count = count + temp

    for i in range(n):
        print(count - a.count(a[i])+1)
    
if __name__ == '__main__' :
    main()

Submission status: Time Limit Exceeded

Solution code:
def c2(x):
    return x * (x - 1) // 2
 
 
n = int(input())
a = list(map(lambda i: int(i) - 1, input().split()))
b = list(a.count(i) for i in range(n))
# c = list(c2(b[i]) for i in range(n))
# d = list(c2(b[i] - 1) for i in range(n))
c = list(c2(i) for i in b)
d = list(c2(i - 1) for i in b)
 
s = sum(c)
# for k in range(n):
#     i = a[k]
#     ans = s - c[i] + d[i]
#     print(ans)
for i in a:
    print(s - c[i] + d[i])

Submission status: Time Limit Exceeded

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

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 = {}
n = int(input())
A = list(map(int, input().split()))
a = list(set(A))


for i in a:
	num = 0
	Ai = copy.copy(A)
	Ai.remove(i)
	for j in a:
		c = Ai.count(j)
		if 2<=c:
			num += cmb(c,2) 
	d[i]=num

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

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


def combinations_count_bin(n):
    if n < 2:
        return 0
    return n*(n-1) // 2

input()
s = list(map(int, input().split(' ')))
set_s = set(s)
full_s = sum([combinations_count_bin(s.count(i)) for i in set_s])

print(*[full_s - (s.count(i) - 1) for i in s], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
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())
l = [int(x) for x in input().split()]

# cl = [0 for i in range(n)]
cl = []
ll = [x for x in set(l) if l.count(x) > 1]

# print(ll)

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (m - 2) / m))

# print(sum)
# print(cl)

def my_index(l, x, default=n):
    if x in l:
        return l.index(x)
    else:
        return default

for i in l:
    # print(sum - cl[i-1])
    idx = my_index(ll, i)
    if idx == n:
        print(sum)
    else:
        print(sum - cl[idx])

Submission status: Time Limit Exceeded

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

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

# T2 = []

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


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

T3 = []
Ans = hoge(T)

for i in range(len(T)):
    T3.append(Ans-(T[i][1]-1))

for i in range(N):
    for j in range(len(T3)):
        if T[j][0] == A[i]:
            print(T3[j])
            break

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
A = [int(x) for x in input().split()]

for i in range(N):
    # nums = {}
    B = A[:i] + A[i + 1:]
    # for j in B:
        # if not j in nums:
            # nums[j] = 1
        # else:
            # nums[j] += 1
    nums = collections.Counter(B)
    count = 0
    for j in nums.values():
        count += j * (j - 1) // 2
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())
l = deque(input().split())
b = 0
a = 0
for x in set(l):
  if l.count(x)>1:
    a += l.count(x)*(l.count(x)-1)/2
for k in range(N):
  b = l.count(l[k])
  print(int(a-b+1))
  b = 0

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

b=np.zeros(N)

for i in range(N):
  b=np.zeros(N)
  tmp=0
  for j in range(N):
    if(not(j==i)):
      b[A[j]-1]=b[A[j]-1]+1
  for j in range(N):
    #print(j,b[j],b[j]*(b[j]-1)/2)
    tmp=tmp+b[j]*(b[j]-1)/2
  print(int(tmp))