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

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

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

        # loop
        while len(ak) > 1:
            a0 = ak[0] # 0番目の要素を検査
            cnt = ak.count(a0)
            if cnt >= 2:
                kumi += kumiawase(cnt ,2)
            ak = [i for i in ak if i != a0]
        print(kumi)

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

resolve()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

n = int(input())
a = list(map(int, input().split()))
nn = len(a) - 1
ctr = Counter(a)

f = {}

for aa in a:
    x = 0
    for k, v in ctr.items():
        if k == aa:
            v -= 1
        if v >= 2:
            if v not in f:
                f[v] = math.factorial(v)
            if (v - 2) not in f:
                f[v - 2] = math.factorial(v - 2)
            c = f[v] // (2 * f[v - 2])
            x += c
    print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def main():
    n = int(input())
    lis = list(map(int,input().split()))
    ans = Counter(lis)
    for j in range(n):
        su = 0
        for k, v in ans.items():
            if k == lis[j]:
                su += (v - 2) * (v - 1) // 2
            else:
                su += (v - 1) * v // 2
        print(su)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
import collections
import copy

@lru_cache(maxsize=2*10**5)
def f(p):
    c=copy.deepcopy(b)
    c[x]-=1
    d=c.values()
    e=0
    for y in d:
        e+=y*(y-1)
    return e//2


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

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

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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


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


count = collections.Counter(a)
ans_arr = [0]*200000


for k in range(n):
    ans = 0
    now = int(a[k])
    if ans_arr[now] != 0:
        print(ans_arr[now])
        continue
    for c in count:
        kosu = count[c]
        if a[k] == c:
            kosu -= 1
        ans += combinations_count(kosu, 2)
    ans_arr[now] = ans
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial

def nCk(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
c = {}
d = {}
for k, v in b.items():
    c[k] = nCk(v, 2) if v > 1 else 0
    d[k] = nCk(v-1, 2) if v > 2 else 0
total = sum(c.values())
for i in range(n):
    ans = total - c[a[i]] + d[a[i]]
    print(ans)

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]=""
    A_k_s=set(A_k)
    for k in A_k_s:
        x=A_k.count(k)
        Com+=int(x*(x-1)/2)
    print(Com)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
As = np.array(list(map(int, input().split())))
a,b = [0]*N,[0]*N
for i in range(N):
  c = np.sum(As==(i+1))
  a[i] = c*(c-1)//2
  b[i] = (c-1)*(c-2)//2
d = sum(a)
for i in As: print(d-a[i-1]+b[i-1])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = list(map(int,input().split()))
for i in range(N):
    sum = 0
    array1 = array.copy()
    array1.pop(i)
    SET = set(array1)
    for j in list(SET):
        if(array1.count(j) <= 1):
            pass
        else:
            sum += (array1.count(j)*(array1.count(j)-1))//2
    print(sum)

Submission status: Time Limit Exceeded

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

number_count = {}
for cmp in data:
    if cmp in number_count:
        number_count[cmp] += 1
        continue
    number_count[cmp] = 1

for i in range(N):
    C = 0
    number_count_temp = number_count.copy()
    number_count_temp[str(data[i])] -= 1
    for cmp in number_count_temp:
        C += int(number_count_temp[cmp] * (number_count_temp[cmp] - 1) / 2)

    print(str(C))

Submission status: Time Limit Exceeded

Solution code:
import itertools

if __name__ == "__main__":
    N = int(input())
    A = input().split()
    for x in range(N):
        tmp = []
        for idx, y in enumerate(A):
            if idx == x:
                continue
            else:
                tmp.append(y)
        result = list(filter(lambda t: t[0] == t[1], list(itertools.combinations(tmp, 2))))
        print(len(result))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import fractions
import decimal
from collections import deque
from collections import Counter
from copy import deepcopy
from collections import defaultdict


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())
L=list(map(str, input().split())) 
ma=defaultdict(int)
for m in L:
    ma[m]=float('inf')
lL=deepcopy(L)
for k in range(n):
    L=deepcopy(lL)
    # print(k,len(L))
    pp=L.pop(k)
    if(ma[pp] != float('inf')):
        print(ma[pp])
        continue
    # print(L)
    s=Counter(L)
    ret=0
    # print(s.values())
    for a in s.values():
        if(a<=1):
            continue
        ret+=cmb(a,2)
    ma[pp]=ret
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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


N = int(input())
L = input().split(" ")

