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

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

for aa in a:
    x = 0
    for k, v in ctr.items():
        if k == aa:
            v -= 1
        if v >= 2:
            c = math.factorial(v) // (2 * math.factorial(v - 2))
            x += c
    print(x)

Submission status: Time Limit Exceeded

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

count = {}
for i in range(N):
    if values[i] not in count:
        count[values[i]] = 1
    else:
        count[values[i]] = count[values[i]] + 1

for i in range(N):
    temp = count.copy()
    temp[values[i]] = temp[values[i]] - 1
    num = 0
    for j in temp:
        num = num + int(temp[j]*(temp[j]-1)/2)
    print(num)

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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


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


count = collections.Counter(a)

for k in range(n):
    ans = 0
    for c in count:
        kosu = count[c]
        if a[k] == c:
            kosu -= 1
        ans += combinations_count(kosu, 2)
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1

for i in range(n):
    b = a[i]
    d[b] = d[b]-1
    count = 0
    for j in d:
        temp_count = d[j]*(d[j]-1)//2
        count += temp_count
    d[b] = d[b] + 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
k = [int(i) for i in input().split()]
pair = {}

for i in range(n):
    pair.setdefault(k[i], []).append(i)

for i in range(n):
    ans = 0
    for value in pair.values():
        l = len(value)-1 if i in value else len(value)
        ans += l*(l-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
dct = {}
for i in range(n):
    if a[i] in dct:
        dct[a[i]] += 1
    else:
        dct[a[i]] = 1
tmp_dct = {}
tmp2_dct = {}
for key, value in dct.items():
    tmp_dct[key] = value * (value - 1) // 2
    tmp2_dct[key] = (value - 1) * (value - 2) // 2
# print(tmp_dct, tmp2_dct)
ans_dct = {}
for key, value in dct.items():
    cnt = 0
    for key2, value2 in dct.items():
        if key == key2:
            cnt += tmp2_dct[key2]
        else:
            cnt += tmp_dct[key2]
        # print(key, key2, cnt)
    ans_dct[key] = cnt
# print(ans_dct)
for i in range(n):
    print(ans_dct[a[i]])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

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

    A_sub = np.array(A_sub)

    # ans = 0
    # uni_vals = np.unique(A_sub)
    # for val in uni_vals:
    #     # val = uni_vals[0]
    #     n = np.where(A_sub == val)[0].shape[0]
    #     ans += (n*(n-1))/2
    dic = {}
    for val in A_sub:
        if val in dic.keys():
            dic[val] += 1
        else:
            dic[val] = 1
    ans = 0
    for val in dic.values():
        ans += (val*(val-1)/2)

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import math, collections
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()))
al = collections.Counter(a)
res = [0]*(n+1)
dp = [0]*(n+1)
for i in al:
    if al[i]==1: 
        continue
    if dp[al[i]] == 0:
        res[i] += combinations_count(al[i], 2)
        dp[al[i]] += res[i]
    else: res[i] = dp[al[i]]
