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 math import factorial as step
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]] += 1
  else:
    dic[a[i]] = 1

def ncr(n, r):
    if n < 0 or r < 0 or n - r < 0:
        return 0
    return step(n) // (step(r) * step(n-r))

# valueのncrのリストを作っておく
x = max(dic.values())
sms=[0]*(x+1)
for i in range(x+1):
  sms[i] = ncr(i,2)
all=sum([sms[v] for v in dic.values()])
for i in range(n):
  print(all-sms[dic[a[i]]]+sms[dic[a[i]]-1])

Submission status: Time Limit Exceeded

Solution code:
from math import factorial as step
n=int(input())
a=list(map(int,input().split()))
dic={}
sms={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]] += 1
  else:
    dic[a[i]] = 1

def ncr(n, r):
    if n < 0 or r < 0 or n - r < 0:
        return 0
    return step(n) // (step(r) * step(n-r))

for k,v in dic.items():
  sms[k] = ncr(v,2)
all=sum(sms.values())
for i in range(n):
  print(all-sms[a[i]]+ncr(dic[a[i]]-1,2))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
data_cnt=collections.Counter(data)
count=0
for i in data_cnt.values():
    count+=calc_combi2(i)
for i in data:
    ans=count+(calc_combi2(data_cnt[i]-1)-calc_combi2(data_cnt[i]))
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
for i in range(n):
    count=0
    cp=copy.copy(data)
    cp.pop(i)
    cp_cnt=list(collections.Counter(cp).values())
    for j in cp_cnt:
        if 2<=j:
            count+=int(calc_combi2(j))
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import collections
def calc_combi2(n):
    if n<=1:
        return 0
    return math.factorial(n)/(math.factorial(n-2)*math.factorial(2))
n=int(input())
data=list(map(int,input().split()))
for i in range(n):
    count=0
    cp=copy.copy(data)
    cp.pop(i)
    cp_cnt=collections.Counter(cp)
    for j in cp_cnt.values():
        count+=int(calc_combi2(j))
    print(count)

Submission status: Time Limit Exceeded

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

#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [0]*N
for i in range(N):
    S[L[i]-1]+=1


for i in range(N):
    tmp=S
    tmp[L[i]-1]-=1
    goukei=0
    for j in range(N):
        if tmp[j]>=2:
            goukei+=(tmp[j]*(tmp[j]-1))//2
    tmp[L[i]-1]+=1
    print(goukei)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial
N=int(input())
A=list(map(int,input().split()))
B=Counter(A)
count=0
def combination(x,y):
    return factorial(x) // (factorial(x - y) * factorial(y))
for key,b in B.items():
    if b>=2:
        count+=combination(b,2)
