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:
n=int(input())
a=list(map(int,raw_input().split()))
dict={}
for i in range(n):
    if dict.get(a[i],False):
        dict[a[i]]+=1
    else:
        dict[a[i]]=1
dict2={}
for i in range(n):
    sum=0
    if a[i] in dict2:
        sum=dict2[a[i]]
    else:
        for j in dict:
            if j==a[i]:
                if dict[j]>2:
                    sum+=((dict[j]-1)*(dict[j]-2))//2
            else:
                if dict[j]>=2:
                    sum += ((dict[j])*(dict[j]-1))//2
        dict2[a[i]]=sum
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
for x in range(N):
  ans = 0
  b = A.pop(x)
  #print(A)
  c = collections.Counter(A)
  #print(c)
  v = [k for j,k in c.items()]
  #print(v)
  for i in v:
    ans += int(i*(i-1)/2)
  print(ans)
  A.insert(x,b)

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.popleft()
  for x in set(l):
    ans += l.count(x)*(l.count(x)-1)/2
  print(int(ans))
  ans = 0
  l.append(a)

Submission status: Time Limit Exceeded

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

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom
    
N = int(input())
List = [int(i) for i in input().split(" ")]
C =Counter(List)
D = {}
E = {}

for i in C:
    if C[i]>=2:
        D[i] = combinations_count(C[i], 2)
    else:
        D[i]=0
        
for i in C:
    if C[i]>=3:
        E[i] = combinations_count(C[i]-1, 2)
    else:
        E[i]=0

  
