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 comb(n, r):
    if n < 2:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
L = input().split(" ")

c = collections.Counter(L)

d = {}
tmp_total = 0 

memo1 = {}
for i in c:
    if c[i] not in memo1.keys():
        tmp = comb(c[i], 2)
        memo1[i] = tmp
        tmp_total += tmp
    else:
        tmp_total += memo1[c[i]]
        
total = tmp_total
memo = {}
for i in L:
    if i not in memo.keys():
        ans = total - memo1[i] + comb(c[i]-1, 2)
        memo[i] = ans
        print(ans)
    else:
        print(memo[i])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
if True:
    n = int(input())
    an= list(map(int, input().split()))

else:
   n=5
   an=[1,1,2,1,2]


for k in range(0,n):
    an2 = an[:]
    del an2[k]
    # print(an2)
    ans = 0
    for b1 in range(0,n-1):
        for b2 in range(b1+1,n-1):
            if an2[b1] == an2[b2]:
                ans += 1
    print(int(ans))
    an2.clear()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
def nCr(n, r): 
  
    return (math.factorial(n) // (math.factorial(r)  
                * math.factorial(n - r)))
n=int(input())
arr=list(map(int,input().split()))
p=Counter(arr)
# print(dict(p))
dict={}
for i in arr:
  if i not in dict:
    total=0
    for j in p:
    #   print(j)
      if j==i:
        if p[j]-1>1:
        	total+=nCr(p[j]-1,2)
      else:
        if p[j]>1:
          total+=nCr(p[j],2)
    dict[i]=total
    print(dict[i])
  else:
    print(dict[i])

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


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

length = int(input())
temp_list = list(map(int, input().split()))

