Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
l = [int(i) for i in input().split()]

c = Counter(l)

for i in l:
    ans = 0
    for k ,v in c.items():
        if k == i:
            ans += (v-1) * (v-2) // 2
        else:
            ans += v * (v-1) //2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import add, mul
from functools import reduce


def cmb(_n, _r):
    _r = min(_n - _r, _r)
    if _r == 0:
        return 1
    return reduce(mul, range(_n, _n - _r, -1)) // reduce(mul, range(1, _r + 1))


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

counts = {}
for a in aa:
    if a in counts:
        continue
    counts[a] = aa.count(a)

cmbs = {}
for c, count in counts.items():
    if count <= 1:
        cmbs[c] = 0
    else:
        cmbs[c] = cmb(count, 2)
all_cmb = reduce(add, cmbs.values())

for a in aa:
    print(all_cmb - (counts[a] - 1))

Submission status: Time Limit Exceeded

Solution code:
def comb(n,k):
    nCk = 1
    MOD = 10**9+7
    # X
    for i in range(n-k+1, n+1):
        nCk *= i
        nCk %= MOD

    # Y
    for i in range(1,k+1):
        nCk *= pow(i,MOD-2,MOD)
        nCk %= MOD
    return nCk

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

cnt = [0]*(n+1)
total = 0
ans = 0

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

for i in range(n):
    total += comb(cnt[i], 2)

for i in range(n):
    print(total - comb(cnt[a[i]], 2) + comb(cnt[a[i]] - 1, 2))

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  params = [int(i) for i in input().split()]
  sorted_params = sorted(params)
  count_dict = {}
  
  pre = -1
  count = 0
  total_same = 0
  for i in sorted_params:    
    if pre == i:
      count += 1
      continue
      
    if count > 0:
      temp1 = 1 if count == 1 else factorial(count+1) / (factorial(count+1-2) * 2)
      total_same += temp1
      count -= 1
      temp2 = 0 if count == 0 else  1 if count == 1 else factorial(count+1) / (factorial(count+1-2) * 2)
      count_dict[pre] = [int(temp1), int(temp2)]
        
    pre = i
    count = 0
  
  if count > 0:
    temp1 = 1 if count == 1 else factorial(count+1) / (factorial(count+1-2) * 2)
    total_same += temp1
    count -= 1
    temp2 = 0 if count == 0 else  1 if count == 1 else factorial(count+1) / (factorial(count+1-2) * 2)
    count_dict[pre] = [int(temp1), int(temp2)]

  for i in params:
    if i in count_dict:
      print(int(total_same - count_dict.get(i)[0] + count_dict.get(i)[1]))
    else:
      print(int(total_same))
      
    
def factorial(value):
  result = value
  for i in range(1, value-1):
    result *= (value-i)
  
  return result

main()

Submission status: Time Limit Exceeded

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

datas = [0] * (n+1)
for a in an:
    datas[a] += 1

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

Submission status: Time Limit Exceeded

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

def comb(n):
    if n == 1:
        return 0
    else:
        return int(n*(n-1)/2)

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

origin = list(set(AS))
num = []
for i in origin:
    num.append(AS.count(i))
comb_list = [-1]*(max(num)+1)
all_num = 0
for i in num:
    if comb_list[i] == -1:
        all_num += comb(i)
        comb_list[i] = comb(i)
    else:
        all_num += comb_list[i]
    
for i in range(n):
    j = num[origin.index(AS[i])]
    ans = all_num - (j-1)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def comb(n):
    if n == 1:
        return 0
    else:
        return int(n*(n-1)/2)

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

origin = list(set(AS))
num = []
for i in origin:
    num.append(AS.count(i))
comb_list = [-1]*(max(num)+1)
all_num = 0
for i in num:
    if comb_list[i] == -1:
        all_num += comb(i)
        comb_list[i] = comb(i)
    else:
        all_num += comb_list[i]
    
for i in range(n):
    j = num[origin.index(AS[i])]
    ans = all_num - (j-1)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def comb(n):
    if n < 2:
        return 0
    else:
        return int(n*(n-1)/2)
        
def sabun(n):
    if n < 2:
        return 0
    else:
        return n-1

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

origin = list(set(AS))
num = []
for i in origin:
    num.append(AS.count(i))
