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:
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under


n = int(input())
a = list(map(int, input().split()))
#print(n, a)
dic = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    ans = 0
    if not a[i] in dic:
        check = {}
        for j in range(n):
            if not a[j] in check and i != j:
                #print('i = {0}'.format(i))
                #print('j = {0}'.format(j))
                ball_n = a.count(a[j]) - int(a[i] == a[j])
                #print('ball_n = {0}'.format(ball_n))
                if ball_n >= 2:
                        cnt = cmb(ball_n, 2)
                        check[a[j]] = cnt
                        #print('check = {0}'.format(check))
                if a[j] in check:
                    ans += check[a[j]]
        dic[a[i]] = ans
    #print('dic = {0}'.format(dic))
    print(dic[a[i]])

Submission status: Time Limit Exceeded

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

n = int(input())
a = list(map(int, input().split()))
#print(n, a)
dic = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    ans = 0
    if not a[i] in dic:
        check = {}
        for j in range(n):
            if not a[j] in check and i != j:
                #print('i = {0}'.format(i))
                #print('j = {0}'.format(j))
                ball_n = a.count(a[j]) - int(a[i] == a[j])
                #print('ball_n = {0}'.format(ball_n))
                if ball_n >= 2:
                        cnt = comb(ball_n, 2)
                        check[a[j]] = cnt
                        #print('check = {0}'.format(check))
                if a[j] in check:
                    ans += check[a[j]]
        dic[a[i]] = ans
    #print('dic = {0}'.format(dic))
    print(dic[a[i]])

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools
N=int(input())
A=list(map(int,input().split()))
dp={}

for k in range(N):
  if A[k] in dp:
    print(dp[A[k]])
  else:
    B=copy.copy(A)
    B.remove(A[k])
    p=itertools.combinations(B,2)
    c=0
    for i in p:
      if i[0]==i[1]:
        c+=1
    dp[A[k]]=c
    print(c)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

n = int(input())
a = list(map(int,input().split()))
c = [0 for _ in range(n+1)]
b = collections.Counter(a)
total = 0
for d,e in b.most_common():#d,e = (ボールの数字,個数)
    if e>2:
        total += ncr(e,2)#全部あるときの組み合わせの個数足す
        c[d] = ncr(e,2)- ncr(e-1,2)#dの数字のボールが１個減ったときの差分覚えておく
    if e == 2:
        total += 1
        c[d]=1
for i in range(n):
    ans = total
    x = a[i]
    ans -= c[x]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

n = int(input())
a = list(map(int,input().split()))
c = [[0 for _ in range(2)] for _ in range(n+1)]
b = collections.Counter(a)
total = 0
for d,e in b.most_common():
    if e>2:
        total += ncr(e,2)
        c[d][0] = ncr(e,2)
        c[d][1] = ncr(e-1,2)
    if e == 2:
        total += 1
        c[d][0] = 1
        c[d][1] = 0
for i in range(n):
    ans = total
    x = a[i]
    ans -= c[x][0]
    ans += c[x][1]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
CA = Counter(A)
all_count = 0
for ca in CA.values():
  all_count += ca*(ca-1)//2

