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())
k=list(map(int,input().split()))
p=[0]*n
for i in range(n):
    count=k.count(i+1)
    p[i]=p[i]+count*max(count-1,0)//2
#print(p)
mas=p
for j in range(n):
    count=k.count(k[j])
    x=max(count-1,0)*max(count-2,0)//2
    print(sum(p)+x-p[k[j]-1])
    
#print(p)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
maxim = max(A)
select_num = [0 for i in range(0,maxim+1)]
select_num2 = [0 for i in range(0,maxim+1)]
count_num = [0 for i in range(0,maxim+1)]
for i in range(1,maxim+1):
  count_num[i] = A.count(i)
  if(count_num[i] > 1):
    select_num[i] = (count_num[i])*(count_num[i] - 1)//2
    select_num2[i] = count_num[i] - 1
sumS = sum(select_num)
for i in range(N):
  print(sumS -select_num2[A[i]])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.buffer.readline

def main():
    n = int(input())
    lis = list(map(int,input().split()))
    ans = Counter(lis)
    for j in range(n):
        su = 0
        for k, v in ans.items():
            if k == lis[j]:
                su += (v - 2) * (v - 1) // 2
            else:
                su += (v - 1) * v // 2
        print(su)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
    ans=0
    b=a[:]
    del b[i]
    c=list(itertools.combinations(b,2))
    for j in range(len(c)):
        if c[j][0]==c[j][1]:
            ans+=1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

input_1 = input()
input_2 = input_1.split()

for k in range(N):
    A = map(int, input_2)

    def equal(num):
        return num / 1
    A = [equal(x) for x in A]

    ans = 0

    A.remove(A[k])

    for l in range(N - 1):
        for m in range(N - 1):
            if A[l] == A[m]:
                ans += 1
        
        ans -= 1

    ans = ans / 2

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

N = int(input())
B = list(map(int, input().split()))
cp_B = copy.copy(B)
p = 0

setB = list(set(B))
num = len(setB)

for i in range(N):
  res = 0
  p = cp_B.pop(0)
  for j in range(num):
    if setB[j] == p:
      res += (B.count(setB[j])-1)*(B.count(setB[j])-2)/2
    else:
      res += B.count(setB[j])*(B.count(setB[j])-1)/2
  print(int(res))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

# kごとに、
# 異なる数がいくつあるかを調べる
# (2以上要素が存在する)異なる数ごとに、
# (要素数)C2を解く。

np_A = np.array(A)

# 辞書作成
dic = {}

for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)
    # print("tmp_k", tmp_k)   # k以上
    # print(np.unique(np_A))

    # 同じ組み合わせのケースを排除する。
    # (num, 要素数)の組み合わせを辞書かで作る
    # →単純にnCrなら(n, r)で保存する

    k_unique = np.unique(tmp_k)
    ans = 0

    for u in k_unique:
        count_u = np.count_nonzero(tmp_k==u)
        if count_u < 2:
            continue
        key = "{0}".format(count_u) + "2"   # key
        if key in dic.keys():
            ans += dic[key]
            continue

        # (count_u) C 2
        tmp = comb(count_u,2)
        ans += tmp
        # 値の更新
        dic[key] = tmp
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=(input().split())
for i in range(n):
    x=0
    for j in range(n):
        for k in range(n):
            if j==i:
                continue
            elif k==i or j==k:
                continue        
            elif j>k and a[j]==a[k] and j!=i and k!=i:
                x=x+1
    print(x)

Submission status: Time Limit Exceeded

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)


def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    n = int(lines[0])
    a = [int(e) for e in lines[1].split(' ')]

    return (n, a)


def solve(n, a):


    
    b = dict()
    for i in range(len(a)):
        cnt = b.get(a[i], 0)
        cnt = cnt + 1
        b[a[i]] = cnt

    c = dict()
    result = []
    for i in range(len(a)):

        if a[i] in c:
            result.append(c[a[i]])
            continue

        result_tmp = 0
        for v, cnt in b.items(): 
            cnt_tmp = cnt
            if v == a[i]:
                cnt_tmp = cnt - 1

            result_tmp = result_tmp + cnt_tmp * (cnt_tmp - 1) // 2
        
        result.append(result_tmp)
        c[a[i]] = result_tmp

    return result 


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
a = list(map(int,input().split()))
count = 0
newa = a


