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()))
base = []

seen = [0]*n

for i in range(n):
  seen[a[i]-1] =  seen[a[i]-1] + 1

for i in range(n):
   if (seen[i] == 0) or (seen[i] == 1):
      continue
   
   base.append((i+1)+seen[i]*n)

for i in range(n):
  count = 0
  for temp in base:
    now = temp//n
    if temp%n == a[i]:
      now = now - 1
      
    count = count + now*(now-1)//2
    
  print(int(count))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
 
def combinations_count(n, r):
  if n == r:
    return 1
  elif n < r:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

combi = collections.Counter(A)
combi_count = {}
total = 0
for k, v in combi.items():
  combi_count[k] = combinations_count(v, 2)
  total += combi_count[k]

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

Submission status: Time Limit Exceeded

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

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

for n in range(N):
  cnt = 0
  for k, v in dictionary.items():
    if k==l[n]:
      v -=1
    cnt += v*(v-1)/2
  print(int(cnt))

Submission status: Time Limit Exceeded

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

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

old_cdict = dict(c)

cdict = dict((key, value) for (key, value) in old_cdict.items() if value >= 2)

# print(cdict)

maxi = 0

for k, v in cdict.items():
    maxi += v * (v - 1) // 2

for a in alist:
    if a in resdict:
        # print('used')
        print(resdict[a])
        continue
    # print('not used')
    if a in cdict:
        cdict[a] -= 1
        temp = 0
        for k,v in cdict.items():
            temp += v*(v-1)//2
        print(temp)
        resdict[a] = temp
        cdict[a] += 1
    else:
        print(maxi)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N = int(input())
A = list(map(int, input().split()))
U = set(A)


al = [0]*(2*(10**5)+1)
for i in range(len(A)):
    al[A[i]] = al[A[i]] + 1

for i in range(len(A)):
    k = A[i]
    ans = sum([len(list(combinations(range(al[v]), 2))), len(list(combinations(range(al[v]-1), 2)))][v == k] for v in U)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 200322 D

import collections

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


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

cmblist = {}
for i in gAc:
    if gAc[i] <= 1:
        cmblist[i] = 0
    else:
        cmblist[i] = cmb(gAc[i],2)

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
# print(A)
# N = 8
# A = [1,2,1,4,2,1,4,1]
counts = {}
for a in A:
  counts[a] = counts.get(a,0) + 1
comb = {0:0,1:0}
for c in counts.values():
  t = comb.get(c,None)
  if t==None:
    comb[c] = int(c * (c-1) * 0.5)

# print(counts)
# print(comb)

dp = {}
for a in A:
  cnt = dp.get(a,-1)
  if cnt==-1:
    cnt = 0
    for k,v in counts.items():
      # print(k,v)
      if a==k:
        t = comb.get(v-1,None)
        if t==None:
          t = int((v-1) * (v-2) * 0.5)
        cnt += t
      else:
        cnt += comb[v]
    dp[a] = cnt
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = input()
al = [int(x) for x in input().split()]
aset = set(al)

# {number: count}
counts_d = {i: sum(a == i for a in al) for i in aset}

vset = set(counts_d.values())
# Count of count
v_counts = {v: sum(v == i for i in counts_d.values()) for v in vset}

whole_patterns = sum(
    (int(k * (k - 1) / 2)) * v for k, v in v_counts.items() if k > 1
)

[print(whole_patterns - counts_d[a] + 1) for a in al]

Submission status: Time Limit Exceeded

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

# 各数値の重複数辞書
counter = collections.Counter(a)

# 組み合わせ数
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 counter.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:
from collections import Counter
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
N = int(input())
List = [int(i) for i in input().split(" ")]
C =Counter(List)
D = {}
E = {}

for i in C:
    if C[i]>=2:
        D[i] = combinations_count(C[i], 2)
    else:
        D[i]=0
        
