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:
n = int(input())
a = list(map(int, input().split()))
x = 0
b = []
for i in range(n):
    y = a.count(i + 1)
    if y != 0:
        b.append(y)
    else:
        b.append(0)
    x += y * (y - 1) / 2

for j in range(n):
    print(int(x - b[a[j] - 1] + 1))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
def bicomb(n):
    return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))

from collections import Counter
cnt = Counter()
    
N = int(input())
A = list(map(int,input().split()))
for i in range(N):
    cnt[A[i]] += 1
ans = 0
for v in cnt.values():
    if v==1:
        continue
    else:
        ans += bicomb(v)
ans2 = ans
for i in A:
    if cnt[i]==1:
        pass
    elif cnt[i]==2:
        ans -= 1
    else:
        ans -= bicomb(cnt[i]) - bicomb(cnt[i]-1)
    print(ans)
    ans = ans2

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb

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

for k in range(n):
    ans=0
    B=A.copy()
    B.pop(k)
    C=list(set(B))
    for j in range(len(C)):
        if B.count(C[j])>1:
             ans+=comb(B.count(C[j]),2, exact=True)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

def comb(n):
    if dp[n]!=-1:
        return dp[n]
    if n-2<0:
        dp[n]=0
        return dp[n]
    dp[n]=math.factorial(n)//(math.factorial(2)*math.factorial(n-2))
    return dp[n]

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

dp=[-1 for i in range(N+1)]
nums={}

for i in range(N):
    if not A[i] in nums:
        nums[A[i]]=1
    else:
        nums[A[i]]+=1

ans=[]
for i in range(N):
    number=0
    nums[A[i]]-=1
    for j in nums.keys():
        number+=comb(nums[j])
    ans.append(number)
    nums[A[i]]+=1

for i in range(N):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

nums=[0 for i in range(N+1)]

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

ans=[]
for i in range(N):
    number=0
    nums[A[i]]-=1
    for j in range(1,N+1):
        number+=comb(nums[j],2)
    ans.append(number)
    nums[A[i]]+=1

