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:
#200322D
N = int(input())
A = list(map(int,input().split()))
count = 0
for i in range(N):
    for j in range(N):
        if A[i] == A[j]:
            count += 1
All = (count - N) / 2
#print(int(All))

for i in range(N):
    count = 0
    for j in range(N):
        if A[i] == A[j]:
            count += 1
    print(int(All - count + 1))

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())

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

P = [-1] * N

count_all = 0
k = list(itertools.combinations(A, 2))
for c in k: # 全体を求める
    if c[0] == c[1]:
        count_all += 1

i = 0
for c in A: # 部分を求める
    for co in A:
        if c == co:
            P[i] += 1
    i += 1

for c in P:
    print(count_all - c)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
for i in range(n):
    res=[]
    for j in sorted(set(l)):
        m=l.count(j) if(j!=l[i]) else l.count(j)-1
        m*=(m-1)
        m//=2
        res.append(m)
    print(sum(res))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

N = int(input())
line = input()
l = []
counts = {}
for n in line.split(' '):
  n = int(n)
  if n not in counts:
    counts[n] = 0
  counts[n] += 1
  l.append(n)

results = {}

for n in counts:
  results[n] = 0
  for m in counts:
    count = counts[m]
    if m == n:
      count -= 1
    if count > 1:
      results[n] += int(factorial(count)) // 2 // int(factorial(count - 2))

for n in l:
  print(results[n])

Submission status: Time Limit Exceeded

Solution code:
import math
def comb(n, r):
    if(n>=r):
        return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
    else:
        return 0

def judge(ls, num):
    ls.sort()
    # print(ls)
    juufku_list = []
    count = 1
    for i in range(num-1):
        if(ls[i] == ls[i+1]):
            count = count+1
            if(i == num-2):
                juufku_list.append(count)
        else:
            if(count>=2):
                juufku_list.append(count)
                count = 1
            else:
                count = 1
    comb_list = []
    # print(juufku_list)
    for i in range(len(juufku_list)):
        comb_list.append(comb(juufku_list[i],2))
    print(sum(comb_list))

num = int(input())
ball_list = list(map(int, input().split()))
copylist = ball_list.copy()
# print(copylist)
for i in range(len(ball_list)):
    del ball_list[i]
    judge(ball_list, len(ball_list))
    # print(copylist)
    ball_list = copylist.copy()

Submission status: Time Limit Exceeded

Solution code:
import math
def comb(n, r):
    if(n>=r):
        return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
    else:
        return 0

def judge(ls, num):
    ls.sort()
    # print(ls)
    juufku_list = []
    count = 1
    for i in range(num-1):
        if(ls[i] == ls[i+1]):
            count = count+1
            if(i == num-2):
                juufku_list.append(count)
        else:
            if(count>=2):
                juufku_list.append(count)
                count = 1
            else:
                count = 1
    comb_list = []
    # print(juufku_list)
    for i in range(len(juufku_list)):
        comb_list.append(comb(juufku_list[i],2))
    print(sum(comb_list))

num = int(input())
ball_list = list(map(int, input().split()))
copylist = ball_list.copy()
# print(copylist)
for i in range(len(ball_list)):
    del ball_list[i]
    judge(ball_list, len(ball_list))
    # print(copylist)
    ball_list = copylist.copy()

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 += i*(i-1)//2
  print(ans)
  A.insert(x,b)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