for i in C:
    if C[i]>=3:
        E[i] = combinations_count(C[i]-1, 2)
    else:
        E[i]=0

  
for j in range(N):
    l = List[j]
    
    d = D[l]
    D[l] = E[l]
    dl = list(D.values())
    print(sum(dl))
    D[l]=d

Submission status: Time Limit Exceeded

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


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

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

Submission status: Time Limit Exceeded

Solution code:
import math

def comb_count(n,r):
    if n<r:
        c=0
    else:
        c=math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
    return c

n=int(input())
A=list(map(int,input().split()))
num=[]
comb=[]
for i in range(n+1):
    num.append(A.count(i))
    comb.append(comb_count(num[i],2))
s=sum(comb)
for j in range(n):
    print(s-comb[A[j]]+comb_count(num[A[j]]-1,2))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A_list = list(map(int, input().split(" ")))
C_dic = collections.Counter(A_list)
ans_dic = {}
temp_dic = {}
for i in A_list:
    if i in ans_dic:
        print(ans_dic[i])
    else:
        temp_sum = 0
        for j in C_dic:
            if i == j:
                temp = C_dic[j]-1
                temp = temp*(temp-1)//2
                temp_sum += temp
            else:
                if j in temp_dic:
                    temp_sum += temp_dic[j]
                else:
                    temp = C_dic[j]
                    temp = temp*(temp-1)//2
                    temp_dic[j] = temp
                    temp_sum += temp

        ans_dic[i] = temp_sum
        print(temp_sum)

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


for k, ak in enumerate(a_list):
    sum_select = 0
    for key in a_dic:
        if ak == key:
            temp_a_num = a_dic[ak] - 1
            sum_select += int(temp_a_num * (temp_a_num-1) / 2)
        else:
            sum_select += int(a_dic[key] * (a_dic[key]-1) / 2)

    print(sum_select)

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)
ans = 0
for k, v in c.items():
    if v > 1:
        ans += combinations_count(v, 2)
total_ans = ans

for k_ball in A:
    if c[k_ball] > 2:
        print(
            total_ans
            - (combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2))
        )
    elif c[k_ball] > 1:
        print(total_ans - 1)
    else:
        print(total_ans)

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-Comb(L[A[i]-1],2)+Comb(L[A[i]-1]-1,2)
    print(pri)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    import numpy as np
    from collections import Counter

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

    tmp = np.array(list(c.values()))
    l1= tmp*(tmp-1)//2
    l2= (tmp-1)*((tmp-1)-1)//2
    S = np.sum(l1)

    for a in A:
        r = list(c).index(a)
        sum = S + l2[r] -l1[r]
        print(int(sum))
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#import time
#start = time.time()
N = int(input())
A = tuple(input().split())
for i in range(N):
    num = 0
    P = list(A)
    #del P[i]
    P[i] = 'A'
    #print(P)
    for j in range(N):
        for k in range(N):
            if j == k:
                a =1
            else:
                if P[j] == P[k]:
                    #print('{0}:{1}  {2}:{3}'.format(P[j],P[k],j,k))
                    num +=1
    print(int(num/2))
#end = time.time()
#print(end-start)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

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

Submission status: Time Limit Exceeded

Solution code:
def cmb2(n):
	result = n * (n - 1) / 2

	return int(result)

if __name__ == "__main__":

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

	A_set = set(A)

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

	for a in A:
		ans_d[str(a)] -= 1
		ans = 0
		for a_n in ans_d.values():
			if a_n <= 1:
				continue
			else:
				ans += cmb2(a_n)
		print(ans)
		ans_d[str(a)] += 1

Submission status: Time Limit Exceeded

