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

num_cnt = [0]*n
res = 0
for num in range(n):
    cnt = a.count(num+1)
    num_cnt[num] = cnt
    if cnt > 0:
        res += cnt*(cnt-1)//2

for a_k in a:
    cnt = num_cnt[a_k-1]
    print(res - (cnt-1)*(cnt - (cnt-2))//2)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
c = [0 for _ in range(max(a))]
for k, v in d.items():
    c[k - 1] = v * (v - 1) / 2

for i in range(n):
    ans = sum(c)
    p = a[i]
    res = c[p - 1] * (d[p] - 2) / d[p]
    ans -= c[p - 1] - res
    print(int(ans))

Submission status: Time Limit Exceeded

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

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

ans = 0    
for i in C.keys():
    ans += (C[i] * (C[i]-1)) // 2
    
for i in A:
    dec = 0
    D = C.copy()
    D[i] += -1
    dec = (C[i] * (C[i]-1) // 2) - (D[i] * (D[i]-1) // 2)
    print(ans-dec)

Submission status: Time Limit Exceeded

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

s = 0

while not(a2 == [0]*n):
    e = a2.count(max(a2))
    s += e*(e-1)/2
    a2 = [0 if i == max(a2) else i for i in a2]

for k in range(n):
    f = a.count(a[k])
    print(int(s - f + 1))

Submission status: Time Limit Exceeded

Solution code:
import queue

d={}
q=queue.Queue()
n=int(input())
nums=list(map(int,input().split()))
for a in nums:    
    if not a in d:
        d[a]=1
    else:
        d[a]+=1
    q.put(a)

while not q.empty():
    answer=0
    p=q.get()
    for num,count in d.items():
        if p==num:
            answer+=int((count-1)*(count-2)/2)
        else:
            answer+=int(count*(count-1)/2)
    print(answer)

Submission status: Time Limit Exceeded

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

#%%
C = {}
for i in A:
    if i in C.keys():
        C[i] += 1
    else:
        C[i] = 1

for i in A:
    ans = 0
    D = C.copy()
    D[i] += -1
    for j in D:
        ans += (D[j] * (D[j]-1)) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter # 重複個数のカウント: Counter(num_list)

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    ans = 0

    cnt_list = Counter(num_list)

    for i in range(n):
        for k, v in cnt_list.items():
            if k == num_list[i]:
                v -= 1
            if v == 1:
                continue
            ans += v * (v-1) // 2
        print(ans)
        ans = 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter # 重複個数のカウント: Counter(num_list)
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    cp_num_list = num_list[:]
    ans = 0

    for i in range(n):
        cp_num_list.pop(i)
        cnt_list = Counter(cp_num_list)
        for v in cnt_list.values():
            if v == 1:
                continue
            ans += comb(v, 2)
        print(ans)
        cp_num_list = num_list[:]
        ans = 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
A = list(map(int, input().split()))
IND = list(set(A))
# CNT = [A.count(x) for x in IND]
# print(IND)
# print(CNT)
CONB = [A.count(x)*(A.count(x)-1)//2 for x in IND]
# print(CONB)
SUM = sum(CONB)
for i in A:
    ans = SUM - A.count(i) + 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 0
ans = 0
dic = {}
for i in set(a):
    b = a.count(i)
    dic.setdefault(i, b)
    if b > 1:
        ans += b*(b-1)//2
for i in a:
    print(ans - (dic[i] - 1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 0
ans = 0
dic = {}
for i in set(a):
    b = a.count(i)
    dic.setdefault(i, a.count(i))
    if b > 1:
        ans += b*(b-1)//2
for i in a:
    print(ans - (dic[i] - 1))

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

counter = {}
for val in a:
  if val in counter:
    counter[val] += 1
  else:
    counter[val] = 1

table = {}
for k, v in counter.items():
  if v == 1 :
    table[k] = [0, 0]
  elif v == 2:
    table[k] = [comb(v,2), 0]
  else: 
    table[k] = [comb(v,2), comb(v-1, 2)]

memo = {}
for num in a:
  result = 0
  if num in memo :
      print(memo[num])
      continue
  for k, v in table.items():
    if k == num:
      result += v[1]
    else :
      result += v[0]
  memo[num] = result
  print(result)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

num=[0]*(N+1) #それぞれの数の個数

cnb=[-1]*(N+1) #それぞれの球を抜いた時のコンビネーション

for i in range(len(ball)):
  num[ball[i]]+=1

for i in range(len(num)):
  if num[i]>=1:
    break
  if i==len(num)+1:
    for j in range(ball):
      print("0")

for j in ball:
  
  if cnb[j]==-1:
    cnb[j]=0
    num[j]-=1

    for k in range(1,len(num)):
      n=num[k]
      if n>=2:
        cnb[j]+=int(n*(n-1)/2)

    print(cnb[j])
    num[j]+=1
  else:
    print(cnb[j])

Submission status: Time Limit Exceeded

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

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

counter = {}
for val in a:
  if val in counter:
    counter[val] += 1
  else:
    counter[val] = 1

table = {}
for k, v in counter.items():
  if v == 1 :
    table[k] = [0, 0]
  elif v == 2:
    table[k] = [comb(v,2), 0]
  else: 
    table[k] = [comb(v,2), comb(v-1, 2)]
    
for num in a:
  result = 0
  for k, v in table.items():
    if k == num:
      result += v[1]
    else :
      result += v[0]
  print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
for i in set(a):
    dic.setdefault(i, a.count(i))
ans = 0
for i in dic.values():
    if i > 1:
        ans += i*(i-1)//2
for i in a:
    print(ans - (dic[i] - 1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

A_count=[A.count(A_s[x]) for x in range(len(A_s))]
# for x in range(len(A_s)):
#   i=A.count(A_s[x])
#   A_count.append(i)



for i in A_count:
  if 2<=i:
    cnt+=(i*(i-1))//2



# [print(cnt) if A_count[A_s.index(A[i])]==1 else print(cnt-A_count[A_s.index(A[i])]+1) for i in range(N)]

for i in A:
  x=A_count[A_s.index(i)]
  if x==1:
    print(cnt)
  else:
    print(cnt-x+1)

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

counter = {}
for val in a:
  if val in counter:
    counter[val] += 1
  else:
    counter[val] = 1

for num in a:
  result = 0
  for k, v in counter.items():
    target = v -1 if num == k else v
    if target > 1:
      result += comb(target, 2)
  print(result)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

num=[0]*(N+1) #それぞれの数の個数

cnb=[-1]*(N+1) #それぞれの球を抜いた時のコンビネーション

for i in range(len(ball)):
  num[ball[i]]+=1


for j in ball:
  
  if cnb[j]==-1:
    cnb[j]=0
    num[j]-=1

    for k in range(1,len(num)):
      n=num[k]
      if n>=2:
        cnb[j]+=int(n*(n-1)/2)

    print(cnb[j])
    num[j]+=1
  else:
    print(cnb[j])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

num=[0]*(N+1) #それぞれの数の個数

cnb=[0]*(N+1) #それぞれの球を抜いた時のコンビネーション

for i in range(len(ball)):
  num[ball[i]]+=1


for j in ball:
  
  if cnb[j]==0:
    num[j]-=1

    for k in range(1,len(num)):
      n=num[k]
      if n>=2:
        cnb[j]+=int(n*(n-1)/2)

    print(cnb[j])
    num[j]+=1
  else:
    print(cnb[j])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

A_count=[A.count(A_s[x]) for x in range(len(A_s))]
# for x in range(len(A_s)):
#   i=A.count(A_s[x])
#   A_count.append(i)



for i in A_count:
  if 2<=i:
    cnt+=(i*(i-1))//2



[print(cnt) if A_count[A_s.index(A[i])]==1 else print(cnt-A_count[A_s.index(A[i])]+1) for i in range(N)]

# for i in range(N):
#   x=A_count[A_s.index(A[i])]
#   if x==1:
#     print(cnt)
#   else:
#     print(cnt-x+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
for i in range(len(a)):
    dic.setdefault(a[i], a.count(a[i]))
ans = 0
for i in dic.values():
    if i > 1:
        ans += i*(i-1)//2
for i in a:
    print(ans - (dic[i] - 1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

A_count=[A.count(A_s[x]) for x in range(len(A_s))]
# for x in range(len(A_s)):
#   i=A.count(A_s[x])
#   A_count.append(i)



for i in A_count:
  if 2<=i:
    cnt+=(i*(i-1))//2



# [print(cnt) if A_count[A_s.index(A[i])]==1 else print(cnt-A_count[A_s.index(A[i])]+1) for i in range(N)]

for i in range(N):
  x=A_count[A_s.index(A[i])]
  if x==1:
    print(cnt)
  else:
    print(cnt-x+1)

Submission status: Time Limit Exceeded

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

num_cnt = {}
res = 0
for num in range(n):
    cnt = a.count(num+1)
    if cnt > 0:
        num_cnt[num+1] = cnt
        res += cnt*(cnt-1)//2

for a_k in a:
    cnt = num_cnt[a_k]
    print(res - (cnt-1)*(cnt - (cnt-2))//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
d={}
ua=list(set(a))
total=0
for x in ua:
    y=a.count(x)
    d[x]=y
    total+=(y-1)*y//2
for x in a:
    print(total-d[x]+1)

Submission status: Time Limit Exceeded

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

res = 0
for num in range(n):
    if a.count(num+1) > 0:
        res += a.count(num+1)*(a.count(num+1)-1)//2

for a_k in a:
    cnt = a.count(a_k)
    print(res - (cnt-1)*(cnt - (cnt-2))//2)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

for x in range(len(A_s)):
  i=A.count(A_s[x])
  A_count.append(i)

  if 2<=i:
    cnt+=(i*(i-1))//2



[print(cnt) if A_count[A_s.index(A[i])]==1 else print(cnt-A_count[A_s.index(A[i])]+1) for i in range(N)]

# for i in range(N):
#   x=A_count[A_s.index(A[i])]
#   if x==1:
#     print(cnt)
#   else:
#     print(cnt-x+1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

num=[0]*(N+1)

for i in range(len(ball)):
  num[ball[i]]+=1

for j in ball:
  key=j
  num[key]-=1
  
  cnb=0
  for k in num:
    if k>=2:
      cnb+=int(k*(k-1)/2)

  print(cnb)
  num[key]+=1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

for x in range(len(A_s)):
  i=A.count(A_s[x])
  A_count.append(i)

  if 2<=i:
    cnt+=(i*(i-1))//2


for i in range(N):
  x=A_count[A_s.index(A[i])]
  if x==1:
    print(cnt)
  else:
    print(cnt-x+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

for x in range(len(A_s)):
  i=A.count(A_s[x])
  A_count.append(i)

  if 2<=i:
    cnt+=(i*(i-1))//2


for i in range(N):
  if A_count[A_s.index(A[i])]==1:
    print(cnt)
  else:
    print(cnt-A_count[A_s.index(A[i])]+1)

Submission status: Time Limit Exceeded

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

def group_by_val(list_):
    res = dict()
    for v in list_:
        if v not in res:
            res[v] = [v]
            continue
        res[v].append(v)
    return res

def comb2(list_):
    leng = len(list_)
    if leng == 1:
        return 0
    return (leng*(leng-1)) // 2

grouped_dict = group_by_val(A)

comb_val = 0
comb_dict = dict()
for k in grouped_dict:
    temp = comb2(grouped_dict[k])
    comb_dict[k] = temp
    comb_val += temp

for a in A:
    alist = grouped_dict[a].copy()
    alist.pop()
    ans = comb_val - comb_dict[a] + comb2(alist)
    print(ans)





# ans1 = dict()
# for k in grouped:
#     ans1[k] = comb2(grouped[k])

# ans2 = dict()
# for k in grouped:
#     grouped_ = grouped[k][1:]
#     ans2[k] = comb2(grouped_)

# for a in A:
#     x = 0
#     for k in ans1:
#         if k == a:
#             continue
#         x += ans1[k]
#     print(x + ans2[a])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def fact(n):
    fact = 1
    for i in range(1,n+1):
        fact *= i
    return fact

def combinations_count(n,r):
    return fact(n) // (fact(n - r) * fact(r))

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = Counter(A)
    comb = 0
    for i in c.values():
        if i >= 2:
            comb += combinations_count(i, 2)
    for i in range(N):
        print(comb - (c[A[i]] - 1))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

num_cnt = {}
for a_i in a:
    if a_i in num_cnt:
        num_cnt[a_i] += 1
    else:
        num_cnt[a_i] = 1
for a_i in a:
    res = 0;
    for k,v in num_cnt.items():
        x = v-1 if k == a_i else v
        res += x*(x-1)//2
    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=[int(x) for x in input().rstrip().split()]
A_s=list(set(A))
A_count=[]
cnt=0

for i in range(len(A_s)):
  A_count.append(A.count(A_s[i]))


for i in A_count:
  if 2<=i:
    cnt+=(i*(i-1))//2




for i in range(N):
  if A_count[A_s.index(A[i])]==1:
    print(cnt)
  else:
    print(cnt-A_count[A_s.index(A[i])]+1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math 
def C(n,r):
    if n-r < 0:
        return 0 
    return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

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

c = 0
cc = {}
for key, val in d.items():
    temp = C(val,2)
    c += temp
    if cc.get(key) is None:
        cc[key] = temp - C(val-1,2)
for i in a:
    print(c - cc[i])

Submission status: Time Limit Exceeded

Solution code:
def C(n):
    if n < 2: return 0
    return n * (n-1) // 2    
    
n = int(input())
a = list(map(int, input().split()))
E = []

for i in range(n):
    if E.count(a[i]) == 0:
        E.append(a[i])

m = len(E)
count = 0

for i in range(m):
    num = a.count(E[i])
    count += C(num)

for i in range(n):
    l = a.count(a[i])
    print(count - C(l) + C(l-1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from collections import defaultdict

def fact(n):
    fact = 1
    for i in range(1,n+1):
        fact *= i
    return fact

def combinations_count(n,r):
    return fact(n) // (fact(n - r) * fact(r))

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = Counter(A)
    d = defaultdict(lambda:-1)
    for i in range(N):
        ans = 0
        if d[A[i]] != -1:
            ans += d[A[i]]
        else:
            c[A[i]] -= 1
            v = list(c.values())
            for j in v:
                if j >= 2:
                    ans += combinations_count(j, 2)
            d[A[i]] = ans
            c[A[i]] += 1
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def C(n):
    if n < 2: return 0
    return n * (n-1) // 2    
    


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

for i in range(n):
    if E.count(a[i]) == 0:
        E.append(a[i])

m = len(E)

num = [0] * m
comb = [0] * m
count = 0

for i in range(m):
    num[i] = a.count(E[i])
    comb[i] = C(num[i])
    count += comb[i]

for i in range(n):
    l = E.index(a[i])
    print(count - comb[l] + C(num[l]-1))

Submission status: Time Limit Exceeded

Solution code:
def C(n):
    if n < 2: return 0
    return n * (n-1) // 2    
    


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

for i in range(n):
    if E.count(a[i]) == 0:
        E.append(a[i])

m = len(E)

num = [0] * m
comb = [0] * m

for i in range(m):
    num[i] = a.count(E[i])
    comb[i] = C(num[i])

count = sum(comb)

for i in range(n):
    l = E.index(a[i])
    print(count - comb[l] + C(num[l]-1))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ua=list(set(a))
d={}
total=0
for x in ua:
    c=a.count(x)
    d[x]=c
    total+=sum(range(c))
for x in a:
    print(total-d[x]+1)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

def C(n,r):
    if n < r: return 0    
    r = min(n-r,r)
    if r == 0: return 1
    nume = reduce(mul, range(n, n - r, -1))
    deno = reduce(mul, range(1,r + 1))
    return nume // deno


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

for i in range(n):
    if E.count(a[i]) == 0:
        E.append(a[i])

m = len(E)

num = [0] * m
comb = [0] * m

for i in range(m):
    num[i] = a.count(E[i])
    comb[i] = C(num[i], 2)

count = sum(comb)

for i in range(n):
    l = E.index(a[i])
    print(count - comb[l] + C(num[l]-1, 2))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ua=list(set(a))
total=sum([sum(range(a.count(x))) for x in ua])
d={}
total=0
for x in ua:
    c=a.count(x)
    d[x]=c
    total+=sum(range(c))
for x in a:
    print(total-d[x]+1)

Submission status: Time Limit Exceeded

Solution code:
import sys
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
N = II()
A = list(MI())
ans = 0
cum = {}
def couple_combination(n):
    if n >= 2:
        return int(n * (n - 1) / 2)
    else:
        return 0
for unique in list(set(A)):
    cum_count = A.count(unique)
    cum[unique] = cum_count
    ans += couple_combination(cum_count)
for i in A:
    print(max(0,ans - cum[i] + 1))

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

for i in range(n):
    if E.count(a[i]) == 0:
        E.append(a[i])

num = [0] * len(E)
comb = [0] * len(E)

for i in range(len(E)):
    num[i] = a.count(E[i])
    comb[i] = C(num[i], 2)

count = sum(comb)

for i in range(n):
    l = E.index(a[i])
    print(count - comb[l] + C(num[l]-1, 2))

Submission status: Time Limit Exceeded

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

n=int(input())
a=list(map(int, input().split()))
b=set(a)
c=list(b)
d=len(b)
ans=0
def f(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
for i in range(d):
    e=a.count(c[i])
    if e==1:
        g=0
    elif e>=2:
        g=f(e,2)
    ans+=g
for i in range(n):
    h=a.count(a[i])
    if h==1:
        j=0
    elif h==2:
        j=1
    elif h>=3:
        j=f(h,2)-f(h-1,2)
    print(ans-j)
# print()

Submission status: Time Limit Exceeded

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

n=int(input())
a=list(map(int, input().split()))
b=set(a)
c=list(b)
d=len(b)
ans=0
def f(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(d):
    e=a.count(c[i])
    if e==1:
        g=0
    elif e>=2:
        g=f(e,2)
    ans+=g
for i in range(n):
    h=a.count(a[i])
    if h==1:
        j=0
    elif h==2:
        j=1
    elif h>=3:
        j=f(h,2)-f(h-1,2)
    print(ans-j)
# print()

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
a=list(map(int, input().split()))
b=set(a)
c=list(b)
d=len(b)
ans=0
def f(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i in range(d):
    e=a.count(c[i])
    if e==1:
        g=0
    elif e>=2:
        g=f(e,2)
    ans+=g
for i in range(n):
    h=a.count(a[i])
    if h==1:
        j=0
    elif h==2:
        j=1
    elif h>=3:
        j=f(h,2)-f(h-1,2)
    print(ans-j)
# print()

Submission status: Time Limit Exceeded

Solution code:
import sys
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
N = II()
A = list(MI())
cum = {}
for i in A:
    if i in cum:
        cum[i] += 1
    else:
        cum[i] = 1
for i in A:
    ans = 0
    for k, v in cum.items():
        if k == i:
            v -= 1
        if v >= 2:
            ans += int(v*(v-1)/2)
    print(ans)

Submission status: Time Limit Exceeded

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

def grouping_values(list_):
    res = dict()
    for v in list_:
        if v not in res:
            res[v] = [v]
            continue
        res[v].append(v)
    return res

def comb2(list_):
    leng = len(list_)
    if leng == 1:
        return 0
    return (leng*(leng-1)) // 2

grouped = grouping_values(A)

ans1 = dict()
for k in grouped:
    ans1[k] = comb2(grouped[k])

ans2 = dict()
for k in grouped:
    grouped_ = grouped[k][1:]
    ans2[k] = comb2(grouped_)

for a in A:
    x = 0
    for k in ans1:
        if k == a:
            continue
        x += ans1[k]
    print(x + ans2[a])

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
nlist = list(map(int,input().split()))
 
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
 
countlist = [0]*(len(nlist) + 1)
for i in range(0,len(nlist) + 1):
    countlist[i] = nlist.count(i)
 
count = 0
for i in range(0,len(nlist)):
    if countlist[i] >= 2:
        count+=combinations_count(countlist[i],2)
 
anslist = []
 
for i in range(0,len(nlist)):
    anslist.append((count-countlist[nlist[i]])+1)
 
for i in anslist:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
IND = list(set(A))
# CNT = [A.count(x) for x in IND]
# print(IND)
# print(CNT)
CONB = [A.count(x)*(A.count(x)-1)//2 for x in IND]
# print(CONB)
SUM = sum(CONB)
CALC = [-1]*(N+1)
for i in A:
    if CALC[i] != -1:
        print(CALC[i])
    else:
        # n = CNT[IND.index(i)]
        # ans = SUM - CONB[IND.index(i)] + (n-1)*(n-2)//2
        ans = SUM - A.count(i) + 1
        CALC[i] = ans
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=sorted(list(set(a)))
di=dict()
di2=dict()
for i in b:
  di[i]=a.count(i)
for i in b: 
  t=0
  if di[i]==0:
    di2[i]=t
    continue
  for j in b:
    if i==j:
      t+=(di[j]-1)*(di[j]-2)//2
    else:
      t+=di[j]*(di[j]-1)//2
  di2[i]=t

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=sorted(list(set(a)))
di=dict()
di2=dict()
for i in b:
  di[i]=a.count(i)
for i in b: 
  t=0
  for j in b:
    if i==j:
      t+=(di[j]-1)*(di[j]-2)//2
    else:
      t+=di[j]*(di[j]-1)//2
  di2[i]=t

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
lis = list(map(int, input().split()))
lis_ = Counter(lis)
c_all = 0
b = list(lis_.keys())
for i in b:
  c_all += int(lis_[i]*(lis_[i]-1)/2)

for x in range(N):
  print(c_all-(lis.count(lis[x])-1))

Submission status: Time Limit Exceeded

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

c = Counter(nums)

for num in nums:
    c[num]-=1
    curr = 0
    for k,v in c.items():
        if v >=2:
            curr+= v*(v-1)//2
    print(curr)
    c[num]+=1

Submission status: Time Limit Exceeded

Solution code:
def cc(c):
    if c<2:return 0
    return int(c*(c-1)/2)

n=int(input())
a=[int(A) for A in input().split()]

cnt=0
for elem in set(a):
    cnt+=cc(a.count(elem))

for k in a:
    print(cnt-(a.count(k)-1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split(' '))))
num_sum = 0
remove_array = np.array([], int)
for i in range(N):
    c1 = np.count_nonzero(A == i + 1)
    c2 = np.count_nonzero(A == A[i])
    if c2 <= 1:
        remove_array = np.append(remove_array, 0)
    else:
        remove_array = np.append(remove_array, c2 - 1)
    num_sum += int(c1 * (c1 - 1) / 2)
print(*(num_sum - remove_array), sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import collections
N=int(input())
lis = list(map(int, input().split()))
lis_ = collections.Counter(lis)
c_all = 0
b = list(lis_.keys())
for i in b:
  c_all += int(lis.count(i)*(lis.count(i)-1)/2)

for x in range(N):
  print(c_all-(lis.count(lis[x])-1))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lis = list(map(int, input().split()))
c_all = 0
for i in range(1, N+1):
  c_all += int(lis.count(i)*(lis.count(i)-1)/2)

for x in range(N):
  print(c_all-(lis.count(lis[x])-1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split(' '))))
num_sum = 0
remove_array = np.array([], int)
for i in range(N):
    c1 = np.count_nonzero(A == i + 1)
    c2 = np.count_nonzero(A == A[i])
    if c2 <= 1:
        remove_array = np.append(remove_array, 0)
    else:
        remove_array = np.append(remove_array, c2 - 1)
    num_sum += int(c1 * (c1 - 1) / 2)
for i in range(N):
    print(num_sum - remove_array[i])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lis = list(map(int, input().split()))
c_all = 0
for i in range(1, N+1):
  c_all += int(lis.count(i)*(lis.count(i)-1)/2)

for x in range(N):
  c = lis.count(lis[x])-1
  print(c_all-c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lis = list(map(int, input().split()))
c_all = 0
for i in range(1, N+1):
  c_all += int(lis.count(i)*(lis.count(i)-1)/2)

for x in range(N):
  lis_ = [lis[i] for i in range(N) if i != x]
  c = lis_.count(lis[x])
  print(c_all-c)

Submission status: Time Limit Exceeded

Solution code:
def cc(c):
    if c<2: return 0
    return int(c*(c-1)/2)

n=int(input())
a=[int(A) for A in input().split()]

acnt=dict()
for elem in set(a):
    acnt[elem] = a.count(elem)

memo = [-1]*2000010
for ak in a:
    if memo[ak] != -1:
        print(memo[ak])
        continue

    cnt=0
    for k,kcnt in acnt.items():
        if ak==k:
            cnt+= cc(kcnt-1)
        else:
            cnt+=cc(kcnt)
    memo[ak]=cnt

    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lis = list(map(int, input().split()))
c_all = 0
for i in range(N-1):
  for j in range(i+1, N):
    if lis[i] == lis[j]:
      c_all += 1

for x in range(N):
  lis_ = [lis[i] for i in range(N) if i != x]
  c = lis_.count(lis[x])
  print(c_all-c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic1 = {}
dic2 = {}
for i in range(N):
  if A[i] in dic1:
    dic1[A[i]] += 1
  else:
    dic1[A[i]] = 1

for j in range(N):
  ans = 0
  for k, v in dic1.items():
    if A[j] == k:
      ans += ((v-1)*(v-2))//2
    else:
      ans += (v*(v-1))//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic1 = {}
dic2 = {}
for i in range(N):
  if A[i] in dic1:
    dic1[A[i]] += 1
    dic2[A[i]].append(i)
  else:
    dic1[A[i]] = 1
    dic2[A[i]] = [i]

for j in range(N):
  ans = 0
  for k, v in dic1.items():
    if j in dic2[k]:
      ans += ((v-1)*(v-2))//2
    else:
      ans += (v*(v-1))//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}
text = ""
count = 0
la = len(a)
for i in range(n):
    c = ao.count(i+1)
    ac += int(c*(c-1)/2)
    acount[i+1] = c

for i in range(n):
    text += "{0}\n".format(ac-acount[ao[i]]+1)
print(text[0:-1])

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.copy(A)
    #tmp.pop(i)

    tmp = A[:i] + A[i+1:]
    #print(tmp)

    res = 0

    for j in set(tmp):
        l = tmp.count(j)
        res += len(list(itertools.combinations([0]*l,2)))
    print(res)

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.copy(A)
    tmp.pop(i)

    res = 0

    for j in set(tmp):
        l = tmp.count(j)
        res += len(list(itertools.combinations([0]*l,2)))
    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
l = [int(i) for i in input().split()]

c = Counter(l)

for i in l:
    ans = sum([v * (v-1) // 2 for v in c.values()])
    print(ans - c[i] + 1)