Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

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

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

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

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

B=sorted(set(A))
sa = []
su = [0]*(N+1)
gokei = 0
for i in range(len(B)):
    x = A.count(B[i])
    if x == 1:
        gokei += 0
        sa.append(0)
    elif x ==2:
        gokei += 1
        sa.append(1)
    else:
        if su[x] == 0:
            z = math.factorial(x)//(math.factorial(x-2)*math.factorial(2))
        else:
            z = su[x]
        gokei += z
        sa.append(z-z*(x-2)//x)

ans = [0]*(N+1)
for i in range(N):
    z = A[i]
    if ans[z] == 0:
        e = gokei - sa[B.index(z)]
        ans.append(e)
        print(e)
    else:
        print(ans[z])

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
List = list(map(int, input().split()))

tem = copy.copy(List)

tem.sort()

kind = tem[0]
cnt = 1
kindlist = []
cntlist =[]
for i in range(len(tem)):
  if i == len(tem) -1:
    kindlist.append(kind)
    cntlist.append(cnt)
  elif kind == tem[i+1]:
    cnt +=1
  else :
    kindlist.append(kind)
    cntlist.append(cnt)
    kind = tem[i+1]
    cnt = 1

ans = {}
x = 0
keys = []
valuses = []
for i in range(len(kindlist)):
  for j in range(len(cntlist)):
    if i == j :
      if cntlist[j] - 1 > 1:
        x += ((cntlist[j] -1) * (cntlist[j] -2 ) )//2
    else:
      if cntlist[j] > 1 :
        x += (cntlist[j] * (cntlist[j] -1 ))//2

  valuses.append(x)
  x = 0


ans.update(zip(kindlist, valuses))

for _ in range(len(List)):
  print(ans[List[_]])

Submission status: Time Limit Exceeded

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

def checkPatternNum(inA):
    totalCount = 0
    countDict = {}
    while(len(inA) > 0):
        targetData = inA[0]
        if(not targetData in countDict):
            targetCount = inA.count(targetData)
            countDict[targetData] = targetCount
            if(2 <= targetCount):
                for n in range(targetCount+1):
                    if(n > 1):
                        totalCount += (n-1)
    return totalCount

for index in range(N):
    targetA = A.copy()
    del targetA[index]
    print(checkPatternNum(targetA))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
N =int(input())
A = list(map(int, input().split()))
a = collections.Counter(A)
cnt = 0
for k in range(N):
    rem = A[k]
    
    if rem in a.keys():
        a[rem] -= 1
    for  i in a.values():
        if i > 1:
            cnt += math.factorial(i)/(2*math.factorial(i-2))
    if rem in a.keys():
        a[rem] += 1
    print(int(cnt))
    cnt = 0

Submission status: Time Limit Exceeded

Solution code:
import copy as cp
import bisect

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

A_sort = cp.copy(A)
A_sort.sort()

number_list = [A_sort[0]]
number_stock_list = [0]
index = 0
for i in range(len(A_sort)):
    if A_sort[i] == number_list[index]:
        number_stock_list[index] += 1
    else:
        number_list.append(A_sort[i])
        number_stock_list.append(1)
        index += 1

for i in range(len(A)):
    index = bisect.bisect_left(number_list, A[i])
    number_stock_list[index] += -1
    x = 0
    for j in range(len(number_stock_list)):
        x += number_stock_list[j] * (number_stock_list[j] - 1) / 2
    print(int(x))
    number_stock_list[index] += +1

Submission status: Time Limit Exceeded

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

u=0
c=b
d=[0]*1000000
k=0
p=[0]*1000000
l=[0]*1000000
o=0
for i in range(a):
    
    
    del b[i]
    #探す
    for j in range(a-1):
        b.count(b[i])
        if b.count(b[i])>=2 and p!=b[j]:
            
            d[k]=b[j]
            k+=1
            p[o]=b[j]
            o+=1
        elif p==b[j]:
            l+=b[j]

            
        
    d.remove(0)
    #
    

    for z in range(len(d)):
        w=1+l.count(d[z])
        for t in range(1,1+l.count(d[z])):
            u+=w-1
            w-=1




        
    


    
    print(u)
    b=c

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

input_1 = input()
input_2 = input_1.split()

A = map(int, input_2)

def equal(num):
    return num
A = [equal(x) for x in A]

for k in range(N):
    ans = 0

    re = A[k]
    A.remove(A[k])

    for l in range(N - 1):
        ans += A.count(A[l]) - 1

    ans = ans / 2

    A.insert(k,re)

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [[] for i in range(n)]

a = [int(i) for i in input().split()]
b = [[] for i in range(n+1)]

for i in a:
    b[i].append(i)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

def ans(N):
    if N >= 2:
        return(cmb(N,2))
    else: return(0)

for i in a:
    cnt = 0
    for j in b:
        try:
            if i == j[0]: cnt += ans(len(j)-1)
            else: cnt += ans(len(j))
        except:
            continue
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
listA = list(map(int,input().split()))
count = 0
l_count = []
ll =[]
m = len(l_count)
for k in range(n):
  a = listA[k]
  for z in range(m):
    ll.append(l_count[z][0])
  if a in ll:
    print(l_count[z][1])
  else:
      del listA[k]
      A = set(listA)
      for i in A:
        c = listA.count(i)
        count += int(c*(c-1)/2)
      print(count)
      listA.insert(k,a)
      l_count.append([a,count])
      count = 0

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num = list(map(int, input().split()))
SetNum = list(set(num))
lenghtNum = len(SetNum)
box = [0]*lenghtNum
for i in range(lenghtNum):
  a = num.count(SetNum[i])
  box[i] = a*(a-1)/2 #要素SetNum[i]の組み合わせの数

for j in num:
  b = SetNum.index(j)
  c = num.count(j)
  if box[b] <= 2:
    d = box[b]
    box[b] = 0
    print(int(sum(box)))
    box[b] = d
  else:
    box[b] = box[b]/c*(c-2)
    print(int(sum(box)))
    box[b] = box[b]*c/(c-2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=sorted([x for x in set(a) if a.count(x)>1], key=a.index)
c=[a.count(i) for i in b]
ans=0
for i in c:
    ans+=((i-1)*i)//2

cnt=ans   
for i in range(n):
   if a[i] in b:
       ans-=(c[b.index(a[i])]-1)
       print(ans)
       ans=cnt
   else:
       print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
li = list(map(int, input().split()))
for k in range(N):
    check_li, ans = li[:k]+li[k+1:], 0
    counter = Counter(check_li)
    for i in counter.most_common():
        cnt = i[1]
        ans += cnt * (cnt-1)
    print(ans//2)

Submission status: Time Limit Exceeded

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

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

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

#print(n)
#print(k)

c = Counter(k)

li = c.most_common()

a_li = [x for x in li if x[1] != 1]

#print(a_li)

max = 0

for i in a_li:
    num = i[1]
    max = max + cmb(num,2)
    
#print(max)

for i in k:
    num = 0
    for a in a_li:
        if a[0] == i:
            num = a[1]
    if num == 0:
        ans = max
    elif num == 2:
        ans = max - 1
    else:
      pre = cmb(num,2)
      cur = cmb(num-1,2)
      ans = max - pre + cur
    print(ans)

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))
  
def main():
  N = int(input())
  l = input().split()
  c = collections.Counter(l)
  ans = 0
  for v in c.values():
    if v >= 2:
      ans += combinations_count(v, 2)
  temp = ans
  for i in range(N):
    ans -= l.count(l[i]) - 1
    print(ans)
    ans = temp
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import collections
from math import factorial
from operator import mul
from functools import reduce

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


N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
#
for a in A:
	c[a]-=1
	#print(c)
	sum=0
	for b in set(c):
		if c[b]>=2:
			sum+=comb(c[b],2)
	c[a]+=1
	print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

n = int(input())

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

an_np = np.array(an)

sum = 0
for i in range(n):
    an_bk = an_np[i]
    an_np[i] = -1
#     u, count = np.unique(an_cp, return_counts=True)
    u = np.unique(an_np)
    cn = 0
    for un in u:
#         print(un)
        uni = len(np.where(an_np == un)[0])
#         print(uni)
        if (uni > 1):
            cn += uni * (uni - 1) / 2
    an_np[i] = an_bk
    print(int(cn))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
@lru_cache(maxsize=1024)
def nC2(n):
    return n * (n-1)//2

N=int(input())
A=list(map(int, input().split()))
Y = set(A)
memo = {}
Z = set()
#数え上げ
str_count = {}
str_count2 = {}
for i in Y:
    str_count[i] = A.count(i)
    str_count2[i] = nC2(str_count[i])
    if str_count[i] != 1:
        Z.add(i)
for k in range(0,N):
    key = A[k]
    if key in memo:
        print(memo[key])
        continue
    c = 0
    for i in Z:
        if i == A[k]:
            c += nC2(str_count[i] - 1)
        else:
            c += str_count2[i]
    memo[key] = c
    print(c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
def conb(n):
	if n>=2:
	    return n*(n-1)//2
	else:
		return 0
list=[A.count(Ai) for Ai in range(1,N+1)]
for k in range(0,N):
	Ak=A[k]-1
	s=0
	for i in range(N):
		if Ak!=i:
			s=s+conb(list[i])
		else:
			s=s+conb(list[i]-1)
	print(s)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = input()
A = list(map(int, input().split()))
ls = []
ansLs = []
for i in range(len(A)):
    l = A.copy()
    if A[i] in ls:
        print(ansLs[ls.index(A[i])])
    else:
        ls.append(l.pop(i))
        count = 0
        for i in list(combinations(l, 2)):
            if i[0] == i[1]:
                count +=1
        ansLs.append(count)
        print(count)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
import math
import collections
import sys


def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))


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