comb_list = [-1]*(max(num)+1)
all_num = 0
for i in num:
    if comb_list[i] == -1:
        all_num += comb(i)
        comb_list[i] = comb(i)
    else:
        all_num += comb_list[i]
    
for i in range(n):
    j = num[origin.index(AS[i])]
    ans = all_num - sabun(j)
    print(ans)

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
sum_V = np.sum(num_list4)
for i in range(n):
    print(num_list5[num_list2[i]-1]-num_list4[num_list2[i]-1]+sum_V)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num_list =  list(map(int,input().split()))
num_list2 = [0]*n
num_list3 = [0]*n
for i in num_list:
    num_list2[i-1] += 1
for i in range(n):
    ans = num_list2[i]*(num_list2[i]-1)//2
    num_list3[i] = ans

for i in num_list:
    ans = (num_list2[i-1]-1)*(num_list2[i-1]-2)//2
    print(ans-num_list3[i-1]+sum(num_list3))

Submission status: Time Limit Exceeded

Solution code:
def comb(n):
    return n*(n-1) // 2
n = int(input())
num_list =  list(map(int,input().split()))
num_list2 = [0]*n
num_list3 = [0]*n
for i in num_list:
    num_list2[i-1] += 1
for i in range(n):
    ans = num_list2[i-1]*(num_list2[i-1]-1)//2
    num_list3[i-1] = ans

for i in num_list:
    ans = (num_list2[i-1]-1)*(num_list2[i-1]-2)//2
    print(ans-num_list3[i-1]+sum(num_list3))
  #  elif size == 1:
        #ans = 0
   # else :
    #    ans = 0 
    #num_list5[num_list2[i]-1] = ans

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

def comb(n):
    if n < 2:
        return 0
    else:
        return int(n*(n-1)/2)
        
def sabun(n):
    if n < 2:
        return 0
    else:
        return n-1

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

origin = list(set(AS))
num = []
for i in origin:
    num.append(AS.count(i))

all_num = 0
for i in num:
    all_num += comb(i)
    
for i in range(n):
    j = num[origin.index(AS[i])]
    ans = all_num - sabun(j)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def comb(n):
    return n*(n-1) // 2
n = int(input())
num_list =  list(map(int,input().split()))
num_list2 = [0]*n
num_list3 = [0]*n
for i in num_list:
    num_list2[i-1] += 1
for i in range(n):
    ans = comb(num_list2[i-1])
    num_list3[i-1] = ans

for i in num_list:
    ans = comb(num_list2[i-1]-1)
    print(ans-num_list3[i-1]+sum(num_list3))
  #  elif size == 1:
        #ans = 0
   # else :
    #    ans = 0 
    #num_list5[num_list2[i]-1] = ans

Submission status: Time Limit Exceeded

Solution code:
def comb(n, r):
    if n == -1:
        n = 0
    return n*(n-1) // 2
n = int(input())
num_list =  list(map(int,input().split()))
num_list2 = [0]*n
num_list3 = [0]*n
for i in num_list:
    num_list2[i-1] += 1
for i in range(n):
    ans = comb(num_list2[i-1],2)
    num_list3[i-1] = ans

for i in num_list:
    ans = comb(num_list2[i-1]-1,2)
    print(ans-num_list3[i-1]+sum(num_list3))
  #  elif size == 1:
        #ans = 0
   # else :
    #    ans = 0 
    #num_list5[num_list2[i]-1] = ans

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
def comb(n, r):
    if n == -1:
        n = 0
    return n*(n-1) // 2
n = int(input())
num_list =  list(map(int,input().split()))
num_list2 = [0]*n
num_list3 = [0]*n
for i in num_list:
    num_list2[i-1] += 1
for i in range(n):
    ans = comb(num_list2[i-1],2)
    num_list3[i-1] = ans

for i in num_list:
    ans = comb(num_list2[i-1]-1,2)
    print(ans-num_list3[i-1]+np.sum(num_list3))
  #  elif size == 1:
        #ans = 0
   # else :
    #    ans = 0 
    #num_list5[num_list2[i]-1] = ans

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
a_i = list(map(int, input().split()))
anslist = [0] * (num + 1)
for x in range(num):
    val = x + 1
    cnt = a_i.count(val)
    if cnt <= 1: anslist[val] = 0
    else: anslist[val] = cnt * (cnt - 1) // 2

