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 sys
import collections
from operator import mul
from functools import reduce
 
def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom
 
n = int(input())
l = [int(x) for x in input().split()]
c = collections.Counter(l)
 
# cl = [0 for i in range(n)]
cl = []
ll = [x for x in set(l) if l.count(x) > 1]
 
# print(ll)
 
if not ll:
    for i in range(n):
        print(0)
    sys.exit()
 
sum = 0
for i in ll:
    nc = combinations_count(c[i], 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (c[i] - 2) / c[i]))
 
# print(sum)
# print(cl)
 
def my_index(l, x, default=n):
    if x in l:
        return l.index(x)
    else:
        return default
 
for i in l:
    # print(sum - cl[i-1])
    idx = my_index(ll, i)
    if idx == n:
        print(sum)
    else:
        print(sum - cl[idx])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math

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

class UnionFindTree():
    def __init__(self, n, vals):
        self.parent = [] # 親のノード
        self.rank = [] # 木の深さ
        self.number = [] # グループの要素数
        self.val = []
        for i in range(n):
            self.parent.append(i)
            self.rank.append(0)
            self.number.append(1)
            self.val.append(vals[i])

    # 根ノードを取得
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    # 併合
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.number[y] += self.number[x]
        else:
            self.parent[y] = x
            self.number[x] += self.number[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じノードか判別
    def same(self, x, y):
        return self.find(x) == self.find(y)

    # グループのノード数を取得
    def member_number(self, x):
        x = self.find(x)
        return self.number[x]

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

uft = UnionFindTree(N, A)

vals = {}
for i in range(N):
    if A[i] in vals:
        uft.unite(vals[A[i]], i)
    else:
        vals[A[i]] = i

comb = {}
group_numbers = {}
total = 0
for k, v in vals.items():
    group_number = uft.member_number(v)
    comb[k] = combination(group_number, 2)
    group_numbers[k] = group_number
    total += comb[k]

ans = {}
for k, v in vals.items():
    ans[k] = int(total - comb[k] + combination(group_numbers[k] - 1, 2))

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
#各番号がいくつずつあるのかを整理
#keyに番号、valueにその番号が書かれてあるボールの数
for i in range(n):
    if not s[i] in ans.keys():
        ans[s[i]] = 1
    else:
        ans[s[i]] += 1
#iC2の総和
answer = 0
for i in ans.values():
    answer += i*(i-1)//2
#answer-nC2+(n-1)C2 = answer-n+1
#nはans[s[i]],よって以下の出力で求められる
for i in range(n):
    print(answer-ans[s[i]]+1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from copy import deepcopy

n = int(input())
a = list(map(int,input().split()))
for i in range(n):
  copy_a = deepcopy(a)
  copy_a.pop(i)
  cnt = Counter(copy_a)
  ans = 0
  for _,c in cnt.items():
    if c >= 2:
      ans += c*(c-1)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math


def kumiawase(n):
    if n == 0 or n == 1:
        return 0
    return math.floor(math.factorial(n) / math.factorial(n - 2) / 2)


n = int(input())
aL = list(map(int, input().split(" ")))

d = {}

for a in aL:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

d2 = {}
d3 = {}

for dk, di in d.items():
    d2[dk] = kumiawase(di)
    d3[dk] = kumiawase(di - 1)

for a in aL:
    sm = sum(d2.values())
    print(sm - d2[a] + d3[a])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def combinations_count(n, r):
    return n * (n-1) // r


n = int(input())
l = [int(x) for x in input().split()]

l1 = []
l2 = []
lll = []
dl = []

for i in l:
    t = l.count(i)
    temp = combinations_count(t, 2)
    if i not in dl:
        dl.append(i)
        lll.append(temp)
    l1.append(temp)
    l2.append(combinations_count(t-1, 2))

total = sum(lll)

for i in range(len(l)):
    print(total-l1[i]+l2[i])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

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

L = collections.Counter(A)

c = 0
for i in range(1,N+1):
    n = L[i]
    if n >=2:
        c += factorial(n) // factorial(2) // factorial(n - 2)

for k in range(N):
    c1 = c
    x = A[k]
    n = L[x]
    if n >= 2:
        c1 -= factorial(n) // factorial(2) // factorial(n - 2)
        if n >= 3:
            c1 += factorial(n-1) // factorial(2) // factorial(n-3)
    print(c1)

Submission status: Time Limit Exceeded

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

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):
    C+=B[A[i]-1]
    B[A[i]-1]+=1
for i in range(N):
    print(int(C-(A.count(A[i]))+1))

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce


def combinations_count(n, r):
    if n < r:
        return 0
    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())
