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
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()))
S = Counter(A)
B = [[0,0,0]]*N
all_sum = 0
#print(S)
for i in S.keys():
    n = A.count(i)
    a = n*(n-1)//2
    if a >= 0 and (n-1)*(n-2)//2 >= 0:
        B[i-1]=(i-1,a,(n-1)*(n-2)//2)#キー、Nの組合せ、N-1の組合せ
    else:
        B[i-1]=(i,a,0)
    all_sum += a
    #print(all_sum,"all")
#print(B)
for j in range(N):
    print(all_sum-B[A[j]-1][1]+B[A[j]-1][2])

Submission status: Time Limit Exceeded

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

for i in range(N):
    a=A.pop(i)
    ans=0
    for j in range(1,N+1):
        n=A.count(j)
        if n>=1:
            ans += n*(n-1)/2
    print(int(ans))
    A.insert(i,a)

Submission status: Time Limit Exceeded

Solution code:
def comb(n, r): 
    if n<r:
        return 0
    return (fact(n) // (fact(r)  
                * fact(n - r))) 
  
def fact(n):   
    res = 1
    if n<=1:
        return 1
    for i in range(2, n+1): 
        res = res * i
    return res 
  

n = int(input())
a = list(map(int,input().split()))
maxa = max(a)
hasha = [0]*(maxa+1)
for i in a:
    hasha[i]+=1
seta = set(a)
mydict=dict()
sum=0
for i in seta:
    mydict[i] = comb(hasha[i],2)
for i in mydict:
    sum+=mydict[i]
