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



for i in range(N):
    ans_L = []
    tmp_L = copy.deepcopy(L)
    tmp_L.pop(i)
    
#     print(tmp_L)
    for j in range(2**(N-1)):
        count = 0
        tmp2_L = []
        for k in range(N-1):
            if j>>k&1 == 1:
                count += 1
                tmp2_L.append(tmp_L[k])
        if count==2:
            ans_L.append(tmp2_L)
#     print(ans_L)
    ans = 0
    for x in ans_L:
        if x[0]==x[1]:
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a_list = list(map(int,input().split()))
c = collections.Counter(a_list)
for i in a_list:
  dic = c.copy()
  dic[i] = dic[i] - 1
  result = 0
  for s in dic.values():
    result += s*(s - 1)//2
  print(result)

Submission status: Time Limit Exceeded

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
arrA = list(map(int,input().split()))

ctA = Counter(arrA)
dp = {}
for i in arrA:
    if i in dp:
        print(dp[i])
        continue
    ctA[i] -= 1
    temp = 0
    for k,v in ctA.items():
        if v>1:
            temp += v*(v-1)//2
    print(temp)
    dp[i] = temp
    ctA[i] += 1

# print(*ans,sep="\n")
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Time Limit Exceeded

Solution code:

N=int(input())
A=list(map(int,input().split()))
B=list(set(A))
C=[]
D=[]
for i in B:
    k=A.count(i)


    C.append(int((k-1)*(k-2)/2))
    D.append(int(k*(k-1)/2))

for i in A:

    iindex=B.index(i)
    iindexnum=C[iindex]
    num=sum(D)-D[iindex]
    ans=iindexnum+num
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import copy

a = input()
a=int(a)
ball=[]
mydict={}

temp=input().split(" ")
for i in range(a):
    ball.append(int(temp[i]))
for i in range(a):
    balltemp=copy.copy(ball)
    dictindex=str(balltemp[i])
    del balltemp[i]
    retnum=0
    if dictindex in mydict:
        print(mydict[dictindex])
        continue
    for j in range(len(balltemp)):
        for k in range(j+1,len(balltemp)):
            if balltemp[j]==balltemp[k]:
                retnum+=1
    mydict[dictindex]=retnum
    print(retnum)

