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, defaultdict
N = int(input())
A = [int(i) for i in input().split()]

f_list = [1]
for i in range(1, N+1):
    f_list.append(f_list[-1]*i)

def c(n):
    return f_list[n]//f_list[n-2]//2

m = Counter(A)
ans = defaultdict(int)
s = 0
for x, y in m.items():
    if y == 1:
        ans[x] = 0
    else:
        ans[x] = c(y)
        s += ans[x]
for a in A:
    print(s - (m[a] - 1))

Submission status: Time Limit Exceeded

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

ans = list(map(lambda x: x*(x-1)//2 , box))
for j in range(N) :
    ans[A[j]] -= (box[A[j]] - 1)
    print(sum(ans))
    ans[A[j]] += (box[A[j]] - 1)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial as f

def esc(x):
    return x*(x-1)/2

re  ={}
ans ={}
n = int(input())

ordem = [int(q) for q in input().split()]

for i in ordem:
    try:
        re[i] +=1
    except:
        re[i] = 1
#até aq ok
for p in re:
    re[p]-=1
    for u in re:
        try:
            ans[p]+=esc(re[u])
        except:
            ans[p]=esc(re[u])
    re[p]+=1

for j in ordem:
    print(int(ans[j]))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial as f

def esc(x):
    return x*(x-1)/2

re  ={}
ans ={}
n = int(input())

ordem = [int(q) for q in input().split()]

val = sorted(ordem)

for i in val:
    try:
        re[i] +=1
    except:
        re[i] = 1
#até aqui ok
for p in re:
    re[p]-=1
    for u in re:
        try:
            ans[p]+=esc(re[u])
        except:
            ans[p]=esc(re[u])
    re[p]+=1

for j in ordem:
    print(int(ans[j]))

Submission status: Time Limit Exceeded

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

comb = 0
for i in range(len(b)):
  num = a.count(b[i])
  comb += num*(num-1)//2

[print(comb-a.count(a[i])+1) for i in range(len(a))]

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
from collections import defaultdict
import math
a=list(map(int,input().split()))
d=defaultdict(int)
for m in a:
  d[m]+=1
sm=0
for m in d:
  if d[m]>1:
    sm+=math.factorial(d[m])//math.factorial(d[m]-2)//2
for m in a:
  if d[m]>2:
    print(sm-math.factorial(d[m])//math.factorial(d[m]-2)//2+math.factorial(d[m]-1)//math.factorial(d[m]-3)//2)
  elif d[m]==2:
    print(sm-1)
  else:
    print(sm)

Submission status: Time Limit Exceeded

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

for i in range(n):
    ans = 0
    num_ans = []
    a_tmp1 = a[i]
    a[i] = 0
    for num in num_list:
        num1 = a.count(num)
        num_ans.append(num1)
    for tmp in num_ans:
        ans += tmp*(tmp-1)/2
    a[i] = a_tmp1
    ans = int(ans)
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    ans = 0
    num_ans = []
    a_tmp = a
    a_tmp1 = a_tmp[i]
    a_tmp[i] = 0
    for num in num_list:
        num1 = sum(x==num for x in a_tmp)
        num_ans.append(num1)
    for tmp in num_ans:
        ans += tmp*(tmp-1)/2
    a_tmp[i] = a_tmp1
    ans = int(ans)
    print(ans)

Submission status: Time Limit Exceeded

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

c=collections.Counter(b)
n,m=zip(*c.most_common())
m=list(m)
n=list(n)
q=0
for i in range(len(m)):
    t=(m[i]*(m[i]-1))//2
    q+=t

for i in range(a):
    w=m[n.index(b[i])]-1
    print(q-w)

Submission status: Time Limit Exceeded

Solution code:

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

def choose2(x):
    return int(x*(x-1)/2)
    
total = 0
for i in b:
    total += choose2(a.count(i))

for i in a:
    s = a.count(i)
    print(total - choose2(s) + choose2(s-1))

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
a = list(map(int, input().split()))
b = set(a)
s = [0]*(n+1)

def choose2(x):
    return int(x*(x-1)/2)


for i in b:
    s[i] = a.count(i)
    
total = 0
for i in s:
    total += choose2(i)

ans = [0]*(n+1)
for i in b:
    ans[i] = (total - choose2(s[i]) + choose2(s[i]-1))

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

Submission status: Time Limit Exceeded

Solution code:
import collections

def cmb(n):
    return int(n*(n-1)/2)

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

c = collections.Counter(lis)
dic = {}

for key, value in c.items():
    dic[key] = cmb(value)

for i in range(n):
    ans = sum(list(dic.values()))
    ans -= dic[lis[i]]
    
    ans += cmb(c[lis[i]] -1)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
a = list(map(int, input().split()))
b = set(a)
s = [0]*(n+1)

def choose2(x):
    return int(x*(x-1)/2)


for i in b:
    s[i] = a.count(i)
    
total = 0
for i in s:
    total += choose2(i)


for i in a:
    ans = 0
    ans = total - choose2(s[i]) + choose2(s[i]-1)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(lis)
dic = {}

for i in range(n):
    if lis[i] in dic.keys():
        continue
    else:
        if c[lis[i]] < 2:
            dic[lis[i]] = 0
        else:
            dic[lis[i]] = cmb(c[lis[i]], 2)


for i in range(n):
    ans = sum(list(dic.values()))
    ans -= dic[lis[i]]
    if c[lis[i]] <= 2:
        ans += 0
    else:
        ans += cmb(c[lis[i]]-1, 2)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(lis)


for i in range(n):
    ans = 0
    a = lis[i]
    for key, value in c.items():
        if key == a:
            if value-1 < 2:
                ans += 0
            else:
                ans += cmb(value-1, 2)
        else:
            if value == 1:
                ans += 0
            else:
                ans += cmb(value, 2)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)
# print(uni)
# print(cou)
com=[]
for i in range(len(cou)):
    nforc=cou[i]
    c=nforc*(nforc-1)//2
    com.append(c)
# com=np.array(com)
# print(com)
totalcombination=sum(com)

'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''
for j in range (N):
    n=A[j] #nはAの数列の数字
    f=np.where(uni==n)[0][0]
    t=com[f]
    nfore=cou[f]
    e=(nfore-1)*(nfore-2)//2
    print(totalcombination-t+e)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)
# print(uni)
# print(cou)
com=[]
for i in range(len(cou)):
    nforc=cou[i]
    c=nforc*(nforc-1)//2
    com.append(c)
# com=np.array(com)
# print(com)
totalcombination=sum(com)

'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''
for j in range (N):
    n=A[j] #nはAの数列の数字
    f=np.where(uni==n)[0][0]
    t=com[f]
    e=(cou[f]-1)*(cou[f]-2)//2
    print(totalcombination-t+e)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def combinations_count(n, r):
    #r = min(r, n - r)
    if r > n:
        return 0
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

d = dict()
for a in A:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1

if len(d) == n:
    for i in range(n):
        print(0)
    exit()

d1 = dict()
for a in d.keys():
    d1[a] = combinations_count(d[a], 2)

ans = []
d3 = dict()
for a in A:
    if a not in d3.keys():
        ans.append(sum(d1.values()) - (d[a] - 1))
    else:
        ans.append(d3[a])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def combinations_count(n, r):
    #r = min(r, n - r)
    if r > n:
        return 0
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

d = dict()
for a in A:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1

if len(d) == n:
    for i in range(n):
        print(0)
    exit()

d1 = dict()
for a in d.keys():
    d1[a] = combinations_count(d[a], 2)

d2 = dict()
for a in d.keys():
    d2[a] = combinations_count(d[a] - 1, 2)

ans = []
d3 = dict()
for a in A:
    if a not in d3.keys():
        ans.append(sum(d1.values()) - d1[a] + d2[a])
    else:
        ans.append(d3[a])

for a in ans:
    print(a)

Submission status: Time Limit Exceeded

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

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0
    
for i in range(N):
    lst[A[i]] += 1

llst = dict(lst) # llst は、valueが1のものを消したリスト
for i in lst:
    if lst[i] == 1:
        llst.pop(i)

c = dict(lst) # c は keyの値である場合の組み合わせの数。
              # c = dict(lst)で異なるオブジェクトとして等価なcを定義
              # c = lst だと、同一のオブジェクトとなってしまう？

for i in lst:
    c[i] = 0
    tmp_lst = dict(llst)
    if lst[i] != 1:
        tmp_lst[i] -= 1
    for j in tmp_lst:
        c[i] += tmp_lst[j]*(tmp_lst[j]-1)//2
        
for i in range(N):
    print(c[A[i]])

Submission status: Time Limit Exceeded

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

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

Aset = set(A)
lst = {}
for i in Aset:
    lst[i] = 0

# print(lst)
    
for i in range(N):
    lst[A[i]] += 1

# print(lst)
c = dict(lst) # c = dict(lst)で異なるオブジェクトとして等価なcを定義
              # c = lst だと、同一のオブジェクトとなってしまう？

for i in lst:
#     print('i: {}'.format(i))
    c[i] = 0
    tmp_lst = dict(lst)
#     print('tmp_lst {}'.format(tmp_lst))
#     print('tmp_lst[i]: {}'.format(tmp_lst[i]))
    tmp_lst[i] -= 1
#     print('tmp_lst[i]: {}'.format(tmp_lst[i]))
    for j in tmp_lst:
#         print(tmp_lst[j])
        c[i] += tmp_lst[j]*(tmp_lst[j]-1)//2
#         print('c[i]: {}'.format(c[i]))
        
for i in range(N):
    print(int(c[A[i]]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)
# print(uni)
# print(cou)
com=[]
for i in range(len(cou)):
    c=(cou[i]*(cou[i]-1))//2
    com.append(c)
# com=np.array(com)
# print(com)
totalcombination=sum(com)

'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''
for j in range (N):
    n=A[j] #nはAの数列の数字
    f=np.where(uni==n)[0][0]
    t=com[f]
    e=(cou[f]-1)*(cou[f]-2)//2
    print(totalcombination-t+e)

Submission status: Time Limit Exceeded

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

from collections import Counter
count = Counter(A)
 
s = 0
for c in list(count.values()):
    s += c*(c-1)/2

ans = 0
for a in A:
    n = A.count(a)
    ans = s - n*(n-1)/2 + (n-1)*(n-2)/2
    print(int(ans))

Submission status: Time Limit Exceeded

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

set_a  = set(A)
count_l = []
for a in set_a:
    count_l.append(A.count(a))
 
s = 0
for c in count_l:
    s += c*(c-1)/2

ans = 0
for a in A:
    n = A.count(a)
    ans = s - n*(n-1)/2 + (n-1)*(n-2)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def comb(n, r):
    if n<r:
        return 0
    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())
A = list(map(int,input().split()))
D = {}
for k in A:
    D[k] = A.count(k)

for k in A:
    count = 0
    for j in D.keys():
        if k == j:
            count += comb(D[j]-1,2)
        else:
            count += comb(D[j],2)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


def inlist():
    return(list(map(int, input().split())))


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


def def_value():
    return 0


def ncr(n, r):
    if r > n:
        return 0
    return n*(n-1) // 2


n = inp()
a = inlist()

count = defaultdict(def_value)

for i in a:
    count[i] += 1

for i in range(n):
    res = 0
    for k in count:
        x = count[k]
        if k == a[i]:
            x = x - 1

        res += ncr(x, 2)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = []
total = 0

m = max(a)
mi = min(a)

if mi == m:
    total = ((len(a)-1)*(len(a)-2))/2
    for _ in range(n):
        print(int(total))

else:
    for i in range(mi, m+1):
        r = a.count(i)
        if r:
            total += (r*(r-1))/2
        l.append(r)

    #print(l)
    #print(total)

    for k in range(n):
        score = total
        if l[a[k]-1]:
            score -= (l[a[k]-1]-1)
        print(int(score))

Submission status: Time Limit Exceeded

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

count = Counter(a)
for i in a:
  ans = 0
  for j in count.items():
    
    if i == j[0]:
      ans += (j[1]-1)*(j[1]-2)//2
    else:
      ans += j[1]*(j[1]-1)//2
  print(ans)

Submission status: Time Limit Exceeded

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

for n in c.items():
  total += n[1]*(n[1]-1)//2

for i in range(N):
  print(total-A.count(A[i]))

Submission status: Time Limit Exceeded

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

for n in c.items():
  if n[1] > 1:
    total += n[1]*(n[1]-1)//2

for i in range(N):
  ans = total
  ans -= A.count(A[i])-1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
total = 0

for n in c.items():
  if n[1] > 1:
    total += math.factorial(n[1]) // (math.factorial(n[1]-2) * 2)

for i in range(N):
  ans = total
  ans -= A.count(A[i])-1
  print(ans)

Submission status: Time Limit Exceeded

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

l = [math.factorial(i[1]) // (math.factorial(i[1]-2) * 2) for i in c.items() if i[1] > 1]
ans = sum(l)

for i in range(N):
  ans -= A.count(A[i])-1
  print(ans)
  ans = sum(l)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
N = int(input())
A = list(map(int, input().split()))
ans = 0

for i in range(N):
  li = A[:i] + A[i+1:]
  c = collections.Counter(li)
  l = [math.factorial(i[1]) // (math.factorial(i[1]-2) * 2) for i in c.items() if i[1] > 1]
  print(sum(l))
  ans = 0

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
N = int(input())
A = list(map(int, input().split()))
ans = 0

for i in range(N):
  li = A[:i] + A[i+1:]
  c = collections.Counter(li)
  l = [i[1] for i in c.items() if i[1] > 1]
  for num in l:
    ans += math.factorial(num) // (math.factorial(num-2) * 2)
  print(ans)
  ans = 0

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
C=Counter(a)
#print(C)
for i in range(n):
    D=C.copy()
    D[a[i]]-=1
    #print(D,C)
    ans=0
    for k,v in D.items():
        if v>=2:
            ans+=v*(v-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    C=Counter(a)
    C[a[i]]-=1
    #print(C)
    ans=0
    for k,v in C.items():
        if v>=2:
            ans+=v*(v-1)//2
    print(ans)

Submission status: Time Limit Exceeded

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


for i in range(N):
  a = A.copy()
  a[i] = 0
  c = collections.Counter(a)
  c = c.items()


  ans = 0
  for m,n in c:
    
    ans += n*(n-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

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

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

c = Counter(a)
d = {}
for each in c:
  d[each] = combinations_count(c[each], 2)
  
for each in a:
  tmp = sum(d.values()) - d[each]
  tmp += combinations_count(c[each]-1, 2)
  print(tmp)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

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

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

c = Counter(a)
counts = c.most_common()
d = {}
for each in counts:
  d[each[0]] = combinations_count(each[1], 2)
  
for each in a:
  tmp = sum(d.values()) - d[each]
  tmp += combinations_count(c[each]-1, 2)
  print(tmp)

Submission status: Time Limit Exceeded

Solution code:
def cmb(n):
  over = n * (n - 1)
  under = 2
  return over // under

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

aggre = [0] * N
prec1 = [0] * N
prec2 = [0] * N

for a in A:
  aggre[a - 1] += 1

uniq = sorted(list(set(aggre)))[1:]

for u in uniq:
  if u < 2:
    continue
  prec1[u - 1] = cmb(u)
  prec2[u - 1] = cmb(u - 1)
"""
print(aggre)
print(uniq)
print(prec1)
print(prec2)
"""
num1 = [0] * N
num2 = [0] * N

for a in A:
  total = aggre[a - 1] - 1
  num1[a - 1] = prec1[total]
  num2[a - 1] = prec2[total]
  
for a in A:
  print(sum(num1) - num1[a - 1] + num2[a - 1])

Submission status: Time Limit Exceeded

Solution code:
def cmb(n):
  over = n * (n - 1)
  under = 2
  return over // under

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

aggre = [0] * N
prec1 = [0] * N
prec2 = [0] * N

for a in A:
  aggre[a - 1] += 1

uniq = sorted(list(set(aggre)))[1:]

for u in uniq:
  if u < 2:
    continue
  prec1[u - 1] = cmb(u)
  prec2[u - 1] = cmb(u - 1)
"""
print(aggre)
print(uniq)
print(prec1)
print(prec2)
"""
num1 = [0] * N
num2 = [0] * N

for a in A:
  total = aggre[a - 1] - 1
  num1[a - 1] = prec1[total]
  num2[a - 1] = prec2[total]
  
  
for a in A:
  print(sum(num1) - num1[a - 1] + num2[a - 1])

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
C=list(set(L))
x=0
for i in range(len(C)):
  a = L.count(C[i])
  x += int(a*(a-1)/2)  
for K in range(N):
  print(x-L.count(L[K])+1)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
C=list(set(L))
x=0
for i in range(len(C)):
  x += int((L.count(C[i]))*(L.count(C[i])-1)/2)  
for K in range(N):
  print(x-L.count(L[K])+1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
count=[0]*N
C=list(set(L))
x=0
d=[0]*len(C)
e=[0]*N
for i in range(len(C)):
  d[i] = int((L.count(C[i]))*(L.count(C[i])-1)/2)
  x += d[i]  
for K in range(N):
  e[K] = L.count(L[K])-1
  print(x-e[K])

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
count=[0]*N
C=list(set(L))
x=0
d=[0]*len(C)
e=[0]*N
for i in range(len(C)):
  d[i] = int((L.count(C[i]))*(L.count(C[i])-1)/2)
  x += d[i]  
for K in range(N):
  e[K] = L.count(L[K])-1
  print(x-e[K])

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(sys.stdin.readline())
L=list(map(int,sys.stdin.readline().split()))
count=[0]*N
for K in range(N):
  B = L[0:K]+L[K+1:] #popを使うとなぜか元のAまで変化してしまう
  C=list(set(L[0:K]+L[K+1:]))
  d = [0]*len(C)
  x=0
  for i in range(len(C)):
    d[i] = int((B.count(C[i]))*(B.count(C[i])-1)/2)
    x += d[i]
  print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(sys.stdin.readline())
A=list(map(int,sys.stdin.readline().split()))
count=[0]*N
def f(K,L):
  B = L[0:K]+L[K+1:] #popを使うとなぜか元のAまで変化してしまう
  C=list(set(B))
  d = [0]*len(C)
  x=0
  for i in range(len(C)):
    d[i] = int((B.count(C[i]))*(B.count(C[i])-1)/2)
    x += d[i]
  return x

for k in range(N):
  count[k]=f(k,A)

for i in count:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=[0]*N
def f(K,L):
  B = L[0:K]+L[K+1:] #popを使うとなぜか元のAまで変化してしまう
  C=list(set(B))
  d = [0]*len(C)
  x=0
  for i in range(len(C)):
    d[i] = int((B.count(C[i]))*(B.count(C[i])-1)/2)
    x += d[i]
  return x


for k in range(N):
  count[k]=f(k,A)

for i in count:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=[0]*N
def f(K,L):
  B = L[0:K]+L[K+1:] #popを使うとなぜか元のAまで変化してしまう
  C=list(set(B))
  c = [0]*len(C)
  d = [0]*len(C)
  x=0
  for i in range(len(C)):
    c[i] = B.count(C[i])
    d[i] = int((c[i])*(c[i]-1)/2)
    x += d[i]
  return x


for k in range(N):
  count[k]=f(k,A)

for i in count:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=[0]*N
def f(K,L):
  B = L[0:K]+L[K+1:] #popを使うとなぜか元のAまで変化してしまう
  C=list(set(B))
  c = [0]*len(C)
  d = [0]*len(C)
  x=0
  for i in range(len(C)):
    c[i] = B.count(C[i])
    d[i] = int((c[i])*(c[i]-1)/2)
    x += d[i]
  return x


for k in range(N):
  count[k]=f(k,A)

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=[0]*N
def f(K,L):
  B = L[0:K]+L[K+1:] #popを使うとなぜか元のAまで変化してしまう
  C=list(set(B))
  c = [0]*len(C)
  d = [0]*len(C)
  for i in range(len(C)):
    c[i] = B.count(C[i])
    d[i] = int((c[i])*(c[i]-1)/2)
  return sum(d)

for k in range(N):
  count[k]=f(k,A)

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=[]
for K in range(N):
  a=0
  for i in range(N):
    for j in range(N-1-i):
      if A[i] == A[j+1+i] and (i != K and (j+1+i) != K):
#        print("start")
#        print(i)
#        print(j+1+i)
#        print(K)
#        print(A[i] == A[j+1+i] and (i != K or (j+1+i) != K))
        a += 1
#        print(a)
  count.append(a)
for i in range(len(count)):
  print(count[i])

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    sum = 0
    b = copy.copy(a)
    b.pop(i)
    c = list(set(b))
    for j in c:
        sum += int(b.count(j) * (b.count(j) - 1) / 2)
    print(sum)

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):
    sum = 0
    b = copy.copy(a)
    b.pop(i)
    c = collections.Counter(b)
    for j in c.values():
       sum += j * (j - 1) /2
    print(int(sum))

Submission status: Time Limit Exceeded

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


for _ in range(n):
    ans = 0
    a = a_l.pop(0)
    a_set = set(a_l)
    for j in a_set:
        
        count = a_l.count(j)
        ans += count*(count-1)*(1/2)

    print(int(ans))
    a_l.append(a)

Submission status: Time Limit Exceeded

Solution code:
import math
from itertools import permutations

def P(n):
    return math.factorial(n)//math.factorial(n-2)
def C(n):
    return P(n)//2
n = int(input())
a = list(map(int,input().split()))
c = [0] * n
for i in range(n):
    c[a[i]-1] += 1
ans = 0
for i in range(n):
    if c[i] > 1:
        ans += C(c[i])
for i in range(n):
    if c[a[i]-1] > 2:
        print(ans - C(c[a[i]-1]) + C(c[a[i]-1]-1))
    elif c[a[i]-1] == 2:
        print(ans - 1)
    else:
        print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(1, N + 1):
    B = A[: i - 1] + A[i:]
    #print(B)
    for j in set(B):
        C = B.count(j)
        if C >= 2:
            ans += C * (C - 1) / 2
    print(int(ans))
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
temp=[0]*n
for i in range(n):
  temp[i]=a.count(a[i])-1
ans=sum(temp)//2
for i in range(n):
  print(ans-temp[i])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
temp=[0]*n
for i in range(n):
  cnt=0
  for j in range(n):
    if a[i]==a[j]:
      cnt=cnt+1
  temp[i]=cnt-1
ans=sum(temp)//2
for i in range(n):
  print(ans-temp[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math


m2 = math.factorial(2)

def nC2(n):
    return math.factorial(n) // (math.factorial(n - 2) * m2)


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

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0, 0)
    else:
        c = nC2(v)
        ans += c
        if v - 1 < 2:
            ac[k] = (v, c, 0)
        else:
            ac[k] = (v, c, nC2(v - 1))

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + v[2])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math


def nC2(n):
    return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))


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

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0, 0)
    else:
        c = nC2(v)
        ans += c
        if v - 1 < 2:
            ac[k] = (v, c, 0)
        else:
            ac[k] = (v, c, nC2(v - 1))

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + v[2])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math


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


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

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0, 0)
    else:
        c = nCr(v, 2)
        ans += c
        if v - 1 < 2:
            ac[k] = (v, c, 0)
        else:
            ac[k] = (v, c, nCr(v - 1, 2))

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + v[2])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math


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


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