def LS():
    return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()


def S():
    return sys.stdin.buffer.readline().rstrip().decode('utf-8')


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


def SRL(n):
    return [list(S()) for i in range(n)]


def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]

# ----------------------------------------------------------------


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


def main():
    n = I()
    ball = LI()
    def_ans = {}
    ans_list = {}

    c = collections.Counter(ball)
    # c_nums = c.values()

    for key, val in c.items():
        if val >= 2:
            def_ans[key] = combinations_count(val, 2)
        else:
            def_ans[key] = 0

    for i in ball:
        if i in ans_list:
            print(ans_list[i])
            continue
        else:
            ans = 0
            # def_ans_temp = deepcopy(def_ans)
            for key, val in def_ans.items():
                if key != i:
                    ans += val
                elif key == i and c[i] >= 3:
                    ans += val * (c[i] - 2) / (c[i])
                elif key == i and c[i] < 3:
                    continue

            ans_list[i] = int(ans)
            print(int(ans))


main()

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 = []
t1 = []
t2 = []
for i in range(M):
  #t.append(A.count(C[i]))
  temp = A.count(C[i])
  if temp >=3:
    t1.append(math.factorial(temp-1)/(math.factorial(temp-3)*2))
    t2.append(math.factorial(temp)/(math.factorial(temp-2)*2))    
  elif temp == 2:
    t1.append(0)
    t2.append(math.factorial(temp)/(math.factorial(temp-2)*2))
  else:
    t1.append(0)
    t2.append(0)

