Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

ai = [False for _ in range(n)]

ai = [int(s) for s in input().split()]

#print(ai)

dic = {}

for i in ai:
    if(i in dic):
        dic[i] += 1
    else:
        dic[i] = 1
    
for i in ai:
    dic[i] -= 1
    res = 0
    for j in dic:
        if(dic[j] < 2):
            continue
        else:
            res += int(dic[j]*(dic[j]-1)/2)
    
    print(res)
    dic[i] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())

As = list(map(lambda i: int(i), input().split()))

ns = [0 for i in range(N)]

for a in As:
    ns[a-1] += 1

def get_comb(arr):
    res = 0
    for a in arr:
        if a == 0:
            continue
        res += a*(a-1)/2
    return int(res)

# print(ns)
for a in As:
    tmp = copy.copy(ns)
    tmp[a-1] -= 1
    print(get_comb(tmp))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
ans = {}
for item in a:
    if item in dic.keys():
        dic[item] += 1
    else:
        dic[item] = 1

all_ans = sum([item * (item - 1) // 2 for item in dic.values()])

for i in range(n):
    print(all_ans - dic[a[i]] + 1)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

removes = {}

c = collections.Counter(A)
c2 = {}
for key in c:
    if c[key] > 1:
        c2[key] = c[key]

def comb(n):
    return (n * (n - 1)) // 2

# print(c2)
for k in range(N):
    i = A[k]
    if i not in removes:
        i_sum = 0
        for key in c2:
            if i == key:
                i_sum += comb(c2[key] - 1)
            else:
                i_sum += comb(c2[key])
        removes[i] = i_sum
    print(removes[i])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, A: "List[int]"):
    from collections import Counter
    import math
    from functools import lru_cache

    @lru_cache(None)
    def combination(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    c = Counter(A)

    dp = [-1] * (N+1)
    ans = []
    for i in range(N):
        if dp[A[i]] != -1:
            ans.append(dp[A[i]])
        else:
            t = 0
            for k,v in c.items():
                if k == A[i]:
                    if v-1 >= 2:
                        t += combination(v-1, 2)
                else:
                    if v >= 2:
                        t += combination(v, 2)
            ans.append(t)
            dp[A[i]] = t
 
    for i in ans:
        print(i)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import decimal
import heapq
import collections
import itertools

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())
d = getInputListInt()


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

dic = {}

for key in d:
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1

for i in range(n):
    ans=0
    tmp_key = d[i]
    for k in dic:
        if k == tmp_key:
            ans += combinations_count(dic[k]-1, 2)
        else:
            ans += combinations_count(dic[k], 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
ball = [int(i) for i in input().split()]
counter = Counter(ball)
for i in range(n):
  counter[ball[i]] -= 1
  result = 0
  print(sum([i * (i - 1) // 2 for i in counter.values()]))
  counter[ball[i]] += 1

Submission status: Time Limit Exceeded

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

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

ans={}
for i in set(a):
    temp=cnt[i]-1
    ans[i]=0
    for x,y in cnt.items():
        if x!=i and y>=2:
            ans[i]+=(y-1)*y//2
        elif x==i and temp>=2:
            ans[i]+=(temp-1)*temp//2

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

Submission status: Time Limit Exceeded

Solution code:
import math


n = int(input())
a = list(map(int, input().split()))
cnt = {}
for v in a:
    if v not in cnt:
        cnt[v] = 0
    cnt[v] += 1
ans = {}
for key1 in cnt.keys():
    s = 0
    for key2, value in cnt.items():
        if key1 == key2:
            value -= 1
        if value <= 1:
            continue
        s += math.factorial(value)//(2*math.factorial(value-2))
    ans[key1] = s
for v in a:
    print(ans[v])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int, input().split()))
freq_arr={}
ans=0
def foo(x):
  return (x*(x-1))//2
for i in set(l):
  freq_arr[i]=(l.count(i))
for i in freq_arr:
  ans+=foo(freq_arr[i])
#print(ans)
ans_arr={}
for i in freq_arr:
    ans_arr[i]=(ans-freq_arr[i]+1)
for i in l:
    print(ans_arr[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
As = list(map(int, input().split()))
for i in range(N):
  Bs = As[:]
  Bs.pop(i)
  c = Counter(Bs)
  ans = 0
  for con in c.most_common():
    if con[1]<2:
      break
    ans += (con[1]*(con[1]-1)/2)
  print(int(ans))

Submission status: Time Limit Exceeded

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

for k in range(N):
    ans = 0
    test = A_list[:k] + A_list[(k+1):]
    for i in set(test):
        tmp = [ix for ix, x in enumerate(test) if x == i]
        n = len(tmp)
        ans += n*(n-1)/2
    print(int(ans))

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

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

def count(index, l):
    counted = []
    duplication = []
    for x in l:
        if x in counted:
            continue
        if l.count(x) > 0:
            duplication.append(l.count(x))
            counted.append(x)
    ans = 0
    for x in duplication:
        ans += comb(x,2)
    print(ans)

for i in range(N):
    l = A[:]
    del l[i]
    count(i, l)

Submission status: Time Limit Exceeded

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

from collections import Counter
d=Counter(a)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A_array = list(map(int, input().split()))
Ai_dict = Counter(A_array)


def calc_comb(Ai, dictionary):
    num = 0
    for k,v in dictionary.items():
        if v>1:
            if k == Ai:
                N_Ai = v -1 #1こ抜いた数. 
            else:
                N_Ai = v
            num += N_Ai*(N_Ai-1)/2
    return int(num)


res_dict = dict([])

for n in A_array:
    if not n in res_dict.keys():
        res_dict[n] = calc_comb(n, Ai_dict)
        print(calc_comb(n, Ai_dict))
    else:
        print(res_dict[n])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

def fact(n):
    r = 2
    if n - r < 0:
        return 0
    return factorial(n) // factorial(r) // factorial(n - r)

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

counts = collections.Counter(BALLS)
for i in range(N):
    print(sum(fact(v if k != BALLS[i] else v - 1) for (k, v) in counts.items()))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
from functools import lru_cache

@lru_cache(maxsize = 64)
def C(n, r):
    if n-r < 0:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
d = {}
sm = 0
for k,v in c.most_common():
    d.update({k:v})
    sm += C(v,2)

for a in A:
    print (sm - C(d.get(a),2) + C(d.get(a)-1,2) )

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

# N = 5
# A = [1, 1, 2, 1, 2]

for i in range(N):
    # i = 2
    A_sub = A.copy()
    A_sub.pop(i)

    A_sub = np.array(A_sub)
    uni_vals = np.unique(A_sub)

    ans = 0
    for val in uni_vals:
        # val = uni_vals[0]
        n = np.where(A_sub == val)[0].shape[0]
        ans += (n*(n-1))/2

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

a_dict = {}
for a in a_list:
    if a in a_dict.keys():
        a_dict[a] += 1
    else:
        a_dict[a] = 1
        
for i in range(len(a_list)):
    a = a_list[i]
    temp_dict = copy.deepcopy(a_dict)
    temp_dict[a] -= 1
    answer = 0
    for key, value in temp_dict.items():
        if value >= 2:
            answer += value*(value-1)//2
    
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
def main():
  import collections
  x = int(input())
  num_list = list(map(int, input().split()))
  for i in range(x):
    set_list = num_list[:i] + num_list[i+1:]
    count = collections.Counter(set_list)
    score = 0
    for j in count.values():
      score += j * (j-1) / 2
    print(int(score))
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial


def choose(n: int) -> int:
    if n < 2:
        return 0
    else:
        return factorial(n) // (2 * factorial(n - 2))


N = int(input())
A = list(map(int, input().split()))
for k in range(N):
    c = Counter(A[:k] + A[k + 1:])

    print(sum(map(choose, c.values())))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
res = list(map(int,input().split()))
sums = 0
ans = []
for i in range(len(res)):
    popl = res.pop(i)
    a = Counter(res)
    sums = 0
    for j in a.values():
        sums += j * (j - 1) // 2
    ans.append(sums)
    res.insert(i,popl)
for k in ans:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

n = int(input())
al = [int(x) for x in input().split()]
cs_orig = collections.Counter(al)
copy_copy = copy.copy

for a in al:
    cs = copy_copy(cs_orig)
    patterns = 0
    cs[a] -= 1
    for count in cs.values():
        if count > 1:
            patterns += int(count * (count-1) / 2)

    print(patterns)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
L = Counter(A)
for i in A:
    ans = []
    L_cp = L.copy()
    L_cp[i] -= 1
    print(sum([i*(i-1)//2 for i in L_cp.values() if i >= 2]))

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
  if i < N - 1:
    A_ = A[:i]
    A_.extend(A[i+1:])
  else:
    A_ = A[:-1]
  c = collections.Counter(A_)
  nums = [c[c_] for c_ in c.keys() if c[c_]  > 1]
  lennum = 0
  for n in nums:
    lennum += n*(n-1) // 2
  print(lennum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())

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

count_array = np.zeros(len(a_list))

for num in a_list:
    count_array[num-1] += 1
for num in a_list:
    temp_array = count_array.copy()
    temp_array[num-1] -= 1
    ans =int((np.sum(temp_array * (temp_array-1)))/2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    c = Counter(A)
    for a in A:
        ans = 0
        for k, v in c.items():
            if k == a: 
                ans += len(list(combinations(range(v-1), 2)))
            else:
                ans += len(list(combinations(range(v), 2)))

        print(ans) 


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import copy

a = input()
a=int(a)
ball=[]

temp=input().split(" ")
for i in range(a):
    ball.append(int(temp[i]))
for i in range(a):
    balltemp=copy.copy(ball)
    del balltemp[i]
    retnum=0
    for j in range(len(balltemp)):
        for k in range(j+1,len(balltemp)):
            if balltemp[j]==balltemp[k]:
                retnum+=1
    print(retnum)

# スペース区切りの整数の入力

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(A)

def count(c, x):
    ret = 0
    for k, v in c.items():
        if k == x:
            v -= 1
        ret += v*(v-1)//2
    return ret

def count_list(c):
    keys = c.keys()
    ret = {}
    for key in keys:
        ret[key] = count(c, key)
    return ret

counts = count_list(c)
for i in range(N):
    print(counts[A[i]])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=set(A)
temp=0
s={}
for i in B:
    t=A.count(i)
    s[i]=t-1
    temp+=(t*(t-1))//2
for i in A:
    print(temp-s[i])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int, input().split()))
s=set(l)
d=dict()

for x in s:
    d[x] = l.count(x)

d2=dict()

for x in l:

    if x in d2.keys():
        print(d2[x])
    else:
        sum = 0
        for key in d.keys():
            a = d[key]
            if key == x:
                a -= 1

            sum += (a*(a - 1)) // 2

        print(sum)
        d2[x]=sum

Submission status: Time Limit Exceeded

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

import collections
c = collections.Counter(A)

ans = []

for x in A:
    _ans = 0
    d = c.copy()
    d[x] -= 1
    for j in d.values():
        _ans += j*(j-1)//2
    ans.append(_ans)

for y in ans:
    print(y)

Submission status: Time Limit Exceeded

Solution code:
def cycle(x):
    result = 0
    for y in range(int(x), 0, -1):
        result += y - 1
    return result


def main():
    N = int(input())
    a_list = list(map(int, input().split()))
    dict_a_list = dict()
    for a in a_list:
        if a in dict_a_list:
            dict_a_list[a] += 1
            continue
        dict_a_list[a] = 1
    for a in a_list:
        result = 0
        dict_a_list[a] -= 1
        for v in dict_a_list.values():
            result += cycle(v)
        print(result)
        dict_a_list[a] += 1


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
d = {}
for i in range(N):
  if A[i] in d:
    d[A[i]] += 1
  else:
    d[A[i]] = 1
#print(A, d)

for i in range(N):
  ans = 0
  for k, v in d.items():
    if k == A[i]:
      ans+= (v-1)*max(v-2,0)/2
    else:
      ans+= v * (v-1)/ 2
  print(int(ans))

Submission status: Time Limit Exceeded

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

def cmb(n, r):
    if n < 2:
        return 0
    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())
A = [int(x) for x in input().split()]
C = Counter(A)
ans = {}
for a, b in C.items():
    ans[a] = 0
    for i, j in C.items():
        if a == i:
            ans[a] += cmb(b-1, 2)
        else:
            ans[a] += cmb(j, 2)
for a in A:
    print(ans[a])

Submission status: Time Limit Exceeded

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

# ans = []
# for i in range(N):

tmp = sorted(A)
dic = {}
for t in tmp:
    dic[t] = tmp.count(t)

# print(dic)

for i in range(N):
    # tmp = dic.get(A[i])
    ans = 0
    for key in list(dic.keys()):
        val = dic.get(key)
        if key == A[i]:
            val -= 1
        # print(key)
        # print(val)
        # print("\t"+str(val * (val - 1) // 2))
        ans += val * (val - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter
B=Counter(A)

for k in range(N):
    C=B-Counter(A[k])
    ans=0
    for c in C.values():
        ans+=c*(c-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

    for i in range(N):
        cnt = 0

        c=Counter(A[:i]+A[i+1:])
        values,counts=zip(*c.most_common())
        for j in counts:
            if j>=2:
                cnt+=j*(j-1)//2
        print(cnt)



resolve()

Submission status: Time Limit Exceeded

Solution code:
def corp(i):
    return i*(i-1)//2

N=int(input())
A=list(map(int,input().split()))
box=[0]*N
for i in A:
    box[i-1]+=1

for i in A:
    sum=0
    for j in range(N):
        if j==i:
            sum+=corp(box[j-1]-1)
        else:
            sum+=corp(box[j-1])
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

n = int(input().strip())
a = list(map(int, input().split()))
b = deepcopy(a)
counts = {}
tmp = b[0]
for elem in b:
    if str(elem) in counts:
        counts[str(elem)] += 1
    else:
        counts[str(elem)] = 1
    if tmp != elem:
        tmp = elem
c = deepcopy(counts)
for k, v in counts.items():
    c[k] = v * (v - 1) // 2
for elem in a:
    print(sum(c.values()) - c[str(elem)] + ((counts[str(elem)] - 1) * (counts[str(elem)] - 2) // 2))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int, input().split()))

for k in range(N):
    hoge = P[:k] + P[k+1:]  
    set_of_numbers = set(hoge)
    result = 0
    for num in set_of_numbers:
        same_num = hoge.count(num)
        result += same_num*(same_num-1)
    print(int(result/2))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S = int(input())
As=input().split()
As=[int(x) for x in As]
total = sum([As.count(x) for x in As])-S
for x in range(S):
    n=As.count(As[x])-1
    print(int((total-n*2)/2))

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
a=list(input().split())
c=collections.Counter(a)
for i in range(n):
  ans=0
  for k,v in c.items():
    if a[i]==k:
      ans+=(v-1)*(v-2)//2
    else:
      ans+=v*(v-1)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

n = int(input())
l = list(map(int, input().split()))
d = {}
s_sum = {}
s = list(set(l))
for i in range(len(s)):
    d[s[i]] = l.count(s[i])
for i in range(len(l)):
    if l[i] in s_sum:
        print(s_sum[l[i]])
    else:
        ans = 0
        e = copy.deepcopy(d)
        e[l[i]] -= 1
        for v in e.values():
            if v > 1:
                ans += math.factorial(v) // math.factorial(v -
                                                           2) // math.factorial(2)
        s_sum[l[i]] = ans
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
n = input()
li = list(map(int,input().split()))
for i in range(len(li)):
    ans = 0
    li2 = copy.copy(li)
    li2[i] = 0
    c = collections.Counter(li2)
    c_key = list(c.values())
    for j in c_key:
        ans += ((j * (j-1)) // 2)
    print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter,defaultdict
n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
#print(cnt)

ans=defaultdict(lambda :0)
for i in set(a):
    temp=cnt[i]-1
    for x,y in cnt.items():
        if x!=i and y>=2:
            ans[i]+=(y-1)*y//2
        elif x==i and temp>=2:
            ans[i]+=(temp-1)*temp//2

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

Submission status: Time Limit Exceeded

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

from collections import defaultdict

cnt = defaultdict(int)
for i in range(n):
    cnt[a[i]] += 1
# print(cnt)

for key, val in list(cnt.items()):
    if val < 2:
        cnt.pop(key)
# print(cnt)

for i in range(n):
    cnt[a[i]] -= 1
    # print(cnt)
    ans = 0
    for val in cnt.values():
        if val >= 2:
            ans += val*(val-1)//2
    print(ans)
    cnt[a[i]] += 1

Submission status: Time Limit Exceeded

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

Lists=[]
for i in range(n):
    Lists.append(a.count(i+1))
#print(a)
#print(Lists)
for i in range(n):
    ans=0
    Lists[(a[i])-1]-=1
    for j in range(n):
        if ((Lists[j])>1):
            ans+=Lists[j]*(Lists[j]-1)//2
    Lists[a[i]-1]+=1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n, *D  = [list(map(int, s.split())) for s in open(0)]
D = D[0]
dic = defaultdict(int)
for d in D:
    dic[d] += 1

for d in D:
    dic[d] -= 1
    comb = 0
    for i in dic.values():
        if i >= 2:
            comb += i * (i - 1) / 2
    dic[d] += 1
    print(int(comb))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans=[0]*N
k=0
for i in range(N-1):
  for v in range(i+1,N):
    if A[i]==A[v]:
      ans[i]-=1
      ans[v]-=1
      k+=1
for i in range(N):
  ans[i]+=k
  print(ans[i])

Submission status: Time Limit Exceeded

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

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

for ai in a:
    ans = 0
    temp = copy.copy(c)
    temp[ai] = temp[ai] - 1
    for v in temp.values():
        # print(v)
        if v >= 2:
            ans += cmb(v, 2)
    print(ans)
temp = copy.copy(c)
temp[1] = temp[1] - 1
# print(c)
# print(temp)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.q = l
        heapq.heapify(self.q)
        return

    def push(self, n):
        heapq.heappush(self.q, n)
        return

    def pop(self):
        return heapq.heappop(self.q)


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)

sys.setrecursionlimit(100000)
nCr = {}


def cmb(n, r):
    if n < r:
        return 0
    if r == 0 or r == n:
        return 1
    if r == 1:
        return n
    if (n, r) in nCr:
        return nCr[(n, r)]
    nCr[(n, r)] = cmb(n-1, r) + cmb(n-1, r-1)
    return nCr[(n, r)]


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    A = Scanner.map_int()
    M = Counter(A)
    MAX = 0
    for v in M.values():
        MAX += cmb(v, 2)
    for i in range(N):
        ans = MAX - cmb(M[A[i]], 2) + cmb(M[A[i]] - 1, 2)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  ans = 0
  b = copy.copy(a)
  b.pop(i)
  c = list(set(b))
  for j in range(len(c)):
    d = b.count(c[j])
    ans += (d*(d-1))//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import math

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


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

D =defaultdict(int)

for i in A:
    D[i] += 1

for i in A:
    D[i] -= 1
    result = 0
    for k,v in D.items():
        result += combinations_count(v,2)
    print(result)
    D[i] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
ball = [int(i) for i in input().split()]
for i in range(n):
  counter = Counter(ball[:i]+ball[i+1:])
  result = 0
  print(sum([i * (i - 1) // 2 for i in counter.values()]))

Submission status: Time Limit Exceeded

Solution code:
import math

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

def add(num):
    n = 0
    for i in range(N):
        if num[i] >= 2:
            n = n + comb(num[i], 2)
    return n


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

num = [0] * N

for i in range(N):
    num[A[i] - 1] = num[A[i] - 1] + 1

for i in range(N):
    num[A[i] - 1] = num[A[i] - 1] - 1
    print(add(num))
    num[A[i] - 1] = num[A[i] - 1] + 1

Submission status: Time Limit Exceeded

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

for i in range(n):
	k = list[i]
	list2.append(list.count(k)-1)
su = sum(list2)/2
for i in range(n):
	print(int(su-list2[i]))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(len(a)):
    ans = 0
    for item, value in c.items():
        if item == a[i]:
            value -= 1
        ans += value * (value-1) // 2

    print(ans)