sum_ = sum(res)
for i in a:
    if al[i]==1:
        print(sum_)
        continue
    if al[i]==2:
        print(sum_-res[i])
        continue
    if dp[al[i]-1] == 0:
        dp[al[i]-1] = combinations_count(al[i]-1, 2)
        print(sum_-res[i]+ dp[al[i]-1])
    else: print(sum_-res[i]+ dp[al[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 j in mem:
        cdic[j] = mem[j]
    else:
        cdic[j] = int((ndic[j] * (ndic[j] -1)) / 2)
        mem[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:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
answer = [-1] * N
p = []
for i in range(N):
  count[i]  = A.count(i + 1)
  
#nanimo = 0 
#for i in range(N):
#  nanimo += count[j] * (count[j] - 1) // 2

for i in range(N):
  if answer[A[i] - 1] != -1:
    p.append(answer[A[i] - 1])
  else:
    count[A[i] - 1] -= 1
    s = 0
    for j in range(N):
      s += count[j] * (count[j] - 1) // 2
    p.append(s)
    answer[A[i] - 1] = s
    count[A[i] - 1] += 1

for i in p:
  print(i)

Submission status: Time Limit Exceeded

Solution code:

N=int(input())
A=list(map(int,input().split()))
B=list(set(A))
C=[]
D=[]
rireki=[]
for i in B:
    k=A.count(i)
    if  not i in rireki:
        C.append(int((k-1)*(k-2)/2))
        D.append(int(k*(k-1)/2))
        rireki.append(i)

Dsum=sum(D)
for i in A:


    iindex=B.index(i)
    iindexnum=C[iindex]
    num=Dsum-D[iindex]
    ans=iindexnum+num
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    count = 0
    tmp = a.copy()
    tmp.pop(k)
    for index1, i in enumerate(tmp):
        for index2, j in enumerate(tmp):
            if (index1!=index2)&(i==j):
                count+=1
    print(int(count/2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
a=[int(A) for A in input().split()]
for k in range(1, n+1):
    ball = a[0:k-1] + a[k:]
    cnt=0
    for cc in Counter(ball).values():
        if cc <2:
            continue
        cnt += cc*(cc-1)/2
    print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
cnt = 0
s = set(A)
for i in s:
    a = A.count(i)
    if a >= 2:
        b = a*(a-1)/2
        cnt += b

for i in range(N):
    print(int(cnt-A.count(A[i])+1))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

cc = collections.Counter(A)

for i in range(N):
    k_val = A[i]
    cnt = 0

    for k, v in cc.items():
        if int(k) == k_val:
            cnt += (v-1)*(v-2)//2
        else:
            cnt += v*(v-1)//2

    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
N = int(input())
A = str(input()).split(' ')
c = collections.Counter(A)

def pick(di, s):
    di[s] -= 1
    k = int(np.sum([k*(k-1)/2 for k in di.values()]))
    di[s] += 1
    return k

  
for i in A:
    print(pick(c, i))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
dictA = {}
for x in A:
    dictA[x] = 0
for x1 in A:
    dictA[x1] += 1

for x2 in A:
    dictB = dictA.copy()
    combisum = 0
    dictB[x2] -= 1
    for x3 in dictB.values():
        combisum += x3 * (x3 - 1) // 2
    print(combisum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int,input().split()))
dic = {}
d = {}
for i in arr:
    if i not in dic:
        dic[i]=1
    else:
        dic[i]+=1
for i in arr:
    ans=0
    if i not in d:
        for j in dic.keys():
            if i==j:
                t=dic[j]-1    
            else:t = dic[j]
            ans+=(t*(t-1))//2
        d[i]=ans
    print(d[i])

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)
ans = 0
for k, v in c.items():
    if v > 1:
        ans += combinations_count(v, 2)
total_ans = ans

for i in range(N):
    k_ball = A[i]
    if c[k_ball] > 2:
        print(
            total_ans
            - (combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2))
        )
    elif c[k_ball] > 1:
        # 2
        print(total_ans - 1)
    else:
        print(total_ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys
n=int(input())
#a=[int(input()) for i in range(n)]
a = list(map(int,input().split()))
c = 0
c_list = []
for i in range(n):
    s = a.pop(i)
    
    for j in itertools.combinations(a, 2):    
        #print(j)
        if(j[0] == j[1]):
            c += 1
        
    a.insert(i,s)
    c_list.append(c)
    c = 0


for i in range(len(c_list)):
    print(c_list[i])

Submission status: Time Limit Exceeded

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
arrA = list(map(int,input().split()))

ctA = Counter(arrA)
dp = {}
cbdp = {}
for i in arrA:
    if i in dp:
        print(dp[i])
        continue
    ctA[i] -= 1
    temp = 0
    for k,v in ctA.items():
        if v<=1:
            continue
        elif v in cbdp:
            temp += cbdp[v]
        else:
            temp += v*(v-1)//2
            cbdp[v] = v*(v-1)//2
    print(temp)
    dp[i] = temp
    ctA[i] += 1

# print(*ans,sep="\n")
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Time Limit Exceeded

Solution code:
import collections

n=int(input())
As=list(map(int,input().split()))
ls=collections.Counter(As)
subd={}
for i in As:
  l=ls.values()
  ans=0
  if i not in subd.keys():
    ls[i]=ls[i]-1
    for k in l:
      ans+=k*(k-1)//2
    subd[i]=ans
    ls[i]=ls[i]+1
  else:
    ans=subd[i]
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
A_num = list(set(A.copy()))
for i in range(N):
    out = 0
    for j in A_num:
        if j == A[i]:
            out += int((A.count(j) * (A.count(j) - 1) / 2 - (A.count(j) - 1)))
        else:
            out += int((A.count(j) * (A.count(j) - 1) / 2))
    print(out)

Submission status: Time Limit Exceeded

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

for i in range(n):
    com = 0
    a2 = a[:]
    del a2[i]
    a3 = list(set(a2))
    for j in range(len(a3)):
        cnt = a2.count(a3[j])
        if cnt > 1:
            com = com + ((cnt * (cnt - 1)) / 2)
    else:
        print(int(com))

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)
#print(dict)

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 = {}
for k, v in dict.items():
    dict_ans1[k] = comb_ct(v, 2)
    dict_ans2[k] = comb_ct(v-1, 2)
#print(dict_ans1)
#print(dict_ans2)

for i_num in a:
    ans = 0
    for k,v in dict.items():
        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:
a=int(input())
ai=[int(i) for i in input().split()]

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

for i in range(len(ai)):
    print(sum([cmb(x,2) for x in [sorted(ai[:i]+ai[i+1:]).count(x) for x in set(sorted(ai[:i]+ai[i+1:])) if sorted(ai[:i]+ai[i+1:]).count(x)>1]]))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(l)
hoge = list(c.values())

for i in l:
    ans = 0
    for ix,k in enumerate(c):# key
        
        if int(k) == int(i) and hoge[ix]-1 >= 2:
            tmp = hoge[ix] - 1
            ans += (tmp * (tmp-1)) / 2
            
        elif int(k) != int(i) and hoge[ix] >= 2:
            tmp = hoge[ix]
            ans += (tmp * (tmp-1)) / 2
            
        else:
            pass
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(n):
    ans=0
    a1=a.copy()
    del a1[i]
    for k in  set(a1):
        if a1.count(k) != 1:
            ans+=combinations_count(a1.count(k),2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
l=list(map(int,input().split()))
d={}
for i in l:
    if i in d:
        d[i]+=1 
    else:
        d[i]=1 
#print(d)
for i in l:
    count=0
    for k in d.keys():
        if k==i :
            x=d[k]
            if x<3:
                continue
            x-=1
            count+=math.factorial(x)/(math.factorial(x-2)*2)
        else:
            x=d[k]
            if x<2:
                continue
            count+=math.factorial(x)/(math.factorial(x-2)*2)
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(map(int, input().split()))
B = set(A)
d = dict()

al = dict()
dl = dict()
el = dict()
ALL = 0
for b in B:
    k = A.count(b)
    d[b] = k
    al[b] = k * (k - 1) // 2
    ALL += al[b]
    dl[b] = (k - 1) * (k - 2) // 2

kl = dict()
for b in B:
    kl[b] = ALL - al[b] + dl[b]

print("\n".join(list(str(kl[a]) for a in A)))

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
import collections
#A

#B

#C


#D
N = int(input())
A = list(map(int, input().split()))
def comb(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
count = collections.Counter(A)
for i in range(N):
    arr = copy.copy(count)
    arr[A[i]] -= 1
    cnt = 0
    for j in range(N):
        if arr[j] >= 2:
           cnt += comb(arr[j],2)
    print(cnt)

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

for i in range(n):
    li_a = li.copy()
    del li_a[i]
    c = collections.Counter(li_a)
    s = list(set(li_a))
    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)
        
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import fractions
import decimal
from collections import deque
from collections import Counter
from copy import deepcopy

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())
L=list(map(str, input().split())) 
lL=deepcopy(L)
for k in range(n):
    L=deepcopy(lL)
    # print(k,len(L))
    L.pop(k)
    # print(L)
    s=Counter(L)
    ret=0
    # print(s.values())
    for a in s.values():
        if(a<=1):
            continue
        ret+=cmb(a,2)
    print(ret)

Submission status: Time Limit Exceeded

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

a_dict = {}
for a in a_list:
    if not(a in a_dict):
        a_dict[a] = 0

    a_dict[a] += 1

for i in range(n):
    k = a_list[i]
    filtered = map(lambda x: x[1]-1 if x[0]
                   == k else x[1], a_dict.items())
    filtered = map(lambda x: x*(x-1)//2, filtered)
    print(sum(filtered))

Submission status: Time Limit Exceeded

Solution code:
import collections
from collections import deque


N = int(input())
A = list(map(int,input().split()))
d = deque(A)
ans = 0
for i in range(N):
    q = d.popleft()
    a = collections.Counter(d)
    tmp = a.values()
    l = list(tmp)
    for j in range(len(l)):
        n = l[j]-1
        ans += n*(n+1)//2
    print(ans)
    ans = 0
    d.append(q)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    A_num = [0] * N
    for i in range(N):
        A_num[A[i]-1] += 1
    #print(A_num)
    A_product = [0] * N
    A_product_1 = [0] * N
    for i in range(N):
        A_product[i] = int(A_num[i] * (A_num[i]-1) / 2)
        if A_num[i] == 0:
            A_product_1[i] = 0
        else:
            A_product_1[i] = int((A_num[i]-1) * (A_num[i]-2) / 2)
    #print(A_product)
    #print(A_product_1)
    for i in range(N):
        k = A[i]
        s = sum(A_product) - A_product[k-1] + A_product_1[k-1]
        print(s)


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)
  A_set = set(A_)
  for j in A_set:
    ans += A_.count(j) * (A_.count(j) - 1) / 2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
for k in range(N):
	s=0
	for i in range(N):
		for j in range(i+1,N):
			if i!=k and j!=k:
				if A[i]==A[j]:
					s=s+1
	print(s)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

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

aset = set(al)
patterns_d = {}

for i in range(1, n+1):
    cs = copy_copy(cs_orig)
    patterns = 0
    if i in aset:
        cs[i] -= 1

        cscs = dict(ccc(cs.values()))
        for k, v in cscs.items():
            if k > 1:
                patterns += (int(k * (k-1) / 2)) * v

        patterns_d[i] = patterns

for a in al:
    print(patterns_d[a])

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

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

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)

Submission status: Time Limit Exceeded

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
import collections as cc
n=int(input())
l=list(map(int,input().split()))
ff=cc.Counter(l)
ff=dict(ff)
ans=[]
for i in range(n):
    f=ff.copy()
    f[l[i]]=max(0,f[l[i]]-1)
    t=0
    for i in f:
        x=f[i]
        t+=(x*(x-1))//2
    ans.append(t)
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

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

total = 0
for i, x in enumerate(A):
    for j, y in enumerate(A):
        if i == j:
            continue
        if x == y:
            total += 1
total = total // 2

for k, val in enumerate(A):
    count = 0
    for i, v in enumerate(A):
        if k == i:
            continue
        if val == v:
            count += 1
    print(total - count)

Submission status: Time Limit Exceeded

Solution code:
import copy

N=int(input())

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

#print(A)


for i in range(N):
    AA=copy.deepcopy(A)
    del AA[i]
    #print(AA)

    Aset=set(AA)
    count=0
    for i in Aset:
        aa=AA.count(i)
        #print(i,aa)
        count+=int((aa*(aa-1))/2)
    print(count)

Submission status: Time Limit Exceeded

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

for k in range(0,N):
    if A.count(A[k]) == 1:
        print(0)
        continue
    X = A[:k] + A[k+1:]
    Y = set(X)
    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 math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

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

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n= int(input())
    s = [int(i) for i in input().split(" ")]
    dict = {}
    for i in s:
        if i in dict:
            dict[i] = dict[i]+1
        else:
            dict[i] = 1
    sum = 0
    for i in dict.values():
        if i > 1:
            sum = sum + (i-1)*i//2
    ans = ""

    for i in s:
        num = sum
        if i in dict :
            num = num -dict[i] +1
        if ans =="":
            ans = str(num)
        else:
            ans = ans + " " + str(num)
        print(num)

Submission status: Time Limit Exceeded

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

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

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''
#import copy

#import numpy as np

import collections
n=int(input())
a = list(map(int, input().split()))
alis= collections.Counter(a)
ss=[]
for i in range(1,n+1):
    ss.append(alis[i]*(alis[i]-1)//2)

for i in range(n):
    ss[a[i]-1] = (alis[a[i]]-1)*(alis[a[i]]-2)//2
    print(sum(ss))
    ss[a[i]-1] = alis[a[i]]*(alis[a[i]]-1)//2

Submission status: Time Limit Exceeded

Solution code:
def check(A,i):
    count = 0
    for j in range(0, N):
        if j != i:
            for k in range(j+1,N):
                if k != i:
                    if A[j] == A[k]:
                        count += 1
#                         print("{} {}".format(A[j],A[k]))
    return count
    

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

for i in range(0,N):
    out = check(A,i)
    print(out)

Submission status: Time Limit Exceeded

Solution code:
import copy


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 a in A_i:
        if a not in cache:
            b = copy.deepcopy(A_i)
            b.remove(a)
            cache[a] = pairs(b)
        print(cache[a])

    return


main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

# print('input >>')
N = int(input())
A = list(map(int,(input().split())))

Ac = Counter(A)

# for k in Ac:
    # print(Ac[k])

sumc = 0
for Ae in dict(Ac):
    sumc += comb(Ac[Ae], 2)

# print('-----output-----')

for k in A:
    print(sumc - comb(Ac[k], 2) + comb(Ac[k]-1, 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.deepcopy(A)
    tmp.pop(i)

    res = 0
    for j in list(set(tmp)):
        l = tmp.count(j)
        if(l<2):
            res += 0
        else:
            res += len(list(itertools.combinations(list(range(l)),2)))

    print(res)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    a_ = a.copy()
    res = 0
    a_.pop(i)
    a_ = sorted(a_)
    a__ = set(a_)
    for j in a__:
        k = a_.count(j)
        if k != 1:
            res += math.factorial(k) // (math.factorial(k-2) * math.factorial(2))
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
B = list(map(int, input().split()))
temp = 0

setB = list(set(B))
num = len(setB)

for i in range(N):
  if i > 0:
    B[i-1] = temp
  temp = B[i]
  B[i] = 0
  res = 0
  for j in range(num):
    res += B.count(setB[j])*(B.count(setB[j])-1)/2
  print(int(res))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

count_dict = defaultdict(int)
for cal in A:
  count_dict[cal] += 1
    
    
"""
それぞれの要素について、1個減った時とそのままの場合での組み合わせ数を事前に計算しておく
"""
from math import factorial
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))
  
# list[0]=そのまま、list[1]=1個減った場合
count_result = defaultdict(list)
for k,v in count_dict.items():
  if v > 2:
    count_result[k] = [combinations_count(v,2), combinations_count(v-1,2)]
  elif v == 2:
    count_result[k] = [1,0]    
  else:
    count_result[k] = [0,0]


"""
対象の数字を指定した場合の答え、を事前に計算しておく
2*10**5
"""
all_ans = 0
for k,v in count_result.items():
  all_ans += v[0]

"""
前から順に表示していく
2*10**5
"""
for _ in A:
  # 対象の数字
  ans = all_ans - count_result[_][0] + count_result[_][1]
  print(ans)

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型で数の種類調べる
  A = list(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:
from functools import reduce
from operator import mul
import math
import copy

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

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

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 index:
		result += (count[j]*(count[j]-1))//2
	count[i] += 1
	print(result)

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

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

for i in range(N):
    result = 0
    pop = A.pop(i)
    c = collections.Counter(A)
    for k, v in c.items():
        if v > 1:
            result = result + combinations_count(v, 2)
    A.insert(i, pop)
    print(result)