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:
import math
i=0
n=int(input())
l=[int(i) for i in input().split()]
d=0
while(i<n):
    a=l[0:i]+l[i+1:]
    c=set(a)
    i=i+1
    counts=0
    for j in c:
        if(a.count(j)==1 or a.count(j)==0):
            continue
        else:
            d=a.count(j)
            e=math.factorial(d)/(math.factorial(2)*math.factorial(d-2))
            counts=counts+e
    print(int(counts))

Submission status: Time Limit Exceeded

Solution code:
import collections as cl
def main() :
    N = int(input())
    li = [s for s in input().split()]

    c = cl.Counter(li)
    exclude_dic = {}
    tmp_result_dic = {}
    for key in c :
        num = c[key]
        combi = ((num-1) * (num-2)) // 2
        if (combi > 0) :
            exclude_dic[key] = combi
        else :
            exclude_dic[key] = 0
        
        combi = (num * (num-1)) // 2
        if (combi > 0 ) :
            tmp_result_dic[key] = combi
        else :
            tmp_result_dic[key] = 0
    
    result_dic = {}
    for exclude_key in tmp_result_dic :
        a = 0
        for include_key in tmp_result_dic :
            if (exclude_key == include_key) :
                continue
            a += tmp_result_dic[include_key]
        
        result_dic[exclude_key] = a

    
    # print(result_dic)
    # print(exclude_dic)
    for key in li :
        res = exclude_dic[key] + result_dic[key]
        
        print(res)
        



main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

input_1 = input()
input_2 = input_1.split()

A = map(int, input_2)

def equal(num):
    return num
A = [equal(x) for x in A]

ans = 0

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

ans = ans / 2
re = ans

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

    print(int(ans))

    ans = re

Submission status: Time Limit Exceeded

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


for k in range(N):
  ans = 0

  for i in range(N):
    if k == i:
      continue
    # print('i:{}'.format(src[i]), end=' ')
    for j in range(i+1,N,1):
      if(k == j):
        continue
      # print('j:{}'.format(src[j]), end=' ')
      if(src[i]==src[j]):
        ans += 1
    # print()
        

  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# bin/env/pypy3
import sys
import collections
import heapq
import math
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
c = collections.Counter(a)
#print(n,a,c)
memo_f = {}
memo_c = {}
def combinations_count(n, r):
    return n*(n-1)//2
    s = str([n,r])
    if s in memo_c:
        return memo_c[s]
    if not n in memo_f: memo_f[n] = math.factorial(n)
    b = max(n - r,0)
    if not b in memo_f: memo_f[b] = math.factorial(b)
    if not r in memo_f: memo_f[r] = math.factorial(r)
    memo_c[s] = memo_f[n] // (memo_f[b] * memo_f[r])
    return memo_c[s]
for e in a:
    c[e] -= 1
    b = [combinations_count(v,2) for v in c.values() if v > 1]
    print(sum(b))
    c[e] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
M =[]
l = 0

from collections import Counter
for i in range(0, N):
  x = 0
  l = L[i]
  M = L
  M.pop(i)
  c = Counter(M)
  for a, b in c.items():
    if b >= 2:
      x += (b*(b-1))/2
  print(int(x))
  M.insert(i, l)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

N = int(input())
num_list = list(map(int, input().split()))
num_set = set(num_list)

# 全数あるときの組み合わせ
ans_dict_1 = {}
# 1個抜いたときの組み合わせ
ans_dict_2 = {}

for i in num_set:
  num = num_list.count(i)
  ans_dict_1[i] = comb(num,2,exact=True)
  ans_dict_2[i] = comb(num-1,2,exact=True)

total = sum(ans_dict_1.values())

