Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

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

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

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for k in range(n):
    c=0
    b=a.copy()
    a.pop(k)
    for i in range(len(a)):
        for j in range(i+1,len(a)):
            if a[i]==a[j]:
                c+=1
    print(int(c))
    a=b

Submission status: Time Limit Exceeded

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

temp=0

for i in range(n):
    c=a.count(i)
    temp=temp+int(c*(c-1)/2)

for k in range(n):
    ch=a[k]
    c2=a.count(ch)
    if c2==0 or c2==1:
        print(temp)
    else:
        ans=temp-int(c2*(c2-1)/2)+int((c2-1)*(c2-2)/2)
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve(d, except_num):
    result = 0
    for k, v in d.items():
        if(k == except_num):
            result += (v-1) * (v-2) / 2
        else:
            result += v * (v-1) / 2
    return int(result)


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

d = {}

for a in A:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1

# print(d)

for i, a in enumerate(A):
    print(solve(d, a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
dic2_a={}
for key in dic_a:
  if dic_a[key]>=2:
    dic2_a[key]=dic_a[key]
    
#print(dic2_a)
dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  dic2_a_copy=dic2_a.copy()
  if a in dic2_a_copy:
    dic2_a_copy[a]-=1
  
  #print(dic_a_copy)
  answer=0
  for val in dic2_a_copy.values():
    answer+=val*(val-1)//2
    
  print(answer)
  dic_answer[a]=answer

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b,c=[],[]
for i in a:
    if i in b:
        c[b.index(i)]+=1
    else:
        b+=[i]
        c+=[1]
for l in a:
    s=0
    for i in range(len(b)):
        p,q=b[i],c[i]
        if p==l:
            s+=(q-1)*(q-2)//2
        else:
            s+=q*(q-1)//2
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

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

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

for i in range(N):
    balls = BALLS[:i] + BALLS[i+1:]
    print(sum(fact(n) for n in collections.Counter(balls).values()))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import math


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


n = int(input())
al = list(map(str, input().split()))
d = defaultdict(lambda: 0)
dd = defaultdict(lambda: 0)
for a in al:
    d[a] += 1

sam = 0
for key, value in d.items():
    dd[key] = combinations_count(value, 2)
    sam += dd[key]
# print(d)
# print(dd)
# print(sam)
for a in al:
    value = d[a]
    print(sam - dd[a] + combinations_count(value-1, 2))

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np

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

cnt = collections.Counter(A)

def cmb(n):
    return n * (n - 1) // 2
    
for i in range(N):
    tmp = cnt.copy()
    tmp[A[i]] -= 1
    ans = int(np.sum(np.array([cmb(k) for k in tmp.values() if k >= 2])))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

if len(a) == len(set(a)):
    for i in range(n):
        print(0)
else:
    for i in range(n):
        ans = 0
        temp = a[:i] + a[i + 1:]
        kw = Counter(temp)
        for k in kw.values():
            if k != 1:
                ans += k * (k - 1) // 2

        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
def input_int_list():
    return [int(thing) for thing in input().split(" ")]

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

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



N=int(input())
A_list=input_int_list()
int_count=collections.Counter(A_list)
for i in range(N):
    number=A_list[i]
    int_count[number]=int_count[number]-1
    print(sum([nC2(int_count[i]) for i in int_count]))
    int_count[number]=int_count[number]+1

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
import collections

N = int(input())
A = list(map(int, input().split()))
def comb(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))

for i in range(N):
    cnt = 0
    arr = copy.copy(A)
    arr.pop(i)
    c = collections.Counter(arr)
    for j in range(N):
        if c[j] >= 2:
           cnt += comb(c[j],2)
    print(cnt)

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()
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
for a in A:
    print(ALL - al[a] + dl[a])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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


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


res_dict = dict([])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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


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

counter = Counter(A)

for i in range(n):
    ans = 0
    for k, v in counter.items():
        if k == A[i]:
            v -= 1
        ans += nCr(v, 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
    B=A.copy()
    B.remove(A[i])
    count=0
    for v in itertools.combinations(B,2):
        #print(v[0],v[1])
        if v[0]==v[1]:
            count+=1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
A = read_int()

d = defaultdict(lambda: int())

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

def solve():
    all = 0
    for a in A:
        d[a] += 1
    for _, v in d.items():
        all += combinations_count(v, 2)
    for a in A:
        res = all - combinations_count(d[a], 2) + combinations_count(d[a] - 1, 2)
        print(res)
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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


def solve(N: int, A: "List[int]"):
    from collections import Counter
    import math
    def combination(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    c = Counter(A)

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



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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

"""
前から順に表示していく
"""

for _ in A:
  # 対象の数字
  ans = 0
  for k,v in count_result.items():
    if k == _:
      ans += v[1]
    else:
      ans += v[0]

  print(ans)

Submission status: Time Limit Exceeded

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

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

count_array = np.zeros(len(a_list))

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

Submission status: Time Limit Exceeded

Solution code:
def f(x):
    return x*(x-1)//2

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

d = collections.defaultdict(int)
for i in a:
    d[i]+=1
c = collections.defaultdict(int)
for i in set(a):
    ans=0
    for j in d:
        if j==i:
            ans+=f(d[j]-1)
        else:
            ans+=f(d[j])
    c[i]=ans

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

Submission status: Time Limit Exceeded

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

from collections import Counter
AC=Counter(A)

for a in A:
    ACT=AC.copy()
    ACT[a]-=1
    print(int(sum(map(lambda x: x*(x-1)/2, ACT.values()))))

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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


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

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

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
dic2_a={}
for key in dic_a:
  if dic_a[key]>=2:
    dic2_a[key]=dic_a[key]
    
#print(dic2_a)
for a in alist:
  dic2_a_copy=dic2_a.copy()
  if a in dic2_a_copy:
    dic2_a_copy[a]-=1
  
  #print(dic_a_copy)
  answer=0
  for val in dic2_a_copy.values():
    answer+=val*(val-1)//2
    
  print(answer)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
def comb(n, r):
    # print(n, r)
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

# comb = Combination(n-1)
c = Counter(a)

for i in range(n):
    num = a[i]
    # c[num] - 1
    temp = 0
    for (key, val) in c.items():
        # print('\n', key, val)
        if val < 2:
            continue
        if key == num:
            if val > 2:
                temp += comb(val - 1, 2)
        else:
            temp += comb(val, 2)
    print(temp)

'''
5
1 1 2 1 2
'''

Submission status: Time Limit Exceeded

Solution code:
import collections
def main():
    n = int(input())
    a = list(map(int,input().split()))
    cnt = collections.Counter(a)
    cnt_m = cnt.most_common()
    if cnt.most_common()[0][1] == 1:
        for i in range(n):
            print(0)

        return

    for i, aa in enumerate(a):
        ans = 0
        for j in range(len(cnt)):
            cnt_a = cnt_m[j][0]
            cnt_num = cnt_m[j][1]
            if cnt_num == 1:
                break
            else:
                if cnt_a == aa:
                    cnt_num -= 1
                ans += cnt_num * (cnt_num - 1)
        print(ans//2)


if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

for k in range(1, N + 1):
    tmp_divided = copy.deepcopy(divided)
    tmp_divided[A_list[k - 1] - 1] -= 1

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

    print(ans)

Submission status: Time Limit Exceeded

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

for key in dic.keys():
    tmp = 0
    for key2 in dic.keys():
        if key == key2:
            tmp += (dic[key2]-1) * (dic[key2]-2) // 2
        else:
            tmp += (dic[key2]) * (dic[key2]-1) // 2
    ans[key] = tmp

for i in range(n):
    print(ans[a[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()))
for i in range(n):
    l = [a[t] for t in range(n) if t != i]
    x = collections.Counter(l).values()
    count = 0
    for j in x:
        if j < 2:
            pass
        else:

            count += combinations_count(j,2)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

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

for i in a:
    ans = 0
    for k in range(len(ax)):
        #ns = ax[]
        ans += count(list(ax.values())[k] - (list(ax.keys())[k] == i), 2)
    print(ans)

Submission status: Time Limit Exceeded

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

from collections import Counter
import copy

for i in range(N):
    num = copy.copy(A)
    num.pop(i)
    c = Counter(num)
    keys, values = zip(*c.most_common())
    ans = 0
    for j in range(len(values)):
        ans += values[j]*(values[j]-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(N)]

total = 0

for i in range(N):
  r = 0
  for j in range(N):
    if i != j and A[i] == A[j]:
      r += 1
  L[i] = r
  total += r

total = total // 2

for i in L:
  print(total - i)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

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

count = Counter(A)
check = {}
for num in A:
    ans = 0
    if num in check.keys():
        ans += check[num]
    else:
        for key, value in count.items():
            if key == num:
                ans += (value-1)*(value-2)//2
            else:
                ans += value*(value-1)//2
        check[num] = ans
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for k in range(n):
    c=0
    b=a.copy()
    a.pop(k)
    for i in range(len(a)):
        for j in range(len(a)):
            if a[i]==a[j] and i!=j:
                c+=1
    print(int(c/2))
    a=b

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().strip().split(" ")]

from collections import defaultdict

count = defaultdict(int)
for i in A:
    count[i] += 1

for i in A:
    count[i] -= 1
    tmp = 0
    for j in count.values():
        tmp += j * (j-1) // 2
    print(tmp)
    count[i] += 1

Submission status: Time Limit Exceeded

Solution code:
# list(map(int,input().split()))
from math import factorial as f
from collections import Counter
from functools import lru_cache


@lru_cache()
def c(n, r=2):
	if n <= 1:
		return 0
	else:
		return f(n) // (f(n - r) * f(r))


def solve(cnt):
	ans = sum(map(c, cnt.values()))
	return ans


@lru_cache()
def solve2(a):
	cnt[a] -= 1
	ans = solve(cnt)
	cnt[a] += 1
	return ans


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

cnt = Counter(arr)
for i in range(n):
	print(solve2(arr[i]))

Submission status: Time Limit Exceeded

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

for k in range(0,N):
    X = A[:k] + A[k+1:]
    c = 0
    for a in range(0,N-1):
        for b in range(a+1,N-1):
            if X[a] == X[b]:
                c +=1
    print(c)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
def nc2(n):
	if n<2:
		return 0
	else:
		return (n*(n-1))//2

n = int(input())
arr = list(map(int,input().split()))
d = {}
for i in range(n):
	try:
		d[arr[i]].append(i)
	except KeyError:
		d[arr[i]] = [i]

done = {}
for i in range(n):
	try:
		if done[arr[i]] > -1:
			continue
	except KeyError:
		ans = 0
		for j in d.keys():
			if j == arr[i]:
				ans+= nc2(len(d[j])-1)
			else:
				ans+= nc2(len(d[j]))
		done[arr[i]] = int(ans)

for i in arr:
	print(done[i])

Submission status: Time Limit Exceeded

Solution code:
def combination(n,r,mod=10**9+7):
    n1 = n+1
    r = 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())
A = list(map(int,input().split()))

num = {}
for i in A:
    if i not in num:
        num[i] = 1
    else:
        num[i] += 1


values = list(num.values())
keys = list(num.keys())
S = 0
for i in range(len(keys)):
    if values[i] >= 2:
        S += combination(values[i],2)

for i in range(N):
    if num[A[i]] >= 3:
        print(S-combination(num[A[i]],2)+combination(num[A[i]]-1,2))
    elif num[A[i]] == 2:
        print(S-1)
    else:
        print(S)

Submission status: Time Limit Exceeded

Solution code:
def f(x):
    return x*(x-1)//2

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

d = collections.defaultdict(int)
for i in a:
    d[i]+=1

for i in a:
    ans=0
    for j in d:
        if j==i:
            ans+=f(d[j]-1)
        else:
            ans+=f(d[j])
    print(ans)

Submission status: Time Limit Exceeded

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

result = {}

for k in range(n):
    if aa[k] in result:
        print(result[aa[k]])
        continue

    count = 0
    for i in range(n):
        if i == k:
            continue
        for j in range(i + 1, n):
            if j == k:
                continue
            if aa[i] == aa[j]:
                count += 1
    print(count)
    result[aa[k]] = count

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial


def perm(n, r):
    return factorial(n) // factorial(n - r)


def comb(n, r):
    if n < r:
        return 0
    return perm(n, r) // factorial(r)


N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
D = {k: comb(v, 2) for k, v in C.items()}
_all = sum(D.values())
for a in A:
    print(_all - D[a] + comb(C[a] - 1, 2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
As = [int(n) for n in input().split()]

C = Counter(As)

L = [(v * (v - 1) // 2, (v - 1) * (v - 2) // 2, k) for k, v in C.items()]

for i in range(N):
    print(sum([y if z == As[i] else x for x, y, z in L]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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


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

for n in A_array:
    print(calc_comb(n, Ai_dict))

Submission status: Time Limit Exceeded

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

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



# print(P)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))

n = INT()
a = LIST()
c = list(set(a))
b = [0 for _ in range(n + 1)]
for i in range(n):
    b[a[i]] += 1
for k in range(n):
    ans = 0
    for i in c:
        tmp = b[i]
        if i == a[k]:
            tmp -= 1
        if tmp >= 2:
            ans += tmp * (tmp - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

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

    for i in range(N):
        cnt = 0

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



resolve()

Submission status: Time Limit Exceeded

Solution code:
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):
  ans = 0
  d[a[i]] -= 1
  for j in d.values():
    ans += j*(j-1)//2
  print(ans)
  d[a[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
# ABC159d


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

    from collections import Counter
    import math

    def c_count(n, r):  # 組み合わせ
        if n - r < 0:
            return 0
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    class Combination:  # メモ化組み合わせ
        def __init__(self):
            super().__init__()
            self.dp = dict()

        def count(self, n, r):
            if self.dp.get((n, r)) != None:
                return self.dp[(n, r)]
            self.dp[(n, r)] = c_count(n, r)
            return self.dp[(n, r)]

    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    d = Combination()
    e = dict()

    ss = 0

    for i in c:
        #print(i, c[i])
        e[i] = d.count(c[i], 2)
        # print(e[i])
        ss += e[i]

    for i in range(n):

        print(ss-d.count(c[a[i]], 2) + d.count(c[a[i]]-1, 2))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())
A = [int(v) for v in input().strip().split()]
C = {}

for a in A:
    C.setdefault(a, 0)
    C[a] += 1

for i, a in enumerate(A):
    C[a] -= 1
    sum_ = sum([n * (n - 1) // 2 for n in C.values() if n > 1])
    C[a] += 1
    print(sum_)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

for k in range(1, N+1):
    ans = 0
    left = A[:k-1]
    right = A[k:]
    array = left + right
    
    array.sort()
    count = 1
    for i in range(N - 2):
        if array[i] == array[i+1]:
            count += 1
        else:
            ans += count*(count-1) // 2
            count = 1
    
    ans += count*(count-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

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


def solve(N: int, A: "List[int]"):
    from collections import Counter
    import math
    def combination(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    c = Counter(A)

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



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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
def cmb(n, r):
    if n <= 0 or n < r:
        return 0
    if n == r or r == 0:
        return 1
    if r == 1:
        return n
    return cmb(n-1,r-1) + cmb(n-1,r)
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnts = [0]*(2*10**5 + 1)
    for v in a:
        cnts[v] += 1
    buf = 0
    for i in range(2*10**5 + 1):
        if cnts[i] > 0:
            buf += cmb(cnts[i], 2)
    for i in range(n):
        x = a[i]
        if cnts[x] <= 1:
            print(buf)
        else:
            ans = buf
            ans -= cmb(cnts[x], 2)
            print(ans + cmb(cnts[x]-1, 2))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(len(a)):
    t = a[:i] + a[i+1:]
    ans = 0
    c = collections.Counter(t)
    for j in c.values():
        ans += j * (j-1) // 2

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
def ncr(n,r):
	if n<r:
		return 0
	else:
		return factorial(n)/(factorial(n-r)*factorial(r))

n = int(input())
arr = list(map(int,input().split()))
d = {}
for i in range(n):
	try:
		d[arr[i]].append(i)
	except KeyError:
		d[arr[i]] = [i]

done = {}
for i in range(n):
	try:
		if done[arr[i]] > -1:
			continue
	except KeyError:
		ans = 0
		for j in d.keys():
			if j == arr[i]:
				ans+= ncr(len(d[j])-1,2)
			else:
				ans+= ncr(len(d[j]),2)
		done[arr[i]] = int(ans)

for i in arr:
	print(done[i])

Submission status: Time Limit Exceeded

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

for i in range(N):
  ans = 0
  tmpA = A[:]
  del tmpA[i]
  setA = set(tmpA)
  for a in setA:
    c = tmpA.count(a)
    if c > 1:
      ans += c * (c-1) // 2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b={}
for i in range(n):
    if a[i] in b:
        b[a[i]]+=1
    else:
        b[a[i]]=1

for i in range(n):
    s = 0
    b[a[i]]-=1
    for key,val in b.items():
        if val>1:
            s+=val*(val-1)//2
    b[a[i]]+=1
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

dic = defaultdict(int)
def calc_patern(n, dic):
    ret = 0
    for k, v in dic.items():
        if int(k) != n:
            ret += v * (v - 1) / 2
        else:
            ret += (v - 1) * (v - 2) / 2
    return ret
for i in range(n):
    dic[a[i]] += 1

for i in range(n):
    print(int(calc_patern(a[i], dic)))