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:
# -*- coding: utf-8 -*-
import math

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

def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    counter = 0
    for a in set(A):
        counter+=combinations_count(A.count(a),2)

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

for i in range(n):
    ans = 0
    pop = a.pop(i)
    new_a = collections.Counter(a)
    # print(new_a)
    for key,val in new_a.items():
        # print(val)
        if val > 1:
            ans += (val * (val - 1) // 2)
    a.insert(i, pop)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [0]*N
for n in range(N):
  B[A[n]-1] +=1
result = [0]*N
for n in range(N):
  total=0
  for m in range(N):
    if m==A[n]:
      total+= (B[m-1]-2)*(B[m-1]-1)//2
    else:
      total+= B[m-1]*(B[m-1]-1)//2
  result[n]=total
for n in range(N):
  print(result[n])

Submission status: Time Limit Exceeded

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

from collections import Counter

cnt = Counter(a)
b = set(a)

for i in range(n):
    ans = 0
    for j in b:
        if j == a[i]:
            c = cnt[j]-1
            ans += c*(c-1)//2
        else:
            c =cnt[j]
            ans += c*(c-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
sum=0
for i in set(A):
    c=A.count(i)
    sum+=c*(c-1)/2

for i in A:
    if A.count(i) >= 2:
        c = A.count(i) - 1
        print(int(sum-c))
    else:
        print(0)

Submission status: Time Limit Exceeded

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

cnt_a=0
cnt_b=0

for i in range(n-1):
    for j in range(i+1, n):
        if a[i] == a[j]:
            cnt_a += 1

for i in range(n):
    cnt_b=0
    for j in range(n):
        if i!=j and a[i]==a[j]:
            cnt_b+=1
    print(cnt_a-cnt_b)

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

memo = [0 for i in range(10 ** 6)]
ans = 0
for k in dic:
  tmp  =dic[k]
  if tmp >= 2:
    if memo[tmp] == 0:
      memo[tmp] = C(tmp, 2)
      ans += memo[tmp]
    else:
      ans += memo[tmp]

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  elif tmp == 2:
    print(ans - 1)
  else:
    if memo[tmp - 1] == 0:
      print(ans - (memo[tmp] - C(tmp-1, 2)))
    else:
      print(ans - (memo[tmp] - memo[tmp-1]))

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

memo = {}
ans = 0
for k in dic:
  tmp  =dic[k]
  if tmp >= 2:
    memo[k] = C(tmp, 2)
    ans += memo[k]

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  elif tmp == 2:
    print(ans - 1)
  else:
    print(ans - (memo[A[i]] - C(tmp-1, 2)))

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

memo = {}
ans = 0
for k in dic:
  tmp  =dic[k]
  if tmp >= 2:
    memo[k] = C(tmp, 2)
    ans += memo[k]

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  if tmp == 2:
    print(ans - 1)
  if tmp > 2:
    print(ans - (memo[A[i]] - C(tmp-1, 2)))

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

memo = {}
ans = 0
for k in dic:
  tmp  =dic[k]
  if tmp >= 2:
    memo[k] = C(tmp, 2)
    ans += memo[k]

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  if tmp == 2:
    print(ans - 1)
  if tmp > 2:
    print(ans - (memo[A[i]] - C(tmp-1, 2)))

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

ans = 0
for k in dic:
  tmp  =dic[k]
  if tmp >= 2:
    ans += C(tmp, 2)

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  if tmp == 2:
    print(ans - 1)
  if tmp > 2:
    print(ans - (C(tmp, 2) - C(tmp-1, 2)))

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

ans = 0
for k in dic:
  if dic[k] >= 2:
    ans += C(dic[k], 2)

for i in range(N):
  tmp = dic[A[i]]
  if tmp == 1:
    print(ans)
  if tmp == 2:
    print(ans - 1)
  if tmp > 2:
    print(ans - (C(tmp, 2) - C(tmp-1, 2)))

Submission status: Time Limit Exceeded

Solution code:
import math

def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)

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

dic = {}
for i in range(N):
  if A[i] in dic:
    dic[A[i]] += 1
  else:
    dic[A[i]] = 1

ans = 0
for k in dic:
  if dic[k] >= 2:
    ans += C(dic[k], 2)

for i in range(N):
  if dic[A[i]] == 1:
    print(ans)
  if dic[A[i]] == 2:
    print(ans - 1)
  if dic[A[i]] > 2:
    print(ans - (C(dic[A[i]], 2) - C(dic[A[i]]-1, 2)))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
a = list(map(int, input().split()))
tmp = 0
for j in range(n):
    num = 0
    ans = 0
    tmp = a[j]
    a[j] = 0
    for k in range(n):
        num = a.count(k+1)
        if num >= 2:
            ans += math.factorial(num)//(math.factorial(num-2)*math.factorial(2))
    a[j] = tmp
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
tmp = 0
for j in range(n):
    num = 0
    ans = 0
    tmp = a[j]
    a[j] = 0
    for k in range(n):
        num = a.count(k+1)
        for l in range(num):
            ans += l
    a[j] = tmp
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
List = list(map(int,input().split()))
List_temp = []
List_count = []
count = 0

for i in range(N):
  count = 0
  List_temp = []
  List_count = []
  
  for j in range(N):
    List_temp.append(int(List[j]))
    
  List_temp[i] = -1
  for k in range(max(List)+1):
    List_count.append(List_temp.count(k))
  
  for l in range(len(List_count)):
    if List_count[l] > 1:
      count = count + (List_count[l])*(List_count[l]-1)/2
  print(int(count))

Submission status: Time Limit Exceeded

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from itertools import combinations
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# a/b の切り上げ


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    n = int(input())
    a = wi()
    b = [0]*n
    ans = 0

    for i in range(n):
        b[i] = a.count(i+1)
    # print(b)
    for i in range(n):
        ans += b[i] * (b[i] - 1) // 2
        #c[i] = b[a[i]-1] - 1

    for i in range(n):
        c = b[a[i]-1]
        print(ans - c + 1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from itertools import combinations
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# a/b の切り上げ


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    n = int(input())
    a = wi()
    b = [0]*n
    ans = 0

    for i in range(n):
        b[i] = a.count(i+1)
    # print(b)
    for i in range(n):
        ans += b[i] * (b[i] - 1) // 2
        #c[i] = b[a[i]-1] - 1

    for i in range(n):
        d = b[a[i]-1] * (b[a[i]-1] - 1)//2
        e = (b[a[i]-1] - 1) * (b[a[i]-1] - 2)//2
        print(ans - d + e)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = [int(i) for i in input().split()]
t = set(L)
dn = {}
dc = {}
All = 0
for i in t:
  dn[i] = L.count(i)
  dc[i] = int(dn[i]*(dn[i]-1)/2)
  All += dc[i]
for i in range(n):
  print(All-dc[L[i]] + int((dn[L[i]]-1)*(dn[L[i]]-2)/2))

Submission status: Time Limit Exceeded

Solution code:
def main():
	s = int(input())
	a = list(map(int,input().split()))
	import collections
	for i in range(s):
		# print("i is {}".format(i))
		c = collections.Counter(a[0:i] + a[i+1:s])
		Set = set(a[0:i] + a[i+1:s])
		# print("Set is {}".format(Set),"c is {}".format(c))
		ans = 0
		for t in Set:
			# print("t is {}".format(t))
			if c[t] > 1:
				ct = int(c[t])
				ans += (ct * (ct-1))/2
		print(int(ans))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
def main():
	s = int(input())
	a = list(map(int,input().split()))
	from operator import mul
	from functools import reduce
	
	def cmb(n, r):
		if n - r < r: r = n - r
		if r == 0: return 1
		if r == 1: return n

		numerator = [n - r + k + 1 for k in range(r)]
		denominator = [k + 1 for k in range(r)]

		for p in range(2,r+1):
			pivot = denominator[p - 1]
			if pivot > 1:
				offset = (n - r) % p
				for k in range(p-1,r,p):
					numerator[k - offset] /= pivot
					denominator[k] /= pivot

		result = 1
		for k in range(r):
			if numerator[k] > 1:
				result *= int(numerator[k])

		return result
	# print(cmb(3,2))
	import collections
	for i in range(s):
		# print("i is {}".format(i))
		c = collections.Counter(a[0:i] + a[i+1:s])
		Set = set(a[0:i] + a[i+1:s])
		# print("Set is {}".format(Set),"c is {}".format(c))
		ans = 0
		for t in Set:
			# print("t is {}".format(t))
			if c[t] > 1:
				ans += cmb(int(c[t]),2)
		print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
def main():
	s = int(input())
	a = list(map(int,input().split()))

	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

	import collections
	for i in range(s):
		c = collections.Counter(a[0:i] + a[i+1:s])
		Set = set(a[0:i] + a[i+1:s])
		ans = 0
		for t in Set:
			if c[t] > 1:
				ans += cmb(int(c[t]),2)
		print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Alist=list(map(int, input().split()))
numdic = {}
for i in Alist:
    if i in numdic:
        numdic[i] += 1
    else:
        numdic[i] = 1
ansdic = dict.fromkeys(numdic, [0,False])
for i in Alist:
    if ansdic[i][1]:
        print(ansdic[i][0])
    else:
        kazu = 0
        numdic[i] -= 1
        for l in numdic.values():
            kazu += l*(l-1)//2
        ansdic[i] = [kazu,True]    
        numdic[i] += 1
        print(kazu)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
src = list(map(int,input().split()))
sor = Counter(src)

for n in range(N):
  i = src[n]
  count = 0
  for key,value in sor.items():
    if int(key) == i:
      count += (value-1)*(value-2)/2
    else:
      count += value*(value-1)/2
  print(int(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Alist=list(map(int, input().split()))
numdic = {}
for i in Alist:
    if i in numdic:
        numdic[i] += 1
    else:
        numdic[i] = 1
for i in Alist:
    numdic[i] -= 1
    kazu = 0
    for l in numdic.values():
        kazu += l*(l-1)//2
    print(kazu)
    numdic[i] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

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

# check
ansed = {}
for idx, a in enumerate(A):
    check_a = A.copy()
    del check_a[idx]

    counter = Counter(check_a).values()
    fef = 0
    for count in [c for c in counter if c >= 2]:
        fef += factorial(count) // (factorial(2) * factorial(count - 2))

    print(fef)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
li=list(map(int,input().split()))
d={}
dif={-1}
ans={}
for i in range(n):
    if li[i] not in dif:
        dif|={li[i]}
    if d.get(li[i])!=None:
        d[li[i]]+=1
    else:
        d[li[i]]=1
dif.remove(-1)
for i in dif:
    g=0
    for j in dif:
        if i!=j:
            g+=d[j]*(d[j]-1)//2
        else:
            g+=(d[j]-2)*(d[j]-1)//2
    ans[i]=g
for i in li:
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))
l=[0]*(n)
l1=[0]*(n)
for i in range(n):
    l[a[i]-1]+=1
    
def combinations_count(n,r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))

for i in range(n):
    if l[i]>=2:
        l1[i]+=combinations_count(l[i],2)

l1sum=sum(l1)
for i in range(n):
    if l[a[i]-1]-1>=2:print(l1sum-l1[a[i]-1]+combinations_count(l[a[i]-1]-1,2))
    else:print(l1sum-l1[a[i]-1])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
List=list(map(int,input().split()))
cnt=[0 for i in range(max(List)+1)]
for x in List:
  cnt[x]+=1
  
for i in range(1,N+1):
  cnt[List[i-1]]-=1
  print(sum([cnt[j]*(cnt[j]-1)//2 for j in range(len(cnt))])) 
  cnt[List[i-1]]+=1

Submission status: Time Limit Exceeded

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

b = []
total = 0

for i in range(n):
    b.append(0)

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

for i in range(n):
    b[a[i]-1] -= 1
    for j in range(n):
        if b[j] >= 2:
            total += b[j]*(b[j]-1)//2
    print(total)
    total = 0
    b[a[i]-1] += 1

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(1, N+1):
    num = A.count(i)
    ans += num * (num - 1) // 2
for k in range(1, N+1):
    n = A.count(A[k-1])
    ans_ = ans - (n - 1)
    print(ans_)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = list(map(int, input().split(" ")))
ans = []

sum = 0
for i in range(1, n + 1):
    tmp = numlist.count(i)
    sum += int(tmp * (tmp - 1) / 2)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = list(map(int, input().split(" ")))
ans = []

for i in range(1, n + 1):
    ans.append(numlist.count(i))

sum = 0
for i in ans:
    sum += int(i * (i - 1) / 2)

for i in numlist:
    print(sum - ans[i - 1] + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = list(map(int, input().split(" ")))
ans = []

for i in range(1, n + 1):
    ans.append(numlist.count(i))

for i in numlist:
    ans[i - 1] -= 1
    sum = 0
    for j in ans:
        sum += int(j * (j - 1) / 2)
    print(sum)
    ans[i - 1] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

n = int(input())
list1 = list(map(int,input().split()))
list2 = collections.Counter(list1)
list3 = copy.copy(list1)

kei1=0
kei2=0

for j in list2.values():
    kei1 += j * (j-1) // 2

for x in range(n):
    list3 = copy.copy(list1)
    k = list3.pop(x)
    if k in list3:
        kei2 = list3.count(k)

    print(kei1-kei2)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
list1 = list(map(int,input().split()))
list2 = copy.copy(list1)
list3 = copy.copy(list1)

kei1=0
kei2=0

for j in range(n):
    l = list2.pop(0)
    if l in list2:
        kei1 += list2.count(l)

for x in range(n):
    list3 = copy.copy(list1)

    k = list3.pop(x)
    if k in list3:
        kei2 = list3.count(k)

    print(kei1-kei2)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
list1 = list(map(int,input().split()))
list2 = copy.copy(list1)

for x in range(n):
    kei = 0
    list2 = copy.copy(list1)
    del list2[x]
    for m in range(n-1):
        l = list2.pop(0)
        if l in list2:
            kei += list2.count(l)

    print(kei,sep="\n")

Submission status: Time Limit Exceeded

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

Al = 0
cnt = 0
def solution(N,A,B,Al,cnt):
  for a in B:
      cnt = A.count(a)
      Al +=  cnt*(cnt-1)//2

  for k in range(N):
      cnt = A.count(A[k])
      print(Al - cnt +1)
solution(N,A,B,Al,cnt)

Submission status: Time Limit Exceeded

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

All = 0
cnt = 0
for a in B:
    cnt = A.count(a)
    All +=  cnt*(cnt-1)//2

for k in range(N):
    ans_k = 0
    cnt = A.count(A[k])
    ans_k = cnt - 1
    Ans = All - ans_k
    print(Ans)

Submission status: Time Limit Exceeded

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

def main(N,A,B):
    for k in range(N):
        ans_k = 0
        rem_A = A[k]
        A.remove(rem_A)
        for n in B:
            cnt = A.count(n)
            ans_k += cnt*(cnt-1)//2
        A.insert(k,rem_A)
        print(ans_k)

main(N,A,B)

Submission status: Time Limit Exceeded

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

for k in range(N):
    ans_k = 0
    rem_A = A[k]
    A.remove(rem_A)
    for n in B:
        cnt = A.count(n)
        ans_k += cnt*(cnt-1)//2
    A.insert(k,rem_A)
    print(ans_k)

Submission status: Time Limit Exceeded

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

for n in range(N):
  list[n]=A.count(n+1)

for n in range(N):
  sum += list[n]*(list[n]-1)//2
  

for n in range(N):
  if list[n]>0:
  	list2[n]=sum-list[n]*(list[n]-1)//2+(list[n]-1)*(list[n]-2)//2
  
for a in A:
  print(list2[a-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def comb(n):
  if n<2:
    return 0
  return int(n*(n-1)/2)

n = int(input())
arr = list(map(int,input().split()))
data = Counter(arr)
t = set(arr)
dp = {}
for i in arr:
	if i in dp.keys():
		print(dp[i])
	else:
		ans = 0
		temp = data[i] - 1
		for j in t:
			if j != i:
				ans += comb(data[j])
			else:
				ans += comb(temp)
		dp[i] = ans
		print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def comb(n):
  if n<2:
    return 0
  return int(n*(n-1)/2)

n = int(input())
arr = list(map(int,input().split()))
data = Counter(arr)

dp = {}
for i in arr:
	if i in dp.keys():
		print(dp[i])
	else:
		ans = 0
		temp = data[i] - 1
		for j in set(arr):
			if j != i:
				ans += comb(data[j])
			else:
				ans += comb(temp)
		dp[i] = ans
		print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    '''
    code here
    '''

    import collections
    import math

    N = int(input())
    A_list = [int(item) for item in input().split()]
    origin_dict = collections.Counter(A_list)

    memo = [-1 for _ in range(N+1)]

    for i in range(N):
        temp_num = A_list[i]

        if memo[temp_num] == -1:
            temp_dict = origin_dict.copy()
            temp_dict[temp_num] -= 1
            # print(temp_dict)
            res = 0
            for j in temp_dict.values():
                res += j*(j-1)//2
            print(res)
            memo[temp_num] = res
        else:
            print(memo[temp_num])

    

if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

c=[0 for i in range(N+1)]
g=[0 for i in range(N+1)]

wa=0
for i in range(1,N+1):
    #b[a[i]-1]=b[a[i]-1]+1
    g[i]=str(a).count(str(i))
    #print(g[i])
    #print(str(a))
    #print(i)
    if(g[i]>1):
        c[i]=combinations_count(g[i],2)
        wa=c[i]+wa
    #    print(g)
    #    print(c[i])
    else:
        c[i]=0

        #print(c[i])


wawa=wa
#print(wa)
for i in range(1,N+1):

    wa=wa-g[a[i-1]]+1
    #print(g[a[i]-1])
    print(wa)
    wa=wawa

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

c=[0 for i in range(N)]

wa=0
for i in range(N):
    #b[a[i]-1]=b[a[i]-1]+1
    g=str(a).count(str(i))
    #print(str(a))
    #print(i)
    if(g>1):
        c[i]=combinations_count(g,2)
        wa=c[i]+wa
    #    print(g)
    #    print(c[i])
    else:
        c[i]=0

        #print(c[i])


wawa=wa
#print(wa)
for i in range(N):
    d=i+1
    wa=wa-str(a).count(str(a[i]))+1
    print(wa)
    wa=wawa

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

c=[0 for i in range(N)]

wa=0
for i in range(N):
    #b[a[i]-1]=b[a[i]-1]+1
    g=str(a).count(str(i))
    #print(str(a))
    #print(i)
    if(g>1):
        c[i]=combinations_count(g,2)
    #    print(g)
    #    print(c[i])
    else:
        c[i]=0
        #print(c[i])

wa=sum(c)
wawa=wa
#print(wa)
for i in range(N):
    d=i+1
    wa=wa-str(a).count(str(a[i]))+1
    print(wa)
    wa=wawa

Submission status: Time Limit Exceeded

Solution code:
import math

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


N=int(input())
a=[int(i) for i in input().split()]
b=[0 for i in range(N)]
c=[0 for i in range(N)]

wa=0
for i in range(N):
    b[a[i]-1]=b[a[i]-1]+1
    if(i>1):
        c[i]=combinations_count(i,2)
    else:
        c[i]=0

for j in range(N):
    for i in range(N):
#        print('a')
        if(b[i]>1):
            #print('{},{}'.format(j,i))
            #print(b[i])

            if(a[j]==i+1):
                if(b[i]-1>1):
                    wa=c[b[i]-1]+wa
            else:
                wa=c[b[i]]+wa


    print(wa)
    wa=0

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
k = list(input().split())
dic = {}
c = 0

for i in range(len(k)):
    if k[i] not in dic:
        freq = k.count(k[i])
        dic[int(k[i])] = k.count(k[i])

for m in dic.values():
    c += (m*(m-1))/2


for n in range(len(k)):
    print(int(c - (dic[int(k[n])]*(dic[int(k[n])]-1)/2) + ((dic[int(k[n])]-1)*((dic[int(k[n])]-2))/2)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
k = list(input().split())
dic = {}
c = 0

for i in range(len(k)):
    if k[i] not in dic:
        freq = k.count(k[i])
        dic[int(k[i])] = k.count(k[i])

for m in dic.values():
    c += (m*(m-1))/2


for n in range(len(k)):
    c -= (dic[int(k[n])]*(dic[int(k[n])]-1)/2)
    c += ((dic[int(k[n])]-1)*((dic[int(k[n])]-2))/2)
    print(int(c))
    c += (dic[int(k[n])]*(dic[int(k[n])]-1)/2)
    c -= ((dic[int(k[n])]-1)*((dic[int(k[n])]-2))/2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=set(a)
s=0

for i in l:
    cnt_i=a.count(i)
    s+=cnt_i*(cnt_i -1)/2

for i in a:
    cnt=a.count(i)
    print(int(s-cnt+1))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=set(a)
li=[]

for i in l:
    cnt_i=a.count(i)
    li.append(cnt_i*(cnt_i -1)/2)
s=sum(li)

for i in a:
    cnt=a.count(i)
    print(int(s-cnt+1))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


def c(n):
    if n < 2:
        return 0
    return factorial(n) // (factorial(n-2) * factorial(2))


def main():
    N = int(input())  # 文字列または整数(一変数)
    a = list(map(int, input().split()))  # 整数のリスト
    for i in range(N):
        a[i] -= 1

    nums = [0 for _ in range(N)]

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

    b = [c(nums[i]) for i in range(N)]
    sum_b = sum(b)

    b_alt = [c(nums[i]-1) for i in range(N)]

    for i in range(N):
        print(sum_b - b[a[i]] + b_alt[a[i]])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


def c(n):
    if n < 2:
        return 0
    return factorial(n) // (factorial(n-2) * factorial(2))


def main():
    N = int(input())  # 文字列または整数(一変数)
    a = list(map(int, input().split()))  # 整数のリスト
    for i in range(N):
        a[i] -= 1

    nums = [0 for _ in range(N)]

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

    b = [c(nums[i]) for i in range(N)]
    sum_b = sum(b)

    for i in range(N):
        print(sum_b - b[a[i]] + c(nums[a[i]] - 1))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

K = {}
for row in A:
    if row in K:
        K[row] += 1
    else:
        K[row] = 1

# 辞書Kに関して全探索
D = {}
for row in A:
    ans = 0
    if row not in D:
        for i in K:
            tmp = K[i]
            if i == row:
                tmp = K[i]-1
            ans += tmp * ( tmp-1 ) / 2
        D[row] = int(ans)
        print(D[row])
    else:
        print(D[row])

Submission status: Time Limit Exceeded

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

K = {}
for row in A:
    if row in K:
        K[row] += 1
    else:
        K[row] = 1

# 辞書Kに関して全探索
D = {}
C = {}
for row in A:
    ans = 0
    if row not in D:
        for i in K:
            tmp = 0
            if i == row:
                tmp = K[i]-1
            else:
                tmp = K[i]
            if tmp not in C:
                ans += tmp * ( tmp-1 ) / 2
            else:
                ans += C[tmp]
        D[row] = int(ans)
        print(D[row])
    else:
        print(D[row])