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,input().split()))
A=sorted(a)
ans=0
memo=0
count=1
for i in range(n-1):
	if A[i]==A[i+1]:
		count+=1
		if i==n-2 and count>=2:
			memo+=count*(count-1)//2
	else:
		if count>=2:
			memo+=count*(count-1)//2
			count=1
for i in range(1,n+1):
	ans=memo
	k=a.count(a[i-1])
	if k>=3:
		ans-=k*(k-1)//2-(k-1)*(k-2)//2
	else:
		ans-=k*(k-1)//2
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
ans=0
memo=0
for i in Counter(a).values():
	if i>1:
		memo+=i*(i-1)//2
for i in range(1,n+1):
	ans=memo
	for j,k in Counter(a).items():
		if a[i-1]==j and k>=2:
			ans-=k*(k-1)//2-(k-1)*(k-2)//2
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
n=int(input())
a=list(map(int,input().split()))
for i in range(1,n+1):
	ans=0
	b=a.copy()
	b[i-1]=0
	for j,k in Counter(b).items():
		if k>=2:
			ans+=k*(k-1)//2
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
n=int(input())
a=list(map(int,input().split()))
def combinations_count(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i in range(1,n+1):
	ans=0
	b=a.copy()
	b[i-1]=0
	for j,k in Counter(b).items():
		if k>=2:
			ans+=combinations_count(k, 2)
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
k= list(map(int, input().split()))
z=[0]*n
cnt=0
for  i in range(n):
    for j in range(i+1,n):
        if k[i]==k[j]:
            cnt+=1
            z[i]+=1
            z[j]+=1
for m in range(n):
    print(cnt-z[m])

Submission status: Time Limit Exceeded

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

# 要素を計算
A_unique = set(A)
cnt = dict()

total = 0

# 組み合わせ数
for a in A_unique :
  total += int(A.count(a) * (A.count(a) -1) / 2)
  cnt[a] = A.count(a)-1

for a2 in A :
  print(total - cnt[a2])

Submission status: Time Limit Exceeded

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

# 要素を計算
A_unique = set(A)
ans = dict()

total = 0

# 組み合わせ数
for a in A_unique :
  total += A.count(a) * (A.count(a) -1) / 2

for a2 in A :
  if not a2 in ans :
    ans[a2] = int(total) - (A.count(a2)-1)
  print(ans[a2])

Submission status: Time Limit Exceeded

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

# 要素を計算
A_unique = set(A)

total = 0

# N個の時の組み合わせ数
for a in A_unique :
  total += A.count(a) * (A.count(a) -1) / 2

for a2 in A :
  print(int(total) - (A.count(a2)-1))

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=[0]*N

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

c_sum=0
for i in range(N):
    if cnt[i]>=2:
        c_sum+=combinations_count(cnt[i], 2)
    else:
        pass

for i in range(N):

    k=cnt[a[i]-1]
    l=cnt[a[i]-1]-1

    if cnt[a[i]-1]>=2:
        k=combinations_count(cnt[a[i]-1],2)
    else:
        k=0
    
    if cnt[a[i]-1]-1>=2:
        l=combinations_count(cnt[a[i]-1]-1,2)
    else:
        l=0
    
    d=-k+l
    print(c_sum+d)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter,defaultdict
from operator import mul
from functools import reduce
def cmb1(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    A_c=Counter(A)
    m=defaultdict(int)
    A_c_items=A_c.items()
    for i,a_c in enumerate(A_c_items):#選ぶ数字
        for j,a_c2 in enumerate(A_c_items):
            sum=0
            if i!=j and a_c2[1]>=2:
                sum+=cmb1(a_c2[1],2)
            elif i==j and a_c2[1]>=3:
                sum+=cmb1(a_c2[1]-1,2)
            m[a_c[0]]+=sum
    for i in range(n):
        print(m[A[i]])


if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

# 書かれた番号の頻度
Cntr = Counter(A)
Freq = sorted(Cntr.items(), key=lambda x: x[1], reverse=True)

# 答えの最大値 (1個しか存在しない番号は、取り除いても答えに影響しない)
ans_max = 0
for kv in Freq:
  if kv[1] < 2: break
  ans_max += kv[1] * (kv[1] - 1) // 2

# 答えのメモ (複数回計算しないように)
memo = dict()

for a in A:
  # 取り除いても答えに影響しない
  if Cntr.get(a) == 1:
    print(ans_max)
    continue

  # 過去に計算済みの番号
  if memo.get(a) != None:
    print(memo[a])
    continue

  ans = 0
  for kv in Freq:
    # 大きい順に見ているので1個になったら終了
    if kv[1] < 2: break
    # key -> value : 番号 -> ボール個数
    cnt = kv[1]
    # 自身は取り除く
    if kv[0] == a: cnt -= 1
    # 2個未満はスキップ
    if cnt < 2: continue
    # 2つ選ぶ方法は n*(n-1)//2
    ans += cnt * (cnt-1) // 2
  print(ans)
  memo[a] = ans

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def solve():
  n = int(input())
  A = list(map(int, input().split()))

  # 書かれた番号の頻度
  Freq = sorted(Counter(A).items(), key=lambda x: x[1], reverse=True)

  # 答えのメモ (複数回計算しないように)
  memo = dict()

  for a in A:
    if memo.get(a) != None:
      print(memo[a])
      continue

    ans = 0
    for kv in Freq:
      # 大きい順に見ているので1個になったら終了
      if kv[1] < 2: break
      # key -> value : 番号 -> ボール個数
      cnt = kv[1]
      # 自身は取り除く
      if kv[0] == a: cnt -= 1
      # 2個未満はスキップ
      if cnt < 2: continue
      # 2つ選ぶ方法は n*(n-1)//2
      ans += cnt * (cnt-1) // 2
    print(ans)
    memo[a] = ans

solve()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def solve():
  n = int(input())
  A = list(map(int, input().split()))

  # 書かれた番号の頻度
  Freq = Counter(A)

  # 答えのメモ (複数回計算しないように)
  memo = dict()

  for a in A:
    if memo.get(a) != None:
      print(memo[a])
      continue

    ans = 0
    for kv in Freq.items():
      # key -> value : 番号 -> ボール個数
      cnt = kv[1]
      # 自身は取り除く
      if kv[0] == a: cnt -= 1
      # 2個未満はスキップ
      if cnt < 2: continue
      # 2つ選ぶ方法は n*(n-1)//2
      ans += cnt * (cnt-1) // 2
    print(ans)
    memo[a] = ans

solve()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def solve():
  n = int(input())
  A = list(map(int, input().split()))

  # 書かれた番号の頻度
  Freq = Counter(A)

  for a in A:
    ans = 0
    for kv in Freq.items():
      # key -> value : 番号 -> ボール個数
      cnt = kv[1]
      # 自身は取り除く
      if kv[0] == a: cnt -= 1
      # 2個未満はスキップ
      if cnt < 2: continue
      # 2つ選ぶ方法は n*(n-1)//2
      ans += cnt * (cnt-1) // 2
    print(ans)

solve()

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
coun = collections.Counter(a)
k = list(coun.keys())
num = list(coun.values())
num_2 = []
for i in range(len(num)):
    B = num[:]
    B[i] = B[i] - 1
    num_2 += [int(sum(list(map(lambda x: (x - 1) * (2 + (x - 2)) / 2, B))))]
dic = dict(zip(k, num_2))
for i in range(n):
    print(dic[a[i]])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
#from scipy.special import comb
# a = comb(n, r)
# >> cannot use in Contest (like AtCoder)


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

    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())
data = list(map(int, input().split()))
count = Counter(data)
#print(count) > Counter({1: 4, 2: 2, 4: 2})
countMost = count.most_common()

lis = [0] * (2 * 10 ** 5)
tot = 0
for i, v in countMost:
    #print(i, v)
    lis[i - 1] = v
    tot += cmb(v, 2)
#print('TOT', tot)
# print(lis[0])
for i, v in enumerate(data):
    num = lis[v - 1]
    if (num == 2):
        #    print('a')
        ans = tot - 1
    else:
        ans = tot - cmb(num, 2) + cmb(num-1, 2)

    print(ans)
'''
dataSort = data.sort()
print(countMost)
print(len(countMost))
'''

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = set(a)
c = list(b)
d = [0]*len(c)
total = 0

for i in range(len(c)):
     d[i] = a.count(c[i])
     t = d[i]
     total += t*(t-1)/2    
    
for j in range(n):
    for k in range(len(c)):
        if a[j] == c[k]:
            print(int(total-d[k]+1))
            break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = set(a)
c = list(b)
d = [0]*len(c)
total = 0

for i in range(len(c)):
     d[i] = a.count(c[i])
     total += d[i]*(d[i]-1)/2    
    
for j in range(n):
    for k in range(len(c)):
        if a[j] == c[k]:
            print(int(total-d[k]+1))
            break

Submission status: Time Limit Exceeded

Solution code:
def main():
    import collections
    import math
    n=int(input())
    a=list(map(int, input().split()))
    c=collections.Counter(a)
    cnt=list(c.items())
    ans=0
    comb=[]
    for i in range(len(cnt)): #各出現数字のAi_C_2を計算
        buf=(cnt[i][1]**2-cnt[i][1])//2
        comb.append(buf)
        ans+=buf
    for ai in a:
        for i in range(len(cnt)):
            if cnt[i][0]==ai:
                print(ans-cnt[i][1]+1)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

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


n=int(input())
a=list(map(int,input().split()))
d1=dict()
for i in a:
    if i not in d1:
        d1[i]=1
    else:
        d1[i] += 1
#print(d1)
d2={i:ABC159D(j) for i,j in d1.items()}
d3={i:ABC159D(j-1)-d2[i] for i,j in d1.items()}
#print(d2,d3)
p=sum(d2.values())
for i in a:
    print(p+d3[i])

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    a_2 = a[:k] + a[k + 1:]
    coun = collections.Counter(a_2)
    num = list(coun.values())
    num_2 = map(lambda x: (x - 1) * (2 + (x - 2)) / 2, num)
    print(int(sum(num_2)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split(' ')))
for k in range(N):
    A_list_temp = A_list.copy()
    A_list_temp.pop(k)
    count = 0
    while len(A_list_temp) > 0:
        for one in A_list_temp:
            A_list_temp.remove(one)
            for one_inner in A_list_temp:
                if one_inner == one:
                    count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=list(map(int,input().split()))
c=set(b)
d={}
all=0
for s in c:
  d[s]=b.count(s)
  all+=d[s]*(d[s]-1)//2
for i in range(a):
  tmp=all-(d[b[i]]*(d[b[i]]-1))//2
  print(tmp+((d[b[i]]-1)*max(0,d[b[i]]-2)//2))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
sum=0
for n in list(dict.fromkeys(A)):
  c = A.count(n)
  sum += c*(c-1)//2
for i in range(N):
  c = A.count(A[i])
  print(sum-(c*(c-1)//2) + ((c-1)*(c-2)//2))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


k = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
s = 0
for i in c.values():
  if i == 1:
    continue
  s += math.factorial(i) // (math.factorial(i - 2) * math.factorial(2))

for i in a:
  j = c[i] - 1
  if j <= 1:
    if j == 0:
      print(s)
      continue
    print(s - 1)
    continue
  b = math.factorial(j) // (math.factorial(j - 2) * math.factorial(2))
  y = math.factorial(j + 1) // (math.factorial(j + 1 - 2) * math.factorial(2))
  d = y - b
  print(s - d)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
A_new=sorted(A)
n=0
sum_number=0
index=[]
number=[]
for i in range(N-1):
    if A_new[i]==A_new[i+1]:
        n=n+1
    else:
        sum_number=n*(n+1)/2+sum_number
        index.append(i)
        number.append(n+1)
        n=0
if n!=0:
    sum_number=n*(n+1)/2+sum_number
if all(number):
    number.append(n+1)
    index.append(N-1)
I=len(index)
for i in range(N):
    for s,S in enumerate(index):
        if A[i]==A_new[S]:
            L=s
            break
    print(int(sum_number+1-number[L]))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a_list=list(map(int,input().split()))
num=0
tot=0
s=set(a_list)

for k in s:
  num=a_list.count(k)
  tot+=num*(num-1)/2

for i in range(n):
  ban=a_list.count(a_list[i])
  if ban==1:
    ban=0
  else:
    ban-=1
  print(str(int(tot-ban)))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


k = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
s = 0
for i in c.values():
  if i == 1:
    s += 0
    continue
  s += math.factorial(i) // (math.factorial(i - 2) * math.factorial(2))

for i in a:
  j = c[i] - 1
  if j == 1:
    print(s - 1)
    continue
  elif j == 0:
    print(s)
    continue
  b = math.factorial(j) // (math.factorial(j - 2) * math.factorial(2))
  y = math.factorial(c[i]) // (math.factorial(c[i] - 2) * math.factorial(2))
  d = y - b
  print(s - d)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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


k = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
s = 0
for i in c.values():
  if i == 1:
    s += 0
    continue
  s += combinations_count(i, 2)

for i in a:
  j = c[i] - 1
  if j == 1:
    print(s - 1)
    continue
  elif j == 0:
    print(s)
    continue
  b = combinations_count(j, 2)
  y = combinations_count(c[i], 2)
  d = y - b
  print(s - d)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a_list=list(map(int,input().split()))
num=[0]*(n+1)
tot=0
s=set(a_list)

for k in s:
  num[k]=a_list.count(k)
  tot+=num[k]*(num[k]-1)/2

for i in range(n):
  ban=num[a_list[i]]
  if ban==1:
    ban=0
  else:
    ban-=1
  print(str(int(tot-ban)))

Submission status: Time Limit Exceeded

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

for i in range(n):
  if i!=0:
    a_list.insert(i-1,ban)
  tot=0
  ban=a_list[i]
  del(a_list[i])
  s=set(a_list)
  for k in s:
    num=a_list.count(k)
    tot+=num*(num-1)/2
  print(str(int(tot)))

Submission status: Time Limit Exceeded

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

for i in range(n):
  tot=0
  c=[]
  for j in range(len(a_list)):
    if j!=i:
      c.append(a_list[j])
  s=set(c)
  for k in s:
    tot+=(c.count(k))*(c.count(k)-1)/2
  print(str(int(tot)))

Submission status: Time Limit Exceeded

Solution code:
import collections 
import math
import copy

def combinations_count(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()))

cnt=0
c =collections.Counter(a) #要素の数

#全体の式の導出
#今回は要素ごとの組み合わせの数

for i in c.values() :
    cnt += combinations_count(i,2)

#各要素の影響を排除して出力
for i in a:
    print(cnt- combinations_count(c[i],2) +combinations_count(c[i]-1,2))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n=int(input())
dd = defaultdict(int)
A=[int(i) for i in input().split()]
for i in A:
    dd[i]+=1
ans = 0
for i in range(n):
    for j in dd:
        if A[i]==j:
            ans+=(((dd[j]-1)*(dd[j]-2))//2)
        else:
            ans+=(dd[j]*(dd[j]-1))//2
    print(ans)
    ans=0

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter, defaultdict
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, *a = map(int, read().split())
counter = list(Counter(a).items())
cnt = defaultdict(int)
memo = defaultdict(int)
for k, v in counter:
    if v > 1:
        cnt[k] = cmb(v, 2)
    else:
        cnt[k] = 0
    if v > 2:
        memo[k] = cmb(v - 1, 2)
    else:
        memo[k] = 0
for aa in a:
    print(sum(cnt.values()) - cnt[aa] + memo[aa])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
a = 0
for m in range(n):
    k = l.count(m+1)
    a += int(k*(k-1)/2)
for m in range(n):
    print(a-l.count(l[m])+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
a = 0
b = []
for m in range(n):
    b.append(l.count(m+1))
    k = b[m]
    a += int(k*(k-1)/2)
for m in range(n):
    print(a-b[l[m]-1]+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
for i in range(n):
    score = 0
    copy = l[:]
    del copy[i]
    for j in range(1,n+1):
        k = copy.count(j)
        score += int(k*(k-1)/2)
    print(score)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

int_list = list(map(int, stdin.readline().rstrip().split()))
cnt_list = [int_list.count(i) for i in range(1, N+1)]

ret_list = [0] * N

# これはO(N)でいける

tmp_ans = 0
for i in range(N):
    if cnt_list[i] > 1:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

# tmp_ans = sum([cnt_list[i]*(cnt_list[i]-1) // 2 for i in range(N) if cnt_list[i] > 1])

for i in range(N):
    if cnt_list[i] < 1:
        tmp_ret = 0
    else:
        tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
    ret_list[i] = tmp_ret

for i in int_list:
    print(ret_list[i-1])

# ret_list = [0 if cnt_list[i] < 1 else tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2 for i in range(N)]

# [print(ret_list[i-1]) for i in int_list]

Submission status: Time Limit Exceeded

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

for i in range(n):
    l = a.copy()
    l_q = []
    ttl = 0

    del l[i]
    ll = list(set(l))
    for i in ll:
        l_q.append(l.count(i))
    for i in l_q:
        if i >=2:
            ttl += i * (i - 1) / 2
    print(int(ttl))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

int_list = list(map(int, stdin.readline().rstrip().split()))

cnt_list = [int_list.count(i) for i in range(1, N+1)]
ret_list = []

# これはO(N)でいける

# tmp_ans = 0
# for i in range(N):
#     if cnt_list[i] > 1:
#         tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

tmp_ans = sum([cnt_list[i]*(cnt_list[i]-1) // 2 for i in range(N) if cnt_list[i] > 1])

# for i in range(N):
#     if cnt_list[i] < 1:
#         tmp_ret = 0
#     else:
#         tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
#     ret_list.append(tmp_ret)

# for i in int_list:
#     print(ret_list[i-1])

ret_list = [0 if cnt_list[i] < 1 else tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2 for i in range(N)]

[print(ret_list[i-1]) for i in int_list]

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

int_list = list(map(int, stdin.readline().rstrip().split()))

cnt_list = [int_list.count(i) for i in range(1, N+1)]
ret_list = []

# これはO(N)でいける

tmp_ans = 0
for i in range(N):
    if cnt_list[i] < 2:
        continue
    else:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

# for i in range(N):
#     if cnt_list[i] < 1:
#         tmp_ret = 0
#     else:
#         tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
#     ret_list.append(tmp_ret)

# for i in int_list:
#     print(ret_list[i-1])

ret_list = [0 if cnt_list[i] < 1 else tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2 for i in range(N)]

[print(ret_list[i-1]) for i in int_list]

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

int_list = list(map(int, stdin.readline().rstrip().split()))

cnt_list = [int_list.count(i) for i in range(1, N+1)]
ret_list = []

# これはO(N)でいける

tmp_ans = 0
for i in range(N):
    if cnt_list[i] < 2:
        continue
    else:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

# for i in range(N):
#     if cnt_list[i] < 1:
#         tmp_ret = 0
#     else:
#         tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
#     ret_list.append(tmp_ret)

ret_list = [0 if cnt_list[i] < 1 else tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2 for i in range(N)]

for i in int_list:
    print(ret_list[i-1])

Submission status: Time Limit Exceeded

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

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

cnt = Counter(a)
for i in range(1, n + 1):
    temp = a[i - 1]
    ans = 0
    for num, k in cnt.items():
        kosu = k
        if num == temp:
            kosu -= 1
        if kosu >= 2:
            ans += com(kosu, 2)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

int_list = list(map(int, stdin.readline().rstrip().split()))

cnt_list = [int_list.count(i) for i in range(1, N+1)]
ret_list = []

# これはO(N)でいける

tmp_ans = 0
for i in range(N):
    if cnt_list[i] < 2:
        continue
    else:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

for i in range(N):
    if cnt_list[i] < 1:
        tmp_ret = 0
    else:
        tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
    ret_list.append(tmp_ret)

for i in int_list:
    print(ret_list[i-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

str_list = stdin.readline().rstrip().split()
int_list = int_data = [int(i) for i in str_list]

cnt_list = [int_list.count(i) for i in range(1, N+1)]
ret_list = []

# これはO(N)でいける

tmp_ans = 0
for i in range(N):
    if cnt_list[i] < 2:
        continue
    else:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

for i in range(N):
    if cnt_list[i] < 1:
        tmp_ret = 0
    else:
        tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
    ret_list.append(tmp_ret)

for i in int_list:
    print(ret_list[i-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

str_list = stdin.readline().rstrip().split()
int_list = int_data = [int(i) for i in str_list]

cnt_list = []
ret_list = []

for i in range(1, N+1):
    tmp_cnt = int_list.count(i)
    cnt_list.append(tmp_cnt)
    
tmp_ans = 0
for i in range(N):
    if cnt_list[i] < 2:
        continue
    else:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2

for i in range(N):
    if cnt_list[i] < 1:
        tmp_ret = 0
    else:
        tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
    ret_list.append(tmp_ret)

for i in int_list:
    print(ret_list[i-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

str_list = stdin.readline().rstrip().split()
int_list = int_data = [int(i) for i in str_list]

cnt_list = []
ret_list = []

for i in range(1, N+1):
    tmp_cnt = int_list.count(i)
    cnt_list.append(tmp_cnt)

for i in range(N):
    tmp_ret = 0
    for j in range(N):
        if i == j:
            if cnt_list[j] < 3:
                continue
            else:
                tmp_ret += (cnt_list[j]-1)*(cnt_list[j]-2) // 2
        else:
            if cnt_list[j] < 2:
                continue
            else:
                tmp_ret += cnt_list[j]*(cnt_list[j]-1) // 2
    
    ret_list.append(tmp_ret)

for i in int_list:
    print(ret_list[i-1])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
import numpy as np
num_list = np.zeros(N+1)
for A_ in A:
    num_list[A_] += 1

for A_ in A:
    num_list[A_] -= 1
    ans_ = 0
    for index_ in num_list:
        ans_ += index_ * (index_ - 1) / 2
    print(int(ans_))
    num_list[A_] += 1

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

counter = np.bincount(A)

ans = []
for i in range(len(A)):
    counter_i = counter.copy()
    counter_i[A[i]] -=1
    sum_i = sum(counter_i * (counter_i -1) //2)
    ans.append(sum_i)
    
print(*ans, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

counter = np.bincount(A)

ans = []
for i in range(len(A)):
    counter_i = counter.copy()
    counter_i[A[i]] -=1
    sum_i = sum(counter_i * (counter_i -1) //2)
    ans.append(sum_i)
    
for i in range(len(ans)):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
import math

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

l = [0] * max(a)
for i in range(n):
    l[a[i] - 1] += 1 


for i in range(n):
    l1 = l[:]
    ans = 0
    l1[a[i] - 1] -= 1
    for j in range(len(l)):
        ans += l1[j] * (l1[j] - 1) / 2
    print(math.floor(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])


# エントリーポイント
def main():
    N = input(int)
    A = input([int, N])

    count = dd(int)
    for a in A:
        count[a] += 1
    ans = {}
    for k in count.keys():
        temp = 0
        for v in count.values():
            temp += (v * (v - 1)) // 2
        ans[k] = temp - count[k] + 1
    for a in A:
        print(ans[a])


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Time Limit Exceeded

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

A_e = list(set(A))

#print(list(set(A)))

for i in A_e:
    x = A.count(i)
    if x >= 2:
        xc2 = x*(x-1)/2
        X = X + xc2
#print("ALL", X)

for k in range(N):
    xk = A.count(A[k])
    print(int(X-xk+1))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])


def debug(*v):
    if not __name__ == '__main__':
        print(*v)


# エントリーポイント
def main():
    N = input(int)
    A = input([int, N])

    count = dd(int)
    for a in A:
        count[a] += 1
    debug(count)
    for a in A:
        ans = 0
        for k, v in count.items():
            temp = v if not k == a else v - 1
            ans += (temp * (temp - 1)) // 2
        print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Time Limit Exceeded

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

for k in range(n):
  counter = 0
  for i in range(n-1):
    if i == k:
      continue
    for j in range(i+1, n):
      if j == k:
        continue
      if a[i] == a[j]:
        counter += 1
  print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A0 = [int(x) for x in input().split()]
A_t = tuple(A0)

for k in range(len(A0)):
    A = list(A_t)
    A.pop(k)
    B = set(A)
    
    count = 0
    for b in B:
          count += (A.count(b) * (A.count(b) - 1))/2
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import defaultdict
n=int(input())
l=list(map(int,input().split()))
d=defaultdict(lambda :0)
d1=defaultdict(lambda :0)
for i in range(n):
    d[l[i]]+=1
for i in d:
    x=d[i]
    if d[i]<2:
        d1[i]=0
    else:
        d1[i]=factorial(x)/(factorial(x-2)*2)
sum=0
for i in d1:
    sum+=d1[i]
d2=defaultdict(lambda :0)
for i in l:
    ans=0
    if i in d2:
        print(d2[i])
    else:
        if d[i]<3:
            ans=sum-d1[i]
        else:
            ans=sum-d1[i]+(factorial(d[i]-1)/(factorial(d[i]-3)*2))
        d2[i]=int(ans)
        print(int(ans))