#print(t,t1,t2)


s = []  
for i in range(M):
  ans = 0
  for j in range(M):
    if i == j:
      ans += t1[j]
    else:
      ans += t2[j]
  s.append(int(ans))

jisyo = dict(zip(C,s))
#print(jisyo[1])
for i in range(N):
  print(jisyo[A[i]])

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
import numpy as np

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

for j in range(N):
  B = copy.copy(A)
  B[j] = 0
  c = collections.Counter(B)
  c = np.array(list(map(int, c.values())))
  print(int(np.sum((c * (c - 1) / 2))))

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  params = [int(i) for i in input().split()]
  sorted_params = sorted(params)
  for param in params:
    first = True
    result = 0
    temp = 0
    pre = -1
    for i in sorted_params:
      if i == param and first:
        first = False
        continue
      
      if pre == i:
        temp += 1
        continue
      
      if temp > 0:
        result += 1 if temp == 1 else factorial(temp+1) / (factorial(temp+1-2) * 2)
        
      pre = i
      temp = 0
    
    if temp > 0:
      result += 1 if temp == 1 else factorial(temp+1) / (factorial(temp+1-2) * 2)
    
    print(int(result))
    
    
def factorial(value):
  result = value
  for i in range(1, value-1):
    result *= (value-i)
  
  return result