l = [int(x) for x in input().split()]

l1 = []
l2 = []
lll = []
dl = []

for i in l:
    t = l.count(i)
    temp = combinations_count(t, 2)
    if i not in dl:
        dl.append(i)
        lll.append(temp)
    l1.append(temp)
    l2.append(combinations_count(t-1, 2))

total = sum(lll)

for i in range(len(l)):
    print(total-l1[i]+l2[i])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

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

L = collections.Counter(A)

c = 0
for z in L.items():
    if z[1] >=2:
        c += factorial(z[1]) // factorial(2) // factorial(z[1] - 2)

for k in range(N):
    c1 = c
    x = A[k]
    if L[x] >= 2:
        c1 -= factorial(L[x]) // factorial(2) // factorial(L[x] - 2)
        if L[x]-1 >= 2:
            c1 += factorial(L[x]-1) // factorial(2) // factorial(L[x]-1 - 2)
    print(c1)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,input().split()))
ans = {}
a = []
sub = {}
answer = 0
a_append = a.append
for i in s:
    if not i in a:
        ans[i] = s.count(i)*(s.count(i)-1)//2
        a_append(i)
        sub[i] = (s.count(i)-2)*(s.count(i)-1)//2
        answer += ans[i]
#print(ans)

#print(answer)
for i in range(n):
    print(answer-ans[s[i]]+sub[s[i]])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
import collections
from functools import reduce
from operator import mul

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

c = collections.Counter(A)

dic = {}
for i in c:
    dic[i]= [combinations_count2(c[i], 2), combinations_count2(c[i] - 1, 2)]
