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 itertools
import collections


n = int(input())
l = list(map(int, input().split()))
counter = collections.Counter(l)
cache = dict()
len_cache = dict()

for i in range(n):
    value = l[i]
    if value in cache:
        print(cache.get(value))
    else:
        sum = 0
        for k, v in counter.items():
            num = v - 1 if k == value else v
            if num in len_cache:
                sum += len_cache[num]
            else:
                count = len(list(itertools.combinations(range(num), 2)))
                len_cache[num] = count
                sum += count
        cache[value] = sum
        print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())


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

A = input().split()
B = []
C = []
for i in range(N):
    B.append(combination(A.count(str(i + 1)) - 1, 2))
    C.append(combination(A.count(str(i + 1)), 2))
for i in A:
    print(B[int(i)-1] - C[int(i)-1] +sum(C))

Submission status: Time Limit Exceeded

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

b=[0]*N
for a in A:
    b[a-1]+=1
    
cnt=0

for k in range(N):
    tmp=b
    tmp[A[k]-1]-=1
    for i in range(N):
        cnt+=tmp[i]*(tmp[i]-1)//2
        if i==N-1:
            print(cnt)
            cnt=0
            tmp[A[k]-1]+=1

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8
import itertools

def main():
    n = int(input())
    a = list(map(int, input().split()))
    count = 0
    
    for v in itertools.combinations(a, 2):
        if v[0] == v[1]:
            count = count + 1

    for i in range(n):
        print(count - a.count(a[i])+1)
    
if __name__ == '__main__' :
    main()

Submission status: Time Limit Exceeded

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

real_ans=0
ans1=len(set(A))
ans2=collections.Counter(A)
ANS=list(ans2.values())

for j in range(ans1):
    real_ans+=(ANS[j]*(ANS[j]-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:
import copy

def n_sum(n):
  x = 1
  ans_sum = 0
  while x <= n:
    ans_sum += x
    x += 1

  return ans_sum

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

new_a = list(set(a))
dummy_a = copy.deepcopy(new_a)

for i in range(len(new_a)):
  dummy_a[i] = a.count(new_a[i])

for i in a:
  dummy_b = copy.deepcopy(dummy_a)
  dummy_b[new_a.index(i)] -= 1
  ans = 0
  for j in dummy_b:
    ans += n_sum(j - 1)
  print(ans)

Submission status: Time Limit Exceeded

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

def cmb(n, r):
    if n < 2:
        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

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

cal = {}
for a, b in C.items():
    cal[a] = cmb(b, 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:
# from sys import stdin
import math


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


N = int(input())
A = list(map(int, input().split()))
# print(A)
d = {}
cnt = 0
for i in range(N):
    d[A[i]] = d.get(A[i], 0)+1
# print(d)
for l in range(N):
    d[A[cnt]] = d.get(A[cnt], 0)-1
    # print(d)
    # print(len(d))
    sum = 0
    for key, value in enumerate(d):
        # print(key,value)
        sum = sum+combinations_count(d.get(value, 0), 2)
    print(sum)
    d[A[cnt]] = d.get(A[cnt], 0)+1
    # d = {}
    cnt += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math
def nCr(n, r): 
  
    return (math.factorial(n) // (math.factorial(r)  
                * math.factorial(n - r)))
n=int(input())
arr=list(map(int,input().split()))
p=Counter(arr)
# print(dict(p))
dict={}
dict2={}
for i in arr:
    if i not in dict:
        total=0
        for j in p:
        #   print(j)
            if j==i:
                if p[j]-1>1:
                    if p[j]-1 not in dict2:
                        temp=nCr(p[j]-1,2)
                        total+=temp
                        dict2[p[j]-1]=temp
                    else:
                        total+=dict2[p[j]-1]
            else:
                if p[j]>1:
                    if p[j] not in dict2:
                        temp=nCr(p[j],2)
                        total+=temp
                        dict2[p[j]]=temp
                    else:
                        total+=dict2[p[j]]
        dict[i]=total
        print(dict[i])
    else:
        print(dict[i])

Submission status: Time Limit Exceeded

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

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return -1

number_list = []
number_stock_list = []
for k in range(N):
    index = my_index(number_list, A[k])
    if index == -1:
        number_list.append(A[k])
        number_stock_list.append(1)
    else:
        number_stock_list[index] += 1

for k in range(N):
    index = my_index(number_list, A[k])
    number_stock_list[index] += -1
    x = 0
    for i in range(len(number_stock_list)):
        x += number_stock_list[i] * (number_stock_list[i] - 1) / 2
    print(int(x))
    number_stock_list[index] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
answer = [-1] * N
p = [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):
  if answer[A[i] - 1] != -1:
    p[i] = answer[A[i] - 1]
  else:
    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))
    p[i] = s
    answer[A[i] - 1] = s

