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

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

for i in range(n):
    dic[a[i]]+=1

for i in range(n):
    count=0
    q = dic.copy()
    q[a[i]]-=1
    for k,v in q.items():
        count += v*(v-1)/2
    
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:

import collections
import math

def ncr(n, r):
    if mem[n][r]:
        return mem[n][r]
    mem[n][r] = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    return mem[n][r]
n = int(input())
mem = [[0 for _ in range(n+1)]for _ in range(n+1)]
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 sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import Counter 
def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    d = Counter(A)
    for i in A:
        ans = 0
        for key, val in d.items():
            if i == key:
                val -= 1
            ans += val*(val-1)//2 
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)
B.append(0)
p = B[0]
y = dict()
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        y[p] = u
        u = 0
    u += 1
    p = q
for j in range(N):
    g = 0
    x = A[j]
    y[x] -= 1
    for l in y.values():
        g += l*(l-1)//2
    print(g)
    y[x] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)
B.append(0)
p = B[0]
y = dict()
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        y[p] = u
        u=0
    u += 1
    p = q
for j in range(N):
    g = 0
    x = A[j]
    y[x] -= 1
    va = list(y.values())
    for l in va:
        g += l*(l-1)//2
    print(g)
    y[x] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
d = dict()
for o in range(N):
    d[o] = A[o]
B = sorted(A)
B.append(0)
p = B[0]
y = dict()
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        y[p] = u
        u=0
    u += 1
    p = q
for j in range(N):
    g = 0
    x = d[j]
    y[x] -= 1
    va = list(y.values())
    for l in va:
        g += l*(l-1)//2
    print(g)
    y[x] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = [i for i in range(N)]
d = dict(zip(C, A))
B = sorted(A)
B.append(0)
p = B[0]
y = dict()
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        y[p] = u
        u=0
    u += 1
    p = q
for j in range(N):
    g = 0
    x = d[j]
    y[x] -= 1
    va = list(y.values())
    for l in va:
        g += l*(l-1)//2
    print(g)
    y[x] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = [i for i in range(N)]
d = dict(zip(C, A))
B = sorted(A)
B.append(0)
p = B[0]
nm = []
nn = []
u = 0
for k in range(N+1):
    q = B[k]
    if p != q:
        nm.append(p)
        nn.append(u)
        u=0
    u += 1
    p = q
y = dict(zip(nm, nn))
P = len(nm)
for j in range(N):
    g = 0
    x = d[j]
    y[x] -= 1
    va = list(y.values())
    for l in range(P):
        v = va[l]
        g += v*(v-1)//2
    print(g)
    y[x] += 1

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

a2 = sorted(a)
a2.append(n+1)
dic = {}
n = 0
cnt = 1
total = 0
for i in a2:
    if n != i:
        if cnt > 1:
            dic[str(n)] = cnt
            total += combi(cnt)
        n = i
        cnt = 1
    else:
        cnt += 1

for i in a:
    if str(i) in dic:
        cnt = dic[str(i)]
        print(total - combi(cnt) + combi(cnt-1))
    else:
        print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
lia = [0]*n
lib = [0]*n
for i in range(n):
    lia[li[i]-1] += 1