for i in num_list:
  print(total - ans_dict_1[i] + ans_dict_2[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = dict()
for x in A:
    if x not in B:
        B[x] = 0
    B[x] += 1

C = dict()
for x in B:
    if B[x] < 2:
        C[x] = 0
    else:
        C[x] = B[x]*(B[x]-1)//2

for x in A:
    r = 0
    for y in C:
        r += C[y] - (B[y]-1) if y == x and C[y] > 0 else C[y]
    print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans_hash = {}
    num_hash = {}
    for n in A:
        if n not in ans_hash:
            ans = 0
            a = A[:]
            i = a.index(n)
            del a[i]
            val = Counter(a).values()
            for num in val:
                if num in num_hash:
                    ans += num_hash[num]
                else:
                    num_hash[num] = num * (num - 1) // 2
                    ans += num_hash[num]
            ans_hash[n] = ans
        print(ans_hash[n])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


def ncr(n, r):
    if n - r >= 0:
        return factorial(n) // factorial(r) // factorial(n - r)
    else:
        return 0


n = int(input())
a = [int(x) for x in input().split()]
nmap = {}

at = list(set(a))
for i in at:
    al = a.count(i)
    nmap[str(i) + 'a'] = ncr(al, 2)
    nmap[str(i)] = nmap[str(i) + 'a'] - al + 1

for i in a:
    result = 0
    for j in at:
        if j == i:
            result += nmap[str(j)]
        else:
            result += nmap[str(j) + 'a']
    print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools
def hoge(a):
  return a[0] == a[1]
n = int(input())
a = list(map(int, input().split()))
k = 0
while k < n:
  c = 0
  l = a[:k] + a[k+1:]
  #print(l)
  matchList = list(filter(lambda x:hoge(x), list(itertools.combinations(l,2))))
  print(len(matchList))
  k=k+1

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

n = int(input())

m = list(map(str,input().split()))

c = dict(collections.Counter(m))
self_d = {}
d = {}
def ans(a):
    if a<2:
        return 0
    else:
        return math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))

for i in m:
    cnt = 0
    for j,k in c.items():
        if j == i:
            if k - 1 >= 2:
                self_d.setdefault(j, ans(k-1))
                cnt += self_d[j]
                
        else:
            d.setdefault(j, ans(k))
            cnt += d[j]
        
    print(cnt)

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]
c = collections.Counter(a)

memo = {}
rmemo = {}

for i in a:
    if rmemo.get(i):
        print(rmemo[i])
        continue

    res = 0
    for k,v in c.items():
        if i == k: v -= 1
        
        if v >= 2:
            if not memo.get(v):
                memo[v] = combinations_count(v, 2)

            res += memo[v]

    rmemo[i] = res
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

N=int(input())
A=list(map(int, input().split()))
l=[]
    
c = collections.Counter(A)
#print(c)
for i in range(N):
    x=0
    for k,v in c.items():
        if A[i]==k:
            if v>=3:
                a=combi(v-1,2)
                x+=a
        else:
            if v>=2:
                a=combi(v,2)
                x+=a
    l.append(x)
#print(l)
[print(i) for i in l]

Submission status: Time Limit Exceeded

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

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

s = ""
for k in A:
    v = c[u.index(k)]
    s += str(sum - v + 1) + "\n"
print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(num) for num in input().split(" ")]
a_set = set(a_list)
total_comb = 0
for i in a_set:
    i_count = a_list.count(i)
    total_comb += int(i_count*(i_count-1)/2)
for k in range(n):
    except_num = a_list[k]
    print(total_comb - (a_list.count(except_num)-1))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

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

nums = [0] * (N + 1)
for i, v in enumerate(A):
    nums[v] += 1
combs = list(map(lambda n: int(comb(n, 2)), nums))
total = sum(combs)

