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 collections
import math

n = int(input())

m = list(map(str,input().split()))

c = dict(collections.Counter(m))
d = {}
hyouji = {}
def ans(a):
    if a<2:
        return 0
    else:
        return math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))

cnt = 0
for i,j in c.items():
    n = ans(j)
    d[i] = n
    cnt += n


for k in m:
    if k in hyouji:
        print(hyouji[k])
    else: 
        if c[k] < 2:
            q = cnt
        elif c[k] == 2:
            q = cnt - 1
        else:
                q = cnt -d[k] + ans(c[k] - 1)

        hyouji[k] = q
        print(hyouji[k])

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int , input().split()))
a = collections.Counter(A)
S = len(a)
k = 0
for j in range (S):
    b = a.most_common()[j][1]
    if b > 1:
        k += b * (b-1)/2
    

for i in range(N):
    s = A[i]
    p = a[s]
    print(int(k - p + 1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = tuple(map(int, input().split()))
for k in range(N):
  c = 0
  A = list(a)
  del A[k]
  while len(A) > 0:
    y = A.count(A[0])
    c += y*(y-1)//2
    x = A[0]
    while x in A:
        A.remove(x)
  print(c)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


n = int(input())
l = [int(x) for x in input().split()]

ll = [x for x in set(l) if l.count(x) > 1]

sm = 0
m = [1 for x in range(n)]
for i in ll:
    t = l.count(i)
    sm += combinations_count(t, 2)
    m[i-1] = t

for i in l:
    print(sm-(m[i-1]-1))

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools
import math
def perm_cnt(n, r):
    return math.factorial(n) // math.factorial(n - r)

n = int(input())
a = list(map(int,input().split()))
jisyo = collections.Counter(a)
jisyo_value = list(jisyo.values())

array = []
for i in range(len(jisyo_value)):
    if jisyo_value[i] >= 2:
        array.append(int(perm_cnt(jisyo_value[i],2)/2))

for i in range(len(a)):
    goukei = sum(array)
    #print(jisyo[a[i]])
    if jisyo[a[i]] > 2:
        print(goukei - int(perm_cnt(jisyo[a[i]],2)/2) + int(perm_cnt(jisyo[a[i]]-1,2)/2))    
    elif jisyo[a[i]] == 2:
        print(goukei  - 1)
    elif jisyo[a[i]] <= 1:
        print(goukei)
        #print(array)
#print(jisyo[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}

for i in a:
    c = ao.count(i)
    ac += (c**2 - c)/2
    acount[i] = 1-c
ac = int(ac)

for key in acount:
    acount[key] += ac
for i in ao:
    print(acount[i])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count_sum = np.sum([A.count(i)*(A.count(i)-1)/2 for i in A_set])
    
for i in A:
    print(int(count_sum-(A.count(i)-1)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy


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

arr = itertools.groupby(a)

combinations_a = {}
combinations_b = {}
for (i, m) in arr:
    l = list(m)
    c = itertools.combinations(l, 2)
    combinations_a[i] = len(list(c))
    l.pop()
    c = itertools.combinations(l, 2)
    combinations_b[i] = len(list(c))

ans_buf = 0
for i in range(0, n):
    remove = aa[i]
    for key in combinations_a:
        if key == remove: continue
        ans_buf += combinations_a[key]

    ans_buf += combinations_b[remove]
    print(ans_buf)
    ans_buf = 0

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
a=[]
A=list(map(int,input().split()))
c=collections.Counter(A)
d=c.most_common()
ans=0
for i in range(len(d)):
    ans+=d[i][1]*(d[i][1]-1)//2
for i in range(N):
    for j in range(len(d)):
        if A[i]==d[j][0]:
            Ans=ans-(d[j][1]-1)
            break
    print(Ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect
import heapq


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

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


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#V, E, r = map(int, input().split())
#INF = V * 10001
#A = [int(i) for i in input().split()]


N = int(input())
A = [int(i) for i in input().split()]
dp = [0]*(N+1)
ans = 0
cnt = 0
tle = 0

As = list(set(A))

for i in As:
  cnt = A.count(i)
  if(cnt<2):
    dp[i] = 0
  else:
    dp[i] = combinations_count(cnt, 2)

tle = sum(dp)

#print(dp)
for i in range(N):
  ans = tle - (A.count(A[i])-1)
  if(ans < 0):
    ans = 0
  print(int(ans))

#print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}

for i in a:
    c = ao.count(i)
    ac += (c**2 - c)/2
    acount[i] = c-1
ac = int(ac)
for i in ao:
    print(ac-acount[i])

Submission status: Time Limit Exceeded

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

m=len(a)
c=list(set(a))
sum1=0

for i in range(len(c)):
    k=a.count(c[i])
    sum1+=k*(k-1)//2



for j in range(m):
    g=a.count(a[j])
    t=sum1-(g-1)
    print(t)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
T = []

for i in range(N):
    flag = 0
    for j in range(len(T)):
        if A[i] == T[j][0]:
            T[j][1] += 1
            flag = 1
            break
    if flag == 0:
        T.append([A[i],1])

Ans = 0
for i in range(len(T)):
    Ans += (T[i][1]-1)*T[i][1]//2

T3 = []

for i in range(len(T)):
    T3.append(Ans-(T[i][1]-1))

for i in range(N):
    for j in range(len(T3)):
        if T[j][0] == A[i]:
            print(T3[j])
            break

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
C = {}

def func01(S,key,C):
    #すでに計算済みの数字ならば計算結果を参照
    #計算未ならば 計算して 計算結果を追加
    if key in C.keys():
        sum_count = C.get(key)

    else:
        sum_count = 0
        B = set(S)

        if len(B) == N:
            sum_count = 0
        else:
            for s in B:
                count_s = S.count(s)
                sum_count += (count_s * (count_s - 1) // 2)
            C.update({key: sum_count})
    print(sum_count)


for i in range(N):
    X = A[0:i] + A[i + 1:N]
    func01(X, A[i], C)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int , input().split()))
a = collections.Counter(A)
S = len(list(set(A)))
k = 0
for j in range (S):
    b = a.most_common()[j][1]
    k = b * (b-1)/2 + k

for i in range(N):
    s = A[i]
    p = a[s]
    print(int(k - p +1))

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


n = int(input())
l = [int(x) for x in input().split()]

cl = [0 for i in range(n)]
ll = [x for x in set(l) if l.count(x) > 1]

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc

for i in l:
    print(sum-(l.count(i)-1))

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect
import heapq


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

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


#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#V, E, r = map(int, input().split())
#INF = V * 10001
#A = [int(i) for i in input().split()]


N = int(input())
A = [int(i) for i in input().split()]
dp = [0]*(N+1)
ans = 0
cnt = 0
minus = 0

As = list(set(A))

for i in As:
  cnt = A.count(i)
  if(cnt<2):
    dp[i] = 0
  else:
    dp[i] = combinations_count(cnt, 2)

ans = sum(dp)

#print(dp)
for i in range(N):
  temp = dp[A[i]]
  if(dp[A[i]] == 1):
    minus = temp-1
  elif(dp[A[i]] == 0):
    minus = temp
  else:
    minus = combinations_count(A.count(A[i])-1, 2)
  print(int(ans-(temp-minus)))

#print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import mul
from functools import reduce


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


n = int(input())
l = [int(x) for x in input().split()]

cl = [0 for i in range(n)]
ll = [x for x in set(l) if l.count(x) > 1]

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc

for i in l:
    print(sum-(l.count(i)-1))

Submission status: Time Limit Exceeded

Solution code:
import math

N=int(input())
A = list(map(int,input().split()))
cnt=[0]*(N+1)
for i in range(1,N+1):
	cnt[i-1]=(A.count(i))
ans=0
for i in range(1,N+1):
	if cnt[i-1]==0:
		ans+=0
	else:
		ans+=cnt[i-1]*(cnt[i-1]-1)//2

for i in range(0,N):
	point=A[i]
	deff=(cnt[point-1]*(cnt[point-1]-1))//2-((cnt[point-1]-1)*(cnt[point-1]-2))//2
	print(ans-deff)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A
a=[]
while len(A)>0:
    b=A[0]
    n=A.count(b)
    if n>1:
        a.append(n)
    A=[x for x in A if x!=b]
c=0
for i in range(len(a)):
    c+=int(a[i]*(a[i]-1)/2)
for i in range(len(B)):
    print(c-B.count(B[i])-1+2)

Submission status: Time Limit Exceeded

Solution code:
import collections
a=int(input())
ai = list(map(int, input().split()))
c = collections.Counter(ai)
val=0
for i in c.values():
    val+=i*(i-1)//2
for i in ai:
    b=ai.count(i)
    print(val-b+1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

a_count_list = []

a_dic = {}

for i in range(len(a_list)):
    key = a_list[i]
    if key in a_dic:
        a_dic[key] += 1
    else:
        a_dic[key] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


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


    print(sum_select)

Submission status: Time Limit Exceeded

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

count_dic = {}
for i in A_set:
    C = A.count(i)
    count_dic[i] = C*(C-1)/2
    
sum_dic = sum(count_dic.values())
    
for i in A:
    print(int(sum_dic-(A.count(i)-1)))

Submission status: Time Limit Exceeded

Solution code:
import math

N=int(input())
A = list(map(int,input().split()))
cnt=[]
B=[]
for i in range(1,N+1):
	cnt.append(A.count(i))
ans=0
for i in range(1,N+1):
	if cnt[i-1]==0:
		ans+=0
	else:
		ans+=cnt[i-1]*(cnt[i-1]-1)//2

for i in range(0,N):
	point=A[i]
	deff=(cnt[point-1]*(cnt[point-1]-1))//2-((cnt[point-1]-1)*(cnt[point-1]-2))//2
	print(ans-deff)

Submission status: Time Limit Exceeded

Solution code:
import collections
a=int(input())
ai=[int(i) for i in input().split()]
c = collections.Counter(ai)
val=0
for i in c.values():
    val+=i*(i-1)//2
for i in ai:
    b=ai.count(i)
    print(val-b+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}
for i in a:
    c = ao.count(i)
    ac += int((c**2 - c)/2)
    acount[i] = c-1

for i in ao:
    print(ac-acount[i])

Submission status: Time Limit Exceeded

Solution code:
N_ = int(input())

A_ = list(map(int,input().split()))
#A_.remove(1)
#print(A_)
def xC(num):
    return (num * (num - 1)) //2

def Re_(reA,t):
    Dup = []
    num = 0
    #print(reA)
    for i in range(len(reA)):
        if Dup.count(reA[i]) == 0:
            Dup.append(reA[i])
        else:
            pass
    for i in range(len(Dup)):
        num += xC(reA.count(Dup[i]))
    return num

for i in range(N_):
    B_ = A_.copy()
    B_.remove(A_[i])
    res = Re_(B_,A_[i])
    print(res)

Submission status: Time Limit Exceeded

Solution code:
# D - Banned K(高速版)
N = int(input())
A = list(map(int, input().split()))

import copy as c
C = []

for i in range(N):
    C.append(A.count(i+1))

C_tmp = [s for s in C if s > 1]

sum0 = 0
for j in C_tmp:
    sum0 = sum0 + j*(j-1)/2

for a in A:
    if C[a-1] >= 2:
        d = C[a-1] - 1
    else:
        d =0
    sum = int(sum0 - d)
    print(sum)

Submission status: Time Limit Exceeded

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

def main(N, lst_A):

    sum_comb = 0
    lst_cntA = []

    for i in range(1, N+1):
        cntA  = lst_A.count(i)
        combA = (cntA * (cntA-1)) // 2
        
        sum_comb += combA
        lst_cntA.append(cntA)

    for A in lst_A:
        print(sum_comb - (lst_cntA[A-1]-1))

    return 0

if __name__ == "__main__":

    main(N, lst_A)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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

import collections
c = collections.Counter(a)

# print(n)
# print(a)

l = [0] * (n + 1)
ll  = [0] * (n + 1)
# for i in range(n):
#     l[a[i]] += 1
#     ll[a[i]] = int((l[a[i]] * (l[a[i]] - 1)) / 2)
    
for key, value in c.items():
    l[key] = value
    ll[key] = int((value * (value - 1)) / 2)
    
# print(l)
# print(ll)
    
for i in range(n):
    v = l[a[i]]
    vv = ll[a[i]]
    vvv = int(((v - 1) * (v - 2)) / 2)
    # print(vvv)
    print(sum(ll) - (vv - vvv))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
cnt = 0
ansx = []
ansy = []

sumall = 0
for x, y in c.items():
    sumall += (y*(y-1)//2)
    ansy.append(y-1)
    ansx.append(x)

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

Submission status: Time Limit Exceeded

Solution code:
#import collections

N = int(input())
A = list(map(int, input().split()))
#A = [1, 2, 1, 4, 2, 1, 4, 1]

count_dic = {}
for i in A:
    C = A.count(i)
    count_dic[i] = C*(C-1)/2

for i in A:
    print(int(sum(count_dic.values())-(A.count(i)-1)))

Submission status: Time Limit Exceeded

Solution code:
import math

N=int(input())
A = list(map(int,input().split()))
cnt=[]
B=[]
for i in range(1,N+1):
	cnt.append(A.count(i))
	
for i in range(1,N+1):
	B.append((cnt[i-1]*(cnt[i-1]-1))//2)
	if cnt[i-1]==0:
		B[i-1]=0
ans=0
for i in range(1,N+1):
	ans+=B[i-1]

for i in range(0,N):
	point=A[i]
	deff=(cnt[point-1]*(cnt[point-1]-1))//2-((cnt[point-1]-1)*(cnt[point-1]-2))//2
	print(ans-deff)

Submission status: Time Limit Exceeded

Solution code:
# D - Banned K(高速版)
N = int(input())
A = list(map(int, input().split()))

import copy as c
C = []

for i in range(N):
    C.append(A.count(i+1))

C_tmp = [s for s in C if s > 1]

sum0 = 0
for j in C_tmp:
    sum0 = sum0 + j*(j-1)/2

for k in range(N):
    if C[A[k]-1] >= 2:
        d = C[A[k]-1] - 1
    else:
        d =0
    sum = int(sum0 - d)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
c = b.most_common()
values, counts = zip(*b.most_common())
d = (counts)
ans = 0

for j in range(len(d)):
    f = d[j]
    if f > 1:
        ans = ans + (f*(f - 1)/2)

for i in range(n):
    g = a[i]
    gc = a.count(g)
    tem = ans - gc + 1
    print(int(tem))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

a_count_list = []

a_dic = {}

for i in range(len(a_list)):
    key = a_list[i]
    if key in a_dic:
        a_dic[key] += 1
    else:
        a_dic[key] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    sum_select += a_solution_dic_at_k[ak]
    for key in a_solution_dic:
        if key != ak:
            sum_select += a_solution_dic[key]


    print(sum_select)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
ai=[int(i) for i in input().split()]

c=[ai.count(x) for x in set(ai) if ai.count(x)>1]
val=sum([x*(x-1)//2 for x in c])
for i in ai:
    b=ai.count(i)
    print(val-b+1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(map(int,input().split()))
set_S=list(set(S))
ans_stock={}
number={}
for i in set_S:
  number[i] = S.count(i)
comb = 0
for i in set_S:
  comb += number[i]*(number[i]-1)/2

for i in S:
  if i in list(ans_stock.keys()):
    print(ans_stock[i])
  else:
    ans = int(comb -  number[i]*(number[i]-1)/2 +  (number[i]-1)*(number[i]-2)/2)
    print(ans)
    ans_stock[i]=ans

Submission status: Time Limit Exceeded

Solution code:
import collections

#Input
n = int(input())
l_a = list(map(int, input().split()))

#Process
d_c = collections.Counter(l_a)
for i in range(n):
    d_o = d_c.copy()
    d_o[l_a[i]] = d_o[l_a[i]] - 1
    l_o = list(d_o.values())
    Ans = 0
    for j in range(len(l_o)):
        if(l_o[j] != 1):
            Ans += int((l_o[j] - 1) * l_o[j] / 2)
    print(Ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
cnt = 0
ansx = []
ansy = []
ansy2 = []

for x, y in c.items():
    ansy.append((y*(y-1)//2))
    ansy2.append(y-1)
    ansx.append(x)
sumall = sum(ansy)

for i in A:
    print(sumall - ansy2[ansx.index(i)])

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

n = int(input())

m = list(map(str,input().split()))

c = dict(collections.Counter(m))
d = {}
d_self ={}
sets = {}
def ans(a):
    if a<2:
        return 0
    else:
        return math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))

for i in m:
    if i in sets:
        print(sets[i])
    else:
        cnt = 0
        for j,k in c.items():
            if j == i:
                if k - 1 >= 2:
                    d_self.setdefault(j, ans(k-1))
                    cnt += d_self[j]
                
            else:
                d.setdefault(j, ans(k))
                cnt += d[j]
        sets[i] = cnt
        print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math

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

C = list(set(A))
M = len(C)
t = []
anst = 0
jisyo = {}
for i in range(M):
  temp = A.count(C[i])
  jisyo[C[i]] = temp
  #t.append(A.count(C[i]))
  if temp >=2:
    anst += temp*(temp-1)/2

#jisyo = dict(zip(C, t))
#print(jisyo)   
for i in range(N):
  #print(t[C.index(A[i])])
  print(int(anst-jisyo[A[i]]+1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = [int(x) for x in input().split()]  

# ボールを全部使えるときの合計を求める
s = 0
for i in range(1,N+1):
    b = An.count(i)
    if b > 1:
        s += int(b*(b-1) / 2)

for k in range(N):
    print(s - (An.count(An[k])-1))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math


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


N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
cnt = 0
ansx = []
ansy = []
ansy2 = []


for x, y in c.items():
    ansy.append(combinations_count(y, 2))
    ansy2.append(y-1)
    ansx.append(x)
sumall = sum(ansy)

for i in A:
    print(sumall - ansy2[ansx.index(i)])

Submission status: Time Limit Exceeded

Solution code:
import collections
from collections import deque
import numpy as np

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

ca = collections.Counter(A)
cakeys = list(ca.keys())
cavalues = np.array(list(ca.values()))

#print(cakeys)
#print(np.array(list(ca.values())))

cavaluesmax = cavalues*(cavalues-1)//2
valuesum = np.sum(cavaluesmax)
#cavaluesmin = (cavalues-1)*(cavalues-2)//2
diff = cavalues-1

ans = valuesum - diff

#print(cavaluesmax)
#print(cavaluesmin)

for i in A:
    #print(cakeys[index])
    index = cakeys.index(i)
    #ans = cavaluesmin[index].sum() + cavaluesmax[:index].sum() + cavaluesmax[index+1:].sum()
    print(ans[index])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

a_count_list = []

a_dic = {}

for i in range(len(a_list)):
    key = a_list[i]
    if key in a_dic:
        a_dic[key] += 1
    else:
        a_dic[key] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    if key in a_solution_dic:
        continue
    else:
        a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
        a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    sum_select += a_solution_dic_at_k[ak]
    for key in a_solution_dic:
        if key != ak:
            sum_select += a_solution_dic[key]


    print(sum_select)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math


def combinations_count(n, r):
    return (n*(n-1)//2)
    # 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 = []
ansy2 = []


for x, y in c.items():
    ansy.append(combinations_count(y, 2))
    ansy2.append(y-1)
    ansx.append(x)
sumall = sum(ansy)

for i in A:
    print(sumall - ansy2[ansx.index(i)])

Submission status: Time Limit Exceeded

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

n = int(input())
al = list(map(int, input().split()))
al_cnt = [0] * (n + 1)

for i in range(1, n + 1):
    al_cnt[i] = al.count(i)
max_cnt = 0

for j in range(1, n + 1):
    if al_cnt[j] >= 2:
        max_cnt += al_cnt[j] * (al_cnt[j] - 1) / 2

for l in range(n):
    if al_cnt[al[l]] >= 3:
        ans = max_cnt - (al_cnt[al[l]] * (al_cnt[al[l]] - 1) / 2) + ((al_cnt[al[l]] - 1) * (al_cnt[al[l]] - 2) / 2)
        print(int(ans) if ans > 0 else 0)
    else:
        ans = max_cnt - (al_cnt[al[l]] * (al_cnt[al[l]] - 1) / 2)
        print(int(ans) if ans > 0 else 0)

Submission status: Time Limit Exceeded

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

import collections
answer_list = []

for i in range(N):
    poped_num = num_list[i]
    num_list.pop(i)
    c = collections.Counter(num_list)
    num_list.insert(i, poped_num)

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

    answer_list.append(comb)

for i in answer_list:
    print(int(i))

Submission status: Time Limit Exceeded

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

for k in range(N):
    cnt = 0
    B = A[k]
    del A[k]
    for l in range(N-1):
        for m in range(l+1, N-1):           
            if A[l] == A[m]:
                cnt += 1
            else:
                cnt += 0
    A.insert(k, B)
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
val = list(c.values())
key = list(c.keys())
cosum = 0
for i in val:
    cosum += (i*(i-1))//2
for i in range(n):
    ind = key.index(a[i])
    count = cosum
    count -= val[ind]-1
    print(count)

Submission status: Time Limit Exceeded

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

A_set = list(set(A))
Ad = {a_set:A.count(a_set) for a_set in A_set}

score_sum = sum([x*(x-1)/2 for x in list(Ad.values())])
output = [str(int(score_sum - (Ad[a] - 1)))for a in A]
print('\n'.join(output))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
num_sum = 0
num_dic = {}
for i in range(1, N+1):
    c = A.count(i)
    num_sum += int(c * (c - 1) / 2)
    num_dic[str(i)] = c

for j in A:
    if num_dic[str(j)] == 1:
        print(num_sum)
    else:
        print(num_sum - (num_dic[str(j)] - 1))

Submission status: Time Limit Exceeded

Solution code:
def list_counter(num_list,num):
    counter = 0
    for n in num_list:
        if n == num:
            counter +=1
    return counter

n = int(input())
a_list = [int(num) for num in input().split(" ")]
a_set = set(a_list)
total_comb = 0
for i in a_set:
    i_count = list_counter(a_list,i)
    total_comb += i_count*(i_count-1)
total_comb = int(total_comb/2)
for k in range(n):
    except_num = a_list[k]
    print(total_comb - (list_counter(a_list,except_num)-1))

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import mul
from functools import reduce


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


n = int(input())
l = [int(x) for x in input().split()]

cl = [0 for i in range(n)]
ll = [x for x in set(l) if l.count(x) > 1]

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    if m < 3:
        cl[i-1] = nc
    else:
        cl[i - 1] = nc - combinations_count(m-1, 2)

# print(sum)
# print(cl)
for i in l:
    print(sum - cl[i - 1])

Submission status: Time Limit Exceeded

Solution code:
# import math

N = int(input())
An = [int(x) for x in input().split()]  
Cn = [An.count(i) for i in range(1,N+1)]  # どの数字が何個あるか？

# ボールを全部使えるときの合計を求める
s = 0
for i in range(N):
    if Cn[i] > 1:
        s += int(Cn[i]*(Cn[i]-1) / 2)

for k in range(N):
    print(s - (Cn[An[k]-1]-1))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

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

for k in range(N):
    ai = copy.copy(A)
    ai.pop(k)
    
    ans = 0
    for i in range(N-1):
        for j in range(i, N-1):
            if ai[i] == ai[j]:
                ans += 1
        ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = []
for i in a:
    c = ao.count(i)
    ac += int((c**2 - c)/2)
    acount.append(c-1)

for i in ao:
    ind = a.index(i)
    print(ac-acount[ind])

Submission status: Time Limit Exceeded

Solution code:
import collections
import math


def combinations_count(n, r):
    return (n*(n-1)//2)
    # 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 = []
ansy2 = []


for x, y in c.items():
    ansy.append(combinations_count(y, 2))
    # ansy2.append(combinations_count(y - 1, 2))
    ansx.append(x)
sumall = sum(ansy)

for i in A:
    print(sumall - A.count(i) + 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
N = int(input())
A = list(map(int, sys.stdin.readline().strip().split()))
AK = []
for k in range(0, len(A)):
    AK.append(sorted([a for i,a in enumerate(A) if k != i]))
count = {}
for i,ak in enumerate(AK):
    n = AK.index(ak)
    if n != i:
        print(count[n])
        continue
    count[i] = 0
    for j in itertools.combinations(ak, 2):
        if j[0] == j[1]:
            count[i] += 1
    print(count[i])