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())
data = [i for i in input().split()]

number_count = {}
for cmp in data:
    if cmp in number_count:
        number_count[cmp] += 1
        continue
    number_count[cmp] = 1

answer = {}
for i in range(N):
    if data[i] in answer:
        print(answer[data[i]])
        continue
    C = 0
    number_count_temp = number_count.copy()
    number_count_temp[str(data[i])] -= 1
    for cmp in number_count_temp:
        C += int(number_count_temp[cmp] * (number_count_temp[cmp] - 1) / 2)

    print(str(C))
    answer[data[i]] = str(C)

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 half(num):
        return num / 2
    def double(num):
        return num * 2
    A = [half(x) for x in A]
    A = [double(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:
n = int(input())
A = list(map(int, input().split()))
ansList = []

for i in range(n):
    ans = 0

    if A[i] in A[0:i - 1] and i != 0:
        num = A.index(A[i])
        ans = ansList[num]
    else:
        At = A[:]
        At.pop(i)

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

            while s in At:
                At.remove(s)

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

    ansList.append(ans)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
c = [A.count(i + 1) for i in range(N)]
ans = [c[i] * (c[i] - 1) // 2 for i in range(N)]
s = sum(ans)

for val in A:
  m = c[val - 1] - 1
  print(s - m)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
A = [int(n) for n in input().split()]
dp = {}
for k in range(1,N+1):
    f = dp.setdefault(A[k-1], 0)
    if f==0:
        a = [n for j, n in enumerate(A) if j!=k-1]
        ans = sum([1 if c[0]==c[1] else 0 for c in combinations(a, 2)])
        print(ans)
        dp[A[k-1]] = ans
    else:
        print(dp[A[k-1]])

Submission status: Time Limit Exceeded

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

nums = []
count = {}
for a_i in a:
    if not a_i in nums:
        nums.append(a_i)
        count[a_i] = 1
    else:
        count[a_i] = count[a_i] + 1

count = {k:v for k, v in count.items() if v > 1}

for a_i in a:
    choice = 0
    for k, v in count.items():
        tmp_v = v if not k == a_i else v -1
        if tmp_v > 1:
            choice += (tmp_v * (tmp_v - 1)/2)
    print(int(choice))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

divided = []
ans = 0
for i in range(N):
    count = A_list.count(i + 1)
    divided.append(count)
    ans += len(list(itertools.combinations(range(count), 2)))

for k in range(1, N + 1):
    n = divided[A_list[k - 1] - 1]
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

Submission status: Time Limit Exceeded

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

counts = Counter(A)

for i in A:
    ans = 0
    for n, count in counts.items():
        temp = count
        if n == i:
            temp -= 1
        if temp < 2:
            continue
        ans += len(list(itertools.combinations(range(temp), 2)))
    print(ans)

Submission status: Time Limit Exceeded

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

comb_dic = {}

def combination_formula_MOD(n, r, MOD):
    """nCrを O(min(n-r, r)) で高速に計算する"""
    def _inv(x, MOD):
        """xの逆元を返す"""
        return pow(x, MOD-2, MOD)
    if r > n: return 0

    if (n,r) in comb_dic:
        return comb_dic[(n,r)]

    r = min(n-r, r)
    bunsi, bunbo = 1, 1
    for i in range(r):
        bunsi = bunsi*(n-i)%MOD
        bunbo = bunbo*(i+1)%MOD

    ans = (bunsi*_inv(bunbo, MOD))%MOD
    comb_dic[(n,r)] = ans
    return ans


def combination_formula(n, r):
    """nCrは、O(min(n-r, r))で実装する
    Notes:
        分子と分母がかなり大きな値になった場合、計算は遅くなるので注意
        求める値がmodをとった値でいい場合、フェルマーの小定理を使った方法が速い。
    """
    if r > n: return 0

    if (n,r) in comb_dic:
        return comb_dic[(n,r)]

    r = min(n-r, r)
    bunsi, bunbo = 1, 1
    for i in range(r):
        bunsi = bunsi*(n-i)
        bunbo = bunbo*(i+1)

    comb_dic[(n,r)] = bunsi//bunbo
    return bunsi//bunbo


def solve():
    N = int(input())
    As = list(map(int, input().split()))
    MOD = 10**9+7

    dic = {}
    for a in As:
        if not a in dic:
            dic[a] = 0
        dic[a] += 1

    ans_dic = {}  # すでに答えを計算したもの
    for a in As:
        if not a in ans_dic:
            ans = 0
            for key, value in dic.items():
                if key == a:
                    value -= 1
                ans += combination_formula(value, 2)
                # ans += combination_formula_MOD(value, 2, MOD)
            ans_dic[a] = ans
            print(ans_dic[a])
        else:
            print(ans_dic[a])



if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import math
 


def nC2(i):
    if i<=1:
        return 0
    return i*(i-1)//2

def compute(count):
    total = 0
    for i in count:
        total += nC2(i)
    return total


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

count = [0] * (N+1)

for value in data:
    count[value] += 1
    

for i in range(len(data)):
    count[data[i]] -= 1
    
    total = compute(count)
    print(total)
          
          
    count[data[i]] += 1

Submission status: Time Limit Exceeded

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

from collections import Counter
d=Counter(a)
f={}
for k,v in d.items():
    f[k]=v*(v-1)/2

ans={}
for k in f.keys():
    ans[k]=(d[k]-1)*(d[k]-2)/2
    for v in f.values():
        ans[k]+=v
    ans[k]-=f[k]

for i in range(n):
    print(int(ans[a[i]]))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split(" ")))

    total = 0
    c_list = []
    a_count_list = []
    for i in range(n):
        count = a.count(i + 1)
        a_count_list.append(count)
        # print(count)
        if count > 1: # 2個以上
            # c = combinations_count(count, 2)
            c = count * (count - 1) // 2
        else:
            c = 0

        c_list.append(c)
        total = total + c

    # print(a, c_list, total, a_count_list)
    for k in range(n):
        if c_list[a[k] - 1] == 1:
            q = 1
        elif c_list[a[k] - 1] == 0:
            q = 0
        else:
            # q = c_list[a[k] - 1] - 1
            q = c_list[a[k] - 1] - c_list[a[k] - 1] * (a_count_list[a[k] - 1] - 2) // a_count_list[a[k] - 1]
        print(total - q)

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools


def main():
    n = int(input())
    a = input().split(" ")
    for i, j in enumerate(a):
        copied = copy.copy(a)
        del copied[i]
        print(len([k for k in itertools.combinations(copied, 2) if k[0] == k[1]]))


main()

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(x) for x in input().split()]