Solution code:
import math
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 main():
    n = int(input())
    num_list = list(map(int, input().split()))  
    for i in range(len(num_list)):
        ansdic = {}
        pop = num_list.pop(i)
        dic = collections.Counter(num_list)
        for k, j in dic.items():
            if j < 2:
              pass
            else:
              ansdic[k] = cmb(j, 2)
        print(sum(ansdic.values()))
        ansdic = {}
        num_list.insert(i, pop)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import defaultdict
  from collections import Counter
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = Counter(a)
  e = defaultdict(int)
  
  
  
  func = lambda x: x*(x-1)//2 if x>=2 else 0
  
  ppp = ''
  
  for x in a:
    get = e.get(x)
    if get == None:
      g = d[x] - 1
      tmp = d.copy()
      tmp.pop(x)
      h = list(tmp.values())
      h.append(g)
      i = list(map(func, h))
      ans = sum(i)
      e[x] = ans
      ppp += str(ans) + '\n'
    else:
      ppp += str(get) + '\n'
    
   
  print(ppp)
      
 
if __name__=='__main__':
    main()

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

Submission status: Time Limit Exceeded

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

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

c=collections.Counter(A)
combs,total=[0]*n,0
for a in set(A):
  tmp=comb(c[a],2)
  combs[a-1]=tmp
  total+=tmp
for a in A:
  print(total-combs[a-1]+comb(c[a]-1,2))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
  l = a.copy()
  l.pop(i)
  x = list(set(l.copy()))
  for j in range(len(x)):
    c = l.count(x[j])
    ans+=c*(c-1)//2
  print(ans)
  ans = 0

Submission status: Time Limit Exceeded

Solution code:
#from scipy.special import comb
#from math import factorial
import collections
from operator import mul
from functools import reduce

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

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

#a = cmb(n,r)
c = collections.Counter(A)
l1 = []
l2 = []
l3 = []
for i in c.keys():
  l1.append(i)
  if c[i] == 1:
    l2.append(0)
    l3.append(0)
  if c[i] == 2:
    l2.append(1)
    l3.append(0)
  if c[i] >= 3:
    l2.append(cmb(c[i], 2))
    l3.append(cmb(c[i]-1, 2))    

l4 = []
for i in c.keys():
  sum = 0
  for j in c.keys():
    var_index = l1.index(j)
    if i == j:
      sum += l3[var_index]
    else:
      sum += l2[var_index]
  l4.append(sum)
#print(l4)
for i in range(N):
  var_index = l1.index(A[i])
  print(l4[var_index])

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby
import copy
import collections

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)]
    c = collections.Counter(B_list)

    for i in c.values():
        if i >= 2:
            n += i*(i-1)/2
    print(int(n))

Submission status: Time Limit Exceeded

Solution code:
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:
N = int(input())

input_1 = input()
input_2 = input_1.split()

for k in range(N):
    A = map(int, input_2)

    def equal(num):
        return num
    A = [equal(x) for x in A]

    ans = 0

    A.remove(A[k])

    for l in range(N - 1):
        ans += A.count(A[l]) - 1

    ans = ans / 2

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
def fac(m):
  return(math.factorial(m)//(2*math.factorial((m-2))))
import collections
import math
N=int(input())
A=list(map(int, input().split()))
C=collections.Counter(A)
for k in range(N):
  C[A[k]]-=1
  L=list(C)
  cnt=0
  for s in L:
    if C[s]>1:
      cnt+=fac(C[s])
  C[A[k]]+=1
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
n=int(input())
a=list(map(int,input().split()))
for i in a:
    c=collections.Counter(a)
    ans=0
    c[i] -=1
    for k in c.values():
        if k != 1:
            ans +=k*(k-1)//2
    print(ans)

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):
    num = a_list[i]
    if num in ans_keys:
        print(int(ans_dict[num]))
    else:
        buff = a_list[:i]
        buff.extend(a_list[i+1:])
        c = Counter(buff)

        tmp_ans = 0
        for value in c.values():
            if value in value_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[num] = tmp_ans
        ans_keys.append(num)
        print(int(tmp_ans))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import math
