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

number_counter = {}
for i in As:
    if str(i) in number_counter:
        number_counter[str(i)] += 1
    else:
        number_counter[str(i)] = 1
# 自分の数字以外の方法の合計は一緒。

howmanyways_in_number_by_default = {}
for k, v in number_counter.items():
    if v == 1:
        howmanyways_in_number_by_default[k] = 0
    else:
        howmanyways_in_number_by_default[k] = (v*(v-1))/2

for i in range(n):
    ways = 0
    # 自分以外の通りから自分のとこを引く
    for k, v in howmanyways_in_number_by_default.items():
        if int(k) == As[i]:
            ways += ((number_counter[str(As[i])]-1)*(number_counter[str(As[i])]-2))/2
        else:
            ways += v

    print(int(ways))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
c = list(sorted(c.items()))
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

if len(c) == 1:
    ans = [combinations_count(n-1, 2)]*n
    print(*ans)
    exit()

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
c = list(sorted(c.items()))
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

if len(c) == 1:
    for i in range(n-1):
        print(combinations_count(n-1, 2), end=' ')
    print(combinations_count(n-1, 2))
    exit()

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
c = list(sorted(c.items()))
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
defo=a[:] #めっちゃ大事
ans=0
for i in range(n):
    a.pop(i)
    p=list(set(a))
    for j in range(len(p)):
        c=collections.Counter(a)
        ans+=c[p[j]]*(c[p[j]]-1)//2
    print(ans)
    ans=0
    a=defo[:]

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    print(ans - a.count(a[i]) + 1)

Submission status: Time Limit Exceeded

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