for a in A:
    ans=count
    if B[a]==2:
        ans-=1
    elif B[a]>=3:
        ans+=combination(B[a]-1,2)-combination(B[a],2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial
N=int(input())
A=list(map(int,input().split()))
B=Counter(A)
for a in A:
    count=0
    for key,b in B.items():
        if b >= 2 and a!=key:
            count += factorial(b) // (factorial(b - 2) * factorial(2))
        elif b>=3 and a==key:
            b-=1
            count += factorial(b) // (factorial(b - 2) * factorial(2))
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
    B=Counter(A[:i]+A[i+1:])
    count=0
    for b in B.values():
        if b >= 2:
            count+=factorial(b)//(factorial(b-2)*factorial(2))
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

for k in range(len(a)):
    c = collections.Counter(a[:k] + a[k+1:])

    if c.most_common()[0][1] == 1:
        print(0)
        continue

    ans = 0
    for _, v in c.items():
        ans += 0 if v < 2 else math.factorial(v)//(2 * math.factorial(v - 2))

    print(ans)

Submission status: Time Limit Exceeded

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

def factorial(n):
    v = 1
    for i in range(1, n + 1):
        v *= i
    return v

def comb(a, b):
    return factorial(a) // (factorial(b) * factorial(a - b))

from collections import Counter
acnt = dict(Counter(a))
value = list(acnt.values())
sv = 0
for i in range(len(value)):
    if value[i] > 1:
        sv += comb(value[i], 2)
for i in range(n):
    cur = a[i]
    cursv = sv
    if acnt[cur] > 2:
        cursv = cursv - acnt[cur] + 1
    elif acnt[cur] == 2:
        cursv -= 1
    print(cursv)

Submission status: Time Limit Exceeded

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

def factorial(n):
    v = 1
    for i in range(1, n + 1):
        v *= i
    return v

def comb(a, b):
    return factorial(a) // (factorial(b) * factorial(a - b))

from collections import Counter
acnt = dict(Counter(a))
value = list(acnt.values())
sv = 0
for i in range(len(value)):
    if value[i] > 1:
        sv += comb(value[i], 2)
for i in range(n):
    cur = a[i]
    cursv = sv
    if acnt[cur] > 2:
        cursv = cursv - comb(acnt[cur], 2) + comb(acnt[cur] - 1, 2)
    elif acnt[cur] == 2:
        cursv -= 1
    print(cursv)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=set(A)
res=0
C=[0]*(N+1)
for b in B:
    C[b]=A.count(b)
    if A.count(b)>1:
            res+=A.count(b)*(A.count(b)-1)//2
for a in A:
    ans=res
    if C[a]<2:
        print(ans)
    else:
        ans-=C[a]*(C[a]-1)//2
        if C[a]!=2:
            ans+=(C[a]-1)*(C[a]-2)//2
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from fractions import gcd
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


def inlist():
    return(list(map(int, input().split())))


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


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


n = inp()

a = inlist()
b = []
m = max(a)
count = [0] * (m+1)
for k in a:
    if count[k] == 0:
        b.append(k)
    count[k] += 1

for i in range(n):
    res = 0
    bad = a[i]
    for j in b:
        if bad == j:
            res += ncr(count[j]-1, 2)
        else:
            res += ncr(count[j], 2)

    print(res)

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from fractions import gcd
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


def inlist():
    return(list(map(int, input().split())))


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


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


n = inp()

a = inlist()
b = set(a)
m = max(a)
count = [0] * (m+1)
for k in a:
    count[k] += 1

for i in range(n):
    res = 0
    bad = a[i]
    for j in b:
        if bad == j:
            res += ncr(count[j]-1, 2)
        else:
            res += ncr(count[j], 2)

    print(res)

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from fractions import gcd
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


def inlist():
    return(list(map(int, input().split())))


def instr():
    s = input()
    return(list(s[:len(s)]))


def invr():
    return(map(int, input().split()))


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


n = inp()

a = inlist()
m = max(a)
count = [0] * (m+1)
for k in a:
    count[k] += 1

for i in range(n):
    res = 0
    bad = a[i]
    for j in range(m+1):
        if bad == j:
            res += ncr(count[j]-1, 2)
        else:
            res += ncr(count[j], 2)

    print(res)

Submission status: Time Limit Exceeded

Solution code:
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()))
B=set(A)
res=0
C=[0]*(N+1)
for b in B:
    C[b]=A.count(b)
    if A.count(b)>1:
            res+=combinations_count(A.count(b),2)
for a in A:
    ans=res
    if C[a]<2:
        print(ans)
    else:
        ans-=combinations_count(C[a],2)
        if C[a]!=2:
            ans+=combinations_count(C[a]-1,2)
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
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()))

for i in range(N):
    res=0
    if i==0:
        B=A[1:]
    elif i!=N-1:
        B=A[:i]+A[i+1:]
    else:
        B=A[:N-1]
    C=set(B)
    for c in C:
        if B.count(c)>1:
            res+=combinations_count(B.count(c),2)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
def Dict_Choice(D):
    S=0
    for k in D:
        x=D[k]
        S+=(x*(x-1))//2
    return S

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

D={}
for i in range(N):
    if A[i] in D:
        D[A[i]]+=1
    else:
        D[A[i]]=1

for i in range(N):
    y=A[i]
    D[y]-=1
    print(Dict_Choice(D))
    D[y]+=1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


for i in range(1,n+1):
    x[a[i-1]-1]-=1
    count = 0
    # x_ = [(k*(k-1))/2 for k in x if k>=2]
    # count = sum(x_)
    for j in x:
        if j>=2:
            count += int((j*(j-1)/2))
    x[a[i-1]-1]+=1
    print(count)