ans = sum(anslist)
for x in range(num):
    val = a_i[x]
    cnt = a_i.count(val)
    if cnt <= 1: print(ans)
    else: 
        sa = (cnt - 1)
        print(ans - sa)

Submission status: Time Limit Exceeded

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

dict = {}
for num in a:
    if num not in dict:
        dict[num] = a.count(num)

total = 0
for key in dict:
    total += dict[key]*(dict[key]-1)/2

for num in a:
    print(int(total - dict[num]+1))

Submission status: Time Limit Exceeded

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

dict = {}
for num in a:
    if num not in dict:
        if a.count(num) >=2:
            dict[num] = a.count(num)

total = 0
for key in dict:
    total += dict[key]*(dict[key]-1)/2

for num in a:
    print(int(total - a.count(num)+1))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

ans=0
B=collections.Counter(A)

keys=list(B.keys())
values=list(B.values())

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


for j in A:
    ANS=ans
    num=values[keys.index(j)]
    ANS=ANS-(num-1)
    print(ANS)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
def c(k):
    s = 0
    for i in range(N):
        if A[i] == k:
            s += 1
    return s
x = 0
for i in range(1,N+1):
    x += c(i)*(c(i)-1)//2
for i in range(N):
    print(x+1-c(A[i]))

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
def comb(n, r):
    return n*(n-1) // 2
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(n):
    num_list3[num_list2[i]-1] += 1
for i in range(n):
   # size = max(0 , num_list3[num_list2[i]-1])
#    if size >= 2:
    ans = comb(num_list3[num_list2[i]-1],2)
  #  elif size == 1:
   #     ans = 0
   # else :
    #    ans = 0 
    num_list4[num_list2[i]-1] = ans

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

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
def comb(n, r):
    return n*(n-1) // 2
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(n):
    num_list3[num_list2[i]-1] += 1
for i in range(n):
    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

for i in range(n):
    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
    print(ans-num_list4[num_list2[i]-1]+np.sum(num_list4))

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools

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

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    for k in range(N):
        cnt = 0
        if k == N-1:
            a = A[:N-1]
        else:
            a = A[:k] + A[k+1:]
        a.sort()
        while True:
            b = list(itertools.takewhile(lambda x: x == a[0], a))
            if 1 < len(b):
                cnt += comb_count(len(b), 2)
            if len(a) < 2:
                break
            a = a[len(b):]
        print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in b.values():
  total += i*(i-1)//2
  