for i in p:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
def comb(n, r):
    bunshi = 1
    for i in range(n-r+1,n+1):
        bunshi = bunshi*i
    bunbo = 1
    for i in range(1,r+1):
        bunbo = bunbo * i 
    return bunshi//bunbo

def pascal(n):
    c = [[0 for i in range(n+1)] for j in range(n+1)]
    c[0][0] = 1
    for i in range(1, n+1):
        for j in range(n+1):
            if j == 0 or j == i:
                c[i][j] = 1
                continue
            c[i][j] = c[i-1][j-1] +c[i-1][j]
    return(c)

def pascal2(n):
    c = [[0 for i in range(2)] for j in range(n)]
    c[0][0] = 2
    c[0][1] = 1
    for i in range(1, n+1):
        for j in range(n+1):
            if j == 0 or j == i:
                c[i][j] = 1
                continue
            c[i][j] = c[i-1][j-1] +c[i-1][j]
    return(c)

N = int(input())
A = list(map(int, input().split()))
c = pascal(N)
duplication = {}
ans = 0
for x in A:
    if x in duplication:
        continue
    count = A.count(x)
    if count > 0:
        duplication[x] = count
        # ans += comb(count,2)
        ans += c[count][2]
for x in A:
    if x not in duplication:
        print(ans)
    else:
        # print(ans -comb(duplication[x],2) +comb(duplication[x]-1,2))
        print(ans -duplication[x]+1)
        # print(ans -c[duplication[x]][2] +c[duplication[x]-1][2])

Submission status: Time Limit Exceeded

Solution code:
import collections

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


def main(n: int, a: list):
    counter = collections.Counter(a)
    pairs = {k: v * (v - 1) / 2 for k, v in counter.items()}

    for i in range(N):

        temp = pairs.copy()

        target = a[i]
        v = counter[target] - 1
        temp[target] = v * (v - 1) / 2

        pair_cnt = sum(temp.values())
        print(int(pair_cnt))


main(N, A)

Submission status: Time Limit Exceeded

Solution code:
import math

def combinations_count(n):
    return math.factorial(n) // (math.factorial(n - 2) * 2)
  
N = int(input())
A = list(map(int, input().split()))

num_list = [0] * max(A)
for i in range(max(A)):
  num_list[i] = A.count(i+1)
  
max_A = max(num_list)
list_comb = [0] * (max_A+1)
for i in range(2, len(list_comb)):
  list_comb[i] = combinations_count(i)
  
list_ans = [0] * max(A)
for i in range(max(A)):
  ans = 0
  for j in range(max(A)):
    if i == j:
      ans += list_comb[num_list[j]-1]
    else:
      ans += list_comb[num_list[j]]
  list_ans[i] = ans

for i in range(N):
  print(list_ans[A[i]-1])

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=[]
  l.extend(k)
  del l[i]
  c=collections.Counter(l)
  print(f(c))

Submission status: Time Limit Exceeded

Solution code:
import math

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

C = list(set(A))
M = len(C)
t = []
for i in range(M):
  t.append(A.count(C[i]))


