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: Wrong Answer

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

mod = 10**9 + 7   # こいつは素数である必要
factorial = [1]
inverse = [1]
for i in range(1, n+1):
    factorial.append(factorial[-1] * i % mod)
    inverse.append(pow(factorial[-1], mod - 2, mod))
def comb(n, r, mod):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % mod

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

dic2 = {}
maxi = 0
for key, num in dic.items():
    temp = comb(num, 2, mod)
    dic2[key] = temp
    maxi += temp

for k in range(n):
    temp = maxi
    num = dic[str(a[k])]
    print(temp - dic2[str(a[k])] + comb(num-1, 2, mod))

Submission status: Wrong Answer

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

from collections import Counter

c = Counter(a)

from scipy.misc import comb

s = set(c.values())

dic = {}
for ss in s:
    if ss not in dic.keys():
        dic[ss] = comb(ss, 2)
for ss in s:
    ss -= 1
    if ss not in dic.keys():
        dic[ss] = comb(ss, 2)

base = 0
for k in c.keys():
    base += dic[c[k]]

for k in range(len(a)):
    print(int(base - dic[c[a[k]]] + dic[c[a[k]] - 1]))

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
ans = 0
for k, v in counter.items():
    if 2 <= v:
        ans += v * (v - 1) // 2
for a in A:
    if a in counter and 2 <= counter[a]:
        v = counter[a]
        diff = v * (v - 1) // 2 - ((v- 1) * (v - 2) // 2)
        print(ans - diff)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7




n = I()

fac = [1] * (n + 1)
inv = [1] * (n + 1)
for j in range(1, n + 1):
    fac[j] = fac[j-1] * j % mod


inv[n] = pow(fac[n], mod-2, mod)
for j in range(n-1, -1, -1):
    inv[j] = inv[j+1] * (j+1) % mod


def comb(n, r):
    if r > n or n < 0 or r < 0:
        return 0
    return fac[n] * inv[n - r] * inv[r] % mod

L = LI()
cnt = Counter(L)
ret = 0
for i in cnt.values():
    if i >= 2:
        ret += comb(i, 2)
for l in L:
    if cnt[l] < 2:
        print(ret)
    else:
        print(ret - comb(cnt[l], 2) + comb(cnt[l] - 1, 2))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
    
fac = [-1] * (10**7+1)
inv = [-1] * (10**7+1)
finv = [-1] * (10**7+1)

fac[0] = fac[1] = 1
inv[1] = 1
finv[0] = finv[1] = 1

def initNCMMod(limit):
    for i in range(2, limit):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod // i) % mod
        finv[i] = finv[i-1] * inv[i] % mod

def NCMMod(n, k):
    if n < k:
        return 0
    if (n < 0 or k < 0):
        return 0
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod
initNCMMod(10**6)

def main():
    N = I()
    A = LI()
    vals = [0 for _ in range(N+1)]
    pick = [0 for _ in range(N+1)]
    for a in A:
        vals[a] += 1

    for i, val in enumerate(vals):
        tot = val
        if val >= 2:
            pick[i] = NCMMod(val, 2)
    picksum = sum(pick)

    for a in A:
        tmp_picksum = picksum - NCMMod(vals[a], 2)
        if vals[a] - 1 >= 2:
            tmp_picksum += NCMMod(vals[a]-1, 2)
        print(tmp_picksum)


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = 2 * pow(10, 5) + 1000  # N は必要分だけ用意する
mod = pow(10, 9) + 7
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

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

Submission status: Wrong Answer

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

N = int(input())
A = [int(x) for x in input().split()]

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

print(d)

for i in A :
    d[i] -= 1
    ans = 0
    for a in d :
        ans += d.get(a)*(d.get(a)-1)//2
    print(ans)
    d[i] += 1

Submission status: Wrong Answer

Solution code:

import sys
def II(): return int(sys.stdin.readline())

def LI(): return list(map(int, sys.stdin.readline().split()))

N = II()
A = LI()

def comb(k):
	if k == 1:
		return 1
	else:
		return int(k*(k-1)/2)