c = collections.Counter(L)

d = {}
tmp_total = 0 

memo1 = {}
L2 = {}

for i in c:
    if c[i] not in memo1.keys():
        tmp = comb(c[i], 2)
        memo1[c[i]] = tmp
        tmp_total += tmp
    else:
        tmp_total += memo1[c[i]]
        
total = tmp_total

for i in L:
    if c[i]-1 not in memo1.keys():
        tmp = comb(c[i]-1, 2)
        memo1[c[i]-1] = tmp
        ans = total - memo1[c[i]] + tmp
        print(ans)
    else:
        ans = total - memo1[c[i]] + memo1[c[i]-1]
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools

n = int(input())
S = list(input().split(" "))

s = [int(n) for n in S]

dic_l = collections.Counter(s)
factor_list = list(dic_l.keys())
count_list = []
for i in range(n):
  tmp = s[i]
  count = 0
  for m in range(len(factor_list)):
    if tmp == factor_list[m]:
      kazu = dic_l[factor_list[m]] -1 
      count = count + len(list(itertools.combinations(range(kazu), 2)))
    else:
      kazu = dic_l[factor_list[m]]
      count = count + len(list(itertools.combinations(range(kazu), 2)))
  count_list.append(count)

count_list = [str(n) for n in count_list]
print("\n".join(count_list))

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

N = int(input())
B = list(map(int, input().split()))
temp = 0

setB = list(set(B))
num = len(setB)

for i in range(N):
  newB = copy.copy(B)
  newB[i] = 0
  res = 0
  for j in range(num):
    res += newB.count(setB[j])*(newB.count(setB[j])-1)/2
  print(int(res))

Submission status: Time Limit Exceeded

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

for i in range(N):
    print(int(C-(B[A[i]-1])+1))

Submission status: Time Limit Exceeded

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


def n_c_2(val):
  ret = 0
  if val > 1:
    ret = (val)*(val-1)//2
  return ret

org_nc2 = {}
org_minus1_nc2 = {}

for k in list(c.keys()):
  org_nc2[k] = n_c_2(c[k])
for k in list(c.keys()):
  org_minus1_nc2[k] = n_c_2(c[k] - 1)

for d in a:
  ans = 0
  for k in list(c.keys()):
    if d == k:
      ans += org_minus1_nc2[k]
    else:
      ans += org_nc2[k]
  print (ans)

Submission status: Time Limit Exceeded

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

import copy

from collections import Counter


ans = [0] * n
for i in range(n):
    b = Counter(a)
    b[a[i]] -= 1
    for w,v in b.items():
        if v >= 2:
            ans[i] += v*(v-1)//2

print(*ans)

Submission status: Time Limit Exceeded

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

#print(c.most_common())

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

for i in range(n):
    ans=0
    for j in range(len(c)):
        if c.most_common()[j][1]>1:
    #登場回数が１以上の要素について考える
            if c.most_common()[j][0]==a[i]:
                x=c.most_common()[j][1]-1
            else:
                x=c.most_common()[j][1]
            ans+=x*(x-1)/2
    print(int(ans))

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

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
            sum += len(list(itertools.combinations(range(num), 2)))
        cache[value] = sum
        print(sum)

Submission status: Time Limit Exceeded

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

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

c = collections.Counter(A)
c_copy = copy.copy(c)
for ai in A:
    c[ai] -= 1
    ans = 0
    for i in c:
        ans += combinations_count(c[i], 2)
    c = copy.copy(c_copy)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = [int(v) for v in input().split()]
    C = [0] * (N + 1)

    for i in A:
        C[i] += 1


    for i in range(N):
        C[A[i]] -= 1
        ans = 0
        for cnt in C:
            ans +=  (cnt * (cnt - 1)) >> 1

        C[A[i]] += 1
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

d0 = {}
for i in range(n):
  s = 0
  tmp = a.popleft()
  if tmp in d0:
    print(d0[tmp])
  else:
    d = {}
    for j in range(n-1):
      if a[j] not in d:
        d[a[j]] = 1
        tmp2 = a.count(a[j])
        if tmp2 >= 2:
          s += combinations_count(tmp2, 2)
    print(s)
    d0[tmp] = s
  a.append(tmp)

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

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

    ans = 0
    for item, val in cntD.most_common():
        if item == a:
            val -= 1
        ans += (val * (val - 1)) // 2

    memo[a - 1] = ans
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1


