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:
def main():
    N = int(input())
    A = list(map(int, input().split()))

    for k in range(N):
        tmp = A.copy()
        del tmp[k]
        ans = 0
        for i in range(len(tmp) - 1):
            for j in range(i + 1, len(tmp)):
                if tmp[i] == tmp[j]:
                    ans += 1
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

bar = {}

for i in range(N):
    if (A[i] in bar):
        bar[A[i]] = bar[A[i]] + 1
    else:
        bar[A[i]] = 1

partOfNum = {}
lackPartOfNum = {}

for j in bar.keys():
    tmp = permutations_count(bar[j], 2)
    partOfNum[j] = int(tmp / 2)
    tmp = permutations_count(bar[j] - 1, 2)
    lackPartOfNum[j] = int(tmp / 2)

allSum = 0
for l in partOfNum.keys():
    allSum += partOfNum[l]

for i in range(N):
    print(allSum - partOfNum[A[i]] + lackPartOfNum[A[i]])

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

data = [0] * (N+1)

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

total = 0

for i in range(N+1):
    if data[i] >= 2:
        total += permutations_count(data[i],2)



for i in range(N):
    t = total
    if data[A[i]] - 1 > 0 :
        print( total-permutations_count(data[A[i]]-1,1) )
    else:
        print(total)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

N = int(input())
As = input().split()
A = [0 for i in range(N)]
for i in range(N):
  A[i] = int(As[i])

# count number of each label appearing
labels = {}
for i,A_i in enumerate(A):
  if A_i in labels:
    labels[A_i] += 1
  else:
    labels[A_i] = 1

answer = [-1 for i in range(N)]
spoiler = {}
for k in range(N):
  if A[k] in spoiler:
    answer[k] = spoiler[A[k]]
  else:
    labels[A[k]] -= 1
    appnums = list(labels.values())
    answer[k] = 0
    for appnum in appnums:
      if appnum>1: answer[k] += int(appnum*(appnum-1)/2)
    spoiler[A[k]] = answer[k]
    labels[A[k]] += 1

for k in range(N):
  print(answer[k])

Submission status: Time Limit Exceeded

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

def abc159d(n, arr):
    dic = {}
    for a in arr:
        if a in dic:
            dic[a] += 1
        else:
            dic[a] = 1
    
    # delete non-paired ball
    del_list = []
    for k, val in dic.items():
        if val == 1:
            del_list.append(k)
    for item in del_list:
        del dic[item]

    ans = []
    for a in arr:
        sum = 0
        for k, val in dic.items():
            n = val
            if k == a:
                n -= 1

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

        ans.append(sum)

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    arr = input().split(" ")
    
    ans = abc159d(n, arr)
    
    # output
    for a in ans:
        print(a)
# else:
    # do nothing

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):
        cAs = As[:k] + As[k + 1:]
        ans = 0
        for i, j in itertools.combinations(cAs, 2):
            if i == j:
                ans += 1
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

def sameNumberCombi(targetList):
    count = 0
    l = list(itertools.combinations(targetList, 2))
    for i in l:
        if i[0] == i[1]:
            count += 1
    return count

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

for i in range(N):
    a = A.copy()
    a.pop(i)
    print(sameNumberCombi(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

a = input()
a=int(a)
ball=[]
mydict={}

temp=input().split(" ")
for i in range(a):
    if not (temp[i] in mydict):
        mydict[temp[i]]=1
    else:
        mydict[temp[i]]+=1
    ball.append(temp[i])

for i in range(len(ball)):
    rettemp=0
    for j in mydict.keys():
        if ball[i]==j:
            rettemp+=(mydict[j]-1)*(mydict[j]-2)/2
        else :
            rettemp+=(mydict[j]-1)*(mydict[j])/2
    print(int(rettemp))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
sum = 0
count = []
for i in set(A):
    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:
from math import factorial
def cmb(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))
##################################################

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

ans = 0

for i in range(n):
    a1 = a[:int(i)] + a[int(i + 1):]
    #print(a1) #a1 check
    for j in range(n):
        b = a1.count(j + 1)
        if b <= 1:
            pass
        else:
            ans += cmb(b, 2)
        #print(ans)
    print(ans)

    ans = 0 # initiation of the variant 'ans'

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

N = int(input())

l = list(map(int,input().split()))
lc = copy.copy(l)
for i in range(N):
  l.pop(i)
  c = collections.Counter(l)
  cnt = 0
  for v in c.values():
    cnt+=v*(v-1)//2
  l = copy.copy(lc)
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    Da = Counter(A)
    Ds = sorted(Da.items(), key=lambda x: x[1])
    lds = len(Ds)
    c1 = Ds.count(1)

    for i in range(n):
        cnt = 0
        for j in range(lds - c1):
            cnt += Ds[j][1]*(Ds[j][1] - 1)//2

        cnt -= Da[A[i]] - 1
        print(cnt)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# bin/env/pypy3
import sys
import collections
import heapq
import math
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
c = collections.Counter(a)
#print(n,a,c)
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(max(n - r,0)) * math.factorial(r))
for e in a:
    c[e] -= 1
    b = [combinations_count(v,2) for v in c.values() if v > 1]
    print(sum(b))
    c[e] += 1