Submission status: Time Limit Exceeded

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

for i in range(1,n+1):
    x[a[i-1]-1]-=1
    count = 0
    for j in x:
        if j>=2:
            count += int((j*(j-1)/2))
    x[a[i-1]-1]+=1
    print(count)

Submission status: Time Limit Exceeded

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

b = set(a)

c = {}

for i in a:
    c[i] = c.get(i, 0) + 1

e = {}
for i in a:
    e[i] = c[i]*(c[i]-1)//2

d = {}

for k in a:
    if d.get(k, "hello") == "hello":
        ans = 0
        for i in b:
            if i != k:
                ans += e[i]
            else:
                ans += e[i]+1-c[i]
        d[k] = ans
    else:
        ans = d[k]
    print(ans)

Submission status: Time Limit Exceeded

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

b = set(a)

c = {}

for i in a:
    c[i] = c.get(i, 0) + 1

d = {}

for k in a:
    if d.get(k, "hello") == "hello":
        ans = 0
        for i in b:
            if i != k:
                ans += c[i]*(c[i]-1)//2
            else:
                ans += (c[i]-1)*(c[i]-2)//2
        d[k] = ans
    else:
        ans = d[k]
    print(ans)

Submission status: Time Limit Exceeded

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

b = set(a)

c = {}

for i in a:
    c[i] = c.get(i, 0) + 1

for k in a:
    ans = 0
    for i in b:
        if i != k:
            ans += c[i]*(c[i]-1)//2
        else:
            ans += (c[i]-1)*(c[i]-2)//2
    print(ans)

Submission status: Time Limit Exceeded

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

b = set(a)

c = {}

for i in b:
    c[i] = a.count(i)

for k in a:
    ans = 0
    for i in b:
        if i != k:
            ans += c[i]*(c[i]-1)//2
        else:
            ans += (c[i]-1)*(c[i]-2)//2
    print(ans)

Submission status: Time Limit Exceeded

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

for a in A:
    try:
        memo[a] += 1
    except:
        memo[a] = 1

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