def kazoeru(newa):
    conbi = 0
    for i in itertools.combinations(newa,2):
        if i[0] == i[1]:
            #print(i[0],i[1])
            conbi += 1
    print(conbi)

for i in range(n):
    num = newa[count]
    del newa[count]
    kazoeru(newa)
    #print(newa)
    newa.insert(count,num)
    count += 1

Submission status: Time Limit Exceeded

Solution code:

import collections
from math import factorial

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

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

for i in range(1, N + 1, 1):
    Except_Ak = A[:i-1] + A[i:]
    # print(Except_Ak)
    c = collections.Counter(Except_Ak)
    values, counts = zip(*c.most_common())
    elem_num_list = list(counts)
    # print(elem_num_list)
    count = 0
    
    for i in range(1, len(elem_num_list) + 1, 1):

        if elem_num_list[i-1] > 1:
            count = count + combinations_count(elem_num_list[i-1], 2)

        else:
            continue

    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
an = list(map(int, input().split()))
bn = list(set(an))
allcount = 0
for i in bn:
    a = an.count(i)
    allcount += a*(a-1)//2

for i in an:
    print(allcount - (an.count(i)-1))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
ans = 0
c = collections.Counter(A)
for k in range(N):
    buf = A[k]
    ans = 0
    A[k] = 0
    c = collections.Counter(A)
    if (max(c.values())==1):
        print(0)
        A[k] = buf
        continue
    for l in c.values():
        ans = ans + l*(l-1)/2
    print(int(ans))
    A[k] = buf

Submission status: Time Limit Exceeded

Solution code:
"""
"""
from math import factorial
from collections import defaultdict


def int_as_array(num): return list(map(int, [y for y in str(num)]))


def array_as_int(arr): return int(''.join(map(str, arr)))


def read_int(): return int(input())


def read_array(): return list(map(int, input().split(' ')))


def array_to_string(arr, sep=' '): return sep.join(map(str, arr))


def matrix_to_string(arr, sep=' '): return '[\n' + '\n'.join(
    [sep.join(map(str, row)) for row in arr]) + '\n]'


def choose_combination(n, r):
    if r > n:
        return 0
    return int((factorial(n) / factorial((n - r))) * (1 / factorial(r)))


def solve(n, array):
    counts = defaultdict(int)
    for x in array:
        counts[x] += 1
    for k_i, k in enumerate(array):
        ways = 0
        for x, count in counts.items():
            adjusted_count = counts[x]
            if x == k:
                adjusted_count -= 1
            ways += choose_combination(adjusted_count, 2)
        print(ways)


N = read_int()
array = read_array()
solve(N, array)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nums = list(map(int,input().split()))
counts = [0] * (N + 1)
ser = [0] * (N + 1)
for i in range(1,N+1):
    a = nums.count(i)
    ser[i] = a
    counts[i] = int(a*(a-1)/2)

for i in range(N):
    copy = counts[:]
    b = nums[i]
    copy[b] = 0
    c = ser[b] - 1
    S = sum(copy) + int(c*(c-1)/2)
    print(S)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int , input().split()))
a = collections.Counter(A)
for i in range(N):
    K =[]
    K.extend(A)
    K.pop(i)
    a = collections.Counter(K)
    S = len(list(set(K)))
    k = 0
    for j in range (S):
        b =a.most_common()[j][1]
        k = b * (b-1)/2 + k
    print(int(k))
    K.clear()
    K.extend(A)

Submission status: Time Limit Exceeded

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

seen = [0]*n

for i in range(n):
  seen[a[i]-1] =  seen[a[i]-1] + 1

for i in range(n):
   if not ((seen[i] == 0) or (seen[i] == 1)):
      base.append([i+1,seen[i]])

for i in range(n):
  count = 0
  for temp in base:
    now = temp[1]
    if temp[0] == a[i]:
      now = now - 1
      
    count = count + now*(now-1)//2
    
  print(int(count))

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

