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 copy
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def D159():
    N = int(input())
    A = input().split()
    #sum = 0
    for i in range(0, N):
        sum = 0
        A_ = copy.deepcopy(A)
        del A_[i]
        num = list(set(A_))
        for j in num:
            if A_.count(j) > 1:
                sum += combinations_count(A_.count(j), 2)
        print(sum)

if __name__ == "__main__":
    D159()

Submission status: Time Limit Exceeded

Solution code:


def main():
    import math
    import collections
    # import numpy as np

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

    # As = np.tile(A, (N, 1))
    # As = [A]*N
    # print(As)
    for i in range(N):
        x = A[i]
        A[i] = 0
        tmp_A = collections.Counter(A)
        su = 0
        for pairs in sorted(tmp_A.values(), reverse=True):
            if pairs >= 2:
                # su += combinations_count(pairs, 2)
                su += pairs*(pairs-1)//2
            else:
                break
        A[i] = x
        print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from _collections import defaultdict
n=int(input())
a=list(map(int,stdin.readline().split()))
dict={}
for i in range(n):
    if dict.get(a[i],False):
        dict[a[i]]+=1
    else:
        dict[a[i]]=1
dict2={}
dict3=defaultdict(list)
for i in dict:
    if dict[i]>1:
        dict3[i].append(((dict[i])*(dict[i]-1))//2)
    if dict[i]>2:
        dict3[i].append(((dict[i]-1)*(dict[i]-2))//2)
[dict.pop(key) for key in list(dict) if dict[key]<2]
for i in range(n):
    sum=0
    if dict2.get(a[i],-1)!=-1:
        sum=dict2[a[i]]
    else:
        for j in dict:
            if j==a[i]:
                if dict[j]>2:
                    sum+=dict3[j][1]
            else:
                if dict[j]>=2:
                    sum +=dict3[j][0]
        dict2[a[i]]=sum
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

A_count = {}
for m in A:
    A_count.setdefault(m, 0)
    A_count[m] = A_count[m] + 1

A_comb = 0
for i in A_count.values():
    if(i > 1):
        A_comb += twocomb(i)

for i in range(N):
    q = A_count[A[i]]
    if(q == 1):
        print(A_comb)
    else:
        if(q == 2):
            print(A_comb-1)
        else:
            print(A_comb - twocomb(q) + twocomb(q-1))

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import scipy.misc 

def each_check(ls):
  nums = list(set(ls))
  total = 0
  for i in nums:
    count = ls.count(i)
    total += scipy.misc.comb(count, 2, exact=True)
  return int(total)

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

for i in L:
  target = L.pop(0)
  print(each_check(L))
  L.append(target)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
    count = 0
    d = {}
    A_ = A[:]
    A_.pop(i)
    A_.sort()
    for j in range(len(A_)-1):
        if A_[j] == A_[j+1]:
            count += 1
        elif A_[j] != A_[j+1]:
            count += 1
            d[A_[j]] = (count*(count-1))//2
            count = 0
    count += 1
    d[A_[j+1]] = (count*(count-1))//2
    print(sum(d.values()))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
dic = {}
ans = 0
a = 0

for i in range(n):
  a = l.count(i+1)
  if a != 0:
    dic[i+1] = a
    x = l.count(i+1)
    ans += int(x*(x-1)/2)
  
for i in range(n):
  if dic.get(l[i]) != None:
    a = dic[l[i]]
    print(ans-a+1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
m = list(map(int,input().split()))
ans=0
O=set(m)
for i in range(N):
    r=m[i]
    del m[i]
    for j in O:
        count=m.count(j)
        ans+=(count*(count-1))/2
    m.insert(i,r)
    print(int(ans))
    ans=0

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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


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

counter = Counter(A)

mapping = {}
for k, v in counter.items():
    mapping[k] = nCr(v, 2)

all_sum = 0
for v in mapping.values():
    all_sum += v

memo = {}
for i in range(n):
    num = A[i]
    if num in memo:
        print(memo[num])
    else:
        t = counter[num]
        ans = all_sum - nCr(t, 2) + nCr(t - 1, 2)
        print(ans)
        memo[num] = ans

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

if N == set(A):
    for _ in range(N):
        print(0)

c2 = math.factorial(2)
umemory = {}
dmemory = {}

count = Counter(A)

for k, v in count.items():
    umemory[k] = 0 if v < 2 else math.factorial(v) // (math.factorial(v - 2) * c2)
    dmemory[k] = 0 if v < 3 else math.factorial(v-1) // (math.factorial(v - 3) * c2)

for k in range(0, N):
    a = A[k]
    hoge = sum(map(lambda kv: kv[1], filter(lambda kv: kv[0] != a, umemory.items())))
    hoge += dmemory[a]
    print(hoge)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    b=a[i]
    del a[i]
    c = collections.Counter(a)
    cv=c.values()
    s=0
    for j in cv:
        s+=comb(j,2)
    print(s)
    a.insert(i,b)

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):
    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())
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:
import math
import sys
sys.setrecursionlimit(10**8)

fact_table = [-1] * (2*10**5)

def factorial(n):

    if n == 1:
        return 1
    if fact_table[n] != -1:
        return fact_table[n]
    else:
        temp = 1
        for i in range(n,0,-1):
            if fact_table[i] != -1:
                temp = temp * fact_table[i]
                break
            else:
                temp = temp * i

        fact_table[n] = temp
        return fact_table[n]


def combi(n,r):
    #階乗が多すぎる！　階乗テーブルだ！　×××メモリーエラーになる 違う！階乗の再帰によるメモリ不足だ！
    if n < r:
        return 0
    return factorial(n) // (factorial(r) * factorial(n-r))

def main():
    #2*10**5程度なら全てリストに収まる！
    #各数値の数をカウント！　k番目の数値だけ-1！
    #そして組み合わせ計算だ！nC2！
    n = int(input())
    num_counter = [0] + [0]*n #1-indexed!
    lst = [0]
    for i in [int(l) for l in input().split()]:
        t = i
        lst.append(t)
        num_counter[t] += 1

    answer_lst = [0]+[0] * n
    even_flag = [False]+[False] * n

    #answer_lst
    for i in range(1,n+1):
        answer_lst[i] = combi(num_counter[i],2)
        #even_flag[i] = (num_counter[i] % 2 == 0) and (num_counter[i] != 0)



    for i in range(1,n+1):
        k = lst[i]
        answer = sum(answer_lst)
        answer -= answer_lst[k]
        answer += combi(num_counter[k]-1,2)
        print(answer)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(N):
    ans = 0
    a = A[:i]+A[i+1:]
    C = collections.Counter(a)
    for v in C.values():
        ans += (v*(v-1))//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

def combination(n, r, mod=10**9+7):
    n1, r = n+1, min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod
    
n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)    
s = list(set(li))
ans = 0