result = 0
kind = list(set(A))
for k in kind:
	result += comb(A.count(k))

for a in A:
	n = A.count(a)
	print(result-n+1)

Submission status: Wrong Answer

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

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

ans = 0
for a in d:
    n = d[a]
    ans += n * (n-1) // 2

print(ans)
for a in A:
    n = d[a]
    if n >= 2:
        print(ans - n + 1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))
B = [0] * (n+1)
for i in range(n):
    B[A[i]] += 1

C = [0] * (n+1)
for i in range(n):
    C[i] = B[i]*(B[i]-1)//2

su = sum(C)

for i in range(n):
    print(su - C[A[i]] + (B[A[i]]-1)*(B[A[i]]-2)//2)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = [0] * 200005
for a in A:
    count[a] += 1

s = 0
for c in count:
    s += c * (c - 1) // 2
print(s)

for a in A:
    c = count[a]
    x = c * (c - 1) // 2
    c -= 1
    y = c * (c - 1) // 2

    print(s - x + y)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [0]*(N+1)
for a in A:
    B[a]+=1
C = [0]*(N+1)
for i in range(N):
    C[i] = (B[i]-1)*B[i]//2
S = sum(C)
for k in range(N):
    s = A[k]
    ans = S-C[s]
    print(ans+(B[s]-2)*(B[s]-1)//2)

Submission status: Wrong Answer

Solution code:
N = int(input())
d = {}
for i in range(1,N+1):
    d[i] = 0
A = list(map(int,input().split()))
for i in range(N):
    a = A[i]
    d[a] += 1
s = 0
for i in range(1,N+1):
    x = d[a]
    s += (x*(x-1))//2
for i in range(N):
    y = A[i]
    z = s + 1 - d[y]
    print(z)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from copy import deepcopy

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

for i in range(n):
    tmp_c = deepcopy(c)
    ans = 0
    tmp_c[a[i]] -= 1
    for v in tmp_c.values():
        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()))

D = collections.Counter(A)
for i in range(N):
  D[A[i]]-=1
  s=0
  for j in D:
    s+=D[j]*(D[j]-1)//2
  print(s)
  D[A[i]]+=1

Submission status: Time Limit Exceeded

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

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

for i in range(N):
  print(ans-(c[A[i]-1]-1)) if c[A[i]-1]>1 else print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(N):
  print(ans-c[A[i]-1]*(c[A[i]-1]-1)//2+(c[A[i]-1]-1)*(c[A[i]-1]-2)//2) if c[A[i]-1]>1 else print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

n=int(input())
a=list(map(int,input().split()))
c=Counter(a)
com=0
for i in range(1,n+1):
    if(c[i]<=1):
        continue
    com+=combinations_count(c[i],2)
for k in range(1,n+1):
    ans=com
    if(c[a[k-1]]>=2):
        ans-=combinations_count(c[a[k-1]],2)
        if((c[a[k-1]]-1)>=2):
            ans+=combinations_count(c[a[k-1]]-1,2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
num_list = list(map(int, input().split()))
cnt_list = [0] * n

for k in range(n):
    cnt_list[k] = num_list.count(num_list[k]) - 1

for k in range(n):
    ans = sum(cnt_list)//2 - cnt_list[k]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [a.count(x)-1 for x in a]
c = sum(b)//2

for y in b:
	print(c-y)

Submission status: Time Limit Exceeded

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

b = [a.count(x)-1 for x in a]
c = sum(b)//2

for i in range(n):
  print(c-b[i])

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.special import comb
n = int(input())
num_list = list(map(int, input().split()))
all = 0

c = collections.Counter(num_list)
value_list = list(c.values())

for i in range(len(value_list)):
    if not value_list[i] == 1:
        all += comb(value_list[i],2,exact=True)

for k in range(n):
    if not num_list.count(num_list[k]) == 1:
        ans = all + comb(num_list.count(num_list[k])-1,2,exact=True) - comb(num_list.count(num_list[k]),2,exact=True)
        print(ans)
    else:
        print(all)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)

culc = {}
# print(K)
# print(C)

for n in K:
    tmp = 0
    # print("n=",n)
    for i in C:
        if C[i] != 1:
            if i == n and C[i]-1 == 1:
                continue
            elif i == n :
                tmp += (C[i]-1)*(C[i]-2)//2
            else:
                tmp += C[i]*(C[i]-1)//2
    #     print("C=",i,"count",C[i],tmp)
    # print("K=",n,"の時、",tmp)
    culc[n]=tmp

# print(culc)

for i in range(N):
    print(culc[A[i]])

Submission status: Time Limit Exceeded

Solution code:
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())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)