main()

Submission status: Time Limit Exceeded

Solution code:
def aC(a):
  if a == 0 or a == 1:
    return 0
  return a*(a-1)/2
 
N = input()
N = int(N)
A = list(map(int, input().split()))
all = 0
count_list = []

for i in range(1,N+1):
  aa = A.count(i)
  all = all + aC(aa)
  count_list.append(aa)
    
for a in A:
  print(int(all-aC(count_list[a-1])+aC(count_list[a-1]-1)))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import numpy as np

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()))
A = np.array(A)

for i in range(N):
    result = 0
    ind = np.ones(N, dtype=bool)
    ind[i] = False
    # pop = A.pop(i)
    c = collections.Counter(A[ind])
    values, counts = zip(*c.most_common())
    for v in counts:
        if v > 1:
            result = result + combinations_count(v, 2)
        else:
            break
        
    # A.insert(i, pop)
    print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = list(map(int, input().split()))
set_l = set(l)

dictionary = {ll: l.count(ll) for ll in set_l}
ll_dict = {}
for ll in set_l:
  cnt = 0
  for k, v in dictionary.items():
    if k==ll:
      v -=1
    cnt += v*(v-1)/2
  ll_dict[ll] = cnt

for n in range(N):
  print(int(ll_dict[l[n]]))

Submission status: Time Limit Exceeded

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

ans = []
for a in set(A):
  if A.count(a) == 1:
    continue
  ans.append((a,A.count(a)))

def f(n):
  return n * (n+1) // 2

memo = [0] * 2 * 10 ** 5
mmemo = [0] * 2 * 10 ** 5
for i in A:
  count = 0
  if i in memo:
    print(mmemo[i])
    continue
  for x,y in ans:
    if i == x:
      y -= 1
    count += f(y - 1)
  memo[i] = i
  mmemo[i] = count
  print(count)

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
a = [0]*N
minus = 0

for i in range(N):
  a[i] = A.count(A[i])
  if(a[i]<2):
    dp[A[i]] = 0
  elif(dp[A[i]] == 0):
    dp[A[i]] = combinations_count(a[i], 2)

ans = sum(dp)

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

#print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  aa = a[:]
  ans = 0
  aa[i] = 0
  for v in itertools.combinations(aa, 2):
    if v[0] == v[1]:
      ans += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
An = input().split()
N = len(An)
index = [0] * N # Aに含まれる数の個数
# index.append(0) # 0の数は0

for i in An:
    if (index[int(i)] == 0):
        index.insert(int(i), int(An.count(i)))