for j in range(len(s)):
    if c[s[j]] <= 1:
        continue
    ans += combination(c[s[j]], 2)

for i in range(n):
    ans_a = ans
    if c[li[i]] <= 1:
        print(ans_a)
        continue
    ans_a -= combination(c[li[i]], 2)
    c[li[i]] -= 1
    if c[li[i]] <= 1:
        print(ans_a)
        c[li[i]] += 1
        continue
    ans_a += combination(c[li[i]], 2)
    c[li[i]] += 1
    # print(c)
    # print(c[s[1]])
    # print(s)
    print(ans_a)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from itertools import permutations, combinations
N = int(input())
List = list(map(int,input().split()))
dic = defaultdict(int)
dic2 = {}
for item in List:
  dic[item] += 1
for item in List:
  if item in dic2:
    Answer = dic2[item]
  else:
    Answer = 0
    for i in dic:
      if i != item:
        if dic[i] >= 2:
          Answer += len(list(combinations(range(dic[i]),2)))
      else:
        if dic[i] >= 3:
          Answer += len(list(combinations(range(dic[i]-1),2)))
  print(Answer)
  dic2[item] = Answer

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
c = [A.count(i + 1) for i in range(N)]
s = 0

for val in c:
  s += val * (val - 1) // 2

for val in A:
  print(s - c[val - 1] + 1)

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 = input().split()
  temp = l
  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)
      else:
        ans += 0
    print(ans)
    l.insert(i, o)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(input().split())
for i in range(N):
    num = 0
    P = list(A)
    del P[i]
    #print(P)
    for j in range(N-1):
        for k in range(N-1):
            if j == k:
                a =1
            else:
                if P[j] == P[k]:
                    #print('{0}:{1}  {2}:{3}'.format(P[j],P[k],j,k))
                    num +=1
    print(int(num/2))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import math
import collections

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

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        tmp = comb_list[value]
        ans_dic[key] = tmp
        sum_all += tmp

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

from collections import Counter
d=Counter(a)
f={}
for k,v in d.items():
    f[k]=v*(v-1)/2

ans={}
for k in f.keys():
    ans[k]=(d[k]-1)*(d[k]-2)/2
    ans[k]+=sum(f.values())
    ans[k]-=f[k]

for i in range(n):
    print(int(ans[a[i]]))

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

