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 = [int(i) for i in input().split()]
N = len(A)
dictA = {i: A[i] for i in range(len(A))}
dictA_sorted = sorted(dictA.items(), key=lambda x:x[1])
A_sorted = [item[1] for item in dictA_sorted]
sorted_index = [item[0] for item in dictA_sorted]
for count in range(N):
    k = sorted_index.index(count)
    ways = 0
    i = 0
    if k == 0:
        B = A_sorted[1:]
    elif k == N - 1:
        B = A_sorted[:N - 1]
    else:
        B = A_sorted[:k] + A_sorted[k + 1:]
    while i < N - 1:
        head = B[i]
        j = 1
        while j + i < N - 1 and B[j + i] == head:
            j += 1
        ways +=  (j * (j - 1)) // 2
        i += j
    print(ways)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
koremade = 0
for i in range(N):
  count[i]  = A.count(i + 1)
  koremade += count[i]
  if koremade == N:
    break
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = float(input())
Ai = list(map(int, input().split()))

dic = collections.Counter(Ai)
diccomb={}
dicselected={}
count = {}
for k in dic:
    num = dic[k]
    diccomb[k] = int(num*(num-1)/2)
    dicselected[k] = int((num-1)*(num-2)/2)

for k in Ai:
    if k not in count:
        count[k] = sum(diccomb.values())-diccomb[k]
    print(dicselected[k]+count[k])

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)
keys, values = zip(*c.most_common())
ans = 0
for i in values:
  if i != 1:
  	ans += (i*(i-1)//2)

for i in a:
    ct = 0
    for j in keys:
      if i == j:
        co = values[ct]
        break
      ct += 1
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
from collections import defaultdict
d = defaultdict(lambda: -1)
d_ = defaultdict(lambda: -1)
n = int(input())
a = [int(x) for x in input().split()]

for i in range(len(a)):
  a_ = a.copy()
  b = a_.pop(i)
  if d[str(b)] != -1:
    print(d[str(b)])
    continue
  A = list(set(a_))
  ans = 0
  for j in A:
    c = a_.count(j)
    if d_[str(c)] != -1:
      ans += d_[str(c)]
    else:
      e = comb(c, 2, exact=True)
      ans += e
      d_[str(c)] = e

  d[str(b)] = ans
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from sys import stdin
# import math
from functools import lru_cache


@lru_cache(maxsize=1000)
def combinations_count(n, r):
    if n < 2:
        return 0
    return n*(n-1)//2


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

d = {}
cnt = 0
for i in range(N):
    d[A[i]] = d.get(A[i], 0)+1
# print(d)
for l in range(N):
    d[A[cnt]] = d.get(A[cnt], 0)-1
    # print(d)
    # print(len(d))
    sum = 0
    for key, value in enumerate(d):
        # print(key,value)
        sum = sum+combinations_count(d.get(value, 0), 2)
    print(sum)
    d[A[cnt]] = d.get(A[cnt], 0)+1
    # d = {}
    cnt += 1

Submission status: Time Limit Exceeded

Solution code:

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

    res = [[e, A.count(e)] for e in set(A) if A.count(e) > 1]

    dic = {}

    for i in range(N):
        ans = 0

        if A[i] in dic:
            print(int(dic[A[i]]))
            continue

        for r in res:
            if r[0] == A[i]:
                ans += (r[1]-1)*(r[1]-2)/2
            else:
                ans += (r[1])*(r[1]-1)/2
        dic[A[i]] = ans

        print(int(ans))


# Solve
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import add

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

def reduce_tmp(tmplist):
  return reduce(add, range(tmplist))

def tmp2(tmplist):
  return sum([reduce_tmp(tmplist.count(ai)) for ai in set(tmplist)])

i = 0

[print(tmp2(a[0:k] + a[k+1:])) for k in range(n)]

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

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

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    num = newa[count]

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

Submission status: Time Limit Exceeded

Solution code:
import collections

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

setB = list(set(B))
n = len(setB)
Num = [0]*n
for j in range(n):
  Num[j] = B.count(setB[j])
  
for i in range(N):
  res = 0
  for j in range(n):
    if B[i] == setB[j]:
      res += (Num[j]-1)*(Num[j]-2)/2
    else:
      res += Num[j]*(Num[j]-1)/2
  print(int(res))

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = map(int, input().split())
li = []
for i in a:
    li.append(i)
for k in range(n):
	dic = collections.Counter(li)
	dic[li[k]] -=1
	x = 0
	for d in dic.values():
		x+= int(d*(d-1)*0.5)
	print(x)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import collections

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


"""
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = 3 * pow(10, 5)  # N は必要分だけ用意する
mod = pow(10, 9) + 7
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    #fact.append(fact[-1] * i)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    #inv.append(-inv[mod%i] * (mod // i))
    factinv.append((factinv[-1] * inv[-1]) % mod)
    #factinv.append(factinv[-1] * inv[-1])
"""

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

count = [0 for _ in range(n+1)]
ans = [0 for _ in range(n+1)]
c = collections.Counter(a)
#print(c)
for i in range(n+1):
    #tmp = a.count(i)
    tmp = c[i]
    #print(tmp)
    count[i] = tmp
    ans[i] = cmb(tmp, 2)
    #print(tmp, ans[i])
#print(count, ans)
sum_ans = sum(ans)
#print(sum_ans)
for k in range(n):
    #count[k] -= 1
    #print(a[k], count[a[k]])
    #print(count[a[k]] - 1 ,cmb(count[a[k]]-1, 2))
    #print(count[a[k]] ,cmb(count[a[k]], 2))
    ans = sum_ans + cmb(count[a[k]]-1, 2) - cmb(count[a[k]], 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())

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


for i in range(N):
    ans = 0
    a_slice = a[:i] + a[i+1:]
    c = collections.Counter(a_slice)
    counter = c.most_common()
    for j in range(len(counter)):
        ans += int(counter[j][1] * (counter[j][1] - 1) / 2)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
data=list(map(int, input().split(' ')))
A={}

def check(DIC):
    n=0
    for value in DIC.values():
        n+=int(value*(value-1)/2)
    
    return n


for value in data:
    if value in A:
        A[value]+=1
    else:
        A[value]=1


#print(A)
B={}
for key in A:
    A[key] -= 1
    #print(check(A))
    B[key] = check(A)
    A[key] +=1

#print(B)
for key in data:
    print(B[key])

Submission status: Time Limit Exceeded

Solution code:
def fact(k):
    if k < 2:
        return 1
    val = 1
    for i in range(2, k+1):
        val = val*i
    return val

n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    temp = a[:]
    temp[k:k+1] = []
    ms = set(temp)
    total = 0
    for m in ms:
        times = temp.count(m)
        if times < 2:
            continue
        total += fact(times) / (fact(2) * fact(times-2))
    print(int(total))

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
num_list = list(input().split())
memo = [0]*(N+1)
memo_flg = [False]*(N+1)
c = collections.Counter(num_list)

def calc(k):
    target = num_list[k]
    num = int(target)
    if(memo_flg[num] is True):
        return memo[num]
    ans = 0
    
    test = dict(c)
    test[target] -= 1

    for i in test.keys():
        ans += (test[i]*(test[i]-1))//2
    memo[num] = ans
    return ans

[print(calc(i)) for i in range(N)]

Submission status: Time Limit Exceeded

Solution code:
import copy
from collections import deque

N = int(input())
li = list(map(int, input().split()))
set_li = set(li)
r = range(N)
d = deque()
for i in r:
    temp = copy.copy(li)
    del temp[i]
    d.append(temp)
def return_num(li):
    total = 0
    for n in set_li:
        c = li.count(n)
        total += c*(c-1)/2
    return int(total)
total_li = [return_num(each) for each in d]
for v in total_li:
    print(v)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
from copy import deepcopy

def func(n):
    ans = math.factorial(n)//((math.factorial(n-2))*math.factorial(2))
    return ans

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

for j in range(N):
    sums = 0
    a1 = deepcopy(a)
    del a1[j]
    ans = dict(collections.Counter(a1))
    for i in ans.values():
        if i < 2:
            continue
        else:
            sums += func(i)
    print(sums)
    #print(a1)
    #print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
 
n = int(input())
c=list(map(int,input().split()))
 
s = Counter(c)
t = list(s.items())
 
nag = len(t)
x = 0
rt = [0 for i in range(nag)]
 
for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
for f in range(nag):
    rt[f] = x - (t[f][1] -1)
  
for j in range(n):
    for k in range(nag):
        if c[j] == t[k][0]:
            print(rt[k])
            break

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
c_lst = []
c = collections.Counter(a)
for i in c.most_common()[::-1]:
    if i[1] >= 2:
        c_lst.append(list(i))
if c_lst:
    values, counts = zip(*c_lst)
else:
    values = []

cnt = []
for i in range(len(c_lst)):
    cnt.append((c_lst[i][1] * (c_lst[i][1] - 1)) // 2)
cnt_ = sum(cnt)
    
for i in a:
    ans = 0
    if i in values:
        ans += ((c_lst[values.index(i)][1] - 1) * (c_lst[values.index(i)][1] - 2)) // 2
        ans += cnt_ - cnt[values.index(i)]
    else:
        ans = cnt_
    print(ans)

Submission status: Time Limit Exceeded

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

import collections
import copy

ans = 0

for i in range(len(A)):
  ans = 0
  list = copy.copy(A)
  list.pop(i)
  c = collections.Counter(list)
  #for v in c.values():
  #  ans += (v*(v-1)/2)
  a = sum([int((i*i-i)/2) for i in c.values()])
  print(a)
  #print(int(ans))

Submission status: Time Limit Exceeded

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

se = set()
dic = dict()
for i in range(N):
    if A[i] in se:
        dic[A[i]] += 1
    else:
        se.add(A[i])
        dic.update({A[i]: 1})
        
result = [0]*N
for i in range(N):
    for k in dic.keys():
        if A[i] == k:
            result[i] += (dic[k]-1)*(dic[k]-2)//2
        else:
            result[i] += (dic[k])*(dic[k]-1)//2

print("\n".join(list(map(str, result))))

Submission status: Time Limit Exceeded

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

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

    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
S = list(input().split(" "))

s = [int(n) for n in S]

dic_l = collections.Counter(s)
factor_list = list(dic_l.keys())
def myfunc():
  for i in range(n):
    tmp = s[i]
    count = 0
    for m in range(len(factor_list)):
      if tmp == factor_list[m]:
        kazu = dic_l[factor_list[m]] -1
        count = count + cmb(kazu, 2)
      else:
        kazu = dic_l[factor_list[m]]
        count = count + cmb(kazu, 2)
    yield(count)

for x in myfunc():
  print(x)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

n = int(input())
a_list = list(map(int, input().split()))
aa_dict = {}
a_dict = {}
for a in a_list:
    if a in aa_dict:
        aa_dict[a] += 1
        if aa_dict[a] == 2:
            a_dict[a] = 2
        else:
            a_dict[a] += 1
    else:
        aa_dict[a] = 1
a_cal_dict = {}
cal_dict = {}
for a in a_list:
    if a in a_cal_dict:
        print(a_cal_dict[a])
    else:
        ans = 0
        for key in a_dict:
            n = 0
            if key == a:
                n = a_dict[key] - 1
            else:
                n = a_dict[key]
            if n < 2:
                continue
            if n in cal_dict:
                ans += cal_dict[n]
            else:
                cal_dict[n] = n * (n-1) // 2
                ans += cal_dict[n]
        print(ans)
        a_cal_dict[a] = ans

Submission status: Time Limit Exceeded

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

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

ans_all = 0
for x in set(A):
    m = A.count(x)
    ans_all += m * (m - 1) // 2

for x in A:
    m = A.count(x) - 1
    print(ans_all - m)

Submission status: Time Limit Exceeded

Solution code:
#from scipy.special import comb
#from math import factorial
import collections

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

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

#a = cmb(n,r)
c = collections.Counter(A)
l1 = []
l2 = []
l3 = []
for i in c.keys():
  l1.append(i)
  if c[i] == 1:
    l2.append(0)
    l3.append(0)
  if c[i] == 2:
    l2.append(1)
    l3.append(0)
  if c[i] >= 3:
    l2.append(cmb(c[i], 2))
    l3.append(cmb(c[i]-1, 2))    

#print(l1)
#print(l2)
#print(l3)
l4 = []
for i in c.keys():
  sum = 0
  for j in c.keys():
    var_index = l1.index(j)
    if i == j:
      sum += l3[var_index]
    else:
      sum += l2[var_index]
  l4.append(sum)
#print(l4)
for i in range(N):
  var_index = l1.index(A[i])
  print(l4[var_index])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
from collections import Counter
cnt = Counter(a for a in A)
cnt = {x : cnt[x] for x in cnt if cnt[x] >= 2}
#print(cnt)
for i in range(N):
  ans = 0
  for a, c in cnt.items():
    if a == A[i]:
      c -= 1
    #if c >= 2:
    ans += int(c * (c - 1) / 2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

N = int(input())
num_list = list(map(int, input().split()))
num_set = set(num_list)

# 全数あるときの組み合わせ
ans_dict_1 = {}
# 1個抜いたときの組み合わせ
ans_dict_2 = {}

for i in num_set:
  ans_dict_1[i] = comb(num_list.count(i),2,exact=True)
  ans_dict_2[i] = comb(num_list.count(i)-1,2,exact=True)

for i in num_list:
  print(sum(ans_dict_1.values()) - ans_dict_1[i] + ans_dict_2[i])

Submission status: Time Limit Exceeded

Solution code:
import math

def resolve():
    n = int(input())
    a = [int(x) for x in input().split()]

    for k in range(n):
        # 初期化
        ak = a[:] # deep copy
        del ak[k]
        kumi = 0
        ended = set()

        # loop
        for i in range(n - 1):
            a0 = ak[i] # i番目の要素を検査
            if a0 in ended:
                continue
            cnt = ak.count(a0)
            if cnt >= 2:
                kumi += kumiawase(cnt ,2)
            ended.add(a0)
        print(kumi)

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

resolve()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import collections


n = int(input())
l = list(map(int, input().split()))
counter = collections.Counter(l)
cache = dict()
len_cache = dict()
sum_v = sum(l)

for i in range(n):
    value = l[i]
    cache_key = sum_v - value
    if sum_v - value in cache:
        print(cache.get(cache_key))
    else:
        total = 0
        for k, v in counter.items():
            num = v - 1 if k == value else v
            if num in len_cache:
                total += len_cache[num]
            else:
                count = len(list(itertools.combinations(range(num), 2)))
                len_cache[num] = count
                total += count
        cache[cache_key] = total
        print(total)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
import collections
import math
def combinations_count2(n):
    if(n<=1):
        answer=0
    else:
        answer=n*(n-1)/2
    return answer

table=collections.Counter(a)
keytable=table.keys()
c=[]

for key in keytable:
    c.append(table[key])
#print(c)
ca=[]
for j in range(len(c)):
    ca.append(combinations_count2(c[j]))
#print(ca)
sums=sum(ca)
#print(sum)
#print(keytable)
#print(table)
for i in range(len(a)):
    keynum=list(keytable).index(a[i])
    answer=sums-ca[keynum]+combinations_count2(c[keynum]-1)
    print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
import collections
from math import factorial

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

for i in range(len(A)):
  if i==0:
    A_temp = A[1:]
  elif i==len(A)-1:
    A_temp = A[0:len(A)-1]
  else:
    A_temp = A[:i] + A[i+1:] 
  c = collections.Counter(A_temp)
  
  co = 0
  for k in c.values():
    if k != 1 and k != 0:
      Mcon = factorial(k) // factorial(2) // factorial(k - 2)
      co += Mcon
  print(co)

Submission status: Time Limit Exceeded

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

cnt_list = [0]*(N+1)
for i in range(1,N+1):
  cnt_list[i] = A.count(i)

result = 0
for i in range(1,N+1):
  result += cnt_list[i] * (cnt_list[i]-1) * (1/2)

res = int(result)
for i in range(0,N):
  print(res-cnt_list[A[i]]+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
N = int(input())
List = [int(i) for i in input().split(" ")]
C =Counter(List)
D = {}
for i in C:
    if C[i]>=2:
        D[i] = combinations_count(C[i], 2)
    else:
        D[i]=0
# print(list(D.values()))    
for j in range(N):
    l = List[j]
    
    d = D[l]
    c = C[l]
    if c>=3:
        D[l] = combinations_count(c-1, 2)
    else:
        D[l] = 0
    dl = list(D.values())
    print(sum(dl))
    D[l]=d

Submission status: Time Limit Exceeded

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

import math
def combinations_count(n, r):
    ret = 0
    if n >= r :
        ret = math.factorial(n) // (math.factorial(n - r) *2)
    return ret

dupl = [x for x in set(A) if A.count(x) > 1]
for i in range(N):
    ans = 0
    l = A[:]
    l.pop(i) 
    for j in dupl:
        p = combinations_count(l.count(j),2) 
        if p > 0:
            ans = ans + p
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
#import numpy as np

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
answer=0
for k in range(n):
  s=a.pop(k)
  t=a
  while len(t)!=0:
    p=t.count(t[0])
    answer=answer+(p)*(p-1)/2
    t=[i for i in t if i!=t[0]]
  print(int(answer))
  answer=0
  a.insert(k,s)

Submission status: Time Limit Exceeded

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

s = collections.Counter(A)

for i in s:
    if s[i] >= 2:
        b = s[i]*(s[i]-1)/2
        cnt += b

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

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

    return ret

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

copy_a = copy.copy(a)
unique_a = set(copy_a)

cnt_all_conbi = 0
a_cnt_dict = {}

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

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

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
 
def combinations_count(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
def main():
  N = int(input())
  l = input().split()
  for i in range(N):
    o = l.pop(i)
    c = collections.Counter(l)
    ans = 0
    for v in c.values():
      if v >= 2:
        ans += combinations_count(v, 2)
      else:
        ans += 0
    print(ans)
    l.insert(i, o)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

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

countlist = [0]*(len(nlist) + 1)
for i in range(0,len(nlist) + 1):
    countlist[i] = nlist.count(i)

count = 0
for i in range(0,len(nlist)):
    if countlist[i] >= 2:
        count+=combinations_count(countlist[i],2)

anslist = []

for i in range(0,len(nlist)):
    anslist.append((count-countlist[nlist[i]])+1)

for i in anslist:
    print(i)

Submission status: Time Limit Exceeded

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

from math import factorial
from itertools import combinations
from collections import Counter
from copy import deepcopy

def main():
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        tmp = a[i]
        a[i] = -1
        tmp_a_c = Counter(a)
        tmp_a_cc = [tmp_a_c[i] for i in tmp_a_c if tmp_a_c[i] > 1]
        r = 0
        for cc in tmp_a_cc:
            r += factorial(cc) // (2 * factorial(cc-2))
        print(r)
        a[i] = tmp


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
import numpy as np
def count_ident(v):
    c = collections.Counter(v)
    val_list=np.array(list(c.values()))
    v_count=val_list*(val_list-1)/2
    print(int(sum(v_count)))

import copy
c = copy.deepcopy(a)    
for i in range(len(a)):
    tmp = copy.deepcopy(a)    
    tmp.pop(i)
    count_ident(tmp)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    uq = list(set(A))
    ans_hash = {}
    num_hash = {}
    for u in uq:
        ans = 0
        a = A[:]
        i = a.index(u)
        del a[i]
        val = Counter(a).values()
        for num in val:
            if num in num_hash:
                ans += num_hash[num]
            else:
                num_hash[num] = num * (num - 1) // 2
                ans += num_hash[num]
        ans_hash[u] = ans
    for n in A:
        print(ans_hash[n])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

for j in range(n):
    count = 1
    for i in z:
        count += i*(i-1)/2
    count -= z[a3.index(a[j])]
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from itertools import combinations
from collections import Counter
import math

N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#-----

A_cnt = Counter(A_list)

A_sum_of_combination={}
A_sum_of_combination_minus1={}
for k,v in A_cnt.items():
    # 個数2で場合分け
    if v >= 2:
        A_sum_of_combination[k] = len(list(combinations( range(v), 2)))
    else:
        A_sum_of_combination[k] = 0
    
    if (v-1) >= 2:
        A_sum_of_combination_minus1[k] = len(list(combinations( range(v-1), 2)))
    else:
        A_sum_of_combination_minus1[k] = 0

sum_all = sum( [ v for k,v in A_sum_of_combination.items()] )

for i in A_list:
    num_selected = A_cnt[i] - 1
    
    if num_selected >= 2:
        ans = sum_all - A_sum_of_combination[i] + A_sum_of_combination_minus1[i]
        print(ans)
    else:
        ans = sum_all - A_sum_of_combination[i]
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
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()))
c = collections.Counter(A)
ans = 0
for k, v in c.items():
    if v > 1:
        ans += combinations_count(v, 2)
total_ans = ans

memo = {}
for k_ball in A:
    if k_ball in set(memo.keys()):
        print(memo[k_ball])
    elif c[k_ball] > 2:
        memo[k_ball] = total_ans - (
            combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2)
        )
        print(memo[k_ball])
    elif c[k_ball] > 1:
        memo[k_ball] = total_ans - 1
        print(memo[k_ball])
    else:
        memo[k_ball] = total_ans
        print(memo[k_ball])

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
lst = list(map(int, input().split()))
sett = set(lst)

if len(lst) == len(sett):
    for i in range(n):
        print(0)
elif len(sett) == 1:
    for i in range(n):
        print(len(list(combinations(range(n-1), 2))))
else:
    newLst = []
    for x in sett:
        newLst.append(lst.count(x))
    dic = dict(zip(sett, newLst))
    for i in range(n):
        summ = 0
        dic[lst[i]] = dic[lst[i]]-1
        for x in list(dic.values()):
            summ = summ + len(list(combinations(range(x), 2)))
        dic[lst[i]] = dic[lst[i]]+1
        print(summ)

Submission status: Time Limit Exceeded

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

kazuset=list(set(kazu))
atai=0
for i in kazuset:
    kosuu=kazu.count(i)
    atai=atai+kosuu*(kosuu-1)/2

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