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
import copy

n = int(input())
l = list(map(int, input().split()))
d = {}
s_sum = {}
s = list(set(l))
m = 0
for i in range(len(s)):
    d[s[i]] = l.count(s[i])
for v in d.values():
    if v > 1:
        m += math.factorial(v) // math.factorial(v -
                                                 2) // math.factorial(2)
for i in range(len(l)):
    if not l[i] in d:
        print(m)
    elif l[i] in s_sum:
        print(s_sum[l[i]])
    else:
        ans = 0
        e = copy.deepcopy(d)
        e[l[i]] -= 1
        for v in e.values():
            if v > 1:
                ans += math.factorial(v) // math.factorial(v -
                                                           2) // math.factorial(2)
        s_sum[l[i]] = ans
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


n = int(input())
A = list(map(int, input().split()))
c = Counter(A).most_common()
ans = 0
for i in range(len(c)):
    n = c[i][1]
    if n < 2:
        break
    ans += n * (n-1) // 2
for i in A:
    cnt = A.count(i)
    if cnt == 1:
        print(ans)
    else:
        ans_ = ans + (cnt-1)*(cnt-2) // 2 - cnt*(cnt-1)//2
        print(ans_)

Submission status: Time Limit Exceeded

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

n = int(input())
A = [int(i) for i in input().split()]
d = {}
for a in A:
    if a in d:
        d.update({a: d.get(a) + 1})
    else:
        d.update({a: 1})

dr = {}
for x in A:
    if x not in dr:
        res = 0
        for item in d:
            y = d.get(item)
            if item == x:
                res += (y-1)*(y-2) // 2
            else:
                res += y*(y-1) // 2
        dr.update({x: res})

    print(dr.get(x))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial


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


N = int(input())
A = list(map(int, input().split()))
l = Counter()
for i in range(N):
    l.update(str(A[i]))

gsum = 0
combD = {}
for key in l.keys():
    tmp = comb(l[key], 2)
    gsum += tmp
    combD[str(key)] = tmp - comb(l[key]-1, 2)

for i in range(N):
    print(gsum - combD[str(A[i])])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
C=Counter(A)
for i in range(n):
    D=C.copy()
    D[A[i]]-=1
    r=0
    for v in D.values():
        if v>=2:
            r+=v*(v-1)//2
    print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import math
n = int(input())
a = [int(x) for x in input().split()]
ans = []
m_dic = defaultdict(int)
for i in a:
    m_dic[i] += 1

for i in range(n):
    count = 0
    for key,value in m_dic.items():
        v = value
        if key == a[i]:
            v = value -1
        if v > 1:
            count += math.factorial(v) // (math.factorial(v - 2) * math.factorial(2))
    ans.append(count)
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
import math
for k in range(N):
    ans = 0
    B = [i for i in A]
    B.pop(k)
    C = list(set(B))
    for i in C:
        n = B.count(i)
        if n <= 1:
            ans += 0
        else:
            ans += (math.factorial(n)//(2*(math.factorial(n - 2))))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
lis = list(map(int,input().split()))

ans = {}
for i in lis:
    if i in ans:
        ans[i] += 1
    else:
        ans[i] = 1

for j in range(n):
    ans_t = copy.copy(ans)
    ans_t[lis[j]] -= 1
    su = 0
    for k in ans_t.values():
        su += (k - 1) * k // 2
    print(su)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
sorted_a=list(set(sorted(a)))
for i in range(n):
    y=a[i]
    print(sum([sum(range(a.count(x) - (1 if x==y else 0))) for x in sorted_a]))

Submission status: Time Limit Exceeded

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

import math
import collections

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

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))

    for i in range(n):
        li = a_li.copy()
        li.pop(i)
        li.sort()
        c = collections.Counter(li)
        ans = 0
        for k, v in c.items():
            if v > 1:
                ans += combinations_count(v, 2)

        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
listA = list(map(int,input().split()))
count = 0
for k in range(n):
  a = listA[k]
  listA.pop(k)
  #print(listA)
  #Aはset型で数の種類調べる
  A = list(set(listA))
  #print(A)
  for i in A:
    c = listA.count(i)
    #print(c)
    count += int(c*(c-1)/2)
  print(count)
  listA.insert(k,a)
  count = 0
  #print(listA)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = {}
for i in a:
  b[i] = a.count(i)*(a.count(i)-1)//2
m = sum(b.values())
for i in a:
  print(m - a.count(i) + 1)

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

aa = {}
for i in a:
  if i in aa:
    aa[i] += 1
  else:
    aa[i] = 1
 