for a in A:
  print(all_count - (A.count(a)-1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy 

N=int(input())
A=list(map(int,(input().split())))
for i in A:
  ans=0
  new_A=copy.copy(A)
  new_A.remove(i)
  c = Counter(new_A)
  t = [j for j in c.values() if j>=2]
  for k in t:
    ans+=k*(k-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy 

N=int(input())
#A=list(map(int,(input().split())))
A=[a for a in range(1,100001)]
for i in A:
  ans=0
  new_A=copy.copy(A)
  new_A.remove(i)
  c = Counter(new_A)
  t = [j for j in c.values() if j>=2]
  for k in t:
    ans+=k*(k-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy 

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

for i in A:
  ans=0
  new_A=copy.copy(A)
  new_A.remove(i)
  c = Counter(new_A)
  t = [j for j in c.values()]
  for k in t:
    ans+=k*(k-1)/2
  print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import math
def fac(k):
    if k >= 2:
        
        return(math.factorial(k)//(math.factorial(k-2)*2))
    else:
        return(0)

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

i = 0
while i < n:
    ans = fac(lst.count(lst[i])-1) + sum([fac(lst.count(j)) for j in set(lst) if j != lst[i]])
    print(ans)
    
    i += 1

Submission status: Time Limit Exceeded

Solution code:
import math
def fac(k):
    if k >= 2:
        
        return(math.factorial(k)//(math.factorial(k-2)*2))
    else:
        return(0)

n = int(input())
lst = list(map(int, input().split()))
factlist = [fac(lst.count(i)-1) for i in lst]
d = [[i,fac(lst.count(i))] for i in set(lst)]
i = 0
while i < n:
    ans = factlist[i] + sum([fac(lst.count(j)) for j in set(lst) if j != lst[i]])
    print(ans)
    i += 1

Submission status: Time Limit Exceeded

Solution code:
import math
def fac(k):
    if k >= 2:
        
        return(math.factorial(k)//(math.factorial(k-2)*2))
    else:
        return(0)

n = int(input())
lst = list(map(int, input().split()))
factlist = [fac(lst.count(i)-1) for i in lst]
d = [[i,fac(lst.count(i))] for i in set(lst)]
i = 0
while i < n:
    ans = factlist[i] + sum([j for [k,j] in d if k!=lst[i]])
    print(ans)
    i += 1

Submission status: Time Limit Exceeded

Solution code:
# abc159_d.py
import sys
import time
import unittest
from io import StringIO
from collections import defaultdict


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


def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    dd = defaultdict(int)
    for a in A:
        dd[a] += 1
    cb = {0: 0}
    for j in dd.keys():
        if dd[j] not in cb.keys():
            cb[dd[j]] = cmb(dd[j], 2)
        if dd[j] > 1 and dd[j] - 1 not in cb.keys():
            cb[dd[j] - 1] = cmb(dd[j] - 1, 2)
    for i in range(N):
        sum = 0
        for j in dd.keys():
            if A[i] == j:
                sum += cb[dd[j] - 1]
            else:
                sum += cb[dd[j]]
        print(int(sum))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Submission status: Time Limit Exceeded

Solution code:
def unique(w):
    d = {}
    for c in w:
        if c in d.keys():
            d[c] += 1
        else:
            d.setdefault(c, 1)
    return list(d.keys()), list(d.values())

from math import factorial

def choose(n, k):
    if n < k:
        return 0
    else:
        return factorial(n) // factorial(k) // factorial(n - k)


import numpy as np
def f(n, a):
    a = np.array(a)
    key, counts = unique(a)
    ans0 = sum([choose(k, 2) for k in counts])
    for k in range(n):
        b = np.where(key == a[k])
        print(ans0 - counts[b[0][0]] + 1)

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

Submission status: Time Limit Exceeded

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

balls = {}

for a in A:
  if a in balls:
    balls[a] += 1
  else:
    balls[a] = 1

for a in A:
  balls[a] -= 1
  res = 0
  for b in balls:
    count = balls[b]
    res += count * (count-1) // 2
  balls[a] += 1
  print(res)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from math import factorial
from collections import Counter, defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def comb(n, r):  # 組み合わせ
    if n <= 0 or r > n: return 0
    return factorial(n) // factorial(n - r) // factorial(r)

N = ir()
A = lr()
counter = Counter(A)
case = 0
dic = defaultdict(int)
for k, v in counter.items():
    dic[k] = v
    case += comb(v, 2)

for a in A:
    num = dic[a]
    answer = case - comb(num, 2) + comb(num-1, 2)
    print(answer)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
l=list(map(int,input().split()))
k=0
d=Counter(l)
v=0
for i in range(n):
    for j in d:
        if j==l[i]:
            # print(l[i])
            v=d[j]-1
        else:
            v=d[j]

        k+=(v*(v-1))//2

        # print(d)
    print(k)
    k=0

Submission status: Time Limit Exceeded

Solution code:
def possibleCombos(num):
    num2 = num - 1
    return (num2*num2 + num2)//2
N = int(input())
A = [i for i in input().split()]
keys = {i:A.count(i) for i in sorted(set(A)) }
for i in A:
    result = 0
    for k in keys.keys():
        count = keys[k] - 1 if k == i else keys[k]
        result += possibleCombos(count)
    print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from math import factorial
n = int(input())
a = list(map(int,input().split()))
ans = []
for it in itertools.combinations(a,2):
    if it[0] == it[1]:
        ans.append(it[0])
for i in range(n):
    if a[i] in ans:
        print(len(ans)-a.count(a[i])+1)
    else:
        print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
a = list(map(int,input().split()))
tmp = 0
#ans = [0]*n
for i in range(n):
    tmp = a[i]
    a[i] = 0
    count = 0
    for it in itertools.combinations(a,2):
        if it[0] == it[1]:
            count += 1
    print(count)
    a[i] = tmp

Submission status: Time Limit Exceeded

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

allCount = 0
c = Counter(A)

for i, v in c.items():
    if (v == 1):
        continue
    else:
        allCount += factorial(v) // factorial(2) // factorial(v - 2)


if (allCount == 0):
    for i in range(N):
        print(0)
    exit()

for i in range(N):
    a = allCount
    num = c[A[i]] - 1
    if (num == 1 or num == 0):
        print(allCount - num)
        continue
    old = factorial(c[A[i]]) // factorial(2) // factorial(c[A[i]] - 2)
    count = factorial(num) // factorial(2) // factorial(num - 2)
    print(a - old + count)

Submission status: Time Limit Exceeded

Solution code:
# コードとしては正しいけどTLEが出る
# from collections import Counter
# from math import factorial
# N = int(input())
# A = list(map(int, input().split()))

# Length = len(list(set(A)))

# if (Length == len(A)):
#     for i in range(N):
#         print(0)
#     exit()

# c = Counter(A)
# d = [0] * N
# count = 0
# for i in range(N):
#     if (d[A[i]] != 0):
#         print(d[A[i]])
#         continue

#     if (c[A[i]] == 1):
#         for j, v in c.items():
#             if (v == 1):
#                 continue
#             else:
#                 count += factorial(v) // factorial(2) // factorial(v - 2)
#     else:
#         for j, v in c.items():
#             if (v == 1):
#                 continue
#             elif (j == A[i] and v - 1 != 1):
#                 count += factorial(v - 1) // factorial(2) // factorial(v - 3)
#             elif (j == A[i] and v - 1 == 1):
#                 continue
#             else:
#                 count += factorial(v) // factorial(2) // factorial(v - 2)
#     d[A[i]] = count
#     print(count)
#     count = 0

from collections import Counter
from math import factorial
N = int(input())
A = list(map(int, input().split()))

allCount = 0
c = Counter(A)

for i, v in c.items():
    if (v == 1):
        continue
    else:
        allCount += factorial(v) // factorial(2) // factorial(v - 2)


if (allCount == 0):
    for i in range(N):
        print(0)
    exit()

for i in range(N):
    a = allCount
    num = c[A[i]] - 1
    if (num == 1 or num == 0):
        print(allCount - num)
        continue
    old = factorial(c[A[i]]) // factorial(2) // factorial(c[A[i]] - 2)
    count = factorial(num) // factorial(2) // factorial(num - 2)
    print(a - old + count)

Submission status: Time Limit Exceeded

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

Length = len(list(set(A)))

if (Length == len(A)):
    for i in range(N):
        print(0)
    exit()

c = Counter(A)
d = [0] * N
count = 0
for i in range(N):
    if (d[A[i]] != 0):
        print(d[A[i]])
        continue

    if (c[A[i]] == 1):
        for j, v in c.items():
            if (v == 1):
                continue
            else:
                count += factorial(v) // factorial(2) // factorial(v - 2)
    else:
        for j, v in c.items():
            if (v == 1):
                continue
            elif (j == A[i] and v - 1 != 1):
                count += factorial(v - 1) // factorial(2) // factorial(v - 3)
            elif (j == A[i] and v - 1 == 1):
                continue
            else:
                count += factorial(v) // factorial(2) // factorial(v - 2)
    d[A[i]] = count
    print(count)
    count = 0

Submission status: Time Limit Exceeded

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

count = 0
for i in range(N):
    if (i == 0):
        ap = A[1:]
    elif(i == N - 1):
        ap = A[:i]
    else:
        ap = A[:i] + A[i + 1:]
    c = Counter(ap)
    for j, v in c.items():
        if (v <= 1):
            continue
        else:
            count += factorial(v) // factorial(2) // factorial(v - 2)
    print(count)

    count = 0

Submission status: Time Limit Exceeded

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

d = dict()
for a in A:
    d.setdefault(a, 0)
    d[a] += 1

for k in range(0, N):
    c = 0
    ak = A[k]
    for key, v in d.items():
        if key == ak:
            c += (v-1)*(v-2)//2
        else:
            c += v*(v-1)//2
    print(c)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    itr = map(int, input().split())
    dic = {}
    ls = []
    for num in itr:
        z = dic.setdefault(num, 0)
        dic[num] = z + 1
        ls.append(num)

    for i in range(n):
        # ls[i]を除く
        a = ls[i]
        dic[a] -= 1
        res = 0
        for k, v in dic.items():
            if v < 2:
                continue
            elif v == 2:
                res += 1
            else:
                res += int(v*(v-1)/2)
        print(res)
        dic[a] += 1


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(100000000)


def main():
    n = int(input())
    ls = list(map(int, input().split()))
    dic = {}
    for x in ls:
        z = dic.setdefault(x, 0)
        dic[x] = z + 1

    for i in range(n):
        # ls[i]を除く
        a = ls[i]
        dic[a] -= 1
        res = 0
        for k, v in dic.items():
            if v < 2:
                continue
            elif v == 2:
                res += 1
            else:
                res += int(v*(v-1)/2)
        print(res)
        dic[a] += 1


main()

Submission status: Time Limit Exceeded

Solution code:
def possibleCombos(num):
    return sum([i-1 for i in range(1,num+1)])


N = int(input())
A = [i for i in input().split()]
keys = {}
for i in A:
    if  i not in keys.keys():
        keys[i] = 1
        continue 
    keys[i] += 1
for i in A:
    combos = 0
    for k in keys.keys():
        num = keys[k]-1 if k == i else keys[k]
        combos += possibleCombos(num)
    print(combos)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = tuple(map(int, input().split()))
ans = 0
lim = max(a)
amount = [0]*lim
for i in range(lim):
    amount[i] = a.count(i+1)
    ans += amount[i]*(amount[i]-1)//2
for j in range(n):
    print(ans - amount[a[j]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = tuple(map(int, input().split()))
    ans = 0
    lim = max(a)
    amount = [0]*lim
    for i in range(lim):
        amount[i] = a.count(i+1)
        ans += amount[i]*(amount[i]-1)//2
    #print(ans)
    for j in range(n):
        ansout = ans
        #print(amount[a[j]-1])
        ansout -= amount[a[j]-1] -1
        print(ansout)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
lim = max(a)
amount = [0]*lim
for i in range(lim):
    amount[i] = a.count(i+1)
    ans += amount[i]*(amount[i]-1)//2
for j in range(n):
    ansout = ans
    ansout -= amount[a[j]-1] -1
    print(ansout)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
amount = [0]*n
preans = [0]*n

for i in range(n):
    amount[i] = a.count(i+1)
    preans[i] = amount[i]*(amount[i]-1)//2

ans = sum(preans)
#print(ans)
for j in range(n):
    ansout = ans
    #print(amount[a[j]-1])
    ansout -= amount[a[j]-1] -1
    print(ansout)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
amount = [0]*n

for i in range(n):
    amount[i] = a.count(i+1)

for j in range(n):
    ans = 0
    for k in range(max(a)):
        ans += amount[k]*(amount[k]-1)//2
    ans -= amount[a[j]-1] -1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
amount = [0]*n

for i in range(n):
    amount[i] = a.count(i+1)

for j in range(n):
    ans = 0
    for k in range(n):
        ans += amount[k]*(amount[k]-1)//2
    ans -= amount[a[j]-1] -1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
amount = [0]*n

for i in range(n):
    amount[i] = a.count(i+1)

for j in range(n):
    ans = 0
    for k in range(n):
        if a[j] == k+1:
            ans += (amount[k]-1)*(amount[k]-2)//2
        else:
            ans += amount[k]*(amount[k]-1)//2
    print(ans)

Submission status: Time Limit Exceeded

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

a_count = Counter(a_input)

score_li = [0]*len(a_count)
exclude_li = [0]*len(a_count)

for i,(k,v) in enumerate(a_count.items()):
    score_li[i]=v*(v-1)//2
    exclude_li[i]=(v-1)*(v-2)//2

score_sum = sum(score_li)

for i in range(n):
    for j,(k,v) in enumerate(a_count.items()):
        if a_input[i]==k:
            print(score_sum-score_li[j]+exclude_li[j])
            break

Submission status: Time Limit Exceeded

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

a_count = Counter(a_input)

score_li = [0]*len(a_count)
exclude_li = [0]*len(a_count)

for i in range(n):
    tmp=0
    for k,v in a_count.items():
        if a_input[i]==k:
            tmp+=(v-1)*(v-2)//2
        else:
            tmp+=v*(v-1)//2
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

for k in range(N):
    counter = 0
    l = Counter([A[i] for i in range(N) if i!=k])
    for v in l.values():
         counter += v*(v-1)//2
    print(counter)

Submission status: Time Limit Exceeded

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

for k in range(N):
    l = sorted([A[i] for i in range(N) if i!=k])
    counter = 0
    result = 0
    for i in range(len(l)):
        counter += 1
        if i!=len(l)-1:
            if l[i] != l[i+1]:
                #print(l[i],counter)
                result += counter*(counter-1)//2
                counter = 0
        else:
            #print(l[i],counter)
            result += counter*(counter-1)//2
    print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for k in range(N):
    l = sorted([A[i] for i in range(N) if i!=k])
    counter = 0
    result = 0
    for i in range(len(l)):
        counter += 1
        if i!=len(l)-1:
            if l[i] != l[i+1]:
                #print(l[i],counter)
                result += len(list(itertools.combinations(range(counter),2)))
                counter = 0
        else:
            #print(l[i],counter)
            result += len(list(itertools.combinations(range(counter),2)))
    print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


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


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

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

for k in range(N):
    t = A[k]
    h[t] -= 1

    ans = 0
    for a, n in h.items():
        if n >= 2:
            ans += nC2(n)
    print(ans)

    h[t] += 1

Submission status: Time Limit Exceeded

Solution code:
# D - Banned K

import sys

sys.setrecursionlimit(10 ** 9)

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

def f(i, ls, chosen):
    n1 = len(ls)
    n2 = len(chosen)
    if n1 <= i or n2 == 2:
        if n2 == 2 and chosen[0] == chosen[1]:
            return 1
        return 0
    return f(i + 1, ls, chosen) + f(i + 1, ls, chosen + [ls[i]])

for i in range(N):
    print(f(0, A[:i] + A[i + 1:], []))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

equal = 0
elements = collections.Counter(A)
for v in elements.values():
    equal += v * (v - 1) // 2

for i in range(n):
    A_tmp = A[: i] + A[i + 1: ]
    equal_i = A_tmp.count(A[i])
    print(equal - equal_i)

Submission status: Time Limit Exceeded

Solution code:
import math

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

equal = 0
elements = set(A)
for e in elements:
    tmp = A.count(e)
    equal += tmp * (tmp - 1) // 2

for i in range(n):
    A_tmp = A[: i] + A[i + 1: ]
    equal_i = A_tmp.count(A[i])
    print(equal - equal_i)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools

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

C = list(itertools.combinations(A, 2))
C = list(c for c in C if c[0] == c[1])
equal = len(C)

for i in range(n):
    A_tmp = A[: i] + A[i + 1: ]
    equal_i = A_tmp.count(A[i])
    print(equal - equal_i)

Submission status: Time Limit Exceeded

Solution code:
import math

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

for i in range(n):
    ans = 0
    A_tmp = A[: i] + A[i + 1: ]
    D = dict.fromkeys(A_tmp, 0)
    for k in D.keys():
        D[k] = A_tmp.count(k)
    for v in D.values():
        if v >= 2:
            ans += math.factorial(v) // (2 * math.factorial(v - 2))
    print(ans)

Submission status: Time Limit Exceeded

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

a_count = Counter(a_input)
for i in range(n):
    li = [(v-1)*(v-2)//2 if k==a_input[i] else v*(v-1)//2 for k,v in a_count.items()]
    print(sum(li))

Submission status: Time Limit Exceeded

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

a_count = Counter(a_input)
for i in range(n):
    li = [v-1 if k==a_input[i] else v for k,v in a_count.items()]
    len_li = len(li)
    tmp=0
    for l in li:
        tmp+=l*(l-1)//2
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import math


def combinations_count(n, r=2):
    return n * (n-1) // 2


def main(N, A):
    for i in range(N):
        copy_A = copy.copy(A)
        copy_A.pop(i)
        dic_copy_A = dict(collections.Counter(copy_A))

        count = 0
        for v in dic_copy_A.values():
            if v >= 2:
                count += combinations_count(v, 2)

        print(count)
        
        
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    main(N, A)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import math


def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))
  
def main2(N, A):
    dic_A = dict(collections.Counter(A))
        
    for i in range(N):
        copy_dic_A = copy.copy(dic_A)
        copy_dic_A[A[i]] -= 1

        count = 0
        for v in copy_dic_A.values():
            if v >= 2:
                count += combinations_count(v, 2)
        print(count)

        
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    main2(N, A)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
import math


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

def main(N, A):
    for i in range(N):
        copy_A = copy.copy(A)
        copy_A.pop(i)
        dic_copy_A = dict(collections.Counter(copy_A))

        count = 0
        for v in dic_copy_A.values():
            if v >= 2:
                count += combinations_count(v, 2)

        print(count)
        
        
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    main(N, A)

Submission status: Time Limit Exceeded

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

cntA={} # 同じ数字iが何個あるか
for a in A:
    if a in cntA.keys():
        cntA[a]+=1
    else:
        cntA[a]=1

cntn={} #同じ数字iから2種類取り出す組み合わせの数
for a in cntA.keys():
    cntn[a]=int(cntA[a]*(cntA[a]-1)/2)

ansn={}
tmpn=0
#k番目の数字iを除いた場合にiから2種類取り出す組み合わせの数
for i in cntA.keys(): 
    tmpn=int((cntA[i]-1)*(cntA[i]-2)/2)
    ansn[i]=sum(cntn.values())+tmpn-cntn[i]

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

Submission status: Time Limit Exceeded

Solution code:
import collections
import functools
import math

n = int(input())
l = list(map(int, input().split(" ")))
c = collections.Counter(l)
m ={}
def f(n , r):
    if (n, r) in m:
        return m[(n,r)]
    if (n < r):
        return 0
    tmp = math.factorial(n) // (math.factorial(n-r) * math.factorial(r))
    m[(n, r)] = tmp
    return tmp


total = functools.reduce(lambda x, y : x + f(y, 2), c.values(), 0)

for i in l:
    print(total - f(c.get(i, 0), 2) + f(c.get(i, 0) - 1, 2))

Submission status: Time Limit Exceeded

Solution code:
import collections
import functools
import math

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

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

total = functools.reduce(lambda x, y : x + f(y, 2), c.values(), 0)

for i in l:
    print(total - f(c.get(i, 0), 2) + f(c.get(i, 0) - 1 , 2))

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
bs = list(map(int, input().split()))
temp = []
ans = 0
for i in range(n):
    temp = list(bs)
    temp.pop(i)
    for k in dict.fromkeys(temp):
        if(temp.count(k) == 2):
            ans += 1
        if(temp.count(k) > 2):
            ans += math.factorial(temp.count(k))//(math.factorial(temp.count(k)-2)*2)
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

n = int(input())
li = list(map(int,input().split()))
c = list(collections.Counter(li).items())
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

if len(c) == 1:
    ans = [combinations_count(n-1, 2)]*n
    print(*ans)
    exit()

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])