Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

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

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

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
from 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 += str_count2[i] - str_count[i] + 1
        else:
            c += str_count2[i]
    memo[key] = c
    print(c)

Submission status: Time Limit Exceeded

Solution code:
def count(x,length):
    num=0
    for i in range(length-1):
        for j in range(i+1,length-1):
            if x[i]==x[j]:
                num+=1
    return num
  
x=int(input())
a=list(map(int,input().split()))

num=0
for i in range(x):
    delete=a[0]
    del a[0]
    print(count(a,x))
    a.append(delete)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
n = int(input())

a = [0] * n
a = input().split()
a = [int(a[i]) for i in range(n)]

total_list = []
hoge = []
for i in a:
    sum = 0
    for j in range(a.count(i) - 1):
        sum += j + 1
    total_list.append(sum)
    hoge.append(a.count(i))

for i in range(n):
    a_val = a[i]
    sum = 0
    tmp_list = []
    for j in range(n):
        if i == j:
            pass
        else:
            if (a[j] in tmp_list):
                pass
            else:
                if a[i] == a[j]:
                    sum += total_list[j] - hoge[j] + 1
                else:
                    sum += total_list[j]
                tmp_list.append(a[j])
    print(sum)

Submission status: Time Limit Exceeded

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

from collections import Counter
for i in range(0, N):
  x = 0
  l = L[i]
  M = L
  M.pop(i)
  c = Counter(M)
  for a, b in c.items():
    if b >= 2:
      x += (b*(b-1))/2
  print(int(x))
  M.insert(i, l)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

# kごとに、
# 異なる数がいくつあるかを調べる
# (2以上要素が存在する)異なる数ごとに、
# (要素数)C2を解く。

np_A = np.array(A)

# 辞書作成
dic = {}

for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)
    # print("tmp_k", tmp_k)   # k以上
    # print(np.unique(np_A))

    # 同じ組み合わせのケースを排除する。
    # (num, 要素数)の組み合わせを辞書かで作る
    # →単純にnCrなら(n, r)で保存する

    k_unique = np.unique(tmp_k)
    ans = 0

    for u in k_unique:
        count_u = np.count_nonzero(tmp_k==u)
        if count_u < 2:
            continue
        key = "{0}".format(count_u) + "2"   # key
        if key in dic.keys():
            ans += dic[key]
            continue

        # (count_u) C 2
        tmp = int(count_u*(count_u-1)/2)
        ans += tmp
        # 値の更新
        dic[key] = tmp
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
"""
"""
from math import factorial
from collections import defaultdict


def int_as_array(num): return list(map(int, [y for y in str(num)]))


def array_as_int(arr): return int(''.join(map(str, arr)))


def read_int(): return int(input())


def read_array(): return list(map(int, input().split(' ')))


def array_to_string(arr, sep=' '): return sep.join(map(str, arr))


def matrix_to_string(arr, sep=' '): return '[\n' + '\n'.join(
    [sep.join(map(str, row)) for row in arr]) + '\n]'


cache = {}


def fac(x):
    if cache.get(x) is not None:
        return cache[x]
    ans = factorial(x)
    cache[x] = ans
    return ans


def choose_combination(n, r):
    if r > n:
        return 0
    return int((fac(n) / fac((n - r))) * (1 / fac(r)))


def solve(n, array):
    counts = defaultdict(int)
    for x in array:
        counts[x] += 1
    for k_i, k in enumerate(array):
        ways = 0
        for x, count in counts.items():
            adjusted_count = counts[x]
            if x == k:
                adjusted_count -= 1
            ways += choose_combination(adjusted_count, 2)
        print(ways)


N = read_int()
array = read_array()
solve(N, array)

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):
    buf = li[k]
    li[k], ans = -1, 0
    counter = Counter(li)
    for i in counter.most_common():
        cnt = i[1]
        ans += cnt * (cnt-1)
    print(ans//2)
    li[k] = buf

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import decimal
import heapq
import collections
import itertools
from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())
d = getInputListInt()


def combinations_count(n, r):
    if n <= 1:
        return 0
    else:
        if n - r < r: r = n - r

        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