Submission status: Time Limit Exceeded

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

from collections import deque
from collections import Counter
import math 
def combinations_count(n):
    if n == 1:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))

q = deque(A)
for i in range(N):
    counts = Counter(A[:i]+A[i+1:]).values()
    print(sum([combinations_count(x) for x in counts]))

Submission status: Time Limit Exceeded

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

a_dict = [0]*max(a_list)
for a in a_list:
    a_dict[a-1] += 1

    
answer_dict = [0]*max(a_list)
for i in range(max(a_list)):
    temp_dict = a_dict*1
    temp_dict[i] -= 1
    answer = 0
    for value in temp_dict:
        if value >= 2:
            answer += value*(value-1)//2
    answer_dict[i] = answer
    
for a in a_list:
    print(answer_dict[a-1])

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    b = a[i]
    a[i] = 0
    x = 0
    for v in collections.Counter(a).values():
        x += v*(v-1)//2
    a[i] = b
    print(x)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


def choose(n: int) -> int:
    if n < 2:
        return 0
    else:
        return (n * (n - 1)) // 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:
import collections

def combination(n, r, mod=10**9+7):
    n1, r = n+1, min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod
    
n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)    
s = list(set(li))

for i in range(n):
    c[li[i]] -= 1
    ans = 0
    # print(c)
    # print(c[s[1]])
    # print(s)
    
    for j in range(len(s)):
        if c[s[j]] <= 1:
            continue
        ans += combination(c[s[j]], 2)
    c[li[i]] += 1
        
    print(ans)

Submission status: Time Limit Exceeded

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

for k in range(0,n):
  dic = {}
  alist = stra.split(' ')
  del alist[k]
  for i in range(0,len(alist)):
    if alist[i] in dic.keys():
      dic[alist[i]]+=1
    else:
      dic[alist[i]] = 1
  cnt = 0
  for k,v in dic.items():
    cnt += (v*(v-1))/2
  print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
import collections,math
def comb(n):
    if n==1 or n==0:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
for i in range(n):
    d=0
    e = l[i]
    l[i] = 0
    b=sorted(l)
    c = collections.Counter(b)
    v = list(zip(*c.most_common()))[1]
    for j in range(len(v)):
        d+= comb(v[j])
    print(d)    
    l[i] = e

Submission status: Time Limit Exceeded

Solution code:
def fac(x):
    a=1
    for i in range(2,x+1):
        a*=i
    return(a)
def npr(x,x1):
    return fac(x)//(fac(x-x1)*fac(x1))
n=int(input())
l=list(map(int,input().split()))
d={}
d1={}
ans=0
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d.keys():
    x=npr(d[i],2)
    d1[i]=x
    ans+=x
for i in l:
    if d[i]>1:
        print(ans-d1[i]+npr(d[i]-1,2))
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
# def cmb(n, k, mod):
#     numerator = 1
#     for i in range(k):
#         numerator = (numerator * (n - i)) % mod

#     denominator = 1
#     for i in range(1, k + 1):
#         denominator = (denominator * i) % mod

#     return (numerator * pow(denominator, mod - 2, mod)) % mod

import math


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


import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
freq = {}
for i in a:
    if i in freq:
        freq[i] += 1
    else:
        freq[i] = 1

inf = 1001001001
dp = [inf for _ in range(200005)]
dp2 = [inf for _ in range(200005)]
mod = 10**13 + 3

base = 0
for k, v in freq.items():
    if v > 2:
        dp[k] = combinations_count(v, 2)
        dp2[k] = combinations_count(v - 1, 2)
    elif v == 2:
        dp[k] = 1
        dp2[k] = 0
    else:
        dp[k] = 0
        dp2[k] = 0
    base += dp[k]

for i in a:
    print(base - dp[i] + dp2[i])

Submission status: Time Limit Exceeded

Solution code:
import math

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

p = 10 ** 9 + 7
NN = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, NN + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

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

memo = [0 for _ in range(N)]
for a in A:
  memo[a-1] += 1
