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:
import collections
import math

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

    Cou = collections.Counter(A)
    ans = 0

    oo = 0
    for c in Cou.values():
        oo += c * (c-1) // 2
    #print(Cou)
    for k in range(1, N+1):
        tmp = Cou.copy()
        c = tmp[A[k-1]] - 1

        print(oo - c)



    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
N = int(input())
l = list(input().split())

dic = collections.Counter(l)
for i in range(N):
    dic[l[i]] -= 1
    a = 0
    for ll in dic.values():
        if int(ll)>1:
            a += ll*(ll-1)/2
    print(str(int(a)))
    # print(str(a_k))
    # print(str(dic))

    dic[l[i]] += 1

Submission status: Time Limit Exceeded

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

af=collections.Counter(A)
res2=list(af.items())
res3=[0]*n
s=0
for i in range(0,len(res2)):
    s=s+com(res2[i][1],2)
for j in range(0,len(res2)):
    res3[res2[j][0]-1]=s-com(res2[j][1],2)+com(res2[j][1]-1,2)
for i in range(n):
    res[i]=res3[A[i]-1]

for j in range(n):
    print(res[j])

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
n = int(input())
items = tuple(map(int, sys.stdin.readline().rstrip().split()))


com = dict()
sumc = 0
que = deque([])
seen = set()
for i in items:
    if i not in com:
        nn = items.count(i)
        com[i] = nn

    else:

        nn = com[i]
    que.append(nn)
    if i in seen:
        continue
    seen.add(i)
    sumc += nn*(nn-1)//2

while que:
    q = que.popleft()
    if q <2:
        print(sumc-q*(q-1)//2)
    else:
        print(sumc-q+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

S = Counter(listA)

##key##
key = S.keys()

##dict##
#my_dict = {}  

list_low = {}
list_high = {}

for i in key:
  num_low = (S[i]-1) * (S[i]-2) /2
  num_high =S[i] * (S[i]-1) /2
  list_low[i] = num_low
  list_high[i] = num_high

for i in listA:
  out =0
  for a in key:
    if i ==a:
      out = out + list_low[a]
    else:
      out = out + list_high[a]
  print(int(out))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nums = list(map(int,input().split()))
M = max(nums)
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)

ans = [0] * M
#print(ser)
for i in range(1,M+1):
    copy = counts[:]
    copy[i] = 0
    #print(ser[i])
    c = ser[i] - 1
    S = sum(copy) + int(c*(c-1)/2)
    ans[i-1] = S

#print(ans)
for j in range(N):
    print(ans[nums[j]-1])

Submission status: Time Limit Exceeded

Solution code:
import math


def ncr(n, k):
    if k > n:
        return 0
    return math.factorial(n) / math.factorial(n - k)


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    if i + 1 == n:
        a2 = a[:i]
    else:
        a2 = a[:i] + a[i+1:]
    c = 0
    s = set(a2)
    for i in s:
        c = c + int(ncr(a2.count(i), 2) // 2)
    print(c)

Submission status: Time Limit Exceeded

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

dict = {}
for i in range(N):
  
  if A[i] in dict:
    print(dict[A[i]])
    continue
  d[A[i]] -=1
  values,counts = zip(*d.most_common())
  ans =0
  for j in range(len(counts)):
    if counts[j] >=2:
      ans += (counts[j]*(counts[j]-1))//2
    
  print(ans)
  d[A[i]] +=1
  dict[A[i]] = ans

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
N = int(input())
A = list(map(int, sys.stdin.readline().strip().split()))
for k in range(0, len(A)):
    B = [a for i,a in enumerate(A) if k != i]
    count = 0
    for i in itertools.combinations(B, 2):
        if i[0] == i[1]:
            count += 1
    print(count)

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

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

from operator import mul
from functools import reduce

def nCr(n,r):
    if n == 1:
        return 0
    r = min(n-r,r)
    if r == 0:
        return 1
    ov = reduce(mul, range(n, n-r, -1))
    un = reduce(mul, range(1, r+1))
    return ov // un

Cs = []
C = []
Ctot = 0
for j in idx:
  if B[j]-1 >= 2:
    Cs.append(nCr(B[j]-1, 2))
  else:
    Cs.append(0)
  tmp = nCr(B[j], 2)
  C.append(tmp)
  Ctot += tmp
for i in range(N):
  current_idx = A[i]-1
  if current_idx in idx:
    count = Ctot + Cs[idx.index(current_idx)]-C[idx.index(current_idx)]
  else:
    count = Ctot
  print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.buffer.readline().rstrip())
    A = list(map(int, sys.stdin.buffer.readline().split()))
    P = set(A)

    ary = {}
    for n in range(N):
        ary[n] = A[n + 1:] + A[:-len(A) + n]

    for a in ary.values():
        tmp = 0
        for p in P:
            t = a.count(p)
            tmp += (t * (t - 1)) // 2

        print(tmp)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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
  latmp = la[:]
  del latmp[i]
  lai = [x for x in set(latmp) if latmp.count(x) > 1]
  for j in lai:
    A += comb(latmp.count(j), 2)
  print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

    ans = {}
    for i in range(len(alist)):
        if alist[i] in ans:
            if i == len(alist)-1:
                print(ans[alist[i]], end='')
            else:
                print(ans[alist[i]])
            continue

        count = 0
        for k, v in collections.Counter(alist).items():
            if k == alist[i]:
                count += cmb(v-1, 2)
            else:
                count += cmb(v, 2)

        if i == len(alist)-1:
            print(count, end='')
        else:
            print(count)
        ans[alist[i]] = count


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
k = list(map(int, input().split()))
d = {}
dd = {}
for i in range(N):
    a = k[i]
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
for key in d:
    a = d[key]
    dd[key] = int(a*(a-1)/2)
for i in range(N):
    res = 0
    for key in d:
        if d[key]>1:
            if key != k[i]:
                res += dd[key]
            else:
                a = d[key]
                res += int((a-1)*(a-2)/2)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import math,collections

def f(c):
  sum=0
  for i in c.values():
    sum+=g(i,2)
  return sum

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

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

for i in range(n):
  c[k[i]]-=1
  print(f(c))
  c[k[i]]+=1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input().replace(' ', ''))
