Problem:


Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.  

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
import math
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = dict(collections.Counter(a))
    for i in range(n):
        ans = 0
        cnt[a[i]] -= 1
        for j in cnt.values():
            if j >= 2:
                ans += math.factorial(j)//(2*math.factorial(j-2))
        cnt[a[i]] += 1
        print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
import math
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnts = []
    for i in range(n):
        if i == 0:
            tmp = dict(collections.Counter(a[1:])) 
        elif i == n-1:
            tmp = dict(collections.Counter(a[:n-1])) 
        else:
            tmp = dict(collections.Counter(a[:i] + a[i+1:]))
        cnts.append(tmp)
    for cnt in cnts:
        ans = 0
        for x, y in cnt.items():
            if y >= 2:
                ans += math.factorial(y)//(2*math.factorial(y-2))
        print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

from collections import Counter
c = Counter(l)

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

num = []
tot = 0
for i,j in c.items():
    if j >= 2:
        tmp1 = combinations_count(j,2)
        tot += tmp1
    else:
        tmp1 = 0
    if j >= 3:
        tmp2 = combinations_count(j-1,2)
    else:
        tmp2 = 0
    num.append([i,tmp1,tmp2])

for x in range(n):
    for y in range(len(c)):
        if num[y][0] == l[x]:
            print(tot - num[y][1] + num[y][2])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from collections import deque
from scipy.special import comb
from copy import deepcopy

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

ldeq = deque()
rdeq = deque(A)

def process(mdeq):
    cm = Counter(mdeq)
    cnt = 0
    for key in cm:
        cnt += comb(cm[key], 2)
    print(int(cnt))

for i in range(N):
    a = rdeq.popleft()
    mdeq = deepcopy(rdeq)
    mdeq.extendleft(ldeq)
    process(mdeq)
    ldeq.append(a)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

B = list(Counter(A).items())

cnt = 0
for i in range(N):
    cnt = 0
    for j in range(len(B)):
        if A[i] == B[j][0]:
             cnt +=(B[j][1]-1)*(B[j][1]-2)//2 
        else:
            cnt +=B[j][1]*(B[j][1]-1)//2
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