memo1 = [0 for _ in range(N)]
for n in range(N):
  memo1[n] = countComb(memo[n], 2, p)

#print(memo)
#print(memo1)
for k in range(N):
  a = A[k]-1
  total = 0
  if a != 0:
    total = sum(memo1[:a])
  total += sum(memo1[a+1:])
  total += countComb(memo[a] - 1, 2, p)
  print(total)

Submission status: Time Limit Exceeded

Solution code:
import collections
from operator import mul
from functools import reduce
mod = 10**9 + 7
def cmb(n,r):
    p,q = 1,1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(i+1)%mod
    return p * pow(q,mod-2,mod) % mod

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

for k in range(N):
  A[k] -= (10**6)
  C = collections.Counter(A)
  A[k] += (10**6)
  values, counts = zip(*C.most_common())
  ans = 0
  for i in range(len(counts)):
    if counts[i] == 1:
      pass
    else:
      ans += cmb(counts[i],2)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

divided = []
for i in range(N):
    divided.append(A_list.count(i + 1))

ans = 0
for j in divided:
    ans += len(list(itertools.combinations(range(j), 2)))

# print(divided)

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

    print(int(ans_k))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
li = list(map(int, input().split()))
for k in range(N):
    check_li, ans = li[:k]+li[k+1:], 0
    for i in range(1, N+1):
        ans += check_li.count(i) * (check_li.count(i)-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(len(A)):
  count = 0
  B = A[:]
  B.pop(i)
  for b in set(B):
    l = len([j for j in B if j == b]) - 1
    count += (l * (l+1)) // 2
  print(count)

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)
val = list(c.values())
key = list(c.keys())
comb = [combinations_count(i,2) if not i < 2 else 0 for i in val]
for i in range(n):
    ind = key.index(a[i])
    count = sum(comb)
    count -= comb[ind]
    hoge = val[ind] -1 
    if hoge < 2:
        pass
    else:
        count += combinations_count(hoge,2)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
 
 
def nCr(n,r):
    if n<r:
        return 0
    
    result = math.factorial(n)//math.factorial(r)//math.factorial(n-r)
    return result

def compute(count):
    total = 0
    for i in count:
        total += nCr(i,2)
    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:
from collections import Counter
import sys
input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(max(a)):
  if a.count(i + 1) > 1:
    answer += a.count(i + 1) * (a.count(i + 1) -1) // 2

for k in range(n):
  if a.count(a[k]) > 2:
    answer1 = answer - (a.count(a[k]) - 1)
  if a.count(a[k]) == 2:
    answer1 = answer - 1
  print(answer1)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c=c.most_common()