for i in range(length):
    tm = temp_list[:]
    tm.pop(i)
    
    c=collections.Counter(tm)
    print(sum([combinations_count(p, 2) for p in c.values() if p>1]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import math
N = int(input())
A = list(map(int,input().split()))

def comb(n, r):
    if n > 1:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        return 0

dict = {}

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

combs = {}

for k in dict:
    combs[k] = comb(dict[k],2)


total = sum(combs.values())

for i in range(N):
    diff = combs[A[i]] - comb(dict[A[i]]-1,2)
    print(total - diff)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = sorted(set(A))
dic = {}
ans = 0

for i in range(len(B)):
    if A.count(B[i]) > 1:
        C = A.count(B[i])
        dic[B[i]] = C
        ans += C*(C-1)//2
    else:
        dic[B[i]] = 1
        
for i in range(n):
    x = dic[A[i]]
    print(ans - x*(x-1)//2 + (x-1)*(x-2)//2)

Submission status: Time Limit Exceeded

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

def count(val):
  cnt = 0
  sameNumber[val] -= 1
  for n in sameNumber:
    cnt += sameNumber[n] * (sameNumber[n]-1) //2
  sameNumber[val] += 1
  return cnt

for n in A:
  res = 0
  res += count(n)
  print(res)

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

    for i in range(n):
        cnt = 0
        for j in range(len(Ds) - Ds.count(1)):
            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:
# -*- coding: utf-8 -*-
import math
import collections

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

dict = collections.Counter(a)

def comb_ct(n, r):
    try:
        x = math.factorial(n - r)
        return math.factorial(n) // (x * math.factorial(r))
    except:
        return 0

dict_ans1 = {}
dict_ans2 = {}
keys = []
for k, v in dict.items():
    keys.append(k)
    dict_ans1[k] = comb_ct(v, 2)
    dict_ans2[k] = comb_ct(v-1, 2)

for i_num in a:
    ans = 0
    for k in keys:
        if k != i_num:
            #dict_ans1の数値を加算
            ans += dict_ans1[k]
        else:    
            #dict_ans2の数値を加算
            ans += dict_ans2[k]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=[]
from collections import Counter
d=Counter(a)
MOD=10**9+7
def comb(n,k):
    if n<k:
        return 0
    if n<0 or k<0:
        return 0
    k=min(n-k,k)
    ans=1
    inv=[1]*(k+1)
    if k>=1:
        ans*=(n-k+1)%MOD
    for i in range(2,k+1):
        inv[i]=MOD-inv[MOD%i]*(MOD//i)%MOD
        ans=ans*(n-k+i)*inv[i]%MOD
    return ans
cnt=[]
v=set()
yut=True
for i in range(n-1):
    if a[i]!=a[i+1]:
        yut=False
if yut:
    for i in range(n):
        print(comb(n-1,2))
    exit()
for i in range(n):
    g=a.count(a[i])
    v.add(a[i])
    cnt.append([a[i],comb(g,2)])
fg=0
cvg=[]
vsd=list(v)
for i in range(n):
    tt=vsd[fg]
    if cnt[i][0]==tt:
        cvg.append(cnt[i][1])
        if  fg<len(vsd)-1:
            fg+=1

sy=sum(cvg)-cvg[-1]
for i in range(n):
    h=a.count(a[i])
    tmp=sy-(h-1)
    ans.append(tmp)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

count = [0 for _ in range(200000)]
line = [int(i) for i in input().split()]
index = []

for i in line:
	# if count[i] == 0:
		# index.append(i)
	count[i] += 1


for i in line:
	count[i] -= 1
	result = 0
	for j in range(200000):
		result += (count[j]*(count[j]-1))//2
	count[i] += 1
	print(result)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(n):
    x = a[:i] + a[i+1:]
    c1 = list(collections.Counter(x).values())
    c2 = sum([j*(j-1)/2 for j in c1 if j >= 2])
    print(int(c2))

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, 10*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()))

for i in range(n):
    l = a[:i]+a[i+1:]
    pattern = 0
    for j in set(l):
        sum = l.count(j)
        pattern += sum*(sum-1)//2
    print(pattern)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n=int(input())
a=list(map(int,stdin.readline().split()))
dict={}
for i in range(n):
    if dict.get(a[i],False):
        dict[a[i]]+=1
    else:
        dict[a[i]]=1
dict2={}
for i in range(n):
    sum=0
    if dict2.get(a[i],-1)!=-1:
        sum=dict2[a[i]]
    else:
        for j in dict:
            if j==a[i]:
                if dict[j]>2:
                    sum+=((dict[j]-1)*(dict[j]-2))//2
            else:
                if dict[j]>=2:
                    sum += ((dict[j])*(dict[j]-1))//2
        dict2[a[i]]=sum
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
 
n = int(input())
A = np.array(list(map(int, input().split(' '))))
 
A_unique = np.unique(A)
num_dict = {}
total = 0
for a in A_unique:
  tmp = np.count_nonzero(A==a)
  num_dict[a] = (tmp*(tmp-1)/2, (tmp-1)*(tmp-2)/2)
  total += tmp*(tmp-1)/2

for k in range(n):
  print(int(total - num_dict[A[k]][0] + num_dict[A[k]][1]))

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

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

N = int(input())
A = list(map(int, input().split()))
B = A[:]
B.sort()
C = [B[0]]
D = [1]
L = 0
for i in range(N-1):
    if B[i] == B[i+1]:
        D[L] += 1
    else:
        C.append(B[i+1])
        D.append(1)
        L += 1

for i in range(N):
    ans = 0
    for j in range(len(C)):
        if C[j] == A[i] and D[j]>2:
            ans += cmb(D[j]-1,2)
        if C[j] != A[i] and D[j]>1:
            ans += cmb(D[j],2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
nlist = list(map(int,input().split()))
xlist = sorted(nlist)

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

count = 0
for i in range(1,len(nlist)+1):
    if nlist.count(i) >= 2:
        count+=combinations_count(nlist.count(i),2)

anslist = []

for i in range(0,len(xlist)):
    anslist.append(count-xlist.count(nlist[i])+1)

for i in anslist:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb
from copy import deepcopy
import sys


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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

for i in range(N):
  ans = 0
  A_ = A.copy()
  A_.pop(i)
  for j in set(A_):
    count = A_.count(j)
    ans += count * (count - 1) / 2
  print(int(ans))

Submission status: Time Limit Exceeded

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


def main():
    n = int(input())
    a_i = tuple(map(int, input().split()))

    count_item = {}
    all_cnt = 0
    for items in collections.Counter(a_i).items():
        if items[1] >= 2:
            pre_cnt = 0
            if items[1] >= 3:
                pre_cnt = len(list(itertools.combinations(range(items[1] - 1), 2)))

            now_cnt = len(list(itertools.combinations(range(items[1]), 2)))
            count_item[items[0]] = (now_cnt, pre_cnt)
            all_cnt += now_cnt
        else:
            count_item[items[0]] = (0, 0)

    for k in a_i:
        items = count_item[k]
        print(all_cnt - (items[0] - items[1]))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
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()))
md=defaultdict(int)
for a in A:
  md[a]+=1
md2={}
md3={}
for key,value in md.items():
  if value>1:
    md2[key]=combinations_count(value,2)
  else:
    md2[key]=0
  value-=1
  if value>1:
    md3[key]=combinations_count(value,2)
  else:
    md3[key]=0
s_md2=sum(md2.values())
for a in A:
  print(s_md2-md2[a]+md3[a])

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
m = {}
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)

