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())
l = [int(x) for x in input().split()]
def motomeru(i):
  m = l.count(i)
  return int(m*(m-1)/2)
def herasu(i):
  m = l.count(i)-1
  return int(m*(m-1)/2)
zenbu = 0
for i in range(n):
  zenbu = zenbu + motomeru(i+1)
for i in range(n):
  a = zenbu - motomeru(l[i]) + herasu(l[i])
  print(a)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
@lru_cache(maxsize=1024)
def nC2(n):
    return n * (n-1)//2

N=int(input())
A=list(map(int, input().split()))
Y = set(A)
memo = {}
for k in range(0,N):
    key = A[k] 
    if A.count(key) == 1:
        print(0)
        continue
    if key in memo:
        print(memo[key])
        continue
    c = 0
    memo2 = {}
    for i in Y:
        sc = A[:k].count(i)
        sc += A[k+1:].count(i)
        if sc == 1:
            continue
        #カウント対象
        c += nC2(sc)
    memo[key] = c
    print(c)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections


combs = {}
results = {}


def get_combination(n):
    if n < 2:
        return 0
    if combs.get(n):
        return combs.get(n)
    else:
        combs[n] = factorial(n) // (factorial(n - 2) * factorial(2))
        return combs[n]


n = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
S = 0
for k, v in c.items():
    S += get_combination(v)
for a in A:
    if results.get(a):
        print(results[a])
    else:
        results[a] = S - get_combination(c[a]) + get_combination(c[a] - 1)
        print(results[a])

Submission status: Time Limit Exceeded

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

seen = [0]*n

for i in range(n):
  seen[a[i]-1] =  seen[a[i]-1] + 1

for i in range(n):
   if not ((seen[i] == 0) or (seen[i] == 1)):
      base.append((i+1)+seen[i]*n)

for i in range(n):
  count = 0
  for temp in base:
    now = temp//n
    if temp%n == a[i]:
      now = now - 1
      
    count = count + now*(now-1)//2
    
  print(int(count))