s = []  
for i in range(M):
  ans = 0
  for j in range(M):
    if i == j:
      p = t[j]-1
    else:
      p = t[j]
    if p >=2:
      ans += math.factorial(p)/(math.factorial(p-2)*2)
  s.append(int(ans))

for i in range(N):
  print(s[C.index(A[i])])

Submission status: Time Limit Exceeded

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

maxkazu=max(kazu)
atai=0
for i in range(maxkazu):
    kosuu=kazu.count(i+1)
    atai=atai+kosuu*(kosuu-1)/2
    
for i in range(n):
    kotae=atai
    kosuu=kazu.count(kazu[i])
    if (kosuu >= 1):
        kotae=atai-kosuu*(kosuu-1)/2+(kosuu-1)*(kosuu-2)/2
    print(int(kotae))

Submission status: Time Limit Exceeded

Solution code:
import copy
import numpy as np
from math import factorial

def num_choice_2(N):
    if N > 1:
        return N * (N - 1) // 2
    else:
        return 0

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

nums = np.zeros(N, dtype=int)
for a_i in A:
    nums[a_i - 1] += 1

num_default = np.sum([num_choice_2(x) for x in nums])

for i in range(N):
    if nums[A[i] - 1] <= 1:
        ans = num_default
    else:
        nums[A[i] - 1] -= 1
        ans = np.sum([num_choice_2(x) for x in nums])
        nums[A[i] - 1] += 1
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
n = int(input())

a_list = list(map(str, input().split()))
c = collections.Counter(a_list)


for k in range(n):
    ans = 0
    #tmp = c[str(a_list[k])] # a_list[k]の数の個数
    #print('tmp')
    #print(tmp)
    for j in c:
        if str(a_list[k]) == j:
            tmp = c[str(a_list[k])]-1
            #print('tmp')
            #print(tmp)
        else:
            tmp = c[str(j)]
            #print('tmp')
            #print(tmp)
        ans += (tmp-1)*tmp/2

    print(int(ans))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

if len(a) == len(set(a)):
    for i in range(n):
        print(0)