for k in range(n):
  now = a[k]
  if now in m:
    print(m[now])
  else:
    c[now] -= 1
    ans = 0
    for asdf in c.values():
      if asdf > 1:
        ans+= math.factorial(asdf) // (math.factorial(asdf - 2) * 2)
    m[now] = ans
    c[now] += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

dic = {}
for i in range(len(a)) :
    if a[i] in dic :
        dic[a[i]] = dic[a[i]] + 1
    else :
        dic[a[i]] = 1

#print(dic)
dicSum = {}
total = 0

for key in dic:
    dicSum[key] = combinations_count(dic[key], 2)
    total += dicSum[key]

#print(dicSum, total)

for i in range(n) :
    #print(total, dicSum[a[i]], combinations_count(dic[a[i]]-1, 2))
    print(total - dicSum[a[i]] + combinations_count(dic[a[i]]-1, 2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def Comb(n,k):
    comb_ans=1
    for i in range(1,k+1):
        comb_ans*=(n-i+1)/i
    comb_ans=int(comb_ans)
    return comb_ans

N=int(input())
A=list(map(int,input().split()))
C=Counter(A)
ans=0
L=[0]*N
for i in range(N):
    L[A[i]-1]+=1
for i in range(len(C)):
    ans+=Comb(C.most_common()[i][1],2)
for i in range(N):
    pri=ans
    pri=ans-Comb(L[A[i]-1],2)+Comb(L[A[i]-1]-1,2)
    print(pri)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
n = int(input())
li = list(map(int,input().split()))

for i in range(n):
    new_list = []
    Sum = 0
    if i == 0:
        new_list = li[1:n]
        S=Counter(new_list)
        for k in range(n):
            if S[k] > 1:
                Sum += (S[k] * (S[k] -1)) / 2
            else:
                continue
        print(int(Sum))
    elif i == n-1:
        new_list = li[:n-1]
        S=Counter(new_list)
        for k in range(n):
            if S[k] > 1:
                Sum += (S[k] * (S[k] -1)) / 2
            else:
                continue
        print(int(Sum))
    else:
        new_list = li[:i] + li[i+1:]
        S=Counter(new_list)
        for k in range(n):
            if S[k] > 1:
                Sum += (S[k] * (S[k] -1)) / 2
            else:
                continue
        print(int(Sum))

Submission status: Time Limit Exceeded

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

nn = int(input())
aa = list(map(int, input().split(' ')))

counts = {}
for a in aa:
    if a not in counts:
        counts[a] = aa.count(a)


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


results = {}
for a, c in counts.items():
    count = 0
    for aaa, cc in counts.items():
        if a == aaa:
            cc -= 1
        if cc <= 1:
            continue
        if cc <= 2:
            count += 1
            continue
        count += cmb(cc, 2)
    results[a] = int(count)

for k in range(nn):
    print(str(results[aa[k]]))

Submission status: Time Limit Exceeded

Solution code:
#


def main():
    n = int(input())
    a = [int(s) for s in input().split(" ")]
    # これだとTLE
    counts = {}
    for i in a:
        if i in counts:
            counts[i] += 1
        else:
            counts[i] = 1
    delete_key = set()
    for k in counts:
        if counts[k] <= 1:
            delete_key.add(k)
    for k in delete_key:
        del counts[k]
    results = {}
    for i in a:
        if i in results:
            print(results[i])
            continue
        count = 0
        for k, v in counts.items():
            if k == i:
                v -= 1
            if v > 1:
                count += (v * (v - 1) // 2)
        print(count)
        results[i] = count


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=N-1
for n in range(N):
  count=0
  b=A[:n]+A[n+1:]
  for i in range(c):
    for j in range(i+1,c):
      if(b[i]==b[j]):
        count+=1
  print(count)

Submission status: Time Limit Exceeded

Solution code:
def print_num(a):

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

if __name__ == "__main__":
    n = int(input())
    a = tuple(input().split())

    for i in range(n):
        b = list(a)
        b.pop(i)
        print_num(a=b)

Submission status: Time Limit Exceeded

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

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

for i in range(n):
  s = 0
  tmp = a.popleft()
  d = {}
  for j in range(n-1):
    if a[j] not in d:
      d[a[j]] = 1
      tmp2 = a.count(a[j])
      if tmp2 >= 2:
        s += combinations_count(tmp2, 2)
  print(s)
  a.append(tmp)

Submission status: Time Limit Exceeded

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

table = set(a)

con = {}
dic = {}
ans = []
for i in table:
    con[i] = a.count(i)
    dic[i] = -1

for x in a:
    if dic[x] == -1:
        ex = (con[x] - 1) * (con[x] - 2) / 2
        no = (con[x]) * (con[x] - 1) / 2
        dic[x] = (int(no), int(ex))

su = sum([int(x[0]) for x in dic.values()])

for i,x in enumerate(a):
    print(su - (dic[x][0] - dic[x][1]))

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
dp = {}
dpa = {}

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

balls = {}
fact = 1
for (i, a) in enumerate(A):
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1

for a in A:
  if not a in dpa:
    check_balls = deepcopy(balls)
    check_balls[a] -= 1
    res = 0
    for count in check_balls.values():
      if not count in dp:
        dp[count] = count * (count-1) // 2
      res += dp[count]
    dpa[a] = res
  print(dpa[a])

Submission status: Time Limit Exceeded

Solution code:
import copy
import math

def permutation_count(x):
    ret = 0
    if 2 <= x:
        ret = math.factorial(x) // (2*(math.factorial(x-2)))

    return ret


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)
    cnt_all_conbi += permutation_count(x)

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

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

comon = c.most_common()
ans = 0
for i in comon:
  if i[1] != 1:
  	ans += (i[1]*(i[1]-1)//2)
  
for i in a:
  if (ans-int(a.count(i))) < 0:
    print(0)
  else:
    for j in comon:
      if i == j[0]:
        co = j[1]
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

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]*(10**6)
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()))

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

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

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    a_copy = copy.copy(a)
    del a_copy[i]
    c = collections.Counter(a_copy)
    cnt = 0
    for i in c.values():
        cnt += i * (i-1) // 2

    
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A_list = list(map(int, input().split(" ")))
C_dic = collections.Counter(A_list)
ans_dic = {}
for i in A_list:
    if i in ans_dic:
        print(ans_dic[i])
    else:
        temp_sum = 0
        for j in C_dic:
            if i == j:
                temp = C_dic[j]-1
            else:
                temp = C_dic[j]
            temp = temp*(temp-1)//2
            temp_sum += temp

        ans_dic[i] = temp_sum
        print(temp_sum)