ac = dict(Counter(a))

ans = 0
for k, v in ac.items():
    if v < 2:
        ac[k] = (v, 0)
    else:
        c = nCr(v, 2)
        ac[k] = (v, c)
        ans += c

for i in a:
    v = ac[i]
    if v[0] - 1 < 2:
        print(ans - v[1])
    else:
        print(ans - v[1] + nCr(v[0] - 1, 2))

Submission status: Time Limit Exceeded

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


def main():
    # input
    n = int(readline().rstrip())
    aList = list(map(int, readline().rstrip().split()))
    aAllList = set(aList)
    aConbiDic = {}
    aDic = {}
    aSum = 0
    for a in aAllList:
        aCount = aList.count(a)
        aDic[str(a)] = aCount
        aConbiDic[str(a)] = (aCount * (aCount - 1)) // 2
        aSum += (aCount * (aCount - 1)) // 2

    for a in aList:
        print(aSum - aDic[str(a)] + 1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import math
def comb(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()))

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

combi = defaultdict(int)
for a in count:
    combi[a] = comb(count[a],2)

for i in range(n):
    print(sum(list(combi.values())) - combi[A[i]] + comb(count[A[i]]-1, 2))

Submission status: Time Limit Exceeded

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

from collections import Counter
c = Counter(a)

for i in range(n):
   c[a[i]] -= 1
   tmp = [x*(x-1)//2 for x in c.values()]
   print(sum(tmp))
   c[a[i]] += 1