else:
    set = set(a)
    count = [a.count(x) for x in set]
    ans = sum([x * (x - 1) // 2 for x in count])

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


def main():
    n = int(input())
    a = input().split(" ")
    for i, j in enumerate(a):
        print(len([k for k in itertools.combinations([l for k, l in enumerate(a) if k != i], 2) if k[0] == k[1]]))


main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
A_s=set(A)
for i in range(N):
    Com=0
    A_k=A.copy()
    A_k[i]=""
    for k in A_s:
        x=A_k.count(k)
        Com+=int(x*(x-1)/2)
    print(Com)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections
import math

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

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

maxnum = max(A)
lis = [-1] * maxnum
bigC = collections.Counter(A)
for i, a in enumerate(A):
  if lis[a-1] < 0:
    newA = copy.deepcopy(A)
    newA.pop(i)
    c = collections.Counter(newA)
    total = 0
    for idx in c:
      total += combinations_count(c[idx], 2)
      
    lis[a-1] = total
    for x in bigC:
      if bigC[x] == bigC[a]:
        lis[x-1] = total
      
    print(total)
  else:
    print(lis[a-1])

Submission status: Time Limit Exceeded

Solution code:
size = int(input())
values = list(map(int, input().split()))

for i in range(size):
    # 集計
    calc_map = {}
    for j in range(size):
        if i == j:
            continue
        if str(values[j]) in calc_map:
            calc_map[str(values[j])] = calc_map[str(values[j])] + 1
        else:
            calc_map[str(values[j])] = 1
    # パターン
    result = 0
    for val in calc_map.keys():
        if 1 < calc_map[val]:
            for k in range(calc_map[val]):
                result = result + k
    print(result)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
  b = a[0:i]+a[i+1:]
  l = collections.Counter(b)
  values, counts = zip(*l.most_common())
  num = len(counts)
  p = 0
  for i in range(num):
    p += int(counts[i]*(counts[i]-1)/2)
  print(p)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = map(int, input().split())
li = []
for i in a:
    li.append(i)
king_li=[]
for k in range(n):
    cp_li = li.copy()
    del cp_li[k]
    k_li=[]
    for val in collections.Counter(cp_li).values():
        in_li=[]
        in_li.append(int(val*(val-1)*0.5))
        k_li.append(sum(in_li))
    king_li.append(sum(k_li))
for k in king_li:
  print(k)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = input().split()
 
for i in range(n):
  b = copy.copy(a)
  del b[i]
  k = 0
  c = set(b)
  for t in c:
    num = b.count(t)
    for i in range(num):
      k += i
  print(k)

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
l=[int(x) for x in input().split()]
for k in range(n):
  L=l.pop(k)
  ans=0
  for i in range(n+1):
    c=l.count(i)
    if c>1:
      ans=ans+math.factorial(c) // (math.factorial(c- 2) * 2)
  print(ans)
  l.insert(k,L)

Submission status: Time Limit Exceeded

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

from collections import Counter

c = Counter(A)
keys, values = zip(*c.most_common())
ans = 0
for i in range(len(values)):
    ans += values[i]*(values[i]-1)//2


for i in range(N):
    l = keys.index(A[i])
    print(ans - values[l]*(values[l]-1)//2 + (values[l]-1)*(values[l]-2)//2)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline
cache = {}


def dfs(n):
    ret = cache.get(n)
    if ret is not None:
        return ret
    ret = 0
    for i in range(1, n + 1):
        ret += (i - 1)
        cache[i] = ret
    return ret


def get_ans(counter):
    ret = 0
    for key, value in counter.items():
        ret += dfs(value)
    return ret


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

    counter = Counter()
    for num in nums:
        counter[num] += 1

    for num in nums:
        counter[num] -= 1
        print(get_ans(counter))
        counter[num] += 1


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

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

for i in range(len(s)):

    c = collections.Counter(s)

    c[s[i]] -= 1
    count=0
#    print(c)
    for j in range(N):
#        print(c[j])
        count += c[j]*(c[j]-1)//2

    print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

arr = itertools.groupby(a)

combinations_a = {}
combinations_b = {}
for (i, m) in arr:
    l = list(m)
    c = itertools.combinations(l, 2)
    combinations_a[i] = len(list(c))
    l.pop()
    c = itertools.combinations(l, 2)
    combinations_b[i] = len(list(c))

ans_buf = 0
for i in range(0, n):
    remove = aa[i]
    for key in combinations_a:
        if key == remove: continue
        ans_buf += combinations_a[key]

    ans_buf += combinations_b[remove]
    print(ans_buf)
    ans_buf = 0

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
n = int(input())
list_A = list(map(int, input().split()))

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

for i in range(n):
    a = list_A.pop(i)
    b = list(set(list_A))
    ans = 0
    for j in range(len(b)):
        c = list_A.count(b[j])
        if c != 1:
            ans += cmb(c,2)
    print(ans)
    list_A.insert(i,a)

Submission status: Time Limit Exceeded

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

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

for i in range(N):
    L = A.copy()
    del L[i]
    c = collections.Counter(L)
    l = [i for i in c.values() if i != 1]
    ans = 0
    for j in l:
        ans += combinations_count(j,2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
from collections import defaultdict
d = defaultdict(lambda: -1)
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for i in range(len(a)):
  a_ = a.copy()
  b = a_.pop(i)
  if d[str(b)] != -1:
    print(d[str(b)])
    continue
  A = list(set(a_))
  ans = 0
  for j in A:
    ans += comb(a_.count(j), 2, exact=True)

  d[str(b)] = ans
  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)

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    r = 0
    tmp = a
    k = tmp[i]
    tmp.pop(i)
    
    for j in range(n-1):
        r = r + tmp[j+1:].count(tmp[j])
    print(r)
    tmp.insert(0,k)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import math
import numpy as np
import collections

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input()) 
a = np.array(list(map(int, input().split())))
for i in range(n):
    ans = 0
    ind = np.ones(n, dtype=bool)
    ind[i] = False
    c = collections.Counter(a[ind])
    for k in c.keys():
        if c[k] >= 2:
            ans += comb(c[k], 2)
        else:
            ans += 0
    print(ans)

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 = {}

for i in num_set:
  count = 0
  for j in num_set:
    if i == j:
      count = count + comb(num_list.count(j)-1,2,exact=True)
    else:
      count = count + comb(num_list.count(j),2,exact=True)
  ans_dict[i] = count

for i in num_list:
  print(ans_dict[i])

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 A.count(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 not in dup:
        print(int(al))
    else :
        print(int(al-cc[k]+1))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
k=list(map(int,input().split()))
p=[0]*n
for i in range(n):
    count=k.count(i+1)
    p[i]=p[i]+count*max(count-1,0)//2
#print(p)
mas=p
for j in range(n):
    count=k.count(k[j])
    x=max(count-1,0)*max(count-2,0)//2
    print(sum(p)+x-p[k[j]-1])

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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 = list(map(int,input().split()))[:n]

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:
n=int(input())
a_list = list(map(int,input().split()))

import collections
c = collections.Counter(a_list)

dict_items = list(c.items())

for i in range(n): # kごと
    ans_i = 0
    for j in range(len(dict_items)):
        if a_list[i] == dict_items[j][0]:
            ans_i += ((dict_items[j][1] - 1) * (dict_items[j][1] - 2)) / 2
        else:
            ans_i += ((dict_items[j][1] - 0) * (dict_items[j][1] - 1)) / 2
    print(int(ans_i))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

N = int(input())
temp = input().split()
A = {}
for i in range(0, N):
    A[i] = temp[i]

B = {}
for i in range(0, N):
    if not(A[i] in B.keys()):
        B[A[i]] = 1
    else:
        B[A[i]] += 1 

for i in range(0, N):
    B[A[i]] -= 1
    S = 0    
    for value2 in B.values():
        S = S + comb(int(value2), 2, exact=True)
    print(S)
    B[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
listA = list(map(int,input().split()))
count = 0
for k in range(n):
  a = listA[k]
  del listA[k]
  A = set(listA)
  for i in A:
    c = listA.count(i)
    count += int(c*(c-1)/2)
  print(count)
  listA.insert(k,a)
  count = 0

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
result = {}
for i in range(n):
    if a[i] in a[:i]:
        print(result[str(a[i])])
        continue
    com = 0
    a2 = a[:]
    del a2[i]
    a3 = list(set(a2))
    l = len(a3)
    for j in range(l):
        cnt = a2.count(a3[j])
        if cnt > 1:
            com = com + ((cnt * (cnt - 1)) / 2)
    else:
        print(int(com))
        result[str(a[i])] = int(com)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
A1=sorted(A)
order=[]
value=[]
cmb=[]
cnt=1

for i in range(1,N):
    if A1[i]!=A1[i-1]:
        order.append(A1[i-1])
        value.append(cnt)
        cmb.append((cnt*(cnt-1)//2))
        cnt=1
        if i==N-1:
            order.append(A1[i])
            value.append(1)
            cmb.append(0)
    else:
        cnt+=1
        if i==N-1:
            order.append(A1[i])
            value.append(cnt)
            cmb.append(cnt*(cnt-1)//2)
SUM=sum(cmb)

for j in range(N):
    n=order.index(A[j])
    v=value[n]
    print(SUM-cmb[n]+(v-1)*(v-2)//2)

Submission status: Time Limit Exceeded

Solution code:
n =int(input())
a = list(map(int,input().split()))
b = []
x = {}
def fx(x) :
    ans = 0
    for i in range(1,x):
        ans += i
    return ans
for i in a :
    if i in x :
        x[i] += 1
        if x[i] == 2:
            b.append(i)
    else :
        x[i] = 1
anskari = 0
for i in b :
    anskari += fx(x[i])
for i in a :
    if x[i] == 1:
        print(anskari)
    else :
        print(anskari - fx(x[i]) + fx(x[i]-1))

Submission status: Time Limit Exceeded

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

c = collections.Counter(a)

comon = c.most_common()
ans = 0

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

for i in a:
    for j in comon:
      if i == j[0]:
        co = j[1]
        break
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy
n = int(input())
integer = list(map(int, input().split()))
length = len(integer)
tmp = []
count = 0
for i in range(length):
  tmp = copy.copy(integer)
  del(tmp[i])
  for j in range(length-2):
    for k in range(j+1, length-1):
      if tmp[j] == tmp[k]:
        count += 1      
  print(count)
  count = 0

Submission status: Time Limit Exceeded

Solution code:
import collections

N = input()
L = input().split()
C = collections.Counter(L)
I = list(C.keys())
V = list(C.values())
result = []
result2 = []

for i in range(len(I)):
    result.append(V[i] * (V[i] - 1) / 2)
    result2.append((V[i] - 1) * (V[i] - 2) / 2)

for l in L:
    print(int(sum(result) - result[I.index(l)] + result2[I.index(l)]))

Submission status: Time Limit Exceeded

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

ans_sum = 0

for i in range(n):
    count = a_li.count(i + 1)
    count = count*(count - 1) // 2
    ans_sum += count

for a in a_li:
    count = a_li.count(a)
    if count == 1:
        print(ans_sum)
    else:
        print(ans_sum - count*(count - 1) // 2 + (count - 1)*(count - 2) // 2)

Submission status: Time Limit Exceeded

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

ans = [0] * 2*10**5

for a in set(A):
  B = A[:]
  B.remove(a)
  count = 0
  for i in set(B):
    l = B.count(i) - 1
    count += l * (l + 1) //2
  ans[a] = count
  
for i in A:
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
l = list(map(int, input().split()))
import collections
z = []
for i in range(k):
  m = [x for x in l]
  m.pop(i)
  n = collections.Counter(m)
  y = [x* (x -1) / 2 for x in n.values()]
  z.append(sum(y))

for i in range(k):
  print(int(z[i]))

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)
for a in A:
    s = 0
    if results.get(a):
        print(results[a])
    else:
        for k, v in c.items():
            if k == a:
                v -= 1
            s += get_combination(v)
        results[a] = s
        print(s)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    b = a[i]
    a[i] = 0
    x = 0
    for v in collections.Counter(a).values():
      if v >=2:
        x += v*(v-1)//2
    a[i] = b
    print(x)

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):
  if A[i] in d:
    print(d[A[i]])
    break
  pre_A = A[:]
  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))

Submission status: Time Limit Exceeded

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


cnt_list = [0]*(N+1)
for i in range(1,N+1):
  cnt_list[i] = A.count(i)

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


for i in range(0,N):
  print(int(result-(cnt_list[A[i]]-1)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nums = list(map(int,input().split()))
M = max(nums)
counts = [0] * (M + 1)
for i in range(1,M+1):
    a = nums.count(i)
    counts[i] = int(a*(a-1)/2)

for i in range(N):
    copy = counts[:]
    b = nums[i]
    copy[b] = 0
    c = nums.count(b) - 1
    S = sum(copy) + int(c*(c-1)/2)
    print(S)

Submission status: Time Limit Exceeded

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


for k in range(N):
  d = c.copy()
  d[A[k]] -= 1
  count_all = 0
  for k, v in d.items():
    if v != 1 and v != 0 :
      count_all += math.factorial(v)//(math.factorial(v-2)*2)
  print(count_all)

Submission status: Time Limit Exceeded

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

import collections
import copy

ans = 0

for i in range(len(A)):
  ans = 0
  list = copy.copy(A)
  list.pop(i)
  c = collections.Counter(list)
  for v in c.values():
    ans += (v*(v-1)/2)
  print(int(ans))