N=int(input())
a=sys.stdin.readline().split()
A=[int(b) for b in a]
numbers=[0]*N
for a in range(1,N+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
answers=[0]*N
for a in range(1,N+1):
    if numbers[a-1]>=2:
        b=numbers[a-1]*(numbers[a-1]-1)//2
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if kotae==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
N=int(input())
a=sys.stdin.readline().split()
A=[int(b) for b in a]
numbers=[0]*N
for a in range(1,N+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
answers=[0]*N
for a in range(1,N+1):
    if numbers[a-1]>=2:
        b=combinations_count(numbers[a-1],2)
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if kotae==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
N=int(input())
a=sys.stdin.readline().split()
A=[int(b) for b in a]
numbers=[0]*(2*10**5)
for a in range(1,N+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
answers=[0]*(N)
for a in range(1,N+1):
    if numbers[a-1]>=2:
        b=combinations_count(numbers[a-1],2)
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if kotae==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
N=int(input())
a=sys.stdin.readline().split()
A=[int(b) for b in a]
numbers=[0]*(2*10**5)
for a in range(1,N+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
answers=[0]*(2*10**5)
for a in range(1,N+1):
    if numbers[a-1]>=2:
        b=combinations_count(numbers[a-1],2)
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if kotae==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
N=int(input())
a=sys.stdin.readline().split()
A=[int(b) for b in a]
numbers=[0]*(2*10**5)
for a in range(1,N+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
answers=[0]*(2*10**5)
for a in range(1,N+1):
    if numbers[a-1]>=2:
        b=combinations_count(numbers[a-1],2)
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if answers[z-1]==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
a=input().split()
A=[int(b) for b in a]
numbers=[0]*(2*10**5)
for a in range(1,N+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
answers=[0]*(2*10**5)
for a in range(1,N+1):
    if numbers[a-1]>=2:
        b=combinations_count(numbers[a-1],2)
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if answers[z-1]==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
a=input().split()
A=[int(b) for b in a]
numbers=[0]*(2*10**5)
for a in range(1,2*10**5+1):
    kosuu=A.count(a)
    if kosuu<=1:
        pass
    else:
        numbers.insert(a-1,kosuu)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
answers=[0]*(2*10**5)
for a in range(1,2*10**5+1):
    if numbers[a-1]>=2:
        b=combinations_count(numbers[a-1],2)
        answers[a-1]+=b
kotae=sum(answers)
for a in range(N):
    z=A[a]
    if answers[z-1]==0:
        print(0)
    else:
        answer=kotae-numbers[z-1]+1
        print(answer)

Submission status: Time Limit Exceeded

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

import collections

def parse_input():
    N = int(input())
    A = list(map(int, input().split()))
    return N, A

if __name__ == "__main__":
    N, A = parse_input()
    counts = collections.defaultdict(int)
    for i in range(N):
        counts[A[i]] += 1

    for i in range(N):
        num = 0
        for a, c in counts.items():
            if a == A[i]:
                c -= 1
            num += max((c * (c - 1)) // 2, 0)
        print(num)

Submission status: Time Limit Exceeded

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

import collections

def parse_input():
    N = int(input())
    A = list(map(int, input().split()))
    return N, A

if __name__ == "__main__":
    N, A = parse_input()
    for i in range(N):
        counts = collections.defaultdict(int)
        for j in range(N):
            if i == j: continue
            counts[A[j]] += 1
        num = 0
        for c in counts.values():
            num += max((c * (c - 1)) // 2, 0)
        print(num)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

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


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

array = np.zeros(n+1)

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

sum = 0
for i in array:
    if i > 1:
        sum += combinations_count(i, 2)

for i in a:
    tmp = sum
    if array[i] > 2:
        tmp -= (combinations_count(array[i],2) - combinations_count(array[i]-1, 2))
    elif array[i] == 2:
        tmp -= combinations_count(array[i], 2)
    elif array[i] == 1:
        tmp = tmp
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
nums = [int(s) for s in input().split()]
counter = Counter(nums)

for outlier in nums:
    res = 0
    counter[outlier] -= 1

    for cnt in counter.values():
        res += (cnt - 1)*cnt // 2

    counter[outlier] += 1
    print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(str, input().split()))
from collections import Counter
for k in range(N):
    Alist = A.copy()
    del Alist[k]#k番目のボールを除いたリスト
    C = Counter(Alist)  #カウント辞書
    count = 0
    for i in range(1,N+1):
        count += (C['{}'.format(i)]*(C['{}'.format(i)]-1))/2
    print(int(count))

Submission status: Time Limit Exceeded

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

for i in range(n):
    pop = list_nums.pop(i)
    new_list = sorted(list_nums)
    num_old = None
    count =1
    sum = 0

    for _ in range(n-1):
        num = new_list.pop(0)
        if num == num_old:
            count += 1

        else:
            num_old = num
            sum += int( count * (count-1) / 2)
            count = 1


    sum += int( count * (count-1) / 2)

    print(sum)
    list_nums.insert(i, pop)

Submission status: Time Limit Exceeded

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

for i in range(n):
    pop = list_nums.pop(i)
    new_list = sorted(list_nums)
    num_old = None
    count = 1
    point = 0
    sum = 0

    for _ in range(n-1):
        num = new_list.pop(0)
        if num == num_old:
            count += 1

        else:
            num_old = num
            point = count * (count-1) / 2
            sum += int(point)
            count = 1
    point = count * (count-1) / 2
    sum += int(point)

    print(sum)
    list_nums.insert(i, pop)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
if N < 3 or 2 * (10**5) < N:
	print("error\nボールは3個以上200000以下")
	sys.exit()

A = list(map(int, input().split()))
k = 0
while not k == len(A):
	# k番目の要素を除外したリストを作成
	tmp = [v for i, v in enumerate(A) if not i == k]
	# 何回等しくなったかflagで確認
	flag = 0
	j = 0
	while not j == len(tmp) - 1:
		h = j + 1
		while not h == len(tmp):

			if (tmp[j] == tmp[h]) and (not j == h):
				flag += 1
			h += 1
		j += 1
	print(flag)
	k += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
k = [int(a) for a in input().split()]
k_set = set(k)
all = 0
count_dic = {}

for b in k_set:
    tmp = k.count(b)
    all += tmp * (tmp - 1) // 2
    count_dic[b] = tmp

for c in k:
    tmp = count_dic[c]
    print(all - (tmp - 1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
k = [int(a) for a in input().split()]
k_set = set(k)
all = 0

for b in k_set:
    tmp = k.count(b)
    all += tmp * (tmp - 1) // 2

for c in k:
    tmp = k.count(c)
    print(all - (tmp - 1))

Submission status: Time Limit Exceeded

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

list2 = [0]*(n+1)
list3 =[0]*(n+1)
list4 = []
for i in range(1,n+1):
    list2[i] = list.count(i)
    list3[i] = list2[i]*(list2[i]-1)/2

for k in range(n):
    print(int(sum(list3)-list2[list[k]]+1))

Submission status: Time Limit Exceeded

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

list2 = [0]*(n+1)
list3 =[0]*(n+1)
list4 = []
for i in range(1,n+1):
    list2[i] = list.count(i)
    list3[i] = list2[i]*(list2[i]-1)/2

for k in range(n):
    list4.append(int(sum(list3)-list2[list[k]]+1))

print('\n'.join(map(str,list4)))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split(" ")))
m = Counter(A)
def nck(n, k): 
    C = [[0 for x in range(k+1)] for x in range(n+1)] 
  
    # Calculate value of Binomial Coefficient in bottom up manner 
    for i in range(n+1): 
        for j in range(min(i, k)+1): 
            # Base Cases 
            if j == 0 or j == i: 
                C[i][j] = 1
  
            # Calculate value using previously stored values 
            else: 
                C[i][j] = C[i-1][j-1] + C[i-1][j] 
  
    return C

CHOOSE_MATRIX = nck(N, 2)
for i in range(len(A)):
    total =0
    m[A[i]] -= 1
    for key, value in m.items():
        # if key == A[i]:
        #     continue
        # else:
        total += CHOOSE_MATRIX[value][2]
    m[A[i]] += 1
    # print(uniquethings*(uniquethings)//2)
    print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split(" ")))
m = Counter(A)
def nck(n, k): 
    C = [[0 for x in range(k+1)] for x in range(n+1)] 
  
    # Calculate value of Binomial Coefficient in bottom up manner 
    for i in range(n+1): 
        for j in range(min(i, k)+1): 
            # Base Cases 
            if j == 0 or j == i: 
                C[i][j] = 1
  
            # Calculate value using previously stored values 
            else: 
                C[i][j] = C[i-1][j-1] + C[i-1][j] 
  
    return C[n][k] 

choose = {n : nck(n, 2) for n in range(N//2)}
for i in range(len(A)):
    total =0
    m[A[i]] -= 1
    for key, value in m.items():
        # if key == A[i]:
        #     continue
        # else:
        if value in choose :
                total += choose[value]
        elif len(A) - value +1 in choose:
            total += choose[len(A) - value +1]
        else:
            newchoose = nck(value, 2)
            choose[value] = newchoose 
            total += newchoose
    m[A[i]] += 1
    # print(uniquethings*(uniquethings)//2)
    print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

def nck(n, k): 
    C = [[0 for x in range(k+1)] for x in range(n+1)] 
  
    # Calculate value of Binomial Coefficient in bottom up manner 
    for i in range(n+1): 
        for j in range(min(i, k)+1): 
            # Base Cases 
            if j == 0 or j == i: 
                C[i][j] = 1
  
            # Calculate value using previously stored values 
            else: 
                C[i][j] = C[i-1][j-1] + C[i-1][j] 
  
    return C[n][k] 

for i in range(len(A)):
    total =0
    m = Counter(A[:i] + A[i+1:])
    for key, value in m.items():
        total += nck(value, 2)

    # print(uniquethings*(uniquethings)//2)
    print(total)

Submission status: Time Limit Exceeded

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

idx = B[0]
idx_count = 0
idx_set = []
for i in range(N):
  if idx != B[i]:
    tmp1 = int(idx_count*(idx_count-1)/2)		# 同一の整数を持つボールの組合せ数
    tmp2 = int((idx_count-1)*(idx_count-2)/2)	# 同一の整数を持つボールを1つ除いたときの組合せ数
    idx_set.append([idx, tmp1, tmp2])
    idx_count = 0
    idx = B[i]
  idx_count+=1
if idx_count != 0:
  tmp1 = int(idx_count*(idx_count-1)/2)			# 同一の整数を持つボールの組合せ数
  tmp2 = int((idx_count-1)*(idx_count-2)/2)		# 同一の整数を持つボールを1つ除いたときの組合せ数
  idx_set.append([idx, tmp1, tmp2])
#print(idx_set)
idx_sum_set = []
all_c = 0
for i in range(len(idx_set)):
  all_c += idx_set[i][1]
for i in range(len(idx_set)):
  tmp = all_c-idx_set[i][1]+idx_set[i][2]
  idx_sum_set.append([idx_set[i][0], tmp])    

for i in range(N):
  j = int(len(idx_sum_set)/2)
  while 1:
    if A[i] == idx_sum_set[j][0]:
      print(idx_sum_set[j][1])
      break
    elif A[i] > idx_sum_set[j][0]:
      j = j + int((len(idx_sum_set)-j)/2)
    elif A[i] < idx_sum_set[j][0]:
      j = int(j/2)

Submission status: Time Limit Exceeded

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

for i in range(N):
  count2=0
  for j in range(N):
    if(a[j]==a[i]):
      count2+=1
 # print(count)
  print((count-N)//2-count2+1)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = input().split()

# total count
dict = set(A)
total_count = 0
for val in dict:
  tmp = A.count(val)
  tmp = int(tmp*(tmp-1)/2)
  total_count += tmp

# substracted count
for k in range(N):
  sub_count = A.count(A[k]) - 1
  print('{}'.format(total_count - sub_count))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = input().split()
for k in range(N):
  A_ = A.copy()
  del A_[k]
  ans = 0
  while len(A_) > 0:
    val = A_[0]
    counts = A_.count(val)
    ans += int(counts * (counts - 1) / 2)
    while True:
      try:
        A_.remove(val)
      except:
        break

  print('{}'.format(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = input().split()
for k in range(N):
  A_ = A.copy()
  del A_[k]
  dic = set(A_)
  ans = 0
  for i in dic:
    tmp = A_.count(i)
    if tmp == 1:
      continue
    ans += int(tmp * (tmp - 1) / 2)

  print('{}'.format(ans))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = input().split()
for k in range(N):
  A_ = A.copy()
  del A_[k]
  dic = set(A_)
  ans = 0
  for i in dic:
    tmp = A_.count(i)
    ans += int(tmp * (tmp - 1) / 2)

  print('{}'.format(ans))

Submission status: Time Limit Exceeded

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

for i in range(N):
    c=0
    for j in range(N):
        if A[i] == A[j]:
            c+=1
    c-=1
    b.append(c)
    a+=c
a /= 2
for i in range(N):
    print(int(a)-b[i])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = input().split()
for k in range(N):
  A_ = A.copy()
  del A_[k]
  dict = {}
  for i in range(N-1):
    if A_[i] in dict.keys():
      dict[A_[i]] += 1
    else:
      dict[A_[i]] = 1

  ans = 0
  for key in dict.keys():
    ans += int(dict[key] * (dict[key] - 1) / 2)
  print('{}'.format(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ls = list(map(int, input().split()))
#print(ls)

for i in range(n):
    lsn  = ls[:]
    del lsn[i]
    lsn1 = list(set(lsn))
    cot = []
    for i in lsn1:
        a = lsn.count(i)
        cot.append(a)
    sm = []
    for i in cot:
        a = i*(i-1)/2
        sm.append(a)
    c = sum(sm)


    print(int(c))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 
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()))
P = list(set(A))
P.sort()
li = Counter(A).most_common()
S = sum(comb(li[i][1],2) for i in range(len(li)) if li[i][1]>=2)
for i in A:
    ans = S
    IDX = P.index(i)
    if li[IDX][1]>2:
        ans -= comb(li[IDX][1],2)-comb(li[IDX][1]-1,2)
    elif li[IDX][1]==2:
        ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 
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()))
P = list(set(A))
tmp = Counter(A)
tmp2 = tmp.most_common()
li = [list(tmp2[i]) for i in range(len(tmp2))]
P.sort()
li.sort()
S = sum(comb(li[i][1],2) for i in range(len(li)) if li[i][1]>=2)
for i in A:
    ans = S
    IDX = P.index(i)
    if li[IDX][1]>2:
        ans -= comb(li[IDX][1],2)-comb(li[IDX][1]-1,2)
    elif li[IDX][1]==2:
        ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
L=list(map(int,input().split()))
R = [0 for _ in range(N)]
sum=0
for i in range(N):
	R[ L[i]-1 ] += 1
#print(R)
for i in range(N):
	sum=0
	for j in range(N):
		comb=R[j]
		if L[i]-1==j:
			comb -= 1
		if comb>1:
			sum += math.factorial(comb) // (math.factorial(comb - 2) * 2)
	print(sum)

Submission status: Time Limit Exceeded

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

dic = {}

#count
for l in lis:
  if l in dic: dic[l] += 1
  else: dic[l] = 1

dic_sum = dic.copy()
#calcurate
for d in dic:
  dic_sum[d]=int(dic[d]*(dic[d]-1)/2)
  
for l in lis:
  dic_sum_n = dic_sum.copy()
  if l in dic:
    dic_sum_n[l] = int(((dic[l]-1)*(dic[l]-2))/2)
    print(sum(dic_sum_n.values()))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy

def count(dic, key):
    ans = 0
    for i in key:
        d = dic[i]
        ans += d * (d - 1) // 2
    return(ans)

N = int(input())
A = list(map(int, input().split()))
a = dict(Counter(A))
k = list(a.keys())
dic = {}
for i in k:
    a2 = copy.copy(a)
    a2[i] -= 1
    dic[i] = count(a2, k)
for i in range(N):
    print(dic[A[i]])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
cnts = Counter(a)

for ai in a:
    ans = 0
    for key,value in cnts.items():
        if key == ai:
            value -= 1
        ans +=  value*(value-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import partial


def combination(k: int, r: int) -> int:
    if(k < r):
        return 0
    else:
        return math.factorial(k) // (math.factorial(k - r) * math.factorial(r))
        
        
def result_func(counter: [int], n: int, an: int) -> int:
    combination_result = map(lambda i, x: combination(x, 2) if an != i else combination(x - 1, 2), range(n), counter)
    return sum(combination_result)


n = int(input())
a_list = input().split()
a = list(map(lambda x: int(x) - 1, a_list))

counter = list(map(a.count, range(n)))
new_result_func = partial(result_func, counter, n)
result_list = map(new_result_func, a)

for r in result_list:
    print(r)

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import partial


def combination(k: int, r: int) -> int:
    if(k < r):
        return 0
    else:
        return math.factorial(k) // (math.factorial(k - r) * math.factorial(r))
        
        
def result_func(counter: [int], n: int, an: int) -> int:
    combination_result = list(map(lambda i: combination(counter[i], 2) if an != i else combination(counter[i] - 1, 2), range(n)))
    return sum(combination_result)


n = int(input())
a_list = input().split()
a = list(map(lambda x: int(x) - 1, a_list))

counter = [a.count(i) for i in range(n)]
new_result_func = partial(result_func, counter, n)
result_list = [new_result_func(an) for an in a]

for r in result_list:
    print(r)

Submission status: Time Limit Exceeded

Solution code:
input_d1 = int(input()) 
input_d2 = list(input().split())

c = sorted(input_d2)
c.append('0')
e = {'0':0}

for i in range(0,input_d1):
    if c[i] == c[i+1]:
        pass
    else:
        e[c[i]] = i + 1 - sum(e.values())

e.pop('0')
d = {}

for i in e.keys():
    d[i] = 1 - e[i] + sum(map(lambda x:x*(x-1)/2 , e.values()))


for i in input_d2:
    print(int(d[i]))

Submission status: Time Limit Exceeded

Solution code:
import collections

input_d1 = int(input()) 
input_d2 = list(input().split())

c = collections.Counter(input_d2)
e = {}

for i in c.keys():
    e[i] = 1 - c[i] + sum(map(lambda x:x*(x-1)/2 , c.values()))

for i in input_d2:
    print(int(e[i]))

Submission status: Time Limit Exceeded

Solution code:
import collections

input_d1 = int(input())
input_d2 = list(input().split())

c = collections.Counter(input_d2)

for i in input_d2:
    summ = 0
    for j in c.keys():
        if i == j:
            summ += (c[j]-2) * (c[j]-1) / 2
        else:
            summ += (c[j]-1) * c[j] / 2
    print(int(summ))

Submission status: Time Limit Exceeded

Solution code:
tmp = int(input())
numbers = list(map(int,input().split()))
dic = {}
for i, num in enumerate(numbers):
  if not num in dic:
    dic[num] = 0
  dic[num] = dic[num] + 1
for i in range(tmp):
  num = numbers[i]
  ans = 0
  for k,v in dic.items():
    if k == num:
      ans = ans + int((v-1) * (v-2) / 2)
    else:
      ans = ans + int(v * (v-1) / 2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
def n_c_two(inp):
  if inp < 2:
    return 0
  return int((inp * (inp-1) ) / 2)

def all_sum(dic,ind):
  ret = 0
  dic[ind] -= 1
  for v in dic.keys():
    ret += n_c_two(dic[v])
  return ret

from collections import Counter

n = int(input())
data = [int(i) for i in input().split()]
c = dict(Counter(data)) 
ans = {}
for val in data:
  if val in ans:
    continue
  ans[val] = all_sum(c.copy(),val)

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

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

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

counter = collections.Counter(A)

for val in counter.values():
    all_comb += combinations_count(val, 2)

for i in A:
    ans = all_comb - combinations_count(counter[i], 2) + combinations_count(counter[i] - 1, 2)
    print(ans)

Submission status: Time Limit Exceeded

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

idx = 1
idx_count = 0
idx_set = []
for i in range(N):
  if idx != B[i]:
    tmp1 = int(idx_count*(idx_count-1)/2)		# 同一の整数を持つボールの組合せ数
    tmp2 = int((idx_count-1)*(idx_count-2)/2)	# 同一の整数を持つボールを1つ除いたときの組合せ数
    idx_set.append([idx, tmp1, tmp2])
    idx_count = 0
    idx = B[i]
  idx_count+=1
if idx_count != 0:
  tmp1 = int(idx_count*(idx_count-1)/2)			# 同一の整数を持つボールの組合せ数
  tmp2 = int((idx_count-1)*(idx_count-2)/2)		# 同一の整数を持つボールを1つ除いたときの組合せ数
  idx_set.append([idx, tmp1, tmp2])

idx_sum_set = []
all_c = 0
for i in range(len(idx_set)):
  all_c += idx_set[i][1]
for i in range(len(idx_set)):
  for j in range(len(idx_set)):
    if i == j:
      tmp = all_c-idx_set[j][1]+idx_set[j][2]
      idx_sum_set.append([idx_set[i][0], tmp])
"""
for i in range(len(idx_set)):
  tmp = 0
  for j in range(len(idx_set)):
    if i == j:	tmp += idx_set[j][2]
    else:		tmp += idx_set[j][1]
  idx_sum_set.append([idx_set[i][0],tmp])
"""

for i in range(N):
  j = int(len(idx_sum_set)/2)
  while 1:
    if A[i] == idx_sum_set[j][0]:
      print(idx_sum_set[j][1])
      break
    elif A[i] > idx_sum_set[j][0]:
      j = j + int((len(idx_sum_set)-j)/2)
    elif A[i] < idx_sum_set[j][0]:
      j = int(j/2)
"""
for i in range(N):
  for j in range(len(idx_sum_set)):
    if A[i] == idx_sum_set[j][0]:
      print(idx_sum_set[j][1])
"""

Submission status: Time Limit Exceeded

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

idx = 1
idx_count = 0
idx_set = []
for i in range(N):
  if idx != B[i]:
    tmp1 = int(idx_count*(idx_count-1)/2)		# 同一の整数を持つボールの組合せ数
    tmp2 = int((idx_count-1)*(idx_count-2)/2)	# 同一の整数を持つボールを1つ除いたときの組合せ数
    idx_set.append([idx, tmp1, tmp2])
    idx_count = 0
    idx = B[i]
  idx_count+=1
if idx_count != 0:
  tmp1 = int(idx_count*(idx_count-1)/2)		# 同一の整数を持つボールの組合せ数
  tmp2 = int((idx_count-1)*(idx_count-2)/2)	# 同一の整数を持つボールを1つ除いたときの組合せ数
  idx_set.append([idx, tmp1, tmp2])

idx_sum_set = []
for i in range(len(idx_set)):
  tmp = 0
  for j in range(len(idx_set)):
    if i == j:
      tmp += idx_set[j][2]
    else:
      tmp += idx_set[j][1]
  idx_sum_set.append([idx_set[i][0],tmp])

for i in range(N):
  j = int(len(idx_sum_set)/2)
  while 1:
    if A[i] == idx_sum_set[j][0]:
      print(idx_sum_set[j][1])
      break
    elif A[i] > idx_sum_set[j][0]:
      j = j + int((len(idx_sum_set)-j)/2)
    elif A[i] < idx_sum_set[j][0]:
      j = int(j/2)
"""
for i in range(N):
  for j in range(len(idx_sum_set)):
    if A[i] == idx_sum_set[j][0]:
      print(idx_sum_set[j][1])
"""

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

# 組み合わせ算出
def combinations_count(n, r):
    if n == 1:
        return 0
    if n == 0:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

if __name__ == '__main__':
    N= int(input())
    a = list(map(int, input().split()))
    c = collections.Counter(a)
    results = {}
    for i in set(a):
        total = 0
        for j in c:
            if i == j:
                total = total + combinations_count(c[j]-1, 2)
            else:
                total += combinations_count(c[j], 2)
        results[i] = total

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