for j in range(N):
    l = List[j]
    
    d = D[l]
    D[l] = E[l]
    dl = list(D.values())
    print(sum(dl))
    D[l]=d

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
IND = list(set(A))
# CNT = [A.count(x) for x in IND]
# print(IND)
# print(CNT)
CONB = [A.count(x)*(A.count(x)-1)//2 for x in IND]
# print(CONB)
SUM = sum(CONB)
CALC = [-1]*(N+1)
for i in A:
    if CALC[i] != -1:
        print(CALC[i])
    else:
        # n = CNT[IND.index(i)]
        # ans = SUM - CONB[IND.index(i)] + (n-1)*(n-2)//2
        ans = SUM - CONB[IND.index(i)] + (A.count(i)-1)*(A.count(i)-2)//2
        CALC[i] = ans
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
*a,= map(int, input().split())

count,res={},{}

for i in range(N):
  count[a[i]]=0
  res[a[i]]=0
  
for i in range(N):
  count[a[i]]+=1

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

def without(a,k):
  cc = 0
  for key in count.keys():
    kc = count[key]
    if key==a[k]:
       kc-=1
    if kc>1:
       cc+=ncn(kc)
  res[a[k]]=cc      
  return cc

def result(n):
  if res[n]>0:
    return res[n]
  return 0

for k in range(N):
  if result(a[k])>0:
    print(result(a[k]))
  else:
    print(without(a,k))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

s = sum([i*(i-1)//2 for i in collections.Counter(a).values()])

for j in a:
    print(s - a.count(j)+1)

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.copy(A)
    #del tmp[i]
    tmp.pop(i)

    res = 0
    for j in set(tmp):
        l = tmp.count(j)
        res += len(list(itertools.combinations([0]*l,2)))

    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb

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

length = len(data)
for i in range(length):
    tmp = data[:i] + data[i+1:]
    counter = Counter(tmp)
    r = [comb(n, 2, exact=True) for n in counter.values()]
    print(int(sum(r)))

Submission status: Time Limit Exceeded

Solution code:
def comb(n, r):
    bunshi = 1
    for i in range(n-r+1,n+1):
        bunshi = bunshi*i
    bunbo = 1
    for i in range(1,r+1):
        bunbo = bunbo * i 
    return bunshi//bunbo

def pascal(n):
    c = [[0 for i in range(n+1)] for j in range(n+1)]
    c[0][0] = 1
    for i in range(1, n+1):
        for j in range(n+1):
            if j == 0 or j == i:
                c[i][j] = 1
                continue
            c[i][j] = c[i-1][j-1] +c[i-1][j]
    return(c)

def nC2(n):
    c = [0 for _ in range(n)]
    c[0] = 1
    for i in range(1, n):
        c[i] = i+1 +c[i-1]
    return(c)

N = int(input())
A = list(map(int, input().split()))
# c = pascal(N)
# nc2 = nC2(N)
duplication = {}
ans = 0
for x in A:
    if x in duplication:
        continue
    count = A.count(x)
    if count > 1:
        duplication[x] = count
        # ans += comb(count,2)
        # ans += nc2[count-2]
        for i in range(count):
            ans += i
for x in A:
    if x not in duplication:
        print(ans)
    else:
        # print(ans -comb(duplication[x],2) +comb(duplication[x]-1,2))
        print(ans -duplication[x]+1)
        # print(ans -c[duplication[x]][2] +c[duplication[x]-1][2])

Submission status: Time Limit Exceeded

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

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



n = int(input())
nums = list(map(int, input().split()))
d = Counter(nums)
# for num in nums:
#     if not d.get(num):
#         d[num] = 1
#     else:
#         d[num] += 1

combination_d = {}

for num in nums:
    counter = 0
    if combination_d.get(num):
        print(combination_d[num])
        continue

    nums.count(num)

    for key in d.keys():
        if key == num:
            if d[key]-1 >= 2:
                counter += cmb(d[key]-1, 2)
        else:
            if d[key] >= 2:
                counter += cmb(d[key], 2)
    
    combination_d[num] = counter
    print(counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
import math
import collections
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def get_result(key, c):
    ans = 0
    for d in c.items():
        if d[0] == key:
            value = d[1]-1
        else:
            value = d[1]
        if value > 1:
            ans += combinations_count(value, 2)
    return int(ans)
ans = {}
c = collections.Counter(a)
for k in range(n):
    key = a[k]
    if key in ans:
        print(ans[key])
    else:
        ans[key] = get_result(key, c)
        print(ans[key])

Submission status: Time Limit Exceeded

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

dic = {}
for num in lis:
    if num in dic:
        dic[num] = dic[num] + 1
    else:
        dic[num] = 1

answer_memo_dic = {}
for key, val in dic.items():
    ans_1 = int(val * (val -1) / 2)
    ans_2 = int((val - 1) * (val - 2) / 2)
    answer_memo_dic[key] = {"ans_1": ans_1, "ans_2": ans_2}

answer_list = []
answer_dic = {}
def answer():
    for n in lis:
        if n in answer_dic:
            answer_list.append(answer_dic[n])
        else:
            answer = 0
            for key, val in answer_memo_dic.items():
                if n == key:
                    answer += val["ans_2"]
                else:
                    answer += val["ans_1"]
            answer_list.append(answer)
            answer_dic[n] = answer

answer()
for a in answer_list:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter()
    count.update(A)
    sum_ = 0
    for i in count.values():
        sum_ += i*(i-1)/2

    for i in range(len(A)):
        remain = A[:i] + A[i+1:]
        took_count = count[A[i]]
        sub = took_count - 1
        print(int(sum_ - sub))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

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

ans_dict = {}
ans_keys = []
value_dict = {1: 0}
value_keys = [1]
for i in range(n):
    num = a_list[i]
    if num in ans_keys:
        print(ans_dict[num])
    else:
        buff = a_list[:i]
        buff.extend(a_list[i+1:])
        c = Counter(buff)

        tmp_ans = 0
        for value in c.values():
            if value in value_keys:
                tmp_ans += value_dict[value]
            else:
                tmp = int((value * (value - 1))/2)
                tmp_ans += tmp
                value_dict[value] = tmp
                value_keys.append(value)
        ans_dict[num] = tmp_ans
        ans_keys.append(num)
        print(tmp_ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

def sameNumberCombi(targetList):
    count = 0
    l = list(itertools.combinations(targetList, 2))
    for i in l:
        if i[0] == i[1]:
            count += 1
    return count

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

for i in range(N):
    a = A[:]
    a.pop(i)
    print(sameNumberCombi(a))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

aval, acount = [], []

for i in range(n):
    try:
        ind = aval.index(i)
    except:
        a = ao[0:i] + ao[i+1:n]
        count = 0
        for j in list(set(a)):
            c = a.count(j)
            count += sum([k for k in range(1, c)])
        print(count)
        aval.append(i)
        acount.append(count)
    else:
        print(acount[ind])

Submission status: Time Limit Exceeded

Solution code:

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

counters = {}
used_list = []
for i in numbers:
    if i in used_list:
        continue
    counters[i] = numbers.count(i)
    used_list.append(i)
cevap_list = {}

for i in range(n):
    sonuc = 0
    try:
        print(cevap_list[numbers[i]])
    except:
        for sayi in counters:
            count = counters[sayi]
            if numbers[i] == sayi:
                count -= 1
            sonuc += count*(count-1)/2
        cevap_list[numbers[i]] = int(sonuc)
        print(int(sonuc))

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

p_dict = {}
def permutation_count(x):
    ret = 0
    if 2 <= x:
        if(not x in p_dict.keys()):
            ret = math.factorial(x) // (2*(math.factorial(x-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
#import numpy as np

N = int(input())
arr = list(map(int, input().split(" ")))
#val, count = np.unique(arr, return_counts=True)
count = collections.Counter(arr)
conv_before = [int(x * (x-1) /2) for x in count.values()]

#conv_before = (count * (count - 1) / 2).astype(int)
conv_before_sum = sum(conv_before)

for i in range(len(arr)):
    k_val = arr[i]
    n_count_k = count[k_val] - 1
    n_conv = int(n_count_k * (n_count_k -1) /2)
    conv = conv_before_sum - conv_before[list(count.keys()).index(k_val)] + n_conv

    #3conv = (count * (count - 1) / 2).astype(int)
    print(conv)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S = int(input())
As=[int(x) for x in input().split()]
total = sum(As.count(x) for x in As)-S
for x in As:
    n=As.count(x)-1
    print(int((total-n*2)/2))

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

#数え上げ
str_count = {}
for i in Y:
    str_count[i] = A.count(i)
for k in range(0,N):
    key2 = A[k]
    if A.count(key2) == 1:
        print(0)
        continue
    if key2 in memo:
        print(memo[key2])
        continue
    c = 0
    for i in Y:
        if i == A[k]:
            sc = str_count[i] - 1
        else:
            sc = str_count[i]
        if sc == 1:
            continue
        #カウント対象
        c += nC2(sc)
    memo[key2] = c
    print(c)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import itertools

import sys

def input():
    return sys.stdin.readline()[:-1]
  
def get_comb(n, r):
  return len(list(itertools.combinations(range(n), r)))
 
def each_check(ls):
  nums = list(set(ls))
  total = 0
  for i in nums:
    count = ls.count(i)
    total += get_comb(count, 2)
  return int(total)
 
N = input()
L = list(map(int, input().split()))
 
for i in L:
  target = L.pop(0)
  print(each_check(L))
  L.append(target)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    D = {}

    for k in range(N):
        if A[k] in D.keys():
            print(D[A[k]])
        else:
            tmp = A.copy()
            del tmp[k]
            ans = 0
            for v in tmp:
                ans += tmp.count(v) - 1
            print(ans // 2)
            D[A[k]] = ans // 2


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for k in range(N):
    ans = [i == j for i, j in itertools.combinations(As[:k] + As[k+1:], 2)]
    print(sum(ans))

del N
del As
del ans

Submission status: Time Limit Exceeded

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

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

m = int(input())
a = list(map(int,input().split()))[:m]

memo = {}

for i in a:
    res = 0
    t = a[:]
    t.remove(i)
    c = collections.Counter(t)
    for v in c.values():
        if v >= 2:
            if not memo.get(v):
                memo[v] = combinations_count(v, 2)

            res += memo[v]
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import itertools

if __name__ == "__main__":
    N = int(input())
    A = input().split()
    for x in range(N):
        tmp = [y for idx, y in enumerate(A) if idx != x]
        result = list(filter(lambda t: t[0] == t[1], itertools.combinations(tmp, 2)))
        print(len(result))

Submission status: Time Limit Exceeded

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

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

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

for i in A:
  count = 0
  for x,y in ans:
    if i == x:
      y -= 1
    count += f(y - 1)  
  print(count)

Submission status: Time Limit Exceeded

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

d = {}

for i in range(N):
    if a[i] not in d:
        d[a[i]] = 1
    else:
        d[a[i]] += 1

for i in range(N):
    d[a[i]] -= 1

    su = 0
    for j in d.values():
      su += int(j * (j - 1) / 2)
    print(su)

    d[a[i]] += 1

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

tmp_keys = []
for key in dic:
    if dic[key] < 2:
        tmp_keys.append(key)

for k in tmp_keys:
    del dic[k]

tmp_len = len(dic)

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

    if tmp_len == 0:
        print(0)
        continue

    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:
import copy
import collections

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

#Process
for i in range(n):
    l_o = copy.copy(l_a)
    del l_o[i]
    c = collections.Counter(l_o)
    #print(c)
    #print(c.values())
    Ans = 0
    for i in range(len(list(c.values()))):
        for j in range(list(c.values())[i] - 1):
            Ans += (j + 1)

    print(Ans)

Submission status: Time Limit Exceeded

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

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

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

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

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

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    cl[i-1] = nc - int(nc*(m-2)/m)

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

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
N=int(input())
list=[]
s=input().split()
for i in range(N):
    list.append(int(s[i]))

for y in range(N):
    list1=copy.copy(list)
    list1.pop(y)
    c = collections.Counter(list1)
    a=0
    for h in c.values():
        a+=(h*(h-1)/2)
    print(int(a))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
a = []
for i in s:
    if not i in a:
        ans[i] = s.count(i)*(s.count(i)-1)//2
        a.append(i)
#print(ans)
answer = 0
for i in ans.values():
    answer += i
#print(answer)
for i in range(n):
    print(answer-ans[s[i]]+((ans[s[i]]*2//s.count(s[i]))*(s.count(s[i])-2)//2))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

# N = 5
# A = [1, 1, 2, 1, 2]

c = collections.Counter(A)
d = {}
for i in c:
    # val = uni_vals[0]
    ans = 0
    for j in c:
        n = c[j]
        if j == i:
            n -= 1
        ans += (n*(n-1))/2
    d[i] = ans

for i in range(N):
    print(int(d[A[i]]))

Submission status: Time Limit Exceeded

Solution code:
number=[]
result=[]
N=int(input())
A=list(map(int,input().split()))
for i in range(N+1):
  number.append(0)
  result.append(0)
i=0
for i in A:
    if i in range(N+1):
      number[i]+=1
    else:
      number[i]=1
i=0
for i in range(N):
  cnt=0
  kie=A.pop(i)
  number[kie]-=1
  if result[kie]==0:
    for j in range(N-1):
      if number[j]>=2:
        cnt+=int((number[j]*(number[j]-1))/2)
    result[kie]=cnt
  A.insert(i,kie)
  number[kie]+=1
i=0
for i in range(N):
  print(result[A[i]])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import collections
n = int(input())
an= list(map(int, input().split()))

for k in range(0,n):
    an2 = an[:]
    del an2[k]
    c = collections.Counter(an2)
    datas = c.most_common()
    ans = 0
    for data in datas:
        ans += data[1] * (data[1]-1)/2
    print(int(ans))
    an2.clear()

Submission status: Time Limit Exceeded

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


for i in A:
  ans = 0
  for j in c.keys():
    if c[j] >1:
      k = c[j]
      if j == i:
        if c[j]>2:
          k -= 1
          ans += math.factorial(k) // (math.factorial(k-2) * math.factorial(2))
      else:
        ans += math.factorial(k) // (math.factorial(k-2) * math.factorial(2))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = (int(input()))
items = list(map(int,input().split()))
for i in range(len(items)):    
    rest = items[:i] + items[i+1:]
    rest_unique = set(rest)
    out = 0
    for j in rest_unique:
        cnt = sum([1 for x in rest if x == j])
        if cnt >= 2:
            out = out + math.factorial(cnt)/math.factorial(cnt-2)/2
    print(int(out))

Submission status: Time Limit Exceeded

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

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

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

for i in range(n):
    b = copy.copy(a)
    del b[i]
    c = collections.Counter(b)
    d = c.most_common()
    values, counts = zip(*c.most_common())
    e = (counts)
    for j in range(len(e)):
        f = e[j]
        ans = ans + (f*(f - 1)/2)
    print(int(ans))
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
m = 0
for k in range(n):
    if m == a[k]:
        print(ans)
        continue
    else:
        m = a[k]
    ak = a[:k] + a[k + 1 :]
    akset = list(set(ak))
    ans = 0
    for i in range(len(akset)):
        tn = ak.count(akset[i]) - 1
        if tn == -1:
            continue
        ans += tn * (tn + 1) // 2
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
k = list(map(int, input().split()))
d = {}
dd = {}
for i in range(N):
    if k[i] in d:
        d[k[i]] += 1
    else:
        d[k[i]] = 1
for key in d:
    dd[key] = d[key]*(d[key]-1)/2
for i in range(N):
    res = 0
    for key in d:
        if d[key]>1:
            if key == k[i]:
                res += (d[key]-1)*(d[key]-2)/2
            else:
                res += dd[key]
    print(int(res))

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 combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

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

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

    for i in ball:
        ans = 0
        # def_ans_temp = deepcopy(def_ans)
        for key, val in def_ans.items():
            if key == i and c[i] >= 3:
                ans += val * (c[i] - 2) / (c[i])
            elif key == i and c[i] < 3:
                continue
            else:
                ans += val
        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:
from math import factorial
import collections

memo={1:0,2:1}
def ajaja(n):
  if n in memo:
    return memo[n]
  if n >=2:
    memo[n]=((n-1)**2+(n-1))/2
  else:
    memo[n]=0
  return memo[n]
  
N=int(input())
A=list(map(int,input().split()))
B=set(A)
C=collections.Counter(A)
ba=[]
for k in B:
  if C[k] <= 1:
    ba.append(k)
for kk in ba:
  B.remove(kk)
for i in range(N):
  C[A[i]]-=1
  ans=0
  for j in B:
    ans+=ajaja(C[j])
  print(int(ans))  
  C[A[i]]+=1

Submission status: Time Limit Exceeded

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

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

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())
data = 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

for k in range(n):
  data_n = data.copy()
  data_n.pop(k)
  cnt = 0
  for i in set(data_n):
    if data_n.count(i) > 1:
      cnt += cmb(data_n.count(i), 2)
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(num) for num in input().split(" ")]

total_comb = 0
ball_num_count = {}
for i in range(1,n+1):
    i_count = a_list.count(i)
    total_comb += int(i_count*(i_count-1)/2)
    ball_num_count[i] = i_count-1
for k in range(n):
    except_num = a_list[k]
    print(total_comb - ball_num_count[except_num])

Submission status: Time Limit Exceeded

Solution code:
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()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if c in comb:
            pans+=comb[c]
        else:
            pans+=combinations_count(c,2)
    print(pans)