for k, val in enumerate(A):
    count = comb(nums[val] - 1, 2)
    print(total - combs[val] + count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    import numpy as np
    from collections import Counter

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

    tmp = np.array(list(c.values()))
    l1= tmp*(tmp-1)//2
    l2= (tmp-1)*((tmp-1)-1)//2
    S = np.sum(l1)

    for a in A:
        r = list(c).index(a)
        sum = S + l2[r] -l1[r]
        print(int(sum))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

import math

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

a_ = []
count_ = []
a_ = a
count = 0
ans = 0
z = 0

for k in range(n):
    ans = 0
    z += 1
    #a = a_
    count_= []
    b = a[k]
    a[k] = -1
    #print(a)
    for i in range(max(a) + 1):#探す数字
        count = 0
        for j in range(n):
            if a[j] != -1 and a[j] == i:
                    count += 1
        count_.append(count)
        #print(count_)
    for c in count_:
        ans += permutations_count(c,2)
    print(ans)
    a[k] = b
    #a = a_

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
 
def main():
    halfdic={}
    n = int(input())
    num_list = list(map(int, input().split()))  
    for i in range(len(num_list)):
        ansdic = {}
        pop = num_list.pop(i)
        if pop in halfdic.keys():
            print(halfdic[pop])
            num_list.insert(i, pop)            
        else:
            dic = collections.Counter(num_list)
            for k, j in dic.items():
                if j < 2:
                  pass
                else:
                  ansdic[k] = int(j * (j-1)/2)
            print(sum(ansdic.values()))
            halfdic[pop] = sum(ansdic.values())
            num_list.insert(i, pop)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial


def combinations_count_bin(n):
    if n < 2:
        return 0
    return n*(n-1) // 2

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

full_s = {i: combinations_count_bin(s.count(i)) for i in set_s}
down_s = {i: combinations_count_bin(s.count(i)-1) for i in set_s}


for i in s:
    print(sum([full_s[k] if i != k else down_s[k] for k in set_s]))

Submission status: Time Limit Exceeded

Solution code:


def print_num(a):

    set_a = set(a)
    num = 0
    for targ_value in set_a:
        num_targ = a.count(targ_value)
        num += int(((num_targ)*(num_targ-1))/2)
    print(num)

if __name__ == "__main__":
    import sys
    n = int(input())
    a = tuple(sys.stdin.readline().split())
    set_a = set(a)
    tuple_set = tuple(set_a)
    tuple_num_targ = [a.count(targ_value) for targ_value in set_a]
    tuple_c = [int(((num_targ)*(num_targ-1))/2) for num_targ in tuple_num_targ]
    all = sum(tuple_c)
    for targ_value in a:
        targ_index = tuple_set.index(targ_value)
        kazu = tuple_num_targ[targ_index]
        targ_sum = int((tuple_c[targ_index] * (kazu-2) / kazu))
        print(all - tuple_c[targ_index] + targ_sum)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
b = collections.Counter(a)
c = b.most_common()
values, counts = zip(*b.most_common())
d = (counts)
ans = 0

for j in range(len(d)):
    f = d[j]
    if f > 1:
        ans = ans + (f*(f - 1)/2)

for i in range(n):
    g = a[i]
    gc = a.count(g)
    tem = ans - gc + 1
    print(int(tem))

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:
n = int(input())

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

d = {}

s = list(set(a))
temp = 0
count = {}
for i in s:
    c = a.count(i)
    count[i] = c
    if c>=2:
        temp += ((c*(c-1))//2)

for i in a:
    c = count[i]
    t = ((c*(c-1))//2)
    res = temp - t
    c -= 1
    res += ((c*(c-1))//2)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections
#from math import factorial
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

n = int(input())
s = list(map(str, input().split()))
ss = ''.join(s)
#print(ss)
for i in range(n):
  sss = ss[:i] + ss[i+1:]
  c = collections.Counter(sss)
  #print(c)
  count=0
  for v in c.values():
    if v >= 2:
      vv = cmb(v, 2)
      count += vv
  print(count)

Submission status: Time Limit Exceeded

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

for i in range(N):
    flag = 0
    for j in range(len(T)):
        if A[i] == T[j][0]:
            T[j][1] += 1
            flag = 1
            break
    if flag == 0:
        T.append([A[i],1])

T2 = []

for i in range(len(T)):
    T2.append(T[i][1])


def hoge(List):
    ans = 0
    for i in range(len(List)):
        ans += (List[i]-1)*List[i]//2
    return ans

T3 = []
Ans = hoge(T2)

for i in range(len(T2)):
    T3.append(Ans-(T2[i]-1))

for i in range(N):
    for j in range(len(T3)):
        if T[j][0] == A[i]:
            print(T3[j])
            break

Submission status: Time Limit Exceeded

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

def comb(n, k):
    return (n * (n - 1)) // 2
    

memo = {}
for i in range(len(aa)):
    if not aa[i] in memo:
        hu = copy.deepcopy(aa)
        del hu[i]
        a = collections.Counter(hu)
        summ = 0
        for j in a:
            summ += comb(a[j], 2)
        memo[aa[i]] = summ
    print(memo[aa[i]])

Submission status: Time Limit Exceeded

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

counters = {}
cevap_list = {}
sayilar = set(numbers)
for i in sayilar:
    count = numbers.count(i)
    counters[i] = count

sonuc = 0
for sayi in counters:
    count = counters[sayi]
    sonuc += int(count * (count - 1) / 2)


for i in range(n):
    print(sonuc-counters[numbers[i]]+1)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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


for i in range(len(A)):
    B = A[:]
    B.pop(i)
    B.sort()
    sum = 0
    prev = -1
    sub_cnt = 1
    for i in range(len(B)):
        if prev == B[i]:
            sub_cnt += 1
        else:
            if prev != -1 and sub_cnt >= 2:
                if sub_cnt >= 2:
                    sum += comb(sub_cnt, 2)
            sub_cnt = 1
        prev = B[i]
    if sub_cnt >= 2:
        sum += comb(sub_cnt, 2)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
for i in a:
    ac += sum([j for j in range(1,ao.count(i))])

for i in ao:
    print(ac-ao.count(i)+1)

Submission status: Time Limit Exceeded

Solution code:
import collections

def count_defaultdict(it):
    counter = collections.defaultdict(int)
    for x in it:
        counter[x] += 1
    return dict(counter)

n = int(input())
l = list(map(int, input().split()))
s = list(set(l))
all_sum = 0
count = {}
for i in range(len(s)):
    v = l.count(s[i])
    all_sum += v * (v - 1) // 2
count = count_defaultdict(l)
for i in range(len(l)):
    if count[l[i]] > 1:
        v = count[l[i]]
        print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
    else:
        print(all_sum)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

def resolve():
    n = int(input())
    a = [int(x) for x in input().split()]

    for k in range(n):
        # 初期化
        ak = a[:] # deep copy
        del ak[k]
        kumi = 0
        ended = set()

        # loop
        for a0 in ak:
            if a0 in ended:
                continue
            cnt = ak.count(a0)
            if cnt >= 2:
                kumi += comb(cnt, 2, exact=True)
            ended.add(a0)
        print(kumi)

resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

d = {}

s = list(set(a))
temp = 0
count = {}
for i in s:
    c = a.count(i)
    count[i] = c
    if c>=2:
        temp += ((c*(c-1))//2)
for i in s:
    c = count[i]
    t = ((c*(c-1))//2)
    res = temp - t
    c -= 1
    res += ((c*(c-1))//2)
    d[i] = res

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

Submission status: Time Limit Exceeded

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

for j in A:
    if A.count(j) == 1:
        print(num_sum)
    else:
        print(num_sum - (A.count(j) - 1))

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)

from operator import mul
from functools import reduce

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

Cs = []
C = []
Ctot = 0
for j in idx:
  if B[j]-1 >= 2:
    Cs.append(nC2(B[j]-1))
  else:
    Cs.append(0)
  tmp = nC2(B[j])
  C.append(tmp)
  Ctot += tmp
for i in range(N):
  current_idx = A[i]-1
  if current_idx in idx:
    count = Ctot + Cs[idx.index(current_idx)]-C[idx.index(current_idx)]
  else:
    count = Ctot
  print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
from collections import Counter
import math

N = int(input())
A = list(map(int, input().split()))
# print(N)
# print(A)
for i in range(N):
    lst = A[:i] + A[i + 1:]
    # print("lst:", lst)
    counter = Counter(lst)
    counter = list(counter.values())
    # print("counter:", counter)
    s = 0
    for i in counter:
        if i >= 2:
            s += math.factorial(i) // (math.factorial(i - 2) * math.factorial(2))
        else:
            s += 0
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans_hash = {}
    num_hash = {}
    for n in A:
        if n in ans_hash:
            print(ans_hash[n])
        else:
            ans = 0
            a = A[:]
            i = a.index(n)
            del a[i]
            val = Counter(a).values()
            for num in val:
                if num in num_hash:
                    ans += num_hash[num]
                else:
                    num_hash[num] = num * (num - 1) // 2
                    ans += num_hash[num]
            ans_hash[n] = ans
            print(ans_hash[n])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
a=list(map(int,input().split(' ')))
l={}
m=0
for i in a:
  l[i]=l.get(i,0)+1
  if i > m:
    m = i
fl={}
for i in range(1, m+1):
  try:
    fl[i]=math.factorial(l[i])//(math.factorial(l[i]-2)*2)
  except:
    fl[i]=0
s=sum(fl.values())
for i in a:
  buf = fl[i]
  if l[i]>=3:
    c = math.factorial(l[i]-1)//(math.factorial(l[i]-3)*2)
  else:
    c = 0
  print(s-fl[i]+c)

Submission status: Time Limit Exceeded

Solution code:
import copy

N=int(input())

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

#print(A)

mama=dict()
papa=dict()
parent=dict()

An=set(A)

for i in An:
    parent[i]=A.count(i)
parecou=0
#print(parent)
for i in parent:
    f=parent[i]
    parecou+=int((f*(f-1)/2))

#print(parecou)
for i in range(N):
    anko=parent[A[i]]
    print(parecou-int((anko*(anko-1)/2))+int(((anko-2)*(anko-1)/2)))
    """
    if(A[i] in mama):
        print(mama[A[i]])
        continue
    
    AA=copy.deepcopy(A)
    ai=A[i]
    del AA[i]
    #print(AA)
    Aset=set(AA)
    count=0
    
    for i in Aset:
        aa=AA.count(i)
        if(aa in papa):
            count+=papa[aa]
            continue
        #print(i,aa)
        aiu=int((aa*(aa-1))/2)
        count+=aiu
        papa[aa]=aiu
    print(count)
    mama[ai]=count
    """

Submission status: Time Limit Exceeded

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


def cmb(n,r):
    if n <= 1: return 0
    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


def main():
    _ = int(input())
    alist = list(map(int, input().split()))

    ans = {}
    ttt = collections.Counter(alist).items()
    for i in range(len(alist)):
        if alist[i] in ans:
            if i == len(alist)-1:
                print(ans[alist[i]], end='')
            else:
                print(ans[alist[i]])
            continue

        count = 0
        count = sum([cmb(v-1, 2) if k == alist[i] else cmb(v, 2) for k, v in ttt])

        if i == len(alist)-1:
            print(count, end='')
        else:
            print(count)
        ans[alist[i]] = count


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int,input().split()))
s = sum([i*(i-1)//2 for i in collections.Counter(a).values()])

for j in a:
    print(s-a.count(j)+1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
def hoge(a):
  return a[0] == a[1]
n = int(input())
a = list(map(int, input().split()))
k = 0
s = 0
while k < n:
  c = 0
  l = a[:k] + a[k+1:]
  #print(l)
  matchList = list(map(hoge, list(itertools.combinations(l,2))))
  print(len(list(filter(lambda x: x, matchList))))
  k=k+1

Submission status: Time Limit Exceeded

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

d = {}

sm = 0
for i in a:
    if i not in d:
        ac = a.count(i)
        bc = ac -1
        d[i] = (ac*(ac-1))/2 - (bc*(bc-1))/2
        sm += (ac*(ac-1))/2


for j in a:
    print sm-d[j]

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
A = input().split(" ")

unique = set(A)

def combination(number):
  if number <= 1:
    return 0
  else:
    return int(number * (number - 1) / 2)

def caluculate(string):
  data = memo[string]
  if data <= 1:
    return(0)
  else:
    return(data - 1) 
  
total = 0
memo = {}
for i in unique:
  times = A.count(i)
  memo[i] = times
  total += combination(times)

# print(memo)
# print(memo.keys())
# print(total)

for i in A:
  #  print(caluculate(i))
  print(total - caluculate(i))

Submission status: Time Limit Exceeded

Solution code:
"""
"""
from math import factorial
from collections import defaultdict


def int_as_array(num): return list(map(int, [y for y in str(num)]))


def array_as_int(arr): return int(''.join(map(str, arr)))


def read_int(): return int(input())


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


def array_to_string(arr, sep=' '): return sep.join(map(str, arr))


def matrix_to_string(arr, sep=' '): return '[\n' + '\n'.join(
    [sep.join(map(str, row)) for row in arr]) + '\n]'


cache = {}


def fac(x):
    if cache.get(x) is not None:
        return cache[x]
    ans = factorial(x)
    cache[x] = ans
    return ans


def choose_combination(n, r):
    if r > n:
        return 0
    return int((fac(n) / fac((n - r))) * (1 / fac(r)))


def solve(n, array):
    counts = defaultdict(int)
    for x in array:
        counts[x] += 1
    ways_cache = {}
    for k in array:
        if ways_cache.get(k) is not None:
            print(ways_cache[k])
            continue
        ways = 0
        for x, count in counts.items():
            adjusted_count = counts[x]
            if x == k:
                adjusted_count -= 1
            ways += choose_combination(adjusted_count, 2)
        ways_cache[k] = ways
        print(ways)


N = read_int()
array = read_array()
solve(N, array)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial


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

    for k in range(N):
        if A[k] in D.keys():
            print(D[A[k]])
        else:
            tmp = A.copy()
            del tmp[k]
            c = Counter(tmp)
            ans = 0
            for v in c.values():
                if v > 1:
                    ans += factorial(v) // (factorial(v - 2) * factorial(2))
            print(ans)
            D[A[k]] = ans


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input().replace(' ', ''))
cnt = [0 for i in range(n)]
conb = [0 for i in range(n)]
for i in range(n):
    cnt[i] = s.count(str(i + 1))
    conb[i] = cnt[i] * (cnt[i] - 1) // 2
Sum = sum(conb)
for k in range(n):
    ans = Sum - conb[int(s[k]) - 1] + (cnt[int(s[k]) - 1] - 1) * (cnt[int(s[k]) - 1] - 2) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
k=list(map(int,input().split()))
p=[]
for i in range(n):
    x=k.pop(i)
    ans=0
    for j in range(n):
        if k[min(j,n-2)]!=p:
            c=k.count(j)
            ans=ans+c*(c-1)//2
            p.append(k[min(j,n-2)])
    print(ans)
    if i!=n-1:
        k.insert(i,x)
    else:
        k.append(x)

Submission status: Time Limit Exceeded

Solution code:
import math,collections

def f(c):
  sum=0
  for i in c.values():
    sum+=i*(i-1)//2
  return sum

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

for i in range(n):
  c[k[i]]-=1
  print(f(c))
  c[k[i]]+=1

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import collections
from sys import stdin


def counter(num_arr):
    uniq_dict = {}
    for val in num_arr:
        uniq_dict[val] = uniq_dict.get(val, 0) + 1
    return uniq_dict


def get_result(data):
    N = data[0][0]
    A = data[1]
    uniq_cnt = counter(A)
    ans = {}
    for key in uniq_cnt.keys():
        tmp = uniq_cnt.copy()
        tmp[key] -= 1
        ans[key] = int(sum([val * (val-1) / 2 for val in tmp.values()]))

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


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    get_result(data)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

n = int(input())
a = list(map(int, input().split()))
b = collections.Counter(a)
c = b.most_common()
values, counts = zip(*b.most_common())
d = (counts)
ans = 0

for j in range(len(d)):
    f = d[j]
    ans = ans + (f*(f - 1)/2)

for i in range(n):
    g = a[i]
    gc = a.count(g)
    tem = ans - gc + 1
    print(int(tem))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import Counter

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

N = int(input())
A = list(input().split())
sum = 0
counter = Counter(A)
for key,value in counter.items():
    sum = sum + combinations_count(value,2)

for k in range(N):
    add = 0
    add = sum - combinations_count(counter[A[k]],2) + combinations_count(counter[A[k]]-1,2) 
    print(add)

Submission status: Time Limit Exceeded

Solution code:
import numpy
import copy
N = int(input())
E=input().split()
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(str(unique[t])))
for k in range(N):
    uni = copy.copy(uniqueNum)
    uni[unique.index(int(E[k]))] = int(uni[unique.index(int(E[k]))]) - 1
    for w in range(len(uni)):
        ans[k] = ans[k] + uni[w]* (uni[w]-1)/2
for i in range(N):
    print(int(ans[i]))

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
A = input().split(" ")

unique = set(A)

def combination(number):
  if number <= 1:
    return 0
  else:
    return int(number * (number - 1) / 2)

def caluculate(string):
  if memo[string] <= 1:
    return(0)
  else:
    return(memo[string] - 1) 
  
total = 0
memo = {}
for i in unique:
  times = A.count(i)
  memo[i] = times
  total += combination(times)

# print(memo)
# print(memo.keys())
# print(total)

for i in A:
  #  print(caluculate(i))
  print(total - caluculate(i))