keys, values = zip(*c.most_common())

ans = 0
co = 0
for i in values:
  if i != 1:
  	ans += (i*(i-1)//2)

for i in a:
    for j in range(len(keys)-1,-1,-1):
      if i == keys[j]:
        co = values[j]
        break
    print(ans-(co*(co-1)//2)+((co-1)*(co-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:
# coding:utf-8
from collections import defaultdict

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

dic = defaultdict(lambda: 0)
N = int(input())
A = list(map(int, input().split(' ')))

for a in A:
    dic[a] += 1

if len(dic) == N:
    for _ in range(N):
        print(0)
    exit(0)
else:
    for a in A:
        ans = 0
        dic[a] -= 1
        for n in list(dic.values()):
            ans += comb(n)
        print(ans)
        dic[a] += 1

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
numlist = list(map(int, input().split()))
dic = {}
for num in numlist:
  if num not in dic:
    dic[num] = 1
  else:
    dic[num] += 1
    
ansdic = {}
for key in dic.keys():
  n = dic[key]
  ansdic[key] = [int(n*(n-1)/2),int((n-1)*(n-2)/2)]
      
ansdic2 = {}
for key in ansdic.keys():
  sum = 0
  for key2 in ansdic.keys():
    if key == key2:
      sum += ansdic[key2][1]
    else:
      sum += ansdic[key2][0]
  ansdic2[key] = sum  
    
for num in numlist:
  print(ansdic2[num])

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for k in range(N):
    indices = list(range(N))
    del indices[k]
    ans = [As[i] == As[j] for i, j in itertools.combinations(indices, 2)]
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

num = input()
num = int(num)

str = input()
list_in = str.split()

count_arr = np.zeros((num+1))
sum_arr = np.zeros((num+1))

for item in list_in:
    count_arr[int(item)] += 1

sum = 0
for i in range(len(count_arr)):
    tmp = count_arr[i]
    tmp = (tmp * (tmp -1))
    sum += tmp
    sum_arr[i] = tmp

for item in list_in:
    tmp = int(item)
    old_sum = sum_arr[tmp]
    tmp = count_arr[tmp] - 1
    tmp = (tmp * (tmp -1))
    result = (sum - old_sum + tmp)/2
    print(int(result))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
A_num = list(set(A.copy()))
A_num_size = len(A_num)
for i in range(N):
    out = 0
    j = 0
    while j < A_num_size:
        if A.count(A_num[j]) == 1:
            A_num.remove(A_num[j])
            A_num_size -= 1
            if A_num_size == 0:
                break
            else:
                continue
        if A_num[j] == A[i]:
            out += int((A.count(A_num[j]) * (A.count(A_num[j]) - 1) / 2 - (A.count(A_num[j]) - 1)))
        else:
            out += int((A.count(A_num[j]) * (A.count(A_num[j]) - 1) / 2))
        j += 1
    print(out)

Submission status: Time Limit Exceeded

Solution code:
import collections

def check(A,num):
    count = 0
    for j in A:
        n = A[j]
        if j == num:
            n -= 1
#         print(n)
        count += n * (n - 1) / 2
    return int(count)
    

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

A2 = collections.Counter(A)
# print(A2)
for i in range(0,N):
    num = A[i]
    out = check(A2,num)
    print(out)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
al = [int(x) for x in input().split()]
aset = list(set(al))
aset.sort()

# {number: count}
counts_d = {i: sum(a == i for a in al) for i in aset}

vset = set(counts_d.values())
# Count of count
v_counts = {v: sum(v == i for i in counts_d.values()) for v in vset}

whole_patterns = 0
for k, v in v_counts.items():
    if k > 1:
        whole_patterns += (int(k * (k-1) / 2)) * v

for a in al:
    print(whole_patterns - counts_d[a] + 1)

Submission status: Time Limit Exceeded

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

real_ans=0
ans1=list(set(A))
for k in range(len(ans1)):
    num_k=A.count(ans1[k])
    real_ans+=(num_k*(num_k-1))//2

for i in range(N):
    num=A.count(A[i])
    re_ans=real_ans-(num-1)
    print(re_ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

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

N = int(input())
A = list(map(int, input().split(' ')))
result = {}

for c in set(A):
  if A.count(c) > 1:
    result[c] = combinations_count(A.count(c), 2) 
  else:
    result[c] = 0
  
for k in range(N):
  count = 0
  result_ = result.copy()
  result_[A[k]] -= A.count(A[k])
  print(sum(result_.values()) + 1)

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

    for a in A:
        r = list(c).index(a)
        sum = np.sum(l1) + l2[r] -l1[r]
        print(int(sum))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

from collections import Counter 
import math 

res = []

# counter ver
A_c = Counter(A)
l = len(A_c)

def ncr(j):
  return math.factorial(j) // math.factorial(2) // math.factorial(j - 2) if j >= 2 else 0

# store ncr for each value 
combs = {k: ncr(v) for k, v in A_c.items()}

s = sum(combs.values())
  
for k in range(N): # k = k - 1
  m = A[k] # value of k
  cnt = combs[m] # number of combinations for k

  n = s
  n -= cnt # the only inaccurate entry
  n += ncr(A_c[m] - 1)
  res.append(n)

print('\n'.join([str(s) for s in res]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(raw_input())
O = list(map(int, raw_input().split()))

cnt = Counter(O)
cnt = dict(cnt)

for i in O:
    cnt[i] = cnt.get(i, 1) - 1
    res = 0

    for k in cnt:
        if cnt[k] >=2:
            res += (cnt[k]*(cnt[k]-1))/2
    print(res)

    cnt[i] += 1

Submission status: Time Limit Exceeded

Solution code:
import collections as col
 
n = int(input())
l = list(map(int,input().split()))
dic = col.Counter(l)

lnum = list(dic.values())
lnumc = list(map(lambda x: x*(x-1)/2, lnum))
base = sum(lnumc)
lnumc1 = list(map(lambda x: (x-1)*(x-2)/2, lnum))

lans = []
for i in range(len(lnum)):
    lans.append(int(base-lnumc[i]+lnumc1[i]))

lpac = list(dic.keys())
for i in range(len(l)):
    tmp = lpac.index(l[i])
    print(lans[tmp])

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
k=list(Counter(a).keys())
t=list(Counter(a).values())
for i in range(n):
  s=np.array(copy.copy(t))
  s[k.index(a[i])]-=1
  d = int(sum(s*(s-np.ones(len(s)))//2))
  print(d)

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:
from collections import Counter

N = int(input())
A = [int(_) for _ in input().split()]
cntD = Counter(A)

memo = [-1] * N

memo2 = [0] * N
for item, data in cntD.most_common():
    memo2[item - 1] = (data * (data - 1)) // 2

for a in A:
    # memoにあるか確認
    if memo[a - 1] != -1:
        print(memo[a - 1])
        continue

    ans = 0
    val = cntD[a] - 1
    ans += (val * (val - 1)) // 2
    left = sum(memo2[:a - 1])
    right = sum(memo2[a:])
    ans += left + right
    memo[a - 1] = ans
    print(ans)

Submission status: Time Limit Exceeded

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

number = [0] * 2000001
com = [0] * 2000001
sumc = 0
que = deque([])
seen = []
for i in items:
    nn = items.count(i)
    que.append(nn)
    if i in seen:
        continue
    seen.append(i)
    sumc += nn*(nn-1)//2

while que:
    q = que.popleft()
    if q <2:
        print(sumc-q*(q-1)//2)
    else:
        print(sumc-q+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
aList = list(map(int, input().split()))
acount = []
for i in aList:
    acount.append(aList.count(i)-1)
sums = sum(acount)
ans = [(sums)//2 - i for i in acount]
for i in range(n):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
for i in range(N):
  count[i]  = A.count(i + 1)
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
answer=0
for k in range(n):
  s=a.pop(k)
  t=a
  while len(t)!=0:
    answer=answer+(t.count(t[0]))*(t.count(t[0])-1)/2
    t=[i for i in t if i!=t[0]]
  print(int(answer))
  answer=0
  a.insert(k,s)

Submission status: Time Limit Exceeded

Solution code:
import math,collections

def f(c):
  sum=0
  for i in c.values():
    sum+=g(i,2)
  return sum

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

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

for i in range(n):
  l=k[0:i]
  l.extend(k[i+1:])
  c=collections.Counter(l)
#  print('l:',l,'c:',c)
  print(f(c))

Submission status: Time Limit Exceeded

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

memo = dict()
for i in range(N):
    lst = copy.copy(A)
    lst.pop(i)
    lst.sort()
    count=0
    if "".join(lst) not in memo:
        for i in itertools.combinations(lst, 2):
            pair = list(i)
            if pair[0] == pair[1]:
                count+=1
        memo["".join(lst)] = count
        print(count)
    else:
        print(memo["".join(lst)])
#print(memo)

Submission status: Time Limit Exceeded

Solution code:
import math


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


num_of_ball = input()

balls = input().split()
balls_i = [int(b) for b in balls]

for i, b in enumerate(balls_i):
    original_lis = balls_i.copy()
    selected = original_lis.pop(i)
    rests = original_lis

    # find comb from rest balls
    rests_set = set(rests)
    combs = 0
    for r in rests_set:
        counts = rests.count(r)
        if counts >= 2:
            combs += combinations_count(counts, 2)
    print(combs)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
D={}
x=0
lis=[]
for a in A:
	if a in D.keys():
		D[a]=D[a]+1
	else:
		D[a]=1

z=0
lis=list(D.keys())
for i in range(len(D)):
	y=D[lis[i]]
	z+=int((y**2-y)/2)
for i in range(len(A)):
	x=0
	y=D[A[i]]
	x=x+z-(y-1)
	print(x)

Submission status: Time Limit Exceeded

Solution code:
def c2(x):
    return x * (x - 1) // 2


n = int(input())
a = list(map(lambda i: int(i) - 1, input().split()))
b = list(a.count(i) for i in range(n))
# c = list(c2(b[i]) for i in range(n))
# d = list(c2(b[i] - 1) for i in range(n))
c = list(c2(i) for i in b)
d = list(c2(i - 1) for i in b)

s = sum(c)
# for k in range(n):
#     i = a[k]
#     ans = s - c[i] + d[i]
#     print(ans)
for i in a:
    print(s - c[i] + d[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = input().split()
if len(set(A)) == 1:
    for _ in range(n):
        print(((n - 1) * (n - 2)) // 2)
    exit()
elif len(set(A)) == len(A):
    for _ in range(n):
        print(0)
    exit()

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

kA = {}
for a in dA:
    kA[a] = (dA[a] - 1) * (dA[a] - 2) // 2
    dA[a] = dA[a] * (dA[a] - 1) // 2

for a in A:
    print(sum(dA.values()) - dA[a] + kA[a])

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

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

for i in range(N):
    flag = True
    result = 0
    pop = A.pop(i)
    c = collections.Counter(A)
    values, counts = zip(*c.most_common())
    for v in counts:
        if v > 1:
            result = result + combinations_count(v, 2)
        else:
            break
        
    A.insert(i, pop)
    print(result)

Submission status: Time Limit Exceeded

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

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

n = int(input())
a_list = list(map(int, input().split()))
tmp_list = a_list[:]
tmp_list.append(n + 100)
tmp_list.sort()


count = 0
tmp_count = 0
for j in range(1, len(tmp_list)):
  if tmp_list[j] == tmp_list[j - 1]:
    tmp_count += 1
  else:
    if tmp_count >= 1:
      count += comb(tmp_count + 1, 2)
    tmp_count = 0

for j in range(len(a_list)):
  c = a_list.count(a_list[j])
  if c == 2:
    print(count - 1)
  elif c > 2:
  	print(count - (comb(c, 2) - comb(c - 1, 2)))
  else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

As = np.asarray(input().split(), dtype=np.uint32)
As_sort = np.sort(As)
As_args = np.argsort(As)

numbers = {}
start = 0
count = 1
search = As_sort[0]
for i in range(n-1):
    if As_sort[i+1] == search:
        count += 1
    else:
        numbers[search] = count
        As[As_args[start:start+count]] = count
        start = start + count
        search = As_sort[i+1]
        count = 1
    if i == n-2:
        numbers[search] = count
        As[As_args[start:]] = count


values = np.asarray(list(numbers.values()), dtype=np.uint32)

full_ans = 0
for i in range(len(values)):
    full_ans += (values[i] * (values[i] - 1) // 2)

answers = full_ans - As + 1

for i in range(n):
    print(answers[i])

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

memo = [0] * (2*(10**5))
num_list = [0] * (2*(10**5))

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

for i in range(n):
    num = a[i]
    if memo[num] != 0:
        print(memo[num])
    else:
        cnt = 0
        for j in range(2*(10**5)):
            if j != num:
                cnt += combinations_count(num_list[j], 2)
            else:
                cnt += combinations_count(num_list[j]-1, 2)
        print(cnt)
        memo[num] = cnt

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)
      count = count + aaa
      if count == N:
        break
    print(int(ans))
    d[aaaaa] = int(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys, math

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
A = read_int()

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

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


n = len(d)
comn2 = [0 for _ in range(N + 1)]
def solve():
    all = 0
    for k, v in d.items():
        all += combinations_count(v, 2)
        comn2[v] = combinations_count(v, 2)
        comn2[v - 1] = combinations_count(v - 1, 2)
    for a in A:
        res = all - comn2[d[a]] + comn2[d[a] - 1]
        print(res)
if __name__ == "__main__":
    solve()