for i in a:
    ans=0
    for j in c:
        t1=j[0]
        t2=j[1]
        
        if t1==i:
            t2-=1
        
        ans+=(t2*(t2-1))//2
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
B = Counter(A)
seen = [-1]*(5*10**5)
for i in range(N):
    if seen[A[i]] != -1:
        print(seen[A[i]])
        continue
    B[A[i]] -= 1
    ans = 0
    for n in B.values():
        ans += n*(n-1)//2
    print(ans)
    seen[A[i]] = ans
    B[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a1 = set(a)
l = []
default = 0
for i in a1:
    l.append(a.count(i))
for j in l:
    default += j * (j - 1) // 2
for k in a:
    print(default - (a.count(k) - 1))

Submission status: Time Limit Exceeded

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

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return 0

A_list = []
x_list = []
for i in range(N):
    t = my_index(A_list, A[i])
    if t == 0:
        A_list.append(A[i])
        number_list = [0]
        number_stock_list = [0]
        for k in range(i):
            index = my_index(number_list, A[k])
            if index == 0:
                number_list.append(A[k])
                number_stock_list.append(1)
            else:
                number_stock_list[index] += 1

        for k in range(i + 1, N):
            index = my_index(number_list, A[k])
            if index == 0:
                number_list.append(A[k])
                number_stock_list.append(1)
            else:
                number_stock_list[index] += 1

        x = 0
        for j in range(1, len(number_stock_list)):
            x += int(number_stock_list[j] * (number_stock_list[j] - 1) / 2)
        x_list.append(x)
        print(x)
    else:
        print(x_list[t])

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(n):
    b = a.copy()
    del b[i]
    c = b.copy()
    c = set(c)
    c = list(c)
    ans = 0
    for j in range(len(c)):
        num = b.count(c[j])
        ans += int(num*(num-1)/2)
    print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict

adict = defaultdict(int)

resdict = {}

for a in alist:
    adict[a] += 1

for a in alist:
    if a in resdict:
        print(resdict[a])
        continue
    adict[a] -= 1
    temp = 0

    for k,v in adict.items():
        # print(k, v)
        if v >= 2:
            temp += v*(v-1)//2
    print(temp)
    resdict[a] = temp
    adict[a] += 1

Submission status: Time Limit Exceeded

Solution code:
S=[];G=[];p=0
import collections
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
  for j in range(N):
    if i!=j:
      S.append(A[j])
  c = collections.Counter(S)
  G=c.most_common()
  #print(len(G))
  for a,b in G:
    #print(a,b)
    p=p+(b*(b-1))//2
  print(p)
  S.clear()
  p=0
  #print(G)

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
a = input().split(" ")
a = [int(i) for i in a]

c = [a.count(i+1) for i in range(n)]

tmp = 0
for i in range(n):
  tmp = tmp + int(c[i] * (c[i] - 1) / 2)

r = [tmp for i in range(n)]
r = [r[i] - c[i] + 1 for i in range(n)]

for i in range(n):
  print(r[a[i]-1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list = list(map(int,input().split()))
di = {}
for i in range(n):
	k = list[i]
	if k not in di:
		di[k]=1
	else:
		di[k]+=1
list_del=[]
for key in di.keys():
	if di[key]==1:
		list_del.append(key)
for i in range(len(list_del)):
	di.pop(list_del[i])
	

for i in range(n):
	k = list[i]
	if k in di:
		di[k]-=1
	co = 0
	for valu in di.values():
		co += valu*(valu-1)/2
	print(int(co))
	
	if k in di:
		di[k]+=1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
P = list(map(int,input().split()))
import collections
c = collections.Counter(P)
t = 0
c1 =c
for i in range(N):
    c1[P[i]]= c1[P[i]]-1
    for j in c1.values():
        t = t+j*(j-1)/2
    print(int(t))
    t = 0
    c1[P[i]]= c1[P[i]]+1

Submission status: Time Limit Exceeded

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

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

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

for i in A:
    cmb = 0
    for j in cdic:
        if i == j and cdic[j] > 0:
            cmb += cdic[j] - (ndic[j] - 1)
        else:
            cmb += cdic[j]
    print(cmb)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():

    input = stdin.readline

    N = int(input())

    A = [0] * N

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

    all_case = 0

    for i in set(A):
        num_count = A.count(i)
        all_case += (num_count * (num_count - 1)) // 2

    for i in A:
        ans = all_case - (A.count(i) - 1)
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def calc_comb(lst_A):

    elem_A = list(set(lst_A))
    sum_comb = 0

    for A in elem_A:
        numA  = lst_A.count(A)
        combA = (numA * (numA-1)) // 2
        sum_comb += combA

    return sum_comb

def main(N, lst_A):

    sum_comb_all = calc_comb(lst_A)

    for A in lst_A:
        numA = lst_A.count(A)
        prev_combA = (numA * (numA-1)) // 2
        new_combA  = ((numA-1) * (numA-2)) // 2

        sum_comb = sum_comb_all - prev_combA + new_combA
        
        print(sum_comb)

if __name__ == "__main__":

    main(N, lst_A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def pairs(L: list):
    aspects = set(L)
    ret = 0
    for i in aspects:
        n = L.count(i)
        ret += (n*(n-1))//2
    return ret


def main():
    N = int(input())
    A_i = list(map(int, input().split()))
    cache = dict()

    for i, a in enumerate(A_i):
        if a in cache:
            print(cache[a])
        else:
            A_i[i] = 0
            r = pairs(A_i)
            A_i[i] = a
            cache[a] = r
            print(r)

    return


main()

Submission status: Time Limit Exceeded

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

MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

N = int(input())
A = list(map(int, input().split()))
dic = {}
for a in A:
    if a in dic: dic[a] += 1
    else: dic[a] = 1

C = {0:0}

for i in range(1, 2*10**5+1):
    C[i] = i*(i-1)//2

ans = []
for i in range(N):
    dic2 = dic.copy()
    dic2[A[i]] -= 1
    a = 0

    for k, v in dic2.items():
        a += C[v]

    ans.append(a)


for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

d = {}

s = list(set(a))

for i in s:
    d[i] = 0

for i in s:
    c = a.count(i)
    for j in d.keys():
        if j != i and c>=2:
            d[j] += ((c*(c-1))//2)
        elif j == i and c>2:
            k = c - 1
            d[j] += (((k-1)*(k))//2)
for i in a:
    print(d[i])

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def comb(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())
a = list(map(int, input().split()))

aa = [0 for i in range(2*10**5)]
for i in range(n):
  aa[a[i]] += 1
 
for i in range(n):
  ans = 0
  aa[a[i]] -= 1
  for key in range(2*10**5):
    if aa[key] > 1:
      ans += comb(aa[key], 2)
  aa[a[i]] += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

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

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

cnt_all_conbi = 0
for i in unique_a:
    x = a.count(i)
    if 2 <= x:
        ret = math.factorial(x) // (2*(math.factorial(x-2)))
        cnt_all_conbi += ret

for i in a:
    x = a.count(i)
    print(cnt_all_conbi - (x-1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
c = Counter(A)

NMAX=2*10**5

tmp = np.zeros((NMAX+1))
for n in c:
    tmp[n] = c[n]

l1= tmp*(tmp-1)//2
tmp -=1
l2= tmp*(tmp-1)//2

for a in A:
    sum = 0
    for i in c:
        if a ==i:
            sum += l2[i]
        else:
            sum += l1[i]
    print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

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

a_cnt = Counter(a)

a_comb = {}
for key, val in a_cnt.items():
    if val >= 2:
        a_comb[key] = combinations_count(val, 2)
    else:
        a_comb[key] = 0


for num in a:
    dic = a_comb.copy()
    if dic[num] <= 1:
        dic[num] = 0
    else: 
        dic[num] = combinations_count(a_cnt[num]-1, 2)
    print(int(sum(dic.values())))

Submission status: Time Limit Exceeded

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

N=int(input())
A=list(map(int,input().split() ))
B=[-1]*N   #メモ化。iの時の方法の総数

for i,item in enumerate(A):
  if B[item-1]>-1:
    #print(i,item,"は計算済み！")
    #print(i,B[item-1],"done")
    print(B[item-1])
  else:
    C=A[0:i]+A[i+1:] #仮で作るリスト
    cnt = Counter(C)
    D=dict(cnt)
    E=[]
    for item2 in D:
      if D[item2]>=2:
        E.append(D[item2])
    #print(i,E)
    opt=0
    for item3 in E:
      opt += comb(item3,2,exact=True)
    B[item-1]=opt
    #print(i,opt)
    print(opt)
    #print(i,E,opt)
    #print(C,cnt)

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)

for k in range(N):
    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
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
l1=[]
l2=[]
s1=list(set(l))
for i in l:
    l1.append(l.count(i))
sum1=0
for i in s1:
    l2.append(l.count(i))
#print(l2,s1)
for i in l2:
    sum1+=(i*(i-1))//2
#print(sum1)
sum=0
sum2=sum1
for i in l:
    sum1=sum2
    sum=0
    c=l2[s1.index(i)]
    sum1-=(c*(c-1))//2 
    #print(sum1)
    c=c-1
    sum+=(c*(c-1))//2+sum1
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
    d[i] = d.get(i, 0)+1

for i in a:
    d[i] -= 1
    temp = 0
    for j in d.values():
        temp += len(list(itertools.combinations(range(j),2)))
    print(temp)
    d[i] += 1

Submission status: Time Limit Exceeded

Solution code:
import collections as cl
import numpy as np
n=int(input())
a=list(map(int,input().split()))

for k in range(n):
    b=a.copy()
    a.pop(k)
    a_=np.array(list(cl.Counter(a).values()))
    a=b
    c=np.sum(a_*(a_-1)/2)
    print(int(c))

Submission status: Time Limit Exceeded

Solution code:
#n
n=int(input())

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

for i in range(n):
    wrkcnt = 0
    ans = 0
    work = a[i]
    a[i] = 0
    newlist = list(set(a))
    for j in range(len(newlist)):
        wrkcnt = a.count(newlist[j])
        ans += wrkcnt * (wrkcnt-1) /2
    a[i] = work
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    l = a[i]
    a[i] = 'X'
    ans = 0
    a_s = set(a)
    for j in a_s:
        ans += comb(a.count(j),2,exact=True)
    print(ans)
    a[i] = l

Submission status: Time Limit Exceeded

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

a_dict = dict()
for x in a:
    if x not in a_dict:
        a_dict[x] = 1
    else:
        a_dict[x] += 1

"""for k, v in a_dict.items():
    if v < 2:   # 1個しかないボールは削除
        del a_dict[k]
    a_dict[k] = v*(v-1)//2  # valueにコンビネーション数を設定"""

a_dict = {k: v for k, v in a_dict.items() if v != 1}

sum = 0
for i in a:
    for k, v in a_dict.items():
        if k == i:
            sum += (v-2)*(v-1)//2
        else:
            sum += v*(v-1)//2
    print(sum)
    sum = 0