from collections import Counter

for k in range(n):
    m = l[:]
    m.pop(k)
    c = Counter(m)

    x = list(c.values())

    y = []
    for i in x:
        if i != 1:
            y.append(i)

    from math import factorial
    def combinations_count(n, r):
        return factorial(n) // (factorial(n - r) * factorial(r))
    
    z = []
    for j in y:
        z.append(combinations_count(j, 2))
    print(sum(z))

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
n=int(input())
A = list(map(int, input().split()))
res=[0]*n
import collections
def com(n, r):
    if n<r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i in range(n):
    ac=copy.deepcopy(A)
    del ac[i]
    ak=collections.Counter(ac)
    aj=list( ak.values() )
    s=0
    for j in range(len(aj)):
        s=s+com(aj[j],2)
    res[i]=s
print(' '.join(map(str,res)))

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
sys.setrecursionlimit(10**8)

fact_table = [-1] * (2*10**5)

def factorial(n):

    if n == 1:
        return 1
    if fact_table[n] != -1:
        return fact_table[n]
    else:
        temp = 1
        for i in range(n,0,-1):
            if fact_table[i] != -1:
                temp = temp * fact_table[i]
                break
            else:
                temp = temp * i

        fact_table[n] = temp
        return fact_table[n]


def combi(n,r):
    #階乗が多すぎる！　階乗テーブルだ！　×××メモリーエラーになる 違う！階乗の再帰によるメモリ不足だ！
    if n < r:
        return 0
    return factorial(n) // (factorial(r) * factorial(n-r))

def main():
    #2*10**5程度なら全てリストに収まる！
    #各数値の数をカウント！　k番目の数値だけ-1！
    #そして組み合わせ計算だ！nC2！
    n = int(input())
    num_counter = [0] + [0]*n #1-indexed!
    lst = [0]
    for i in [int(l) for l in input().split()]:
        t = i
        lst.append(t)
        num_counter[t] += 1



    for i in range(1,n+1):
        total = 0
        k = lst[i]
        num_counter[k] -= 1
        for j in num_counter:
            total += combi(j,2)

        num_counter[k] += 1
        print(total)




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools


def main():
    N = int(input())
    As = list(map(int, input().split()))

    for k in range(N):
        indices = list(range(N))
        del indices[k]
        ans = 0
        for i, j in itertools.combinations(indices, 2):
            if As[i] == As[j]:
                ans += 1
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def comb(n, r):
    if (n-r) >= 0:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        return 0
for i in range(len(A)):
    ans=0
    a = copy.copy(A)
    a.pop(i)
    c = collections.Counter(a)
    for j in c.values():
        ans += comb(j,2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import _pickle

if __name__ == "__main__":
    N = int(input())
    A = input().split()
    for x in range(N):
        tmp = _pickle.loads(_pickle.dumps(A, -1))
        del tmp[x]
        result = list(filter(lambda t: t[0] == t[1], list(itertools.combinations(tmp, 2))))
        print(len(result))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = [int(_) for _ in input().split()]
setnum = set(numlist)
dicnum = {}
nsum = 0

for i in setnum:
    dicnum[i] = numlist.count(i)
    if dicnum[i] >= 2:
        nsum += dicnum[i]*(dicnum[i]-1)//2
    
for j in range(n):
    ans = nsum
    cal = dicnum[numlist[j]]
    if cal >= 2:
        ans = ans - (cal-1)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np

N = int(input())
arr = np.array(list(map(int, input().split(" "))))
#val, count = np.unique(arr, return_counts=True)
count = np.bincount(arr)
conv_before = (count * (count - 1) / 2).astype(int)
conv_before_sum = np.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[k_val] + n_conv

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import copy
import math
def nP2(N):
    return (math.factorial(N)/((math.factorial(2))*math.factorial(N-2)))
L = list(map(int,input().split()))
Count = [0 for _ in range(N+1)]
for el in L:
    Count[el]+=1
for balls in L:
    res = 0
    Count[balls]-=1
    for i in Count:
        if i >= 2:
            res += nP2(i)
    Count[balls] += 1
    print(int(res))

Submission status: Time Limit Exceeded

Solution code:
# cook your dish here
from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
    return list(map(int, stdin.readline().split()))

def In():
    return map(int, stdin.readline().split())

def I():
    return int(stdin.readline())

P = 1000000007
n = I()
lis = L()
s = list(set(lis))
s. sort()
if len(s) == n:
    for i in range(n):
        print(0)
else:
    arr = [0 for i in range(len(s))]
    for i in lis:
        x = s.index(i)
        arr[x] += 1
    sm = 0
    for i in arr:
        sm += (((i)*(i-1))//2)
    arr2 = []
    for i in range(len(s)):
        v = arr[i]
        val = sm - ((((v)*(v-1))//2)) + (((v-2)*(v-1))//2)
        arr2.append(val)
        
    for i in lis:
        x = s.index(i)
        v = arr2[x]
        print(v)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
def main():
    n = int(input())
    As = input().split(" ")
    partial = reversed(list(itertools.combinations(As, r=n-1)))
    for As_tmp in partial:
        #print(As_tmp)
        a = np.array(list(itertools.combinations(As_tmp, 2)))
        print(np.count_nonzero([a[:,0] == a[:,1]]))
        #b = map(lambda x: len(set(x))-2, a)
        #ans = - sum(b)
        #print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from functools import lru_cache

@lru_cache(maxsize=None)
def cmbcount(n):
    if n > 1:
        return factorial(n) // (factorial(n - 2) * factorial(2))
    return 0

N = int(input())
A = list(map(int, input().split()))
samecnt = [0] * (N + 1)
for a in A:
    samecnt[a] += 1
cnts = [cmbcount(n) for n in samecnt]
allcnt = sum(cnts)
for i in range(N):
    a = A[i]
    o = cnts[a]
    n = cmbcount(samecnt[a] - 1)
    ans = allcnt - (o - n)
    print(ans)

Submission status: Time Limit Exceeded

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

for k in range(len(A)):
    ans=0
    A_c=A[:]
    A_c.pop(k)
    for i in range(len(A_c)-1):
        for j in range(i,len(A_c)-1):
            if A_c[i]==A_c[j+1]:
                ans+=1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
k=0
b=sorted(a)
c=collections.Counter(a)
for i in range(b[-1]):
  v=c[i+1]
  k+=v*(v-1)//2
for i in a:
  ans=k-a.count(i)+1
  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)
#print(c)

csum = 0
for i in c.values():
    if i == 1:
        csum += 0
    else:
        csum += combinations_count(i, 2)

#print(csum)

#print(A)
for i in A:
    #print(c[i], i)
    if c[i] == 1:
        print(csum)
    elif c[i] == 2:
        print(csum-1)
    else:
        print(csum - combinations_count(c[i], 2) + combinations_count(c[i]-1, 2))

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
l = list(input().split())
ans = 0
for k in range(N):
  m = copy.deepcopy(l)
  m.remove(m[k])
  for x in set(m):
    ans += m.count(x)*(m.count(x)-1)/2
  print(int(ans))
  ans = 0

Submission status: Time Limit Exceeded

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

def main():
    n = int(input())
    lis = tuple(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:
n = int(input())
l = list(map(int, input().split()))
nl = {}
cal = {}
kal = {}
cl = {}
for num in l:
  if num in nl:
    nl[num] = nl[num] + 1
  else:
    nl[num] = 1
for n in nl:
  if nl[n] > 1:
    cal[n] = nl[n] * (nl[n]-1) / 2
  else:
    cal[n] = 0
  if nl[n]-1 > 1:
    kal[n] = (nl[n]-1) * (nl[n]-2) / 2
  else:
    kal[n] = 0
for n in nl:
  cnt = 0
  for m in nl:
    if n == m:
      cnt += kal[m]
    else:
      cnt += cal[m]
  cl[n] = int(cnt)
for i in l:
  print(cl[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

def cmb(n, r):
    num = 1
    for i in range(1, r + 1):
        num = num * (n - i + 1) // i
    return num

n = int(input())
a = list(map(int, input().split()))
nn = len(a) - 1
ctr = Counter(a)

f = {}

for aa in a:
    x = 0
    for k, v in ctr.items():
        if k == aa:
            v -= 1
        if v >= 2:
            #c = f[v] // (2 * f[v - 2])
            x += cmb(v, 2)
    print(x)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
from operator import mul
from functools import reduce


def cmb(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    elif r < 0:
        return 0
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


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

    for a in As:
        Bs = deepcopy(As)
        Bs.remove(a)
        b_set = set(Bs)
        ans = 0
        for b in b_set:
            a = cmb(Bs.count(b), 2)
            ans += a
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())

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

for i in range(N):
    a1 = copy.deepcopy(a)
    a1.remove(a1[i])
    res = 0

    for j in range(1, N + 1):
        m = a1.count(j)
        if m != 0:
          res += int(m * (m - 1) / 2)
    
    print(res)

Submission status: Time Limit Exceeded

Solution code:
def aC(a):
  if a == 0:
    return 0
  return a*(a-1)/2
 
N = input()
N = int(N)
A = list(map(int, input().split()))
  
for i in range(0,N):
  pre_A = A[:]
  pre_A.pop(i)
  ans = 0
  count = 0
  for i in range(1,N+1):
    aaa = pre_A.count(i)
    ans = ans + aC(aaa)
    count = count + aaa
    if count == N:
      break
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
L = Counter(A)
anss = {}
for k,v in L.items():
    L[k] -= 1
    anss[k] = sum([i*(i-1)//2 for i in list(L.values()) if i >= 2])
    L[k] += 1
for i in A:
    print(anss[i])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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


def solve(a):
    ar = np.array(a)
    u= np.unique(ar)
    c = 0
    for j in u:
      v = len(np.where(a==j)[0])
      c += int(v*(v-1)/2)
    print(c)

for k in range(N):
    p_A = A[:k] + A[k+1:]
    solve(p_A)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
  A=0
  lai = la[:]
  del lai[i]
  c = collections.Counter(lai)
  values, counts = zip(*c.most_common())
  j=0
  while counts[j] >= 2:
    A += comb(counts[j], 2)
    j += 1
    if j==len(counts):
      break
  print(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
listA = list(map(int,input().split()))
listA_hozon = listA

S = Counter(listA)
#print(S)

##key##
key = S.keys()
#for i in key:
#  print(i)

for i in listA:
  out = 0
  listA = listA_hozon
  for a in key:
    if a == i:
      num = S[a]-1
      add = (S[a]-1) * (S[a]-2) /2
      out = out + add
    else:
      num = S[a]
      add = S[a] * (S[a]-1) /2
      out = out +add
  print(int(out))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


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

def calc(a, i):
    _a = a[:i] + a[i+1:]
    set_a = set(_a)
    return sum([combinations_count(_a.count(s), 2) for s in set_a])
    

input()
s = list(map(int, input().split(' ')))

for output in [calc(s, i) for i in range(len(s))]:
    print(output)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = input().split()
for i in range(n):
  count = 0
  R = L[:i] + L[i+1:]
  S = list(set(R))
  r = len(R)
  for i in range(len(S)):
    a = R.count(S[i])
    count += a*(a-1)//2
  print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

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

ans_dict = {}
value_dict = {1: 0}
for i in range(n):
    if i in ans_dict.keys():
        print(ans_dict[i])
    else:
        buff = a_list[:i]
        buff.extend(a_list[i+1:])
        c = Counter(buff)

        tmp_ans = 0
        for value in c.values():
            if value in value_dict.keys():
                tmp_ans += value_dict[value]
            else:
                tmp = (value * (value - 1))/2
                tmp_ans += tmp
                value_dict[value] = tmp
        ans_dict[i] = tmp_ans
        print(int(tmp_ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

N = input()
L = input().split()
C = collections.Counter(L)
I = list(C.keys())
V = list(C.values())
result = []
result2 = []
sum = 0

for i in range(len(I)):
    sum += V[i] * (V[i] - 1) / 2
    result.append(V[i] * (V[i] - 1) / 2)
    result2.append((V[i] - 1) * (V[i] - 2) / 2)

for l in L:
    print(int(sum - result[I.index(l)] + result2[I.index(l)]))

Submission status: Time Limit Exceeded

Solution code:
import collections
from sys import stdin

def counter(num_arr):
    uniq_dict = {}
    for val in num_arr:
        uniq_dict[val] = uniq_dict.get(val, 0) + 1
    return uniq_dict


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


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

Submission status: Time Limit Exceeded

Solution code:
# -*- 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
      
for num in numlist:
  sum = 0
  for key in dic.keys():
    n = dic[key]
    if num == key:
      n -= 1
    sum += int(n*(n-1)/2)
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input(''))
stra = input('')

alist = stra.split(' ')
dic = {}
for i in range(0,len(alist)):
  if alist[i] in dic.keys():
    dic[alist[i]]+=1
  else:
    dic[alist[i]] = 1

for k in range(0,n):
  cnt = 0
  key = alist[k]
  for k,v in dic.items():
    if k== key:
      cnt += ((v-1)*(v-2))/2
    else:
      cnt += (v*(v-1))/2
  print(int(cnt))

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

comon = c.most_common()
ans = 0

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

Submission status: Time Limit Exceeded

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

counts = {}
for i in range(N):
    a = A[i]
    if a in counts:
        counts[a] += 1
        continue
    counts[a] = 1

# print(counts)

ans_memory = {}
for k in range(N):
    if A[k] in ans_memory:
        print(ans_memory[A[k]])
        continue

    counts_tmp = counts.copy()
    counts_tmp[A[k]] -= 1

    # print(counts_tmp)
    ans = 0
    for v in counts_tmp.values():
        ans += v * (v - 1) // 2
    ans_memory[A[k]] = ans
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy, math

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

result = []

num_dict = {}

for x in set(a):
    num_dict[x] = a.count(x)

def calc(x):
    if x >= 2:
        return math.factorial(x) / (math.factorial(x - 2) * 2)
    else:
        return 0

result_dict = {}
result_minus1_dict = {}

for x in set(a):
    result_dict[x] = calc(num_dict[x])
    result_minus1_dict[x] = calc(num_dict[x] - 1)

for i in range(len(a)):
    result = 0

    for key, value in result_dict.items():
        if key == a[i]:
            result += result_minus1_dict[a[i]]
        else:
            result += value

    print(int(result))

Submission status: Time Limit Exceeded

Solution code:
def main():
  from functools import reduce
  from operator import mul
  
  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 f in d.keys():
    g = d[f] - 1
    h = [v for k,v in d.items() if (k != f and v>=2)]
    h.append(g)
    i = list(map(func, h))
    ans = sum(i)
    
    e[f] = ans
    
  for x in a:
    ans = e[x]
    print(ans)

    
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:
N = int(input())
A = list(map(int, input().split()))
A_set = list(set(A))
Ad = {a_set: A.count(a_set) for a_set in A_set}
score_sum = sum([(x>1) * x * (x-1) / 2 for x in list(Ad.values())])
Ad_score = {ad_key: score_sum - (ad_value -1) * (ad_value > 0) for ad_key, ad_value in Ad.items()}
A_out = [str(int(Ad_score[a])) for a in A]

print('\n'.join(A_out))

Submission status: Time Limit Exceeded

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

n = int(input())
A = [int(i) for i in input().split()]
d = {}

As = set(A)
for x in As:
    d.update({x: A.count(x)})

summ = 0
for x in As:
    y = d.get(x)
    summ += y*(y-1) // 2

for a0 in A:
    print(summ - (d.get(a0)-1))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

N = int(input())
A = input().split()
B = {}
for i in range(0, N):
    if not(A[i] in B.keys()):
        B[A[i]] = 1
    else:
        B[A[i]] += 1 

for i in range(0, N):
    B[A[i]] -= 1
    S = 0    
    for value2 in B.values():
        S = S + comb(int(value2), 2, exact=True)
    print(S)
    B[A[i]] += 1

Submission status: Time Limit Exceeded

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


dict_A={}
B=list(set(A))
sum=0


for b in B:
    a=A.count(b)
    dict_A[b]=a-1
    sum+=a*(a-1)//2

for a in A:
    print(sum-dict_A[a])

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

for i in range(N):
    result = 0
    for k in dic.keys():
        if A[i] == k:
            result += (dic[k]-1)*(dic[k]-2)//2
        else:
            result += (dic[k])*(dic[k]-1)//2
    print(result)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
a_i = list(map(int, input().split()))
anslist = [0] * (num + 1)
printlist = [0] * (num + 1)
for x in range(num):
    val = x + 1
    cnt = a_i.count(val)
    if cnt <= 1: anslist[val] = 0
    else: anslist[val] = cnt * (cnt - 1) // 2

ans = sum(anslist)
for x in range(num):
    val = a_i[x]
    cnt = a_i.count(val)
    if printlist[val] != 0: 
        print(printlist[val])
        continue
    if cnt <= 1: print(ans)
    else: 
        sa = anslist[val] - (cnt - 1) * (cnt - 2) // 2
        print(ans - sa)
        printlist[val] = ans - sa

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np
from math import factorial

def num_choice_2(N):
    if N > 1:
        return N * (N - 1) // 2
    else:
        return 0

N = int(input())
A = [int(x) for x in input().split()]

nums = np.zeros(N, dtype=int)
for a_i in A:
    nums[a_i - 1] += 1

for i in range(N):
    nums[A[i] - 1] -= 1
    ans = np.sum([num_choice_2(x) for x in nums])
    nums[A[i] - 1] += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
N=int(input())
A=list(map(int, input().split()))
for k in range(N):
  cnt=0
  B=copy.copy(A)
  del B[k]
  C=[x for x in B if B.count(x) > 1]
  for s in set(C):
    m=C.count(s)
    cnt+=math.factorial(m)//(2*math.factorial((m-2)))
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import mul
import math
import copy
 
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 = int(input())
l = list(map(int, input().split()))
d = {}
s_sum = {}
s_ans = {}
s = list(set(l))
m = 0
for i in range(len(s)):
    v = l.count(s[i])
    d[s[i]] = l.count(s[i])
    if v > 1:
        a = cmb(v, 2)
        s_ans[s[i]] = a
        m += a
    else:
        s_ans[s[i]] = 0
for i in range(len(l)):
    if not d[l[i]] > 1:
        print(m)
    elif l[i] in s_sum:
        print(s_sum[l[i]])
    else:
        e = copy.deepcopy(d)
        e[l[i]] -= 1
        ans = m - s_ans[l[i]]
        if e[l[i]] > 1:
            ans += cmb(e[l[i]], 2)
        s_sum[l[i]] = ans
        print(ans)