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:
import math
def A():
    N, M = map(int, input().split())
    if N == 1 == M:
        print(0)
        return
    ans = 0
    if N > 1:
        ans += math.factorial(N)//(2*(math.factorial(N-2)))
    if M > 1:
        ans += math.factorial(M)//(2*(math.factorial(M-2)))
    print(ans)

def B():
    S = input()
    if not S:
        print('No')
        return
    # for i in range(len(S)//4):
    #     # print(S[i], S[len(S)//2-i-1], S[len(S)//2+i+1], S[-i-1])
    #     if not (S[i] == S[(len(S)-1)//2-i-1] == S[(len(S)+3)//2+i-1] == S[-i-1]):
    #         print('No')
    #         return
    for i in range(len(S)):
        if not S[i] == S[len(S)-i-1]:
            print('No')
            return
    for i in range((len(S)-1)//2):
        if not S[i] == S[(len(S)-1)//2-i-1]:
            print('No')
            return
        if not S[(len(S)+3)//2+i-1] == S[len(S)-i-1]:
            print('No')
            return
    print('Yes')

import math
def C():
    L = int(input())
    print(math.pow((L/3), 3))

from collections import Counter
def D():
    N = int(input())
    A = [int(i) for i in input().split()]
    counts = Counter(A)
    combi = 0
    for a in counts:
        if counts[a] > 1:
            combi += math.factorial(counts[a])//(2*(math.factorial(counts[a]-2)))
    for k in range(N):
        print(max(0, combi-(counts[A[k]]-1)))
D()

Submission status: Accepted

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

aa = [0]*N
for a in AA:
  aa[a-1] += 1
ans = 0
for i in range(N):
  if aa[i-1] > 1:
    ans += int(aa[i-1]*(aa[i-1]-1)/2)
for i in range(N):
  if aa[AA[i]-1] >1:
    print(int(ans-aa[AA[i]-1]+1))
  else:
    print(ans)

Submission status: Accepted

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

from collections import Counter
B=Counter(A)

ans=0
for c in B.values():
        ans+=c*(c-1)//2

for k in range(N):
    e=B[A[k]]
    ans_k=ans-e*(e-1)//2+(e-1)*(e-2)//2
    print(ans_k)

Submission status: Accepted

Solution code:
from collections import Counter

input()
a = list(map(int, input().split()))
c = Counter(a)
s = sum(k * (k - 1) // 2  for k in c.values())
for x in a:
  print(s - c[x] + 1)

Submission status: Accepted

Solution code:
import collections

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

# k番目の数字の総数がkey、そのときの答えがvalue
removes = {}

c = collections.Counter(A)
c2 = {}
for key in c:
    if c[key] > 1:
        c2[key] = c[key]

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

# print(c2)
for k in range(N):
    # k番目の数字
    i = A[k]
    # k番目の数字の総数
    count = c[i]
    if count not in removes:
        answer = 0
        for key in c2:
            if i == key:
                answer += comb(c2[key] - 1)
            else:
                answer += comb(c2[key])
        removes[count] = answer
    print(removes[count])

Submission status: Accepted

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

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

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

for i in range(N):
    num = A[i]
    if count[num] > 2:
        result = total - (count[num] * (count[num] - 1) / 2) + ((count[num] - 1) * (count[num] - 2) / 2)
    elif count[num] == 2:
        result = total - 1
    else:
        result = total
    print(int(result))

Submission status: Accepted

Solution code:
def main():
  import collections
  x = int(input())
  num_list = list(map(int, input().split()))
  count = collections.Counter(num_list)
  score = 0
  for j in count.values():
    score += j * (j-1) / 2
  for k in num_list:
    k_num = count[k]
    print(int(score - k_num + 1))
main()

Submission status: Accepted

Solution code:
# sys.stdin.readline()
import sys
input = sys.stdin.readline
import collections

N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
cnt=[]
for k in c:
    cnt.append(c[k]*(c[k]-1)//2)

s=sum(cnt)
for i in range(N):
    print(s-c[A[i]]+1)

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
dic = {0: 0}
for a in aa:
    dic[a] = dic.get(a, 0) + 1

total = 0
for _, v in dic.items():
    total += (v * (v - 1)) // 2

for i in range(n):
    v = dic[aa[i]]
    x = total - (v * (v - 1)) // 2
    x += ((v - 1) * (v - 2)) // 2
    print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!python3.4
# -*- coding: utf-8 -*-
# abc159/abc159_d
import sys
from collections import Counter

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    A = i2nn()
    cntr = Counter()
    c_dict = {}
    for a in A:
        cntr[a] += 1
    sum_c = 0
    for k in cntr:
        n = cntr[k]
        c = n * (n - 1) // 2 if n > 1 else 0
        sum_c += c
        c_dict[k] = c
    for i in range(N):
        k = A[i]
        n = cntr[k] - 1
        c = n * (n - 1) // 2 if n > 1 else 0
        print(sum_c - c_dict[k] + c)
    return

main()

Submission status: Accepted

Solution code:
n = int(input())

ai = [False for _ in range(n)]

ai = [int(s) for s in input().split()]

#print(ai)

dic = {}

for i in ai:
    if(i in dic):
        dic[i] += 1
    else:
        dic[i] = 1
mx = 0
for i in dic:
    if(dic[i] < 2):
            continue
    else:
        mx += int(dic[i]*(dic[i]-1)/2)

for i in ai:
    if(dic[i] < 2):
        print(mx)
    else:
        res = mx - (dic[i]-1)
        print(int(res))

Submission status: Accepted

Solution code:
n=input()
t=map(int,raw_input().split())
l=[0]*(2*pow(10,5)+4)
for i in t:
    l[i]+=1
s=0
for x in l:
    s+=(x*(x-1))/2
for x in t:
    a=l[x]
    print s- (a*(a-1))/2 + (a-1)*(a-2)/2

Submission status: Accepted

Solution code:
import collections
def main():
    n = int(input())
    a = list(map(int,input().split()))
    cnt_ary = [0] * n

    for i,aa in enumerate(a):
        cnt_ary[aa-1] += 1


    tmp = 0
    ans_ary = [0] * n
    for i, cc in enumerate(cnt_ary):
        ans_ary[i] = cc * (cc-1)

    t = sum(ans_ary)
    ans = 0
    for i, aa in enumerate(a):
        ans = 0
        ans = t - ans_ary[aa-1]
        ans += (cnt_ary[aa-1] - 1) * (cnt_ary[aa-1] - 2)
        print(ans//2)



if __name__=='__main__':
    main()

Submission status: Accepted

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



for i in range(n):
    t=b[a[i]]
    s=t*(t-1)//2
    r=(t-1)*(t-2)//2

    print(p-s+r)

Submission status: Accepted

Solution code:
import sys
from pprint import pprint
from collections import Counter
from math import factorial

memo = {}
def ncr(n, r):
    if r > n:
        return 0
    key = "_".join(map(str, (n, r)))
    if key in memo:
        return memo[key]

    ans = factorial(n) // factorial(r) // factorial(n - r)
    memo[key] = ans
    return ans

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

counter = Counter(A)
sum_ans = 0
for key in counter:
    sum_ans += ncr(counter[key], 2)

for a_i in A:
    print(sum_ans - ncr(counter[a_i], 2) + ncr(counter[a_i] - 1, 2))

Submission status: Accepted

Solution code:
n = int(input())
ls = list(map(int,input().split()))
import collections
co = collections.Counter(ls)
ans = 0
for idx,num in co.items():
    ans +=num*(num-1)//2
for i in range(n):
    idx = ls[i]
    num = co[idx]
    if num == 1:
        print(ans)
    else:
        print(ans - num*(num-1)//2 + (num-1)*(num-2)//2)

Submission status: Accepted

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

nums = {}

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

pats = {}
for key in nums:
    pats[key] = nums[key] * (nums[key]-1) // 2

sum = 0
for key in pats:
    sum += pats[key]


for a in A:
    if pats[a] == 1:
        print(sum - 1)
    elif pats[a] == 0:
        print(sum)
    else:
        print(sum + (1-nums[a]))

Submission status: Accepted

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

dp = {}
for a in An:
    if not a in dp:
        dp[a] = 1
    else:
        dp[a] += 1
count = 0
for v in dp.values():
    if v > 1:
        count += v*(v-1)//2
for a in An:
    v = dp[a] 
    diff = 0
    if dp[a] > 1:
        diff = (v*(v-1)//2) - ((v-1)*(v-2)//2)
    print(count-diff)

Submission status: Accepted

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

count_dict = collections.Counter(A)
c_list = [0]*(N+1)
ans_list = [-1]*(N+1)
ans = [0]*(N)

for k,v in count_dict.items():
    c_list[k] = max(v*(v-1)/2,0)
S = sum(c_list)

for i in range(N):
    x = A[i]
    if ans_list[x] != -1:
        ans[i] = ans_list[x]
        continue
    v = count_dict[x]
    tmp = S - c_list[x] + max((v-2)*(v-1)/2,0)
#    print("tmp:",tmp,"S:",S,"c_list:",c_list[x],"v:",v)
    ans_list[x] = int(tmp)
    ans[i] = int(tmp)

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

Submission status: Accepted

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline




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


def II():
    return int(input())

directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'

res=[]
def search(seq,k,n):
    global N
    
    if k==N+1:

        tmp=[ascii_lowercase[s-1] for s in seq[1:]]
        res.append("".join(tmp))
        return 
    
    seq[k]=n
    search(seq,k+1,n+1)
    
    for l in range(1,n):
        seq[k] = l
        search(seq, k+1,n)
    

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

    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 int(result)


def nCr(n, r):
    """
    Calculate the number of combination (nCr = nPr/r!).
    The parameters need to meet the condition of n >= r >= 0.
    It returns 1 if r == 0, which means there is one pattern
    to choice 0 items out of the number of n.
    """

    # 10C7 = 10C3
    r = min(r, n-r)

    # Calculate the numerator.
    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i

    # Calculate the denominator. Should use math.factorial?
    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i

    return numerator // denominator


N=int(input())
A=acinput()


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

cb=[0]*len(y)

b=0
for i in range(1,len(y)):
    if y[i] >= 2:
        cb[i]=cmb(y[i],2)
    
        b+=cb[i]
    
for i in range(N):
    k=A[i]
    #print(i,k,y)
    tmp=0
    if y[k]-1>=2:
        tmp = nCr(y[k]-1, 2)
    tmp2=b-cb[k]+tmp
    #print("y",y[k],nCr(y[k]-1, 2))
    print(tmp2)
    
#print(cb)
    
#print(y)
#print(b)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
from math import ceil

def main():
    n = int(input())
    a = list(map(int,input().split()))
    d = {}
    for x in a:
        d[x] = d.get(x,0)+1
    ans = 0
    for x in d.values():
        ans+=(x*(x-1))//2
    for x in a:
        if d[x] == 1:
            print(ans)
            continue
        old = (d[x]*(d[x]-1))//2
        d[x]-=1
        new = (d[x]*(d[x]-1))//2
        d[x]+=1
        print(ans-old+new)







# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ball = [0]*(n+1)

for i in range(n):
    ball[a[i]] += 1

zenbu = 0
    
for i in range(n+1):
    tmp = ball[i]
    if tmp > 1:
        zenbu += (tmp*(tmp-1))//2
        
for i in range(n):
    tmp = ball[a[i]]
    if tmp > 1:
        #sabun = (tmp*(tmp-1))//2 - ((tmp-1)*(tmp-2))//2
        print(zenbu-(tmp-1))
    else:
        print(zenbu)

Submission status: Accepted

Solution code:
import collections 

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

c = collections.Counter(l_a)

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

###
for k in range(1,N+1):
  n = c[l_a[k-1]]
  print(t-(n-1))

Submission status: Accepted

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

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

ans=0

for i in range(n):
    ans+=c[i]*(c[i]-1)

ans=ans//2+1
for i in range(n):
    print(ans-c[a[i]-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

memo = [0]*(2*(10**5)+1)

for i in t:
    memo[i]+=1

sumv=0

for j in memo:
    sumv+=j*(j-1)//2

for i in t:
    print(sumv-(memo[i]*(memo[i]-1))//2+((memo[i]-2)*(memo[i]-1)//2))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
dic={}
for i in range(n):
  if x[i] in dic.keys():
    dic[x[i]]+=1
  else:
    dic[x[i]]=1
s=0
for i in dic.keys():
  s+=dic[i]*(dic[i]-1)//2
for i in range(n):
  print(s-dic[x[i]]+1)

Submission status: Accepted

Solution code:
N = int(input())
array = tuple(map(int, input().split()))

from collections import Counter
cnt = Counter(array)
total = 0
for key in cnt:
  if cnt[key] >= 2:
  	total += int(cnt[key] * (cnt[key] - 1) / 2)
    
for key in cnt:
  if cnt[key] == 1:
    cnt[key] = total
  else:
    temp = total - int(cnt[key] * (cnt[key] - 1) / 2)
    cnt[key] = temp + int((cnt[key]-1) * (cnt[key] - 2) / 2)

for num in array:
    print(cnt[num])

Submission status: Accepted

Solution code:
N=int(input())
a=[int(x) for x in input().split()]
import collections
c=collections.Counter(a)
d=list(c.keys())
def nc2(x):
    return x*(x-1)//2
ans0=0
for i in d:
    ans0+=nc2(c[i])
for i in range(len(a)):
    print(ans0-(c[a[i]]-1))

Submission status: Accepted

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

import sys

DEBUG = False

def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep = " "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return

def nr_choose2(nr_ball):
    return nr_ball * (nr_ball - 1) // 2

def main():
    n = read(int)
    as_ = read_list(int)
    nr_balls_by_num = {}
    nr_choose2_by_num = {}
    for a in as_:
        if a not in nr_balls_by_num:
            nr_balls_by_num[a] = 0
        nr_balls_by_num[a] += 1

    nr_all_choose2 = 0
    for num in nr_balls_by_num:
        nr_choose2_by_num[num] = nr_choose2(nr_balls_by_num[num])
        nr_all_choose2 += nr_choose2_by_num[num]

    for k in range(len(as_)):
        ans = nr_all_choose2 - nr_choose2_by_num[as_[k]]
        ans += nr_choose2(nr_balls_by_num[as_[k]] - 1)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def binom(n,r):
    if n<r:
        return 0
    if n<0:
        return 0
    if n==r:
        return 1

    result=1
    for i in range(r):
        result*=(n-i)
        result//=(i+1)

    return result


n=int(input())
count=[0 for _ in range(n)]
minus=[0 for _ in range(n)]
an=[int(c) for c in input().split()]
for i in range(n):
    count[an[i]-1]+=1

sum_=0
for i in range(n):
    sum_+=binom(count[i],2)
    minus[i]=binom(count[i]-1,1)

for i in range(n):
    print(sum_-minus[an[i]-1])

Submission status: Accepted

Solution code:
import math
from collections import Counter


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


N = int(input())

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


c = Counter()

ints = list()
for a in A:
    ints.append(a)
    c[a] += 1

t = 0
for a in set(ints):
    t += comb(c[a], 2)

for a in ints:
    print(t - (c[a] - 1))

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
cs = {k: v*(v-1)//2 for k, v in c.items() if v!=1}
s = sum(cs.values())

for v in a:
    if v in cs:
        print(s-(c[v]-1))
    else:
        print(s)

Submission status: Accepted

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

number = [0]*n
for item in A:#O(n)
    number[item-1] += 1

ans = 0

for items in number:
    ans += items*(items-1)//2

for item in A:
    print(ans - (number[item-1]-1))

Submission status: Accepted

Solution code:
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
all_pair=0
for val in dic_a.values():
  all_pair+=val*(val-1)//2

#print(dic_a,all_pair)
dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  now=dic_a[a]
  kill=now*(now-1)//2-(now-1)*(now-2)//2
  answer=all_pair-kill
  
  print(answer)
  dic_answer[a]=answer

Submission status: Accepted

Solution code:
from collections import Counter

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

    ans=0
    c = Counter(A)
    values, counts = zip(*c.most_common())
    for j in counts:
        if j >= 2:
            ans += j * (j - 1) // 2

    for i in A:
        if c[i]>=3:
            print(ans-(c[i]*(c[i]-1)//2-(c[i]-1)*(c[i]-2)//2))
        elif c[i]==2:
            print(ans-1)
        else:
            print(ans)






resolve()

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
As = [int(n) for n in input().split()]

C = Counter(As)

ans = sum(v * (v - 1) // 2 for k, v in C.items())
M = [C[a] - 1 for a in As]

for m in M:
    print(ans - m)

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(x) for x in input().split()]
a_dict = {}

for a in a_list:
  if a in a_dict.keys():
    a_dict[a] += 1
  else:
    a_dict[a] = 1


sum_value = sum([int((v*(v-1))/2)for v in a_dict.values()])

for a in a_list:
  print(sum_value-a_dict[a]+1)

Submission status: Accepted

Solution code:
import bisect, collections
 
 
 
def solution():
    d = collections.defaultdict(int)
    n = int(input().strip())
    a = [int(x) for x in input().strip().split(" ")]
    for x in a:
        d[x] += 1
    total = 0
    for _, v in d.items():
        total += v*(v-1)//2
    for x in a:
        print(total-d[x]*(d[x]-1)//2+(d[x]-1)*(d[x]-2)//2)
    

def main():    
    # T = int(input().strip())
    for _ in range(1):
        solution()

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=input()
arr=map(int, raw_input().split())
count=[0]*(n+1)
for i in arr:
  count[i]+=1
res=0
for i in xrange(n+1):
  if count[i]>1:
    res+=count[i]*(count[i]-1)/2
# print count
for i in xrange(n):
  s=count[arr[i]]
  if s>1:
    if s-1>1:
        print res-s*(s-1)/2+(s-1)*(s-2)/2
    else:
        print res-s*(s-1)/2
  else:
    print res

Submission status: Accepted

Solution code:
import math

N = int(input())
nums = list(map(int, input().split()))
numsB = nums[:]
d = dict()

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

for num in nums:
    if num not in d:
        d[num] = 1
    else:
        d[num] += 1

total = 0
for i in d:
    n = d[i]
    if n == 2:
        total += 1
    elif n > 2:
        total += combinations_count(n, 2)

for num in numsB:
    tmp = d[num] - 1
#    if tmp >= 2:
#        hoge = factorial(tmp) / factorial(2) / factorial(tmp - 2)
    print(int(total - tmp))

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)
cM = c.most_common()
n_sel = 0
for i in range(len(c)):
  if cM[i][1] > 1:
    n_sel += cmb(cM[i][1], 2)
  else:
    break;
    
for k in range(n):
  k_sel = c[a[k]] - 1
  print(n_sel - k_sel)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f(x):
    return x*(x-1)//2

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

d = collections.defaultdict(int)
for i in a:
    d[i]+=1
c = collections.defaultdict(int)
ans=0
for i in set(a):
    ans+=f(d[i])

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

Submission status: Accepted

Solution code:
import sys,math,copy,queue,itertools
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
N = int(input())
A = LI()

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

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

for i in range(N):
    ans = tc
    cnt = CA[A[i]]
    if cnt > 1:
        ans -= cnt * (cnt - 1) // 2 - (cnt - 1) * (cnt - 2) // 2
    print (ans)

Submission status: Accepted

Solution code:
n = int(input())
a_arr = []
# key = a, val = (len, nC2 - n-1C2)
a_map = {}
total = 0

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

for x in input().split():
  a = int(x)
  a_arr.append(a)
  if a in a_map.keys():
    a_map[a][0] = a_map[a][0] + 1

    thiscomb = comb(a_map[a][0])
    prevcomb = comb(a_map[a][0]-1)
    a_map[a][1] = thiscomb - prevcomb
    total = total - prevcomb + thiscomb
  else:
    a_map[a] = [1, 0]


for a in a_arr:
  print(int(total - a_map[a][1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

b = Counter(A)

bnum = list(b.values())
ans = 0
for i in range(len(bnum)):
    ans = ans + bnum[i]*(bnum[i]-1)/2
    
    
for j in range(N):
    temp = b[A[j]]
    print(int(ans-temp+1))

Submission status: Accepted

Solution code:
from collections import Counter

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

A_set = set(A)
a = Counter(A)
ruiseki = 0
for i in A_set:
    count = a[i]
    ruiseki += count*(count-1)/2
    
for i in range(N):
    b = A[i]
    front = a[b] * (a[b]-1) / 2
    later = (a[b]-1) * (a[b]-2) / 2
    ans = ruiseki - front + later
    print(int(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def cmb(n):
    return (n * (n-1)) // 2


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

count = {}
for i in a:
    if i in count:
        count[i] += 1
    else:
        count[i] = 1

total = 0
nbk = {}
for k, c in count.items():
    tmp = cmb(c)
    nbk[k] = tmp
    total += tmp

for k in range(n):
    orginal = nbk[a[k]]
    removed = cmb(count[a[k]] - 1)
    print(total - orginal + removed)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
def combinations_count2(n):
  return n*(n-1) // 2
d=[0]*(N+1)
for a in A:
  d[a]+=1
ans=0
for i in range(1,N+1):
  if d[i]>1:
    _=combinations_count2(d[i])
    ans+=_
    d[i]=_-combinations_count2(d[i]-1)
  else:
    d[i]=0
for a in A:
  print(ans-d[a])

Submission status: Accepted

Solution code:
from scipy.misc import comb
N=int(input())
A=list(map(int,input().split()))
nset=set([])
ndict={}
for a in A:
  if a in nset:
    ndict[a]+=1
  else:
    nset.add(a)
    ndict[a]=1
#print(nset,ndict)
all=0
for n in nset:
  tmp=ndict[n]
  if tmp>=2:
    all+=comb(tmp,2,exact=True)
all=int(all)
for a in A:
  tmp=ndict[a]
  if tmp==1:
    print(all)
  elif tmp==2:
    print(all-1)
  else:
    print( all-comb(tmp,2,exact=True)+comb(tmp-1,2,exact=True) )

Submission status: Accepted

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

count_dict = collections.Counter(A)
combination_dict = count_dict.copy()
diff_dict = count_dict.copy()

for k, v in count_dict.items():
    temp = combination_dict[k]
    combination_dict[k] = int(temp * (temp - 1) / 2)

for k, v in count_dict.items():
    temp = count_dict[k]
    diff_dict[k] = temp - 1

s = sum(combination_dict.values())

for k in A:
    print(s - diff_dict[k])

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(A)
combinations = {k: int(v * (v - 1) / 2) for k, v in counter.items()}
# print(combinations)
total = sum(combinations.values())
# print(total)
for a in A:
  print(total - (counter[a] - 1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def input_multiple_number_as_list():
    return list(map(int, input().split()))
N = int(input())
list_A = input_multiple_number_as_list()

#list_A.sort()
cntlist = [0]*(N+1)

for k in range(N):
    cntlist[list_A[k]]+=1

totalcomb = 0

for k in cntlist:
    totalcomb += (k*(k-1)//2)
for k in list_A:
    print(totalcomb-((cntlist[k])*(cntlist[k]-1)//2)+((cntlist[k]-1)*(cntlist[k]-2)//2))

Submission status: Accepted

Solution code:
# N: ボールの個数
# Ai: i番目のボールに書かれている整数
N = int(input())
As = list(map(int, input().split()))
As_sorted = sorted(As)

pre_num = 0
sum_cnt = 0
inner_sum_cnt = 1
num_cnt_list = [0 for i in range(max(As)+1)]  # 各数字が何個入っているかを見る
for A in As_sorted:
    if pre_num == A:
        inner_sum_cnt += 1
    else:
        if pre_num != 0:
            sum_cnt += inner_sum_cnt * (inner_sum_cnt - 1) // 2
            num_cnt_list[pre_num] = inner_sum_cnt
        pre_num = A
        inner_sum_cnt = 1

if pre_num == A:
    sum_cnt += inner_sum_cnt * (inner_sum_cnt - 1) // 2
    num_cnt_list[pre_num] = inner_sum_cnt

for A in As:
    cnt = num_cnt_list[A]
    if cnt < 2:
        print(sum_cnt)
    else:
        print(sum_cnt - cnt + 1)

Submission status: Accepted

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

for i in A:
    K[i] += 1
# print(K)
for i in range(N+1):
    M[i] = K[i]*(K[i]-1)//2
# print("K:",K)
y = sum(M)
# print("y:",y)

for a in A:
    # print("a:",a)
    print(y-K[a]+1)

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def comb(n,r):
    p,q=1,1
    for i in range(r):
        p=p*(n-i) 
        q=q*(i+1) 
    return p//q

def main():
    n = int(input())
    As = list(map(int, input().split()))
    Ps = list()
    cnt = 0
    counter = Counter(As)
    for i in range(n):
        Ps.append(counter[As[i]]-1)
    
    for i in counter:
        cnt += comb(counter[i], 2)

    for i in range(n):
        print(cnt-Ps[i])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a_ = list(map(int,input().split()))
adic = {}
for i in range(n):
    a = a_[i]
    if a in adic.keys():
        adic[a] += 1
    else:
        adic[a] = 1
ans = 0
for i in adic.values():
    if i >= 2:
        ans += i*(i-1)//2
moto = ans
for i in range(n):
    a = a_[i]
    b = adic[a]
    if b >= 2:
        ans -= b*(b-1)//2
        ans += (b-1)*(b-2)//2
    print(ans)
    ans = moto

Submission status: Accepted

Solution code:
from collections import Counter

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

Count = Counter(A)
s = sum(x * (x-1) // 2 for x in Count.values())

for a in A:
  print(s - Count[a] + 1)