d_c = {}
d_2_c = {}
for i in d:
    d_c[i] = d[i] * (d[i] - 1) // 2
    d_2_c[i] = (d[i] - 1) * (d[i] - 2) // 2


d_3 ={}

for i in range(n):
    b = a[i]
    count = 0
    if b not in d_3:
        for j in d:
            if j != b:
                count += d_c[j]
            else:
                count += d_2_c[j]
        d_3[b] = count
        print(count)
    else:
        print(d_3[b])

Submission status: Time Limit Exceeded

Solution code:
def d():
    from copy import copy

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

    sA = sorted(A)
    cnt = {}
    for k in set(sA):
        cnt[k] = sA.count(k)

    for i in A:
        new_cnt = copy(cnt)
        new_cnt[i] -= 1
        patterns = list(map(lambda x: x * (x-1) / 2, new_cnt.values()))
        print(int(sum(patterns)))


##########
if __name__ == "__main__":
    d()

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

import copy
def combinations(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

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

    SortList = copy.copy(AList)
    SortList.sort()

    NumberList = []
    CountList = []
    tmp = SortList[0]
    count = 0
    for i in SortList:
        if tmp == i:
            count += 1
        else:
            NumberList.append(tmp)
            CountList.append(count)
            tmp = i
            count = 1
    NumberList.append(tmp)
    CountList.append(count)

    CountListCopy = copy.copy(CountList)

    result = 0
    for i in range(0, len(AList)):
        CountListCopy[NumberList.index(AList[i])] -= 1
        for q in CountListCopy:
            if q < 2:
                continue
            else:
                result += combinations(q, 2)
        print(result)
        result = 0
    
        CountListCopy = copy.copy(CountList)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
m = list(map(int,input().split()))
ans=0
for i in range(N):
    r=m[i]
    del m[i]
    O=set(m)
    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:
import copy
import itertools


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


main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
d = {}
d_sum = {}
for i in range(N):
  if A[i] in d:
    d[A[i]] += 1
    d_sum[A[i]] += d[A[i]] - 1
  else:
    d[A[i]] = 1
    d_sum[A[i]] = 0
#print(A, d,d_sum)
#print(sum(A), sum(d.values()))
for i in range(N):
  print(sum(d_sum.values()) - (d[A[i]] - 1))

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 = []
d = []
for i in range(n):
    b = a.count(i)
    c.append(c2(b))
    d.append(c2(b - 1))
for k in range(n):
    i = a[k]
    ans = sum(c) - c[i] + d[i]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
for i in range(len(A)):
  ans = 0
  l = A[:i] + A[i+1:]
  ans += sum([l.count(x) * (l.count(x) - 1) / 2 for x in set(l) if l.count(x) > 1])
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
def nc2(n):
	if n<2:
		return 0
	else:
		return (n * (n-1))//2
n = int(input())
li = list(map(int, input().split()))
import collections as cl
d = cl.Counter(li)
# print(d)
for i in li:
	ans = 0
	for k,v in d.items():
		if k!=i:
			ans+=nc2(v)
		else:
			ans+=nc2(v-1)
	print(ans)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(1,n+1):
    tmp = li.count(i)
    count += (tmp * (tmp-1))//2

for k in range(n):
    tmp = li.count(li[k])
    answer = count - (tmp * (tmp-1))//2 + ((tmp-1) * (tmp-2))//2
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

n = int(input())
a = list(map(int, sys.stdin.readline().split()))
c = collections.Counter(a)
ans = [0 for i in range(sorted(c.keys())[-1]+1)]

for i in c.keys():
    tmp = 0
    for j in c.keys():
        if i == j:
            tmp += (c[j]-1)*(c[j]-2)//2
        else:
            tmp += c[j]*(c[j]-1)//2
    ans[i] = tmp

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input()
for k in range(n):
    aCopy = a.split()
    res = 0
    del aCopy[k]
    for i in range(n):
        count = 0
        for ai in aCopy:
            if ai == str(i + 1):
                count += 1
        res += int(count * (count - 1) / 2)
    print(res)

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 comon:
  if i[1] != 1:
  	ans += (i[1]*(i[1]-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 math
def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

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

num = [0] * N

for i in A:
    num[i - 1] = num[i - 1] + 1

a = [0] * N
b = [0] * N

for i in range(N):
    if num[i] >= 3:
        a[i] = comb(num[i] - 1, 2)
        b[i] = comb(num[i] - 1, 1)
    elif num[i] == 2:
        a[i] = 0
        b[i] = 1

for i in A:
    print(sum(a) + sum(b) - b[i - 1])

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    _ = I()
    a_list = input().split()

    c = Counter(a_list)
    dict = {}
    for k1, v1 in c.items():
        t = 0
        for k2, v2 in c.items():
            if k1 == k2:
                v2 -= 1
            if v2 >= 2:
                t += int(v2 * (v2 - 1) / 2)
            dict[k1] = t

    for a in a_list:
        print(dict[a])
    pass


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(input())
readline = stdin.readline
A = list(readline().split())

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

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

cmemo = {}

for a in A:
    ans = 0
    for dk, dv in dmemo.items():
        if dk == a:
            dv -= 1
        
        if dv not in cmemo:
            val = ncr(dv)
            cmemo[dv] = val
        ans += cmemo[dv]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(n):
    used_list = []
    sonuc = 0
    for j in range(n):
        if j == i:
            continue
        if numbers[j] not in used_list:
            counter = numbers.count(numbers[j])
            used_list.append(numbers[j])
        else:
            continue
        if numbers[j] == numbers[i]:
            counter -= 1
        # print("count", counter)
        sonuc += int(counter*(counter-1)/2)
    print(sonuc)

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)
B = input().split(" ")
A = []
for a in B:
  A.append(int(a))
  
for i in range(0,N):
  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:
from scipy.misc import comb

n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    l = a[i]
    a[i] = 'X'
    ans = 0
    a_s = set(a)
    for j in a_s:
        c = a.count(j)
        if c < 2:
            continue
        ans += comb(c,2,exact=True)
    print(ans)
    a[i] = l

Submission status: Time Limit Exceeded

Solution code:
import collections
import numpy as np

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

for i in range(n):
    x = a[:i] + a[i+1:]
    c1 = np.array(list(collections.Counter(x).values()))
    c2 = c1[c1 >= 2]
    c3 = np.sum(c2*(c2-1)/2)
    print(int(c3))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numlist = [int(_) for _ in input().split()]
setnum = set(numlist)
dicnum = {}
nsum = 0

for i in setnum:
    dicnum[i] = numlist.count(i)

for l in setnum:
    cal = dicnum[l]
    if cal >= 2:
        nsum += cal*(cal-1)//2
    
for j in range(n):
    ans = nsum
    dicpos = numlist[j]
    cal2 = dicnum[dicpos]
    if cal2 >= 2:
        ans = ans - (cal2-1)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
IND = list(set(A))
CNT = [A.count(x) for x in IND]
# print(IND)
# print(CNT)
CONB = [x*(x-1)//2 for x in CNT]
# print(CONB)
SUM = sum(CONB)
for i in A:
    n = CNT[IND.index(i)]
    ans = SUM - CONB[IND.index(i)] + (n-1)*(n-2)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
 
def main():
    n = int(input())
    As = list(map(int, input().split(" ")))
    partial = reversed(list(itertools.combinations(As, r=n-1)))
    for As_tmp in partial:
        #print(As_tmp)
        a = list(itertools.combinations(As_tmp, 2))
        b = map(lambda x: len(set(x))-2, a)
        ans = - sum(b)
        print(ans)
 
if __name__ == '__main__':
    main()

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 factorial(N) // (factorial(N - 2)* 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

for i in range(N):
    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 sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
c = [A.count(i + 1) for i in range(N)]
ans = [c[i] * (c[i] - 1) // 2 for i in range(N)]

for val in A:
  m = c[val - 1] - 1
  print(sum(ans) - m)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int, input().split()))
set_of_numbers = set(P)
num_dict = {}
num_dict_k = {}
for num in set_of_numbers:
    num_of_num = P.count(num)
    pair_set = int(num_of_num*(num_of_num-1)/2)
    # pair_set_k = int((num_of_num-1)*(num_of_num-2)/2)
    num_dict[num] = P.count(num)
    # if (pair_set_k < 0):
    #     pair_set_k = 0
    # if (pair_set < 0):
    #     pair_set = 0
    # num_dict[num] = pair_set
    # num_dict_k[num] = pair_set_k
    # print(pair_set, pair_set_k)


for k in range(N):
    # print('===========')
    # print(k)
    # 取り除く数字はP[k]
    result = 0
    for num in set_of_numbers:
        numbers = num_dict[num]
        # print(num, numbers)
        if (num == P[k]):
            result += (numbers-1)*(numbers-2)/2
        else:
            result += numbers*(numbers-1)/2
    print(int(result))

Submission status: Time Limit Exceeded

Solution code:
# 200322 D

import collections
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

N = int(input())
A = list(map(int,input().split()))
#Ac = collections.Counter(A)

for i in range(len(A)):
    Ac = collections.Counter(A)
    Ac[A[i]] -= 1
    cmbsum = 0
    for j in Ac:
        if Ac[j] <= 1:
            cmbsum += 0
        else:
            cmbsum += cmb(Ac[j],2)
    print(cmbsum)

Submission status: Time Limit Exceeded

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

p = 7+pow(10,9)
def cmb(n,r,p):
    numerator = 1
    for i in range(n-r+1, n+1):
        numerator = numerator * i % p
    denominator = 1
    for i in range(1, r+1):
        denominator = denominator * i % p
    return numerator * pow(denominator, p-2, p) % p


b = list(set(a))
c = 0
for i in range(len(b)):
    if a.count(b[i]) == 1:
        continue
    else:
        c += cmb(a.count(b[i]), 2, p)

for i in range(n):
    if a.count(a[i]) == 1:
        print(c)
    elif a.count(a[i]) == 2:
        print(c-1)
    else:
        print(c-(a.count(a[i])-1))

Submission status: Time Limit Exceeded

Solution code:
import math
def fcomb(a, b):
  if a < b:
    return 0
  return math.factorial(a) // math.factorial(a - b) // math.factorial(b)

N = int(input())
nMax = 2 * 10 ** 5
l = [0] * (nMax + 1)
ldef = [0] * (nMax + 1)

ak = list(map(int, input().split()))
numMax = 0
for num in ak:
  l[num] += 1
  if num > numMax:
    numMax = num
ctotal = 0
for i in range(numMax + 1):
  if l[i] >= 2:
    com = fcomb(l[i], 2)
    ctotal += com
    ldef[i] = fcomb(l[i] - 1, 2) - com
for num in ak:
  total = ctotal + ldef[num]
  print(total)

Submission status: Time Limit Exceeded

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

for k in range(N):
    count = [0]*(N+1)
    for i in range(N):
        #print(i)
        if i != k:
            count[A[i]] += 1
    print(int(sum([x*(x-1)/2 for x in count])))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
 
n = int(input())
A = np.array(list(map(int, input().split(' '))))
 
A_unique = np.unique(A)
num_dict = {}
total = 0
for a in A_unique:
  tmp = np.count_nonzero(A==a)
  num_dict[a] = (tmp*(tmp-1)/2, (tmp-1)*(tmp-2)/2)
  total += tmp*(tmp-1)/2

ans_dict = {}
for a in A_unique:
  ans_dict[a] = int(total - num_dict[a][0] + num_dict[a][1])

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

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
def combination(a, b):
    tmp = 1
    for i in range(a - b + 1, a + 1):
        tmp *= i
    return tmp // math.factorial(b)

N = int(input())
listA = list(map(int, input().split()))
# 各数字の出現回数をカウント
countA = collections.Counter(listA)

for i in listA:
    count = 0
    for x, y in countA.most_common():
        if x == i:
            y -= 1
        count += combination(y, 2)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

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

ans_dict = {}
for i in range(n):
    if i in ans_dict.keys():
        print(ans_dict[i])
    else:
        buff = a_list[:i]
        buff.extend(a_list[i+1:])
        c = Counter(buff)

        tmp_ans = 0
        for key, value in c.items():
            if value == 1:
                continue
            else:
                tmp_ans += (value * (value - 1))/2
        ans_dict[i] = tmp_ans
        print(int(tmp_ans))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
 
def combinations_count(n, r):
  if n == r:
    return 1
  elif n < r:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

combi = collections.Counter(A)
new_combi = {}
for k, v in combi.items():
  if v > 1:
    new_combi[k] = v
#print(combi)

for i in range(N):
  ans = 0
  
  # i番目の要素をcombiからdecrement
  if A[i] in new_combi:
    new_combi[A[i]] -= 1
    for v in new_combi.values():
      #print(v)
      ans += combinations_count(v, 2)
  
  print(ans)

  # i番目の要素をcombiからincrement
  if A[i] in new_combi:
    new_combi[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy

N=int(input())

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

#print(A)

mama=dict()

for i in range(N):

    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)
        #print(i,aa)
        count+=int((aa*(aa-1))/2)
    print(count)
    mama[ai]=count