for i in range(N):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from copy import deepcopy
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    a_c= Counter(A)
    values = a_c.values()
    val_cnt = Counter(values)
    cnt = 0
    for a in A:
        val_cnt_c = deepcopy(val_cnt)
        val_cnt_c[a_c[a]] -= 1
        val_cnt_c[a_c[a]-1] += 1
        cnt = {int(k*(k-1)/2*v) if k > 1 else 0 for k, v in val_cnt_c.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from copy import deepcopy

def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    a_c = Counter(A)
    res = [int(v*(v-1)/2) for v in a_c.values()]
    cnt = sum(res)
    for a in A:
        a_c_c = deepcopy(a_c)
        ans = (cnt - (a_c_c[a]-1))
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from copy import deepcopy

def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    a_c= Counter(A)
    values = a_c.values()
    val_cnt = Counter(values)
    cnt = 0
    for a in A:
        a_c_c = deepcopy(a_c)
        val_cnt_c = deepcopy(val_cnt)
        val_cnt_c[a_c_c[a]] -= 1
        val_cnt_c[a_c_c[a]-1] += 1
        cnt = {int(k*(k-1)/2*v)  if k > 1 else 0 for k, v in val_cnt_c.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():

    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 = [int(x) for x in input().split()]
    a_c= Counter(A)
    values = a_c.values()
    val_cnt = Counter(values)
    cnt = 0
    for a in A:
        a_c_c = deepcopy(a_c)
        val_cnt_c = deepcopy(val_cnt)
        val_cnt_c[a_c_c[a]] -= 1
        val_cnt_c[a_c_c[a]-1] += 1
        cnt = {cmb(k, 2) * v  if k > 1 else 0 for k, v in val_cnt_c.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce
from collections import Counter
from copy import deepcopy
import sys
sys.setrecursionlimit(10**7)

def main():
    nCr = {}
    def cmb(n, r):
        if r == 0 or r == n: return 1
        if r == 1: return n
        if (n,r) in nCr: return nCr[(n,r)]
        nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
        return nCr[(n,r)]

    n = int(input())
    A = [int(x) for x in input().split()]
    a_c= Counter(A)
    cnt = 0
    for a in A:
        a_c_c = deepcopy(a_c)
        a_c_c[a] = a_c_c[a] - 1
        values = a_c_c.values()
        val_cnt = Counter(values)
        cnt = {cmb(k, 2) * v  if k > 1 else 0 for k, v in val_cnt.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():

    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 = [int(x) for x in input().split()]
    a_c= Counter(A)
    cnt = 0
    for a in A:
        a_c_c = deepcopy(a_c)
        a_c_c[a] = a_c_c[a] - 1
        values = a_c_c.values()
        val_cnt = Counter(values)
        cnt = {cmb(k, 2) * v  if k > 1 else 0 for k, v in val_cnt.items()}
        print(sum(cnt))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
N = int(input())
A = list(map(int,input().split()))
dp = {}
dp2 = {}
C = collections.Counter(A)
for i in range(N):
	if dp.get(A[i], False):
		print(dp[A[i]])
		continue
	temp = copy.copy(C)
	temp[A[i]] -= 1
	result = 0
	for c in temp.most_common():
		if c[1] <= 1:
			break
		else:
			if dp2.get(c[1], False):
				result += dp2[c[1]]
			else:
				dp2[c[1]] = sum(range(0, c[1]))
				result += dp2[c[1]]
	dp[A[i]] = result
	print(dp[A[i]])

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy
N = int(input())
A = list(map(int,input().split()))
dp = {}
C = collections.Counter(A)
for i in range(N):
	if dp.get(A[i], False):
		print(dp[A[i]])
		continue
	temp = copy.copy(C)
	temp[A[i]] -= 1
	result = 0
	for c in temp.most_common():
		if c[1] <= 1:
			break
		else:
			result += sum(range(0, c[1]))
	dp[A[i]] = result
	print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
b = set(a)
from scipy.special import comb
# a = comb(n, r)
list_1 = deque([0]*200001)
list_2 = deque([0]*200001)
count=0
for k,i in enumerate(b):
  num = 0
  num = a.count(i)
  num1 = int(num*(num-1)/2)
  list_1[i]= num1
  list_2[i] = int((num1/num)*(num-2))
  
count=sum(list_1)
for i in a:
  print(count-list_1[i]+list_2[i])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
b = set(a)
from scipy.special import comb
# a = comb(n, r)
list_1 = deque([0]*200001)
list_2 = deque([0]*200001)
count=0
for k,i in enumerate(b):
  num = 0
  num = a.count(i)
  list_1[i] = comb(num, 2, exact=True)
  list_2[i] = comb(num-1, 2,exact=True)
  
count=sum(list_1)
for i in a:
  print(count-list_1[i]+list_2[i])

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

s = set(A)
d = {}
for n in s:
  d[n] = A.count(n)
  
cache = {}
for n in A:
  total = 0
  v = cache.get(n)
  if v == None:
    d[n] -= 1
    for m in s:
      if d[m] < 2:
        continue
      total += combinations_count(d[m],2)
    d[n] += 1
    cache[n] = total
  else:
    total = v
  print(total)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

s = set(A)
d = {}
for n in s:
  d[n] = A.count(n)
  
for n in A:
  total = 0
  d[n] -= 1
  for m in s:
    if d[m] < 2:
      continue
    total += combinations_count(d[m],2)
  d[n] += 1
  print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
from functools import lru_cache
from functools import reduce
from operator import mul
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


@lru_cache(None)
def cmb(n: int, r: int):
    if n < r:
        return 0
    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


def main():
    _ = int(input().strip())
    A = [int(i) for i in input().strip().split()]
    cnts = Counter(A)
    for a in A:
        tmp = 0
        cnts[a] -= 1
        for k, v in cnts.items():
            tmp += cmb(v, 2)
        print(tmp)
        cnts[a] += 1
    return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
N = int(input())
A = list(map(int, input().split()))
n = {}
for i in range(N):
    if A[i] not in n:
        n[A[i]] = 1
    else:
        n[A[i]] += 1
for i in range(N):
    n[A[i]] -= 1
    ans = 0
    for j in n:
        if n[j] > 1:
            ans += factorial(n[j]) // 2 // factorial(n[j]-2)
    n[A[i]] += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
N = int(input())
A = list(map(int, input().split()))
n = [0] * (2 * 10 ** 5 + 1)
for i in range(N):
    n[A[i]] += 1
for i in range(N):
    n[A[i]] -= 1
    ans = 0
    for j in range(2 * 10 ** 5 + 1):
        if n[j] > 1:
            ans += factorial(n[j]) // 2 // factorial(n[j]-2)
    n[A[i]] += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
an = tuple(map(str, input().split()))
for i in range(n):
    an2=list(an)
    Ans = 0
    an2[i]="0"
    c = collections.Counter(an2)
    c_list = list(c.values())
    
    for j in range(len(c_list)):
        Ans += (c_list[j]-1)*c_list[j]/2
    print(int(Ans))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
an = list(map(str, input().split()))

test_list = [[an[i] for i in range(n)] for i in range(n)]
for i in range(n):
    Ans = 0
    test_list[i][i]="0"
    c = collections.Counter(test_list[i])
    c_list = list(c.values())
    
    for j in range(len(c_list)):
        Ans += (c_list[j]-1)*c_list[j]/2
    print(int(Ans))

Submission status: Time Limit Exceeded

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

S={}
for i in range(n):
    a = A[i]
    if a in S :
        S[a]+=1
    else:
        S[a]=1

K=[]
for i in range(n):
    a = A[i]
    S[a]-=1
    c=0
    K=list(S.keys())
    for j in range(len(S)):
        s = S[K[j]]
        if s>=2 :
            c += s*(s-1)//2   
    S[a]+=1
    print(c)

Submission status: Time Limit Exceeded

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
n = ini()
a = inl()
b = [0] * n
ans = 0
for i in range(n):
    b[i] = a.count(i+1)
    ans += b[i] * (b[i]-1) // 2
for i in range(n):
    print(ans - b[a[i]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
K = list(map(int,input().split()))
"""
N = 4
K = [1, 2 ,3 ,4]
"""
a = list(set(K))
#print(a)
a_count = {}

for i in a :
    a_count[i] = K.count(i)

#print(a_count)

ans_dict = 0
for l in a_count.values() :
        
    ans_dict += l * (l-1)  // 2

#print(ans_dict)

"""
for i in a_count :

    a_count_2 = copy.deepcopy(a_count)
    
    a_count_2[i] -= 1

    #print(a_count_2)
    
    ans_dict[i] = 0
    for l in a_count_2.values() :
        
        ans_dict[i] += l * (l-1)  // 2

#print(ans_dict)
"""

for i in K:
    print(ans_dict - (a_count[i]-1))
    pass

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
K = list(map(int,input().split()))
"""
N = 4
K = [1, 2 ,3 ,4]
"""
a = list(set(K))
#print(a)
a_count = {}

for i in a :
    a_count[i] = K.count(i)

#print(a_count)

ans_dict = {}
for i in a_count :

    a_count_2 = copy.deepcopy(a_count)
    
    a_count_2[i] -= 1

    #print(a_count_2)
    
    ans_dict[i] = 0
    for l in a_count_2.values() :
        
        ans_dict[i] += l * (l-1)  // 2

#print(ans_dict)

for i in K:
    print(ans_dict[i])
    pass

Submission status: Time Limit Exceeded

Solution code:
import copy

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

a = list(set(K))
#print(a)
a_count = {}

for i in a :
    a_count[i] = K.count(i)

#print(a_count)



for i in range(N):
    a_count_2 = copy.deepcopy(a_count)
    
    a_count_2[K[i]] -= 1

    #print(a_count_2)
    ans = 0

    for i in a_count_2.values() :
        
        ans += i * (i-1)  // 2

    print(ans)

    pass

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  if lst[i] not in dic:
    dic[lst[i]] = 1
  else:
    dic[lst[i]] += 1
#print(dic)
dic2 = {}
for key, value in dic.items():
  dic2[key] = [value * (value - 1) // 2, (value - 1) * (value - 2) // 2]
ans = {}
for i in range(n):
  count = 0
  m = lst[i]
  if m not in ans:
    for key, value in dic2.items():
      if key == m:
        count += value[1]
      else:
        count += value[0]
    ans[m] = count
  else:
    count = ans[m]
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

ansdic = {}

for a in dic:
    ans = 0
    for aa in dic:
        if a == aa:
            t = dic[aa] - 1
        else:
            t = dic[aa]

        if t > 1:
            ans += t * (t - 1) // 2
        
    ansdic[a] = ans

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

kcnt = []
c = Counter(a)
for j in c.values():
    kcnt.append(j*(j-1)//2)
for i in a:
    print(sum(kcnt)-(c[i]-1))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy

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

cnum = []
ans = 0

for k in range(1,n+1):
    cans = []
    dammy = copy.copy(a)
    del dammy[k-1]
    c = Counter(dammy)
    cnum = [c[i] for i in c if c[i] >=2]
    for j in cnum:
        cans.append(j*(j-1)//2)
    print(sum(cans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import copy

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

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

cnum = []
ans = 0

for k in range(1,n+1):
    cans = []
    dammy = copy.copy(a)
    del dammy[k-1]
    c = Counter(dammy)
    cnum = [c[i] for i in c if c[i] >=2]
    for j in cnum:
        cans.append(combinations_count(j, 2))
    print(sum(cans))

Submission status: Time Limit Exceeded

Solution code:
def combination(n,r):
    n_=1
    r_=1
    n_r=1
    for i in range(1,n+1):
        n_*=i
    for i in range(1,n-r+1):
        n_r*=i
    for i in range(1,r+1):
        r_*=i

    return n_//(n_r*r_)

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

    ball_dic={}

    for a in A:
        if a in ball_dic.keys():
            ball_dic[a]+=1
        else:
            ball_dic[a]=1


    all_patern=0

    for key in ball_dic.keys():
        if ball_dic[key]>=2:
            all_patern+=combination(ball_dic[key],2)

    for a in A:
        if ball_dic[a]-1<2:
            res=all_patern-combination(ball_dic[a],2)
        else:
            res=all_patern-combination(ball_dic[a],2)+combination(ball_dic[a]-1,2)

        print(res)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
mark1 = []
mark2 = []
for i in range(N):
    c[A[i]] = c[A[i]] - 1
    tmp,tmp2 = zip(*c.most_common())
    tmp2 = list(tmp2)
    if(tmp2 == mark1):
        print(str(ans1))
        if(tmp2 == mark2):
            print(str(ans2))
        else:
            for j in range(len(tmp2)):
                if(tmp2[j] <= 1):
                    tmp2[j] = 0
                else:
                    tmp2[j] = combinations_count(tmp2[j],2)
            ans2 = sum(tmp2)
            print(str(ans2))
            mark2 = tmp2
    else:
        for j in range(len(tmp2)):
            if(tmp2[j] <= 1):
                tmp2[j] = 0
            else:
                tmp2[j] = combinations_count(tmp2[j],2)
        ans1 = sum(tmp2)
        print(str(ans1))
        mark1 = tmp2
    c[A[i]] = c[A[i]] + 1

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

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

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

for i in range(n):
    ans = 0
    b = a.copy()
    del b[i]
    bb = collections.Counter(b)
    for j in bb:
        if bb[j] > 1:
            ans += com(bb[j],2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb

n = int(input())
a = list(map(int, input().split()))
l = [0] * (max(a))

for i in a:
    l[i-1] += 1
    #logging.debug(a)

ans = 0
for i in a:
    l[i-1] -= 1
    for j in l:
        ans += comb(j,2, exact=True)
    print(ans)
    ans = 0
    l[i-1] += 1

Submission status: Time Limit Exceeded

Solution code:
from math import comb

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

for k in range(0,N):
  new_arr = [arr[i] for i in range(0,len(arr)) if i!= k]
  set_new_arr = set(new_arr)
  
  identical_balls = 0
  
  for item in set_new_arr:
    identical_balls += comb(new_arr.count(item), 2)
    
  print(identical_balls)

Submission status: Time Limit Exceeded

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

for k in range(0,N):
  identical_balls = 0
  for i in range(0,N-1):
    for j in range(i+1, N):
      if arr[i] == arr[j] and i != k and j != k:
        identical_balls += 1
  print(identical_balls)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))        
D,d = [0]*(n+1),[0]*(n+1)
C = lambda x: x*(x-1)//2
s = 0
for i in set(A):
  D[i] = A.count(i)
  s += C(D[i])
for i in set(A):
  d[i] = s-D[i]+1
for i in A:
  print(d[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))        
D,d = {},{}
C = lambda x: x*(x-1)//2

for i in set(A):
  D[i] = A.count(i)
  
for i in set(A):
  res = 0
  for j,k in D.items():
    if i==j:
      res += C(k-1)
      continue
    res += C(k)  
  d[i] = res

for i in A:
  print(d[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))        
D = {}
C = lambda x: x*(x-1)//2

for i in set(A):
  D[i] = A.count(i)
  
for i in A:
  res = 0
  for j,k in D.items():
    if i==j:
      res += C(k-1)
      continue
    res += C(k)
  print(res)

Submission status: Time Limit Exceeded

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

cnt = 0
B = A.copy()
for i in range(N):
  A = B.copy()
  del A[i]
  a = list((it.combinations(A,2)))
  for j in range(len(a)):
    if a[j][0] == a[j][1]:
      cnt += 1
  print(cnt)
  cnt = 0

Submission status: Time Limit Exceeded

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

for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1

for i in range(n):
    d2 = d.copy()
    d2[a[i]] -= 1

    cnt = 0
    for value in d2.values():
        cnt += value*(value-1)//2

    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a = list(input().split())
c = Counter(a)
c = list(c.items())
x = []
y = []
for i in range(len(c)):
    x.append(c[i][0])
    y.append(c[i][1])
ans = 0
for i in range(len(c)):
    ans += y[i] * (y[i] - 1) // 2
for i in range(n):
    print(ans - (y[x.index(a[i])] - 1))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
x = []
y = []
for i in range(n):
    if a[i] in x:
        y[x.index(a[i])] += 1
    else:
        x.append(a[i])
        y.append(1)
ans = 0
for i in range(len(y)):
    if y[i] >= 2:
        ans += y[i] * (y[i] - 1) // 2
for i in range(n):
    print(ans - (y[x.index(a[i])] - 1))

Submission status: Time Limit Exceeded

Solution code:
import itertools
N=int(input())
A=list(map(int,input().split()))
B=[0]*(N+1)
C=0
for i in A:
    B[i-1]+=1
for i in B:
    C+=len(list(itertools.combinations(range(i),2)))
for i in A:
    print(C-B[i-1]+1)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import copy
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
for i in A:
   B[i-1]+=1 
def X (k):
    c=0
    C=copy.deepcopy(B)
    C[k-1]-=1
    for i in range(N):
        if C[i-1]>=2:
            c+=len(list(combinations(range(C[i-1]),2)))
    return c
for j in A:
    print(X(j))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from math import factorial


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


n = int(input())
A = list(map(int, input().split()))
m = 0
c = Counter(A)
d = Counter([])
for k, v in c.items():
    co = combinations_count(v, 2)
    d[k] = co
    m += co
# print(c)
# print(d)
for a in A:
    print(m-d[a]+combinations_count(c[a]-1, 2))

Submission status: Time Limit Exceeded

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

# カウントして、同じ数のpermutaionを取ればよい
import collections
import math
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

# def combinations_with_replacement_count(n, r):
#     return combinations_count(n + r - 1, r)


# def permutations_count(n, r):
#     return math.factorial(n) // math.factorial(n - r)

c = collections.Counter(A)

# print(c)
# print(len(c))

# 1をマイナスしつつ、各数字の(x-1)C2を取る
pl = {}
# xC2
ql = {}
for k, v in c.items():
    # print(k, v)
    if v >= 3:
        pl[k] = combinations_count(v - 1, 2)
        ql[k] = combinations_count(v, 2)
    elif v == 2:
        pl[k] = 0
        ql[k] = 1
    else:
        pl[k] = 0
        ql[k] = 0
# print('pl', pl)
# print('ql', ql)

for a in A:
    t = 0
    for k, v in c.items():
        if a == k:
            t += pl[k]
        else:
            t += ql[k]
    print(t)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += B[i]*(B[i]-1)//2

for k in range(N):
    print(S-B[A[k]]+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += int(B[i]*(B[i]-1)/2)

for i in range(N):
    print(S-B[A[i]]+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [0]*(N+1)
S = 0

for i in range(1, N+1):
    B[i] = A.count(i)
    S += int(B[i]*(B[i]-1)/2)

for i in range(N):
    if B[A[i]] < 2:
        print(S)
    else:
        print(S-B[A[i]]+1)

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb
from collections import Counter

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

count = Counter(A)
combs, all = {}, 0
for k in count.keys():
    c = comb(count[k], 2)
    combs[k] = c
    all += c

for a in A:
    ans = all - combs[a] + comb(count[a]-1,2)
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb
from collections import Counter

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

count = Counter(A)
combs = {k:comb(count[k], 2) for k in count.keys()}
all = sum(combs.values())

for a in A:
    ans = all - combs[a] + comb(count[a]-1,2)
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
for i in range(N):
    ans = 0
    B = A[:i] + A[i+1:]
    C = collections.Counter(B)
    for j in set(B):
        D = C[j]
        if D > 1:
            ans += combinations_count(D,2)
    print(ans)

Submission status: Time Limit Exceeded

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

from operator import mul
from functools import reduce

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

a_c = Counter(A)
for i in range(n):
    tmp = 0
    for k,v in a_c.items():
        if k==A[i]:
            v-=1
        if v>=2:
            tmp+=cmb(v,2)
    print(tmp)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def count(A):
    cnt = 0
    for a in A:
        cnt += (a*(a-1))//2
    return cnt


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

    max_A = max(A)

    count_A = [0]*(max_A+1)

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

    for a in A:
        cal_A = deepcopy(count_A)
        cal_A[a] -= 1
        print(count(cal_A))

Submission status: Time Limit Exceeded

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


n = int(input())
a = list(map(int, input().split()))
arr = [0] * (2 * 10 ** 5 + 1)

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


for i in a:
    arr[i] -= 1
    ans = 0
    for j in arr:
        if j > 1:
            ans += nC2(j)
    arr[i] += 1

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from operator import mul
from functools import reduce


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


n = int(input())
a = list(map(int, input().split()))
arr = [0] * (2 * 10 ** 5 + 1)

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

for i in a:
    arr[i] -= 1
    ans = 0
    for j in arr:
        if j > 1:
            ans += combinations_count(j, 2)
    arr[i] += 1

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
 
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    acopy = a.copy()
    del acopy[i]
    c = collections.Counter(acopy)
    itm = c.values()
    ans = 0
    for j in itm:
        ans += j*(j-1)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
T = tuple(map(int, input().split()))
C = Counter(T)
C = C.most_common()
for t in T:
  ans = 0
  for c in C:
    if c[1] == 1:
      break
    if c[0] == t:
      ans += ((c[1] - 1) * (c[1] - 2)) // 2
    else:
      ans += (c[1] * (c[1] - 1)) // 2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))
dict={}
if len(set(a))==1:
  [print((n-1)*(n-2)//2) for _ in range(n)]
  exit()
for i in range(n):
  check=a[:i]+a[i+1:]
  if len(set(check))>1:
    for j in check:
      if j not in dict:
        dict[j] = 1
      else:
        dict[j] += 1
    if set(dict.values())=={1}:
      print(0)
    else:
      if len(dict.values())<=2:
        print(sum(set([i[1] for i in sorted(dict.items(),key=lambda x:-x[1]) if i[1]>1])))
      else:
        print(sum(set([i[1] for i in sorted(dict.items(),key=lambda x:-x[1])])))
  dict={}