dic = {}
dic_ans = {}
dic_comb = {}

for key in d:
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1



for i in range(n):
    ans = 0
    tmp_key = d[i]


    if tmp_key in dic_ans:
        print(dic_ans[tmp_key])
    else:
        for k in dic:
            if k == tmp_key:
                tmp_dic_idx = dic[k]-1
                if tmp_dic_idx in dic_comb:
                    ans += dic_comb[tmp_dic_idx]
                else:
                    tmp_calc = combinations_count(tmp_dic_idx, 2)
                    ans += tmp_calc
                    dic_comb[tmp_dic_idx] = tmp_calc
            else:
                tmp_dic_idx = dic[k]
                if tmp_dic_idx in dic_comb:
                    ans += dic_comb[tmp_dic_idx]
                else:
                    tmp_calc = combinations_count(tmp_dic_idx, 2)
                    ans += tmp_calc
                    dic_comb[tmp_dic_idx] = tmp_calc
        print(ans)
        dic_ans[tmp_key] = ans

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import collections
import copy


def solve(N: int, A: "List[int]"):
    C = collections.Counter(A)
    for k in range(len(A)):
        c = copy.copy(C)
        c[A[k]] -= 1
        values, counts = zip(*c.most_common())
        ans = 0
        for i in counts:
            ans += int(i * (i-1) / 2)
        print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
import copy
import collections

N = int(input())
A = list(map(int, input().split()))
  
c = collections.Counter(A)
c_keys, c_values, ans_list, temp_ans = list(c.keys()), list(c.values()), [], 0 
for i in range(len(c_keys)):
  temp_ans += c_values[i] * (c_values[i]-1)
temp_ans = temp_ans//2
for i in range(len(c_values)):
  ans_list.append(temp_ans - c_values[i] + 1)
for i in range(N):
  print(ans_list[c_keys.index(A[i])])

Submission status: Time Limit Exceeded

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

for i in range(n):   #k除外
    a = inp.copy()
    a.pop(i)
    l = [0] * n

    j = -1
    while(len(a) > 0):
        j += 1
        target = a[0]
        l[j] = 1
        a.pop(0)
        flag = 0
        num = -1
        for k in range(len(a)):
            if len(a) == 0:
                break
                
            if flag:
                flag = 0
            else :
                num += 1 

            if a[num] == target:
                l[j] += 1
                a.pop(num)
                flag = 1

    sum = 0
    max = 0
    for m in range(n - 1):
        if l[m] == 0:
            break
        sum += l[m] * (l[m]-1) / 2

        if sum > max :
            max = sum

    print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
import collections as cl
def main() :
    N = int(input())
    li = [s for s in input().split()]

    c = cl.Counter(li)
    exclude_dic = {}
    result_dic = {}
    for key in c :
        num = c[key]
        combi = ((num-1) * (num-2)) // 2
        if (combi > 0) :
            exclude_dic[key] = combi
        else :
            exclude_dic[key] = 0
        
        combi = (num * (num-1)) // 2
        if (combi > 0 ) :
            result_dic[key] = combi
        else :
            result_dic[key] = 0
    
    # print(result_dic)
    # print(exclude_dic)
    for key in li :
        res = exclude_dic[key]
        for item in result_dic :
            if (key == item) :
                continue
            # print("key",key, "item", item, result_dic[item])
            res += result_dic[item]
        
        print(res)
        



main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

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

unique_a = set(a)

cnt_all_conbi = 0
a_cnt_dict = {}

for i in unique_a:
    x = a.count(i)
    a_cnt_dict[i] = x
    cnt_all_conbi += (x*(x-1)) //2