Submission status: Time Limit Exceeded

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    n=int(input())
    a=list(map(int,input().split()))
    from collections import Counter
    ss=Counter(a)
    s1,s2 = zip(*(ss.most_common()))
    s3 = [(i*(i-1))//2 for i in s2] 
    maxs=sum(s3)
    dp=[0]*len(s1)
    cnt=0
    for i in range(len(s1)):
        dp[i]=maxs-s3[i]+((s2[i]-1)*(s2[i]-2)//2)

    for i in a:
        print(dp[s1.index(i)])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

nums = []
count = {}
for a_i in a:
    if not a_i in nums:
        nums.append(a_i)
        count[a_i] = 1
    else:
        count[a_i] = count[a_i] + 1

count = {k:v for k, v in count.items() if v > 1}

ans = ""
for i, a_i in enumerate(a):
    choice = 0
    for k, v in count.items():
        tmp_v = v if not k == a_i else v -1
        if tmp_v > 1:
            choice += (tmp_v * (tmp_v - 1)/2)
    if i == (len(a) - 1):
        ans += str(int(choice))
    else:
        ans += str(int(choice)) + "\n"

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array([int(c) for c in input().split()], dtype=int)
c = np.zeros((n,), dtype=int)
for i in range(a.shape[0]):
    c[a[i]-1]+=1
count = (c * (c-1) / 2).sum()
for i in range(a.shape[0]):
    pp = c[a[i]-1]
    print(int(count - pp * (pp - 1) / 2 + (pp-1) * (pp-2) / 2))

Submission status: Time Limit Exceeded

Solution code:

N=int(input())
A=list(map(int,input().split()))
setA=set(A)
number1={}#-1
number2={}
ans={}
for i in setA:
    tmp=A.count(i) - 1
    souwa=(tmp - 1)*(tmp)//2
    number1[i]=souwa
    souwa=tmp*(tmp+1)//2
    number2[i]=souwa
t=sum(number2.values())
for a in setA:
    total=number1[a]
    total+=t
    total-=number2[a]
    ans[a]=total

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
li = list(map(int, input().split()))
for i in range(len(li)):
    goukei = 0
    x = li[i]
    del li[i]
#    print(li)
    for j in range(N):
        goukei += li.count(j) * (li.count(j)-1) / 2
    print(int(goukei))
    li.insert(i, x)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A_list = [int(_) for _ in input().split()]

ans = 0
temp = Counter(A_list)
for item in temp.most_common():
    v = item[1]
    if v < 2:
        break
    ans += v*(v-1)/2

for k in range(N):
    tgt = A_list[k]
    n = A_list.count(tgt)
    print(int(ans - n + 1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])

u = list(np.unique(A))
c = []
sum = 0
for j in u:
    _ = len(np.where(A == j)[0])
    c.append(_)
    sum += int(_ * (_ - 1) / 2)

for k in range(N):
    v = c[u.index(A[k])]
    print(sum - v + 1)

Submission status: Time Limit Exceeded

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

a1=len(list(set(a)))

total=0
for j in range(a1):
    x=c.most_common()[j][1]
    total+=x*(x-1)/2

for i in range(n):
    ans=total-c[a[i]]+1
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
n = int(input())
aa = list(map(int, input().split()))

def comb(n, k):
    p = (10 ** 9) + 7
    """power_funcを用いて(nCk) mod p を求める"""
    from math import factorial
    if n<0 or k<0 or n<k: return 0
    if n==0 or k==0: return 1
    a=factorial(n) %p
    b=factorial(k) %p
    c=factorial(n-k) %p
    return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
    """a^b mod p を求める"""
    if b==0: return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a * power_func(a, b - 1, p)) % p
    
memo = {}
a = collections.Counter(aa)
ans = 0
for i in range(len(aa)):
    if not aa[i] in memo:
        aaa = a[aa[i]]
        if aaa - 2 >= 0:
            sub = comb(aaa, 2)
            memo[aa[i]] = sub
        else:
            memo[aa[i]] = 0

memo2 = {}
for i in range(len(aa)):
    if not aa[i] in memo2:
        aaa = a[aa[i]] - 1
        if aaa - 2 >= 0:
            sub = comb(aaa, 2)
            memo2[aa[i]] = sub
        else:
            memo2[aa[i]] = 0

for i in range(len(aa)):
    ans = 0
    ans += memo2[aa[i]]
    for key in memo:
        if aa[i] != key:
            ans += memo[key]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

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

c = collections.Counter(a)

ans_list = {}

for i in c:
  sum = 0
  for j in c:
    if j == i:
      sum = sum + comb(c[j]-1)
    else:
      sum = sum + comb(c[j])
  ans_list[i] = sum

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

Submission status: Time Limit Exceeded

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

counters = {}
used_list = []
for i in numbers:
    if i in used_list:
        continue
    counters[i] = numbers.count(i)
    used_list.append(i)
for i in range(n):
    sonuc = 0
    for sayi in counters:
        count = counters[sayi]
        if numbers[i] == sayi:
            count -= 1
        sonuc += count*(count-1)/2
    print(int(sonuc))

Submission status: Time Limit Exceeded

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

mat = []

for i in range(n):
  mat.append([int(a[i] == a[j]) * int(i != j) for j in range(n)])

tmp = 0
for i in range(n):
  s = [sum(mat[j]) for j in range(len(mat)) if j != i]

rsum = [sum(mat[i]) for i in range(n)]
tsum = sum(rsum)

res = [int((tsum - rsum[i] * 2) / 2) for i in range(n)]
for i in range(n):
  print(res[i])

Submission status: Time Limit Exceeded

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

for i in range(max(a)):
  answer += a.count(i + 1) * (a.count(i + 1) -1) // 2

for k in range(n):
  if a.count(a[k]) > 1:
    answer1 = answer - (a.count(a[k]) - 1)
  print(answer1)

Submission status: Time Limit Exceeded

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

def combinations_count(n, r):
    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

m = int(input())
a = list(map(int,input().split()))[:m]

for i in a:
    res = 0
    t = a[:]
    t.remove(i)
    c = collections.Counter(t)
    for v in c.values():
        if v >= 2:
            res += combinations_count(v, 2)
    
    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter()
    count.update(A)
    sum_ = sum([i*(i-1)/2 for i in count.values()])

    for i in range(len(A)):
        remain = A[:i] + A[i+1:]
        took = A[i]
        took_count = count[took]
        sub = took_count - 1
        print(int(sum_ - sub))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
from math import factorial

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

N = int(input())

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


for i in range(N):
    if i == 0:
        c = collections.Counter(A[1:])
        tmp = 0
        for v in c.values():
            if v > 1:
                tmp += ncr(v,2)
    else:
        B = A[0:i] + A[i+1:]
        c = collections.Counter(B)
        tmp = 0
        for v in c.values():
            if v > 1:
                tmp += ncr(v,2)
    print(tmp)
    tmp = 0

Submission status: Time Limit Exceeded

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

C = collections.Counter(A)
#print(C)
calc = {}
for i,cnt in C.items():
    calc[i] = cnt*(cnt-1)//2
#print("calc:",calc)
for i in range(N):
    ans = 0
    for j,k in calc.items():
#        print(j,k,A[i])
        if j != A[i]:
            ans += k
#    print("ans",ans)
#    print("A[i]",A[i],"calc[A[i]]",calc[A[i]])
    if calc[A[i]] != 0:
        ans += (C[A[i]]-1)*(C[A[i]]-2)//2
#        print((calc[A[i]]-2)*(calc[A[i]]-1)//2)
#        ans += (calc[A[i]]-2)*(calc[A[i]]-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache


@lru_cache(maxsize=None)
def get_sum(k):
    filtered = map(lambda x: x[1]-1 if x[0]
                   == k else x[1], a_dict.items())
    filtered = map(lambda x: x*(x-1)//2, filtered)
    return sum(filtered)


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

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

    a_dict[a] += 1


for i in range(n):
    k = a_list[i]
    print(get_sum(k))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

sa = sorted(list(set(a)))
nl = [combinations_count(a.count(i), 2) for i in sa]
nlmin = [combinations_count(a.count(i)-1, 2) for i in sa]

for i in range(n):
    value = 0
    rapop = a.copy().pop(i)
    for j in range(len(sa)):
        if rapop == sa[j]:
            value += nlmin[j]
        else:
            value += nl[j]
    print(value)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = sorted(set(A))
ans = 0

for i in range(len(B)):
    m = A.count(B[i])
    if m > 1:
        ans += m*(m-1)//2
        
for i in range(n):
    m = A.count(A[i])
    print(ans + 1 - m)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import add

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

def reduce_tmp(tmplist):
  return reduce(add, range(tmplist))

i = 0
for k in range(n):
  atmp = a[0:k] + a[k+1:]
  i = 0
  i += sum([reduce_tmp(atmp.count(ai)) for ai in set(atmp)])
  #for ai in set(atmp):
  #  i += reduce_tmp(atmp.count(ai))
  print(i)

Submission status: Time Limit Exceeded

Solution code:
#!/user/bin/env python3
# -*- conding: utf-8 -*-

import collections as cl


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

    for i in range(N):
        li = A[:i] + A[i+1:]
        c = cl.Counter(li)
        count = 0
        for j in c.values():
            count += int(j * (j-1) / 2)
        print(count)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
N=int(input())
A=list(map(int,input().split()))
B=list(set(A))
lB=len(B)
for j in range(1,N+1):
    print(sum([ comb((A[:j-1]+A[j:]).count(B[i]),2,exact=True) for i in range(lB)]))

Submission status: Time Limit Exceeded

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

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

for k in range(n):
  a_k = a[:k] + a[k+1:]
  a_k.sort()
  count = a_k.count(1)
  way = (count*(count-1)//2) if count > 1 else 0
  elements = count
  while True:
    if elements >= n-1:
      break
    new_num = a_k[elements]
    count = a_k.count(new_num)
    way += (count*(count-1)//2) if count > 1 else 0
    elements += count
  print(way)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
A = list(map(int, input().strip().split()))
U = max(A) + 3
c = np.zeros((U),dtype=np.int64)
for a in A:
    c[a] += 1
#print(c)


q = np.zeros((c.max() + 3),dtype=np.int64)
q[2] = 1
#q[3] = 3
#q[4] = 6
for i in range(3,len(q)):
    q[i] = int(q[i-1] * i / (i-2))
#print(q)

s = 0
dex = np.where(c>0)
for i in dex[0]:
    s += q[c[i]]
#print(s)
for i in range(n):
    #print(A[i],q[c[A[i]]],q[c[A[i]]-1])
    print(s - q[c[A[i]]] + q[c[A[i]]-1])

Submission status: Time Limit Exceeded

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

for i in range(N):
    nums = {}
    B = A[:i] + A[i + 1:]
    for j in B:
        if not j in nums:
            nums[j] = 1
        else:
            nums[j] += 1
    count = 0
    for j in nums:
        count += nums[j] * (nums[j] - 1) // 2
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    total = 0
    x = a.copy()
    del x[i]
    x = collections.Counter(x)
    for v in x.values():
        total += v*(v-1)//2
    print (total)

Submission status: Time Limit Exceeded

Solution code:
def aC(a):
  if a == 0:
    return 0
  return a*(a-1)/2
 
N = input()
N = int(N)
A = list(map(int, input().split()))
d = {}
  
for i in range(0,N):
  pre_A = A[:]
  aaaaa = pre_A[i]
  if aaaaa in d:
    print(d[A[i]])
  else:
    pre_A.pop(i)
    ans = 0
    count = 0
    for i in range(1,N+1):
      aaa = pre_A.count(i)
      ans = ans + aC(aaa)
    print(int(ans))
    d[aaaaa] = int(ans)

Submission status: Time Limit Exceeded

Solution code:

def main():
  from collections import defaultdict
  
  n = int(input())
  a = list(map(int, input().split()))
  
  d = defaultdict(int)
  e = defaultdict(int)
  
  for x in a:
    d[x] += 1
  
  func = lambda x: x*(x-1)//2 if x>=2 else 0
  
  ppp = ''
  
  for x in a:
    get = e.get(x)
    if get == None:
      g = d[x] - 1
      h = [v for k,v in d.items() if (k != x and v>=2)]
      h.append(g)
      i = list(map(func, h))
      ans = sum(i)
      e[x] = ans
      ppp += str(ans) + '\n'
    else:
      ppp += str(get) + '\n'
    
   
  print(ppp)
      
 
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(num) for num in input().split(" ")]

total_comb = 0
ball_num_count = {}
for i in range(1,n+1):
    i_count = a_list.count(i)
    total_comb += int(i_count*(i_count-1)/2)
    ball_num_count[i] = int(i_count*(i_count-1)/2 - (i_count-1)*(i_count-2)/2)
for i in range(n):
    except_num = a_list[i]
    print(total_comb - ball_num_count[except_num])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

nums = list(map(int, input().split()))
temp = nums.copy()
for i in range(n):
    nums.pop(i)
    t = {}
    for j in nums:
        t[j] = nums.count(j)
    result = 0
    for v in t.values():
        if (v > 1):
            result += int(v*(v-1)/2)
    print(result)
    nums = temp.copy()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    ak = a[:k] + a[k + 1 :]
    akset = list(set(ak))
    ans = 0
    for i in range(len(akset)):
        tn = ak.count(akset[i]) - 1
        if tn == -1:
            continue
        ans += tn * (tn + 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def counter(lss):
    n = len(lss)
    cnt = 0
    anums = []
    for i in range(1,(n + 1)):
        anum = 0
        for j in range(n):
            if (i == lss[j]):
                anum += 1
        cnt += (anum * (anum - 1)) // 2
    return cnt

n = int(input())

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

for k in range(n):
    if (k == 0):
        lss = ls[1:n]
    elif (k == n-1):
        lss = ls[0:n-1]
    else:
        lss = ls[0:k] + ls[k + 1:n]
    print(counter(lss))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


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

def calc(a, i):
    _a = a[:i] + a[i+1:]
    set_a = set(_a)
    res = sum(map(lambda x: combinations_count(_a.count(x), 2), set_a))
    return res

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

for output in [calc(s, i) for i in range(len(s))]:
    print(output)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
li = list(map(int, input().split()))
for k in range(N):
    check_li, ans = li[:k]+li[k+1:], 0
    for i in list(set(check_li)):
        cnt = check_li.count(i)
        ans += cnt * (cnt-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    B = sorted(A[0:i] + A[i+1:N])
    s = 0
    for j in range(s,len(B)):
        for k in range(j+1,len(B)):
            if B[j] == B[k]:
                s += 1
            else:
                break
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import collections
import copy

@lru_cache(maxsize=2*10**5)
def f(p):
    c=copy.deepcopy(b)
    c[x]-=1
    d=collections.Counter(c.values())
    dk=d.keys()
    re=0
    for o in dk:
        if o>1:
            re+=o*(o-1)*d[o]
    return re//2


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

for x in a:
    print(f(x))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
alist = tuple(map(int,input().split()))
from operator import mul
from functools import reduce

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

adic = {}
ans = 0
for i in alist:
  if i not in adic:
    adic[i] = 1
  else:
    adic[i] += 1
for i in range(n):
  for j in adic:
    if j == alist[i]:
      if adic[j] >= 3:
        ans += cmb(adic[j]-1,2)
      else:
        ans += 0
    else:
      if adic[j] >= 2:
        ans += cmb(adic[j],2)
      else:
        ans += 0
  print(ans)
  ans = 0

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

def conbi(n, r = 2):
    if n - r < 0:
        return 0
    return int(factorial(n) / (factorial(n - r) * factorial(r)))

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

for a in A:
    ans = 0
    for cnt in A_cnt.keys():
        if A_cnt[cnt] == 1:
            continue
        else:
            if cnt == a:
                n = A_cnt[cnt] - 1
                ans += conbi(n)
            else:
                ans += conbi(A_cnt[cnt])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

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

COUNTS = collections.Counter(BALLS)


MEMO = {}
def fact(n):
    if MEMO.get(n):
        return MEMO[n]
    r = 2
    if n - r < 0:
        MEMO[n] = 0
        return 0
    MEMO[n] = factorial(n) // factorial(r) // factorial(n - r)
    return MEMO[n]

MEMO2 = {}
def solve(n):
    if MEMO2.get(n):
        return MEMO2[n]

    MEMO2[n] = sum(fact(v if k != n else v - 1) for (k, v) in COUNTS.items())
    return MEMO2[n]

for i in range(N):
    print(solve(BALLS[i]))

Submission status: Time Limit Exceeded

Solution code:
import math

past_ans = {}

def solve(k, arr):
    # iの計算結果を入れるdict
    all_ans = {}
    tmp_arr = []
    tmp_arr.extend(arr)
    deleted_num = tmp_arr.pop(k)

    # 過去に計算していた場合
    if deleted_num in past_ans:
        return past_ans[deleted_num]

    # 新しく計算する場合
    set_arr = []
    set_arr = list(set(tmp_arr))
    ans = 0
    for i in set_arr:
        # iを過去に計算していた場合, all_ansから取得する
        if i in all_ans:
            ans += all_ans[i]
        else:
            all_ans[i] = combinations_count(tmp_arr.count(i), 2)
            ans += all_ans[i]
    past_ans[deleted_num] = ans
    return ans

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

if __name__ == "__main__":
    n = int(input())
    arr = [int(s) for s in list(input().split())]
    [print(solve(k, arr)) for k in range(n)]

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
ans = {}

cal = {}
for a, b in C.items():
    cal[a] = b*(b-1)//2
for i in C.keys():
    ans[i] = sum(cal.values()) - (C[i] - 1)
for a in A:
    print(ans[a])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

a = [int(i) for i in input().split(' ')]

for i in range(N):
  b = [0] * N
  for j in range(N):
    if j == i:
      pass
    else:
      b[a[j] - 1] += 1
  
  res = 0
  for k in range(N):
    res += int(b[k] * (b[k] - 1) / 2)
  
  print(res)

Submission status: Time Limit Exceeded

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

A_set = set(A)

def cmb2(n):
	result = n * (n - 1) / 2

	return int(result)

ans_d = {}
for a_s in A_set:
	#print(A.count(a))
	ans_d[str(a_s)] = A.count(a_s)

for a in A:
    A_new = ans_d.copy()
    A_new[str(a)] -= 1
    ans = 0
    #print(A_new)
    #print("="*10)
    for a_n in A_new.values():
        if a_n <= 1:
            continue
        else:
            ans += cmb2(a_n)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
A = [int(s) for s in input().split()]
B = [0 for i in range(N)]
idx = []
for i in range(N):
  B[A[i]-1] += 1
  if B[A[i]-1] >= 2 and not A[i]-1 in idx:
    idx.append(A[i]-1)

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

Cs = []
C = []
Ctot = 0
for j in idx:
  if B[j]-1 >= 2:
    Cs.append(nCr(B[j]-1, 2))
  else:
    Cs.append(0)
  tmp = nCr(B[j], 2)
  C.append(tmp)
  Ctot += tmp

for i in range(N):
  current_idx = A[i]-1
  count = Ctot
  for k, j in enumerate(idx):
    if j == current_idx:
      count += Cs[k]
      count -= C[k]
  print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

a = list(set(A))
cc = Counter(A)
dup = [x for x in a if cc[x] > 1]
m = len(a)
l = len(A) 
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

for k in A:
    if k in dup:
        print(int(al-cc[k]+1))
    else :
        print(int(al))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math


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


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

summ = sum([comb(v, 2) for v in c.values()])
for ai in a:
    print(summ - comb(c[ai], 2) + comb(c[ai] - 1, 2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
for i in range(n):
    rd = a[0: i] + a[i + 1:]
    co = 0
    for j in range(n - 2):
        co += rd[j + 1:].count(rd[j])
    print(co)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
 
def combinations_count(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
def main():
  N = int(input())
  l = list(map(int, input().split()))
  for i in range(N):
    o = l.pop(i)
    c = collections.Counter(l)
    ans = 0
    for v in c.values():
      if v >= 2:
        ans += combinations_count(v, 2)
    print(ans)
    l.insert(i, o)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import math
n=int(input())
A = list(map(int, input().split()))
res=[0]*n
import collections
res1=[-1]*n
def com(n, r):
    if n<r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i in range(n):
    if res1[A[i]-1]==-1:
        ac=copy.deepcopy(A)
        del ac[i]
        ak=collections.Counter(ac)
        aj=list( ak.values() )
        s=0
        for j in range(len(aj)):
            s=s+com(aj[j],2)
        res[i]=s
        res1[A[i]-1]=s
    else:
        res[i]=res1[A[i]-1]
for j in range(n):
    print(res[j])

Submission status: Time Limit Exceeded

Solution code:
import collections
def cal(n):
    ans = int(n*(n-1)/2)
    return ans 
n = int(input())
a = list(map(int,input().split()))

temp_list = []
for i in range(1,n+1):
    temp = a[:i-1]+a[i:]
    # print(temp)
    temp_c = collections.Counter(temp)
    temp_k = temp_c.values()
    temp_cal = list(map(cal,temp_k))
    print(sum(temp_cal))

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
data = list(map(int, input().split()))

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

for k in range(n):
  data_n = data.copy()
  data_n.pop(k)
  cnt = 0
  for i in set(data_n):
    if data_n.count(i) > 1:
      cnt += combinations_count(data_n.count(i), 2)
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
input()
aa = list(map(int, input().split()))
uu = list(set(aa))
cc = uu.copy()
for i in range(len(uu)):
    cc[i] = aa.count(uu[i])
dic = dict(zip(uu,cc))
total = 0
for v in cc:
    total += v * (v-1) / 2

for a in aa:
    print(int(total - dic[a] + 1))

Submission status: Time Limit Exceeded

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

counters = {}
for i in numbers:
    counters[i] = numbers.count(i)
for i in range(n):
    sonuc = 0
    for sayi in counters:
        count = counters[sayi]
        if numbers[i] == sayi:
            count -= 1
        sonuc += count*(count-1)/2
    print(int(sonuc))

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
a = { i:v for i, v in enumerate(input().split(" "))}
combination_a = list(itertools.combinations(a, 2))
for k in range(n):
  count = 0
  for i in combination_a:
    a_1 = i[0]
    a_2 = i[1]
    if a_1 == k:
      continue
    if a_2 == k:
      continue
    if a[a_1] == a[a_2]:
      count += 1
  print(count)

Submission status: Time Limit Exceeded

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

for j in a:
    ans = 0
    for k in c.keys():
        m = c[k]
        if j == k and m >= 3:
            ans += (m-1) * (m-2) // 2
        elif not j == k and m >= 2:
            ans += m * (m-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections

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

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

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

    # A_sub = np.array(A_sub)

    ans = 0
    # u, counts = np.unique(A_sub, return_counts=True)
    c = collections.Counter(A_sub)
    for j in c:
        # val = uni_vals[0]
        n = c[j]
        if j == A[i]:
            n -= 1
        ans += (n*(n-1))/2

    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

nC2 = [0] * (N + 1)
nC2[0] = 0
nC2[1] = 0
for i in range(2, N + 1):
    nC2[i] = i * (i - 1) // 2

# print(nC2)

d = Counter(A)
# print(d)
k = d.keys()

# print(k)

for a in A:
    res = 0
    for kk in k:
        if a == kk:
            res += nC2[d[kk] - 1]
        else:
            res += nC2[d[kk]]
    # print(a, d[a], res)
    print(res)