for i in range(n):
  ans = 0
  aa[a[i]] -= 1
  for key in aa.keys():
    if aa[key] > 1:
      ans += comb(aa[key], 2)
  aa[a[i]] += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=list(map(int,input().split()))
freq=[]
pairs=0

for a in lst:
  freq.append(lst.count(a))

for a in set(lst):
  count=lst.count(a)
  pairs+=(count*(count-1)/2)

for a in freq:
  print(int(pairs-a+1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

import math

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

def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    hist = [0] * 2*pow(10, 5)
    for a in A:
        hist[a-1] += 1

    all = 0
    for h in hist:
        if h >= 2:
            all += combinations_count(h, 2)

    for a in A:
        h = hist[a-1]
        if h > 2:
            diff = combinations_count(h, 2) - combinations_count(h-1, 2)
            print(all - diff)
        elif h == 2:
            print(all - 1)
        else:
            print(all)
    
if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())
A = [int(v) for v in input().strip().split()]
C = {}
S = {}

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

for c in C.values():
    if c < 1:
        continue
    S.setdefault(c, c * (c - 1) // 2)
    if c < 2:
        continue
    S.setdefault(c - 1, (c - 1) * (c - 2) // 2)

for i, a in enumerate(A):
    C[a] -= 1
    sum_ = sum([S[n] for n in C.values() if n > 1])
    C[a] += 1
    print(sum_)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections

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

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

master = {}
min1 = {}

for i in list(ax.keys()):
    master[i] = count(ax[i], 2)
    min1[i] = count(ax[i]-1, 2)

for i in a:
    ans = 0
    for k in list(ax.keys()):
        ans += master[k] if(i!=k) else min1[k]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
ai=[int(i) for i in input().split()]

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result

for i in range(len(ai)):
    b=sorted(ai[:i]+ai[i+1:])
    c=[b.count(x) for x in set(b) if b.count(x) > 1]
    ans=[cmb(x,2) for x in c]
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np

def main():
    n = int(input())
    As = list(map(int, input().split(" ")))
    for k in range(n):
        if k == 0:
            As_tmp = As[1:]
        else:
            As_tmp = As[:k] + As[k+1:]
        a = list(itertools.combinations(As_tmp, 2))
        b = list(map(set, a))
        b = np.array(list(map(len, b))) - 2
        #print(a)
        #print(b)
        ans = - np.sum(b)
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math


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

n = int(input())
a = list(map(int, input().split()))
cnt = {}
for v in a:
    if v not in cnt:
        cnt[v] = 0
    cnt[v] += 1
ans = {}
for key1 in cnt.keys():
    ans[key1] = sum(nc2(value) if key1 != key2 else nc2(value-1) for key2, value in cnt.items())
for v in a:
    print(ans[v])

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce

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

numList = [0] * (2*(10**5))
numSet = set()

for i in range(len(a)):
    numList[a[i]] += 1
    if(numList[a[i]]==2):
        numSet.add(a[i])

for i in range(len(a)):
    s = 0
    for j in numSet:
        if(j == a[i]):
            if(numList[j]>2):
                s += ncr(numList[j]-1,2)
        else:
            s += ncr(numList[j],2)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
import collections

MEMO = {}
def fact(n):
    if MEMO.get(n):
        return MEMO[n]
    r = 2
    if n - r < 0:
        return 0
    MEMO[n] = factorial(n) // factorial(r) // factorial(n - r)
    return MEMO[n]

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

counts = collections.Counter(BALLS)
for i in range(N):
    print(sum(fact(v if k != BALLS[i] else v - 1) for (k, v) in counts.items()))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [0]
a.extend(list(map(int, input().split())))
max_a = max(a)
counter = [0]*(max_a+1)
combi = [0]*(max_a+1)
for num in a:
    counter[num] += 1
# print(counter)
for i in range(1, max_a+1):
    if counter[i] >= 2:
        for j in range(max_a+1):
            if i != j:
                combi[j] += counter[i]*(counter[i]-1)//2
            else:
                combi[j] += (counter[i]-1)*(counter[i]-2)//2
for j in range(1,n+1):
    print(combi[a[j]])

Submission status: Time Limit Exceeded

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

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

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

nums_not_1 = {}
for key in nums:
    if nums[key] > 1:
        nums_not_1[key] = nums[key]

for k in range(N):
    ans = 0

    for key in nums_not_1:
        num = nums_not_1[key]
        if A[k] == key:
            num -= 1
        ans += cmb2(num)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    # initialize
    N = int(input())
    nums = [int(n) for n in input().split(' ')]

    hash_table = {}
    for n in nums:
        if n in hash_table.keys():
            hash_table[n] += 1
        else:
            hash_table[n] = 1

    for n in nums:
        ans = 0
        for k, v in hash_table.items():
            if k == n:
                v -= 1
            ans += v * (v - 1) / 2 if v > 1 else 0
        print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

As = np.asarray(input().split())

numbers = {}

for i in range(n):
    if As[i] in numbers:
        numbers[As[i]] += 1
    else:
        numbers[As[i]] = 1

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

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

for i in range(n):
    print(full_ans - (numbers[As[i]] - 1))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for k in range(n):
    c=0
    b=a.copy()
    a.pop(k)
    for i in list(set(a)):
        c+=(a.count(i)*(a.count(i)-1))/2
    print(int(c))
    a=b

Submission status: Time Limit Exceeded

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

N = int(input())
A = input().split()

for x in A:
    a = A[:]
    a.remove(x)
    ans = 0
    for y in set(a):
        m = a.count(y)
        ans += m * (m - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


class Memoize:
    def __init__(self, fn):
        self.fn = fn
        self.memo = {}

    def __call__(self, c, x):
        if x not in self.memo:
            self.memo[x] = self.fn(c, x)
        return self.memo[x]


@Memoize
def cal(c, x):
    s = 0
    for i in c:
        if i == x:
            s += combinations_count(c[i]-1)
        else:
            s += combinations_count(c[i])
    return s


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


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    for i in range(n):
        print(cal(c, a[i]))

Submission status: Time Limit Exceeded

Solution code:
import copy
import itertools

n = int(input())
input_line = [int(i) for i in input().split()]

for i in range(n):
    count = 0
    input_copy = copy.copy(input_line)
    input_copy.pop(i)
    for j in list(itertools.combinations(input_copy, 2)):
        if j[0] == j[1]:
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
n = int(input())
a = np.array(list(map(int, input().split())))
for i in range(n):
    np.delete(a, i)
    _a = np.delete(a, i)
    unique = np.unique(_a)
    for j in range(len(unique)):
        k = np.count_nonzero(_a == unique[j])
        if j == 0:
            if k >= 2:
                total = k * (k - 1) / 2 
            else:
                total = 0
        else:
            if k >= 2:
                total += k * (k - 1) / 2 
    print(int(total))

Submission status: Time Limit Exceeded

Solution code:
import collections

n=int(input())
As=list(map(int,input().split()))
ls=collections.Counter(As)
for i in As:
  ls[i]=ls[i]-1
  l=ls.values()
  ans=0
  for k in l:
    ans+=k*(k-1)//2
  ls[i]=ls[i]+1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
book = {}
array = [int(i) for i in input().split()]

for i in array:
    book[i] = book.get(i, 0) + 1

for i in range(n):
    s = 0
    for k, v in book.items():
        if k == array[i]:
            v -= 1
        s += (v * (v - 1)) // 2 if v > 0 else 0
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math


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


N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
cnt = 0
for i in A:
    for x, y in c.items():
        if i == x and y >= 3:
            cnt += combinations_count(y - 1, 2)
        elif i != x and y >= 2:
            cnt += combinations_count(y, 2)
    print(cnt)
    cnt = 0

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

from copy import deepcopy
import math
import collections
import sys


def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))


def I():
    return int(sys.stdin.buffer.readline())


def LS():
    return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()


def S():
    return sys.stdin.buffer.readline().rstrip().decode('utf-8')


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


def SRL(n):
    return [list(S()) for i in range(n)]


def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]

# ----------------------------------------------------------------


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


def main():
    n = I()
    ball = LI()

    for i in range(n):
        ans = 0
        ball_temp = deepcopy(ball)
        ball_temp.pop(i)
        c = collections.Counter(ball_temp)
        c_nums = c.values()

        for p in c_nums:
            if p >= 2:
                ans += combinations_count(p, 2)

        print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(a.count(i) for i in range(1, n + 1))


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


for k in range(n):
    i = a[k] - 1
    ans = c2(b[i] - 1)
    for j in range(n):
        if i != j:
            ans += c2(b[j])
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    ans=0
    k=A[:i]+A[i+1:]
    sk=set(k)
    ck=col.Counter(k)
    for i in sk:
        if ck[i]==1:
            continue
        else:
            ans += (ck[i]*(ck[i]-1))//2
    print(ans)

Submission status: Time Limit Exceeded

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

#print(N,A)

for i in range(N):
  d = {}
  for j,a in enumerate(A):
    if j != i:
      d[a] = d.get(a, 0) + 1
  sum = 0
  for v in d.values():
    sum += int(v*(v-1)/2)
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())

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