num = int(input())
ball_original = list(map(int, input().split()))
ans = 0

for i in range(num):
    ball = copy.copy(ball_original)
    ball.pop(i)
    c = collections.Counter(ball)
    values, counts = zip(*c.most_common())
    
    for j in range(len(counts)):
        ans = ans + int(counts[j]*(counts[j]-1)/2)
        
    print(ans)
    
    ans = 0

Submission status: Time Limit Exceeded

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

target = [i for i in list(set(ao)) if ao.count(i) != 1]
aval = []
acount = []
for i in range(n):
    try:
        ind = aval.index(i)
    except:
        a = ao[0:i] + ao[i+1:n]
        count = 0
        for t in target:
            tmp = a.count(t)
            count += sum([i for i in range(tmp-1, 0, -1)])
        print(count)
        aval.append(i)
        acount.append(count)
    else:
        print(acount[ind])

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    
    N = int(input())
    A = list(map(int, sys.stdin.readline().split()))
    
    set_a = set(A)
    
    d = {}
    comb_sum = 0
    
    for i in set_a:
        n = A.count(i)
        if n == 1:
            comb = 0
            comb_ex = 0
        else:
            comb = n * (n - 1) / 2
            comb_ex = (n - 1) * (n - 2) / 2
        comb_sum += comb
        d[i] = comb_ex - comb
     
    for n in A:
        ans = int(d[n] + comb_sum)
        print(ans)
      
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

a = list(set(A))
cc = Counter(A)
dup = [x for x in a if cc[x] > 1]
m = len(a)
l = len(A) 
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

