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:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
A = LI()

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

def cnt_ans(ball_sets):
    cnt = 0
    for ball_set in ball_sets:
        if ball_set > 1:
            cnt += ball_set * (ball_set - 1) // 2
        
    return cnt
    
for i in range(N):
    ball_dic_c = copy.deepcopy(ball_dic)
    ball_dic_c[A[i]] -= 1
    ball_sets = ball_dic_c.values()
    print(cnt_ans(ball_sets))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(A)

def count(c, x):
    c[x] -= 1
    ret = 0
    for v in c.values():
        ret += v*(v-1)//2
    return ret

N = len(A)
for i in range(N):
    print(count(c.copy(), A[i]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import collections

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

    balls_dict = collections.Counter(balls)

    for ball in balls:
        answer = 0
        for num, quantity in balls_dict.items():
            if num == ball:
                answer += (quantity-2) * (quantity-1) // 2
            else:
                answer += quantity * (quantity-1) // 2

        print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np


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


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

    dic = {}
    Ans = 0
    for l in List:
        n = count[l]
        num = n * (n - 1) // 2
        Ans += num
        dic[l] = num

    for i in range(N):
        n = count[A[i]] - 1
        num = n * (n - 1) // 2
        answer = Ans - dic[A[i]] + num
        print(answer)


main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list = list(map(int,input().split()))
kosuu = []
total = 0
for i in range(N):
    a = list.count(i+1)
    kosuu.append(a)
    total += ( a * (a-1) ) //2
for i in range(N):
    a = kosuu[list[i]-1]
    print(total-a*(a-1)//2 + (a-1)*(a-2)//2)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
a = LIST()
b = Counter(a)
c = list(set(a))
for k in range(n):
    ans = 0
    for i in c:
        tmp = b[i]
        if i == a[k]:
            tmp -= 1
        if tmp >= 2:
            ans += tmp * (tmp - 1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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


for k in range(N):
    t = A[k]

    dic[t] -= 1

    cnt = 0

    for key, val in dic.items():
        if val < 2:
            continue

        cnt += int((val * (val - 1)) / 2)

    print(cnt)
    dic[t] += 1

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int, input().split()))
freq_arr={}
ans=0
def foo(x):
  return (x*(x-1))//2
for i in set(l):
  freq_arr[i]=(l.count(i))
for i in freq_arr:
  ans+=foo(freq_arr[i])
#print(ans)
ans_arr={}
for i in freq_arr:
    ans_arr[i]=(ans-freq_arr[i]+1)
for i in l:
    print(ans_arr[i])

Submission status: Time Limit Exceeded

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

import collections

c = collections.Counter(A)

ans = []
for a in A:
  ans_a = 0
  for k, v in c.items():
    if k == a:
      ans_a += max(v - 1, 0) * max(v - 2, 0) // 2
    else:
      ans_a += v * max(v - 1, 0) // 2
  ans.append(ans_a)
print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    A = tuple(iim())

    d1 = dict()
    for ai in A:
        if ai in d1:
            d1[ai] += 1
        else:
            d1[ai] = 1

    aa = []
    items = d1.items()
    for ak in A:
        ans = 0
        for k, v in items:
            if k == ak:
                v -= 1
            if v > 1:
                ans += v * (v-1) // 2
        aa.append(ans)
    print(*aa, sep="\n")


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import copy

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

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

for i, a in enumerate(A):
    c = copy.deepcopy(C)
    c[a] -= 1
    sum_ = sum([ n * (n - 1) // 2 for _, n in c.items() if n > 1])
    print(sum_)

Submission status: Time Limit Exceeded

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res
# Summarize count of factor within list --- END ---

# nCr -- START --
def nCr(n,r):
  if n<r:
    return 0
  return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
# nCr --- END ---

def main():
  n=I()
  l=LI()
  mp=[0]*(n+1)

  sl=summarize_list(l)
  # print(sl)

  ans=0
  for x,c in sl:
    mp[x]=c
    ans+=nCr(c,2)

  # print(mp)
  # print(ans)

  for x in l:
    print(ans-nCr(mp[x],2)+nCr(mp[x]-1,2))

main()
# print(main())

Submission status: Time Limit Exceeded

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

from collections import Counter
c = Counter(a)

answers=dict()
for k in range(n):
    ak = a[k]
    if ak in answers:
        ans = answers[ak]
    else:
        temp = [c[cc]-(ak==cc) for cc in c]
        ans = sum(t*(t-1)//2 for t in temp)
        answers[ak]=ans
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

#####

def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs


def main(inputs):
	n=int(inputs[0][0])
	aa=getIntList(inputs[1])
	
	count=Counter(aa)
	
# 	print(n)
# 	print(aa)
# 	print(count0)
	
	ans={}
	pat={}
	for k in range(n):
		targetA=aa[k]
		if targetA in ans:
			print(ans[targetA])
			continue
		
		count[targetA]-=1
		n=0
		for ai,c in count.items():
			if c in pat:
				ni=pat[c]
			else:
				ni=c*(c-1)//2
				pat[c]=ni
			n+=ni
		ans[targetA]=n
		print(n)
		count[targetA]+=1
		
	

if __name__=="__main__":
	inputs=getInputs()
# 	inputs=simInputs()
	main(inputs)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from collections import Counter

def kumi(d):
    ans = 0
    for k, v in d.items():
        ans += v * max(0, v-1) // 2
    return ans

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    d = dict(Counter(v))
    for i in v:
        d[i] -= 1
        print(kumi(d))
        d[i] += 1


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list = list(map(int,input().split()))
kosuu = []
total = 0
for i in range(N):
    a = list.count(i+1)
    kosuu.append(a)
    total += ( a * (a-1) ) //2
for i in range(N):
    a = kosuu[list[i]-1]
    print(total-a*(a-1)//2 + (a-1)*(a-2)//2)

Submission status: Time Limit Exceeded

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

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

count = Counter(A)

for num in A:
    ans = 0
    for key, value in count.items():
        if key != num:
            ans += value*(value-1)//2
        else:
            ans += (value-1)*(value-2)//2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
for i in range(n):
  d = c.copy()
  num = li[i]
  d[num] -= 1
  ans = 0
  for v in d.values():
    v = int(v)
    ans += v*(v-1)//2
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter
setrecursionlimit(1000000)

# INF = float('inf')
INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    cA = Counter(A)

    for ai in A:
        cA[ai] -= 1
        count = 0
        for key in cA:
            v = cA[key]
            count += (v * (v - 1)) // 2
        cA[ai] += 1
        print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # queue
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n = int(input())
    a = list(map(int,input().split()))
    mp = {}
    for i in range(n):
        if a[i] in mp.keys():
            mp[a[i]] += 1
        else:
            mp[a[i]] = 1
    for i in range(n):
        ans = 0
        for k in mp.keys():
            x = mp[k]
            if k==a[i]:
                x -= 1
            ans += x * (x - 1) // 2
        print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

counts = {}

for a in A:
    if a not in counts:
        counts[a] = 1
    else:
        counts[a] += 1

for a in A:
    ans = 0
    counts[a] -= 1

    for k, v in counts.items():
        if v >= 2:
            ans += v * (v - 1) // 2
    counts[a] += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def conb(n,r): 
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = inp()
a = inpl()
c = Counter(a)
res = 0
d = dict()
for key in list(c):
    # print(c[key])
    if c[key] > 1:
        tmp = conb(c[key], 2)
        res += tmp
        d[key] = tmp
    else:
        d[key] = 0
for i,t in enumerate(a):
    if d[t] == 0:
        print(res)
        continue
    if c[t]-1 > 1:
        tmp = conb(c[t]-1,2)
    else:
        tmp = 0
    print(tmp + res - d[t])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from collections import Counter

def kumi(d):
    ans = 0
    for k, v in d.items():
        ans += v * max(0, v-1) // 2
    return ans

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    d = dict(Counter(v))
    for i in v:
        t = dict(d)
        t[i] -= 1
        print(kumi(t))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

setA = set(A)
counts = {}
for e in setA:
    counts[e] = A.count(e)

ans = 0
for e in setA:
    ans += counts[e] * (counts[e]-1) // 2

for i in range(0, len(A)):
    e = A[i]
    print(ans - (counts[e] * (counts[e]-1) // 2) +
          ((counts[e]-1) * (counts[e]-2) // 2))

Submission status: Time Limit Exceeded

Solution code:

from collections import Counter

#####

def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs


def main(inputs):
	n=int(inputs[0][0])
	aa=getIntList(inputs[1])
	
	count0=Counter(aa)
	
# 	print(n)
# 	print(aa)
# 	print(count0)
	
	ans={}
	for k in range(n):
		targetA=aa[k]
		if targetA in ans:
			print(ans[targetA])
			continue
		
		count=count0.copy()
		count[targetA]-=1
		n=0
		for ai,c in count.items():
			n+=c*(c-1)//2
		ans[targetA]=n
		print(n)
		
	

if __name__=="__main__":
	inputs=getInputs()
# 	inputs=simInputs()
	main(inputs)

Submission status: Time Limit Exceeded

Solution code:
# list(map(int,input().split()))
from math import factorial as f
from collections import Counter


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


def solve(cnt):
	ans = sum(map(c, cnt.values()))
	print(ans)


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

cnt = Counter(arr)
for i in range(n):
	a = arr[i]
	cnt[a] -= 1
	solve(cnt)
	cnt[a] += 1

Submission status: Time Limit Exceeded

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

for ai in a:
  if not a_pop.get(ai):
    a_pop[ai] = 1
  else:
    a_pop[ai] += 1

for ai in a:
  ans = 0
  for k, v in a_pop.items():
    if k == ai:
      if v > 2:
        ans += (v - 1) * (v - 2) // 2
    else:
      if v > 1:
        ans += v * (v - 1) // 2
  print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int, input().split()))
freq_arr={}
ans=0
def foo(x):
  return (x*(x-1))//2
for i in set(l):
  freq_arr[i]=(l.count(i))
for i in freq_arr:
  ans+=foo(freq_arr[i])
for i in range(n):
  x=freq_arr[l[i]]
  print(ans-x+1)

Submission status: Time Limit Exceeded

Solution code:
import collections
import copy

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

for a in A:
    pC = copy.deepcopy(C)

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

Submission status: Time Limit Exceeded

Solution code:
import math
def cmb(n, r):
    if n==0:
        return 0

    if n==1 and r==2:
        return 0
    if n==2 and r==2:
        return 1

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


def main():
    import sys
    from collections import defaultdict

    N=int(sys.stdin.readline())
    AL=list(map(int,sys.stdin.readline().split()))
    dic=defaultdict(int)
    for a in AL:
        dic[a]+=1


    K=[cmb(v,2) for k,v in dic.items()]
    total=sum(K)

    for i in range(N):
        a=dic[AL[i]]
        print(total-cmb(a,2)+cmb(a-1,2))

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict

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

d = defaultdict(int)
for i in A :
    d[i] += 1

for i in A :
    d[i] -= 1
    ans = 0
    for a in d :
        ans += d.get(a)*(d.get(a)-1)//2
    print(ans)
    d[i] += 1

Submission status: Time Limit Exceeded

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

for i in range(n):
    r = 0
    for x, y in c:
        if x == a[i]:
            r += max(((y-1)*(y-2)//2), 0)
        else:
            r += (y-1)*y //2
    print(r)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


counter = {}
for number in A:
    if number not in counter:
        counter[number] = 1
    else:
        counter[number] += 1

answer = [0] * N

for i, number in enumerate(A):
    tmp = 0
    for k, c in counter.items():
        if k == number:
            c -= 1
        tmp += c * (c-1) // 2
    answer[i] = str(tmp)

print('\n'.join(answer))

Submission status: Time Limit Exceeded

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

for i in range(0, len(A)):
    B = A[:i] + A[i+1:]
    ans = 0
    for e in set(B):
        l = B.count(e)
        ans += l * (l-1) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    def comb(n, k):
        a = n * (n - 1)
        return a // 2
    n = int(input())
    a = list(map(int, input().split()))
    count = {}
    for i in a:
        count[i] = count.get(i, 0) + 1

    for i in a:
        ans = 0
        for k, v in count.items():
            if k == i:
                ans += comb(v - 1, 2)
            else:
                ans += comb(v, 2)
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import defaultdict
    N = int(input())
    *a, = map(int, input().split())

    d = defaultdict(int)
    for x in a:
        d[x] += 1

    for x in a:
        t = 0
        d[x] -= 1

        for key, value in d.items():
            t += value * (value - 1) // 2

        d[x] += 1

        print(t)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(v) for v in input().split()]

field = {}
for v in a:
    if not v in field:
        field[v] = 1
    else:
        field[v] += 1

for i in a:
    if i in field:
        field[i] -= 1
    
    ans = 0
    for k,v in field.items():
        ans += ((v*(v-1))/2)

    print(int(ans))

    if i in field:
        field[i] += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N=int(input())
A=[int(i) for i in input().split(" ")]
c=Counter(A)
c=dict(c)

for a in A:
    c[a]-=1
    retval=0
    for key in c:
        retval+=c[key]*(c[key]-1)//2
    print(retval)
    c[a]+=1

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
lst = [0]*(max(a)+1)

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

num_lst = [0]*(max(a)+1)
for i in range(1,max(a)+1):
    if not lst[i] in [0,1]:
        num_lst[i] = lst[i]*(lst[i]-1)//2
ans = sum(num_lst)

for i in a:
    if lst[i] in [0,1]:
        print(ans)
    else:
        print(ans-num_lst[i]+((lst[i]-1)*(lst[i]-2)//2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=[0 for i in range(N+1)]
C=[0 for i in range(N+1)]
D=[0 for i in range(N+1)]
for i in range(N):
    B[A[i]]+=1
for i in range(1,N+1):
    C[i]=B[i]*(B[i]-1)//2
sum_=sum(C)
for i in range(1,N+1):
    print(sum_-B[A[i-1]]*(B[A[i-1]]-1)//2+(B[A[i-1]]-2)*(B[A[i-1]]-1)//2)

Submission status: Accepted

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

D = collections.Counter(A)
s=0
for i in D:
  s+=D[i]*(D[i]-1)//2
  
for i in range(N):
  print(s-(D[A[i]]-1))

Submission status: Accepted

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

c = collections.Counter(A)
ans = 0
for i in c:
  ans+=c[i]*(c[i]-1)//2

for i in range(N):
  print(ans-(c[A[i]]-1))

Submission status: Accepted

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


c = Counter(A)

ans = 0
for i in c:
    ans += c[i] * (c[i] - 1) // 2

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

Submission status: Accepted

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())

n=int(input())
from collections import Counter
ar=lis()
c=Counter(ar)
s=0
for i in c.values():
    s+=((i*(i-1))//2)
for i in ar:
    print(s-c[i]+1)

Submission status: Accepted

Solution code:
def nC2(n):
    if n <= 1:
        return 0
    return n * (n - 1) // 2

N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x: x - 1, A))

A_count = [0] * N
for a in A:
    A_count[a] += 1

combis = [0] * N
for i, a in enumerate(A_count):
    combis[i] = nC2(a)

combis_m1 = [0] * N
for i, a in enumerate(A_count):
    combis_m1[i] = nC2(a-1)

total_sum = sum(combis)
for a in A:
    print(total_sum - combis[a] + combis_m1[a])

Submission status: Accepted

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()))
c=Counter(a)
com=0
for i in range(1,n+1):
    if(c[i]<=1):
        continue
    com+=combinations_count(c[i],2)
for k in range(1,n+1):
    ans=com
    ans-=c[a[k-1]]-1
    print(ans)

Submission status: Accepted

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()))
c=Counter(a)
com=0
for i in range(1,n+1):
    if(c[i]<=1):
        continue
    com+=combinations_count(c[i],2)
for k in range(1,n+1):
    ans=com
    ans-=c[a[k-1]]-1
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
for i in c:
  cnt += c[i]*(c[i]-1)//2



for i in range(n):
	print(cnt-c[a[i]]+1)

Submission status: Accepted

Solution code:
import collections
n = int(input())
num_list = list(map(int, input().split()))
all = 0

c = collections.Counter(num_list)

for i in c:
    all += c[i]*(c[i]-1)//2

for k in range(n):
    print(all - c[num_list[k]] + 1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)
ans = 0
for i in K:
    ans += C[i]*(C[i]-1)//2

for i in A:
    print(ans-(C[i]-1)) # nシグマk = n(n-1)/2 ......

Submission status: Accepted

Solution code:
n = int(input())
lst = [ int(i) for i in input().split() ]

visited = {}

for d in lst:
  if d in visited.keys():
    visited[d] += 1
  else:
    visited[d] = 1
count = 0
for d in visited.keys():
  count += int(visited[d]*(visited[d]-1)/2)
for d in lst:
  v = visited[d] - 1
  ans = count - v
  print(ans)

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for i in range(N):
        dd[A[i]]+=1
    
    ans=0
    for k,v in dd.items():
        ans+=(v*(v-1))//2
        
    for i in range(N):
        a=A[i]
        v=dd[A[i]]
        temp=ans
        temp-=(v*(v-1))//2
        temp+=((v-1)*(v-2))//2
        
        print(temp)
    
main()

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a_array = list(map(int, input().split()))
a_counter = Counter(a_array)
count_max = max(a_counter.values())

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

nc2_array = [0] * (count_max + 1)
sum_comb = 0
for a, count in a_counter.items():
    comb = nC2(count)
    sum_comb += comb
    nc2_array[count] = comb
    nc2_array[count - 1] = nC2(count - 1)

for a in a_array:
    a_count = a_counter[a]
    ans = sum_comb - nc2_array[a_count] + nc2_array[a_count - 1]
    print(ans)

Submission status: Accepted

Solution code:
import collections
def resolve():
    n = int(input())
    a = tuple(map(int,input().split()))
    A = collections.Counter(a)
    cnt_0 = 0
    for i in A.values():
        cnt_0 += int(i*(i-1)/2)
    for i in a:
        cnt_1 = A[i]-1
        print(cnt_0-cnt_1)
resolve()

Submission status: Accepted

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


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


for i in range(N):
    try:
        count_dict[As[i]] += 1
    except KeyError:
        count_dict[As[i]] = 1

combo = 0
for key in count_dict:
    combo += calc_combo(count_dict[key])

for i in range(N):
    num_delete = As[i]
    try:
        num_count = count_dict[num_delete]
        out = combo - calc_combo(num_count) + calc_combo(num_count - 1)
        print(out)
    except KeyError:
        print(combo)

Submission status: Accepted

Solution code:
n=(int)(input())
a=input().split()
for i in range(n):
  a[i]=(int)(a[i])
  
dic={}
sum=0
for i in range(n):
  if not a[i] in dic:
    dic[a[i]]=1
  else:
    dic[a[i]]+=1
    sum+=dic[a[i]]-1

for i in a:
  print(sum-dic[i]+1)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
x=[0]*n
for i in range(n):
  x[l[i]-1]+=1
d=0
for i in range(n):
  d=d+x[i]*(x[i]-1)//2

for i in range(n):
  b=d-(x[l[i]-1]-1)
  print(b)

Submission status: Accepted

Solution code:
import collections

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

cmb = 0
for x in c.values():
    cmb += x * (x - 1) // 2
    
for z in a:
    y = c.get(z)
    before = y * (y - 1) // 2
    after = (y - 1) * (y - 2) //2
    ans = cmb - before + after
    print(ans)

Submission status: Accepted

Solution code:
import collections
import math

def cmb_factorial(s):
    return math.factorial(s) // (math.factorial(2) * math.factorial(s - 2))

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
f = {}

cmb = 0
for x in c.values():
    f[1] = 0
    f[2] = 1
    if x >= 3:
        f[x] = cmb_factorial(x)
        if x - 1 not in f:
            f[x - 1] = cmb_factorial(x - 1)
    cmb += f.get(x)

for z in a:
    y = c.get(z)
    ans = cmb
    if y >= 2:
        ans = cmb - f.get(y) + f.get(y - 1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import *
from math import *
N = int(input())
A = list(map(int,input().split()))
C = Counter(A)
S = 0

for k,v in C.items():
  S+=comb(v,2)

for a in A:
  print(S-(comb(C[a],2)-comb(C[a]-1,2)))

Submission status: Accepted

Solution code:
N = int(input())
Alst = list(map(int, input().split()))
Blst = [0]*(N+1)
num = 0
for i in Alst:
    num = num + Blst[i]
    Blst[i] += 1

for i in Alst:
    k = Blst[i] -1
    print(num - k)

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

def main():
    n = i_input()
    a = i_list()

    k = [0]
    for i in range(2*10**5):
        k.append(k[i]+(i+1))
    t = a.copy()
    t.sort()

    s = 0

    m = [0]*n

    for i,n in itertools.groupby(t):
        l = len(list(n))
        s += k[l-1]
        if l > 1:
            m[i-1] = k[l-2] - k[l-1]

    for i in a:
        print(s + m[i-1])







if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
""" スニペット """
def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split()))
""" スニペット """
import math
# 2つ選ぶ通り数を算出 nC2
def choose2(n):
    return math.floor(n*(n-1)/2)

# インプット
N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N+1)
# ユニーク数の配列を求める
for i in range(N):
    uni[An[i]] += 1
sumWay = 0
# 各数値の2通りの選択通り数を足していく ここのindex=0がいらない
for i in range(N+1):
    sumWay += choose2(uni[i])
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print(sumWay - choose2(uni[An[i]]) + choose2(uni[An[i]]-1))

Submission status: Accepted

Solution code:
""" スニペット """
def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split()))
""" スニペット """
import math
# 2つ選ぶ通り数を算出 nC2
def choose2(n):
    return n*(n-1)//2
# インプット
N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N+1)
# ユニーク数の配列を求める
for i in range(N):
    uni[An[i]] += 1
sumWay = 0
con = [0] * (N+1)	#con[i] := iC2
for i in range(N+1):
  con[i] = choose2(i)
# 各数値の2通りの選択通り数を足していく
"""
for i in range(N + 1):
    sumWay += con(uni[i])
"""
#↑を書きかえてみた
for i in uni:
  sumWay += con[i]
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print(sumWay - con[uni[An[i]]] + con[uni[An[i]]-1])