for i in range(n):
  to = a.count(a[i])-1
  print(total-(to*(to+1)//2)+(to*(to-1)//2))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
n = int(input())
a = list(map(int,input().split()))
for i in range(len(a)):
  o = 0
  b = a[:]
  del b[i]
  c = collections.Counter(b)
  for i in c.values():
    if i-2<1:
      r=1
    else:
      r=i-2
    o += (math.factorial(i) // math.factorial(r)//2)
  print(int(o))

Submission status: Time Limit Exceeded

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

for i in range(N):
    A0=A.copy()
    A0.pop(i)
    A0=sorted(A0)
    
    ii=0
    C=0
    L=0
    while ii<N-1:
        C=A0.count(A0[ii])
        ii+=C
        L=int(C*(C-1)/2)+L
    print(L)

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = {}
uni = list(map(int,numpy.unique(E)))
for a in range(len(uni)):
    unique[uni[a]] = E.count(uni[a])
full =0
for t in unique.values():
    if t == 1:
        continue
    full = full + t * (t-1)/2
for m in range(N):
    print(int(full - unique[E[m]] +1))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = {}
uni = list(map(int,numpy.unique(E)))
for a in range(len(uni)):
    unique[uni[a]] = E.count(uni[a])
full =0
for t in unique.values():
    full = full + t * (t-1)/2
for m in range(N):
    print(int(full - unique[E[m]] +1))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(map(int,numpy.unique(E)))
uniqueNum = []
uniqueAns = {}
full =0
depunique = []
for t in range(len(unique)):
    if E.count(unique[t]) == 1:
        continue
    else:
        depunique.append(unique[t])
        uniqueNum.append(E.count(unique[t]))
for l in range(len(uniqueNum)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(uniqueNum)):
    uniqueAns[depunique[m]] = full - uniqueNum[m]+1
for k in range(N):
    if int(E[k]) in depunique:
        print(int(uniqueAns[E[k]]))
    else:
        print(int(full))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(map(int,numpy.unique(E)))
uniqueNum = []
uniqueAns = {}
full =0
depunique = []
for t in range(len(unique)):
    if E.count(unique[t]) == 1:
        continue
    else:
        depunique.append(unique[t])
for u in range(len(depunique)):
    uniqueNum.append(E.count(depunique[u]))
for l in range(len(uniqueNum)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(uniqueNum)):
    uniqueAns[depunique[m]] = full - uniqueNum[m]+1
for k in range(N):
    if int(E[k]) in depunique:
        print(int(uniqueAns[E[k]]))
    else:
        print(int(full))

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
N = I()
A = LI()
_123 = [0]*N
_123count = [0]*N
for x in A:
    _123[x-1] += 1
_123copy = copy.copy(_123)
_123copy.sort(reverse=True)
for i,x in enumerate(_123copy):
    if x==0:
        break
    if x==1:
        _123count[i] = 0
    else:
        _123count[i] = math.factorial(x)//((math.factorial(x-2))*(math.factorial(2)))
_sum = sum(_123count)
for x in A:
    temp = _123[x-1]-1
    if temp<=0:
        print(_sum)
    elif temp==1:
        print(_sum-1)
    else:
        tempminus = math.factorial(temp+1)//((math.factorial(temp-1))*(math.factorial(2)))
        tempplus = math.factorial(temp)//((math.factorial(temp-2))*(math.factorial(2)))
        print(_sum-tempminus+tempplus)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
aset=list(set(a))
ans=0
alist=[ 0 for i in range(n)]
for _ in a:
    alist[_ - 1] += 1
for j in aset:
    k=a.count(j)
    ans += (k*(k-1)) // 2
for u in a:
    print(ans - alist[u - 1]+1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
aset=list(set(a))
ans=0
alist=[ 0 for i in range(n)]
for _ in a:
    alist[_ - 1] += 1
for j in aset:
    ans += (a.count(j)*(a.count(j)-1)) // 2
for u in a:
    print(ans - alist[u - 1]+1)

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(map(int,numpy.unique(E)))
uniqueNum = []
uniqueAns = {}
full =0
for t in range(len(unique)):
    uniqueNum.append(E.count(unique[t]))
    full = full + uniqueNum[t] * (uniqueNum[t]-1)/2
for m in range(len(unique)):
    uniqueAns[unique[m]] = full - uniqueNum[m]+1
for k in range(N):
    print(int(uniqueAns[E[k]]))

Submission status: Time Limit Exceeded

Solution code:
import copy
n=int(input())
a=list(map(int,input().split()))
aset=list(set(a))
ans=0
alist=[ 0 for i in range(n)]
for _ in a:
    alist[_ - 1] += 1
for j in aset:
    ans += (a.count(j)*(a.count(j)-1)) // 2
for u in a:
    print(ans - alist[u - 1]+1)

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(map(int,numpy.unique(E)))
uniqueNum = []
uniqueAns = []
full =0
for t in range(len(unique)):
    uniqueNum.append(E.count(unique[t]))
    full = full + uniqueNum[t] * (uniqueNum[t]-1)/2
for m in range(len(unique)):
    uniqueAns.append(full - uniqueNum[m]+1)
for k in range(N):
    print(int(uniqueAns[unique.index(E[k])]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = []
 
for t in range(len(unique)):
    uniqueNum.append(E.count(unique[t]))
uniqueAns = []
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns.append(full - uniqueNum[m]+1)
for k in range(N):
    ans.append(uniqueAns[unique.index(E[k])])
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(map(int,numpy.unique(E)))
uniqueNum = []
uniqueAns = []
full =0
for t in range(len(unique)):
    uniqueNum.append(E.count(unique[t]))
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns.append(full - uniqueNum[m]+1)
for k in range(N):
    ans.append(uniqueAns[unique.index(E[k])])
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = []
uniqueAns = []
full =0
for t in range(len(unique)):
    uniqueNum.append(E.count(unique[t]))
    full = full + uniqueNum[t] * (uniqueNum[t]-1)/2

for m in range(len(unique)):
    uniqueAns.append(full - uniqueNum[m]+1)
for k in range(N):
    ans.append(uniqueAns[unique.index(E[k])])
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = []

for t in range(len(unique)):
    uniqueNum.append(E.count(unique[t]))
uniqueAns = []
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns.append(full - uniqueNum[m]+1)
for k in range(N):
    ans.append(uniqueAns[unique.index(E[k])])
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = []
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = E.count(unique[t])
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans.append(uniqueAns[unique.index(E[k])])
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = E.count(unique[t])
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(E[k])]
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = E.count(unique[t])
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(int(E[k]))]
    print(int(ans[k]))

Submission status: Time Limit Exceeded

Solution code:
import math

def ncr(n,r):
    f = math.factorial
    return f(n) // f(r) // f(n-r)

a = int(input())
A = list(map(int, input().split()))  
d = {}
for k in A:
  if k in d:
    d[k] += 1
  else:
    d[k] = 1
 
res = 0
for k in d:
    if d[k] >= 2:
      res += ncr(d[k], 2)

for k in A:
  temp = res
  m = d[k]
  d[k] -= 1
  if m >= 2:
    temp -= ncr(m, 2)

  if d[k] >= 2:
    temp += ncr(d[k], 2)
  print(temp)
  d[k] += 1

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = E.count(unique[t])
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(int(E[k]))]
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
N = int(input())
E= list(map(int,input().split()))
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(unique[t]))
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(int(E[k]))]
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
import copy
N = int(input())
E= list(map(int,input().split()))
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(unique[t]))
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(int(E[k]))]
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
import copy
N = int(input())
E=input().split()
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(str(unique[t])))
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = uniqueAns[unique.index(int(E[k]))]
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
import copy
N = int(input())
E=input().split()
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(str(unique[t])))
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for m in range(len(unique)):
    uniqueAns[m] = full - uniqueNum[m]+1