for i in range(n):
    if lia[i] >= 2:
        lia[i] -= 1
        lib[i] += (lia[i]*(lia[i]+1)//2)
for i in range(n):
    a = lib[li[i]-1]
    lib[li[i]-1] = (lia[li[i]-1]-1)*(lia[li[i]-1])//2
    print(sum(lib))
    lib[li[i]-1] = a

Submission status: Time Limit Exceeded

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

ans = {}
key = list(C.keys())
val = list(C.values())

for i in range(len(key)):
    ans[key[i]] = val[i] * (val[i]-1) //2

ans_val = [int(_) for _ in list(ans.values())]

for j in range(N):
    print(sum(ans_val) - (C[A[j]] - 1) )

Submission status: Time Limit Exceeded

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

ans = {}
key = list(C.keys())
val = list(C.values())

for i in range(len(key)):
    ans[key[i]] = val[i] * (val[i]-1) //2

ans_val = [int(_) for _ in list(ans.values())]

for j in range(N):
    print(sum(ans_val) - (C[A[j]] - 1) )

Submission status: Time Limit Exceeded

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

ans = {}
key = C.keys()
val = C.values()
key = list(key)
val = list(val)

for i in range(len(key)):
    ans[key[i]] = val[i] * (val[i]-1) //2

ans_key = [ans.keys()]
ans_val = list(ans.values())
ans_val = [int(_) for _ in ans_val]

for j in range(N):
    print(sum(ans_val) - (C[A[j]] - 1) )

Submission status: Time Limit Exceeded

Solution code:
N = int(input()) #N: ボールの個数

A = list(map(int, input().split()))
count_A = [0]*(N+1)
count_pattern = [0]*(N+1)

for i in A:
    if count_A[i]==0:
        a = A.count(i)
        count_A[i] = a
        count_pattern[i] = (a*(a-1)//2)

max = sum(count_pattern)

for i in range(N):
    a = A[i]
    b = count_A[a]
    print(max - (b-1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
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()))

c = Counter(a)
s = 0

for i in c.keys():
    n = c[i]
    if n >= 2:
        s += comb(n, 2)

for i in a:
    n = c[i]
    if 1 <= n-1 < 2:
        ans = s - comb(n, 2)
    elif n-1 >= 2:
        ans = s - comb(n, 2) + comb(n-1, 2)
    else:
        ans = s


    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

for i in A:
    ans = 0
    for key in c:
        if key == i:
            tmp = c[key] - 1
            ans += tmp*(tmp-1) // 2
        else:
            ans += c[key]*(c[key]-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A=list(map(int,input().split()))
#print(A)
B={}
for i in A:
    #print(i)
    if i in B:
        B[i]=B[i]+1
        #print(B)
    else:
        B[i]=1
        #print(B)

for i in A:
    tmp=0
    for k,v in B.items():
        if i==k:
            tmp+=int(0.5*(v-1)*(v-2))
            
        else:
            tmp+=int(0.5*(v)*(v-1))

    print(tmp)

Submission status: Time Limit Exceeded

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

for i in range(N):
    count=0
    for j in range(N):
        
        for k in range(j,N):
            if i==j:
                continue
            elif j==k or i==k:
                continue
            #print(A[j],A[k])
            if A[j]==A[k]:
                count+=1
            else:
                pass
    print(count)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
dict = defaultdict(int)
for a in A:
    dict[a] += 1

ans = 0
for key, value in list(dict.items()):
    ans += value*(value-1) // 2

for a in A:
    tmp = ans - dict[a]*(dict[a]-1)//2 + (dict[a]-1)*(dict[a]-2)//2
    print(tmp)
"""


class HashMap:
    # 997は1000より小さい素数というだけで特にそれ以上の理由はない
    def __init__(self, mod=997):
        self.mod = mod
        self._hashmap = [None for _ in range(self.mod)]  # 同じキーが来たらvalueを上書きする

    def __del__(self):
        del self._hashmap
        #print('HashMap object deleted')

    def _calculateHash(self, key):
        key = str(key)
        if key.isalnum():
            if key.isdecimal():
                hash = int(key) % self.mod
            else:
                newKey = ''
                for k in key:
                    if k.isdecimal():
                        newKey += k
                    else:
                        newKey += str(ord(k))
                hash = int(newKey) % self.mod
        else:
            # keyに英数字以外が含まれていた時
            raise Exception(
                'KeyError: Key must be str of alphabets or integer')
        return hash

    def add(self, key, value):
        # hashmapにkey, valueを追加する
        hash = self._calculateHash(key)
        if self._hashmap[hash] is None:
            self._hashmap[hash] = [[key, value]]
        else:
            for i, l in enumerate(self._hashmap[hash]):
                if l[0] == key:
                    self._hashmap[hash][i] = [key, value]
                    #print('same key indicated, value overwritten')
                    break
            self._hashmap[hash].append([key, value])

    def getValue(self, key):
        hash = self._calculateHash(key)
        if self._hashmap[hash] is None:
            raise Exception('KeyError: indicated Key is not be added')
        else:
            for l in self._hashmap[hash]:
                if l[0] == key:
                    return l[1]


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

dict = HashMap()
for a in A:
    try:
        tmp = dict.getValue(a)
        dict.add(a, tmp+1)
    except:
        dict.add(a, 1)

ans = 0
for a in list(set(A)):
    ans += dict.getValue(a)*(dict.getValue(a)-1) // 2

for a in A:
    dict_a = dict.getValue(a)
    tmp = ans - dict_a*(dict_a-1)//2 + (dict_a-1)*(dict_a-2)//2
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
import itertools as iter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    nums = [A.count(i) for i in range(N+1)]
    comb = sum((i*(i-1)//2 for i in nums))
    for k in range(N):
        print(comb-(nums[A[k]]-1))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools as iter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    nums = [0] * (N+1)
    for i in A:
        nums[i] += 1
    
    comb = [0] * (N+1)
    for i in range(N+1):
        comb[i] = len(list(iter.combinations(range(nums[i]), 2)))
    
    for k in range(1, N+1):
        c = comb[:]
        i = A[k-1]
        #print("k",k, "/ num", i)
        c[i] = len(list(iter.combinations(range(nums[i]-1), 2)))
        print(sum(c))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    check=[0]*n
    for i in l:
        check[i-1]+=1
    for j in l:
        cnt=0
        check[j-1]-=1
        for k in check:
            if k>=2:
                cnt+=k*(k-1)//2
        print(cnt)
        check[j-1]+=1
resolve()

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools
def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = len(list(itertools.combinations([i for i in range(c)],2)))
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools
def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = len(list(itertools.combinations([i for i in range(c)],2)))
    sum1 = sum(comb)
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools
def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        comb[i] = len(list(itertools.combinations([i for i in range(c)],2)))
    print(sum(comb))

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            comb[A[i-1]] += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            comb[A[i]] -= n-1
        else:
            comb[A[i]] = 0
        counter[A[i]] -= 1
        print(sum(comb))

main()

Submission status: Time Limit Exceeded

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

    for i in range(N):
        coll = collections.Counter(A)
        coll[A[i]] -= 1
        ans = 0
        for j in range(1,N+1):
            c = coll[j]
            if c>1:
                ans += len(list(itertools.combinations([i for i in range(c)],2)))
        print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
m = {0: 0, 1: 0}


def comb(n):
    if n in m:
        return m[n]

    res = n * (n - 1) // 2
    m[n] = res
    return res


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

memo = {}

for i, _a in enumerate(a):
    if not _a in memo:
        memo[_a] = [1, None]
    else:
        memo[_a][0] += 1

for i, v in memo.items():
    p = comb(v[0])
    q = (p * (v[0] - 2)) // v[0]

    if p == 0:
        q = 0

    v[1] = (p, q)

for _a in a:
    sum = 0

    for i, v in memo.items():
        if i == _a:
            sum += v[1][1]
        else:
            sum += v[1][0]

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

m = {0: 0, 1: 0}


def comb(n):
    if n in m:
        return m[n]

    res = math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
    m[n] = res
    return res


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

memo = {}

for i, _a in enumerate(a):
    if not _a in memo:
        memo[_a] = [1, None]
    else:
        memo[_a][0] += 1

for i, v in memo.items():
    p = comb(v[0])
    q = (p * (v[0] - 2)) // v[0]

    if p == 0:
        q = 0

    v[1] = (p, q)

for _a in a:
    sum = 0

    for i, v in memo.items():
        if i == _a:
            sum += v[1][1]
        else:
            sum += v[1][0]

    print(sum)

Submission status: Time Limit Exceeded

Solution code:
import math

m = {0: 0, 1: 0}


def comb(n):
    if n in m:
        return m[n]

    res = math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
    m[n] = res
    return res


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

memo = {}

for i, _a in enumerate(a):
    if not _a in memo:
        memo[_a] = 1
    else:
        memo[_a] += 1

for i, _a in enumerate(a):
    memo[_a] -= 1

    sum = 0
    for i, v in memo.items():
        sum += comb(v)
    print(sum)

    memo[_a] += 1

Submission status: Time Limit Exceeded

Solution code:
import math


def comb(n, r):
    if n == 1 or n == 0:
        return 0

    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

memo = {}

for i, _a in enumerate(a):
    if not _a in memo:
        memo[_a] = 1
    else:
        memo[_a] += 1

for i, _a in enumerate(a):
    memo[_a] -= 1

    sum = 0
    for i, v in memo.items():
        sum += comb(v, 2)
    print(sum)

    memo[_a] += 1

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
a_list = list(map(int,input().split()))
same = 0
for a,b in combinations(a_list,2):
    if a == b:
        same += 1
for i in range(n):
    print(same - a_list.count(a_list[i])+1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
d = {}
for i in l:
    d[i]=l.count(i)
cp = 0
for i in d.values():
    cp+= i*(i-1)//2
for i in l:
    print(cp-d[i]+1)

Submission status: Time Limit Exceeded

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

a_set = set(a)
total = 0
cnt = 0

for i in a_set:
    cnt = a.count(i)
    total += cnt*(cnt-1)//2

tmp = 0
for i in a:
    tmp = a.count(i) - 1
    print(total - tmp)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().rstrip().split()))
for i in a:
    a.remove(i)
    d = 0
    for t in range(0,len(a)):
        for j in range(t+1,len(a)):
            if a[t] == a[j]:
                d += 1
    print(d)
    a.insert(0,i)

Submission status: Time Limit Exceeded

Solution code:
#D
n = int(input())
a = [int(_) for _ in input().split()]
for k in range(len(a)):
    ans = 0
    popnum = a[0]
    del a[0]
    for i in range(len(list(set(a)))):
        #print(a.count(list(set(a))[i]))
        tmp = a.count(list(set(a))[i])
        if tmp < 2:
            pass
        else:
            ans += tmp * (tmp - 1) / 2
    print(int(ans))
    a.append(popnum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
for k in range(len(a)):
    ans = 0
    popnum = a[0]
    del a[0]
    for i in range(len(list(set(a)))):
        #print(a.count(list(set(a))[i]))
        tmp = a.count(list(set(a))[i])
        if tmp < 2:
            pass
        else:
            ans += tmp * (tmp - 1) / 2
    print(int(ans))
    a.append(popnum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
for k in range(len(a)):
    ans = 0
    popnum = a.pop(0)
    for i in range(len(list(set(a)))):
        #print(a.count(list(set(a))[i]))
        if a.count(list(set(a))[i]) < 2:
            pass
        else:
            ans += a.count(list(set(a))[i]) * (a.count(list(set(a))[i]) - 1) / 2
    print(int(ans))
    a.append(popnum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
counter_comb = list(map(lambda y: int(y*(y-1)/2), counter.values()))

for i in range(n):
    z = counter[A[i]]
    if z>0:
        print(sum(counter_comb)-(z-1))
    else:
        print(sum(counter_comb))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
counter = [0]*n
for i in range(n):
    counter[A[i]-1] += 1
 
counter_comb = list(map(lambda y: int(y*(y-1)/2), counter))
 
for i in range(n):
    z = counter[A[i]-1]
    if z>0:
        print(sum(counter_comb)-(z-1))
    else:
        print(sum(counter_comb))

Submission status: Time Limit Exceeded

Solution code:
def nCr(n, r): 
  
    return (fact(n) / (fact(r)  
                * fact(n - r))) 
  
# Returns factorial of n 
def fact(n): 
  
    res = 1
      
    for i in range(2, n+1): 
        res = res * i 
          
    return res
n=int(input())
a=list(map(int,input().split()))    
b=[0]*(n+1)
ans=0
c=[]
d=[0]*(n+1)
for i in a:
    b[i]+=1
for j in a:
    if j not in c:
        c.append(j)
        b[j]-=1
        for k in b:
            if k>=2:
                ans+=int(nCr(k,2))
        b[j]+=1
        print(ans)
        d[j]+=ans
        ans=0
    else:
        print(d[j])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
counter = [0]*n
for i in range(n):
    counter[A[i]-1] += 1

counter_comb = list(map(lambda y: int(y*(y-1)/2), counter))

for i in range(n):
    z = counter[A[i]-1]
    counter_copy = counter_comb.copy()
    if z>0:
        counter_copy[A[i]-1] -= z-1
    print(sum(counter_copy))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
counter = [0]*n
for i in range(n):
    counter[A[i]-1] += 1

for i in range(n):
    counter_copy = counter.copy()
    if counter_copy[A[i]-1]>1:
        counter_copy[A[i]-1] -= 1
    x = list(map(lambda y: int(y*(y-1)/2), counter_copy))
    print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

# def comb(n):
#     return int(n*(n-1)/2)

def main():
    N = I()
    A_list = LI()
    A_ctr = collections.Counter(A_list)
    for i in range(1,N+1):
        ans = 0
        for k,v in A_ctr.items():
            if A_list[i-1]==k:
                # ans += comb(v-1)
                n = v-1
                ans += int(n*(n-1)/2)
            else:
                n = v
                ans += int(n*(n-1)/2)
        else:
            print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def comb(n):
    return int(n*(n-1)/2)

def main():
    N = I()
    A_list = LI()
    A_ctr = collections.Counter(A_list)
    for i in range(1,N+1):
        ans = 0
        for k,v in A_ctr.items():
            if A_list[i-1]==k:
                ans += comb(v-1)
            else:
                ans += comb(v)
        else:
            print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
def cmb(n):
    return n * (n-1) / 2

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

#dic1はその要素を含む、dic2はその要素を含まない解
dic1 = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    if not a[i] in dic1:
        ball_n = a.count(a[i])
        if ball_n >= 2:
            ans1 = cmb(ball_n)
            dic1[a[i]] = ans1


sum = sum(dic1.values())
for i in range(n):
    ball_n = a.count(a[i])
    c = 0
    b = 0
    if a[i] in dic1:
        b = dic1[a[i]]
        c = int(dic1[a[i]] * (ball_n-2) / ball_n)
    ans = int(sum - b + c)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def nCr(n, r): 
  
    return (fact(n) / (fact(r)  
                * fact(n - r))) 
  
# Returns factorial of n 
def fact(n): 
  
    res = 1
      
    for i in range(2, n+1): 
        res = res * i 
          
    return res
n=int(input())
a=list(map(int,input().split()))    
b=[0]*(n+1)
ans=0
for i in a:
    b[i]+=1
for j in a:
    b[j]-=1
    for k in b:
        if k>=2:
            ans+=int(nCr(k,2))
    b[j]+=1
    print(ans)
    ans=0

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def comb(n):
    return int(n*(n-1)/2)

def main():
    N = I()
    A_list = LI()
    A_ctr = collections.Counter(A_list)
    for i in range(1,N+1):
        tmp_A_ctr = A_ctr.copy()
        tmp_A_ctr[A_list[i-1]] -= 1
        ans = 0
        for v in tmp_A_ctr.values():
            ans += comb(v)
        else:
            print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
A = list(map(int,input().split()))
B = collections.Counter(A)
C = B.copy()
for i in A:
    B[i] -= 1
    total = 0
    for j in B.values():
        total += j*(j-1)//2
    B = C.copy()
    print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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


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


for a in A:
    d[a] += 1

for i in range(N):
    ans = []
    tmp = d.copy()
    tmp[A[i]] -= 1
    tmp = list(tmp.values())
    for t in tmp:
        if memo[t] == 0:
            memo[t] = combi(t)
        ans.append(memo[t])
    print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache
from collections import defaultdict

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


@lru_cache(maxsize=None)
def combi(n):
    return n * (n-1) // 2


for a in A:
    d[a] += 1

ans = []
for i in range(N):
    tmp = d.copy()
    tmp[A[i]] -= 1
    ans = sum(list(map(combi, list(tmp.values()))))
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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


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


for a in A:
    d[a] += 1

ans = []
for i in range(N):
    tmp = d.copy()
    tmp[A[i]] -= 1
    ans = sum(list(map(combi, list(tmp.values()))))
    print(ans)

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)

#dic1はその要素を含む、dic2はその要素を含まない解
dic1 = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    if not a[i] in dic1:
        ball_n = a.count(a[i])
        if ball_n >= 2:
            ans1 = cmb(ball_n, 2)
            dic1[a[i]] = ans1


sum = sum(dic1.values())
for i in range(n):
    ball_n = a.count(a[i])
    c = 0
    b = 0
    if a[i] in dic1:
        b = dic1[a[i]]
        c = int(dic1[a[i]] * (ball_n-2) / ball_n)
    ans = sum - b + c
    print(ans)

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)

#dic1はその要素を含む、dic2はその要素を含まない解
dic1 = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    if not a[i] in dic1: 
        ball_n = a.count(a[i])
        if ball_n >= 2:
            ans1 = cmb(ball_n, 2)
            dic1[a[i]] = ans1
sum = sum(dic1.values())
for i in range(n):
    cnt = 0
    ball_n = a.count(a[i])
    if ball_n >= 3:
        cnt = cmb(ball_n-1, 2)
    ans = sum + cnt
    if a[i] in dic1:
        ans -= dic1[a[i]] 
    print(ans)

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)

#dic1はその要素を含む、dic2はその要素を含まない解
dic1 = {}
dic2 = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    if not a[i] in dic1: 
        ans1 = 0
        ans2 = 0
        ball_n = a.count(a[i])
        if ball_n >= 3:
            ans2 = cmb(ball_n-1, 2)
            ans1 = cmb(ball_n, 2)
        elif ball_n >= 2:
            ans2 = 0
            ans1 = cmb(ball_n, 2)
        dic1[a[i]] = ans1
        dic2[a[i]] = ans2

sum = sum(dic1.values())
for i in range(n):
    ans = sum - dic1[a[i]] + dic2[a[i]]
    print(ans)

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)

#dic1はその要素を含む、dic2はその要素を含まない解
dic1 = {}
dic2 = {}
#a[i]の要素に対する組み合わせが計算されていない場合
#計算後辞書に保存
#辞書から解を出力
for i in range(n):
    if not a[i] in dic1: 
        ans1 = 0
        ans2 = 0
        ball_n = a.count(a[i])
        if ball_n >= 3:
            ans2 = cmb(ball_n-1, 2)
            ans1 = cmb(ball_n, 2)
        elif ball_n >= 2:
            ans2 = 0
            ans1 = cmb(ball_n, 2)
        dic1[a[i]] = ans1
        dic2[a[i]] = ans2

for i in range(n):
    ans = 0
    ans = sum(dic1.values()) - dic1[a[i]] + dic2[a[i]]
    print(ans)