cnt = [0 for i in range(n)]
conb = [0 for i in range(n)]
for i in range(n):
    cnt[i] = s.count(str(i + 1))
    conb[i] = cnt[i] * (cnt[i] - 1) // 2
for k in range(n):
    ans = sum(conb) - conb[int(s[k]) - 1] + (cnt[int(s[k]) - 1] - 1) * (cnt[int(s[k]) - 1] - 2) // 2
    print(ans)

Submission status: Time Limit Exceeded

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

dic = {}
for num in lis:
    if num in dic:
        dic[num] = dic[num] + 1
    else:
        dic[num] = 1

answer_memo_dic = {}
for key, val in dic.items():
    ans_1 = int(val * (val -1) / 2)
    ans_2 = int((val - 1) * (val - 2) / 2)
    answer_memo_dic[key] = {"ans_1": ans_1, "ans_2": ans_2}

answer_dic = {}
def answer():
    for n in lis:
        if n in answer_dic:
            print(answer_dic[n])
        else:
            answer = 0
            for key, val in answer_memo_dic.items():
                if n == key:
                    answer += val["ans_2"]
                else:
                    answer += val["ans_1"]
            print(answer)
            answer_dic[n] = answer

answer()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy.misc import comb

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

for i, _ in enumerate(data):
    tmp = data[:i] + data[i+1:]
    counter = Counter(tmp)
    r = [comb(n, 2, exact=True) for n in counter.values()]
    print(int(sum(r)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections
n = input()
a = list(input().split())

# 各数値の重複数辞書
counter = collections.Counter(a)

# 組み合わせ数
def combi(num):
    if num < 2:
        pair = 0
    else:
        pair = int(num * (num-1) / 2)
    return pair

# 各数字を抽出しない時と抽出した時の組み合わせ数
fulldic = dict()
reducedic = dict()
for k, v in counter.items():
    fulldic[k] = combi(v)
    reducedic[k] = combi(v-1)
    
for el in a:
    print(sum(fulldic.values()) - (fulldic[el] - reducedic[el]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
import math
import collections
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def get_result(key, c):
    ans = 0
    for d in c.items():
        if d[0] == key:
            value = d[1]-1
        else:
            value = d[1]
        if value > 1:
            ans += combinations_count(value, 2)
    return int(ans)
ans = {}
tmp = a.copy()
tmp.sort()
c = collections.Counter(tmp)
for k in range(n):
    key = a[k]
    if key in ans:
        print(ans[key])
    else:
        ans[key] = get_result(key, c)
        print(ans[key])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
for i in range(n):
    rd = a.copy()
    del rd[i]
    co = 0
    for j in range(n - 2):
        co += rd[j + 1:].count(rd[j])
    print(co)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

b = []
c = []

for i in range(1, N + 1):
  m = a.count(i)
  if m != 0:
    s = int(m * (m - 1) / 2)
    m = m - 1
  else:
    s = 0
  b.append(m)
  c.append(s)

t = sum(c)

for i in a:
  res = t - b[i - 1]
  print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

# print(nC2)


def comb(x):
    if x == 0:
        return 0
    if x == 1:
        return 0
    return x * (x - 1) // 2


d = Counter(A)
# print(d)
k = d.keys()

# print(k)

for a in A:
    res = 0
    for kk in k:
        if a == kk:
            res += comb(d[kk] - 1)
            # res += nC2[d[kk] - 1]
        else:
            res += comb(d[kk])
            # res += nC2[d[kk]]
    # print(a, d[a], res)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

cmb2 = [0] * 200001
for n in range(2, 200001):
    num = 1
    r = 2
    for i in range(1, r + 1):
        num = num * (n - i + 1) // i
    cmb2[n] = num

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

memo = {}
for aa in a:
    if aa not in memo:
        x = 0
        for k, v in ctr.items():
            if k == aa:
                v -= 1
            if v >= 2:
                x += cmb2[v]
        memo[aa] = x
    print(memo[aa])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
l=[int(x) for x in input().split()]
for k in range(n):
  L=l.pop(k)
  ans=0
  for i in range(n+1):
    c=l.count(i)
    if c>1:
      ans=ans+math.factorial(c) // (math.factorial(c- 2) * 2)
  print(ans)
  l.insert(k,L)

Submission status: Time Limit Exceeded

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

for i in range(len(a)):
    tmp = list(a)    
    tmp.pop(i)
    count_ident(tmp)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
num_list =  input().split(' ')
num_list2 = [int(n) for n in num_list]
num_list3 = [0]*n
num_list4 = [0]*n
num_list5 = [0]*n
for i in range(len(num_list2)):
    num_list3[num_list2[i]-1] += 1
for i in range(len(num_list2)):
    if num_list4[num_list2[i]-1] == 0:
        size = max(0 , num_list3[num_list2[i]-1])
        if size >= 2:
            ans = comb(size,2)
        elif size == 1:
            ans = 0
        else :
            ans = 0 
        num_list4[num_list2[i]-1] = ans

    if num_list5[num_list2[i]-1] == 0:
        size = max(0 , num_list3[num_list2[i]-1]-1)
        if size >= 2:
            ans = comb(size,2)
        elif size == 1:
            ans = 0
        else :
            ans = 0 
        num_list5[num_list2[i]-1] = ans
for i in range(n):
    print(num_list5[num_list2[i]-1]-num_list4[num_list2[i]-1]+np.sum(num_list4))

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

#Input
n = int(input())
l_a = list(map(int, input().split()))

#Process
for i in range(n):
    l_o = copy.copy(l_a)
    del l_o[i]
    l_c = list(collections.Counter(l_o).values())
    Ans = 0
    for i in range(len(l_c)):
        for j in range(l_c[i] - 1):
            Ans += (j + 1)
    print(Ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
n=int(input())
a=list(map(int,input().split()))
anslist=[]
alist=[]
for i in range(n):
    if(a[i] in alist):
        anslist.append(0)
        print(anslist[a.index(a[i])])
        continue
    else:
        alist.append(a[i])
        ans = 0
        ad=copy.copy(a)
        del(ad[i])
        adset=list(set(ad))
        for j in adset:
            if(ad.count(j)>=2):
                ans += (ad.count(j)*(ad.count(j)-1)) // 2
            else:
                continue
    anslist.append(ans)
    print(ans)

Submission status: Time Limit Exceeded

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


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


n = int(input())
nums = list(map(int, input().split()))
d = Counter(nums)
# for num in nums:
#     if not d.get(num):
#         d[num] = 1
#     else:
#         d[num] += 1

combination_d = {}

for num in nums:
    counter = 0
    if combination_d.get(num):
        print(combination_d[num])
        continue

    nums.count(num)

    for key in d.keys():
        if key == num:
            if d[key]-1 >= 2:
                counter += cmb(d[key]-1, 2)
        else:
            if d[key] >= 2:
                counter += cmb(d[key], 2)
    
    combination_d[num] = counter
    print(counter)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
n = int(input())

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

total_list = []
for i in a:
    total_list.append(a.count(i))

for i in range(n):
    a_val = a[i]
    sum = 0
    tmp_list = []
    for j in range(n):
        # print("j = {}".format(j))
        if i == j:
            pass
        else:
            if (a[j] in tmp_list):
                pass
            else:
                if a[i] == a[j]:
                    for k in range(total_list[j] - 2):
                        sum += k + 1
                else:
                    for k in range(total_list[j] - 1):
                        sum += k + 1
                tmp_list.append(a[j])
        # print("sum = {}".format(sum))
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())
l = deque(input().split())
ans = 0
for k in range(N):
  a = l.popleft()
  for x in set(l):
    if l.count(x)>1:
      ans += l.count(x)*(l.count(x)-1)/2
  print(int(ans))
  ans = 0
  l.append(a)

Submission status: Time Limit Exceeded

Solution code:

import collections

n = int(input())
a = list(map(int, input().split()))
c_lst = []
c = collections.Counter(a)
c_lst = c.most_common()

values, counts = zip(*c_lst)

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

Submission status: Time Limit Exceeded

Solution code:
import collections as col
 
n = int(input())
l = list(map(int,input().split()))
dic = col.Counter(l)
 
lnum = list(dic.values())
lnumc = list(map(lambda x: x*(x-1)/2, lnum))
base = sum(lnumc)
lnumc1 = list(map(lambda x: (x-1)*(x-2)/2, lnum))
 

lans = [int(base-i+j) for (i,j) in zip(lnumc,lnumc1)]

lpac = list(dic.keys())
for i in range(len(l)):
    tmp = lpac.index(l[i])
    print(lans[tmp])

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
ls = [int(x) for x in input().split()]
for x in ls:
    newls = ls.copy()
    counter = 0
    newls.remove(x)
    for x in tuple(itertools.combinations(newls, 2)):
        if x[0] == x[1]:
            counter += 1
    print(counter)

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import defaultdict
  from collections import Counter
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = Counter(a)
  e = defaultdict(int)
  
  
  
  func = lambda x: x*(x-1)//2 if x>=2 else 0
  
  ppp = ''
  
  for x in a:
    get = e.get(x)
    if get == None:
      tmp = d.copy()
      tmp[x] -= 1
      h = list(tmp.values())
      i = list(map(func, h))
      ans = sum(i)
      e[x] = ans
      ppp += str(ans) + '\n'
    else:
      ppp += str(get) + '\n'
    
   
  print(ppp)
      
 
if __name__=='__main__':
    main()

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

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

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

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

    return result


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

    c = Counter(A)
    l = [0 for _ in range(2 * (10**5) + 7)]
    h = [0 for _ in range(2 * (10**5) + 7)]

    for k, v in c.items():
        l[k] = cmb(v-1, 2)
        h[k] = cmb(v, 2)

    for a in A:
        ans = sum(h) - h[a] + l[a]
        print(ans) 


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(n):
  ans = 0
  temp = a_list.copy()
  temp.pop(i)
  for a in set_a:
    cnt = temp.count(a)
    ans += cnt*(cnt-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
A = [int(n) for n in input().split()]
dp = {}
C = {}
for a in set(A):
    C[a] = A.count(a)
#    print(C)
for a in A:
    B = A.copy()
    f = dp.setdefault(a, 0)
    if f==0:
        B.remove(a)
#        print(B)
#        print([c for c in combinations(B, 2)])
        ans = sum([1 if c[0]==c[1] else 0 for c in combinations(B, 2)])
        print(ans)
        dp[a] = ans
    else:
        print(dp[a])

Submission status: Time Limit Exceeded

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

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

N = int(input())
L = list(map(int, input().split()))
M = [0] *(N+1)
cnt = [0] *(N+1)

for i in L:
    cnt[i] += 1
    C = cnt[i]
    if C == 2:
        M[i] = 2
    elif C > 2:
        M[i] *= C
        M[i] //= (C-2)
ans = sum(M)//2

for i in range(N):
    A = L[i]
    B = cnt[A]
    if B == 2:
        print(ans - 1)
    elif B > 2:
        print(ans -combinations_count(B,2) +combinations_count(B-1,2))
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque
from collections import Counter
from scipy.misc import comb
# from scipy.special import comb
from copy import deepcopy

n = int(input())
k = list(map(int, input().split()))
for i in range(len(k)):
    ans = 0
    k_n = deepcopy(k)
    k_n.pop(i)
    c = Counter(k_n)
    for key in c:
        ans += comb(c[key], 2, exact=True)
    print(ans)

Submission status: Time Limit Exceeded

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

    return conbi

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

    if num not in dict.keys():
        del newa[count]
        
        conbi = 0
        for i in itertools.combinations(newa,2):
            if i[0] == i[1]:
                conbi += 1
        ans = conbi
        dict[num] = ans
        print(ans)
        newa.insert(count,num)
        count += 1
    else:
        print(dict[num])
        count += 1

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]
cl = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(cl)

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

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
    ans=0
    p=[]
    b=a[:]
    del b[i]
    c = collections.Counter(b)
    for j in range(n-1): #bの長さ
        if b[j] not in p:
            ans+=c[b[j]]*(c[b[j]]-1)//2
        p.append(b[j])
    print(ans)

Submission status: Time Limit Exceeded

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

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

for k in range(0,n):
    before = ""
    ans = 0
    same_count = 0
    c = collections.Counter(an)
    datas = c.most_common()
    ans = 0
    for data in datas:
        if data[0] == an[k]:
            if data[1] > 1:
                ans += (data[1]-1) * (data[1]-2)/2
        else:
            ans += data[1] * (data[1]-1)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
def P(n,r):
    return math.factorial(n) // math.factorial(n-r)
def C(n,r):
    return P(n,r) // math.factorial(r)
n = int(input())
a = list(map(str,input().split()))
ans = []
cnt = 0
for i in range(n):
    tmp = a[:i] + a[i+1:]
    set_tmp = set(tmp)
    c = collections.Counter(tmp)
    cnt = 0
    for j in set_tmp:
        if int(c[j]) < 2:
            cnt += 0
        else:
            cnt += C(int(c[j]),2)
    ans.append(cnt)
    cnt = 0
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
from collections import defaultdict

N = int(input())
A = list(map(int, input().split(' ')))
ban = 0
while ban < N:
    dic = defaultdict(lambda: 0)
    ans = 0
    for idx, a in enumerate(A):
        if idx == ban:
            pass
        else:
            dic[a] += 1
            if dic[a] >= 2:
                ans += dic[a] - 1
    print(ans)
    ban += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
aList = list(map(int, input().split()))
my_count = aList.count
acount = [my_count(i)-1 for i in aList]
sums = sum(acount)
for i in acount:
    print((sums)//2 - i)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
k = input().split()
for i in range(n):
    delk = k[i]
    del k[i]
    solk = copy.copy(k)
    solk.sort(reverse=False)
    ans = 0
    while len(solk)>1:
        cnt = 0
        while solk[0]==solk[1]:
            cnt += 1
            del solk[0]
            if len(solk)==1:
                break
        del solk[0]
        ans += cnt*(cnt+1)//2
    print(ans)
    k.insert(i,delk)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools

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

for i in range(N):
    tmp = copy.copy(A)
    tmp.pop(i)

    res = 0
    for j in set(tmp):
        l = tmp.count(j)
        res += len(list(itertools.combinations([0]*l,2)))

    print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
s = list(set(l))
all_sum = 0
count = {}
for i in range(len(s)):
    v = l.count(s[i])
    if v > 1:
        all_sum += v * (v - 1) // 2
        count[s[i]] = v
for i in range(len(l)):
    if l[i] in count:
        v = count[l[i]]
        print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
    else:
        print(all_sum)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
N=int(input())
s=input().split()
list=list(map(int,s))
for y in range(N):
    list1=copy.copy(list)
    list1.pop(y)
    c = collections.Counter(list1)
    a=0
    for h in c.values():
        a+=(h*(h-1))
    print(int(a/2))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

def calc(k):
    num = num_list[k]
    
    if(memo_flg[num] is True):
        return memo[num]
    ans = 0
    
    for i,j in c.items():
        if(i==num):
            j -= 1
        ans += (j*(j-1))//2
    memo[num] = ans
    memo_flg[num] = True
    return ans

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