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
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

from collections import deque
As = deque()

N = int(input().rstrip())
As.extend([int(x) for x in input().rstrip().split()])
d = {}
for n in As:
    if n not in d.keys():
        d[n] = 1
    else:
        d[n] += 1
ans = {}
calc_combi_count = {}
for n in As:
    if n in ans.keys():
        print(ans[n])
        continue
    d[n] -= 1
    ret = 0
    for val in d.values():
        if val > 1:
            if val in calc_combi_count.keys():
                ret += calc_combi_count[val]
            else:
                calc_combi_count[val] = combinations_count(val, 2)
                ret += calc_combi_count[val]
    ans[n] = ret
    print(ret)
    d[n] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np
from math import factorial

def num_choice_2(N):
    if N > 1:
        return factorial(N) // (factorial(N - 2)* 2)
    else:
        return 0

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

nums = np.zeros(N, dtype=int)
for a_i in A:
    nums[a_i - 1] += 1

for i in range(N):
    tmp_nums = copy.deepcopy(nums)
    tmp_nums[A[i] - 1] -= 1
    ans = np.sum([num_choice_2(x) for x in tmp_nums])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def print_num(a):

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

if __name__ == "__main__":
    n = int(input())
    a = tuple(input().split())

    for i in range(n):
        b = list(a)
        b.pop(i)
        print_num(a=b)

Submission status: Time Limit Exceeded

Solution code:
import collections
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())
L = input().split(" ")

c = collections.Counter(L)

d = {}
tmp_total = 0 

memo1 = {}
for i in c:
    if c[i] not in memo1.keys():
        tmp = comb(c[i], 2)
        memo1[i] = tmp
        tmp_total += tmp
    else:
        tmp_total += memo1[c[i]]
        
total = tmp_total
memo = {}
for i in L:
    if i not in memo.keys():
        ans = total - memo1[i] + comb(c[i]-1, 2)
        memo[i] = ans
        print(ans)
    else:
        print(memo[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

n = int(input())
A = list(map(int,input().split()))
aset = set(A)
aDict = {}
for aName in aset:
    aDict.update({aName:0})
for a in A:
    aDict[a] += 1

for k in range(n):
    ans = 0
    aDict[A[k]] -= 1
    for i in aset:
        if aDict[i] == 1 or aDict[i] == 0:
            continue
        else:
            ans += cmb(aDict[i],2)
    print(ans)
    aDict[A[k]] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
a_list = [int(i) for i in input().split()]

a_dict = [0]*max(a_list)
for a in a_list:
    a_dict[a-1] += 1

    
answer_dict = [0]*max(a_list)
for i in range(max(a_list)):
    temp_dict = copy.deepcopy(a_dict)
    temp_dict[i] -= 1
    answer = 0
    for value in temp_dict:
        if value >= 2:
            answer += value*(value-1)//2
    answer_dict[i] = answer
    
for a in a_list:
    print(answer_dict[a-1])

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
A = [int(n) for n in input().split()]
for k in range(1,N+1):
    a = [n for j, n in enumerate(A) if j!=k-1]
    print(sum([1 if c[0]==c[1] else 0 for c in combinations(a, 2)]))

Submission status: Time Limit Exceeded

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


def Comb(S):
    n = len(S)
    number = 0
    for i in range(n):
        for j in range(i+1, n):
            if S[i] == S[j]:
                number += 1
    return number


numlist = []
for i in range(N):
    number = 0
    for j in range(N):
        if A[i] == A[j]:
            number += 1
    numlist.append(number-1)
sum = Comb(A)

for k in range(N):
    print(sum-numlist[k])

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()))
Ay = list(set(A))
c = collections.Counter(A)
cnt = 0
ansx = []
ansy = []
for i in Ay:
    for x, y in c.items():
        if i == x and y >= 3:
            cnt += combinations_count(y - 1, 2)
        elif i != x and y >= 2:
            cnt += combinations_count(y, 2)
    ansx.append(i)
    ansy.append(cnt)
    cnt = 0


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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    A_num = [0] * N
    for i in range(N):
        A_num[A[i]-1] += 1
        
    A_product = [0] * N
    A_product_1 = [0] * N
    for i in range(N):
        a = A_num[i]
        A_product[i] = int(a * (a-1) / 2)
        if a == 0:
            A_product_1[i] = 0
        else:
            A_product_1[i] = int((a-1) * (a-2) / 2)

    for i in range(N):
        k = A[i]
        s = sum(A_product) - A_product[k-1] + A_product_1[k-1]
        print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

