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()))
count = [0]*N
for i in range(N):
    count[A[i]-1] += 1
s = 0
for i in range(N):
    s += count[i] * (count[i]-1) // 2
for i in range(N):
    if count[A[i]-1] == 0 or count[A[i]-1] == 1:
        print(s)
    else:
        print(s - count[A[i]-1] + 1)

Submission status: Accepted

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

for k in range(1,n+1):
    print(sum - combinations[a[k-1]-1] + max(0, c[a[k-1]-1] -1) * max(0, c[a[k-1]-1] - 2) // 2)

Submission status: Accepted

Solution code:
import collections
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


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

c=collections.Counter(A)
list=c.values()
result=0

for x in list:
    if x>1:
        result+=cmb(x,2)
for y in c.keys():
    c[y]=result-(c[y]-1)

for z in A:
    print(c[z])

Submission status: Accepted

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

c = Counter(A)
num_combination = [v * (v-1)//2 for v in c.values()]
ans = sum(num_combination)
for x in A:
  print(ans - c[x] + 1)

Submission status: Accepted

Solution code:
import collections
import math
def cmb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
la = list(map(int,input().split()))
all = 0
c = collections.Counter(la)
#print(c)
for i in range(N+1):
    all += cmb(c[i],2)
#print(all)
for k in range(N):
    print(all-c[la[k]]+1)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

total = 0
for v in d.values():
    if v != 1 and v != 0:
        total += int(v * (v - 1) / 2)

for i in range(N):
    tmp = d[A[i]] - 1
    print(total - tmp)

Submission status: Accepted

Solution code:
import math
from collections import Counter

def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d
    
def ABC159D(a):
    if a<2:
        return 0
    else:
        return (a*(a-1))//2

n=int(input())
a=list(map(int,input().split()))
d = dict()
#for i in a:
#    d=mycounter(d,i)
dd=Counter(a)
#print(d)
p=0
for i in dd.values():p+=ABC159D(i)
print('\n'.join(map(str,[p-dd[i]+1 for i in a])))

Submission status: Accepted

Solution code:
import math
from collections import Counter

def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d
    
def ABC159D(a):
    if a<2:
        return 0
    else:
        return math.factorial(a)//math.factorial(a-2)//2

n=int(input())
a=list(map(int,input().split()))
d = dict()
#for i in a:
#    d=mycounter(d,i)
dd=Counter(a)
#print(d)
p=0
for i in dd.values():p+=ABC159D(i)
print('\n'.join(map(str,[p-dd[i]+1 for i in a])))

Submission status: Accepted

Solution code:
import math
from collections import Counter

def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d
    
def ABC159D(a):
    if a<2:
        return 0
    else:
        return math.factorial(a)//math.factorial(a-2)//2

n=int(input())
a=list(map(int,input().split()))
d = dict()
#for i in a:
#    d=mycounter(d,i)
dd=Counter(a)
#print(d)
p=0
for i in dd.values():p+=ABC159D(i)
for i in a:
    print(p-dd[i]+1)

Submission status: Accepted

Solution code:
import math

def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d
    
def ABC159D(a):
    if a<2:
        return 0
    else:
        return math.factorial(a)//math.factorial(a-2)//2

n=int(input())
a=list(map(int,input().split()))
d = dict()
for i in a:
    d=mycounter(d,i)
p=0
for i in d.values():p+=ABC159D(i)
for i in a:
    print(p-d[i]+1)

Submission status: Accepted

Solution code:
import math

def mycounter(d,i):
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    return d
    
def ABC159D(a):
    if a<2:
        return 0
    else:
        return math.factorial(a)//math.factorial(a-2)//2

n=int(input())
a=list(map(int,input().split()))
d = dict()
for i in a:
    d=mycounter(d,i)
p=sum([ABC159D(i) for i in d.values()])
for i in a:
    print(p-d[i]+1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
num = [0] * (N+1)
for x in A:
    num[x] += 1
sum = 0
for x in num:
    sum += x * (x - 1) // 2
for x in A:
    print(sum - num[x] * (num[x] - 1) // 2 + (num[x] - 1) * (num[x] - 2) // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  # 1e17


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


n = int(input())
A = list(map(int, input().split()))
B = [0] * (n + 1)
for a in A:
    B[a] += 1
ans = sum(cal(b) for b in B)
for a in A:
    num = ans
    num -= cal(B[a])
    B[a] -= 1
    num += cal(B[a])
    print(num)
    B[a] += 1

Submission status: Accepted

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

import collections

c = collections.Counter(a)

s = 0
for i in c:
    j = c[i]

    s += j * (j - 1) // 2

for i in range(n):
    j = c[a[i]]
    s2 = s - j * (j - 1) // 2 + max(0, (j - 1) * (j - 2) // 2)
    print(s2)

Submission status: Accepted

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

index=[0]*(n+1)

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

ans=0
for i in index:
    ans+=i*(i-1)/2

for a in A:
    print(str(int(ans-index[a]+1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array(input().split(),dtype=int)
sum = 0

kn = [0 for i in range(n+1)]
for i in range(n):
    kn[a[i]] += 1
for i in range(n+1):
    sum += kn[i]*(kn[i]-1)//2

for i in range(n):
    x = kn[a[i]]
    print(sum-x+1)

Submission status: Accepted

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

for i in range(N):
    cnt[a[i]-1]+=1

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

for i in range(N):
    k=(cnt[a[i]-1])*(cnt[a[i]-1]-1) //2
    l=(cnt[a[i]-1]-1)*(cnt[a[i]-1]-2) //2
    d=k-l
    print(c_sum-d)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
from operator import mul
from functools import reduce
def cmb1(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

def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    A_c=Counter(A)

    combi=0
    for ac in A_c.items():
        if ac[1]>=2:
            combi+=cmb1(ac[1],2)
    for i in range(n):
        print(combi-(A_c[A[i]]-1))



if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter,defaultdict
from operator import mul
from functools import reduce
def cmb1(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

def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    A_c=Counter(A)

    combi=0
    for ac in A_c.items():
        if ac[1]>=2:
            combi+=cmb1(ac[1],2)
    for i in range(n):
        print(combi-(A_c[A[i]]-1))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

# 書かれた番号の頻度
Freq = Counter(A)

# 答えの最大値
ans_max = 0
for kv in Freq.items():
  ans_max += kv[1] * (kv[1] - 1) // 2

for a in A:
  # 1個しか存在しない番号は、取り除いても答えに影響しない
  if Freq.get(a) == 1:
    print(ans_max)
    continue

  # x個の場合と、x-1個の場合と、の差分
  x = Freq.get(a)
  diff = (x * (x-1) // 2) - ((x-1) * (x-2) // 2)

  # そのぶんだけ、答えの最大値から減らす
  ans = ans_max - diff
  print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

# 書かれた番号の頻度
Freq = dict(sorted(Counter(A).items(), key=lambda x: x[1], reverse=True))

# 答えの最大値
ans_max = 0
for kv in Freq.items():
  ans_max += kv[1] * (kv[1] - 1) // 2

for a in A:
  # 1個しか存在しない番号は、取り除いても答えに影響しない
  if Freq.get(a) == 1:
    print(ans_max)
    continue

  # x個の場合と、x-1個の場合と、の差分
  x = Freq.get(a)
  diff = (x * (x-1) // 2) - ((x-1) * (x-2) // 2)

  # そのぶんだけ、答えの最大値から減らす
  ans = ans_max - diff
  print(ans)

Submission status: Accepted

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

b = [0 for _ in range(max(a))]
for i in a:
    b[i - 1] += 1
ans = 0
for i in b:
    ans += int(i * (i-1) / 2)

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

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
dic={}
for e in ls:
  if (e in dic):
    dic[e]+=1
  else:
    dic[e]=1
Sum=0
for key,value in dic.items():
  Sum+=value*(value-1)//2

for i in range(N):
  print(Sum-dic[ls[i]]+1)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
dif = [0] * (n + 1)
sum_c = 0
c = Counter(a)
for i, v in c.items():
    if v < 2:
        continue
    val = v * (v - 1) / 2
    dif[i] = v - 1
    sum_c += val
print('\n'.join([str(int(sum_c - dif[i])) for i in a]))

Submission status: Accepted

Solution code:
from collections import Counter
#from scipy.special import comb
# a = comb(n, r)
# >> cannot use in Contest (like AtCoder)


def cmb(n, r):
    if n - r < r:
        r = n - r
    if r == 0:
        return 1
    if r == 1:
        return n
    if n <= 1:
        return 0

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


N = int(input())
data = list(map(int, input().split()))
count = Counter(data)
#print(count) > Counter({1: 4, 2: 2, 4: 2})
countMost = count.most_common()

lis = [0] * (2 * 10 ** 5)
combination = [0] * (2 * 10 ** 5)
tot = 0
for i, v in countMost:
    #print(i, v)
    lis[i - 1] = v
    val = cmb(v, 2)
    tot += val
    combination[i - 1] = val
#print('TOT', tot)
# print(lis[0])
for i, v in enumerate(data):
    num = lis[v - 1]
    if (num == 2):
        #    print('a')
        ans = tot - 1
    else:
        ans = tot - combination[v-1] + cmb(num-1, 2)

    print(ans)
'''
dataSort = data.sort()
print(countMost)
print(len(countMost))
'''

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
B = Counter(A)
C = {key:(B[key]*(B[key]-1)//2) for key in B}
ans = sum(C.values())
#print(C,ans)
#print(C[3])
for i in A:
    if C[i] < 1:
        print(ans)
    else:
        print(ans - C[i] + (B[i]-1)*(B[i]-2)//2)

Submission status: Accepted

Solution code:
def main():
    import collections
    import math
    n=int(input())
    a=list(map(int, input().split()))
    c=collections.Counter(a)
    cnt=list(c.items())
    ans=0
    comb=[]
    for i in range(len(cnt)): #各出現数字のAi_C_2を計算
        buf=(cnt[i][1]**2-cnt[i][1])//2
        comb.append(buf)
        ans+=buf
    for ai in a:
        print(ans-c[ai]+1)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

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

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


lista = [0] * (n + 1)
sum = 0
for j in range(n):
    lista[a[j]] += 1
for i in lista:
    if i >= 2:
        sum += cmb(i, 2)
for i in a:
    print(sum - lista[i] + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def com(n):
    return n*(n-1)//2
ans=0
n=int(input())
a=list(map(int,input().split()))
coun=[0]*(n+1)
for i in range(n):
    coun[a[i]]+=1
for i in range(n+1):
    ans+=com(coun[i])
for i in range(n):
    m=com(coun[a[i]]-1)
    o=com(coun[a[i]])
    print(ans-o+m)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter
from scipy.misc import comb
N = int(input())
A = list(map(int, input().split()))

total = 0
answers = np.array([0] * (N + 1), dtype=np.int64)
for number, appearance in Counter(A).items():
    flat = comb(appearance, 2, exact=True)
    minus_one = comb(appearance - 1, 2, exact=True)
    total += flat
    answers[number] = minus_one - flat
answers += total

print(*answers[A], sep="\n")

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb
import numpy as np
N = int(input())
A = list(map(int, input().split()))

total = 0
answers = np.array([0] * (N + 1))
for number, appearance in Counter(A).items():
    flat = comb(appearance, 2, exact=True)
    minus_one = comb(appearance - 1, 2, exact=True)
    total += flat
    answers[number] = minus_one - flat
answers += total

print(*answers[A], sep="\n")

Submission status: Accepted

Solution code:
import collections


k = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
s = 0
for i in c.values():
  s += i * (i - 1) / 2

for i in a:
  j = c[i] - 1
  b = j * (j - 1) // 2
  y = (j + 1) * j // 2
  d = y - b
  print(int(s - d))

Submission status: Accepted

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

for i in A:
    B[i] += 1
sum_original = sum([j*(j-1)//2 for j in B])
for k in range(N):
    print(sum_original - B[A[k]] + 1)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

for i in range(N):
  ans2 = ans - seisu[A[i]] + 1
  print(ans2)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
dd = defaultdict(int)
A=[int(i) for i in input().split()]
for i in A:
    dd[i]+=1
memo = [0]*(n+1)
for i in dd:
    memo[i]=(dd[i]*(dd[i]-1))//2
ans = sum(memo)
done = [-1]*(n+1)
for i in range(n):
  print(int(ans-memo[A[i]]+(memo[A[i]])*((dd[A[i]]-2)/dd[A[i]])))

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
dd = defaultdict(int)
A=[int(i) for i in input().split()]
for i in A:
    dd[i]+=1
memo = [0]*(n+1)
for i in dd:
    memo[i]=(dd[i]*(dd[i]-1))//2
ans = sum(memo)
done = [-1]*(n+1)
for i in range(n):
    if done[A[i]]==-1:
        done[A[i]]=(int(ans-memo[A[i]]+(memo[A[i]])*((dd[A[i]]-2)/dd[A[i]])))
        print(done[A[i]])

    else:
        print(done[A[i]])

Submission status: Accepted

Solution code:
import collections 
import math

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

cnt=0
c =collections.Counter(a) #要素の数

#全体の式の導出
#今回は要素ごとの組み合わせの数

for i in c.values() :
    cnt += i*(i-1)//2

#各要素の影響を排除して出力
for i in a:
    print(cnt- c[i]*(c[i]-1)//2 + (c[i]-1)*(c[i]-2)//2)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
counts = [0] * (N + 1)
for a in As:
    counts[a] += 1
tot = 0
for count in counts:
    tot += (count * count - count) // 2
for a in As:
    print(tot - counts[a] + 1)

Submission status: Accepted

Solution code:
from collections import Counter

def main():

    N = int(input(""))
    A = list(map(int,input().split()))
    X = 0
     
    A_e = list(set(A))
     
    x = Counter(A)
     
    for i in A_e:
        if x[i] >= 2:
            xc2 = x[i]*(x[i]-1)/2
            X = X + xc2
     
    for k in A:
        print(int(X-x[k]+1))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter, defaultdict
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


n, *a = map(int, read().split())
counter = list(Counter(a).items())
cnt = defaultdict(int)
memo = defaultdict(int)
for k, v in counter:
    if v > 1:
        cnt[k] = cmb(v, 2)
        if v > 2:
            memo[k] = cmb(v - 1, 2)
        else:
            memo[k] = 0
    else:
        cnt[k] = 0
        memo[k] = 0
ans = sum(cnt.values())
for aa in a:
    print(ans - cnt[aa] + memo[aa])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

N = stdin.readline().rstrip().split()
N = int(N[0])

int_list = list(map(int, stdin.readline().rstrip().split()))
cnt_list = [0] * N

for i in int_list:
    cnt_list[i-1] += 1

ret_list = [0] * N

# これはO(N)でいける

tmp_ans = 0
for i in range(N):
    if cnt_list[i] > 1:
        tmp_ans += cnt_list[i]*(cnt_list[i]-1) // 2


for i in range(N):
    if cnt_list[i] < 1:
        tmp_ret = 0
    else:
        tmp_ret = tmp_ans - cnt_list[i]*(cnt_list[i]-1) // 2 + (cnt_list[i]-1)*(cnt_list[i]-2) // 2
    ret_list[i] = tmp_ret

for i in int_list:
    print(ret_list[i-1])

Submission status: Accepted

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

from collections import Counter

ttl = 0
cnt = Counter(a)
for i in cnt.values():
    ttl += i * (i - 1) // 2

k_ttl = 0
for i in range(n):
    print(ttl - (cnt[a[i]] - 1))

Submission status: Accepted

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

cnt = Counter(a)
amount = 0
for i in cnt.values():
    amount += i * (i - 1) // 2

for i in range(n):
    ans = amount + 1 - cnt[a[i]]
    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 range(1, n + 1):
    ans += c[i] * (c[i] - 1) // 2
for i in range(n):
    print(ans - c[a[i]] + 1)

Submission status: Accepted

Solution code:
n=int(input());A=list(map(int,input().split(' ')))
B=[]
i = 0
while i < n:
    B.append((A[i],i))
    i += 1
B.sort()
klist = [0]*n
total = 0;count = 1;value = B[0][0];k_temp = [B[0][1]]
i = 1
while i < n:
    if value == B[i][0]:
        count += 1
        k_temp.append(B[i][1])
    else:
        t = count*(count-1)//2
        if t != 0:
            total += t
            diff = t - (count-1)*(count-2)//2
            for p in k_temp:
                klist[p] = -1*diff
        count = 1
        k_temp = [B[i][1]]
        value = B[i][0]
    i += 1
if count != 1:
    t = count*(count-1)//2
    total += t
    diff = t - (count-1)*(count-2)//2
    for p in k_temp:
        klist[p] = -1*diff
for k in klist:
    print(k+total)

Submission status: Accepted

Solution code:
import numpy as np

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

counter = np.bincount(A)

total = (counter * (counter - 1)).sum() // 2
diff = counter -1
ans = total - diff[A]

print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import numpy as np

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

counter = np.bincount(A)

total = (counter * (counter - 1)).sum() // 2
diff = (counter * (counter-1)//2 - (counter-1) * (counter-2)//2)
ans = total - diff[A]

print(*ans, sep="\n")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
import numpy as np
num_list = np.zeros(N+1)
for A_ in A:
    num_list[A_] += 1

all_pattern = 0
for number_ in num_list:
    all_pattern += number_ * (number_ - 1) / 2

for A_ in A:
    print(int(all_pattern - (num_list[A_] - 1)))

Submission status: Accepted

Solution code:
import numpy as np

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

counter = np.bincount(A)
 
total = (counter * (counter - 1)).sum() // 2
answer = (total - (counter - 1))[A]
 
print('\n'.join(answer.astype(str)))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np
 
N = int(readline())
A = np.array(read().split(), np.int64)
counter = np.bincount(A)
 
total = (counter * (counter - 1)).sum() // 2
answer = (total - (counter - 1))[A]
 
print('\n'.join(answer.astype(str)))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input(""))
A = list(map(int,input().split()))
X = 0

A_e = list(set(A))

x = Counter(A)

for i in A_e:
    if x[i] >= 2:
        xc2 = x[i]*(x[i]-1)/2
        X = X + xc2

for k in A:
    print(int(X-x[k]+1))

Submission status: Accepted

Solution code:
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])


# エントリーポイント
def main():
    N = input(int)
    A = input([int, N])

    count = dd(int)
    for a in A:
        count[a] += 1
    sum_ = 0
    for k, v in count.items():
        sum_ += (v * (v - 1)) // 2
    for a in A:
        print(sum_ - count[a] + 1)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

Solution code:
import collections
import math

url = "https://atcoder.jp//contests/abc159/tasks/abc159_d"


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


def main():
    input()
    t = list(map(int, input().split()))
    col = collections.Counter(t)
    com = {}
    al = 0
    for key in col:
        if col[key] < 2:
            com[key] = 0
        else:
            com[key] = combinations_count(col[key], 2)
        al += com[key]
    for v in t:
        c = col[v] - 1
        if c <= 1:
            c = 0
        else:
            c = com[v] - c
        tmp = (al - com[v]) + c
        print(tmp)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

def r(i) :
  if i < 2:
    return 1
  else :
    return i*(i-1)/2

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

total = 0
c = defaultdict(int)
for j in range(a):
  c[b[j]] += 1
for key in c.keys():
  if c[key] > 1:
    total += r(c[key])

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

Submission status: Accepted

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

c = collections.Counter(a)
# print(c)
s = 0
for i in list(c.values()):
    s += i*(i-1)//2

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

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    a = list(map(int,input().split()))
    full = [0 for i in range(n+1)]
    one = [0 for i in range(n+1)]


    c = Counter(a)
    for i in range(1,n+1):
        if 1 <= c[i]:
            full[i] = c[i]*(c[i]-1)//2
        if 2 <= c[i]:
            one[i] = (c[i]-1)*(c[i]-2)//2
    sum_full = sum(full)

    for i in range(len(a)):
        print(sum_full - full[a[i]]+one[a[i]])

    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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

S = [0] * (N+1)

for i in range(N):
    S[A[i]] += 1
    ans_ = 0
for i in range(1,N+1):
    s = S[i]
    ans_ += s*(s-1)//2
for i in range(N):
    ans = copy.copy(ans_)
    a = S[A[i]]
    if a > 1:
        ans -= a*(a-1)//2
        ans += (a-1)*(a-2)//2
    print(ans)

Submission status: Accepted

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

d={}
sum=0
for i in range(n):
  if l[i] not in d:
    d[l[i]]=1
  else:
    d[l[i]]+=1
   
l1=[]
for i in d:
  if d[i]>1:
    l.append((d[i]*(d[i]-1))//2)
    sum+=((d[i]*(d[i]-1))//2)

#print(d,sum)
for i in range(n):
  if d[l[i]]==2:
    print(sum-1)
  elif d[l[i]]>2:
    we=((d[l[i]]*(d[l[i]]-1))//2-((d[l[i]]-1)*(d[l[i]]-2))//2)
    print(sum-we)
  else:
    print(sum)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    a = lmi()
    d = defaultdict(int)
    for num in a:
        d[num] += 1
    total = 0
    for _, v in d.items():
        total += v * (v-1) // 2
    for num in a:
        print(total - (d[num] - 1))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
#n,p=map(int,input().split())
al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

dic={}
for a in al:
    dic[a]=dic.get(a,0)+1
total=0
for v in dic.values():
    total+=v*(v-1)//2

for k in range(n):
    res=total-(dic[al[k]]-1)
    print(res)