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 collections

def nCr(n, r):
    """
    Calculate the number of combination (nCr = nPr/r!).
    The parameters need to meet the condition of n >= r >= 0.
    It returns 1 if r == 0, which means there is one pattern
    to choice 0 items out of the number of n.
    """

    # 10C7 = 10C3
    r = min(r, n-r)

    # Calculate the numerator.
    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i

    # Calculate the denominator. Should use math.factorial?
    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i

    return numerator // denominator

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

for i in range(n):
    li_a = li.copy()
    del li_a[i]
    c = collections.Counter(li_a)
    s = list(set(li_a))
    ans = 0
    # print(c)
    # print(c[s[1]])
    # print(s)
    
    for j in range(len(s)):
        if c[s[j]] <= 1:
            continue
        ans += nCr(c[s[j]], 2)
        
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def ncr(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())
a = list(map(int, input().split()))

numList = [0] * (n+1)
numSet = set()

resultList = [0] * (n+1)
resultSet = set()

for i in range(len(a)):
    numList[a[i]] += 1
    if(numList[a[i]]==2):
        numSet.add(a[i])

for i in range(len(a)):
    if(a[i] in resultSet):
        print(resultList[a[i]])
        continue
    s = 0
    for j in numSet:
        if(j == a[i]):
            if(numList[j]>2):
                s += ncr(numList[j]-1,2)
        else:
            s += ncr(numList[j],2)
    print(s)
    resultList[a[i]] = s
    resultSet.add(a[i])

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 = {}
total_num = 0
for elm in a_set:
    cur_list = [ind for ind in range(n) if a_list[ind] == elm]
    length_dict[elm] = len(cur_list)
    total_num += int(length_dict[elm]*(length_dict[elm]-1)/2)
difference = {}
for elm in a_set:
    difference[elm] = int(length_dict[elm]*(length_dict[elm]-1)/2) - int((length_dict[elm]-1)*(length_dict[elm]-2)/2)


# print(num_dict0, num_dict1)
for ind in range(n):
    elm = a_list[ind]
    print(total_num-difference[elm])

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = input().split()

for i in range(n):
  c = 0
  b = copy.copy(a)
  b.pop(i)
  for j in range(n):
    d = b.count(str(j)) 
    c += int(d*(d-1)/2)
  print(c)

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools

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

for i in range(n):
    count = 0
    input_copy = copy.copy(input_line)
    input_copy.pop(i)
    input_set = set(input_copy)
    banned = []
    for j in input_set:
        banned.append((j, j))
    for j in banned:
        count += list(itertools.combinations(input_copy, 2)).count(j)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
def aC(a):
  if a == 0:
    return 0
  return a*(a-1)/2

N = input()
N = int(N)
B = input().split(" ")
A = []
for a in B:
  A.append(int(a))
  
for i in range(0,N):
  pre_A = A[:]
  pre_A.pop(i)
  count_list = []
  for i in range(1,N+1):
    count_list.append(pre_A.count(i))

  ans = 0
  for i in count_list:
    ans = ans + aC(i)

  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
c = c.most_common()
d = {}
for i in range(len(c)):
    d2 = 0
    for j in range(len(c)):
        if i==j:
            d2+=c2c(c[j][1]-1)
        else:
            d2+=c2c(c[j][1])
    d[c[i][0]]=d2

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

Submission status: Time Limit Exceeded

Solution code:
import math, collections
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()))
al = collections.Counter(a)
res = [0]*(n+1)
for i in al:
    if al[i]==1:
        continue
    res[i] += combinations_count(al[i], 2)
sum_ = sum(res)
for i in a:
    if al[i]==1:
        print(sum_)
        continue
    if al[i]==2:
        print(sum_-res[i])
        continue
    print(sum_-res[i]+ combinations_count(al[i]-1, 2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int,input().split()))
dic = {}
for i in arr:
    if i not in dic:
        dic[i]=1
    else:
        dic[i]+=1
