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: Accepted

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

Anum = [0] * N
for i in A:
    Anum[i - 1] += 1

cnt = 0
for i in Anum:
    cnt += i * (i - 1) // 2

for k in range(N):
    ans = cnt - (Anum[A[k] - 1] - 1)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

for i in a:
  print(ans-c[i]+1)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

for i in a:
  print(ans-c[i]+1)

Submission status: Accepted

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

count = [0] * (N+1)
for aa in a:
    count[aa] += 1

su = 0
for ct in count:
    su += ct*(ct-1)/2
    
for aa in a:
    print(int(su-count[aa]+1))

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = [int(i) for i in input().split()]
di = defaultdict(int)
for a in A:
    di[a] += 1
ans = sum([i*(i-1)//2 for i in di.values()])

for a in A:
    print(ans-(di[a]-1))

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = [0]*a
for i in range(a):
    c[b[i]-1] += 1
s = sum([i*(i-1)//2 for i in c])
for i in b:
    print(s-(c[i-1]-1))

Submission status: Accepted

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

def choose2(n):
    if n >= 2:
        return (n * (n - 1)) // 2
    else:
        return 0

count = [0] * (n + 1)
for i in range(n):
    count[a[i]] += 1
total = 0
for i in range(n + 1):
    total += choose2(count[i])
for k in range(n):
    print(total - (choose2(count[a[k]]) - choose2(count[a[k]] - 1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


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

c = Counter(arr)
c_ = {k: v*(v-1)//2 for k, v in c.items()}
s = sum(c_.values())

for a in arr:
    tmp = c[a] - 1
    ans = s - c_[a] + tmp*(tmp-1)//2
    print(ans)

Submission status: Accepted

Solution code:
import collections 
from collections import Counter

n = input()
*l, = map(int, input().split())
c = Counter(l)

A = [int(i*(i-1)/2) for i in c.values()]
A_sum = sum(A)

for j in l:
	print(A_sum - c[j] + 1)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
d = {}
s = set()
for i in l:
    s.add(i)
    if i in d:
        d[i] = d[i]+1

    else:
        d[i] = 1

som = 0
for i in s:
    x = d[i]
    if x >= 2:
        w = x*(x-1)
        w = w//2
        som = som + w

for i in l:
    x = d[i]
    if x > 2:
        f = x*(x-1)
        f = f//2
        x = x-1
        w = x*(x-1)
        w = w//2
        re = som - f + w
        print(re)
        continue

    if x == 2:
        re = som - 1
        print(re)
        continue

    print(som)

Submission status: Accepted

Solution code:
from collections import Counter as f
n=input()
*l,=map(int,input().split())
A=f(l)
c=sum([i*(i-1)//2 for i in A.values()])
for j in l:
  print(c-A[j]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
a = [0] * N
for i in A:
    a[i-1] += 1
cnt = 0
cnt += sum([i*(i-1)//2 for i in a])
for i in A:
    print(cnt - (a[i-1]-1))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
B = [int(x) for x in input().split()]
A=Counter(B)
V, C = zip(*A.most_common())
count=0
i=0
dict={}
for x in C:
  count+=x*(x-1)/2
  dict[V[i]]=x
  i+=1
for x in B:
  ans=count-dict[x]+1
  print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
B = [int(x) for x in input().split()]
A=Counter(B)
V, C = zip(*A.most_common())
count=0
for x in C:
  if x==0:
    break
  count+=x*(x-1)/2
for x in B:
  ans=count-A[x]+1
  print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

dic = dict()
for i in A:
    if i not in dic.keys():
        dic[i] = 1
    else:
        dic[i] += 1
#print(dic)

for j in dic.values():
    if j >= 2:
        ans += (j*(j-1))//2

    #print(ans)

for k in A:
    yo = dic[k]
    if yo >= 3:
        ans_k = ans - (yo*(yo-1))//2 + ((yo-1)*(yo-2))//2
    elif yo == 2:
        ans_k = ans -(yo*(yo-1))//2
    else:
        ans_k = ans
        pass
    print(ans_k)

Submission status: Accepted

Solution code:
import math


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


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

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

b = 0
for n in d.values():
    b += comb(n, 2)

for k in range(N):
    a = A[k]
    print(b - d[a] + 1)

Submission status: Accepted

Solution code:
mod = 10**9 + 7

def main():
    N = iip(listed=False)
    A = iip(True)
    #print(X, Y)

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

    defres = 0
    for i in num:
        if i <= 1:
            continue
        defres += i*(i-1)//2

    result = []
    for k in A:
        nk = num[k]
        if nk <= 1:
            result.append(defres)
            continue
        diff = nk*(nk-1)//2 - (nk-1)*(nk-2)//2
        result.append(defres - diff)

    print("\n".join([str(i) for i in result]))


#####################################################ライブラリ集ここから

def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        #print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for a in a_s:
  count_a[a] += 1

choices = sum(map(lambda x: x*(x-1)//2, count_a))

for a in a_s:
  print(choices - (count_a[a]-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

num_count = collections.Counter(a)
total = sum([num*(num-1)//2 for num in num_count.values()])

for i in range(n):
    print(total-num_count[a[i]]+1)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a_list = list(map(int, input().split()))
c = collections.Counter(a_list)
ans = 0
for i in c.values():
    ans += i*(i-1)//2
for i in range(n):
    print(ans - (c[a_list[i]]-1))

Submission status: Accepted

Solution code:
import collections


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

    A_count = collections.Counter(A)
    conv_all = sum([(A_c_i * (A_c_i-1))//2 for A_c_i in A_count.values()])

    for i in range(n):
        print(conv_all - A_count[A[i]] + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def f(n):
  return (n*(n-1))//2
N=int(input())
A=list(map(int,input().split()))
d=dict()
ans=0
#それぞれが何個あるかを辞書に登録
for i in range(len(A)):
  if A[i] not in d:
    d[A[i]]=0
  d[A[i]]+=1
for i in d:
  ans+=f(d[i])
for i in range(len(A)):
  print(ans-d[A[i]]+1)

Submission status: Accepted

Solution code:
#TLE
n = int(input())
a = input().split()
ans = 0
import collections
dic = collections.Counter(a)
for i in dic.values():
    ans += i * (i-1)
ans /= 2
for i in range(n):
    print(int(ans - dic[a[i]] + 1))

Submission status: Accepted

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

for a in a_s:
  count_a[a] += 1

choices = sum(map(lambda x: x*(x-1)//2, count_a))

for a in a_s:
  print(choices - (count_a[a]-1))

Submission status: Accepted

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

#dの中に、ある整数に対し何個のボールがあるかの情報を格納。
d={}
for i in l:
  if i in d:
    d[i]+=1
  else:
    d[i]=1

#全部のボールを使えるときの数を計算。同時に、整数iが一つ減った時の値を計算。
all = 0
decrease = {}
for k, v in d.items():
  all+=v*(v-1)//2
  decrease[k] = v-1

#ループして出力
for i in l:
  print(all-decrease[i])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
dict_ = {}
for i in range(n):
    if a[i] in dict_:
        dict_[a[i]] += 1
    else:
        dict_[a[i]] = 1
ways = 0
for key, value in dict_.items():
    if value >= 2:
        ways += value * (value - 1) // 2
for i in range(n):
    print(ways - dict_[a[i]] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

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

# 先にk番目を抜かない場合を求めておく
ans = 0
for n in c.values():
    ans += n*(n-1)//2

# k番目を抜く場合の差分を加える
for k in range(N):
    m = c[A[k]]
    print(ans - (m-1))  # -m*(m-1)//2 + (m-1)*(m-2)//2

Submission status: Accepted

Solution code:
import collections
N=int(input())
lst=input().split()
s=0
dic=collections.Counter(lst)
for i in dic.values():
    s+=(i*i-i)//2
for i in lst:
    print(s+1-dic[i])

Submission status: Accepted

Solution code:
#ABC159D
import sys, math
from collections import Counter

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

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

total = 0
L = Counter(A)
for v in L.values():
    if v>=2:
        total += comb(v)

for k in range(N):
    print(total - (L[A[k]]-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input());

a = [int(i) for i in input().split()];
b = [int(0) for i in range(n)];

for i in a:
  b[i - 1] += 1;

#print(a);
#print(b);

sum = 0;
for i in b:
  sum += i * (i - 1) // 2;
#print(sum);
for i in a:
  print(sum - b[i - 1] + 1);

Submission status: Accepted

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

dic = dict()
for a in A:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1

base = 0

for v in dic.values():
    base += v * (v - 1) // 2

for i in range(N):
    ord = dic[A[i]]
    print(base - (ord * (ord - 1) // 2) + ((ord - 1) * (ord - 2) // 2))

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
d=collections.Counter(a)
s=0
#print(d)
for i in d:
    s=s+d[i]*(d[i]-1)//2
#print(s)
for i in range(n):
    print(s-(d[a[i]]*(d[a[i]]-1))//2+(max(d[a[i]]-1,0))*(max(d[a[i]]-2,0))//2)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(x) for x in input().split()]
m=[0]*(200007)
for x in l:
    m[x]=m[x]+1

total=0
for x in m:
    total+=(x*(x-1))//2

for x in l:
    print(total-m[x]+1)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = [0]*n
for a in A:
    cnt[a-1] += 1
ans = 0
for i in range(n):
    ans += cnt[i] * (cnt[i]-1) // 2
for a in A:
    print(ans-cnt[a-1]+1)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))

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

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

# all
ret = 0
for key in dict.keys():
  val = dict[key]
  ret += comb(val)

# i >= 0
for i in range(N):
  val_n = dict[A[i]]
  next_ret = ret - comb(val_n) + comb(val_n - 1)
  print(next_ret)

Submission status: Accepted

Solution code:

import collections

# 入力 : N
# 入力 : A1 A2 ... AN
N = int(input())
A = list(map(int, input().split()))

C = collections.Counter(A)

Total = sum([i*(i-1)/2 for i in C.values()])

for Ak in A:
    print(int(Total - C[Ak]+1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

N=int(input())  
a=list(map(int,input().split()))
b= collections.Counter(a)
c= b.most_common()

L=len(b)

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

for i in a:
    ans = total - (b[i]-1)
    print(ans)

Submission status: Accepted

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

d = collections.Counter(A)
total = 0
for i in d.values():
    n = i*(i-1)//2
    total+=n

for m in A:
    a= total - (d[m]*(d[m]-1))//2 +((d[m]-1)* (d[m]-2))//2
    print(a)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
for key in a:
  d[key] = d.get(key, 0) + 1
sum = 0
for key in d:
  sum += d[key]*(d[key]-1)//2
for i in range(n):
  print(sum - d[a[i]] + 1)

Submission status: Accepted

Solution code:
import collections

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

a=collections.Counter(A)

total=0
for v in a.values():
    if v>1:
        c=(v*(v-1))//2
        total+=c

for i in A:
    ans=total-(a[i]*(a[i]-1))//2+((a[i]-1)*(a[i]-2))//2
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

cnt = collections.Counter(num_list)
ttl = 0
for v in cnt.values():
    ttl += v*(v-1)//2
for a in num_list:
    print(ttl - (cnt[a] - 1))

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = iin()
A = iln()
cnt = collections.Counter(A)

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

total = 0
for value in cnt.values():
    if value >= 2:
        total += c2(value)

for i in range(N):
    if A[i] in cnt.keys() and cnt[A[i]] >= 2:
        print(total-c2(cnt[A[i]])+c2(cnt[A[i]]-1))
    else:
        print(total)

Submission status: Accepted

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

Submission status: Accepted

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

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

s = 0
for i in range(n):
  s += cnt[i] * (cnt[i] - 1) // 2

for i in range(n):
  print(s-cnt[a[i]-1]+1, end="")
  if i != n-1:
    print(" ")
  else:
    print("\n")

Submission status: Accepted

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

from collections import Counter

ac = Counter(a)
acm = ac.most_common()

ans = 0

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

for ai in a:
    print(ans - ac[ai] + 1)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().strip().split()))[:n]
dict1={}
for i in arr:
	if i in dict1:
		dict1[i]+=1
	else:
		dict1[i]=1
sum1=0
for i in dict1:
	sum1+=((dict1[i]*(dict1[i]-1))//2)
for i in arr:
	k=sum1
	n1=dict1[i]
	k-=((n1*(n1-1))//2)
	k+=((n1-1)*(n1-1-1)//2)
	print(k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
## coding: UTF-8
#最後の方のテストケースでREになってしまう。時間切れ。
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product

N = int(input())

s = input().split()
p = [int(w) for w in s]
#print('p:{}'.format(p))

number_list = [0 for i in range(N)]
'''
for i in range(N):
    counter = 0
    for j in range(N):
        if(p[j] == i+1):
            counter +=1
    number_list.append(counter)
'''
#print(number_list)

for i in range(N):
    number_list[p[i] - 1] += 1
#print(number_list)



def n_C_2(n):
    if(n == -1):
        return 0
    else:
        return int(n * (n-1) / 2)


full_combi = []
pickup_combi = []
print_combi = []

for i in range(N):
    full_combi.append(n_C_2(number_list[i]))
    pickup_combi.append(n_C_2(number_list[i] - 1))
#print('full_combi:{}'.format(full_combi))
#print('pickup_combi:{}'.format(pickup_combi))

all_full = sum(full_combi)
for i in range(N):
    print_combi.append(all_full - full_combi[i] + pickup_combi[i])
#print('print_combi:{}'.format(print_combi))

for i in range(N):
    index = p[i] - 1
    print(print_combi[index])

Submission status: Accepted

Solution code:
def main():
    import math
    import collections
    N = int(input())
    A = list(map(int, input().split()))
    tt = 0
    #ans = list()
    ann = 0

    def comb(m, n):
        #return math.factorial(m) // (math.factorial(n) * math.factorial(m - n))
        return ((m - 1 + 1) / 1) * (m - 2 + 1) / 2

    '''
    def A_count(n):
        if n in memo:
            return memo[n]
        h = A.count(i)
        memo[n] = A.count(i)
        return h
    '''

    C = collections.Counter(A)
    for i in C.values():
        if i>=2:
            tt = tt + comb(i, 2)

    for i in A:
        f = C[i] #A_count(A[i])
        if f>=3:
            ann = (tt - comb(f, 2) + comb(f-1, 2))
        elif f==2:
            ann = tt - comb(f, 2)
        else:
            ann = tt
        print(int(ann))

    #for i in ans:
    #    print(i)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = II()
a = LI()

b = [0]*(n+1)
count = 0
for i in range(n):
    b[a[i]] += 1
for i in range(n+1):
    count += b[i]*(b[i]-1)/2
for i in a:
    print(int(count-b[i]+1))

Submission status: Accepted

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

Submission status: Accepted

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

B = [0]*(N+1)
for l in range(N):
    B[A[l]] += 1
ans = 0
for i in range(N+1):
    ans += int(B[i] * (B[i]-1) / 2)
for k in range(N):
        print(ans - B[A[k]] + 1)

Submission status: Accepted

Solution code:
from collections import Counter


def solve(string):
    n, *a = map(int, string.split())
    c = Counter(a)
    b = sum(v * (v - 1) // 2 for v in c.values())
    return "\n".join(str(b - c[_a] + 1) for _a in a)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

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

# 各数字の個数を格納
dict = {}
for a in A:
    if a in dict:
        dict[a] += 1
    else:
        dict[a] = 1

# k番目の値を消さない場合の場合分けの個数
ans = 0
for d in dict.values():
    if d > 1:
        ans += (d * (d - 1)) // 2

# k番目の値を除いた場合に減る場合の数を引いて答えを出力
for k in range(N):
    w = dict[A[k]]
    # 減る数
    decrease = 0
    if w == 1:
        pass
    elif w == 2:
        decrease = -1
    elif w > 2:
        decrease = ((w - 1) * (w - 2) // 2) - (w * (w - 1) // 2 )
    print(ans + decrease)

Submission status: Accepted

Solution code:
import collections as cl
N = int(input())
A = list(map(int,input().split()))
cn = cl.Counter(A)
sumC = sum([N*(N-1)//2 for N in cn.values()])
for k in range(N):
  sum = sumC - cn[A[k]] +1
  print(sum)

Submission status: Accepted

Solution code:
import collections as cl
N = int(input())
A = list(map(int,input().split()))
cn = cl.Counter(A)
sumC = sum([n*(n-1)//2 for n in cn.values()])
for k in range(N):
  print(sumC - cn[A[k]] +1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
kosuu = [0]*(N+1)
for i in range(N):
    kosuu[A[i]] += 1

base = 0
for i in kosuu:
    base += i*(i-1)//2
for i in range(N):
    print(base-(kosuu[A[i]]-1))

Submission status: Accepted

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

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

normal = 0
for i in ball:
    normal += i*(i-1)//2

for i in range(N):
    m = ball[A[i]]
    print(normal-m*(m-1)//2+(m-1)*(m-2)//2)

Submission status: Accepted

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

from collections import Counter

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

c = Counter(A)

sum_comb = 0

for key in c:
    sum_comb += c[key] * (c[key] - 1) // 2

for num in A:
    tmp = c[num] * (c[num] - 1) // 2
    tmp2 = (c[num]-1) * (c[num] - 2) // 2
    print(sum_comb - tmp + tmp2)

Submission status: Accepted

Solution code:
import collections

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

count = collections.Counter(a)
total = 0

for i in count.keys():
    total += int(count[i] * (count[i] - 1) / 2)

for v in a:
    print(total - count[v] + 1)

Submission status: Accepted

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

import collections
c= collections.Counter(A)

counter = 0
for j in c.keys():
    if c[j]>=2:
        counter += c[j]*(c[j]-1)/2


for i in A:
    c[i]
    print(int(counter-c[i]+1))

Submission status: Accepted

Solution code:
import math

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

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

sum = 0
for i in range(N):
    t = count[i]
    if t >= 2:
        sum += math.factorial(t) // (math.factorial(t - 2) * 2)

for i in range(N):
    t = count[A[i] - 1]
    if t > 1:
        print(sum - t + 1)
    else:
        print(sum)

Submission status: Accepted

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

d=dict()
for i in a:
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
    
#print(d)
    
ans1=dict()
ans2=dict()

for i in d.keys():
  ans1[i]=d[i]*(d[i]-1)//2
  ans2[i]=(d[i]-1)*(d[i]-2)//2
  
#print(ans1)
#print(ans2)
  
s=0
for i in d.values():
  s+=i*(i-1)//2
  
ans=[0 for i in range(n)]
for i in range(n):
  ans[i]=s-ans1[a[i]]+ans2[a[i]]
  
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
counter = Counter(a)
d = [0]*(n+1)
count = 0
for el, cnt in counter.most_common():
    d[el] = cnt
    count += cnt*(cnt-1)
count //= 2
for el in a:
    print(count - d[el] + 1)

Submission status: Accepted

Solution code:
from math import factorial
from collections import Counter

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

N = int(input())
A = list(input().split())
sum = 0
counter = Counter(A)
for key,value in counter.items():
    sum = sum + combinations_count(value,2)

for k in range(N):
    add = 0
    add = sum - combinations_count(counter[A[k]],2) + combinations_count(counter[A[k]]-1,2) 
    print(add)

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int, input().split()))
sameNum = [0]*(N)
sum = 0
# kを削除していないlistでの組み合わせを調べる
for i in range(N):
    sameNum[array[i]-1] += 1
for i in range(N):
    sum += sameNum[i]*(sameNum[i]-1)//2

for k in range(1, N+1):
    sumT = sum
    print(sumT - sameNum[array[k-1]-1] + 1)

Submission status: Accepted

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

# 1が3個、2が５個、、、みたいなものを管理する配列
counter = [0 for i in range(N+1)]
for i in range(N):
    counter[As[i]] += 1

# kC2を求める。（2C2 ~ NC2まで）
k_C_2 = [0 for i in range(N+1)]
k_C_2[2] = 1
for i in range(3, N+1):
    k_C_2[i] = k_C_2[i-1] * i / (i-2)


# 全体を求める
all_case = 0
for i in range(N+1):
    if counter[i] >= 2:
        all_case += k_C_2[counter[i]]

# 除く
for i in range(N):
    if counter[As[i]] == 2:
        print(int(all_case - 1))
    elif counter[As[i]] >= 3:
        print(int(all_case - k_C_2[counter[As[i]]] + k_C_2[counter[As[i]]-1]))
    else:
        print(int(all_case))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
integer = list(map(int, input().split()))
length = len(integer)
counter = Counter(integer)
keys = counter.keys()
total = 0
for key in keys:
  total += counter[key] * ( counter[key] -1 ) / 2
total = int(total)

for i in integer:
  tmp = total
  tmp -= counter[i] - 1
  print(tmp)

Submission status: Accepted

Solution code:
import collections

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

count = collections.Counter(a)

tmp = [0 for _ in range(n)]
tmp2 = [0 for _ in range(n)]

for key,value in count.items():
	if value == 1:
		continue
	tmp[key-1] = value*(value-1) // 2
	tmp2[key-1] = (value-1)*(value-2) // 2
tmp3 = sum(tmp)

for i in range(n):
	print(tmp3 - tmp[a[i]-1] + tmp2[a[i]-1])

Submission status: Accepted

Solution code:
import collections
N = int(input())
li = list(map(int, input().split()))
cn = collections.Counter(li)
sumC = sum([n*(n-1)//2 for n in cn.values()])
for k in range(N):
    print(sumC-cn[li[k]] + 1)

Submission status: Accepted

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

for i in A:
    D[i] += 1
S = 0
for i in range(1,N+1):
    tmp = D[i]
    S += tmp*(tmp-1)//2

for i in A:
    tmp = D[i]
    print(S-tmp*(tmp-1)//2+(tmp-1)*(tmp-2)//2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ct=[0]*n
cmb=[0]*n
s_cmb=0
for i in range(0,n):
  a[i]-=1
  ct[a[i]]+=1
for i in range(0,n):
  cmb[i]=ct[i]*(ct[i]-1)//2
  s_cmb+=cmb[i]
for i in range(0,n):
  print(s_cmb-cmb[a[i]]+(ct[a[i]]-1)*(ct[a[i]]-2)//2)

Submission status: Accepted

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

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

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

cnt = [0] * N
for i in range(N):
    cnt[A[i]] += 1
total = 0
for i in range(N):
    total += choose2(cnt[i])

for i in range(N):
    ans = total
    ans -= choose2(cnt[A[i]])
    ans += choose2(cnt[A[i]]-1)
    print(ans)

Submission status: Accepted

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

def comb(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()))
li = Counter(A)
S = 0
for i,j in enumerate(li):
    if li[j]>=2:
        S += comb(li[j],2)

for i in A:
    ans = S
    tmp = li[i]
    if tmp>2:
        ans -= comb(tmp,2)-comb(tmp-1,2)
    elif tmp==2:
        ans -= 1
    print(ans)

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):
    a=A[i]
    Ans=ans-c[a]+1
    print(Ans)