count_array = np.zeros(len(a_list))
for i in range(len(a_list)):
    count_array[i] += a_list.count(i+1)
for num in a_list:
    temp_array = count_array.copy()
    temp_array[num-1] -= 1
    ans =int((np.sum(temp_array * (temp_array-1)))/2)
    print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict

cnt = defaultdict(int)
for i in range(n):
    cnt[a[i]] += 1
# print(cnt)

for key, val in list(cnt.items()):
    if val < 2:
        cnt.pop(key)
# print(cnt)

for i in range(n):
    flag = False
    if a[i] in cnt:
        cnt[a[i]] -= 1
        flag = True
    # print(cnt)
    ans = 0
    for val in cnt.values():
        if val >= 2:
            ans += val*(val-1)//2
    print(ans)
    if flag:
        cnt[a[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
def cmb(n,r):
    return factorial(n) // factorial(r) // factorial(n - r)

n=ii()
arr=limii()

cou=Counter(arr)
ans=0
for i,j in cou.items():
    if j>=2:
        ans+=cmb(j,2)

for i in arr:
    if cou[i]>2:
        a=0
        print(ans-cmb(cou[i],2)+cmb(cou[i]-1,2))
    elif cou[i]==2:
        print(ans-1)
    else:
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
x=list(map(int,input().split()))
dic={}
for i in range(n):
  if x[i] in dic.keys():
    dic[x[i]]+=1
  else:
    dic[x[i]]=1
s=0
for i in dic.keys():
  s+=dic[i]*(dic[i]-1)//2
for i in range(n):
  print(s-dic[x[i]]+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(map(int, input().split()))
B = set(A)
d = dict()

al = dict()
dl = dict()
el = dict()
ALL = 0
for b in B:
    k = A.count(b)
    d[b] = k
    al[b] = k * (k - 1) // 2
    ALL += al[b]
    dl[b] = (k - 1) * (k - 2) // 2

kl = dict()
for b in B:
    kl[b] = ALL - al[b] + dl[b]

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
setA = list(set(A))
countA = [A.count(a) for a in setA]

for i in range(N):
  ans = 0
  index = setA.index(A[i])

  for i in range(len(setA)):
    if i == index:
      c = countA[i] - 1
    else:
      c = countA[i]
    if c > 1:
      ans += c * (c-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)

sum = 0

for a in A:
    for key in c.keys():
        if key == a:
            value_k = c[key] - 1
            sum += value_k * (value_k - 1) // 2
        else:
            value_k = c[key]
            sum += value_k * (value_k - 1) // 2
    print(sum)
    sum = 0

Submission status: Time Limit Exceeded

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

for i in range(N):
  cnt = 0
  for j in range(N):
    for k in range(N):
      if i!=j and i!=k and j!=k and l[j]==l[k]:
        cnt += 1
  print(int(cnt/2))

Submission status: Time Limit Exceeded

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
arrA = list(map(int,input().split()))

ctA = Counter(arrA)
ans = []
for i in arrA:
    ctA[i] -= 1
    temp = 0
    for k,v in ctA.items():
        if v>1:
            temp += v*(v-1)//2
    print(temp)
    ctA[i] += 1

# print(*ans,sep="\n")
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
res = list(map(int,input().split()))
sums = 0
ans = []
a = Counter(res)
for i in range(len(res)):
    a[res[i]] -= 1
    sums = 0
    for j in a.values():
        sums += j * (j - 1) // 2
    ans.append(sums)
    a[res[i]] += 1
for k in ans:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
import sys

n = int(sys.stdin.readline().strip())
nums = list(map(int, sys.stdin.readline().strip().split()))

d = dict()
for one in nums:
    if one in d:
        d[one] += 1
    else:
        d[one] = 1

def C(a):
    return (a*(a-1)//2 )

res_history = dict()

for one in nums:
    if one in res_history:
        print(res_history[one])
    else:
        d[one] -= 1
        res = 0
        for k,v in d.items():
            if v==2:
                res += 1
            elif v > 2:
                res += C(v)
        print (res)
        d[one] += 1
        res_history[one] = res

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    List = np.unique(A)
    count = np.bincount(A)
    # print(count)

    dic = {}
    Ans = 0
    for l in List:
        n = count[l]
        num = n * (n - 1) // 2
        Ans += num
        dic[l] = num
    B = np.empty(N, dtype=np.int)
    for l in List:
        n = count[l] - 1
        num = n * (n - 1) // 2
        B = np.where(A == l, int(Ans - dic[l] + num), B)
    print(*B, sep="\n")


main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
val = list(c.values())
key = list(c.keys())
for i in range(n):
    ind = key.index(a[i])
    val[ind] -= 1
    count = 0
    for j in val:
        if j < 2:
            pass
        else:   
            count += combinations_count(j,2)
    print(count)
    val[ind] += 1

Submission status: Time Limit Exceeded

Solution code:
import copy

import math


def main():
    import collections

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

    for k in range(N):
        tmp_A = ignore(A, k)
        tmp_A = collections.Counter(tmp_A)
        su = 0
        for c in tmp_A:
            pair = tmp_A[c]
            if pair >= 2:
                su += combinations_count(pair, 2)
        print(su)


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


def ignore(A, i):
    x = copy.deepcopy(A)
    del x[i]
    return x


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
def input_int_list():
    return [int(thing) for thing in input().split(" ")]

nCr = {}

def nC2(n):
    if n in nCr:
        return nCr[n]
    else:
        val=n*(n-1)//2
        nCr[n]=val
        return val



N=int(input())
A_list=input_int_list()
int_count=collections.Counter(A_list)
for i in range(N):
    number=A_list[i]
    int_count[number]=int_count[number]-1
    print(sum([nC2(int_count[i]) for i in int_count]))
    int_count[number]=int_count[number]+1

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
def ncr(n, r):
    return factorial(n) // factorial(r) // factorial(n - r)


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

numList = [0] * (2*(10**5))
numSet = set()

for i in range(len(a)):
    numList[a[i]] += 1
    if(numList[a[i]]==2):
        numSet.add(a[i])

for i in range(len(a)):
    s = 0
    for j in numSet:
        if(j == a[i]):
            if(numList[j]>2):
                s += ncr(numList[j]-1,2)
        else:
            s += ncr(numList[j],2)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [0]
a.extend(list(map(int, input().split())))
max_a = max(a)
counter = [0]*(max_a+1)
combi = [0]*(max_a+1)
for num in a:
    counter[num] += 1
# print(counter)
for i in range(1, max_a+1):
    if counter[i] != 0:
        for j in range(max_a+1):
            if i != j:
                combi[j] += counter[i]*(counter[i]-1)//2
            else:
                combi[j] += (counter[i]-1)*(counter[i]-2)//2
for j in range(1,n+1):
    print(combi[a[j]])

Submission status: Time Limit Exceeded

Solution code:

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


for i in range(n):
	li = l.copy()
	li.pop(i)

	lis = list(set(li))
	co = 0
	for ii in lis:
		m = li.count(ii)
		co += (m * (m - 1) // 2)

	print(co)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
inp = list(map(lambda x: int(x), input().split(" ")))

counts = {}
for i in range(N):
    counts[inp[i]] = 0
for i in range(N):
    counts[inp[i]] += 1

ret = {}

for i in range(N):
    ret[inp[i]] = -1

for i in range(N):
    if (ret[inp[i]] != -1):
        print(ret[inp[i]])
    else:
        sum1 = 0
        for k in counts.keys():
            if (k==inp[i]):
                sum1 += (counts[k]-1)*(counts[k]-2)//2
            else:
                sum1 += (counts[k])*(counts[k]-1)//2
        ret[inp[i]] = sum1
        print(sum1)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
exact_counter = {}
for v, n in counter.items():
    if n != 1:
        exact_counter[v] = n

for k in range(N):
    a = A[k]
    ans = 0
    for v, n in exact_counter.items():
        if a == v:
            n -= 1
        ans += n * (n - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

a=[int(i) for i in input().split(" ")]

k=[0]*max(a)
for i in a:
    k[i-1]+=1

list=[]
for j in range(len(k)):
    if not j+1 in a:
        list.append(0)
        continue
    l=[i for i in k]
    l[j]-=1
    list.append(sum([i*(i-1)//2 for i in l]))

for i in a:
    print(list[i-1])

Submission status: Time Limit Exceeded

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

a_dict = dict()
for x in a:
    if x not in a_dict:
        a_dict[x] = 1
    else:
        a_dict[x] += 1

sum = 0

for i in a:
    for k, v in a_dict.items():
        if k == i:
            v -= 1
        if v > 1:
            sum += v*(v-1)//2

    print(sum)
    sum = 0

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
ai=[int(i) for i in input().split()]

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

for i in range(len(ai)):
    b=sorted(ai[:i]+ai[i+1:])
    c=[b.count(x) for x in set(b) if b.count(x) > 1]
    ans=[cmb(x,2) for x in c]
    print(sum(ans))