import numpy as np
import collections

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input()) 
a = np.array(list(map(int, input().split())))
ans = {}
for i in range(n):   
    if a[i] in ans.keys():
        print(ans[a[i]])
        continue
    ans[a[i]] = 0
    ind = np.ones(n, dtype=bool)
    ind[i] = False
    c = collections.Counter(a[ind])
    for k in c.keys():
        if c[k] >= 2:
            ans[a[i]] += comb(c[k], 2)
    print(ans[a[i]])

Submission status: Time Limit Exceeded

Solution code:
import copy

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

M=copy.copy(A)

i=0
n=0
k=1

for j in range(N):
    B=A.pop(i)
    #print(A)
    for l in range(N):
        a=A.count(k)
        s=int(a*(a-1)/2)
        n=n+s
        k=k+1
    print(n)
    #print(A)
    A=copy.copy(M)
    n=0
    i=i+1
    k=1

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

    Cou = collections.Counter(A)
    ans = 0
    for k in range(1, N+1):
        tmp = Cou.copy()
        tmp[A[k-1]] -= 1
        ans = 0
        for c in tmp.values():
            ans += c * (c-1) // 2
        print(ans)


    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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 += comb(val, 2, exact=True)
    
    print(ans)

Submission status: Time Limit Exceeded

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

from collections import deque
from collections import Counter
import math 
def combinations_count(n):
    return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
def combinations_count2(l):
    return sum([combinations_count(x) for x in l if x > 1])
    
q = deque(A)
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:
from itertools import combinations

N = input()
A = list(map(int, input().split()))
for i in range(len(A)):
    l = A.copy()
    l.pop(i)
    count = 0
    for i in list(combinations(l, 2)):
        if i[0] == i[1]:
            count +=1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import decimal
import heapq
import collections
import itertools

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())
d = getInputListInt()


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

dic = {}
dic_ans = {}
dic_comb = {}

for key in d:
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1

tmp_keys = []
for key in dic:
    if dic[key] < 2:
        tmp_keys.append(key)

for k in tmp_keys:
    del dic[k]

tmp_len = len(dic)

for i in range(n):
    ans = 0
    tmp_key = d[i]

    if tmp_len == 0:
        print(0)
        continue

    if tmp_key in dic_ans:
        print(dic_ans[tmp_key])
    else:
        for k in dic:
            if k == tmp_key:
                tmp_dic_idx = dic[k]-1
                if tmp_dic_idx in dic_comb:
                    ans += dic_comb[tmp_dic_idx]
                else:
                    tmp_calc = combinations_count(tmp_dic_idx, 2)
                    ans += tmp_calc
                    dic_comb[tmp_dic_idx] = tmp_calc
            else:
                tmp_dic_idx = dic[k]
                if tmp_dic_idx in dic_comb:
                    ans += dic_comb[tmp_dic_idx]
                else:
                    tmp_calc = combinations_count(tmp_dic_idx, 2)
                    ans += tmp_calc
                    dic_comb[tmp_dic_idx] = tmp_calc
        print(ans)
        dic_ans[tmp_key] = ans

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

_ = int(input())
ns = input().split(" ")
cs = collections.Counter(ns)

c = 0
for n in ns:
	for k, v in cs.items():
		if v==1:
			c += 0
		elif n==k:
			c += combinations_count(v-1, 2)
		else:
			c += combinations_count(v, 2)
	print(c)
	c = 0

Submission status: Time Limit Exceeded

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

def reduce_tmp(tmplist):
  return sum(range(tmplist))

def tmp2(tmplist):
  return sum([reduce_tmp(tmplist.count(ai)) for ai in set(tmplist)])

i = 0

[print(tmp2(a[0:k] + a[k+1:])) for k in range(n)]

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
anslist = []
 
for i in range(1, n+1):
  p = a.pop(-i)
  ans = 0
  for j in set(a):
    k = a.count(j)
    ans += k*(k-1)/2
  anslist.append(int(ans))
  a.append(p)
 