for i in a:
    x = a_cnt_dict[i]
    print(cnt_all_conbi - (x-1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
checklist = list(set(A))
countdic = {}
nCrdic = {}

for i in checklist:
    countdic[i] = A.count(i)
for key,value in countdic.items():
    nCrdic[key] = (value*(value-1))//2 if value > 1 else 0
for i in A:
    if countdic[i] > 2:
        ans = sum(nCrdic.values()) - nCrdic[i] + ((countdic[i]-1)*(countdic[i]-2))//2
    else: ans = sum(nCrdic.values()) - nCrdic[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum=0
m=len(a)

def todo(i,b):
    c=list(set(b))
    sum=0
    for i in range(len(c)):
        if b.count(c[i])>1:
            k=b.count(c[i])
            sum+=k*(k-1)//2
    print(sum)

for i in range(m):
    d=a.copy()
    d.pop(i)
    todo(i,d)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

a = np.array(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:
N = int(input())
A = list(map(int, input().split()))
Z = []
for i in range(N):
    a = A.copy()
    answeri = 0
    del a[i]
    for k in range(0, len(a)):
        for j in range(k + 1, len(a)):
            if a[k] == 0:
                continue
            elif a[k] == a[j]:
                answeri += 1
    Z.append(answeri)
for x in Z:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
li = list(map(int, input().split()))
def calc(a):
    return int(a*(a-1)*0.5)
for k in range(n):
	dic = collections.Counter(li)
	dic[li[k]] -=1
	data2 = map(calc, list(dic.values()))
	print(sum(data2))

Submission status: Time Limit Exceeded

Solution code:
import collections
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(str,input().split()))
ans = []
cnt = 0
for i in range(n):
    tmp = a[:i] + a[i+1:]
    set_tmp = set(tmp)
    c = collections.Counter(tmp)
    cnt = 0
    for j in set_tmp:
        if int(c[j]) < 2:
            cnt += 0
        else:
            cnt += C(int(c[j]),2)
    ans.append(cnt)
    cnt = 0
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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 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] = val * (val - 1) / 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()

# def main():
#     n = I()
#     ball = LI()
#     ans = []

#     for i in range(n):
#         ans = 0
#         ball_temp = deepcopy(ball)
#         ball_temp.pop(i)
#         c = collections.Counter(ball_temp)
#         c_nums = c.values()

#         for p in c_nums:
#             if p >= 2:
#                 ans += combinations_count(p, 2)

#         print(ans)

# main()

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

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

for i in range(len(List)):
    print(List[i])

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import defaultdict
  from collections import Counter
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = Counter(a)
  e = defaultdict(int)
  
  func = lambda x: x*(x-1)//2 if x>=2 else 0
  
  
  for k in d.keys():
    tmp = d.copy()
    tmp[k] -= 1
    h = list(tmp.values())
    i = list(map(func, h))
    ans = sum(i)
    e[k] = ans
    
  p = [str(e[x])+'\n' for x in a]
  q = ''.join(p)
  print(q)
      
 
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(n):
  A=0
  latmp = la[:]
  del latmp[i]
  lai = [x for x in set(latmp) if latmp.count(x) > 1]
  for j in lai:
    m = latmp.count(j)
    A += int(m*(m-1)/2)
  print(A)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
k=np.array(list(Counter(a).keys()))
t=np.array(list(Counter(a).values()))
l=np.ones(len(t))
for i in range(n):
  s=np.copy(t)
  s[int(np.where(k==a[i])[0])]-=1
  print(int(sum(s*(s-l)//2)))

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:
    dic = dict(zip(sett, [lst.count(x) for x in sett]))
    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:
import collections
from operator import mul
from functools import reduce


def cmb(n,r):
    if n <= 1: 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


def main():
    _ = int(input())
    alist = list(map(int, input().split()))

    ans = {}
    for i in range(len(alist)):
        if alist[i] in ans:
            if i == len(alist)-1:
                print(ans[alist[i]], end='')
            else:
                print(ans[alist[i]])
            continue

        count = 0
        ttt = collections.Counter(alist).items()
        count = sum([cmb(v-1, 2) if k == alist[i] else cmb(v, 2) for k, v in ttt])

        if i == len(alist)-1:
            print(count, end='')
        else:
            print(count)
        ans[alist[i]] = count


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
import collections
from functools import reduce
from operator import mul

def combinations_count2(n, r):
    if n < 2:
        return 0
    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

c = collections.Counter(A)

dic = {}
for i in c:
    dic[i]= [combinations_count2(c[i], 2), combinations_count2(c[i] - 1, 2)]
dic_ans = {}
for ai in A:
    if ai in dic_ans:
         print(dic_ans[ai])
    ans = 0
    for i in dic:
        if i == ai:
            ans += dic[i][1]
        else:
            ans += dic[i][0]
    print(ans)

Submission status: Time Limit Exceeded

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

def aC(a):
  if a == 0 or a == 1:
    return 0
  return a*(a-1)/2
 
N = input()
N = int(N)
A = [int(e) for e in 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:
  ab = count_list[a-1]
  print(int(all-aC(ab)+aC(ab-1)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data = [int(x) for x in input().split()]
data_dict = {}

# ボールに書かれた整数を集計する
for x in data:
    if x in data_dict.keys():
        data_dict[x] = data_dict[x] + 1
    else:
        data_dict[x] = 1

#pdb.set_trace()

for k in range(n):
    data_dict_copy = data_dict.copy()
    # k番目のボールの個数を減らす
    data_dict_copy[data[k]] = data_dict_copy[data[k]] - 1

    answer = 0
    for x in data_dict_copy.values():
        answer += int(x*(x-1)/2)

    print(answer)

Submission status: Time Limit Exceeded

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

all = 0
ways =0
for k in range(n):
    delk = balls[0:k] + balls[k + 1::]
    all = 0
    ways= 0
    for i in range(1, n+1):
        a = delk.count(i)
        all += a
        ways += a*(a - 1) // 2
        if all>=n:
            break
    print(ways)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

n = int(input())

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

c = dict(collections.Counter(m))

def ans(a):
    if a<2:
        return 0
    else:
        return math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))

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

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
sys.setrecursionlimit(100000)
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)]

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

memo = [0 for _ in range(N)]
for a in A:
  memo[a-1] += 1

memo1 = [0 for _ in range(N)]
for n in range(N):
  memo1[n] = cmb(memo[n], 2)

total = sum(memo1)
for k in range(N):
  a = A[k]-1
  answer = 0
  answer += total
  answer -= memo1[a]
  answer += cmb(memo[a] - 1, 2)
  print(answer)

Submission status: Time Limit Exceeded

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

for i in range(N):
    B = sorted(A[0:i] + A[i+1:N])
    s = 0
    n = 0
    for j in range(n,len(B)):
        for k in range(j+1,len(B)):
            if B[j] == B[k]:
                s += 1
                n += 1
            else:
                n += 1
                break
    print(s)

Submission status: Time Limit Exceeded

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

z = []
for i in range(len(a3)):
     z.append(a.count(a3[i]))

q = 1
for i in z:
    q += i*(i-1)/2

for j in range(n):
    count = q
    count -= z[a3.index(a[j])]
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

N=int(input())
A=deque(map(int,input().split()))
setA=set(A)
number1={}#-1
number2={}
ans={}
if(len(setA)==1):
    answer= len(A) - 1
    q=(answer - 1) * answer //2
    for _ in range(len(A)):
        print(q)
    sys.exit()


if(len(setA)==len(A)):
    for _ in range(len(A)):
        print(0)
    sys.exit()


for i in setA:
    tmp=A.count(i) - 1
    souwa=(tmp - 1)*(tmp)//2
    number1[i]=souwa
    souwa=tmp*(tmp+1)//2
    number2[i]=souwa
    total=number1[i]
    total-=number2[i]
    ans[i]=total
t=sum(number2.values())

for a in A:
    print(ans[a]+t)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(N):
	Atmp = A.copy()
	Atmp.pop(i)
	l = collections.Counter(Atmp)

	l_v = l.most_common()
	res = 0
	for j in range(len(l_v)):	
		t = int(l_v[j][1])
		if t >= 2:
			res += t * (t - 1) // 2
	print(res)

Submission status: Time Limit Exceeded

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

kazuset=list(set(kazu))
atai=0
for i in kazuset:
    kosuu=kazu.count(i)
    atai=atai+kosuu*(kosuu-1)/2
    
for i in range(n):
    kotae=atai
    kosuu=kazu.count(kazu[i])
    if (kosuu >= 1):
        kotae=atai-kosuu*(kosuu-1)/2+(kosuu-1)*(kosuu-2)/2
    print(int(kotae))

Submission status: Time Limit Exceeded

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

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:
                if a[j]==a[k] and j!=i and k!=i:
                    x=x+1
    print(x)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

p_dict = {}
def permutation_count(x):
    ret = 0
    if x == 2:
        ret = 1
    elif 2 < x:
        if(not x in p_dict.keys()):
            ret = (x * (x-1)) // (2)
        else:
            ret = p_dict[x]

    return ret

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

unique_a = set(a)

cnt_all_conbi = 0
a_cnt_dict = {}

for i in unique_a:
    x = a.count(i)
    a_cnt_dict[i] = x
    cnt_all_conbi += permutation_count(x)

for i in a:
    x = a_cnt_dict[i]
    print(cnt_all_conbi - (x-1))

Submission status: Time Limit Exceeded

Solution code:

import collections
from math import factorial

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

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

for i in range(1, N + 1, 1):
    Except_Ak = A[:i-1] + A[i:]
    # print(Except_Ak)
    c = collections.Counter(Except_Ak)
    values, counts = zip(*c.most_common())
    counts_list = list(counts)
    elem_num_list = [combinations_count(e, 2) for e in counts_list if e > 1]
    # print(elem_num_list)
    sum_num = sum(elem_num_list)

    print(sum_num)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())
l = deque(input().split())
ans = 0
for k in range(N):
  a=l[k]
  l[k]=0
  for x in set(l):
    if l.count(x)>1:
      ans += l.count(x)*(l.count(x)-1)/2
  print(int(ans))
  ans = 0
  l[k]=a

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
 
N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = [x for x in a if cc[x] > 1]
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)
 

[print(int(al-cc[k]+1)) if k in dup else print(int(al)) for k in A]

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.buffer.readline

def main():
    n = int(input())
    lis = list(map(int,input().split()))
    ans = Counter(lis)
    for j in lis:
        su = 0
        for k, v in ans.items():
            if k == j:
                su += (v - 2) * (v - 1) // 2
            else:
                su += (v - 1) * v // 2
        print(su)

if __name__ == '__main__':
    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 = [int(e) for e in 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:
  ab = count_list[a-1]
  print(int(all-aC(ab)+aC(ab-1)))

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
    dct_A = {}

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

    return sum_comb, dct_A



def main(N, lst_A):

    sum_comb_all, dct_A = calc_comb(lst_A)
    lst_sum_comb = []

    for A in lst_A:
        numA = dct_A[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:
def cmb2(n):
	result = n * (n - 1) / 2

	return result

if __name__ == "__main__":

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

	A_set = set(A)

	ans_d = {}
	for a_s in A_set:
		#print(A.count(a))
		ans_d[str(a_s)] = A.count(a_s)



	for a in A:
		A_new = ans_d.copy()
		A_new[str(a)] -= 1
		ans = 0
		#print(A_new)
		#print("="*10)
		for a_n in A_new.values():
			if a_n <= 1:
				continue
			else:
				ans += cmb2(a_n)
		print(int(ans))
		#ans_d[str(a)] += 1

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
su=0
a = list(map(int, input().split())) 
da={}
for i in range(len(a)):
    if a[i] in da:
        da[a[i]]+=1
    else:
        da[a[i]]=1

key=da.keys()
for k in range(len(a)):
    da[a[k]]-=1
    for p in da.keys():
        su+=da[p]*(da[p]-1)/2
    print(int(su))
    su=0
    da[a[k]]+=1

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
a = list(map(int,input().split()))
count = 0
newa = a
dict = {}
def kazoeru(newa):
    conbi = 0
    for i in itertools.combinations(newa,2):
        if i[0] == i[1]:
            conbi += 1
    return conbi

for i in range(n):
    num = newa[count]
    if num in dict.keys():
        print(dict[num])
        count += 1
    else:
        del newa[count]
        ans = kazoeru(newa)
        dict[num] = ans
        print(ans)
        newa.insert(count,num)
        count += 1