nag = len(t)
x = 0
ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A= list(map(int, input().split()))
for i in range (N):
    z=0
    for j in range (N):
        for k in range(N):
            if j!=i and k!=i and j<k and A[j]==A[k]:
                z+=1
    print(z)

Submission status: Time Limit Exceeded

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

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

for i in range(len(box)):
    tmp = box.copy()
    tmp.pop(i)
    c = Counter(tmp)
    keys = c.keys()
    cnt = 0
    for k in keys:
        #print('k, c[k]: ',k ,c[k])
        if c[k] != 1:
            cnt += int(1/2 * c[k] * (c[k] - 1))
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
for k in range(N):
    buf = A[k]
    ans = 0
    A[k] = 0
    for l in range (N-1):
        ans = ans + (A.count(l+1))*(A.count(l+1)-1)/2
    print(int(ans))
    A[k] = buf

Submission status: Time Limit Exceeded

Solution code:
import bisect
import math
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))
  
cnt=[]
for i in range(max(A)):
  cnt.append(A.count(i+1))
combs=[]
for c in cnt:
  combs.append(comb(c,2))
total=sum(combs)
for a in A:
  print(total-combs[a-1]+comb(cnt[a-1]-1,2))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
sorted_a=list(set(sorted(a)))
d={}
for i in range(n):
    y=a[i]
    if y in d:
        print(d[y])
        continue
    total=sum([sum(range(a.count(x) - (1 if x==y else 0))) for x in sorted_a])
    print(total)
    d[y]=total

Submission status: Time Limit Exceeded

Solution code:
def cal(x):
    b=0
    for i in range(N):
        a = balls.count(i+1)
        if i+1 == balls[x]:
            a -= 1
        b += a*(a-1)//2
    return b

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


for j in range(N):
    print(cal(j))

Submission status: Time Limit Exceeded

Solution code:
import collections

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


def main(n: int, a: list):
    for i in range(N):
        b = a.copy()
        b.pop(i)

        counter = collections.Counter(b)

        # print(counter)

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

        print(int(pair_cnt))


main(N, A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(map(int, input().split()))
B = set(A)
d = dict()
al = dict()
dl = dict()
ALL = 0
for b in B:
    k = A.count(b)
    d[b] = k
    al[b] = k * (k - 1) // 2
    ALL += al[b]

kl = dict()
for b in B:
    kl[b] = ALL - d[b] + 1

print("\n".join(list(str(kl[a]) for a in A)))

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(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:
N = int(input())
A=input().split()
for i in range(N):
    count=0
    for j in range(N-1):
        for k in range(j+1,N):
            # print(i,j,k, A[i], A[j], A[k])
            if i==k:
                # print(i,"i continue")
                continue
            if i==j:
                # print(i,"j continue")
                continue
            if A[j] == A[k]:
                count +=1
    print(count)
    count=0

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
from collections import Counter

def cmb(n, r):
    if n < 2:
        return 0
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
ans = {}
for a in C.keys():
    ans[a] = sum([cmb(C[i]-1, 2) if i == a else cmb(C[i], 2) for i in C.keys()])

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

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())
l = list(map(int, input().split()))

for i in range(N):
  cnt = 0
  for j in range(N):
    for k in range(j+1, N):
      if i!=j and i!=k and l[j]==l[k]:
        cnt += 1
  print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
import math

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

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

maxnum = max(A)
lis = [-1] * maxnum

for i, a in enumerate(A):
  if lis[a-1] < 0:
    newA = copy.deepcopy(A)
    newA.pop(i)
    c = collections.Counter(newA)
    total = 0
    for idx in c:
      total += combinations_count(c[idx], 2)
      
    lis[a-1] = total
    print(total)
  else:
    print(lis[a-1])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = 2 * pow(10, 5)  # N は必要分だけ用意する
mod = pow(10, 9) + 7
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)


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

count = [0 for _ in range(n+1)]
ans = [0 for _ in range(n+1)]
for i in range(n+1):
    tmp = a.count(i)
    count[i] = tmp
    ans[i] = cmb(tmp, 2)