for i in range(n):
    cnt[a[i]] -= 1
    ans = 0
    for key,val in cnt.items():
        ans += val*(val-1)//2
    print(ans)
    cnt[a[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import collections

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

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
c = list(sorted(c.items())) #昇順にソート
d = [0]*n #その数が何回出てくるか
x = 0 #全てのボールから、できる組合せの総数

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1
    d[a-1] = b

for i in li:
    y = d[i-1]
    if y > 2:
        ans = x - combinations_count(y, 2) + combinations_count(y-1, 2)
    elif y == 2:
        ans = x - 1
    else:
        ans = x
    print(ans)

Submission status: Time Limit Exceeded

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

cntA={} # 同じ数字が何個あるか
for a in A:
    if a in cntA.keys():
        cntA[a]+=1
    else:
        cntA[a]=1

cntn={} #同じ数字から2種類取り出す組み合わせの数
for a in cntA.keys():
    cntn[a]=int(cntA[a]*(cntA[a]-1)/2)
# print(cntA)
# print(cntn)

ans=0
ansn={}
#k番目を除いた場合に同じ数字から2種類取り出す組み合わせの数
for i in cntA.keys(): 
    tmpn=copy.deepcopy(cntn)
    tmpn[i]=int((cntA[i]-1)*(cntA[i]-2)/2)
    for j in cntA.keys():
        ans=ans+tmpn[j]
    ansn[i]=ans
    ans=0

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

Submission status: Time Limit Exceeded

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

nums = list(set(a))
for _ in range(n):
  ans = 0
  t = a.pop(0)
  for j in nums:
    s = a.count(j)
    ans += s*(s-1)//2
  print(ans)
  a.append(t)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for k in range(N):
  a=A[:k]+A[k+1:]
  a.sort()
  gr=itertools.groupby(a)
  s=0
  for _,group in gr:
    l=len(list(group))
    if l>=2:
      s+=int(l*(l-1)/2)
  print(s)

Submission status: Time Limit Exceeded

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

balls = []
for k in range(n):
    ball=[a[i] for i in range(n) if i!=k]
    balls.append(ball)

ans = []
for k in range(n):
    cnt=0
    for i in range(len(balls)-1):
        for j in range(i+1,len(balls)-1):
            if balls[k][i]==balls[k][j]:
                cnt+=1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 00:07:17 2020

@author: shinba
"""
import collections

def conbination(n):
    return int(n*(n-1)/2)

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

B = collections.Counter(A)

key = []
val = []

for i,j in B.items():
    key.append(i)
    val.append(j)

for i in range(len(B)):
    W = 0
    for j in range(len(B)):
        if i == j:
            val[j] -= 1
            s = j
        W += conbination(val[j])
    B[key[i]] = W
    val[s] += 1

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

Submission status: Time Limit Exceeded

Solution code:
import copy
# In=[list(map(int,i.split())) for i in Input.split('\n')]
# N=In[0][0]
# A=In[1]
N=int(input())
A=list(map(int,input().split()))
counter=[0 for i in range(N+1)]
ans2=[0 for i in range(N+1)]
B=[]
for i in A:
    counter[i]+=1
ans=[0 for i in range(N+1)]
for i,value in enumerate(counter):
    if value>0:
        ans[i]=(value*(value-1)//2)
        ans2[i]=((value-1)*(value-2)//2)
for i in A:
    ans1=copy.deepcopy(ans)
    ans1[i]=ans2[i]
    print(sum(ans1))

Submission status: Time Limit Exceeded

Solution code:
import copy
#In=[list(map(int,i.split())) for i in Input.split('\n')]
#N=In[0][0]
#A=In[1]
N=int(input())
A=list(map(int,input().split()))
counter=[0 for i in range(N+1)]
ans2=[0 for i in range(N+1)]
B=[]
for i in A:
    counter[i]+=1
ans=[0 for i in range(N+1)]
for i,value in enumerate(counter):
    if value>0:
        ans[i]=(value*(value-1)//2)
        ans2[i]=((value-1)*(value-2)//2)
for i in A:
    ans1=copy.deepcopy(ans)
    ans1[i]=ans2[i]
    print(sum(ans1))

Submission status: Time Limit Exceeded

Solution code:
stdin =open(0).read().split('\n')
N=int(stdin[0])
A=list(map(int,stdin[1].split()))
d=dict(zip(set(A), [0]*len(set(A))))
ans=0
for i in set(A):
    d[i]=A.count(i)
    ans+=d[i]*(d[i]-1)//2
for a in A:
    num = d[a]
    print(ans-num*(num-1)//2+(num-1)*(num-2)//2)

Submission status: Time Limit Exceeded

Solution code:
stdin =open(0).read().split('\n')
N=int(stdin[0])
A=list(map(int,stdin[1].split()))
d=dict(zip(set(A), [0]*len(set(A))))
ans=0
for i in set(A):
    d[i]=A.count(i)
    ans+=d[i]*(d[i]-1)//2
for j in range(N):
    num = d[A[j]]
    print(ans-num*(num-1)//2+(num-1)*(num-2)//2)

Submission status: Time Limit Exceeded

Solution code:
import copy
# In=[list(map(int,i.split())) for i in Input.split('\n')]
# N=In[0][0]
# A=In[1]
N=int(input())
I=list(map(int,input().split()))
for j in range(N):
    counter=[0 for i in range(N+1)]
    A=copy.deepcopy(I)
    A.pop(j)
    B=[]
    for i in A:
        counter[i]+=1
    for i in counter:
        if i>1:
            B.append(i*(i-1)/2)
    print(int(sum(B)))

Submission status: Time Limit Exceeded

Solution code:
def comb(n):
    if n<=1:return 0
    r=1
    for i in range(n,n-2,-1):
       r*=i
    return r//2
stdin =open(0).read().split('\n')
N=int(stdin[0])
A=list(map(int,stdin[1].split()))
d=dict(zip(set(A), [0]*len(set(A))))
for i in set(A):
    d[i]=A.count(i)
for j in range(N):
    ans=0
    for k, v in d.items():
        if A[j]==k:
            ans+=comb(v-1)
        else:
            ans+=comb(v)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def comb(n):
    if n==1:return 0
    r=1
    for i in range(n,n-2,-1):
       r*=i
    return r//2
stdin =open(0).read().split('\n')
N=int(stdin[0])
A=list(map(int,stdin[1].split()))
ans=[0]*N
for i in range(N):
    A=list(map(int,stdin[1].split()))
    A.pop(i)
    for j in set(A):
        ans[i]+=comb(A.count(j))
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import copy
from math import factorial as f
# In=[list(map(int,i.split())) for i in Input.split('\n')]
# N=In[0][0]
# A=In[1]
N=int(input())
I=list(map(int,input().split()))
for j in range(N):
    counter=[0 for i in range(N+1)]
    A=copy.deepcopy(I)
    A.pop(j)
    B=[]
    for i in A:
        counter[i]+=1
    for i in counter:
        if i>1:
            B.append(f(i)/(f(2)*f(i-2)))
    print(int(sum(B)))

Submission status: Time Limit Exceeded

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

c=0
for i in dic.values():
  c+=i*(i-1)/2
for j in range(1,N+1):
  d=c-dic[A[j-1]]+1
  print(int(d))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
dic={}
for i in set(A):
  dic[i]=A.count(i) 
def ans(k,dic):
  dic[A[k-1]]+=-1
  c=0
  for i in dic.values():
    c+=i*(i-1)/2
  dic[A[k-1]]+=1  
  return int(c)
for k in range(1,N+1):
  print(ans(k,dic))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
dic={}
for i in range(len(A)):
  if A[i] in dic:
    dic[A[i]]+=1
  else:
    dic[A[i]]=1    
def ans(k,dic):
  dic[A[k-1]]+=-1
  c=0
  for i in dic.values():
    c+=i*(i-1)/2
  dic[A[k-1]]+=1  
  return int(c)
for k in range(1,N+1):
  print(ans(k,dic))

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

nc=[-1]*(n+1)
def nConb(n):
  if nc[n] != -1:
    return nc[n]
  nc[n]=n*(n-1)//2
  return nc[n]
  
for i in range(n):
  temp=0
  for k,v in c.items():
    if k == a[i]:
      temp+=nConb(v-1)
    else:
      temp+=nConb(v)
  print(temp)

Submission status: Time Limit Exceeded

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

b = {}
for a in A:
    if a not in b:
        b[a] = 0
        
    b[a] += 1


for a in A:
    count = 0
    for c in b:
        n = b[c]
        if a == c:
            n -= 1
        
        count += n * (n - 1) // 2
    print(count)

Submission status: Time Limit Exceeded

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

a_types = set(A)
count_dict_fake = [0] * N
for each in a_types:
    count_dict_fake[each - 1] = A.count(each)

count_dict = [0] * N
for each in A:
    count_dict[each - 1] += 1

all_types = 0
for value in count_dict:
    all_types += value * (value - 1) // 2

# result_dict = {}
# for each in a_types:
#     result_dict[each] = all_types - count_dict[each] + 1

for a in A:
    print(all_types - count_dict[a - 1] + 1)

Submission status: Time Limit Exceeded

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

a_types = set(A)

count_dict = {}
for each in a_types:
    count_dict[each] = A.count(each)

all_types = 0
for value in count_dict.values():
    all_types += value * (value - 1) // 2

# result_dict = {}
# for each in a_types:
#     result_dict[each] = all_types - count_dict[each] + 1

for a in A:
    print(all_types - count_dict[a] + 1)

Submission status: Time Limit Exceeded

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

a_types = set(A)

count_dict = {}
for each in a_types:
    count_dict[each] = A.count(each)

all_types = 0
for value in count_dict.values():
    all_types += value * (value - 1) // 2

result_dict = {}
for each in a_types:
    result_dict[each] = int(all_types - count_dict[each] + 1)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import itertools

num = defaultdict(lambda : 0)
n = int(input())
a = [int(i) for i in input().split()]
for i in a:
    num[i] += 1
memo = {i: 0 for i in num.keys()}
total = 0

for i, j in num.items():
    x = len([k for k in itertools.combinations(range(j), 2)])
    memo[i] = x
    total += x
    
for i, j in enumerate(a):
    y = num[j]
    z = memo[j]
    
    if y == 1:
        print(total)
    else:
        print(total - z + len([k for k in itertools.combinations(range(y-1), 2)]))

Submission status: Time Limit Exceeded

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

a_types = set(A)

count_dict = {}
for each in a_types:
    count_dict[each] = A.count(each)

all_types = 0
for value in count_dict.values():
    all_types += value * (value - 1) / 2

result_dict = {}
for each in a_types:
    result_dict[each] = int(all_types - count_dict[each] + 1)

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

Submission status: Time Limit Exceeded

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

a_types = set(A)

count_dict = {}
for each in a_types:
    count_dict[each] = A.count(each)

all_types = 0
for value in count_dict.values():
    all_types += value * (value - 1) / 2

for a in A:
    print(int(all_types - count_dict[a] + 1))
# no_execlusion_results = {}
# for each in a_types:
#     no_execlusion_results[each] = 
# result_dict = {}
# for each in a_types:
#     tmp_dict = copy.deepcopy(count_dict)
#     tmp_dict[each] = count_dict[each] - 1
#     combinations = 0
#     for value in tmp_dict.values():
#         combinations += int(value * (value - 1) / 2)
#     result_dict[each] = combinations
# 
# print(result_dict)
# for a in A:
#     print(result_dict[a])

Submission status: Time Limit Exceeded

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

a_types = set(A)

count_dict = {}
for each in a_types:
    count_dict[each] = A.count(each)

result_dict = {}
for each in a_types:
    tmp_dict = copy.deepcopy(count_dict)
    tmp_dict[each] = count_dict[each] - 1
    combinations = 0
    for value in tmp_dict.values():
        combinations += int(value * (value - 1) / 2)
    result_dict[each] = combinations

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

Submission status: Time Limit Exceeded

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

a_types = set(A)

count_dict = {}
for each in a_types:
    count_dict[each] = A.count(each)

for a in A:
    tmp_dict = copy.deepcopy(count_dict)
    tmp_dict[a] = count_dict[a] - 1
    combinations = 0
    for value in tmp_dict.values():
        combinations += int(value * (value - 1) / 2)
    print(combinations)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

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

count = 0
B = copy.copy(A)
Num = list(set(B))
for num in Num:
    count += nC2(B.count(num))
#print(count)

for k in range(n):
    B = copy.copy(A)
    num = B.pop(k)
#    print()
#    print(k, num, B)
    print(count - B.count(num))

Submission status: Time Limit Exceeded

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

dic = dict()
for i in a:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

cnt = 0
dic_v = list(dic.values())
for i in range(len(dic_v)):
    x = dic_v[i]
    cnt += int(x*(x-1)/2)
    
def count(k):
    cnt = -1
    for i in range(n):
        if a[i] == a[k]:
            cnt += 1
    return cnt

for k in range(n):
    print(cnt - count(k))

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

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

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

for k in range(n):
    B = copy.copy(A)
    B.pop(k)
#    print()
#    print(k, B)
    Num = list(set(B))
    
    count = 0
    for num in Num:
        count += nC2(B.count(num))
#        print(num, B.count(num))
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

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

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

for k in range(n):
    B = copy.copy(A)
    B.pop(k)
#    print()
#    print(k, B)
    Num = list(set(B))
    
    count = 0
    for num in Num:
        count += combination(B.count(num), 2)
#        print(num, B.count(num))
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

cnt = np.array([0]*n)

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

c = cnt*(cnt-1)//2
sum_ = np.sum(c)
cnt = np.where(cnt>=1,sum_-cnt+1,cnt)

for i in range(n):
    print(cnt[a[i]-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

cnt = np.array([0]*n)
ans = 0

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

c = cnt*(cnt-1)//2

for i in range(n):
    ans = np.sum(c) -cnt[a[i]-1] +1
    print(ans)

Submission status: Time Limit Exceeded

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

for k in range(N):
  S = sorted(A[:k]+A[k+1:])
  sum = 0
  num = 1
  for j in range(N-2):
    if S[j] == S[j+1]:
      num += 1
      if j == N-3:
        sum += num*(num-1)//2
        num = 1
    else:
      sum += num*(num-1)//2
      num = 1
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

#要素の数を数えたい場合（key:数）
n=int(input())
li = list(map(int,input().split()))

dic={}
for i in li:
    if i not in dic:
        dic[i] = 1
    elif dic[i] >= 1:
        dic[i] += 1
        
#print(dic)

for i in range(n):
    cnt = 0
    for j in dic:
        cnt += dic[j]*(dic[j]-1)/2
    #print(math.floor(cnt))

for i in range(n):
    print(math.floor(cnt - dic[li[i]]+1))

Submission status: Time Limit Exceeded

Solution code:
import math

#要素の数を数えたい場合（key:数）
n=int(input())
li = list(map(int,input().split()))

dic={}
for i in li:
    if i not in dic:
        dic[i] = 1
    elif dic[i] >= 1:
        dic[i] += 1
        
#print(dic)

for i in range(n):
    cnt = 0
    dic[li[i]] -= 1
    for j in dic:
        cnt += dic[j]*(dic[j]-1)/2
    print(math.floor(cnt))
    dic[li[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a=list(map(int,input().split()))
c=Counter(a)
for i in range(n):
    ans = 0
    tmp = a[i]
    for b,j in c.items():
        if b == tmp:
            ans += ((j-1)*(j-2))//2
        else:
            ans += (j*(j-1))//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

def ncr(n,r):
    return factorial(n)//(factorial(r)*factorial(n-r))

N = int(input())
l = list(map(int,input().split()))
h = {}
for i in l:
    if i in h:
        h[i] += 1
    else:
        h[i] = 1

def solve():
    global l
    global h
    global N
    for i in range(N):
        h[l[i]] -= 1
        m = 0
        for j in h.values():
            if j >= 2:
                m += ncr(j,2)
        print(m)
        h[l[i]] += 1
solve()

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
As = list(map(int,input().split()))
Cs = collections.Counter(As)
#rlt = [Cs[i]*(Cs[i]-1)//2 for i in Cs]
#print(Cs)
for i in range(N):
  Cs2 = Cs.copy()
  rlt = (Cs[As[i]] - 1)*(Cs[As[i]] - 2)//2
  del Cs2[As[i]]
  for key in Cs2:
    rlt = rlt + Cs2[key]*(Cs2[key] - 1)//2
  print(rlt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
A = list(map(int,input().split()))
K = [0]*N

for k in range(N):
  B = A[:k]+A[k+1:]
  for v in itertools.combinations(B,2):
    if v[0] == v[1]:
      K[k] += 1
for i in range(N):
  print(K[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def main():
    N = int(input())
    A = [ a for a in input().split()]
    pair = defaultdict(int)
    for i in range(N):
        pair[A[i]] += 1

    for i in range(N):
        pair[A[i]] -= 1
        comb = 0
        for k, v in pair.items():
            if v >= 2:
                comb += v*(v-1)//2
        print(comb)
        pair[A[i]] += 1

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# 君指先跃动の光は、私の一生不変の信仰に、唯私の超电磁炮永生き！
from sys import stdin, stdout
import math
import bisect
from collections import defaultdict
import datetime
import random

n = int(stdin.readline().strip())

arr = list(map(int, stdin.readline().strip().split()))

d = defaultdict(int)

ans = 0

for each in arr:
    d[each] += 1

for key, value in d.items():
    ans += value * (value - 1) / 2
    
for each in arr:
    tmp = ans - (d[each] * (d[each] - 1) / 2)
    tmp += ((d[each] - 1) * (d[each] - 2) / 2)
    stdout.writelines(str(int(tmp)) + '\n')

Submission status: Time Limit Exceeded

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

n = readInt()
a = readInts()

d = defaultdict(int)

ans = 0

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

for k in range(0,n):
	e = d.copy()
	e[a[k]]-=1
	ans = 0
	for j in e:
		ans+=comb(e[j],2)
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split()]
d = collections.defaultdict(int)
dic = collections.defaultdict(int)
dic_1 = collections.defaultdict(int)
c_ls = [1]
c_ls_inv = [1]
for i in range(1,n+1):
    c_ls.append(c_ls[-1]*i)
    c_ls_inv.append(c_ls_inv[-1]/i)
    d[a[i-1]] += 1

for key in d.keys():
    try:
        dic[key] = c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2]
    except:
        dic[key] = 0
    try:
        dic_1[key] = c_ls[d[key]-1]*c_ls_inv[d[key]-3]*c_ls_inv[2]
    except:
        dic_1[key] = 0
sm = sum(dic.values())
for i in range(n):
    print(int(sm-dic[a[i]] + dic_1[a[i]]))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split()]
d = collections.defaultdict(int)
dic = collections.defaultdict(int)
dic_1 = collections.defaultdict(int)
c_ls = [1]
c_ls_inv = [1]
for i in range(1,n+1):
    c_ls.append(c_ls[-1]*i)
    c_ls_inv.append(c_ls_inv[-1]/i)
    d[a[i-1]] += 1

for key in d.keys():
    try:
        dic[key] = int(c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2])
    except:
        dic[key] = 0
    try:
        dic_1[key] = int(c_ls[d[key]-1]*c_ls_inv[d[key]-3]*c_ls_inv[2])
    except:
        dic_1[key] = 0
sm = sum(dic.values())
for i in range(n):
    print(sm-dic[a[i]] + dic_1[a[i]])

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split()]
d = collections.defaultdict(int)
dic = collections.defaultdict(int)
dic_1 = collections.defaultdict(int)
c_ls = [1]
c_ls_inv = [1]
for i in range(1,n+1):
    c_ls.append(c_ls[-1]*i)
    c_ls_inv.append(c_ls_inv[-1]/i)

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

for key in d.keys():
    try:
        dic[key] = int(c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2])
    except:
        dic[key] = 0
    try:
        dic_1[key] = int(c_ls[d[key]-1]*c_ls_inv[d[key]-3]*c_ls_inv[2])
    except:
        dic_1[key] = 0
sm = sum(dic.values())
for i in range(n):
    print(sm-dic[a[i]] + dic_1[a[i]])

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split()]
d = collections.defaultdict(int)
dic = collections.defaultdict(int)
dic_1 = collections.defaultdict(int)
c_ls = [1]
c_ls_inv = [1]
for i in range(1,n+1):
    c_ls.append(c_ls[-1]*i)
    c_ls_inv.append(c_ls_inv[-1]/i)


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

for key in d.keys():
    if d[key] >= 3:
        dic[key] = int(c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2])
        dic_1[key] = int(c_ls[d[key]-1]*c_ls_inv[d[key]-3]*c_ls_inv[2])
    elif d[key] == 2:
        dic[key] = int(c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2])
        dic_1[key] = 0
sm = sum(dic.values())
for i in range(n):
    print(sm-dic[a[i]] + dic_1[a[i]])

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = [int(i) for i in input().split()]
d = collections.defaultdict(int)
dic = collections.defaultdict(int)
dic_1 = collections.defaultdict(int)
c_ls = [1]
c_ls_inv = [1]
for i in range(1,n+1):
    c_ls.append(c_ls[-1]*i)
    c_ls_inv.append(c_ls_inv[-1]/i)


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

for key in d.keys():
    if d[key] >= 3:
        dic[key] = int(c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2])
        dic_1[key] = int(c_ls[d[key]-1]*c_ls_inv[d[key]-3]*c_ls_inv[2])
    elif d[key] == 2:
        dic[key] = int(c_ls[d[key]]*c_ls_inv[d[key]-2]*c_ls_inv[2])
        dic_1[key] = 0

for i in range(n):
    print(sum(dic.values())-dic[a[i]] + dic_1[a[i]])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, Counter

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

d = defaultdict(int)

all = 0
for i, a in enumerate(A):
    # i=1;val=A[i]
    cnt = Counter(A[i + 1:])
    all += cnt[a]

val_cnt = Counter(A)
for a in A:
    # a=1
    print(all - (val_cnt[a] - 1))

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    assert len(A) == N
    ball = collections.Counter(A)
    for k in range(N):
        total = []
        ball[A[k]] -= 1
        total = [combinations_count(i, 2) for i in ball.values() if i >= 2]
        ball[A[k]] += 1
        print(sum(total))


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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
As = list(map(int, stdin.readline().rstrip().split()))

nums = []
ans = 0

s = set(As)
s = list(s)


for i in range(len(s)):
    num = As.count(s[i])
    nums.append(num)
    p = num * (num-1) / 2
    ans += p

for k in range(n):
    num = nums[s.index(As[k])] - 1
    p = num*(num-1)/2
    bigp = (num+1)*num/2
    print(int(ans - bigp + p))

Submission status: Time Limit Exceeded

Solution code:
#import copy
import numpy as np

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

A_list = np.array([0]* (N +1))

for i in range(N):
    A_list[A[i]]+=1

for j in range(N):
    A_list_minus  = np.copy(A_list)
    A_list_minus[A[j]] -=1
    print(sum(np.array(A_list_minus)*(np.array(A_list_minus)-1)//2))

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np

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

A_list = np.array([0]* (N +1))

for i in range(N):
    A_list[A[i]]+=1

for j in range(N):
    A_list_minus  = copy.deepcopy(A_list)
    A_list_minus[A[j]] -=1
    print(sum(np.array(A_list_minus)*(np.array(A_list_minus)-1)//2))

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np

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

A_list = [0]* (N +1)

for i in range(N):
    A_list[A[i]]+=1

for j in range(N):
    A_list_minus  = copy.deepcopy(A_list)
    A_list_minus[A[j]] -=1
    print(sum(np.array(A_list_minus)*(np.array(A_list_minus)-1)//2))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
l=[int((L.count(i))*(L.count(i)-1)/2) for i in range(1,N+1)]
for j in range(N):
    if l[L[j]-1]==0:
        print(int(sum(l)))
    else:
        print(int(sum(l)-L.count(L[j])+1))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
l=[int((L.count(i))*(L.count(i)-1)/2) for i in range(1,N+1)]
for j in range(N):
    if l[L[j]-1]==0:
        print(int(sum(l)))
    else:
        print(int(sum(l)-l[L[j]-1]+(L.count(L[j])-1)*(L.count(L[j])-2)/2))