for k in range(N):
    ans[k] = int(uniqueAns[unique.index(int(E[k]))])
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy
import copy
N = int(input())
E=input().split()
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(str(unique[t])))
uniqueAns = [0 for _ in range(len(unique))]
full =0
for l in range(len(unique)):
    full = full + uniqueNum[l] * (uniqueNum[l]-1)/2
for k in range(N):
    uni = copy.copy(uniqueNum)
    uni[unique.index(int(E[k]))] = int(uni[unique.index(int(E[k]))]) - 1
    ans[k] = full -int(uni[unique.index(int(E[k]))])
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

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

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

class UnionFindTree():
    def __init__(self, n):
        self.parent = [] # 親のノード
        self.rank = [] # 木の深さ
        self.number = [] # グループの要素数
        for i in range(n):
            self.parent.append(i)
            self.rank.append(0)
            self.number.append(1)

    # 根ノードを取得
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    # 併合
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.number[y] += self.number[x]
        else:
            self.parent[y] = x
            self.number[x] += self.number[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じノードか判別
    def same(self, x, y):
        return self.find(x) == self.find(y)

    # グループのノード数を取得
    def member_number(self, x):
        x = self.find(x)
        return self.number[x]

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

uft = UnionFindTree(N)

vals = {}
for i in range(N):
    if A[i] in vals:
        uft.unite(vals[A[i]], i)
    else:
        vals[A[i]] = i

comb = {}
group_numbers = {}
total = 0
comb_memo = {}
for k, v in vals.items():
    group_number = uft.member_number(v)
    if group_number not in comb_memo:
        comb_memo[group_number] = combination(group_number, 2)
    comb[k] = comb_memo[group_number]

    group_numbers[k] = group_number
    total += comb[k]

ans = {}
for k, v in vals.items():
    if group_numbers[k] - 1 not in comb_memo:
        comb_memo[group_numbers[k] - 1] = combination(group_numbers[k] - 1, 2)
    ans[k] = int(total - comb[k] + comb_memo[group_numbers[k] - 1])

for a in A:
    print(ans[a])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
#各番号がいくつずつあるのかを整理
#keyに番号、valueにその番号が書かれてあるボールの数
for i in range(n):
    if not s[i] in ans.keys():
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1
#iC2の総和
answer = 0
for i,j in ans.items():
    answer += j*(j-1)//2
#answer-nC2+(n-1)C2 = answer-n+1
#nはans[s[i]],よって以下の出力で求められる
for i in range(n):
    print(answer-ans[s[i]]+1)