Submission status: Time Limit Exceeded

Solution code:

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

d = dict()
for i in range(n):
	li = l.copy()
	tr = li.pop(i)
	if tr in d:
		print(d[tr])
	else:


		lis = list(set(li))
		co = 0
		for ii in lis:
			m = li.count(ii)
			co += (m * (m - 1) // 2)

		print(co)
		d[tr] = co

Submission status: Time Limit Exceeded

Solution code:
import itertools
import collections


n = int(input())
l = list(map(int, input().split()))
counter = collections.Counter(l)
for i in range(n):
    sum = 0
    for k, v in counter.items():
        num = v - 1 if k == l[i] else v
        sum += len(list(itertools.combinations(range(num), 2)))
    print(sum)

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 = {}
dic_ans = {}
dic_comb = {}

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]

    if tmp_key in dic_ans:
        print(dic_ans[tmp_key])
    else:
        for k in dic:
            if k == tmp_key:
                tmp_dic_idx = dic[k]-1
                if tmp_dic_idx in dic_comb:
                    ans += dic_comb[tmp_dic_idx]
                else:
                    tmp_calc = combinations_count(tmp_dic_idx, 2)
                    ans += tmp_calc
                    dic_comb[tmp_dic_idx] = tmp_calc
            else:
                tmp_dic_idx = dic[k]
                if tmp_dic_idx in dic_comb:
                    ans += dic_comb[tmp_dic_idx]
                else:
                    tmp_calc = combinations_count(tmp_dic_idx, 2)
                    ans += tmp_calc
                    dic_comb[tmp_dic_idx] = tmp_calc
        print(ans)
        dic_ans[tmp_key] = ans

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 = {}

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