#print(count, ans)
sum_ans = sum(ans)
#print(sum_ans)
for k in range(n):
    #count[k] -= 1
    #print(a[k], count[a[k]])
    #print(cmb(count[a[k]]-1, 2) , cmb(count[a[k]], 2))
    ans = sum_ans + cmb(count[a[k]]-1, 2) - cmb(count[a[k]], 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
d={}
n=int(input())
a=list(map(int,input().strip().split(" ")))
for i in a:
    if i not in d:
        d[i]=1
    else:
        d[i]=d[i]+1

for j in a:
    ans=0
    d[j]-=1
    for ii in d.values():
        ans+=(ii*(ii-1)//2)
    print(ans)
    d[j]+=1

Submission status: Time Limit Exceeded

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

for i in range(N):
    point = 0
    A = orig_A.copy()
    del(A[i])
    for n in list(set(A)):
        kosuu = A.count(n)
        if kosuu >= 2:
            point += (kosuu*(kosuu-1))/2
    print(int(point))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
c = [A.count(i + 1) for i in range(N)]
ans = [c[i] * (c[i] - 1) // 2 for i in range(N)]

for val in A:
  m = c[val - 1] - 1
  print(sum(ans) - m)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A
for i in range(N):
  x=A[i]
  A.pop(i)
  S=list(set(A))
  sum=0
  for j in range(len(S)):
    a=A.count(S[j])
    sum+=a*(a-1)//2
  A.insert(i,x)
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

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

for i in range(len(a_list)):
    buff = a_list[:i]
    buff.extend(a_list[i+1:])
    c = Counter(buff)

    tmp_ans = 0

    for key, value in c.items():
        if value == 1:
            continue
        else:
            tmp_ans += (value * (value - 1))/2

    print(int(tmp_ans))

Submission status: Time Limit Exceeded

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

# define combination
fact = [1, 1]
for i in range(2, N+1):
    fact.append(fact[-1]*i)
def comb(n, r):
    return fact[n] // (fact[n - r] * fact[r])

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

for i, n in enumerate(nums):
    combs[i] = comb(n, 2)

sumall = sum(combs)

ans = []
for i, a in enumerate(A):
    ans.append(sumall - combs[a] + comb(nums[a]-1, 2))

# output
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
num=[int(i) for i in input().split()]
num_dict={}
for i in num:
    if i not in num_dict:
        num_dict[i]=1
    else:
        num_dict[i]+=1
for i in range(a):
    counter=0
    tmp=num_dict[num[i]]-1
    for key,value in num_dict.items():
        if key==num[i]:
            value=tmp
        if value>=2:
            counter+=int(value*(value-1)/2)
    print(counter)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A_list = [int(_) for _ in input().split()]

for k in range(N):
    ans = 0
    test = A_list[:k] + A_list[(k+1):]
    temp = Counter(test)
    for item in temp.most_common():
        v = item[1]
        if v < 2:
            break
        ans += v*(v-1)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
if True:
    n = int(input())
    an= list(map(int, input().split()))

else:
   n=5
   an=[1,1,2,1,2]


for k in range(0,n):
    an2 = an[:]
    del an2[k]
    # print(an2)
    ans = 0
    for b1 in range(0,n-1):
        for b2 in range(0,n-1):
            if b1 == b2:
                continue
            if an2[b1] == an2[b2]:
                ans += 1
    print(int(ans/2))

Submission status: Time Limit Exceeded

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

for i in range(N):
  x = []
  for k in range(N):
    if i ==k :
      continue
    x.append(A[k])
    
    
    
  d = Counter(x)
  values,counts = zip(*d.most_common())
  ans = 0
  for j in range(len(counts)):
    ans += (counts[j]*(counts[j]-1))//2
    
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
A = np.array(list(map(int, input().split(' '))))

A_unique = np.unique(A)
num_dict = {}
total = 0
for a in A_unique:
  tmp = np.sum(A==a)
  num_dict[a] = (tmp*(tmp-1)/2, (tmp-1)*(tmp-2)/2)
  total += tmp*(tmp-1)/2

for k in range(n):
  print(int(total - num_dict[A[k]][0] + num_dict[A[k]][1]))

Submission status: Time Limit Exceeded

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

import collections

for k in range(n):
    b = a[:k]
    c = a[k+1:]
    d = b + c
    e = collections.Counter(d)
    count = 0
    for i in e.keys():
        m = e[i]
        count += m*(m-1)//2
    print(count)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    import sys
    
    N = int(input())
    A = list(map(int, sys.stdin.readline().split()))
    
    set_a = set(A)
    
    num_dic = {}
    comb_dic = {}
    comb_dic_ex = {}
    comb_sum = 0
    
    for i in set_a:
        num_dic[i] = A.count(i)
        n = num_dic[i]
        if num_dic[i] == 1:
            comb_dic[i] = 0
            comb_dic_ex[i] = 0
        else:
            comb_dic[i] = n * (n - 1) / 2
            comb_dic_ex[i] = (n - 1) * (n - 2) / 2
        comb_sum += comb_dic[i]
     
    for j in range(N):
        n = A[j]
        ans = int(comb_dic_ex[n] + comb_sum - comb_dic[n])
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python

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

    ball_dict = {}
    for i in range(n):
        if a[i] in ball_dict.keys():
            ball_dict[a[i]] += 1
        else:
            ball_dict.setdefault(a[i], 1)

    for i in range(n):
        new_dict = ball_dict.copy()
        new_dict[a[i]] -= 1
        ans = 0
        for v in new_dict.values():
            ans += v * (v - 1) // 2
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
dp = {}

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

balls = {}
fact = 1
for (i, a) in enumerate(A):
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1

for a in A:
  check_balls = deepcopy(balls)
  check_balls[a] -= 1
  res = 0
  for count in check_balls.values():
    if not count in dp:
      dp[count] = count * (count-1) // 2
    res += dp[count]
  print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
d = {}
for i in range(N):
  if A[i] in d:
    d[A[i]] += 1
  else:
    d[A[i]] = 1
#合計を求める
d_sum = {}
for i in set(A):
  d_sum[i] = int(d[i] *(d[i] - 1)/ 2)

#print(A, d,d_sum)
#print(sum(A), sum(d.values()))
for i in range(N):
  print(sum(d_sum.values()) - (d[A[i]] - 1))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
from sys import stdin

N = int(stdin.readline())
count1 = [0] * (N + 1)
count2 = [0] * (N + 1)
A = list(map(int, stdin.readline().split()))

B = list(set(A))

for b in B:
    num_of_b = A.count(b)
    if num_of_b > 2:
        count1[b] = comb(num_of_b, 2, exact=True)
        count2[b] = comb(num_of_b - 1, 2, exact=True)
    elif num_of_b == 2:
        count1[b] = 1


for a in A:
    ans = 0
    ans += sum(count1) - count1[a]
    ans += count2[a]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb
from copy import deepcopy


def main():
    n = int(input())
    As = list(map(int, input().split()))

    for a in As:
        Bs = deepcopy(As)
        Bs.remove(a)
        b_set = set(Bs)
        ans = 0
        for b in b_set:
            ans += len(list(comb(range(Bs.count(b)), 2)))
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt = {}
cnt2 = {}
check = []
for i in range(N):
  #print(cnt)
  if A[i] not in check:
    check.append(A[i])
    cnt[A[i]] = 0
    cnt2[A[i]] = 1
  else:
    cnt[A[i]] += cnt2[A[i]]
    cnt2[A[i]] += 1
ans = sum(cnt.values())
#print(ans, cnt, cnt2)
for i in range(N):
  print(ans - cnt2[A[i]]+1)

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools

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

for i in range(N):
    tmp = copy.deepcopy(A)
    tmp.pop(i)

    res = 0
    for j in list(set(tmp)):
        l = tmp.count(j)
        if(l<2):
            res += 0
        elif(l == 2):
            res += 1
        else:
            res += len(list(itertools.combinations(list(range(l)),2)))

    print(res)

Submission status: Time Limit Exceeded

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

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

for i in range(N):
    number_list = [0]
    number_stock_list = [0]
    for k in range(i):
        index = my_index(number_list, A[k])
        if index == 0:
            number_list.append(A[k])
            number_stock_list.append(1)
        else:
            number_stock_list[index] += 1

    for k in range(i + 1, N):
        index = my_index(number_list, A[k])
        if index == 0:
            number_list.append(A[k])
            number_stock_list.append(1)
        else:
            number_stock_list[index] += 1

    x = 0
    for j in range(1, len(number_stock_list)):
        x += number_stock_list[j] * (number_stock_list[j] - 1) / 2
    print(int(x))

Submission status: Time Limit Exceeded

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


def solve(N: int, A: "List[int]"):
    nums = [0] * N
    for i in A:
        nums[int(i) - 1] += 1
    
    #print(nums)
    for k in range(0, N):
        nums[int(A[k]) - 1] -= 1
        #print("aaa" + str(k))
        #print(nums)
        sum = 0
        for num in nums:
            sum += num * ( num - 1 ) // 2
        print(sum)
        nums[int(A[k]) - 1] += 1
    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(input().split())
for i in range(N):
  tmp = [int(x) for j,x in enumerate(A) if j != i]
  #print('tmp:',tmp)
  sumcounter = 0
  counter = Counter(tmp)
  #print('counter:',counter)
  for j in range(1,N+1):
    if j in counter.keys():
      n = counter.get(j)
      #print('n:',n,'j:',j)
      sumcounter += n*(n-1)/2
  print(int(sumcounter))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
n=int(input())
s=sys.stdin.readlines()
A=list(map(int,s[0].split()))
C=Counter(A)
        
for i in range(n):
    D=C.copy()
    D[A[i]]-=1
    r=0
    for v in D.values():
        r+=v*(v-1)//2
    print(r)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from math import factorial

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

amount = np.array([])
for i in range(N):
	amount = np.append(amount, np.sum(A == i + 1))

for i in range(N):
	amount[A[i] - 1] -= 1
	ans = 0
	for j in range(N):
		if amount[j] <= 1:
			pass
		else:
			ans += factorial(amount[j]) / factorial(2) / factorial(amount[j] - 2)
	print(int(ans))
	amount[A[i] - 1] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from copy import deepcopy

def combi2(num):
    return int(num*(num-1)/2)


N = int(input())
a = [int(e) for e in input().split()]

result_cache = {}
target_num_table = defaultdict(int)

for n in a:
    target_num_table[n] += 1

for n in a:
    if not n in result_cache.keys():
        table = deepcopy(target_num_table)
        table[n] -= 1
        result = sum([combi2(target) for target in table.values()])
        result_cache[n] = result
    else:
        result = result_cache[n]

    print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    if dict.get(a[i],False):
        dict[a[i]]+=1
    else:
        dict[a[i]]=1
dict2={}
for i in range(n):
    sum=0
    if dict2.get(a[i],-1)!=-1:
        sum=dict2[a[i]]
    else:
        for j in dict:
            if j==a[i]:
                if dict[j]>2:
                    sum+=((dict[j]-1)*(dict[j]-2))//2
            else:
                if dict[j]>=2:
                    sum += ((dict[j])*(dict[j]-1))//2
        dict2[a[i]]=sum
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math


def main():
    import collections

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

    for k in range(N):
        tmp_A = ignore(A, k)
        tmp_A = collections.Counter(tmp_A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                su += combinations_count(pairs, 2)
            else:
                break
        print(su)


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


def ignore(A, i):
    x = copy.copy(A)
    # del x[i]
    # return x
    x[i] = 0
    return x


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for n in range(N):
  count=0
  b=A[:n]+A[n+1:]
  for i in range(len(b)):
    for j in range(i+1,len(b)):
      if(b[i]==b[j]):
        count+=1
  print(count)

Submission status: Time Limit Exceeded

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

c = collections.Counter(As)

sum_ = 0
for key, val in c.items():
  sum_ += (val*(val-1)/2)

sum_ = int(sum_)

dic = {}
for i in list(set(As)):
  dic[i] = As.count(i)-1
      
for i in range(N):
  print(sum_-dic[As[i]])

Submission status: Time Limit Exceeded

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

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

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

    count_dict = collections.Counter(A)

    for k in range(N):
        ans = 0
        for key, value in count_dict.items():
            if key == A[k]:
                value -= 1
            ans += int(comb(value, 2))
        print(ans)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(N):
  ans = 0
  A_ = A.copy()
  A_.pop(i)
  for j in set(A_):
    ans += A_.count(j) * (A_.count(j) - 1) / 2
  print(int(ans))

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

comon = c.most_common()
ans = 0
for i in comon:
  if i[1] != 1:
  	ans += (i[1]*(i[1]-1)//2)
  
for i in a:
  if (ans-int(a.count(i))) < 0:
    print(0)
  else:
    co = a.count(i)
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import Counter
import math


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


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

    for a in A:
        cnt = 0
        for key, value in c.items():
            if key == a:
                value -= 1
            if value > 1:
                cnt += combinations_count(value, 2)
        print(cnt)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N= int(input())
A = list(map(int, input().split()))
B=[0]*N
C=[0]*N
for i in range(N):
    B[i]=A.count(i+1)
for i in range(N):
    B[i] = B[i] - 1
    for j in range(N):
        if B[j] > 1:
            C[i] = C[i] + B[j] * (B[j] - 1) / 2
    B[i] = B[i] + 1

for i in range(N):
    print(int(C[A[i]-1]))