# result
result=[] # 選び出す方法の個数
# result.append(0)
for j in range(len(An) + 1):
    A = index[j]
    if (A == 0):
        result.append([0, 0])
    else:
        result.append([(A*(A-1))//2, ((A-1)*(A-2))//2])


for k in range(1, len(result)):
    ans = 0 # 答え
    for l in range(1, len(result)):
        if (l == int(An[k-1])):
            ans += result[l][1]
            # print("lk result:{}, l:{}".format(result[l][1], l))
        else:
            ans += result[l][0]
            # print("result:{}, l:{}".format(result[l][0], l))
    print(ans)

# print("result = ")
# print(result)
# print("index = ")
# print(index)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    b=a[i]
    a[i]=0
    c = collections.Counter(a)
    cv=c.values()
    s=0
    csv= map(lambda x: comb(x,2), cv)
    s=sum(csv)
    print(s)
    a[i]=b

Submission status: Time Limit Exceeded

Solution code:
import math

from operator import mul
from functools import reduce

N = int(input())
A = [int(s) for s in input().split()]
B = [0 for i in range(N)]
idx = []
for i in range(N):
  B[A[i]-1] += 1
  if B[A[i]-1] >= 2 and not A[i]-1 in idx:
    idx.append(A[i]-1)

def nCr(n,r):
    if n == 1:
        return 0
    r = min(n-r,r)
    if r == 0:
        return 1
    ov = reduce(mul, range(n, n-r, -1))
    un = reduce(mul, range(1, r+1))
    return ov // un

Cs = []
C = []
Ctot = 0
for j in idx:
  if B[j]-1 >= 2:
    Cs.append(nCr(B[j]-1, 2))
  else:
    Cs.append(0)
  tmp = nCr(B[j], 2)
  C.append(tmp)
  Ctot += tmp
for i in range(N):
  current_idx = A[i]-1
  if current_idx in idx:
    count = Ctot + Cs[idx.index(current_idx)]-C[idx.index(current_idx)]
  else:
    count = Ctot
  print(count)

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())
A = list(map(int, input().split(' ')))
result = {}

for c in set(A):
  if A.count(c) > 1:
    count = 0
    for x in range(1, A.count(c)):
      count += x
    result[c] = count
  else:
    result[c] = 0
  
for k in range(N):
  count = 0
  result_ = result.copy()
  result_[A[k]] -= A.count(A[k])
  print(sum(result_.values()) + 1)

Submission status: Time Limit Exceeded

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

#print(N)
#print(A)
all = {}
for a in A:
  if a not in all:
    all[a] = 0
  all[a] +=1

#print(all)
for k in range(len(A)):
  ans = 0
  for key, v in all.items():
    if v > 1:
      if key == A[k]:
      	ans += int(max(int(v-1)*(v-2)/2, 0))
      else:
        ans += int(v*(v-1)/2)

  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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


memo = {}
memo2 = {}
for i in range(n):
    if nums[i] in memo2:
        print(memo2[nums[i]])
    else:
        a_list = nums[:i] + nums[i+1:]
        cnt = 0
        for j in set(a_list):
            u_num = a_list.count(j)
            if u_num in memo:
                cnt += memo[u_num]
            else:
                calc = comb(u_num, 2)
                cnt += calc
                memo[u_num] = calc

        memo2[nums[i]] = cnt
        print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = list(map(int, input().split()))
sett = set(lst)

if len(lst) == len(sett):
    for i in range(n):
        print(0)
elif len(sett) == 1:
    for i in range(n):
        print(sum(list(range(3, (n-1)+1))) - ((n-1)-3))
else:
    newLst = []
    for x in sett:
        newLst.append(lst.count(x))
    dic = dict(zip(sett, newLst))
    for i in range(n):
        summ = 0
        dic[lst[i]] = dic[lst[i]]-1
        for x in list(dic.values()):
            if x < 2:
                pass
            elif x == 2:
                summ = summ + 1
            else:
                summ = summ + sum(list(range(3, x+1))) - (x-3)
        dic[lst[i]] = dic[lst[i]]+1
        print(summ)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial
N=int(input())
A=list(map(int,input().split(" ")))
count=Counter(A)
com={}
def combo(n):
    if n>=2:
        return factorial(n)//(factorial(n-2)*2)
    else:
        return 0

for k,v in count.items():
    com[k]=combo(v)
s=sum(com.values())
for i in range(N):
    su=s-combo(count[A[i]])+combo(count[A[i]]-1)
    print(su)

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    c = collections.Counter(A)
    for k in range(len(A)):
        ans = 0
        c[A[k]] = c[A[k]] - 1
        ans = sum([cmb(i, 2) for i in c.values()])
        print(ans)
        c[A[k]] = c[A[k]] + 1
    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = [0] * M
#print(ser)
for i in range(1,M+1):
    copy = counts[:]
    copy[i] = 0
    #print(ser[i])
    c = ser[i] - 1
    S = sum(copy) + int(c*(c-1)/2)
    ans[i-1] = S

#print(ans)
for j in range(N):
    print(ans[nums[j]-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

B = list(set(A))

C = []
for i in B:
  C.append([i,A.count(i)])

for i in A:
  ans = 0
  for j in range(len(C)):
    if C[j][0] == i:
      num = C[j][1] - 1
    else:
      num = C[j][1]
    if num < 2:
      ans += 0
    else:
      ans += num * (num-1) // 2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
A = input().split(" ")

unique = set(A)

def combination(number):
  if number == 0 or number == 1:
    return 0
  else:
    return int(number * (number - 1) / 2)

def caluculate(string):
  if memo[string] == 0:
    return(0)
  if memo[string] == 1:
    return(0)
  else:
    return(memo[string] - 1) 
  
total = 0
memo = {}
for i in unique:
  times = A.count(i)
  memo[i] = times
  total += combination(times)

# print(memo)
# print(memo.keys())
# print(total)

for i in A:
  #  print(caluculate(i))
  print(total - caluculate(i))

Submission status: Time Limit Exceeded

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    n=int(input())
    a=list(map(int,input().split()))
    s1=list(set(a))
    if len(a)==len(s1):
        for i in a:
            print(0)
        return 0

    dp=[float('inf')]*len(s1)
    for i in a:
        idx=s1.index(i)
        if dp[idx] == float('inf'):
            cnt=0
            sum=0
            for j in s1:
                cnt=a.count(j)
                if i==j:
                    sum+=(cnt-1)*(cnt-2)//2
                else:
                    sum+=cnt*(cnt-1)//2 
                dp[idx]=sum
        print(dp[idx])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections
import copy
def cmb(n,r):
  if n-r<0:
    return 0
  return factorial(n) // factorial(r) // factorial(n - r)
n=int(input())
a=list(map(int,input().split()))
ccc=[]
for i in range(n):
  ccc.append(cmb(i,2))

c=collections.Counter(a)
for aa in a:
  t=copy.copy(c)
  o=0
  t[aa]-=1
  for i in t.values():
    o+=ccc[i]
  print(o)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

a = np.array([i for i in list(map(int, input().split()))])
d = {}
s = {}

for i in set(a):
  d[i] = len(a[a==i])
  
sum = 0
for k in d.values():
  if k>1:
    sum += (k*(k-1))//2

for i in range(n):
  nd = d[a[i]]
  ans = sum-(nd*(nd-1))//2
  nd -= 1
  if nd>1:
    ans += (nd*(nd-1))//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
def cmb(n):
	if n<=1:
		return 0
	else:
		return int(n*(n-1)*0.5)
def cmb2(n):
  if n<=1:
    return 0
  else:
    return 1-n
N = int(input())
A = [int(x) for x in input().split()]
ans=[0]*N
lists={}
k=0
for i in range(N):
  if not A[i] in lists:
    n=A.count(A[i])
    l=cmb(n)
    m=cmb2(n)
    k+=l
    lists[A[i]]=m
    ans[i]=m
  else:
    ans[i]=lists[A[i]]
    
for i in range(N):
  ans[i]+=k
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int, input().split()))
set_of_numbers = set(P)
diff = {}
result_sum_all = 0
for num in set_of_numbers:
    num_of_num = P.count(num)
    pair_set = int(num_of_num*(num_of_num-1)/2)
    pair_set_k = int((num_of_num-1)*(num_of_num-2)/2)
    result_sum_all += pair_set
    diff[num] = pair_set_k - pair_set

for k in range(N):
    # 取り除く数字はP[k]
    result = result_sum_all + diff[P[k]]
    print(result)

Submission status: Time Limit Exceeded

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

counters = {}
cevap_list = {}
sayilar = set(numbers)
for i in sayilar:
    count = numbers.count(i)
    counters[i] = count

for i in sayilar:
    sonuc = 0
    for sayi in counters:
        count = counters[sayi]
        if i == sayi:
            count -= 1
        sonuc += count * (count - 1) / 2
    cevap_list[i] = int(sonuc)

for i in range(n):
    print(cevap_list[numbers[i]])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ser = list(map(int, input().split()))
cnt = [0]*(N+1)
for s in ser: cnt[s] += 1
for n in ser:
    cnt[n] -= 1
    ans = 0
    for c in cnt:
        ans += (c*(c-1)//2 if c>=2 else 0)
    cnt[n] += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

#****************************************************************
def comb(n):
    if n < 2:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
#****************************************************************

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

c = collections.Counter(A)
full_ans = sum(list(map(comb, c.values())))
dict_ans = {}

for i in c.keys():
    target = c.copy()
    target[i] -= 1
    if target[i] == 0:
        dict_ans[i] = full_ans
    else:
        dict_ans[i] = sum(list(map(comb, target.values())))

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