for j in reversed(anslist):
  print(j)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
num_list =  input().split(' ')
num_list2 = [int(n) for n in num_list]
num_list3 = [0]*n
num_list4 = [0]*n
num_list5 = [0]*n
for i in range(len(num_list2)):
    num_list3[num_list2[i]-1] += 1
for i in range(len(num_list2)):
    size = max(0 , num_list3[num_list2[i]-1])
    if size >= 2:
        ans = comb(size,2)
    elif size == 1:
        ans = 0
    else :
        ans = 0 
    num_list4[num_list2[i]-1] = ans

    size = max(0 , num_list3[num_list2[i]-1]-1)
    if size >= 2:
        ans = comb(size,2)
    elif size == 1:
        ans = 0
    else :
        ans = 0 
    num_list5[num_list2[i]-1] = ans
for i in range(n):
    print(num_list5[num_list2[i]-1]-num_list4[num_list2[i]-1]+np.sum(num_list4))

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    b=a[i]
    a[i]=0
    c = collections.Counter(a)
    cv=c.values()
    s=0
    for j in cv:
        s+=comb(j,2)
    print(s)
    a[i]=b

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data = 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

for k in range(n):
  data_n = data.copy()
  data_n.pop(k)
  cnt = 0
  for i in set(data_n):
    if data_n.count(i) > 1:
      cnt += cmb(data_n.count(i), 2)
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
for k in range(0,N):
  B = A[:k]+A[k+1:]
  d = collections.Counter(B)
  nums = d.values()
  ans = 0
  for v in d.values():
    if v >= 2:
      ans += v*(v-1)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

S = Counter(listA)

##key##
key = S.keys()

##dict##
my_dict = {}  

for i in key:
  out = 0
  for a in key:
    if a == i:
      num = S[a]-1
      add = (S[a]-1) * (S[a]-2) /2
      out = out + add      
    else:
      num = S[a]
      add = S[a] * (S[a]-1) /2
      out = out +add
  my_dict[i] = int(out) 

for i in listA:
  print(my_dict[i])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import Counter

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

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

for k in range(N):
    sum = 0
    for key,value in counter.items():
        if key == A[k]:
            value = value - 1
        sum = sum + combinations_count(value,2)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from collections import Counter

lines = stdin.readlines()

n = int(lines[0].rstrip())
a = [int(x) for x in lines[1].rstrip().split()]

c = Counter(a)

res = []
for k in range(n):
    part_res = 0
    for i in c.keys():
        if not i == a[k]:
            quant = c[i]
        else:
            quant = c[i] - 1
        if quant > 1:
            part_res += int((quant * (quant - 1)) / 2)
    res.extend([part_res])

for j in range(len(res)):
    print(res[j])

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    l = a[i]
    a[i] = ''
    ans = 0
    a_s = set(a)
    for j in a_s:
        c = a.count(j)
        ans += comb(c,2,exact=True)
    print(ans)
    a[i] = l

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S = int(input())
As=[int(x) for x in input().split()]
total = sum(As.count(x) for x in As)-S
for x in range(S):
    n=As.count(As[x])-1
    print(int((total-n*2)/2))

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
from math import factorial
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N = i_i()
A = i_l()
h = {}
h[0] = 0
h[1] = 0
C = collections.Counter(A)

for v in C.values():
    if v >= 2:
        h[v] = int(v * (v-1) / 2)

for x in A:
    ans = 0
    tmp = C[x] - 1
    if tmp >= 2:
        if h.get(tmp):
             ans += h.get(tmp)
        else:
            h[tmp] = int(tmp * (tmp-1) / 2)
            ans += h[tmp]
    for c in C.keys():
        if x != c:
            v = C[c]
            if v >= 2:
                if h.get(v):
                    ans += h.get(v)
                else:
                    h[v] = int(v * (v-1) / 2)
                    ans += h[v]
    print(int(ans))