dic_ans = {}
for ai in A:
    if ai in dic_ans:
        print(dic_ans[ai])
        continue
    ans = 0
    for i in dic:
        if i == ai:
            ans += dic[i][1]
        else:
            ans += dic[i][0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
m=int(input())
a=list(map(int,input().split()))
total=0
count=0
for j in range(m):
   c = a.count(j+1)
   if c>1:
      count = math.factorial(c)//math.factorial(2)//math.factorial(c-2)
      total = total + count
      count=0
for k in range(m):
    c = a.count(a[k])
    d=total
    if c > 2:
        d =d - math.factorial(c)//math.factorial(2)//math.factorial(c-2)+math.factorial(c-1)//math.factorial(2)//math.factorial(c-3)
    if c==2:
        d= d-1
    print(d)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

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

L = collections.Counter(A)

L = L.most_common()

c = 0
for z in L:
    if z[1] >=2:
        c += factorial(z[1]) // factorial(2) // factorial(z[1] - 2)

for k in range(N):
    c1 = c
    x = A[k]
    for z in L:
        if z[0] == x and z[1] >= 2:
            c1 -= factorial(z[1]) // factorial(2) // factorial(z[1] - 2)
            if z[1]-1 >= 2:
                c1 += factorial(z[1]-1) // factorial(2) // factorial(z[1]-1 - 2)
    print(c1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = (input().split())
B = []
C = 0
for i in range(N + 1):
  B.append(A.count(str(i)))
  C += int(B[i] * (B[i] - 1) / 2)
for j in range(N):
  print(0 if B[int(A[j])] < 2 else C + 1 - B[int(A[j])])

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.buffer.readline().rstrip())
    A = list(map(int, sys.stdin.buffer.readline().split()))
    P = set(A)

    obj = {}
    for p in P:
        obj[p] = A.count(p)

    p = 0
    for v in obj.values():
        p += v * (v - 1) // 2

    for a in A:
        n = obj[a]
        t = p - n * (n - 1) // 2 + (n - 1) * (n - 2) // 2
        print(t)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
m=int(input())
a=list(map(int,input().split()))
total=0
count=0
for j in range(m):
   c = a.count(j+1)
   if c>1:
      count = math.factorial(c)//math.factorial(2)//math.factorial(c-2)
      total = total + count
      count=0
for k in range(m):
    c = a.count(a[k])
    d=copy.copy(total)
    if c > 2:
        d =d - math.factorial(c)//math.factorial(2)//math.factorial(c-2)+math.factorial(c-1)//math.factorial(2)//math.factorial(c-3)
    if c==2:
        d= d-1
    print(d)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from math import factorial

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

dic = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	dic[newdata[i]] = x * (x - 1) // 2
	after[newdata[i]] = (x - 1) * (x - 2) // 2

numsum = sum(dic.values())

ans = {}
for key in dic.keys():
	ans[key] = numsum - dic[key] + after[key]

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

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import numpy as np

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

c = collections.Counter(A)
ans = 0
for v in c.values():
    if v > 1:
        ans = ans + combinations_count(v, 2)

for i in range(N):
    print(ans + combinations_count(c[A[i]]-1, 2) - combinations_count(c[A[i]], 2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial


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

    C = Counter(A)

    def nc2(n):
        if n <= 1:
            return 0
        return factorial(n) // (factorial(n - 2) * factorial(2))

    total = 0
    for v in C.values():
        total += nc2(v)

    for k in range(N):
        print(total - (nc2(C[A[k]]) - nc2(C[A[k]] - 1)))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# input
n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
all_sum_select = 0
for key in list(a_dic.keys()):
    all_sum_select += int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)

for k, ak in enumerate(a_list):
    if ak in a_dic.keys():
        sum_select_at_k = all_sum_select + int((a_dic[ak]-1) * (a_dic[ak]-2) / 2) \
                           - int(a_dic[ak] * (a_dic[ak]-1) / 2)


    print(sum_select_at_k)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import numpy as np

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

c = collections.Counter(A)
ans = 0
for k, v in c.items():
    if v > 1:
        ans = ans + combinations_count(v, 2)

for i in range(N):
    hoge = c[A[i]] - 1
    print(ans + combinations_count(hoge, 2) - combinations_count(hoge+1, 2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from math import factorial

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

dic = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	dic[newdata[i]] = x * (x - 1) // 2
	after[newdata[i]] = (x - 1) * (x - 2) // 2

numsum = 0
for dicv in dic.values():
	numsum += dicv 

ans = {}
for key in dic.keys():
	ans[key] = numsum - dic[key] + after[key]

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dA = {}
A = []
for a in input().split():
    A.append(a)
    if a in dA:
        dA[a] += 1
    else:
        dA[a] = 1

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from math import factorial

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

dic = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	if x <= 1:
		dic[newdata[i]] = 0
	else:
		dic[newdata[i]] = x * (x - 1) // 2
	if x - 1 <= 1:
		after[newdata[i]] = 0
	else:
		after[newdata[i]] = (x - 1) * (x - 2) // 2

numsum = 0
for dicv in dic.values():
	numsum += dicv 

ans = {}
for key in dic.keys():
	ans[key] = numsum - dic[key] + after[key]

for i in range(N):
	print(ans[A[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

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

for a in A:
    print(sum(dA.values()) - A.count(a) + 1)

Submission status: Time Limit Exceeded

Solution code:
n = input()
al = [int(x) for x in input().split()]
aset = set(al)

# {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 = sum(
    (int(k * (k - 1) / 2)) * v for k, v in v_counts.items() if k > 1
)

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

# C013_scrambled まで通り，次からTLE

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

for i in num_set:
  num_dict[i] = num_list.count(i)

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

for k, v in num_dict.items():
  ans_dict_1[k] = comb(v,2,exact=True)
  ans_dict_2[k] = comb(v-1,2,exact=True)

total = sum(ans_dict_1.values())

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = 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()) - A.count(a) + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from math import factorial

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

dic = {}
after = {}

newdata = np.unique(A)
for i in range(len(newdata)):
	x = np.sum(A == newdata[i])
	if x <= 1:
		dic[newdata[i]] = 0
	else:
		dic[newdata[i]] = x * (x - 1) // 2
	if x - 1 <= 1:
		after[newdata[i]] = 0
	else:
		after[newdata[i]] = (x - 1) * (x - 2) // 2

numsum = 0
for dicv in dic.values():
	numsum += dicv 


for i in range(N):
	print(int(numsum - dic[A[i]] + after[A[i]]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# input
n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in list(a_dic.keys()):
    if a_dic[key] == 1:
        del a_dic[key]
        continue
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)

for k, ak in enumerate(a_list):
    sum_select = 0
    if ak in a_dic.keys():
        sum_select += int((a_dic[ak]-1) * (a_dic[ak]-2) / 2)
    for key in a_solution_dic:
        if key != ak:
            sum_select += a_solution_dic[key]


    print(sum_select)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy

m=int(input())
a=list(map(int,input().split()))
total=0
count=0
for i in range(m):
    b = copy.copy(a)
    del b[i]
    for j in range(m-1):
       c = b.count(j+1)
       if c>1:
          count = math.factorial(c)//math.factorial(2)//math.factorial(c-2)
          total = total + count
          count=0
    print(total)
    total=0

Submission status: Time Limit Exceeded

Solution code:
import collections


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

memo = {}
rmemo = {}

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

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

            res += memo[v]

    rmemo[i] = res
    print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=set(A)
ans=0
for i in B:
  counts=A.count(i)
  ans+=(counts)*(counts-1)/2
for k in A:
  counts2=A.count(k)
  print(int(ans-counts2+1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = (input().split())
B = []
C = 0
for i in range(N + 1):
  B.append(A.count(str(i)))
  C += int(B[i] * (B[i] - 1) / 2)
for j in range(N):
  if B[int(A[j])] > 1:
    D = C + 1 - B[int(A[j])]
    print(D)
  else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}
text = ""
count = 0
while count < len(a):
    i = a[count]
    c = ao.count(i)
    ac += c*c/2 - c/2
    acount[i] = 1-c
    count += 1
ac = int(ac)

for key in acount:
    acount[key] += ac

count = 0
while count < n:
    text += "{0}\n".format(acount[ao[count]])
    count += 1
print(text[0:-1])

Submission status: Time Limit Exceeded

Solution code:
# TLE
def test():
    duplication = {}
    ans = 0
    for x in A:
        if x in duplication:
            continue
        count = A.count(x)
        if count > 0:
            duplication[x] = count
            ans += count*(count-1)//2
    for x in A:
        print(ans -(duplication[x]-1))

from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
def test2():
    sum = 0
    count_dic = defaultdict(int)
    for x in A:
        count_dic[x] +=1
    for v in count_dic.values():
        sum += v*(v-1)//2
    for x in A:
       print(sum -(count_dic[x]-1))
test()

Submission status: Time Limit Exceeded

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

import collections
answer_list = []

for i in range(N):
    new_list = num_list[0:i]+num_list[i+1:N]
    c = collections.Counter(new_list)

    comb = 0

    for value in c.values():
        comb += value*(value-1)/2

    answer_list.append(comb)

for i in answer_list:
    print(int(i))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data = [int(x) for x in input().split()]
cnt = [0]*(n+1)

# ボールに書かれた整数を集計する
for x in data:
    cnt[x] += 1

for k in range(n):
    # k番目のボールの個数を減らす
    cnt_copy = cnt[:]
    cnt_copy[data[k]] -= 1

    answer = 0
    for x in cnt_copy:
        answer += int(x*(x-1)/2)

    print(answer)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# input
n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    if a_dic[key] == 1:
        continue
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    if a_dic[ak] <= 2:
        pass
    else:
        sum_select += int((a_dic[ak]-1) * (a_dic[ak]-2) / 2)
    for key in a_solution_dic:
        if key != ak:
            if a_dic[key] == 1:
                pass
            else:
                sum_select += a_solution_dic[key]


    print(sum_select)

Submission status: Time Limit Exceeded

Solution code:
input()
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}
text = ""
while len(a) != 0:
    i = a.pop(0)
    c = ao.count(i)
    ac += c*c/2 - c/2
    acount[i] = 1-c
ac = int(ac)

for key in acount:
    acount[key] += ac

for i in ao:
    text += "{0}\n".format(acount[i])
print(text[0:-1])

Submission status: Time Limit Exceeded

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

s = 0
c = set()

for i in range(n):
    if a[i] not in c:
        num = a.count(a[i])
        s += num*(num-1) / 2
        c.add(a[i])

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

Submission status: Time Limit Exceeded

Solution code:

from collections import Counter
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
A_set = np.unique(A)
count_dict= Counter(A)
count_sum = np.sum([count_dict[i]*(count_dict[i]-1)/2 for i in A_set])
    
for i in A:
    print(int(count_sum-(np.count_nonzero(A==i)-1)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}
text = ""
while len(a) != 0:
    i = a.pop(0)
    c = ao.count(i)
    ac += (c**2 - c)/2
    acount[i] = 1-c
ac = int(ac)

for key in acount:
    acount[key] += ac

for i in ao:
    text += "{0}\n".format(acount[i])
print(text[0:-1])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# input
n = int(input())
a_list = []
a_dic = {}
for ai in input().split():
    int_ai = int(ai)
    a_list.append(int_ai)
    if int_ai in a_dic:
        a_dic[int_ai] += 1
    else:
        a_dic[int_ai] = 1

a_solution_dic = {}
a_solution_dic_at_k = {}
for key in a_dic:
    a_solution_dic[key] = int(a_dic[key] * (a_dic[key]-1) / 2)
    #a_solution_dic_at_k[key] = int((a_dic[key]-1) * (a_dic[key]-2) / 2)


for k, ak in enumerate(a_list):
    sum_select = 0
    sum_select += int((a_dic[ak]-1) * (a_dic[ak]-2) / 2)
    for key in a_solution_dic:
        if key != ak:
            sum_select += a_solution_dic[key]


    print(sum_select)

Submission status: Time Limit Exceeded

Solution code:
import copy
import collections

N = int(input())
A_list = list(map(int,input().split()))
c = dict(collections.Counter(A_list))


for k in range(1,N+1):
    n = 0
    num = A_list[k-1]
    Dict = copy.copy(c)
    Dict[num] -= 1
    for k,i in Dict.items():
        n += i*(i-1)/2
    print(int(n))

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

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

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

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

total = sum(ans_dict_1.values())

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
keys=[]
values=[]
ans=0
for i in A:
  if i in keys:
    pass
  else:
    counts=A.count(i)
    keys.append(i)
    values.append(counts)
    ans+=(counts)*(counts-1)/2
for k in A:
  a=keys.index(k)
  print(int(ans-values[a]+1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
C = {}

def func01(S,key,C):

    sum_count = 0
    B = set(S)

    if len(B) == N:
        sum_count = 0
    else:
        for s in B:
            count_s = S.count(s)
            sum_count += (count_s * (count_s - 1) // 2)
        C.update({key: sum_count})
    return(sum_count)

for i in range(N):
#すでに計算済みの数字ならば計算結果を参照
#計算未ならば 計算して 計算結果を追加

    if A[i] in C.keys():
        sum_count = C.get(A[i])
    else:
        X = A[0:i] + A[i + 1:N]
        sum_count = func01(X, A[i], C)
    print(sum_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):
    #fact_tableが機能してない！？

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

                break
            else:
                append_counter += 1
                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)

    tmp = sum(answer_lst)

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



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = {}
text = ""
for i in a:
    c = ao.count(i)
    ac += (c**2 - c)/2
    acount[i] = 1-c
ac = int(ac)

for key in acount:
    acount[key] += ac

for i in ao:
    text += "{0}\n".format(acount[i])
print(text[0:-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
A_set = np.unique(A)
count_sum = np.sum([np.count_nonzero(A==i)*(np.count_nonzero(A==i)-1)/2 for i in A_set])
    
for i in A:
    print(int(count_sum-(np.count_nonzero(A==i)-1)))

Submission status: Time Limit Exceeded

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

for i in range(N):
  if str(A[i]) in Adic.keys():
    Adic[str(A[i])] += 1
  else:
    Adic[str(A[i])] = 1
else:
  for i in A:
    count = 0
    ADic = Adic.copy()
    ADic[str(i)] -= 1
    for j in ADic.keys():
      count += ADic[j] * (ADic[j] - 1) // 2
    else:
      print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
keys=[]
values=[]
for i in A:
  if i in keys:
    pass
  else:
    counts=A.count(i)
    keys.append(i)
    values.append(counts)
ans=0
for j in values:
  ans+=j*(j-1)/2
for k in A:
  a=keys.index(k)
  print(int(ans-values[a]+1))