# print(memo)
for i in range(N):
    ans = 0
    for key in memo.keys():
        if memo[key] < 2:
            continue
        if A[i] == key:
            if memo[key] - 1 < 2:
                continue
            else:
                ans += combinations_count(memo[key]-1, 2)
                # print(ans)
        else:
            ans += combinations_count(memo[key], 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
cnt=[0 for i in range(n)]
for i in a:
  cnt[i-1]+=1

nc2=[]
for i in cnt:
  nc2.append(((i)*(i-1))//2)
  
for i in range(n):
  print(sum(nc2)-nc2[a[i]-1]+((cnt[a[i]-1]-1)*(cnt[a[i]-1]-2))//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
cnt=[0 for i in range(n)]
nc2=[0 for i in range(n)]
for i in a:
  cnt[i-1]+=1
  nc2[i-1]=((cnt[i-1])*(cnt[i-1]-1))//2

for i in a:
  print(sum(nc2)-nc2[i-1]+((cnt[i-1]-1)*(cnt[i-1]-2))//2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
cnt=[0 for i in range(n)]
nc2=[0 for i in range(n)]
for i in a:
  cnt[i-1]+=1

for i in a:
  nc2[i-1]=((cnt[i-1])*(cnt[i-1]-1))//2
for i in a:
  print(sum(nc2)-nc2[i-1]+((cnt[i-1]-1)*(cnt[i-1]-2))//2)

Submission status: Time Limit Exceeded

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

n = 0
B = set(A)

for k in B:
    cnt = A.count(k)
    n +=  cnt * (cnt-1) // 2

for i in range(N):
    a = A[i]
    print(n - A.count(a) + 1)

Submission status: Time Limit Exceeded

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

n = 0
C = set(A)

for k in C:
    cnt = A.count(k)
    n +=  cnt * (cnt-1) // 2

for i in range(N):
    a = A[i]
    print(n - A.count(a) + 1)

Submission status: Time Limit Exceeded

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

n = 0
C = set(A)

for k in C:
    cnt = A.count(k)
    n +=  cnt * (cnt-1) // 2

for k in range(N):
    a = A[k]
    print(n - A.count(a) + 1)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

n = 0
C = set(A)

for k in C:
    cnt = A.count(k)
    n +=  cnt * (cnt-1) // 2

for k in range(N):
    a = A[k]
    print(n - A.count(a) + 1)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

ans = 0
C = set(A)

for k in range(N):
    B = copy.copy(A)
    #k = k + 1
    del B[k]
    for x in C:
        cnt = B.count(x)
        ans += cnt * (cnt-1) // 2
        cnt = 0
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
import copy

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

ans = 0

for k in range(N):
    B = copy.copy(A)
    #k = k + 1
    del B[k]
    for x in range(N):
        cnt = B.count(x+1)
        ans += cnt * (cnt-1) // 2
        cnt = 0
    print(ans)
    ans = 0

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement
from collections import Counter

n = int(input())
a = list(map(int, input().split())) #1-index

#n個のボールから書かれている整数が等しい異なる２つを選ぶ方法
n_l = list(Counter(a).values())
all_p = 0
for i in n_l:
    if i > 1:
        #all_p += len((list(combinations_with_replacement(range(1,i), 2))))
        all_p += (i-1)*i//2
#k番目のボールを覗いたn-1個から、k番目のボールと同じ整数を選ぶ方法
for k in range(n):
    print(all_p - (a.count(a[k])-1))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement
from collections import Counter
import sys
input = sys.stdin.readline


n = int(input())
a = list(map(int, input().split())) #1-index

#n個のボールから書かれている整数が等しい異なる２つを選ぶ方法
n_l = list(Counter(a).values())
all_p = 0
for i in n_l:
    if i > 1:
        all_p += len((list(combinations_with_replacement(range(1,i), 2))))

#k番目のボールを覗いたn-1個から、k番目のボールと同じ整数を選ぶ方法
for k in range(n):
    print(all_p - (a.count(a[k])-1))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement
from collections import Counter

n = int(input())
a = list(map(int, input().split())) #1-index

#n個のボールから書かれている整数が等しい異なる２つを選ぶ方法
n_l = list(Counter(a).values())
all_p = 0
for i in n_l:
    if i > 1:
        all_p += len((list(combinations_with_replacement(range(1,i), 2))))

#k番目のボールを覗いたn-1個から、k番目のボールと同じ整数を選ぶ方法
for k in range(n):
    print(all_p - (a.count(a[k])-1))

Submission status: Time Limit Exceeded

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

from itertools import combinations_with_replacement
from collections import Counter

n = int(input())
a = list(map(int, input().split())) #1-index

ans_l = []
for k in range(n):
    ans = 0
    temp = a[k]
    a.remove(a[k]) #k番目を取り除いた
    c_v = list(Counter(a).values())
    for i in c_v:
        if i > 1:
            ans += len((list(combinations_with_replacement(range(1,i), 2))))
    a.insert(k,temp)
    ans_l.append(ans)

for i in ans_l:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement
from collections import Counter

n = int(input())
a = list(map(int, input().split())) #1-index

ans_l = []
for k in range(n):
    ans = 0
    temp = a[k]
    a.remove(a[k]) #k番目を取り除いた
    c_v = list(Counter(a).values())
    for i in c_v:
        if i > 1:
            ans += len((list(combinations_with_replacement(range(1,i), 2))))
    a.insert(k,temp)
    ans_l.append(ans)

for i in ans_l:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


import collections
def main():
    N = int(input())
    A = list(map(int,input().split()))
    for i in range(N):
        p = A.pop(i)
        c = collections.Counter(A)
        ans = 0
        for j in c.values():
            ans += j * (j - 1) / 2
        A.insert(i, p)
        print(int(ans))
        

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

count = {}
for x in A:
    if count.get(x):
        count[x] += 1
    else:
        count[x] = 1

for i in range(N):
    count[A[i]] -= 1
    s = 0
    for x in count.values():
        s += x * (x - 1) // 2
    print(s)
    count[A[i]] += 1

Submission status: Time Limit Exceeded

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

res = 0
d = {}

d_res = {}

for i in range(N):
    if l[i] in d:
        d[l[i]] += 1
    else:
        d[l[i]] = 1



def get_n(d, x):
    res = 0
    for k,v in d.items():
        if k == x:
            res += ((v-1) * (v-2))/2
        else:
            res += (v * max(0,v-1))/2
    res = int(res)
    d_res[x] = res
    return res


for e in l:
    if e in d_res:
        print(d_res[e])
    else:
        print(get_n(d, e))

Submission status: Time Limit Exceeded

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

res = 0
d = {}

for i in range(N):
    if l[i] in d:
        d[l[i]] += 1
    else:
        d[l[i]] = 1



def get_n(d, x):
    res = 0
    for k,v in d.items():
        if k == x:
            res += ((v-1) * (v-2))/2
        else:
            res += (v * max(0,v-1))/2
    return res


for e in l:
    print(int(get_n(d, e)) )
    #break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
a=list(map(str,input().split()))
s=list(itertools.combinations(a,2))
cnt=0
for i in s:
  if i[0]==i[1]:
    cnt+=1

for i in a:
  c=a.count(i)
  print(cnt-c+1)

Submission status: Time Limit Exceeded

Solution code:
import collections

N=int(input())
L=list(map(int,input().split()))
M=collections.Counter(L)
K=[]
for i,j in (M.items()):
	K.append(j*(j-1)//2)
T=sum(K)
for k in L:
	for i,j in (M.items()):
		if k==i:
			print(T-(j*(j-1)//2)+(j-1)*(j-2)//2)

Submission status: Time Limit Exceeded

Solution code:
import copy
N=int(input())
A=[int(i) for i in input().split()]
T=sorted(set(A))
ANS=[]
for i in T:
    ans=0
    S=A.copy()
    del S[A.index(i)]
    S.sort()
    K=set(S)
    for t in K:
        c=S.count(t)
        ans+=c*(c-1)/2
    ANS.append(ans)
for k in A:
    print(int(ANS[T.index(k)]))

Submission status: Time Limit Exceeded

Solution code:
import copy
N=int(input())
A=[int(i) for i in input().split()]
for i in range(N):
    ans=0
    S=A.copy()
    del S[i]
    S.sort()
    K=set(S)
    for t in K:
        c=S.count(t)
        ans+=c*(c-1)/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def comb(n,k):
    nCk = 1
    MOD = 10**9+7

    for i in range(n-k+1, n+1):
        nCk *= i
        nCk %= MOD

    for i in range(1,k+1):
        nCk *= pow(i,MOD-2,MOD)
        nCk %= MOD
    return nCk

N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
for i in A:
    ans = 0
    for j in C:
        if i == j:
            ans += comb((C[j] - 1), 2)
        else:
            ans += comb(C[j], 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
A=list(map(int,input().split()))
#print(A)
B=collections.Counter(A)
#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 = [int(i) for i in input().split()]
b = {}
c = set()
for i in a:
    if i not in b:
        b[i] = 1
    else:
        b[i] += 1
        c.add(i)
for i in range(n):
    b[a[i]] -= 1
    x = 0
    for j in c:
        x += (b[j] * (b[j] - 1)) // 2
    print(x)
    b[a[i]] += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = set(a)
for i in range(n):
    c = a[: i] + a[i + 1 :]
    d = 0
    for j in b:
        e = c.count(j)
        d += (e * (e - 1)) // 2
    print(d)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
se_ = set(A)
 
 
all = 0
for i in se_:
    all += A.count(i)*(A.count(i)-1)//2
    
 
for i in A:
    final=0
    final = all - A.count(i)+1
    print(final)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def com(n,r):
    return n*(n-1)//2

N = int(input())
A = list(map(int,input().split()))
se_ = Counter(A)
key = se_.keys()


all = 0
for i in key:
    all += com(se_[i],2)
    

for i in A:
    final=0
    final = all - com(A.count(i),2) + com(A.count(i)-1,2)
    print(final)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
def com(n,r):
    return n*(n-1)//2

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


all = 0
for i in se_:
    all += com(A.count(i),2)
    

for i in A:
    final=0
    final = all - com(A.count(i),2) + com(A.count(i)-1,2)
    print(final)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
def com(n,r):
    if n-r<0:
        return 0
    return factorial(n) // factorial(r) // factorial(n - r)

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


all = 0
for i in se_:
    all += com(A.count(i),2)
    

for i in A:
    final=0
    final = all - com(A.count(i),2) + com(A.count(i)-1,2)
    print(final)

Submission status: Time Limit Exceeded

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

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


for i in A:
    counter = 0
    for kind in se_:
        if kind == i:
            counter += li_[A.count(kind)-1]
        else:
            counter += li_[A.count(kind)]
    print(counter)

Submission status: Time Limit Exceeded

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


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


for i in A:
    counter = 0
    for kind in se_:
        if kind == i:
            counter += count(A.count(kind)-1)
        else:
            counter += count(A.count(kind))  
    print(counter)

Submission status: Time Limit Exceeded

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

ball = [0 for _ in range(N)]

for i in range(N):
    ball[A[i] - 1] += 1

#print("ball", ball)
for k in range(N):
    ans = 0
    ball_new = [b for b in ball]
    ball_new[A[k] - 1] -= 1

    #print("ball_n", ball_new)
    for i in range(N):
        ball_num = ball_new[i]
        ans += ball_num*(ball_num-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

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

n = int(input())
a = list(map(int,input().split()))
n_list = [[] for i in range(n)]
len_list = [0 for i in range(n)]

for i in range(n):
    n_list[a[i]-1].append(i)

for i in range(n):
    if len(n_list[i]) == 0:
        len_list[i] == 0
    elif len(n_list[i]) == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(len(n_list[i]))

sum_len = sum(len_list)

for i in range(n):
    if len_list[a[i]-1] == 0:
        print(sum_len)
    elif len_list[a[i]-1] == 1 or len_list[a[i]-1] == 2:
        print(sum_len - 1)
    else:
        print(sum_len - len_list[a[i]-1] + combinations_count(len(n_list[a[i]-1]) - 1))

Submission status: Time Limit Exceeded

Solution code:
import math

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

n = int(input())
a = list(map(int,input().split()))
n_list = [[] for i in range(n)]
len_list = [0 for i in range(n)]

for i in range(n):
    n_list[a[i]-1].append(i)

for i in range(n):
    if len(n_list[i]) == 0:
        len_list[i] == 0
    elif len(n_list[i]) == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(len(n_list[i]))

sum_len = sum(len_list)
ans_list = [sum_len for i in range(n)]

for i in range(n):
    if len_list[a[i]-1] == 0:
        continue
    elif len_list[a[i]-1] == 1:
        ans_list[i] = ans_list[i] - 1
    elif len_list[a[i]-1] == 2:
        ans_list[i] = ans_list[i] - 1
    else:
        ans_list[i] = ans_list[i] - len_list[a[i]-1] + combinations_count(len(n_list[a[i]-1]) - 1)

for i in range(n):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
alst = list(map(int,input().split()))

for i in range(N):
    tmp = alst[i]
    alst.remove(alst[i])
    cnt = 0
    for v in itertools.combinations(alst, 2):
        if v[0] == v[1]:
            cnt += 1
    print(cnt)
    alst.insert(i,tmp)

Submission status: Time Limit Exceeded

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

from collections import Counter
counter=Counter(a)

# 番号nを取り除いた時の組み合わせをメモする
memo={}
def calc_combi_if_exclude(n):
    if n in memo:
        return memo[n]
    else:
        combi=0
        for num,cnt in counter.items():
            n_cnt=cnt
            if num==n:
                n_cnt-=1
            combi+=n_cnt*(n_cnt-1)//2
        memo[n]=combi
        return combi
    
for i in range(n):
    combi=0
    print(calc_combi_if_exclude(a[i]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
counter=Counter(a)
#print(a)
def calc_combi(n,m):
    a1=1
    a2=1
#    print(f'n={n} m={m}')
    for i in range(m):
        a1*=(n-i)
        a2*=(i+1)
#        print(f'a1={a1} a2={a2}')
    return a1//a2
#print(counter)
for i in range(n):
    combi=0
    for num,cnt in counter.items():
#        print(f'num={num} cnt={cnt}')
        n_cnt=cnt
        if num==a[i]:
            n_cnt-=1
        combi+=calc_combi(n_cnt ,2)
    print(combi)