for i in range(N):
    jogai = A[i]
    seed = bar[jogai] - 1
    tmp2 = permutations_count(seed, 2)
    lackCombination = int(tmp2 / 2)
    for k in partOfNum.keys():
        if (k != jogai):
            lackCombination += partOfNum[k]
    print(lackCombination)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

import copy

def combinations(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

N = int(input())

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

SortList = copy.copy(AList)
SortList.sort()

NumberList = []
CountList = []
tmp = SortList[0]
count = 0
for i in SortList:
    if tmp == i:
        count += 1
    else:
        NumberList.append(tmp)
        CountList.append(count)
        tmp = i
        count = 1
NumberList.append(tmp)
CountList.append(count)

CountListCopy = copy.copy(CountList)

result = 0
for i in range(0, len(AList)):
    CountListCopy[NumberList.index(AList[i])] -= 1
    for q in CountListCopy:
        if q < 2:
            continue
        else:
            result += combinations(q, 2)
    print(result)
    result = 0
    
    CountListCopy = copy.copy(CountList)

Submission status: Time Limit Exceeded

Solution code:
def comb(n, k, MOD = 10 ** 9 + 7):
    if n < k:
        return 0
    if n - k < k:
        k = n - k
    comb = 1
    for x in range(n - k + 1, n + 1):
        comb = (comb * x) % MOD
    d = 1
    for x in range(1, k + 1):
        d = (d * x) % MOD
    comb *= pow(d, MOD - 2, MOD)
    return comb % MOD

def main():
    N = int(input())
    A = [int(v) for v in input().split()]
    C = [0] * (N + 1)

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


    for i in range(N):
        C[A[i]] -= 1
        ans = 0
        for cnt in C:
            ans += comb(cnt, 2)
        C[A[i]] += 1
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import collections


nc2_memo = {}
def nc2(n):
    if n <= 1:
        return 0
    if n in nc2_memo:
        return nc2_memo[n]
    r = math.factorial(n)//(2*math.factorial(n-2))
    nc2_memo[n] = r
    return r



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

cnt = collections.Counter(a)
each_ans = {}
for key, value in cnt.items():
    each_ans[key] = (nc2(value), nc2(value-1))
all_sum = sum(value[0] for _, value in each_ans.items())
ans = {}
for key in cnt.keys():
    ans[key] = all_sum - each_ans[key][0] + each_ans[key][1]
for i in a:
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

duplication = {}
for x in A:
    if x in duplication:
        continue
    if A.count(x) > 0:
        duplication[x] = A.count(x)
ans = 0
for x in duplication.values():
    ans += comb(x,2)
for x in A:
    if x not in duplication:
        print(ans)
    else:
        print(ans -comb(duplication[x],2) +comb(duplication[x]-1,2))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

balls = [0]*n
for a in A:
    balls[a-1] += 1

tei = math.factorial(2)
ball_permu = [0]*n
ball_permu_target = [0]*n
for i in range(n):
    if balls[i] < 2:
        ball_permu[i] = 0
    else:
        ball_permu[i] = math.factorial(
            balls[i])//(math.factorial(balls[i]-2)*tei)

    ball = balls[i]-1
    if ball < 2:
        ball_permu_target[i] = 0
    else:
        ball_permu_target[i] = math.factorial(
            ball)//(math.factorial(ball-2)*tei)

ball_sum = sum(ball_permu)
# print(balls)
# print(ball_permu)
# print(ball_permu_target)
for a in A:
    ans = ball_sum-ball_permu[a-1]+ball_permu_target[a-1]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
Y = set(A)
for k in range(0,N):
    if A.count(A[k]) == 1:
        print(0)
        continue
    X = A[:k] + A[k+1:]
    c = 0
    for i in Y:
        sc = X.count(i)
        if sc == 1:
            continue
        c += sc * (sc-1)//2
    print(c)

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[0:i]+A[i+1:N]
    ans1=len(set(B))
    ans2=collections.Counter(B)
    ANS=list(ans2.values())
    real_ans=0
    for j in range(ans1):
        real_ans+=(ANS[j]*(ANS[j]-1))//2
    print(real_ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
from operator import mul
from functools import reduce

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

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}

    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    ANS = {}
    for k in X.keys():
        val = 0
        for k2 in X.keys():
            if k == k2:
                val += Y[k2]
            else:
                val += X[k2]
        ANS[k] = val
    for a in A:
        print(ANS[a])

if __name__ == '__main__':
    main()