count_a = Counter(A)
ans_list = [None] * (N+1)
for i in range(N):
    if ans_list[A[i]] != None:
        print(ans_list[A[i]])
        continue
    ans = 0
    count_a[A[i]] -= 1
    for a in count_a.values():
        if a > 1:
            ans += int(a * (a-1) / 2)
    print(ans)
    ans_list[A[i]] = ans
    count_a[A[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

count_a = Counter(A)

for i in range(N):
    ans = 0
    count_a[A[i]] -= 1
    for a in count_a.values():
        if a > 1:
            ans += int(a * (a-1) / 2)
    print(ans)
    count_a[A[i]] += 1

Submission status: Time Limit Exceeded

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

for i in range(n):
  ans = 0
  acopy = a.copy()
  del acopy[i]
  m = [x for x in set(acopy) if acopy.count(x) > 1]
  for j in range(len(m)):
    k = acopy.count(m[j])
    ans += k*(k-1)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    d = defaultdict(int)
    for e in a:
        d[e] += 1
    sm = 0
    xd = defaultdict(int)
    for k in d:
        if d[k] >= 2:
            xd[k] = factorial(d[k]) // (2 * factorial(d[k]-2))
        sm += xd[k]
    for e in a:
        d[e] -= 1
        sm -= xd[e]
        x = 0
        if d[e] >= 2:
            x = factorial(d[e]) // (2 * factorial(d[e]-2))
        print(sm + x)
        sm += xd[e]
        d[e] += 1


    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""


"""

Submission status: Time Limit Exceeded

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

l = a.copy()

a.sort()
ans = 0
i = 0
while True:
    cnt = 0
    while a[i + cnt] == a[i]:
        cnt += 1
        if i + cnt == n:
            break
    ans += cnt*(cnt - 1)//2
    i += cnt
    if i == n:
        break

for i in range(n):
    num = l[i]
    del l[i]
    ac = l.count(num)
    print(ans - ac)
    l.insert(0, num)

Submission status: Time Limit Exceeded

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

l = a.copy()

a.sort()
ans = 0
i = 0
while True:
    cnt = 0
    while a[i + cnt] == a[i]:
        cnt += 1
        if i + cnt == n:
            break
    ans += cnt*(cnt - 1)//2
    i += cnt
    if i == n:
        break

for i in range(n):
    num = l[i]
    del l[i]
    ac = l.count(num)
    print(ans + (ac*(ac - 1) - ac*(ac + 1))//2)
    l.insert(0, num)

Submission status: Time Limit Exceeded

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

l = []
for i in range(n):
    num = a[i]
    del a[i]
    ac = a.count(num)
    l.append((ac*(ac - 1) - ac*(ac + 1))//2)
    a.insert(0, num)

a.sort()
ans = 0
i = 0
while True:
    cnt = 0
    while a[i + cnt] == a[i]:
        cnt += 1
        if i + cnt == n:
            break
    ans += cnt*(cnt - 1)//2
    i += cnt
    if i == n:
        break

for i in l:
    print(ans + i)

Submission status: Time Limit Exceeded

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

def cul(l):
    l.sort()
    ans = 0
    i = 0
    while True:
        cnt = 0
        while l[i + cnt] == l[i]:
            cnt += 1
            if i + cnt == len(l):
                break
        ans += cnt*(cnt - 1)//2
        i += cnt
        if i == len(l):
            break
    return ans

for i in range(n):
    num = a[i]
    del a[i]
    l = a.copy()
    print(cul(l))
    a.insert(0, num)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def main():
    n = int(input())
    balls = [int(i) for i in input().split()]

    # v - value written on ball
    # b - ball index in [0, n)
    # ct - number of times a value occurs

    # hist[v] = ct
    hist = Counter(balls)
    diff_counts = set(hist.values())
    combos1 = { ct: 0 if ct == 1 else (ct-1) * (ct-2) // 2 for ct in
            diff_counts }
    combos = { ct: ct * (ct-1) // 2 for ct in diff_counts  }

    for b in range(n):
        v = balls[b]
        ct = hist[v]
        tot = sum(combos[hist[v2]] for v2 in hist.keys())
        print(tot - combos[ct] + combos1[ct])

main()

Submission status: Time Limit Exceeded

Solution code:

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

deta = [0] * (n+1)

for i in a:
    deta[i] += 1

ans = [0] * (n+1)

for j in range(n+1):
    if deta[j] > 1:
        ans[j] = deta[j]*(deta[j]-1)/2
    else :
        ans[j] = 0

for k in a:
    if deta[k] == 2:
        print(int(sum(ans) - 1))
    elif deta[k] == 1:
        print(int(sum(ans)))
    else:
        print(int(sum(ans) - deta[k]*(deta[k]-1)/2 + (deta[k]-1)*(deta[k]-2)/2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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


for i in range(N):
    kosu = Counter(A)

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


def C(N):
    if N <= 0:
        return 0
    else:
        return N*(N-1)//2


for a in A:
    print(tempSum - kosu[a]+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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


for i in range(N):
    kosu = Counter(A)

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


def C(N):
    if N <= 0:
        return 0
    else:
        return N*(N-1)//2


for a in A:
    print(tempSum - C(kosu[a]) + C(kosu[a] - 1))

Submission status: Time Limit Exceeded

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

c=collections.Counter(b)
n,m=zip(*c.most_common())
n,m=list(n),list(m)
result=0
for i in range(len(m)):
    result+=(m[i]*(m[i]-1))//2

for i in b:
    print(result-m[n.index(i)]+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

kosu = [0] * N

for i in range(N):
    kosu = Counter(A)

tempSum = 0
for i in range(N):
    tempSum += kosu[i+1] * (kosu[i+1] - 1) // 2


def C(N):
    if N <= 0:
        return 0
    else:
        return N*(N-1)//2


for a in A:
    print(tempSum - C(kosu[a]) + C(kosu[a] - 1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

kosu = [0] * N

for i in range(N):
    kosu = Counter(A)

tempSum = 0
for i in range(N):
    tempSum += kosu[i+1] * (kosu[i+1] - 1) // 2


def C(N):
    if N <= 0:
        return 0
    else:
        return N*(N-1)//2


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

Submission status: Time Limit Exceeded

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

kosu = [0] * N

for i in range(N):
    kosu[i] = (A.count(i+1))

tempSum = 0
for i in range(N):
    tempSum += kosu[i] * (kosu[i] - 1) // 2

def C(N):
    if N <= 0:
        return 0
    else:
        return N*(N-1)//2


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

Submission status: Time Limit Exceeded

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

c=collections.Counter(b)
n,m=zip(*c.most_common())
n,m=list(n),list(m)
result=0
for i in range(len(m)):
    result+=(m[i]*(m[i]-1))//2
for i in range(a):
    print(result-(m[n.index(b[i])]-1))

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb

INF = 100100100

N = int(input())
A = list(map(int, input().split()))
sorted_A = sorted(A)
countNums = [0]*(N+1)

target = sorted_A[0]
for i in range(N):
    if target==sorted_A[i]:
        countNums[target] += 1
    else:
        target = sorted_A[i]
        countNums[target] += 1;


for i in range(N):
    a = A[i]
    countNums[a] -= 1
    ans = 0
    for c in countNums:
        ans += int(comb(c, 2))
    print(ans)
    countNums[a] += 1

Submission status: Time Limit Exceeded

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

#%%
d = defaultdict(lambda: 0)
for key in A:
    d[key] += 1

for a in A:
    ans = 0
    for k, v in d.items():
        if k == a:
            v -= 1
        ans += v * (v - 1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = 0

for i in range(N):
    c = a[i]
    del a[i]
    a1 = sorted(a)
    for j in range(N-2):
        for k in range(j+1,N-1):
            if a1[j] == a1[k]:
                ans += 1
    print(ans)
    ans = 0
    a.insert(i,c)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
c = collections.Counter(a)
dp = [-1] * 2*10**5
for i in a:
    if dp[i-1] != -1:
        print(dp[i-1])
        continue
    ans = 0
    for k, v in c.items():
        ci = v
        if k == i:
            ci = max(0, v-1)
        ans += ci*(ci-1)//2
    dp[i-1] = ans
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
c = collections.Counter(a)
for i in a:
    ans = 0
    for k, v in c.items():
        ci = v
        if k == i:
            ci = max(0, v-1)
        ans += ci*(ci-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int, input().split()))
t = {}
niko = {}
ans = {}

for a in arr:
    if a in t:
        t[a] += 1
    else:
        t[a] = 1
    if t[a] >= 2:
        niko[a] = t[a]

for a in arr:
    if a not in ans:
        # 2個以上あるやつから選ぶ
        x = 0
        if a in niko:
            niko[a] -= 1
        for _, v in niko.items():
            if v >= 2:
                x += v * (v - 1) // 2
        if a in niko:
            niko[a] += 1
        ans[a] = x
    print(ans[a])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int, input().split()))
t = {}
ans = {}

for a in arr:
    if a in t:
        t[a] += 1
    else:
        t[a] = 1

for a in arr:
    if a not in ans:
        # 2個以上あるやつから選ぶ
        x = 0
        t[a] -= 1
        for k, v in t.items():
            if v >= 2:
                x += v * (v - 1) // 2
        t[a] += 1
        ans[a] = x
    print(ans[a])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


def tri(num):
    return int(num*(num-1)/2)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    s = sum([tri(j) for j in c.values()])
    for i in a:
        cnt = a.count(i)
        x = s - tri(cnt) + tri(cnt-1)
        print(x)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def tri(num):
    return int(num*(num+1)/2)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        cnt = 0
        ban = a[i]
        for j in range(10):
            plus = a.count(j)
            if j == ban:
                plus -= 1
            if plus:
                cnt += tri(plus-1)
        print(cnt)
    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def tri(num):
    return int(num*(num+1)/2)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    for i in range(n):
        cnt = 0
        ban = a[i]
        for j in range(10):
            # print(f'j{j}')
            plus = a.count(j)
            if j == ban:
                plus -= 1
            if plus:
                cnt += tri(plus-1)
            # print(f'plus{plus}')
        print(cnt)
    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy

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

for k in range(n):
    a_copy = copy.copy(a)
    del a_copy[k]
    a_set = set(a_copy)
    cnt = 0
    for i in a_set:
        num = a_copy.count(i)
        cnt += (num * (num-1)) / 2
    print(int(cnt))

Submission status: Time Limit Exceeded

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

a_2 = [0 for i in range(n)]
for i in range(n):
    a_2[a[i]-1] += 1

al = [0 for i in range(n)]
c = 0
for i in a_2:
    if i > 1:
        al[c] += i * (i-1) / 2
    c += 1

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sum(al)-(al[f-1]-t*(t-1)/2)))

Submission status: Time Limit Exceeded

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

a_2 = [0 for i in range(n)]
for i in range(n):
    a_2[a[i]-1] += 1

al = [0 for i in range(n)]
c = 0
for i in a_2:
    al[c] += int(i * (i-1) / 2)
    c += 1

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sum(al)-(al[f-1]-t*(t-1)/2)))

Submission status: Time Limit Exceeded

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

a_2 = [0 for i in range(n)]
for i in range(n):
    a_2[a[i]-1] += 1

al = []
for i in a_2:
    al.append(int(i * (i-1) / 2))

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sum(al)-(al[f-1]-t*(t-1)/2)))

Submission status: Time Limit Exceeded

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

a_2 = [0 for i in range(max(a))]
al = []
for i in range(a_m):
    a_2[i] += a.count(i+1)
    al.append(int(a_2[i] * (a_2[i]-1) / 2))

al_s = sum(al)

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(al_s-(al[f-1]-t*(t-1)/2)))

Submission status: Time Limit Exceeded

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

a_2 = [0 for i in range(max(a))]
for i in range(max(a)):
    a_2[i] += a.count(i+1)

al = []
for i in a_2:
    al.append(int(i * (i-1) / 2))

for i in range(n):
    f = a[i]
    t = a_2[f-1] - 1
    print(int(sum(al)-(al[f-1]-t*(t-1)/2)))

Submission status: Time Limit Exceeded

Solution code:
import collections
import itertools

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

for i in range(n):
  ans = 0
  b = a.copy()
  del b[i]
  c = collections.Counter(b)
  for j in list(c.keys()):
    if c[j] == 1:
      continue
    c_list = list(itertools.combinations('a'*c[j], 2))
    ans += len(c_list)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

def combinations_count(n, r): # 組み合わせ nCr
    if n < r or n == 0:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
a = list(map(int,input().split()))
a_set = collections.Counter(a)
b = list(a_set)
total = 0

for v in b: # 全通り
    if 2 <= a_set[v]:
        total += combinations_count(a_set[v],2)

for v in a:
    if a_set[v] < 2:
        print(total)
    else:
        print(total-combinations_count(a_set[v],2)+combinations_count(a_set[v]-1,2))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

def combinations_count(n, r): # 組み合わせ nCr
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
a = list(map(int,input().split()))
a_set = collections.Counter(a)
b = list(a_set)
total = 0

for v in b: # 全通り
    if 2 <= a_set[v]:
        total += combinations_count(a_set[v],2)

max_num = max(a_set.values()) # 最大個数
pair_list = [0]*max_num

for n in range(2,max_num+1): # 個数ごとの組み合わせ数を格納
    pair_list[n-1] = combinations_count(n,2)

for v in a:
    if a_set[v] < 2:
        print(total-pair_list[a_set[v]-1])
    else:
        print(total-pair_list[a_set[v]-1]+pair_list[a_set[v]-2])

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

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


N = I()
A = LI()

for i in range(0, N):
    A_part = A[:i] + A[i+1:]
    counter_a = Counter(A_part)
    choice_total = sum([(count*(count-1)//2) for count in counter_a.values() if count > 1])
    print(choice_total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

dp=[-1 for i in range(N)]

def comb(num):
    if dp[num]!=-1:
        return dp[num]
    dp[num]=(num*(num-1))//2
    return dp[num]

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

dict={i:0 for i in A}

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

for i in A:
    num=0
    for j in dict.keys():
        if j==i:
            num+=comb(dict[j]-1)
            continue
        num+=comb(dict[j])
    print(num)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

dict={i:0 for i in A}

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

for i in A:
    num=0
    for j in dict.keys():
        if j==i:
            num+=((dict[i]-1)*(dict[i]-2)//2)
            continue
        num+=((dict[j]*(dict[j]-1))//2)
    print(num)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

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

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

dic = Counter(A)

for i in range(N):
    a = A[i]
    dic[a] -= 1
    ans = 0
    for v in dic.values():
        if v >= 2:
            ans += combinations_count(v)
    dic[a] += 1
    print(ans)

Submission status: Time Limit Exceeded

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

dic = Counter(A)

for i in range(N):
    a = A[i]
    dic[a] -= 1
    ans = 0
    for v in dic.values():
        if v >= 2:
            ans += combinations_count(v, 2)
    dic[a] += 1
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
  M = A.count(i+1)
  S += M*(M-1)//2

for j in range(N):
  X = A.count(A[j])
  C = X*(X-1)//2
  D = (X-1)*(X-2)//2
  S -= C
  S += D
  print(S)
  S -= D
  S += C

Submission status: Time Limit Exceeded

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

for i in range(N):
  M = A.count(i+1)
  S += M*(M-1)//2

for j in range(N):
  X = A.count(A[j])
  S -= X*(X-1)//2
  S += (X-1)*(X-2)//2
  print(S)
  S -= (X-1)*(X-2)//2
  S += X*(X-1)//2

Submission status: Time Limit Exceeded

Solution code:

def main(N, A):
    B = list(set(A))
    C = []
    S = 0
    for b in B:
        c = A.count(b)
        C.append(c)
        S += c * (c - 1) // 2
    for i in range(N):
        d = B.index(A[i])
        ans = S - C[d] + 1
        print(ans)

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()))
B = []
C = []
S = 0
for a in A:
    c = A.count(a)
    C.append(c)
    if a not in B:
        B.append(a)
        S += c * (c - 1) // 2

for i in range(N):
    ans = S - C[i] + 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(set(A))
C = []
S = 0
for b in B:
    c = A.count(b)
    C.append(c)
    S += c * (c - 1) // 2
for i in range(N):
    d = B.index(A[i])
    ans = S - C[d] + 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
dic = {}
ans = {}
more_than_two = []

for num in A:
    if num in dic:
        dic[num] += 1
        if num not in more_than_two:
            more_than_two.append(num)
    else:
        dic[num] = 1

for num in A:
    if num in ans:
        print(ans[num])
    else:
        dic[num] -= 1
        total = 0
        for a in more_than_two:
            total += dic[a] * (dic[a]-1) / 2
        ans[num] = int(total)
        print(int(total))
        dic[num] += 1

Submission status: Time Limit Exceeded

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

for num in A:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

for num in A:
    if num in ans:
        print(ans[num])
    else:
        dic[num] -= 1
        total = 0
        for value in dic.values():
            total += value * (value-1) / 2
        ans[num] = int(total)
        print(int(total))
        dic[num] += 1

Submission status: Time Limit Exceeded

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

for num in A:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

for num in A:
    dic[num] -= 1
    total = 0
    for value in dic.values():
        total += value * (value-1) / 2
    print(int(total))
    dic[num] += 1

Submission status: Time Limit Exceeded

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

# In[22]:


import collections
import math


# In[31]:


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


# In[32]:


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


# In[33]:


for i in range(N):
    mylist = list(collections.Counter(A[:i] + A[i+1:]).items())
    ans = 0
    for x,cnt in mylist:
        if cnt > 1:
            ans += combinations_count(cnt, 2)
    print(ans)


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from operator import mul
from functools import reduce

N = int(input())

line = [int(x) for x in input().split()]
counter = Counter(line)

result = {}

for i in range(N):
    target = line[i]
    collection = 0
    if target in result:
        print(result[target])
    else:
        for key, value in counter.items():
            if key == target:
                value -= 1

            collection += int((value * (value-1) / 2))
        result[target] = collection
        print(collection)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
Alist = list(map(int,input().split()))
numcntlist = [0] * N 
numCounter = Counter(Alist)

for i in range(1,N+1):
    numcntlist[i-1] = numCounter[i]

ans = 0

for i in Alist:
    numcntlist[i-1] -= 1 
    for j in numcntlist:
        if j == 0 or j == 1:
            None
        else:
            ans += int(j*(j-1)/2)
    
    print(ans)
    numcntlist[i-1] += 1
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
list1=list(map(int,input().split()))
list2=[0]*N

for i in range(N):
    list2[i]=list1.count(i+1)

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

total=0
for i in list2:
    total+=choose2(i)


for i in list1:
    ans=total-(list2[i-1]-1)
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)
# print(uni)
# print(cou)
com=[]
for i in range(len(cou)):
    nforc=cou[i]
    c=nforc*(nforc-1)//2
    com.append(c)
# print(com)
totalcombination=sum(com)

'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''
dic={}
for num in uni:
    dic.setdefault(num)

#print(dic)

for j in range (N):
    n=A[j] #nはAの数列の数字
    if dic[n]==None:
        f=np.where(uni==n)[0][0]
        t=com[f]
        nfore=cou[f]
        e=(nfore-1)*(nfore-2)//2
        a=totalcombination-t+e
        dic[n]=a
        print(a)
    else:
        print(dic[n])

Submission status: Time Limit Exceeded

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

# 0 0
# 1 0
# 2 1
# 3 3
# 4 6

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

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

data = collections.Counter(A)
dict = {}

for i in range(N):
    if dict.get(A[i]) == None :
        ans = 0
        tmp = copy.copy(data)
        tmp[A[i]] = data[A[i]] - 1
        C = list(tmp.values())
        for j in range(len(C)):
            if C[j] >= 2:
                ans += comb(C[j],2)
        dict[A[i]] = ans
    else:
        ans = dict.get(A[i])
    print(ans)

Submission status: Time Limit Exceeded

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

# 0 0
# 1 0
# 2 1
# 3 3
# 4 6

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

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

data = collections.Counter(A)

for i in range(N):
    #print(data)
    ans = 0
    tmp = copy.copy(data)
    tmp[A[i]] = data[A[i]] - 1
    C = list(tmp.values())
    #print(C)
    for j in range(len(C)):
        if C[j] >= 2:
            ans += comb(C[j],2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

d = Counter(a)

dict1 = {}
dict2 = {}
for key in d:
  m = d[key]
  if m == 1:
    dict1[key] = 0
    dict2[key] = 0
  elif m == 2:
    dict1[key] = 1
    dict2[key] = 0
  else:
    dict1[key] = m*(m-1)//2
    dict2[key] = (m-1)*(m-2)//2

for each in a:
  tmp = sum(dict1.values()) - dict1[each] + dict2[each]
  print(tmp)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
line = list(map(int, input().split()))
dic = set(line)
nums = 0
for i in dic:
    cnt = line.count(i) - 1
    nums += (cnt * (cnt + 1)) // 2

for i in line:
    cnt = line.count(i) - 1
    print(nums - cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
line = list(map(int, input().split()))
added = []
nums = 0
for i in line:
    if i not in added:
        cnt = line.count(i) - 1
        nums += (cnt * (cnt + 1)) // 2
        added.append(i)

for i in line:
    cnt = line.count(i) - 1
    print(nums - cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]

f_list = [0 for i in range(N+1)]
f_list[0] = 1
for i in range(1, N+1):
    f_list[i] = f_list[i-1]*i

def c(n):
    return f_list[n]//f_list[n-2]//2

m = Counter(A)
s = 0
for x, y in m.items():
    if y != 1:
        s += c(y)
for a in A:
    print(s - (m[a] - 1))