for i in range(n):
    ans = sum + comb(hasha[a[i]]-1, 2) - comb(hasha[a[i]],2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=set(a)
cnt=0
for i in b:
  x=a.count(i)
  c=x*(x-1)//2
  cnt+=c
for i in a:
  x=a.count(i)
  print(cnt+(1-x))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

C = collections.Counter(A)

for a in A:
    s = 0
    for c in C:
        if c == a:
            s += (C[c] - 1) * (C[c] - 2) // 2
        else:
            s += C[c] * (C[c] - 1) // 2
    print(s)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[0]*(max(a)-min(a)+1)
for i in range(n):
  b[a[i]-min(a)]+=1

def cc(x):
  return x*(x-1)//2

l=len(b)
sum=0

for i in range(l):
  sum+=cc(b[i])
  
for i in range(n):
  k=b[a[i]-min(a)]
  if k<=1:
    print(sum)
  else:
    print(sum-cc(k)+cc(k-1))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np
N = list(map(int, (stdin.readline().strip().split())))[0]
M = list(map(int, (stdin.readline().strip().split())))
M=np.array(M)
c_list=[]
num_list=np.array([0]*(2*10**5+1))
ans=0
for i in M:
    num_list[i-1]+=1
ans=sum(num_list*(num_list-1))//2
for k in range(N):
    num=num_list[M[k]-1]-1
    print(ans-num)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np
N = list(map(int, (stdin.readline().strip().split())))[0]
M = list(map(int, (stdin.readline().strip().split())))
M=np.array(M)
c_list=[]
ans=0
for i in range(1,max(M)+1):
    num=np.count_nonzero(M == i)
    c_list.append(num)
    #num = M.count(i)
    ans+=(num*(num-1))//2
for k in range(N):
    num=c_list[M[k]-1]-1
    #num = M.count(M[k])-1
    print(ans-num)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np
N = list(map(int, (stdin.readline().strip().split())))[0]
M = list(map(int, (stdin.readline().strip().split())))
#M=np.array(M)
c_list=[]
ans=0
for i in range(1,max(M)+1):
    #num=np.count_nonzero(M == i)
    num = M.count(i)
    ans+=(num*(num-1))//2
for k in range(N):
    #num=(np.count_nonzero(M ==M[k]))-1
    num = M.count(M[k])-1
    print(ans-num)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np
N = list(map(int, (stdin.readline().strip().split())))[0]
M = list(map(int, (stdin.readline().strip().split())))
M=np.array(M)
c_list=[]
ans=0
for i in range(1,max(M)+1):
    num=np.count_nonzero(M == i)
    ans+=(num*(num-1))//2
for k in range(N):
    num=(np.count_nonzero(M ==M[k]))-1
    print(ans-num)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

check = set(list(a))
fac = []

cnt_list = [0] * max(a)


for i in check:
    cnt_list[int(i)-1] = a.count(int(i))

for i in cnt_list:
    if(i >= 2):
        fac.append(factorial(i)// (factorial(i-2) * factorial(2)))
    else:
        fac.append(0)
ans = []

for i in range(len(fac)):
    if(fac[i] <= 1):
        ans.append(sum(fac) - fac[i])
    else:
        ans.append(sum(fac) - fac[i] + factorial(cnt_list[i]-1) // (factorial(cnt_list[i]-3) * factorial(2)))

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
l=[0]*N
L=[0]*N

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

for i in range(N):
  L[i]=int(l[i]*(l[i]-1)/2)

for i in range(N):
  print(str(sum(L)-L[A[i]-1]+max(int((l[A[i]-1]-1)*(l[A[i]-1]-2)/2),0)))

Submission status: Time Limit Exceeded

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

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

for i in range(N):
  ans=0
  for j in range(N):
    if A[i]==j+1:
      ans+=int((l[j]-1)*(l[j]-2)/2)
    else:
      ans+=int(l[j]*(l[j]-1)/2)
  print(str(ans))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np
N = list(map(int, (stdin.readline().strip().split())))[0]
M = list(map(int, (stdin.readline().strip().split())))
M=np.array(M)
c_list=[]
for i in range(1,N+1):
    c_list.append(np.count_nonzero(M == i))
for k in range(N):
    c_list[M[k]-1]-=1
    l=np.array(list(filter((lambda x: x >=2),c_list)))
    ans=sum(l*(l-1))//2
    print(ans)
    c_list[M[k] - 1] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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


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

c = Counter(A_i)
cnt_dict = {}

for a_i in A_i:
    if a_i in cnt_dict:
        print(cnt_dict[a_i])
        continue
        
    tmp = c.copy()
    tmp[a_i] = tmp[a_i] - 1
    total = sum(list(map(Get_nC2, tmp.values())))
    cnt_dict[a_i] = total
    print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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


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

c = Counter(A_i)

for a_i in A_i:
    tmp = c.copy()
    tmp[a_i] = tmp[a_i] - 1
    total = sum(list(map(Get_nC2, tmp.values())))
    print(total)

Submission status: Time Limit Exceeded

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

count =[0 for i in range(N)]
for i in range(N):
    count[A_n[i]-1] +=1
ans =[count[i]*(count[i]-1)//2 for i in range(N)]

for i in range(N):
    print(sum(ans)-count[A_n[i]-1]+1)

Submission status: Time Limit Exceeded

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

count =[0 for i in range(N)]
for i in range(N):
    count[A_n[i]-1] +=1
ans =[count[i]*(count[i]-1)//2 for i in range(N)]

for i in range(N):
    old = count[A_n[i]-1]*(count[A_n[i]-1]-1)//2
    count[A_n[i]-1] -= 1
    new = count[A_n[i]-1]*(count[A_n[i]-1]-1)//2
    print(sum(ans)-old+new)
    count[A_n[i]-1] += 1

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split(' ')))
c = collections.Counter(a)
dict_1 = {}
dict_2 = {}
for key, value in c.items():
    dict_1[key] = value * (value-1) // 2
    if value - 1 == 0:
        dict_2[key] = 0
    else:
        dict_2[key] = (value-1) * (value-2) // 2
    
for k in range(n):
    ans = 0
    for key, val in dict_1.items():
        if key == a[k]:
            ans += dict_2[key]
        else:
            ans += dict_1[key]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def Get_nC2(n):
    return int(n*(n-1) / 2);


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

all_cnt = 0
num_cnt_dict = {}
comb_cnt_dict = {}
for i in range(min(A_i), max(A_i)+1):
    if i in A_i and A_i.count(i) > 1:
        num_cnt_dict[i] = A_i.count(i)
        all_cnt += Get_nC2(num_cnt_dict[i])

for a_i in A_i:
    if a_i in comb_cnt_dict:
        print(comb_cnt_dict[a_i])
    elif a_i in num_cnt_dict:
        comb_cnt_dict[a_i] = all_cnt - Get_nC2(num_cnt_dict[a_i]) + Get_nC2(num_cnt_dict[a_i]-1)
        print(comb_cnt_dict[a_i])
    else:
        print(0)

Submission status: Time Limit Exceeded

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

ans =0
for i in range(N-1):
    for j in range(i +1,N):
        if A_n[i] == A_n[j]:
            ans += 1
            
for i in range(N):
    minus = 0
    for j in range(N):
        if A_n[i] == A_n[j] and i != j:
            minus +=1
    print(ans-minus)

Submission status: Time Limit Exceeded

Solution code:
def Get_nC2(n):
    return int(n*(n-1) / 2);


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

all_cnt = 0
num_cnt_dict = {}
comb_cnt_dict = {}
for i in range(1, N+1):
    if i in A_i and A_i.count(i) > 1:
        num_cnt_dict[i] = A_i.count(i)
        all_cnt += Get_nC2(num_cnt_dict[i])

for a_i in A_i:
    if a_i in comb_cnt_dict:
        print(comb_cnt_dict[a_i])
    elif a_i in num_cnt_dict:
        comb_cnt_dict[a_i] = all_cnt - Get_nC2(num_cnt_dict[a_i]) + Get_nC2(num_cnt_dict[a_i]-1)
        print(comb_cnt_dict[a_i])
    else:
        print(0)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
c=lambda n:comb(n,2,exact=True)
n=int(input())
a=list(map(int,input().split()))
b=[a.count(i+1)for i in range(n)]
s=0
for i in b:
    s+=c(i)
for k in range(n):
    print(s-b[a[k]-1]+1)

Submission status: Time Limit Exceeded

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

d = {a: A.count(a) for a in set(A)}

for a in A:
    d[a] -= 1
    print(int(sum([value * (value - 1) / 2 for value in d.values()])))
    d[a] += 1

Submission status: Time Limit Exceeded

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

d = {a: A.count(a) for a in set(A)}

for a in A:
    d = d.copy()
    d[a] -= 1
    print(int(sum([value * (value - 1) / 2 for value in d.values()])))
    d[a] += 1

Submission status: Time Limit Exceeded

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

d = {a: A.count(a)  for a in set(A)}

for a in A:
    c = d.copy()
    c[a] -= 1
    print(int(sum([value * (value - 1) / 2 for value in c.values()])))

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
def combinations_count(n, r):
  if n == 0 or n == 1:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
C = [0]*n
for i in range(n):
  C[a[i]-1] += 1
for i in range(n):
  ans = sum(combinations_count(a, 2) for a in C)
for i in range(n):
  print(ans -max(0,C[a[i]-1]-1))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
a0=[ai for ai in a]
b=[-1]*(n+1)
base=0
for i in range(n):
    j=n-1-i
    if b[a[j]]==-1:
        c=a.count(a[j])
        b[a[j]]=c
        base+=c*(c-1)//2
        a.pop()
for i in range(n):
    d=b[a0[i]]
    print(base-d+1)

Submission status: Time Limit Exceeded

Solution code:

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


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

for a_i in A_i:
    cnt = 0
    for j in range(min(A_i), max(A_i)+1):
        j_cnt = A_i.count(j)
        if a_i == j:
            j_cnt -= 1

        cnt += Get_nC2(j_cnt)

    print(cnt)

Submission status: Time Limit Exceeded

Solution code:

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


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

for a_i in A_i:
    cnt = 0
    for j in range(1, N+1):
        j_cnt = A_i.count(j)
        if a_i == j:
            j_cnt -= 1

        cnt += Get_nC2(j_cnt)

    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[-1]*(n+1)
base=0
for i in range(n):
    if b[a[i]]==-1:
        c=a.count(a[i])
        b[a[i]]=c
        base+=c*(c-1)//2
for i in range(n):
    d=b[a[i]]
    print(base-d+1)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
A = list(map(int, input().split()))
B = collections.Counter(A)
D = collections.defaultdict(int)

for k, i in B.items():
    for l, j in B.items():
        if k == l:
            x = (j-1)*(j-2) // 2
            D[k] += x if x > 0 else 0
        else:
            x = j*(j-1) // 2
            D[k] += x if x > 0 else 0

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

Submission status: Time Limit Exceeded

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

for a in A:
    C = b.copy()
    x = 0
    # a == A[k]
    C[a] -= 1
    for d in C.values():
        x += d*(d-1)//2
    print(x)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=[0]*len(a)
all=0
for i in range(len(a)-1):
  for j in range(i+1,len(a)):
    if a[i]==a[j] and i!=j:
      c[i]+=1
      c[j]+=1
      all+=1
for i in range(len(a)):
  print(int(all-c[i]))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[[0]*len(a) for i in range(len(a))]
c=[]
all=0
for i in range(len(a)):
  c.append(0)
for i in range(len(a)):
  for j in range(len(a)):
    if a[i]==a[j] and i!=j:
      b[i][j]=1
      c[i]+=1/2
      c[j]+=1/2
      all+=0.5
for i in range(len(a)):
  print(int(all-c[i]))

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import collections

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

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

counter = collections.Counter(A)
dic = {c: comb(counter[c], 2) for c in counter.keys()}

for i in range(N):
    i_comb = comb(counter[A[i]]-1, 2)
    new_dict = copy.deepcopy(dic)
    new_dict[A[i]] = i_comb
    print(sum(new_dict.values()))

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

for i in range(N):
    B = A[:i] + A[i+1:]
    ans = 0
    for c in set(B):
        find = len(list(filter(lambda x: x == c, B)))
        # print("c:{}, find:{}".format(c, find))
        if find <= 1:
            ans += 0
        else:
            ans += comb(find, 2)
    print(ans)

Submission status: Time Limit Exceeded

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

setA = set(A)
an = 0
for num in setA:
    d = A.count(num)
    an += d*(d-1)//2

for i in range(n):
    ans = an
    t = A[i]
    e = A.count(A[i])
    ans -= e*(e-1)//2
    ans += (e-1)*(e-2)//2
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    AA = A.copy()
    AA.remove(AA[i])
    B = set(AA)
    cnt = 0
    for num in B:
        d = AA.count(num)
        cnt += d*(d-1)//2
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

V = collections.Counter(A)

W=0
for i in V.values():
    W += i * (i - 1) // 2

for i in range (0,N):
    stock=A[0]

    del A[0]
    
    count=0
    for j in range (0,len(A)):
        
        if A[j]==stock:
            count+=1
            
    print(W-count)
        
    A.append(stock)

Submission status: Time Limit Exceeded

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

V=[]
for v in itertools.permutations(A, 2):
    #print(v)
    if v[0]==v[1]:
        V.append(v)
#print(V,len(V))

for i in range (0,N):
    stock=A[0]
    #print(stock)
    del A[0]
    
    count=0
    for j in range (0,len(A)):
        
        if A[j]==stock:
            count+=1
            
    print(int(len(V)/2)-count)
        
    A.append(stock)

Submission status: Time Limit Exceeded

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

for i in range (0,N):
    stock=A[0]
    del A[0]
    
    W=[]
    for w in itertools.permutations(A, 2):
        
        if w[0]==w[1]:
            W.append(w)
            
    print(int(len(W)/2))
        
    A.append(stock)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [0]*(n+1)
c = [0]*(n+1)
s = [0]*(n+1)
for j in a:
    s[j] += 1
for i in a:    
    b[i] = s[i]*(s[i]-1)//2
    c[i] = (s[i]-1)*(s[i]-2)//2

for k in range(n):
    if b[a[k]] < 1:
        print(sum(b))
    elif b[a[k]]==1:
        print(sum(b)-1)
    else:
        print(sum(b)-b[a[k]]+c[a[k]])

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
b = [0]*(n+1)
c = [0]*(n+1)
x = collections.Counter(a)
for i in a:
    s = x[i]
    b[i] = s*(s-1)//2
    c[i] = (s-1)*(s-2)//2

for k in range(n):
    if b[a[k]] < 1:
        print(sum(b))
    elif b[a[k]]==1:
        print(sum(b)-1)
    else:
        print(sum(b)-b[a[k]]+c[a[k]])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [0]*(n+1)
c = [0]*(n+1)
for i in a:
    s = a.count(i)
    b[i] = s*(s-1)//2
    c[i] = (s-1)*(s-2)//2

for k in range(n):
    if b[a[k]] < 1:
        print(sum(b))
    elif b[a[k]]==1:
        print(sum(b)-1)
    else:
        print(sum(b)-b[a[k]]+c[a[k]])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(1,1+n):
  c=a.count(i)
  ans+=(c*(c-1))//2
for k in range(n):
  c=a.count(a[k])
  print(ans-c+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
an = list(map(int, input().split()))
n_list = [0]*n

for i in range(n):
  n_list[an[i]-1] += 1

for j in range(n):
  n_list1 = n_list[:]
  n_list1[an[j]-1] -= 1
  print(int(sum([x*(x-1)/2 for x in n_list1])))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
an = list(map(int, input().split()))
n_list = [0]*n

for i in range(n):
  n_list[an[i]-1] += 1

for j in range(n):
  n_list1 = n_list[:]
  n_list1[an[j]-1] -= 1
  print(int(sum(list(map(lambda x: x*(x-1)/2, n_list1)))))

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    UNI = set(A)
    #CNT = [[i, A.count(i), int(A.count(i)*(A.count(i)-1)/2), int((A.count(i)-1)*(A.count(i)-2)/2)] for i in UNI]
    CNT = [[i, A.count(i)] for i in UNI]
    for j in A:
        sum = 0
        for i,cnt in CNT:
            if j == i: cnt -= 1
            sum += int(cnt*(cnt - 1) /2)
        print(sum)

main()

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.keys():
        d[A[i]] += 1
    else:
        d[A[i]] = 1
 
sum = 0
 
for i in A:
    d[i] -= 1
    sum = 0
    for key in d.keys():
        sum += d[key]*(d[key] - 1) // 2
    print(sum)
    d[i] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


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

for k in range(N):
    c_a = count_A.copy()
    c_a[A[k]] -= 1
    ans = 0
    for v in c_a.values():
        ans += v*(v-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import numpy as np


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

for k in range(N):
    c_a = count_A.copy()
    c_a[A[k]] -= 1
    a = np.array(list(c_a.values())).reshape(-1,)
    b = a-1
    print(np.dot(b, a) // 2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
l = []
ans = 0
for i in range(N):
    l.append(A.count(i+1))
for j in l:
    ans += int(j*(j-1)/2)
for k in range(N):
    print(ans - l[A[k]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
ans = [0]*n
for k in range(n):
    b = a[:]
    del b[k]
    #print(b)
    """もっといいやりかたある
    for l in range(n-2):
        for m in range(l+1, n-1):
            if b[l] == b[m]:
                count[k] += 1
    """
    c = Counter(b)  # ｂの要素の数と値の辞書を出してくれる
    #print(c)
    #print(c.keys())
    #print(c.values())
    for m in c.values():  # Counterで出した辞書の値（values)のリストの中身でfor
        ans[k] += int((m*(m-1))/2)  # mC2をしている

for i in range(n):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = [0]*n
for k in range(n):
    b = a[:]
    del b[k]
    #print(b)
    for l in range(n-2):
        for m in range(l+1, n-1):
            if b[l] == b[m]:
                count[k] += 1

    print(count[k])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
def combi(n):
    return n*(n-1)//2
 
count_all = 0
count_cut = 0
 
 
for i in range(n):
    count = a.count(i+1)
    count_all += combi(count)
    
for k in range(n):
    #aのk番目の要素と同じ要素の数を(k番目の要素も含めて)出し、1を引く
    count = a.count(a[k]) - 1
    ans = count_all - count
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

def combi(n):
    return n*(n-1)//2

"""
for i in range(n):
    count = a.count(i+1)
    count_all += combi(count)
"""
count_all = sum([combi(a.count(i+1)) for i in range(n)])
"""
for k in range(n):
    #aのk番目の要素と同じ要素の数を(k番目の要素も含めて)出し、1を引く
    count = a.count(a[k]) - 1
    ans = count_all - count
    print(ans)
"""
count = [a.count(a[k]) - 1 for k in range(n)]
x = np.array([count_all]*n)
y = np.array(count)
ans = x-y
for s in ans.tolist():
    print(s)

Submission status: Time Limit Exceeded

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

def combi(n):
    return n*(n-1)//2

count_all = 0
count_cut = 0


for i in range(n):
    count = a.count(i+1)
    count_all += combi(count)
    
for k in range(n):
    #aのk番目の要素と同じ要素の数を(k番目の要素も含めて)出し、1を引く
    count = a.count(a[k]) - 1
    ans = count_all - count
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n=int(input())
a=deque(list(map(int,input().split())))
lst=[0]*n
for i in a:
  lst[i-1]+=1
lst_=list(map(lambda x:x*(x-1)//2,lst))
for i in range(n):
  ans=sum(lst_)
  p=a.popleft()
  if lst_[p-1]==0:
    print(ans)
  else:
    print(ans-(lst_[p-1]-(lst[p-1]-1)*(lst[p-1]-2)//2))
  a.append(p)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
line = input()
balls = line.split()
for i in range(N):
    count = 0
    temp = line.split()
    temp.pop(i);
    unique = set(temp)
    for ball in unique:
        c = temp.count(ball)
        count+=(c-1)*(c)/2
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
line = input()
balls = line.split()
for i in range(N):
    count = 0
    temp = line.split()
    temp.pop(i);
    seen = set()
    for ball in temp:
        if ball not in seen:
            seen.add(ball)
            c = temp.count(ball)
            count+=(c-1)*(c)/2
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = [int(n) for n in input().split()]

vals = {}
for A in As:
    if A in vals:
        vals[A] += 1
    else:
        vals[A] = 1

def waystochoose(k):
    total = 0
    for val in vals:
        if val == k:
            total += (vals[val] - 1)*(vals[val] - 2) // 2
        else:
            total += vals[val]*(vals[val]-1) // 2
    return total

checked = {}
for k in range(len(As)):
    if not As[k] in checked:
        ways = waystochoose(As[k])
        print(ways)
        checked[As[k]] = ways
    else:
        print(checked[As[k]])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = [int(n) for n in input().split()]

vals = {}
for A in As:
    if A in vals:
        vals[A] += 1
    else:
        vals[A] = 1

for k in range(len(As)):
    Ak = As[k]
    total = 0
    for val in vals:
        if val == Ak:
            total += (vals[val] - 1)*(vals[val] - 2) // 2
        else:
            total += vals[val]*(vals[val]-1) // 2
    print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
for k in range(len(A)):
  ans = 0
  search = A.copy()
  search.pop(k)
  unique = set(search)
  for u in unique:
    c = search.count(u)
    ans += c * (c-1) / 2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb
N = int(input())
A = input().split()
C = Counter(A)
def banned_count(seq):
    for i in range(N):
        C[A[i]] -= 1
        print(sum([comb(j, 2, exact=True) for j in C.values()]))
        C[A[i]] += 1
banned_count(A)

Submission status: Time Limit Exceeded

Solution code:
import math
from collections import Counter

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

def comb2(n):
    return n * (n - 1) // 2

c = Counter(A)
ans = 0
for m in c.values():
    if m >= 2:
        ans +=comb2(m)

for a in A:
    print(ans - A.count(a)+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = list(map(int,input().split()))
    for k in range(n):
        a_ = a[0:k]+a[k+1:]
        c = Counter(a_)
        s = 0
        for k in c.keys():
            cnt = c[k]
            if cnt>1:
                s+=cnt*(cnt-1)//2
        print(s)
            
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    cnt = dict()
    ans = dict()
    for i in a:
        cnt[i] = cnt.get(i, 0) + 1

    for (idx, v) in cnt.items():
        ans[idx] = v * (v - 1) // 2

    for k in range(len(a)):
        res = sum(ans.values()) - (cnt[a[k]] - 1)
        print(res)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    cnt = dict()
    for i in a:
        cnt[i] = cnt.get(i, 0) + 1

    for k in range(len(a)):
        cnt2 = cnt.copy()
        cnt2[a[k]] -= 1

        sum = 0
        for c in cnt2.values():
            sum += c * (c - 1) // 2

        print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    a_try = [] + a
    count = 0
    del a_try[k]
    while a_try != []:
        number = a_try[0]
        c = a_try.count(number)
        count += ((c - 1) * c // 2)
        a_try = [i for i in a_try if i != number]
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
lin = input().split()
# 要素の個数を求める
dic_s = {}
for c in lin:
  dic_s.setdefault(c, 0)
  dic_s[c] += 1
# 1つしかない要素は計算対象外
dic_cp = dic_s.copy()
for key, value in dic_cp.items():
  if value == 1:
    del dic_s[key]
dic_tot = {}
for c in lin:
  # 1度求めたら再計算しない
  # 同じ数字が多いと時間短縮
  if dic_tot.get(c):
    print(int(dic_tot[c]))
  else:
    # 指定数値を除いた2つを選ぶ
    tot = 0
    for key,value in dic_s.items():
      v = value
      if key == c:
        v -= 1
      tot += v * (v - 1) / 2
    dic_tot.setdefault(c,tot)
    print(int(tot))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
for i in range(n):
  x=0
  for j in set(a):
    c=a[0:i].count(j)+a[i+1:n].count(j)
    x+=int(c*(c-1)/2)
  print(x)

Submission status: Time Limit Exceeded

Solution code:
N = int(input(""))
A = input("").split()
x_all = 0

ak = 0

for i in range(N-1):
    for j in range(N):
        if A[i] == A[i+j+1]:
            x_all += 1
        if i+j+1 == N-1:
            break

for k in range(N): 
    x = 0
    for m in range(N):
        if A[k] == A[m]:
            x += 1
    ak = x - 1

    if x_all-ak > 0:
        print(x_all-ak)
    elif x_all-ak <= 0:
        print(0)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n = int(input())
a = list(map(int, input().split()))
num = 0
for i in range(n):
    b = a.count(i)
    num += int(b*(b-1)/2)
for k in range(1,n+1):
    print(num - a.count(a[k-1]) + 1)

Submission status: Time Limit Exceeded

Solution code:
def comb(val):
    if(val > 0):
        return int(val*(val-1) / 2)
    else:
        return 0
    
def make_map(balls):
    balls_map = {}
    for ball in balls:
        if(ball in balls_map):
            balls_map[ball] += 1
        else:
            balls_map[ball] = 1
    return balls_map

def main():
    args = input().rstrip().split(' ')
    N = int(args[0])
    balls = [val for val in input().rstrip().split(' ')]
    balls_map = make_map(balls)
    
    sum_ = 0
    for ball in balls:
        balls_map[ball] -= 1
        sum_ = 0
        for val in balls_map.values():
            sum_ += comb(val)
        print(sum_)
        balls_map[ball] += 1
    
if __name__ == "__main__":
    main()