culc = {}
# print(K)
# print(C)

for n in K:
    tmp = 0
    # print("n=",n)
    for i in C:
        if C[i] != 1:
            if i == n and C[i]-1 == 1:
                continue
            elif i == n :
                tmp += cmb(C[i]-1,2)
            else:
                tmp += cmb(C[i],2)
        # print("C=",i,"count",C[i],tmp)
    # print("K=",n,"の時、",tmp)
    culc[n]=tmp

# print(culc)

for i in range(N):
    print(culc[A[i]])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = [ int(i) for i in input().split() ]

visited = {}

for d in lst:
  if d in visited.keys():
    visited[d] += 1
  else:
    visited[d] = 1
    
searched = {}
for i in range(n):
  if i in searched.keys():
    print(searched[i])
    continue
  visited[lst[i]] -= 1
  ans = 0
  for d in visited.keys():
    v = visited[d]
    ans += v*(v-1)/2
  print(int(ans))
  searched[i] = ans
  visited[lst[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = [ int(i) for i in input().split() ]

visited = {}

for d in lst:
  if d in visited.keys():
    visited[d] += 1
  else:
    visited[d] = 1
for i in range(n):
  visited[lst[i]] -= 1
  ans = 0
  for d in visited.keys():
    v = visited[d]
    ans += v*(v-1)/2
  print(int(ans))
  visited[lst[i]] += 1

Submission status: Time Limit Exceeded

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

#全部のパターン数計算→その数のときのパターン数消す
S = set(A)
S_cnt = {}
ma = 0
for i in S:
    #S_cnt[i] = A.count(i)
    cnt = A.count(i)
    S_cnt[i] = cnt
    ma += cnt*(cnt-1)//2

for i in A:
    cnt = S_cnt[i]
    print(ma-cnt+1)

Submission status: Time Limit Exceeded

Solution code:
#python3のmapはlistでなくイテレータを返す。listがほしいときはlistで囲む
#numpyを使わなければpypyでもOK
import sys
import copy
#import math
#import itertools
#import numpy as np
#import re

N = int(input())
A = [int(c) for c in input().split()]

#全部のパターン数計算→その数のときのパターン数消す
S = set(A)
S_cnt = {}
ma = 0
for i in S:
    #S_cnt[i] = A.count(i)
    cnt = A.count(i)
    S_cnt[i] = cnt
    ma += cnt*(cnt-1)//2

for i in A:
    cnt = S_cnt[i]
    print(ma-cnt+1)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    a = tuple(map(int,input().split()))
    cnt_0 = 0
    for i in set(a):
        b = a.count(i)
        cnt_0 += int(b*(b-1)/2)
    for i in a:
        b = a.count(i)
        cnt_1 = b-1
        print(cnt_0-cnt_1)

resolve()

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

cmb = 0
for x in c.values():
    if x >= 2:
        cmb += math.factorial(x) // (math.factorial(2) * math.factorial(x - 2))  

for z in a:
    y = c.get(z)
    ans = cmb
    if y == 2:
        ans -= 1
    elif y >= 3:
        ans -= (math.factorial(y) // (math.factorial(2) * math.factorial(y - 2))) - (math.factorial(y - 1) // (math.factorial(2) * math.factorial(y - 3)))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.special import comb

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

cc = collections.Counter(a)
b = [c[1] for c in cc.items()]
x = len(b)
t = 0
for i in range(x):
  t += comb(b[i], 2)

for i in range(n):
  y = a.count(a[i])
  if y >=2:
    ans = t - comb(y,2) + comb(y-1,2)
  else:
    ans = t
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.special import comb

n = int(input())
aa = list(map(int, input().split()))
xx = [0]*n
for i in range(n):
  a = 0
  if i == 0:
    bb = aa[1:]
  elif i == n-1:
    bb = aa[:-1]
  else:
    bb = aa[:i] + aa[i+1:]
  cc = collections.Counter(bb)
  d = [c[1] for c in cc.items()]
  for j in range(len(d)):
    a += comb(d[j], 2, exact=True)
  print(a)

Submission status: Time Limit Exceeded

Solution code:
import collections
from scipy.special import comb

n = int(input())
aa = list(map(int, input().split()))
xx = [0]*n
for i in range(n):
  a = 0
  if i == 0:
    bb = aa[1:]
  elif i == n-1:
    bb = aa[:-1]
  else:
    bb = aa[:i] + aa[i+1:]
  cc = collections.Counter(bb)
  d = [c[1] for c in cc.items()]
  for j in range(len(d)):
    a += comb(d[j], 2, exact=True)
  xx[i] = a
  
for i in range(n):
  print(xx[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Alst = list(map(int, input().split()))
Blst = [0]*(N+1)
num = 0
for i in range(1, N+1):
    tmp = Alst.count(i)
    Blst[i] = tmp
    num = num + tmp*(tmp-1)//2

for i in range(N):
    k = Blst[Alst[i]] -1
    print(num - k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=set(a)
data={i:a.count(i) for i in s}
for i in range(n):
  data[a[i]]-=1
  ans=0
  for j in data:
    ans+=data[j]*(data[j]-1)//2
  print(ans)
  data[a[i]]+=1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
data={}
for i in range(n):
  if a[i] not in data:
    data[a[i]]=1
  else:
    data[a[i]]+=1
for i in range(n):
  data[a[i]]-=1
  ans=0
  for j in data:
    if data[j]>=2:
      ans+=data[j]*(data[j]-1)//2
  print(ans)
  data[a[i]]+=1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math


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


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

k = 0
for v in c.values():
    if v == 1:
        continue
    else:
        k += combinations_count(v, 2)

for a in al:
    if c[a] == 1:
        print(k)
    elif c[a] == 2:
        print(k-combinations_count(c[a], 2))
    else:
        print(k-combinations_count(c[a], 2)+combinations_count(c[a]-1,2))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

def combi(nam,r):
  return math.factorial(nam)//(math.factorial(nam-r)*math.factorial(r))
  
n=int(input())
a=list(map(int,input().split()))

for i in range(n):
  b=a[:i]+a[i+1:]
  c=collections.Counter(b)
  d=0
  for j in c.values():
    if j>1:
      d+=combi(j,2)
  print(d)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    d = defaultdict(lambda:0)
    d2 = {}
    for i in a:
        d[i] += 1
    for i,j in d.items():
        x = j*(j-1)//2
        d2[i] = x
    ans = 0
    for i,j in d.items():
        ans = sum(d2.values())
    for i in range(n):
        print(ans-d[a[i]]+1)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math


def combination(n, r):
    if n <= 1:
        return 0
    elif n == 2:
        return 1
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def count_list(lst):
    count_lst = [0] * (max(lst) + 1)

    for i in lst:
        count_lst[i] += 1

    return count_lst


def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    lst1 = count_list(a_lst)
    lst2 = [0] * len(lst1)
    for i in range(len(lst1)):
        lst2[i] = combination(lst1[i], 2)
    tmp = sum(lst2)

    for i in range(n):
        a = a_lst[i]
        tmp_tmp = tmp
        tmp -= lst2[a]
        tmp += combination(lst1[a] - 1, 2)
        print(tmp)
        tmp = tmp_tmp


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
from collections import Counter
from scipy.special import comb

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

for k in range(n):
  a = copy(A)
  a.pop(k)
  c = Counter(a)
  ans = 0
  for i in range(1,n+1):
    ans += comb(c[i],2,exact=True)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

def c(n, r):
    if n >= r:
        return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))
    return 0

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

d = Counter(A)

sum_c = 0
for v in d.values():
    if v >= 2:
        sum_c += c(v, 2)

for a in A:
    x = sum_c - c(d[a], 2) + c(d[a]-1, 2)
    print(x)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math, copy

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

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

d = Counter(A)

for a in A:
    x = copy.copy(d)
    x[a] -= 1
    ans = 0
    for v in x.values():
        if v >= 2:
            ans += c(v, 2)
    print(ans)

Submission status: Time Limit Exceeded

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

    a_dict = {}
    for a in a_list:
        if a not in a_dict:
            a_dict[a] = 1
        else:
            a_dict[a] += 1

    for k in range(n):
        cnt = 0
        a_dict[a_list[k]] -= 1
        for v in a_dict.values():
            if v >= 2:
                cnt += v * (v - 1) // 2
        a_dict[a_list[k]] += 1
        print(cnt)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

cl=Counter(l)

for i in range(n):
  if i==0:
    num=0
    cl[l[i]]-=1
    for j in cl.values():
      num+=(j*(j-1)/2)
  else:
    cl[l[i]]-=1
    cl[l[i-1]]+=1
    num=0
    for j in cl.values():
      num+=(j*(j-1)/2)
  print(int(num))

Submission status: Time Limit Exceeded

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

import collections
count = collections.Counter(a)

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

# print(count)
s = 0
for i in count.keys():
    s += c(count[i], 2)
# print(s)

for i in range(n):
    ans = s - c(count[a[i]], 2) + c(count[a[i]] - 1, 2)
    print(ans)

Submission status: Time Limit Exceeded

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

li = [0] * (N + 1)
for i in A:
  li[i] += 1

for i in A:
  li[i] -= 1
  count = 0
  for j in li[1:]:
    if j >= 2: count += j * (j - 1) // 2
  li[i] += 1
  print(count)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
n=int(input())
a=list(map(int,input().split()))
dict={}
s=set(a)
for i in s:
  dict[i]=a.count(i)
for i in a:
  dict[i]-=1
  ans=0
  for j in dict:
    if dict[j]>=2:
      ans+=factorial(dict[j])//(2*factorial(dict[j]-2))
  dict[i]+=1
  print(ans)

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 k,v in count.items():
    if k == i: v -= 1
    ans += v*(v-1)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ls = [0] * (N + 1)

C = 0
for i in set(A):
  n = A.count(i)
  ls[i] += n
  C += n * (n - 1) //2
  
for a in A:
  n = ls[a]
  print(C - (n - 1))

Submission status: Time Limit Exceeded

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

C = 0
for i in set(A):
  n = A.count(i)
  C += n * (n - 1) //2
  
for a in A:
  n = A.count(a)
  print(C - (n - 1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

N = int(input())
A = [int(i) for i in input().split()]
cnt_list = Counter(A)
ans_dict = {}

for i in range(N):        
    if not ans_dict.get(A[i]):
        ans = 0
        for num, cnt in cnt_list.items():
            if num == A[i]:
                cnt = cnt-1
            ans += factorial(cnt) // factorial(2) // factorial(cnt - 2) if cnt > 1 else 0
        ans_dict[A[i]] = ans
    print(ans_dict[A[i]])

Submission status: Time Limit Exceeded

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

def find(idx, K):
  res = 0
  idx[K] -= 1
  for key in idx:
    if idx[key] != 0:
      res += int(idx[key] * (idx[key] - 1) / 2)
  return res
  
for i in A:
  if i in idx:
    idx[i] += 1
  else:
    idx[i] = 1
    
for i in range(N):
  print(find(copy.copy(idx), A[i]))

Submission status: Time Limit Exceeded

Solution code:

def combi(num):
    return num*(num-1)//2

def cnt_combi(l):
    seta = set(l)
    ret=0
    while len(seta) > 0:
        i = seta.pop()
        cnt = l.count(i)
        ret += combi(cnt)
    return ret

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

for i in range(n):

    print(cnt_combi(a[:i]+a[i+1:]))

Submission status: Time Limit Exceeded

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


def check(L):
    s = 0

    a = 0
    c = 0
    #print(L)
    for i in range(len(L)):
        if a != L[i]:
            s += comb(c)
            a = L[i]
            c = 1 
        else:
            c += 1
    s += comb(c)

    return s

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

D = {}
for i in range(len(A)):
    D[i] = A[i]

D = sorted(D.items(), key=lambda x: x[1])
V = {}
for i in range(len(D)):
    V[D[i][0]] = i

AA = sorted(A)

for k in range(N):
    AAA = list(AA)
    del(AAA[V[k]])
    print(check(AAA))

Submission status: Time Limit Exceeded

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


def check(L):
    s = 0

    a = 0
    c = 0
    #print(L)
    for i in range(len(L)):
        if a != L[i]:
            s += comb(c)
            a = L[i]
            c = 1 
        else:
            c += 1
    s += comb(c)

    return s

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

for k in range(N):
    AA = list(A)
    del(AA[k])
    AA = sorted(AA)
    print(check(AA))

Submission status: Time Limit Exceeded

Solution code:
#D
from scipy.special import comb
N=int(input())
A=list(map(int,input().split()))

def nCr(i):
    if i<=1:
        return 0
    else:
        return i*(i-1)//2

for i in range(N):
    A_copy=A.copy()
    A_copy.pop(i)
    A_uniq=list(set(A_copy))
    comb_i=0
    for j in A_uniq:
        uniq_len=A_copy.count(j)
        comb_i+=nCr(uniq_len)
    print(comb_i)

Submission status: Time Limit Exceeded

Solution code:
#D
from scipy.special import comb
N=int(input())
A=list(map(int,input().split()))

for i in range(N):
    A_copy=A.copy()
    A_copy.pop(i)
    A_uniq=list(set(A_copy))
    comb_i=0
    for j in A_uniq:
        uniq_len=A_copy.count(j)
        comb_i+=comb(uniq_len,2,exact=True)
    print(comb_i)

Submission status: Time Limit Exceeded

Solution code:
#D
from itertools import combinations
N=int(input())
A=list(map(int,input().split()))

A_uniq=list(set(A))

for i in range(N):
    A_copy=A.copy()
    A_copy.pop(i)
    comb_i=0
    for j in A_uniq:
        uniq_len=A_copy.count(j)
        comb_i+=len(list(combinations(range(uniq_len),2)))
    print(comb_i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
cnt=0
b=[]
flag=0

for i in range(n):
    for j in range(n):
        if i!=j:
            b.append(a[j])
    for k in range(n-2):
        b.sort()
        if b[k]==b[k+1]:
            cnt+=1
            flag+=1
        else:
            ans+=((cnt+1)*(cnt)//2)
            flag=0
            cnt=0
    else:
        if flag!=0:
            ans+=((cnt+1)*(cnt)//2)
            print(ans)
            cnt=0
            ans=0
            b=[]
        else:
            print(ans)
            cnt=0
            ans=0
            b=[]

Submission status: Time Limit Exceeded

Solution code:
#21 D - Banned K
import collections
from collections import deque
N = int(input())
A = list(map(int,input().split()))
 
cnt = collections.Counter(A)

ans = deque([])
for i in A:
    cnt_cpy = cnt.copy()
    cnt_cpy[i] -= 1
    comb = 0
    for j in cnt_cpy.values():
        if j >= 2:
            comb += (j*(j-1))//2
    ans.append(comb)
for k in ans:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
#21 D - Banned K
import collections
N = int(input())
A = list(map(int,input().split()))

cnt = collections.Counter(A)

ans = []
for i in A:
    cnt_cpy = cnt.copy()
    cnt_cpy[i] -= 1
    comb = 0
    for j in cnt_cpy.values():
        if j >= 2:
            comb += (j*(j-1))//2
    ans.append(comb)
for k in ans:
    print(k)