for k in A:
    if k not in dup:
        print(int(al))
    else :
        print(int(al-cc[k]+1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
import collections

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

for i in range(n):
    _a = np.delete(a, i)
    c_dict = collections.Counter(_a)
    total = 0
    for v in c_dict.values():
        if v >= 2:
            total += v * (v - 1) / 2
    print(int(total))

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
A = [int(s) for s in input().split()]
B = [0 for i in range(N)]
idx = []
for i in range(N):
  B[A[i]-1] += 1
  if B[A[i]-1] >= 2 and not A[i]-1 in idx:
    idx.append(A[i]-1)

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

Cs = []
C = []
for j in idx:
  if B[j]-1 >= 2:
    Cs.append(nCr(B[j]-1, 2))
  else:
    Cs.append(0)
  C.append(nCr(B[j], 2))
Ctot = sum(C)

for i in range(N):
  current_idx = A[i]-1
  count = Ctot
  for k, j in enumerate(idx):
    if j == current_idx:
      count += Cs[k]
      count -= C[k]
  print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

buff = ""
for i in A:
    cmb = 0
    for j in cdic:
        if i == j and cdic[j] > 0:
            cmb += cdic[j] - (ndic[j] - 1)
        else:
            cmb += cdic[j]
    buff += str(cmb) + "\n"

print(buff)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
import copy
n = int(input())
l = list(map(int,input().split()))
for i in range(n):
    a = copy.deepcopy(l)
    del a[i]
    #print(a)
    a_set = set(a)
    ln = max(a_set)
    ans_list = [0 for j in range(ln)]
    for k in range(n-1):
        if a[k] in a:
            ans_list[a[k]-1] += 1
    ans = 0
    for x in range(ln):
        ans += (ans_list[x] * ((ans_list[x])-1)) // 2
    print(ans)

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 = list(map(int, 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 bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())
##################################################
N = I()
A = LI()
_123 = [0]*N
_123count = [0]*N
for x in A:
    _123[x-1] += 1
_123copy = copy.copy(_123)
_123copy.sort(reverse=True)
_count = {}
for i,x in enumerate(_123copy):
    if x==0:
        break
    if x==1:
        _123count[i] = 0
    elif i!=0 and _123copy[i]==_123copy[i-1]:
        _123count[i] = _123count[i-1]
    else:
        _123count[i] = math.factorial(x)//((math.factorial(x-2))*(math.factorial(2)))
        _count[x] = _123count[i]
_sum = sum(_123count)
for x in A:
    temp = _123[x-1]-1
    if temp<=0:
        print(_sum)
    elif temp==1:
        print(_sum-1)
    else:
        if (temp+1) in _count:
            tempminus = _count[temp+1]
        else:
            tempminus = math.factorial(temp+1)//((math.factorial(temp-1))*(math.factorial(2)))
            _count[temp+1] = tempminus
        if (temp) in _count:
            tempplus = _count[temp]
        else:
            tempplus = math.factorial(temp)//((math.factorial(temp-2))*(math.factorial(2)))
            _count[temp] = tempplus
        print(_sum-tempminus+tempplus)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(set(A))
dic = dict()
ans = 0
for i in range(len(B)):
    ans += A.count(B[i])*(A.count(B[i])-1)/2
    dic[B[i]]=A.count(B[i])
#print(dic)
    
for i in range(N):
         print(int(ans -dic[A[i]]*(dic[A[i]]-1)/2+(dic[A[i]]-1)*(dic[A[i]]-2)/2))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(a):
  kari1 = b[0:i]
  kari2 = b[i+1:a]
  c = kari1 +kari2
  c = collections.Counter(c)
  c = list(c.values())
  s = 0
  for k in range(len(c)):
    s += c[k]*(c[k]-1)/2
  print(int(s))

Submission status: Time Limit Exceeded

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

dic = {}
ans = 0
for t in A:
    if t in dic.keys():
        continue
    tmp = A.count(t)
    dic[t] = tmp
    ans += tmp*(tmp-1)//2

for a in A:
    val = dic.get(a)
    print(ans-val+1)

Submission status: Time Limit Exceeded

Solution code:
import collections
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()))

c = collections.Counter(A)
dict_ans = {}

for i in c.keys():
    target = c.copy()
    target[i] -= 1
    ans = 0
    for k in [j for j in target.values() if j >= 2]:
        ans += comb(k, 2)
    dict_ans[i] = ans

for i in A:
    print(dict_ans[i])

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
N=int(input())
A=list(map(int,input().split()))
for j in range(1,N+1):
    print(sum([ comb((A[:j-1]+A[j:]).count(i),2,exact=True) for i in range(1,N+1)]))

Submission status: Time Limit Exceeded

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

for i in range(N):
    flag = 0
    for j in range(len(T)):
        if A[i] == T[j][0]:
            T[j][1] += 1
            flag = 1
            break
    if flag == 0:
        T.append([A[i],1])

T2 = []

for i in range(len(T)):
    T2.append(T[i][1])


def hoge(List):
    ans = 0
    for i in range(len(List)):
        ans += (List[i]-1)*List[i]//2
    return ans

T3 = []

for i in range(len(T)):
    T2[i] -= 1
    T3.append(hoge(T2))
    T2[i] += 1

for i in range(N):
    for j in range(len(T)):
        if T[j][0] == A[i]:
            print(T3[j])
            break

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
def print_num(a):

    set_a = set(a)
    num = 0
    for targ_value in set_a:
        num_targ = a.count(targ_value)
        num += int(((num_targ)*(num_targ-1))/2)
    print(num)

if __name__ == "__main__":
    n = int(input())
    a = tuple(input().split())
    set_a = set(a)
    list_set = list(set_a)
    list_num_targ = [a.count(targ_value) for targ_value in set_a]
    list_c = [int(((num_targ)*(num_targ-1))/2) for num_targ in list_num_targ]
    all = sum(list_c)
    for targ_value in a:
        targ_index = list_set.index(targ_value)
        kazu = list_num_targ[targ_index]
        targ_sum = int((list_c[targ_index] * (kazu-2) / kazu))
        print(all - list_c[targ_index] + targ_sum)

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  #import collections
  #c = collections.Counter
  j = 0
  for i in range(len(s)):
    j += int(a.count(s[i])*(a.count(s[i])-1)/2)
  #print(j)
  for i in range(n):
    if a.count(a[i]) >= 2:
      print(int(j-(a.count(a[i])-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int, input().split()))
set_of_numbers = set(P)
# num_dict = {}
# num_dict_k = {}
diff = {}
result_sum_all = 0
for num in set_of_numbers:
    num_of_num = P.count(num)
    pair_set = int(num_of_num*(num_of_num-1)/2)
    pair_set_k = int((num_of_num-1)*(num_of_num-2)/2)
    # num_dict[num] = P.count(num)
    result_sum_all += pair_set
    diff[num] = pair_set_k - pair_set
    # num_dict[num] = int(pair_set)
    # num_dict_k[num] = int(pair_set_k)
    # print(pair_set, pair_set_k)


for k in range(N):
    # 取り除く数字はP[k]
    result = result_sum_all + diff[P[k]]# + num_dict_k[P[k]]
    print(result)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c=collections.Counter(A)
kazu=list(c.values())
kazu1=[i*(i-1)/2 for i in kazu if i>1]

for k in range(N):
    sub=c[A[k]]-1
    print(int(sum(kazu1)-sub))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

n = int(input())
a_list = list(map(int, input().split()))
a_col = collections.Counter(a_list)
c_dic = {}
sm = sum([c * (c - 1) for c in a_col.values()]) // 2

for a in a_list:
    if a in c_dic:
        print(c_dic[a])
    else:
        ans = sm - combinations_count(a_col[a], 2) + combinations_count(a_col[a] - 1, 2)
        print(ans)
        c_dic[a] = ans

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
numlist = list(map(int, input().split()))
dic = {}
for num in numlist:
  if num not in dic:
    dic[num] = 1
  else:
    dic[num] += 1
    
ansdick = {}
for key in dic.keys():
  n = dic[key]
  ansdick[key] = [int(n*(n-1)/2),int((n-1)*(n-2)/2)]
      
for num in numlist:
  sum = 0
  for key in dic.keys():
    if num == key:
      sum += ansdick[key][1]
    else:
      sum += ansdick[key][0]
  print(sum)

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] = math.factorial(v) // (math.factorial(v - 2) * math.factorial(2))
    else:
        A_sum_of_combination[k] = 0
    
    if (v-1) >= 2:
        A_sum_of_combination_minus1[k] = math.factorial(v-1) // (math.factorial((v-1) - 2) * math.factorial(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:
from operator import mul
from functools import reduce

def cmb(n):
    r = min(n-2,2)
    over = reduce(mul, [n,n-1])
    under = reduce(mul, [1,2])
    return over // under

def main():
  import math
  import collections
  import sys
  input = sys.stdin.readline
  m = {}
  n = int(input())
  a = list(map(int,input().split()))
  c = collections.Counter(a)
  ab = range(n)
  if a==ab:
    print("\n".join([0]*n))
    sys.exit()
  for k in a:
    if k in m:
      print(m[k])
    else:
      c[k] -= 1
      ans = 0
      for asdf in c.values():
        if asdf > 1:
          ans+= cmb(asdf)
      m[k] = ans
      c[k] += 1
      print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# import numpy as np
def resolve():
    N = ir()
    # A = np.array(lr(), np.int32)
    A = lr()
    A = [[A[i], i] for i in range(N)]
    A.append([2*10**5+1, N+1])
    A = sorted(A, key=lambda x: x[0])
    c = []
    d = []
    e = []
    tmp = 1
    st = 0
    for i in range(N+1):
        if tmp == A[i][0]:
            st += 1
        else:
            if st > 1:
                c.append(tmp)
                d.append(st)
                e.append(st*(st-1)//2)
            else:
                c.append(tmp)
                d.append(st)
                e.append(0)
            tmp = A[i][0]
            st = 1
    ans = []
    tmp = 0
    # print(c, d, e)
    for i in range(N):
        if A[i][0] > c[tmp]:
            tmp += 1
        num = d[tmp]
        plu = 0
        if num > 2:
            plu = (num-1)*(num-2)//2
        ans.append([sum(e)-e[tmp]+plu, A[i][1]])
    # print(ans)
    # print(iA)
    ans = sorted(ans, key=lambda x: x[1])
    for i in range(N):
        print(ans[i][0])

resolve()

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for k in range(n):
    test = []
    for j in range(n):
        if j == k: continue
        test.append(a[j])
        
    test.sort()
    ans = 0
    cnt = 1
    for i in range(len(test)-1):
        if test[i] == test[i+1]:
            ans += cnt
            cnt += 1
        else:
            cnt = 1
    print(ans)

Submission status: Time Limit Exceeded

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_sort = sorted(a)

    ball_dict = {}
    count = 1
    for i in range(1, n):
        if a_sort[i] == a_sort[i - 1]:
            count += 1
        else:
            ball_dict.setdefault(a_sort[i - 1], count)
            count = 1
    ball_dict.setdefault(a_sort[n - 1], count)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
l = list(map(int,input().split()))
c = collections.Counter(l)
li = []
for k,v in c.items():
    li.append(v*(v-1)//2)
    
for i in range(N):
  s = sum(li) - c[l[i]] + 1
  print(s)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


n = int(input())
a = [int(x) for x in input().split()]
nmap = {}


def ncr(n, r):
    if n - r >= 0:
        return factorial(n) // factorial(r) // factorial(n - r)
    else:
        return 0


at = sorted(list(set(a)))
for i in at:
    al = [x for x in a if x == i]
    nmap[str(i)] = ncr(len(al) - 1, 2)
    nmap[str(i) + 'a'] = ncr(len(al), 2)

for i in a:
    result = 0
    for j in at:
        if j == i:
            result += nmap[str(j)]
        else:
            result += nmap[str(j) + 'a']
    print(result)

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import deque
  from collections import defaultdict
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = defaultdict(int)
  e = defaultdict(int)
  
  for x in a:
    d[x] += 1
  
  for x in a:
    get = e.get(x)
    if get == None:
      g = d[x] - 1
      h = [v for k,v in d.items() if (k != x and v>=2)]
      h.append(g)
      i = list(map(func, h))
      ans = sum(i)
      e[x] = ans
      print(ans)
    else:
      print(get)
    


    
def func(x):
  if x < 2:
    return 0
  else:
    ans = x*(x-1)//2
    return ans 
      
 
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
s_A = set(A)
d = {}
for a in s_A:
    tmp = 0
    for item in c.items():
        if item[0] == a:
            if item[1] > 1:
                    tmp += int((c[a] - 1 ) * (c[a] - 2) / 2)
            continue
        if item[1] > 1:
            tmp += int(item[1] * (item[1] - 1) / 2)
    d[a] = tmp

for a in A:
    print(d[a])

Submission status: Time Limit Exceeded

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

for i in range(N):
  a = []
  for j in range(N):
    if j != i:
      a.append(A[j])
  #print('=======',a)
  c = collections.Counter(a)
  ans = 0
  for k in c.values():
    #print(k)
    if k > 1:
      ans += math.factorial(k) // (math.factorial(k-2) * math.factorial(2))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
al = [int(x) for x in input().split()]
aset = set(al)

# {number: count}
counts_d = {i: sum(j == i for j in al) for i in range(1, n + 1) if i in aset}

whole_patterns = 0
for v in counts_d.values():
    whole_patterns += int(v * (v - 1) / 2)

for a in al:
    print(whole_patterns - counts_d[a] + 1)

Submission status: Time Limit Exceeded

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

ct = [0] * (2*10**5+1)
ans = [0] * (n+1)

for i in range(len(la)):
    ct[la[i]] += 1

sa = set(la)

for j in sa:
    ans[j] = ct[j] * (ct[j]-1) // 2

# print(ct[0:10])
# print(ans)

for k in range(n):
    knum = la[k]
    ans[knum] = (ct[knum] - 1) * (ct[knum] - 2) // 2
    print(sum(ans))
    ans[knum] = (ct[knum] - 1) * ct[knum] // 2

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

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

for i in range(len(N_list)):
    count = 0
    N_copy = N_list.copy()
    N_copy.pop(i)
    num_list = list(itertools.combinations(N_copy,2))
    for i,j in num_list:
        if i==j:
            count+=1
    print(count)

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 = list(map(int, input().split()))
  temp = l
  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()