# スペース区切りの整数の入力

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, A: "List[int]"):
    from collections import Counter
    import math
    from functools import lru_cache

    @lru_cache(None)
    def combination(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    c = Counter(A)
    
    ta = 0
    for k,v in c.items():
        if v >= 2:
            ta += combination(v, 2)
            
    # dp = [-1] * (N+1)
    ans = []
    for i in range(N):
        t = ta
        if c[A[i]] >= 2:
            t -= combination(c[A[i]], 2)
        if c[A[i]]-1 >= 2:
            t += combination(c[A[i]]-1, 2)    
        ans.append(t)

 
    for i in ans:
        print(i)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# D
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 = dict(collections.Counter(A))
su = 0
for i in c.values():
    if (i >= 2):
        su += combinations_count(i, 2)

for i in A:
    if (c[i]  >= 3):
        print(su - combinations_count(c[i],2) + combinations_count(c[i]-1,2))
    elif (c[i] == 2):
        print(su - 1)
    else:
        print(su)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
def motomeru(i):
  m = l.count(i)
  return int(m*(m-1)/2)
def herasu(i):
  m = l.count(i)-1
  return int(m*(m-1)/2)
zenbu = 0
for i in range(n):
  zenbu = zenbu + motomeru(i+1)
for i in range(n):
  a = zenbu - motomeru(l[i]) + herasu(l[i])
  print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
N=int(input())
A=list(map(int,input().split()))

l=[0]*(N+1)
cnt=0

for k in range(N):
	for i in range(len(A)):
		if i==k:
			continue
		l[A[i]]+=1
	for j in l:
		cnt+=(j*(j-1))//2
	print(cnt)
	cnt=0
	l=[0]*(N+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ARR = [int(s) for s in input().split(" ")]
import numpy as np

def calculate(n,arr):

    dict = {}
    for ar in arr:

        if dict.get(ar) == None:
            dict.__setitem__(ar,1)
        else:
            tmp = dict.get(ar)
            tmp = tmp + 1
            dict.__setitem__(ar,tmp)


    for ar in arr:
        tmp = dict.get(ar)
        dict.__setitem__(ar,tmp-1)

        vals = np.array(list(dict.values()))
        print(sum((vals * (vals-1)) // 2))

        dict.__setitem__(ar,tmp)

calculate(N,ARR)

Submission status: Time Limit Exceeded

Solution code:
def func(num):
    return int(num * (num - 1) / 2)


n = int(input())
a = list(map(int, input().split()))
dict = {}
for i in range(n):
    if a[i] in dict:
        dict[a[i]] = dict[a[i]] + 1
    else:
        dict[a[i]] = 1
for i in range(n):
    # print("xxx", i, a[i])
    sum = 0
    for key, value in dict.items():
        if key == a[i]:
            # print("aaa")
            sum = sum + func(value - 1)
        else:
            # print("bbb")
            sum = sum + func(value)
        # print("yyy", key, value, sum)
    print(sum)

Submission status: Time Limit Exceeded

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

for i in range(N):
    B = Counter(A[:i] + A[i+1:])
    #print(B)
    ans = 0
    for n in B.values():
        ans += n*(n-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    for i in range(N):
        ans = 0
        a = A[:]
        del a[i]
        C = Counter(a)
        val = C.values()
        for num in val:
            ans += num * (num - 1) // 2
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Lists=[]
for i in range(n):
    Lists.append(a.count(i+1))
#print(a)
#print(Lists)
moto=0
for j in range(n):
    if ((Lists[j]>1)):
        moto+=Lists[j]*(Lists[j]-1)//2

#print(moto)
#print(Lists)
for i in range(n):
    if (Lists[a[i]-1]>1):
        ans=moto+1-Lists[a[i]-1]
    else:
        ans=moto
    print(ans)

Submission status: Time Limit Exceeded

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

for k in range(len(A)):
    A1 = A[:k]
    A1.extend(A[k+1:])
    S1 = list(set(A1))
    ans = 0
    for i in range(len(S1)):
        med1 = A1.count(S1[i])
        ans = ans + int(med1*(med1-1)/2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = tuple(map(int, input().split()))
C = Counter(A)

ansers = dict()
for i in range(N):
    print(sum(v*(v-1)//2 if A[i]!=k else (v-1)*(v-2)//2 for k, v in C.items()))

Submission status: Time Limit Exceeded

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

def put_item(table, item):
    for i in range(0, len(table)):
        if table[i][0] == item:
            table[i][1] = table[i][1] + 1
            return 
    table.append([item, 1])

def histogram(data):
    result = []
    for i in range(0, len(data)):
        put_item(result, data[i])
    return result

base = 0
for i in range(len(histogram(A))):
    base = base + histogram(A)[i][1]*(histogram(A)[i][1]-1)//2

for i in range(N):
    j=0
    while A[i] != histogram(A)[j][0]:
        j = j+1
    print(base - histogram(A)[j][1]*(histogram(A)[j][1]-1)//2 + (histogram(A)[j][1]-2)*(histogram(A)[j][1]-1)//2)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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


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


for k in range(n):
    ans = 0
    temp_a = a[:]
    del temp_a[k]
    temp_count = collections.Counter(temp_a)
    for c in temp_count:
        ans += combinations_count(temp_count[c], 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
#


def main():
    n = int(input())
    a = [int(s) for s in input().split(" ")]
    counts = {}
    for i in a:
        if i in counts:
            counts[i] += 1
        else:
            counts[i] = 1
    results = {}
    for i in a:
        if i in results:
            print(results[i])
            continue
        count = 0
        for k, v in counts.items():
            if k == i:
                v -= 1
            if v > 1:
                count += (v * (v - 1) // 2)
        print(count)
        results[i] = count


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

def main():
    n = int(input())
    As = list(map(int, input().split(" ")))
    for k in range(n):
        if k == 0:
            As_tmp = As[1:]
        else:
            As_tmp = As[:k] + As[k+1:]
        a = list(itertools.combinations(As_tmp, 2))
        #b = list(map(set, a))
        #b = list(map(len, b))
        #b = map(lambda x: x-2, b)
        b = map(lambda x: len(set(x))-2, a)
        ans = - sum(b)
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(n):
  ans = 0
  # 全てのキーに対して処理を実施
  for k in c.keys():
    if (k == a[i]):
      ans += int((c[k]-1)*(c[k]-2)/2)
    else:
      ans += int(c[k]*(c[k]-1)/2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
from functools import lru_cache
 
@lru_cache(maxsize = 64)
def C(n, r):
    if n-r < 0:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
#d = {}
d = [-1]*1000000
sm = 0
for k,v in c.most_common():

    d[k] = v
    sm += C(v,2)

for a in A:
    print (sm - C(d[a],2) + C(d[a]-1,2) )

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


f = math.factorial


def comb(n, r, memo):
    if (n, r) in memo:
        return memo[n, r]
    elif n < r:
        memo[n, r] = 0
        return 0
    else:
        res = memo[n, r] = f(n) // f(r) // f(n - r)
        return res


N = int(input())
A = list(map(int, input().split()))
memo = {}
counts = collections.Counter(A)
for a in A:
    res = 0
    for val, count in counts.items():
        if val == a:
            count -= 1
        res += comb(count, 2, memo)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

def comb(n, r):
    if n <= 1:
        return 0
    return perm(n, r)//math.factorial(r)

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

for k in range(N):
    #print(k)
    A_ = A[:]
    A_.pop(k)
    A_counter = Counter(A_)
    #print(A_counter)
    ans = 0
    for v in A_counter.values():
        #print(v)
        ans += comb(v,2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
*a,= map(int, input().split())
def without(a,k):
  return a[0:k]+a[k+1:]

def sames(x):
  x=sorted(x)
  count=0
  for i in range(len(x)):
    ch=x[i]
    for j in range(i+1,len(x)):
      if x[j]!=ch:
        break
      else:
        count+=1
  return count

for k in range(N):
  print(sames(without(a,k)))

Submission status: Time Limit Exceeded

Solution code:

from collections import deque

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

from itertools import combinations
def comb(L,num):
    # 組み合わせ、コンビネーション
    #print(L)
    return list(combinations(L,num))


for i in range(n):
    b_L = a_L[0:i] + a_L[i+1:]
    ans = 0
    for k,v in comb(b_L,2):
        if k == v:
            ans += 1
    print(ans)
    #print(comb(b_L,2))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

dic = defaultdict(int)
ans1 = defaultdict(int)
ans2 = defaultdict(int)

def calc_patern1(n, ans, dic):
    for k, v in dic.items():
        ans[k] = v * (v - 1) / 2


def calc_patern2(n, ans, dic):
    for k, v in dic.items():
        ans[k] = (v - 1) * (v - 2) / 2

for i in range(n):
    dic[a[i]] += 1
for v in dic.values():
    calc_patern1(v, ans1, dic)
    calc_patern2(v - 1, ans2, dic)
for i in range(n):
    ans = 0
    for k, v in dic.items():
        #print(ans)
        if k == a[i]:
            ans += ans2[k]
        else:
            ans += ans1[k]
    print(int(ans))

Submission status: Time Limit Exceeded

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

B = []
for i in range(0,N-1,1):
  a = 0
  for j in range(i+1,N,1):
    if A[i] == A[j]:
      a += 1
  B.append(a)

C = []
for i in range(N):
  b = 0
  for j in range(N):
    if A[i] == A[j]:
      b += 1
  C.append(b-1)
  
for i in range(N):
  print(sum(B)-C[i])

Submission status: Time Limit Exceeded

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

def calc_comb(lst_A):

    elem_A = list(set(lst_A))
    sum_comb = 0

    for A in elem_A:
        numA  = lst_A.count(A)
        combA = (numA * (numA-1)) // 2
        sum_comb += combA

    return sum_comb



def main(N, lst_A):

    sum_comb_all = calc_comb(lst_A)
    lst_sum_comb = []

    for A in lst_A:
        numA = lst_A.count(A)
        prev_combA = (numA * (numA-1)) // 2
        new_combA  = ((numA-1) * (numA-2)) // 2

        sum_comb = sum_comb_all - prev_combA + new_combA
        lst_sum_comb.append(sum_comb)

    return lst_sum_comb

if __name__ == "__main__":

    lst_answer = main(N, lst_A)
    for answer in lst_answer:
        print(answer)

Submission status: Time Limit Exceeded

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

from itertools import combinations

for k in range(N):
  S = A[:k]+A[k+1:]
  ans = 0
  for c in combinations(S,2):
    if c[0]==c[1]:
      ans +=1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


N = int(input())
count1 = [0] * (N + 1)
count2 = [0] * (N + 1)
A = list(map(int, input().split()))

B = list(set(A))

for b in B:
    num_of_b = A.count(b)
    if num_of_b > 2:
        comb = factorial(
            num_of_b) // (factorial(num_of_b - 2) * factorial(2))
        comb_minus1 = factorial(
            num_of_b - 1) // (factorial(num_of_b - 1 - 2) * factorial(2))
        count1[b] = comb
        count2[b] = comb_minus1
    elif num_of_b == 2:
        count1[b] = 1


for a in A:
    ans = 0
    ans += sum(count1) - count1[a]
    ans += count2[a]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = [int(_) for _ in input().split()]
setnum = set(numlist)
dicnum = {}

for i in setnum:
    dicnum[i] = numlist.count(i)
    
for j in range(n):
    ans = 0
    dicpos = numlist[j]
    for k in setnum:
        if k == dicpos:
            cal = dicnum[k] - 1
        else:
            cal = dicnum[k]
        if cal >= 2:
            ans += cal*(cal-1)//2
    print(ans)

Submission status: Time Limit Exceeded

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

m = max(a)
ans = [0 for _ in range(m)]
azk = [0 for _ in range(m)]

for i in range(m):
    ans[i] = a.count(i + 1)
    azk[i] = round(ans[i] * (ans[i] - 1) / 2)
dic = {}

for i in range(n):
    ppt = 0
    if a[i] in dic:
        print(dic[a[i]])
        continue
    for j in range(m):
        if (j + 1) == a[i]:
            ppt += round((ans[j] - 1)*(ans[j] - 2) / 2)
        else:
            ppt += azk[j]
    dic[a[i]] = ppt
    print(ppt)

Submission status: Time Limit Exceeded

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

for k in range(N):
    a = nums[k]
    nums[k] = 0
    ans = 0
    for i in range(N):
        b = nums.count(i)
        ans += int((b*(b-1)/2))
    print(ans)
    nums[k] = a

Submission status: Time Limit Exceeded

Solution code:
def main():
    # initialize
    _ = int(input())
    nums = [int(n) for n in input().split(' ')]

    hash_table = {}
    for n in nums:
        if n in hash_table.keys():
            hash_table[n] += 1
        else:
            hash_table[n] = 1

    ans_cash = {}
    for n in nums:
        if n in ans_cash.keys():
            ans = ans_cash[n]
        else:
            ans = 0
            for k, v in hash_table.items():
                if k == n:
                    v -= 1
                ans += v * (v - 1) / 2 if v > 1 else 0
            ans_cash[n] = ans
        print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for k in range(N):
    ans = 0
    test = A_list[:k] + A_list[(k+1):]
    for i in set(test):
        n = test.count(i)
        if n >= 2:
            ans += n*(n-1)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

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

L = collections.Counter(A)

for i in range(N):
    ans = 0
    for a in AS:
        if A[i] == a:
            ans += comb(L[a]-1)
        else:
            ans += comb(L[a])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
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

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}

    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    for i in range(N):
        val = 0
        for k in X.keys():
            if A[i] == k:
                val += Y[k]
            else:
                val += X[k]
        print(val)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

if len(a) == len(set(a)):
    for i in range(n):
        print(0)
else:
    for i in range(n):
        ans = 0
        temp = a[:i] + a[i + 1:]
        for k in [temp.count(x) for x in set(temp)]:
            if k != 1:
                ans += k * (k - 1) // 2

        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


def nc2(n):
    if n <= 1:
        return 0
    return math.factorial(n)//(2*math.factorial(n-2))

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

cnt = collections.Counter(a)
ans = {}
for key1 in cnt.keys():
    ans[key1] = sum(nc2(value) if key1 != key2 else nc2(value-1) for key2, value in cnt.items())
for v in a:
    print(ans[v])

Submission status: Time Limit Exceeded

Solution code:
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

def main():

    N = int(input())

    a_l = [0] * N

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

    for i in nums:
        a_l[i-1] += 1

    for i in nums:
        a_l[i-1] -= 1
        total = 0
        for j in a_l:
            if j > 1:
                total += cmb(j,2)
        print(total)
        a_l[i-1] += 1




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

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

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    hist = [0] * 2*pow(10, 5)
    for a in A:
        hist[a-1] += 1

    all = 0
    for h in hist:
        if h >= 2:
            all += combinations_count(h, 2)

    for a in A:
        h = hist[a-1]
        if h > 2:
            diff = combinations_count(h, 2) - combinations_count(h-1, 2)
            print(all - diff)
        elif h == 2:
            print(all - 1)
        else:
            print(all)
    
if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

n = int(input())
a = sys.stdin.readline().split()
c = collections.Counter(a)
for i in a:
    ans = 0
    for j in c.keys():
        if i == j:
            ans += (c[j]-1)*(c[j]-2)//2
        else:
            ans += c[j]*(c[j]-1)//2
    print(ans)

Submission status: Time Limit Exceeded

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

total = 0
for i in range(N):
    for j in range(i+1,N):
        if A[i] == A[j]:
            total += 1


for i in range(N):
    tmp = 0
    to = total
    for j in range(N):
        if i != j and A[i] == A[j]:
            tmp += 1
    
    print(to-tmp)

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)
cnt = 0
for i in A:
    for x, y in c.items():
        if i == x and y >= 3:
            cnt += combinations_count(y - 1, 2)
        elif i != x and y >= 2:
            cnt += combinations_count(y, 2)
    print(cnt)
    cnt = 0

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

n = int(input())
al = [int(x) for x in input().split()]
cs_orig = dict(collections.Counter(al))
copy_copy = copy.copy

aset = set(al)
patterns_d = {}

for i in range(1, n+1):
    cs = copy_copy(cs_orig)
    patterns = 0
    if i in aset:
        cs[i] -= 1
        for count in cs.values():
            if count > 1:
                patterns += int(count * (count - 1) / 2)

        patterns_d[i] = patterns

for a in al:
    print(patterns_d[a])

Submission status: Time Limit Exceeded

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

import collections
c = collections.Counter(A)

for x in A:
    _ans = 0
    d = c.copy()
    d[x] -= 1
    _ans = sum([j*(j-1)//2 for j in d.values()])
    print(_ans)

Submission status: Time Limit Exceeded

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

for k, val in enumerate(A):
    count = 0
    for i, x in enumerate(A):
        for j, y in enumerate(A):
            if i == k or j == k or i == j:
                continue
            if x == y:
                count += 1
    print(count // 2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
# print(a_list)
a_set = set(a_list)
length_dict = {}
for elm in a_set:
    cur_list = [ind for ind in range(n) if a_list[ind] == elm]
    length_dict[elm] = len(cur_list)
num_dict0 = {}
num_dict1 = {}
for elm in a_set:
    num_dict0[elm] = int(length_dict[elm]*(length_dict[elm]-1)/2)
    num_dict1[elm] = int((length_dict[elm]-1)*(length_dict[elm]-2)/2)

# print(num_dict0, num_dict1)
for ind in range(n):
    cur_elm = a_list[ind]
    output = 0
    for elm in a_set:
        if elm == cur_elm:
            output += num_dict1[elm]
        else:
            output += num_dict0[elm]
    print(output)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int, input().split()))
set_of_numbers = set(P)
for k in range(N):
    # 取り除く数字はP[k]
    # hoge = P[:k] + P[k+1:]  
    # set_of_numbers = set(hoge)
    result = 0
    for num in set_of_numbers:
        if (num == P[k]):
            same_num = P.count(num) - 1
        else:
            same_num = P.count(num)
        result += same_num*(same_num-1)
    print(int(result/2))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=(input().split())
for i in range(n):
    x=0
    for j in range(n):
        for k in range(n):
            if j>k and a[j]==a[k] and j!=i and k!=i:
                x=x+1
    print(x)

Submission status: Time Limit Exceeded

Solution code:
import math

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

for k in range(N):
  ans = 0
  B = A.copy()
  del B[k] #ここでBはk番目が削除されたものとなっている。
  C = list(set(B)) 
  M = len(C)
  for i in range(M):
    if B.count(C[i]) >=2:
      t = B.count(C[i])
      ans += math.factorial(t)/((math.factorial(t-2))*(math.factorial(2)))
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
a_list = [int(i) for i in input().split()]

a_dict = {}
for a in a_list:
    if a in a_dict.keys():
        a_dict[a] += 1
    else:
        a_dict[a] = 1

    
answer_dict = {}
for a in a_dict.keys():
    temp_dict = copy.deepcopy(a_dict)
    temp_dict[a] -= 1
    answer = 0
    for key, value in temp_dict.items():
        if value >= 2:
            answer += value*(value-1)//2
    answer_dict[a] = answer
    
for a in a_list:
    print(answer_dict[a])

Submission status: Time Limit Exceeded

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

sum_ = 0
for i in range(N):
  for j in range(N):
    if (i == j) | (i<=j):
      continue
    if As[i] == As[j]:
      sum_ += 1
      
for i in range(N):
  print(sum_-As.count(As[i])+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = {}
C = {}
for a in A:
    c = A.count(a)
    C[a] = c
    B[a] = c * (c - 1) / 2

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

ans={}
for i in set(a):
    temp=cnt[i]-1
    ans[i]=0
    for x,y in cnt.items():
        if x!=i and y>=2:
            ans[i]+=(y-1)*y//2
        elif x==i and temp>=2:
            ans[i]+=(temp-1)*temp//2

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

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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


N = int(input())
L = input().split(" ")

c = collections.Counter(L)

d = {}
tmp = 0 

for i in c:
    d[i] = comb(c[i], 2)
    tmp += d[i]
total = tmp

memo = {}
for i in L:
    if i not in memo.keys():
        ans = total - d[i] + comb(c[i]-1, 2)
        memo[i] = ans
        print(ans)
    else:
        print(memo[i])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for x in s:
    d[x] = l.count(x)

d2=dict()

sum = 0
for key in d.keys():
    a = d[key]
    sum += (a*(a - 1)) // 2


for x in l:
    print(sum - d[x] +1)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

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

for i in range(N):
  temp_A = copy.deepcopy(A)
  temp_A.pop(i)
  c = collections.Counter(temp_A)
  temp_c = list(c.values())
  ans = 0
  for i in range(len(temp_c)):
    ans += int(temp_c[i]*(temp_c[i]-1)/2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools

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

for i in range(N):
    tmp = copy.deepcopy(A)
    tmp.pop(i)

    res = 0
    #t = list(set(tmp))
    for j in list(set(tmp)):
        l = tmp.count(j)
        res += len(list(itertools.combinations(list(range(l)),2)))

    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

count_dict = defaultdict(int)
for cal in A:
  count_dict[cal] += 1
    
    
"""
それぞれの要素について、1個減った時とそのままの場合での組み合わせ数を事前に計算しておく
"""
from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))
  
# list[0]=そのまま、list[1]=1個減った場合
count_result = defaultdict(list)
for k,v in count_dict.items():
  if v > 2:
    count_result[k] = [combinations_count(v,2), combinations_count(v-1,2)]
  elif v == 2:
    count_result[k] = [1,0]    
  else:
    count_result[k] = [0,0]


"""
対象の数字を指定した場合の答え、を事前に計算しておく
"""
ans_dict = defaultdict(int)
for k,v in count_dict.items():
  ans_dict[k] = count_result[k][1]
  for _k,_v in count_result.items():
    if _k != k:
      ans_dict[k] += _v[0]

    
"""
前から順に表示していく
"""
for _ in A:
  # 対象の数字
  ans = ans_dict[_]
  print(ans)