for i in arr:
    ans=0
    for j in dic.keys():
        if i==j:
            t=dic[j]-1
            
        else:t = dic[j]
        ans+=(t*(t-1))//2
    print(ans)

Submission status: Time Limit Exceeded

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

a = list(set(A))
a_n = []
c = []
new = []
if len(a) != N:
  for i in range(len(a)):
    x = A.count(a[i])
    a_n.append(x)
    c.append(x*(x-1)//2)
    new.append((x-1)*(x-2)//2)

  ans = sum(c)
  for i in range(N):
    n = a.index(A[i])
    print(ans-c[n]+new[n])
else:
  for _ in range(N):
    print(0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
alist = list(map(int, input().split()))
keys = list(set(alist))
values = [[]]* len(keys)
for i in range(len(keys)):
  values[i] = alist.count(keys[i])
d = dict(zip(keys, values))
tot = sum(s*(s-1)//2 for s in values)
for j in range(N):
  print(tot-d[alist[j]]+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
setA = list(set(A))
setN = len(setA)
countA = [A.count(a) for a in setA]
ansA = [countA[i]*(countA[i]-1)//2 for i in range(setN)]
sumA = sum(ansA)

for i in range(N):
  index = setA.index(A[i])
  ans = sumA - ansA[index] + (countA[index] -1) * (countA[index] -2) //2
  print(ans)

Submission status: Time Limit Exceeded

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

def solve(N: int, A: "List[int]"):
    nums = [0] * N
    for i in A:
        nums[int(i) - 1] += 1
    
    #print(nums)
    for k in range(0, N):
        cp = copy.copy(nums)
        cp[int(A[k]) - 1] -= 1
        #print("aaa" + str(k))
        #print(nums)
        sum = 0
        for num in cp:
            sum += num * ( num - 1 ) // 2
        print(sum)
    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 collections
import math
 
def combinations_count(n, r):
    return 0 if n < r else math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

combi = collections.Counter(A)
#print(combi)

for i in range(N):
  ans = 0
  
  # i番目の要素をcombiからdecrement
  combi[A[i]] -= 1
  
  #print(combi)
  
  for v in combi.values():
    #print(v)
    ans += combinations_count(v, 2)
  
  print(ans)

  # i番目の要素をcombiからincrement
  combi[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 

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

for i in range(n):
    _a = np.delete(a, i)
    unique = np.unique(_a)
    for j in range(len(unique)):
        k = np.count_nonzero(_a == unique[j])
        if j == 0:
            if k >= 2:
                total = k * (k - 1) / 2 
            else:
                total = 0
        else:
            if k >= 2:
                total += k * (k - 1) / 2 
    print(int(total))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from math import factorial

mem = {}


def num_combinations(_n, r):
    if _n < r:
        return 0
    elif _n in mem.keys():
        return mem[_n]
    mem[_n] = int(factorial(_n) / (factorial(r) * factorial(_n - r)))
    return mem[_n]


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)

    for n in xs:
        freq[n] += 1

    res = 0

    for k in range(len(xs)):
        res = 0
        for num in freq.keys():
            res += num_combinations(freq[num] - (1 if xs[k] == num else 0), 2)
        print(res)

Submission status: Time Limit Exceeded

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

for i in range(n):
    ans = 0
    At = A[:]
    At.pop(i)

    while At != []:
        lb = len(At)
        s = At[0]

        while s in At:
            At.remove(s)

        la = len(At)
        c = lb - la
        ans = int(ans + (c * (c - 1) / 2))

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = {}
C = {}
for a in A:
    if a in C:
        continue
    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] + 1))

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
total = 0
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 v in c.values():
    if v >= 2:
        total += cmb(v, 2)
# print(total)

for ai in a:
    if c[ai] >= 2:
        # print('Y')
        ans = total - cmb(c[ai], 2)
        if c[ai] - 1 >= 2:
            ans += cmb(c[ai] - 1, 2)
    else:
        ans = total
    print(ans)
    # ans = 0
    # temp = copy.copy(c)
    # temp[ai] = temp[ai] - 1

    # for v in temp.values():
    #     # print(v)
    #     if v >= 2:
    #         ans += cmb(v, 2)
    # print(ans)
# temp = copy.copy(c)
# temp[1] = temp[1] - 1
# print(c)
# print(temp)

Submission status: Time Limit Exceeded

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

N = int(input())
A = input().split()

ans = {}
for x in set(A):
    a = A[:]
    a.remove(x)
    t = 0
    for y in set(a):
        m = a.count(y)
        t += m * (m - 1) // 2
    ans[x] = t

for x in A:
    print(ans[x])

Submission status: Time Limit Exceeded

Solution code:
import math

def Combination(n, r):
	if n >= r:
		return math.factorial(n) // (r * math.factorial(n - r))
	else:
		return 0

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

cnt = [0] * N
for i in A:
	cnt[i - 1] += 1

cmb = [0] * N
for i in range(N):
	cmb[i] = Combination(cnt[i], 2)

asum = sum(cmb)
for i in A:
	print(asum - cmb[i - 1] + Combination(cnt[i - 1] - 1, 2))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
num = list(input().split(' '))
dic = collections.Counter(num)


for i in num:
  result = 0
  for j in dic:
    if i == j:
      result += (dic[j] - 1) * (dic[j] - 2) / 2
    else:
      result += dic[j] * (dic[j] - 1) / 2
  print(int(result))

Submission status: Time Limit Exceeded

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

def main():
    n = int(input())
    lis = list(map(int,input().split()))

    ans = {}
    for i in lis:
        if i in ans:
            ans[i] += 1
        else:
            ans[i] = 1

    for j in range(n):
        ans_t = copy.copy(ans)
        ans_t[lis[j]] -= 1
        su = 0
        for k in ans_t.values():
            su += (k - 1) * k // 2
        print(su)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

def solve(k, arr):
    tmp_arr = []
    tmp_arr.extend(arr)
    deleted_num = tmp_arr.pop(k)
    set_arr = []
    set_arr = list(set(tmp_arr))
    ans = 0
    for i in set_arr:
        ans += combinations_count(tmp_arr.count(i), 2)
    return ans

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

if __name__ == "__main__":
    n = int(input())
    arr = [int(s) for s in list(input().split())]
    for k in range(n):
        print(solve(k, arr))

Submission status: Time Limit Exceeded

Solution code:
import collections as cc
n=int(input())
l=list(map(int,input().split()))
ff=cc.Counter(l)
ff=dict(ff)
ans=[]
for i in range(n):
    f=ff.copy()
    f[l[i]]=max(0,f[l[i]]-1)
    t=0
    for i in f:
        x=f[i]
        t+=(x*(x-1))//2
    ans.append(t)
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,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:
N=int(input())
A=list(map(int, input().split()))

for k in range(0,N):
    X = A[:k] + A[k+1:]
    Y = set(X)
    c = 0
    for i in Y:
        sc = X.count(i)
        c += sc * (sc-1)//2
    print(c)

Submission status: Time Limit Exceeded

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

Lists=[]
moto=0
for i in range(n):
    Lists.append(a.count(i+1))
    if ((Lists[i]>1)):
        moto+=Lists[i]*(Lists[i]-1)//2
#print(a)
#print(Lists)
#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()))
B=[0]*N
for i in range(N):
    B[i]=A.count(i+1)
for i in range(N):
    B[A[i]-1]=B[A[i]-1]-1
    C = 0
    for j in range(N):

        if B[j]>1:
            C=C+B[j]*(B[j]-1)/2
    print(int(C))
    B[A[i] - 1] = B[A[i] - 1] +1

Submission status: Time Limit Exceeded

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

num_count = {}
count_comb = {}
num_ans = {}

def comb2(n):
    if n not in count_comb:
        count_comb[n] = (n * (n - 1)) // 2
    return count_comb[n]

def ans(ex_num):
    if ex_num in num_ans:
        return num_ans[ex_num]

    result = 0
    for n, c in num_count.items():
        if n == ex_num:
            result += comb2(c - 1)
        else:
            result += comb2(c)
    num_ans[ex_num] = result
    return result


for a in As:
    if a not in num_count:
        num_count[a] = 0
    num_count[a] += 1

for a in As:
    print(ans(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
m = 0
c = {}
for i in list(set(a)):
  c[i] = a.count(i)
  m += c[i]*(c[i]-1)//2
for i in a:
  print(m - c[i] + 1)

Submission status: Time Limit Exceeded

Solution code:
import copy

import math


def main():
    import collections

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

    for k in range(N):
        tmp_A = ignore(A, k)
        tmp_A = collections.Counter(tmp_A)
        su = 0
        for c in tmp_A:
            pair = tmp_A[c]
            if pair >= 2:
                su += combinations_count(pair, 2)
        print(su)


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


def ignore(A, i):
    x = copy.copy(A)
    # del x[i]
    # return x
    x[i] = 0
    return x


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def cycle(x):
    result = 0
    for y in range(int(x), 0, -1):
        result += y - 1
    return result


def main():
    input()
    a_list = list(map(int, input().split()))
    dict_a_list = dict()
    dict_result = dict()
    for a in a_list:
        if a in dict_a_list:
            dict_a_list[a] += 1
            continue
        dict_a_list[a] = 1
    for a in a_list:
        result = 0
        if a in dict_result:
            print(dict_result[a])
            continue
        dict_a_list[a] -= 1
        for v in dict_a_list.values():
            result += cycle(v)
        print(result)
        dict_result[a] = result
        dict_a_list[a] += 1


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(N):
  ans = 0
  A_ = copy.copy(A)
  A_.pop(i)
  A_set = set(A_)
  for j in A_set:
    ans += A_.count(j) * (A_.count(j) - 1) / 2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
B = Counter(A)
for i in range(N):
    B[A[i]] -= 1
    ans = 0
    for n in B.values():
        ans += n*(n-1)//2
    print(ans)
    B[A[i]] += 1

Submission status: Time Limit Exceeded

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

ans=0
ll=[0]*(n+1)
for i in range(n+1):
    a=l.count(i)
    ll[i]=a
    ans+=(int(a*(a-1)/2))

for j in l:
    b=int(ll[j]*(ll[j]-1)/2)
    c=int((ll[j]-1)*(ll[j]-2)/2)
    print(ans-b+c)

Submission status: Time Limit Exceeded

Solution code:
n, *D  = [list(map(int, s.split())) for s in open(0)]
D = D[0]
dic = dict()
for d1 in D:
 
    if d1 not in dic:
        dic[d1] = 1
    else:
        dic[d1] += 1

for d2 in D:
    comb = 0
    dic[d2] -= 1
    for i in dic.values():
        if i >= 2:
            comb += i * (i - 1) / 2
    dic[d2] += 1
    print(int(comb))

Submission status: Time Limit Exceeded

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

NMAX=2*10**5

tmp = [0]*(NMAX+1)
for n in c:
    tmp[n] = (c[n] * (c[n] - 1) // 2)

for a in A:
    sum = 0
    for i in c:
        if a ==i:
            n =c[i]-1
            sum += n * (n - 1) // 2
        else:
            sum += tmp[i]

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1

    return None


N = int(input())

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

array = sorted(A)
type_of_num = list()
quantity_of_num = list()

count = 1
for i in range(N-1):
    if array[i] == array[i+1]:
        count += 1
    else:
        type_of_num.append(array[i])
        quantity_of_num.append(count)

        count = 1
type_of_num.append(array[i])
quantity_of_num.append(count)

for k in range(1, N+1):
    ans = 0
    
    type_index = binary_search(type_of_num, A[k-1])
    for i in range(len(quantity_of_num)):
        if i != type_index:
            quantity = quantity_of_num[i]
        else:
            quantity = quantity_of_num[i] - 1
        
        ans += quantity*(quantity - 1) // 2
    
    print(ans)

Submission status: Time Limit Exceeded

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

def count(n, r):
    return comb(n, r, exact=True)
"""
def count(n, r):
    if (n < r):
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
"""
n = int(input())
a = list(map(int, input().split()))
ax = collections.Counter(a)

master = {}
min1 = {}

for i in list(ax.keys()):
    master[i] = count(ax[i], 2)
    min1[i] = count(ax[i]-1, 2)

for i in a:
    ans = 0
    for k in list(ax.keys()):
        ans += master[k] if(i!=k) else min1[k]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
from sys import stdin



def get_result(data):
    N = data[0][0]
    A = data[1]
    uniq_cnt = collections.Counter(A)
    
    for i in range(N):
        ans = 0
        for key, value in uniq_cnt.items():
            if key == A[i]:
                value -= 1
            ans += int(value * (value - 1) / 2)
        print(ans)


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    get_result(data)

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):
      continue
    if As[i] == As[j]:
      sum_ += 1

dic = {}
for i in list(set(As)):
  dic[i] = As.count(i)-1
      
for i in range(N):
  print(sum_-dic[As[i]])

Submission status: Time Limit Exceeded

Solution code:
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict

def main():
    N = int(input())
    As = [int(x) for x in input().split()]

    d = defaultdict(int)

    for i in range(N):
        d[As[i]] += 1
    
    for i in range(N):
        ans = 0
        for k in d.keys():
            j = d[k]
            if (k == As[i]):
                j -= 1
            if j > 1:
                ans += j*(j-1)//2
        print(ans)

# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc159","d",main)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
def comb(n, r):
    # print(n, r)
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

# All combination
total = 0
for (key, val) in c.items():
    # print('\n', key, val)
    if val > 1:
        total += comb(val, 2)

# print('all:', total)

for i in range(n):
    num = a[i]
    org = c[num]
    # print(org)
    # total - orgC2 + (org-1)C2
    if org > 2:
        ans = total - comb(org, 2) + comb(org-1, 2)
    elif org == 1 or org == 0:
        ans = total
    else:
        ans = total - comb(org, 2)
    print(ans)


'''
5
1 1 2 1 2
'''

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
answer = [-1] * N
p = []
for i in range(N):
  count[i]  = A.count(i + 1)

for i in range(N):
  if answer[A[i] - 1] != -1:
    p.append(answer[A[i] - 1])
  else:
    count[A[i] - 1] -= 1
    s = 0
    for j in range(N):
      s += count[j] * (count[j] - 1) // 2
    p.append(s)
    count[A[i] - 1] += 1

for i in p:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
cnt = 0
for i in range(1,N+1):
    a = A.count(i)
    if a >= 2:
        b = a*(a-1)/2
        cnt += b

for i in range(N):
    print(int(cnt-A.count(A[i])+1))

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)

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))

    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

        # (count_u) C 2
        ans += comb(count_u,2)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
import copy


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

for ai in a:
    cc = copy.deepcopy(c)
    cc[ai] -= 1
    print(sum(comb(v, 2) for v in cc.values()))

Submission status: Time Limit Exceeded

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

balls = Counter(A)
for i in range(N):
    # 除外
    balls[A[i]] -= 1

    # 2個以上のペア
    s = 0
    for k, v in balls.most_common():        
        if v >= 2:
            s += v * (v - 1) // 2
    print(s)
    balls[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    a1 = a
    a2 = set(a1)
    ans = 0
    for j in a2:
        if j == a1[i]:
            cnt = a1.count(j) - 1
        else:
            cnt = a1.count(j)
        ans += cnt * (cnt - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

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

# 数値Aの頻度
ndic = {}
for i in A:
    if i in ndic:
        ndic[i] += 1
    else:
        ndic[i] = 1

# 2つの同じ数値Aの選び方
cdic = {}
for j in ndic:
    cdic[j] = int((ndic[j] * (ndic[j] -1)) / 2)


for i in A:
    cmb = 0
    for j in cdic:
        if i == j and cdic[j] > 0:
            cmb += cdic[j] - (ndic[j] - 1)
        else:
            cmb += cdic[j]
    print(cmb)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


nc2_memo = {}
def nc2(n):
    if n <= 1:
        return 0
    if n in nc2_memo:
        return nc2_memo[n]
    r = math.factorial(n)//(2*math.factorial(n-2))
    nc2_memo[n] = r
    return r


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:
import sys

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

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

set_a = set(A)

num_dic = {}
comb_dic = {}
comb_dic_ex = {}

for i in set_a:
    num_dic[i] = A.count(i)
    if num_dic[i] == 1:
        comb_dic[i] = 0
        comb_dic_ex[i] = 0
    else:
        comb_dic[i] = cmb(num_dic[i])
        comb_dic_ex[i] = cmb(num_dic[i] - 1)
    
comb_sum = sum(comb_dic.values())
 
for j in range(N):
    n = A[j]
    ans = int(comb_dic_ex[n] + comb_sum - comb_dic[n])
    print(ans)

Submission status: Time Limit Exceeded

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

import math
import collections

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

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))

    for i in range(n):
        li = a_li.copy()
        li.pop(i)
        c = collections.Counter(li)
        ans = 0
        for k, v in c.items():
            if v > 1:
                ans += combinations_count(v, 2)

        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys, math

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
A = read_int()

d = defaultdict(lambda: int())

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

def solve():
    all = 0
    Ma = 0
    for a in A:
        d[a] += 1
        Ma = max(Ma, a)
    M = [0 for _ in range(Ma + 1)]
    for k, v in d.items():
        for kj, vj in d.items():
            if k == kj:
                M[kj] += combinations_count(v - 1, 2)
            else:
                M[kj] += combinations_count(v, 2)
        # all += combinations_count(v, 2)
    # for a in A:
    #     res = all - combinations_count(d[a], 2) + combinations_count(d[a] - 1, 2)
    # print(M)
    for a in A:
        print(M[a])
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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


dic = {}
for t in A:
    dic[t] = A.count(t)

ans_dict = {}
for i in list(dic.keys()):
    ans = 0
    for key in list(dic.keys()):
        val = dic.get(key)
        if i == key:
            val -= 1
        ans += val * (val - 1) // 2
    ans_dict[i] = ans

# print(ans_dict)

for i in range(N):
    print(ans_dict.get(A[i]))

Submission status: Time Limit Exceeded

Solution code:
#Fib
from functools import lru_cache
counter = 0
@lru_cache(maxsize=1024)
def fib(n):
    global counter
    counter += 1
    if n in [0, 1]:
        return 1
    return fib(n - 1) + fib(n - 2)

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

for k in range(0,N):
    X = A[:k] + A[k+1:]
    Y = set(X)
    c = 0
    for i in Y:
        sc = X.count(i)
        c += sc * (sc-1) //2
    print(c)

Submission status: Time Limit Exceeded

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

target = list(set(ao))
for i in range(n):
    a = ao[0:i] + ao[i+1:n] 
    count = 0
    for t in target:
        tmp = a.count(t)
        count += sum([i for i in range(tmp-1, 0, -1)])
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

for i in range(n):
    ans = 0
    c[a[i]] -= 1
    for v in c.values():
        ans += v*(v-1)//2
    c[a[i]] += 1
    print(ans)

Submission status: Time Limit Exceeded

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

#print(N,A)

d = {}
for a in A:
  d[a] = d.get(a, 0) + 1

for i in range(N):
  tmp_d = d.copy()
  tmp_d[A[i]] -= 1
  sum = 0
  for v in tmp_d.values():
    sum += int(v*(v-1)/2)
  print(sum)