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 collections
N = int(input())
A = list(map(int, input().split()))


C = collections.Counter(A)

d = {}

total = 0
for k, v in C.items():
    d[k] = (v*(v-1)//2, (v-1)*(v-2)//2)
    total += d[k][0]


for i in range(N):
    minus = d[A[i]][0]
    plus = d[A[i]][1]
    print(total - minus + plus)

Submission status: Accepted

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

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

for i in a:
    sa = dic[i] * (dic[i] - 1) // 2 - ((dic[i] - 1) * (dic[i] - 2) // 2)
    print(ans - sa)

Submission status: Accepted

Solution code:
import sys
from collections import Counter,defaultdict

N=input()
A=map(int, sys.stdin.readline().split())
C=defaultdict(lambda: 0)
for x in A:
    C[x]+=1
total=0

for x in C.values():
    total+=x*(x-1)/2

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

Submission status: Accepted

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

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

c = Counter(a)
d = dict(c.most_common())

x = sum([comb(v, 2, exact=True) for v in d.values()])
for i in range(n):
    print(x - (comb(d[a[i]], 2, exact=True) - comb(d[a[i]] - 1, 2, exact=True)))

Submission status: Accepted

Solution code:
import collections

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

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

for i in range(n):
  d=c[a[i]]-1
  print(total-d)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

from collections import Counter

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input().strip())
    A = [int(s) for s in input().strip().split()]

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

    for a in A:
        result = w - (cnt[a] - 1)
        print(result)


# 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")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    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:
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
C = Counter(A)
s = 0
d = {}
for k in C.keys():
    v = C[k]
    t = v*(v-1)//2
    s += t
    d[k] = t
for a in A:
    print(s-d[a]+((C[a]-1)*(C[a]-2)//2))

Submission status: Accepted

Solution code:
from collections import Counter

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

tbl0 = Counter(a)
tbl = tuple(Counter(a).items())

t = 0
for v in tbl:
    t += v[1] * (v[1] - 1) // 2

for i in range(n):
    s = tbl0[a[i]]
    print(t - (s - 1))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().strip().split(" ")]

from collections import defaultdict

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

diff = {}
total = 0
for i in count.keys():
    tmp = count[i] * (count[i]-1) // 2
    if tmp == 0:
        diff[i] = 0
    else:
        diff[i] = tmp - (count[i]-1) * (count[i]-2) // 2
    total += tmp

prnt = 0
for i in A:
    prnt = total
    prnt -= diff[i]
    print(prnt)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
def calc_comb(n):
    return (n * (n-1)) // 2

N = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
# calc nC2
sum_val = 0
for key in cnt.keys():
    if 3 <= cnt[key]:
        sum_val += calc_comb(cnt[key])
    elif cnt[key] == 2:
        sum_val += 1

for i in range(N):
    cnt[A[i]] -= 1
    if 2 <= cnt[A[i]]:
        res = sum_val - calc_comb(cnt[A[i]] + 1) + calc_comb(cnt[A[i]])
        print(res)
    elif cnt[A[i]] == 1:
        res = sum_val - 1
        print(res)
    else:
        print(sum_val)
    cnt[A[i]] += 1

Submission status: Accepted

Solution code:
# Author: cr4zjh0bp
# Created: Sun Mar 22 11:54:41 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import Counter

n = ni()
a = na()
c = Counter(a)
d = dict(c)
r = 0
for k, v in d.items():
    r += v * (v - 1) // 2

for i in range(n):
    print(r - d[a[i]] * (d[a[i]] - 1) // 2 + ((d[a[i]] - 1) * (d[a[i]] - 2) // 2))

Submission status: Accepted

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

Submission status: Accepted

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

d = [0]*n
for i in range(n):
    d[al[i]-1] += 1

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

for k in range(n):
    a = al[k]-1
    if d[a]<2:
        print(s)
        continue
    if d[a]==2:
        print(s-1)
        continue
    res = s - d[a]*(d[a]-1)//2 + (d[a]-1)*(d[a]-2)//2
    print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
cnt = Counter(a)
ans = {}
for key in cnt.keys():
    v = cnt[key]
    ans[key] = v * (v-1) // 2
s = sum(list(ans.values()))
for ai in a:
    if cnt[ai] == 1:
        print(s)
    else:
        print(int(s - ans[ai] + (ans[ai] / cnt[ai]) * (cnt[ai] - 2)))

Submission status: Accepted

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

Submission status: Accepted

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

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

S=0
for i in range(N+1):
    if B[i]!=0:
        S += int(B[i]*(B[i]-1)/2)

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

Submission status: Accepted

Solution code:
import collections
import numpy as np

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

di = collections.Counter(li)
#print(di)

arr = np.array(list(di.values()))
arrr = arr - 1
c = np.dot(arr, arrr) // 2
#print(c)

#(n * n-1)-(n-1 * n-2) / 2 = n-1

for v in li:
    x = int(di[v])
    if x == 1:
        print(c)
    else:
        print(c-(x-1))

Submission status: Accepted

Solution code:
# Credits Geeks forGeeks
n=200001
k=2
C = [[0 for x in range(k+1)] for x in range(n+1)] 
for i in range(n+1): 
    for j in range(min(i, k)+1): 
        if j == 0 or j == i: 
            C[i][j] = 1
        else: 
            C[i][j] = C[i-1][j-1] + C[i-1][j]
t=int(input())
a=[int(x) for x in input().split()]
d=dict()
d2=dict()
for i in a:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
total=0
for i in d:
    # print(i,d[i])
    count=d[i]
    if(count<2):
        d2[i]=0
    else:
        # ncr
        d2[i]=C[count][2]
    total+=d2[i]
# print("Total "+str(total))
# print(C[2][2])
for i in a:
    count=d[i]
    if(count<=2):
        ans=0
    else:
        ans=C[count-1][2]
    print(total-d2[i]+ans)

Submission status: Accepted

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

m = defaultdict(int)
for a in A:
    m[a] += 1

total = 0

for k, v in m.items():
    if v >= 2:
        total += v * (v-1) // 2

for a in A:
    if m[a] >= 2:
        print(total - (m[a]-1))
    else:
        print(total)

Submission status: Accepted

Solution code:
N = int(input())
A=list(map(int,input().split()))
count_list = [0 for _ in range(N)]
choose_list = [0 for _ in range(N)]
for a in A:
    count_list[a-1] += 1
for i,c in enumerate(count_list):
    choose_list[i] = int(c*(c-1)/2)
sum = sum(choose_list)

for i in range(N):
    ball = A[i] - 1
    ball_num = count_list[ball] - 1
    print(sum - choose_list[ball] + ball_num*(ball_num-1)//2)

Submission status: Accepted

Solution code:
import sys
from math import factorial
from collections import Counter
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    c = Counter(A)

    data = [0] * (N+1)
    sum_count = 0
    for i, v in c.items():
        if v < 2:
            continue
        value = factorial(v) // 2 // factorial(v - 2)
        data[i] = value - value * (v - 2) // v
        sum_count += value

    print('\n'.join([str(sum_count - data[a]) for a in A]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
count = [0]*n
base = 0
list = [int(i) for i in input().split()]

for i in list:
    count[i-1] += 1

for j in range(n):
    m = count[j]
    base += m*(m-1)//2

for k in list:
    print(base - count[k-1] + 1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(s) for s in input().split()]
d={}
for i in range(0,n):
    if a[i] not in d:
        d[a[i]]=1
    else:
        d[a[i]]+=1 
d1={}
s=0
for i in range(0,n):
    if a[i] not in d1:
        d1[a[i]]=(d[a[i]]*(d[a[i]]-1))//2
        s+=d1[a[i]]
for i in range(0,n):
    print(s-d[a[i]]+1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
d_cnt = dict()
d_comb = dict()
for x in a:
    cnt = d_cnt.get(x, -1) + 1
    d_cnt[x] = cnt
    if cnt >= 1:
        d_comb[x] = d_comb.get(x, 0) + cnt
base = sum(d_comb.values())
for x in a:
    print(base - d_cnt.get(x))

Submission status: Accepted

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

num_cnt = [0] * 200001
for i in a:
    num_cnt[i] += 1

pat_cnt = [0] * 200001
for i in range(200001):
    b = num_cnt[i]
    pat_cnt[i] = (b*(b-1))//2

ans = sum(pat_cnt)

for i in range(n):
    #print(ans, pat_cnt[a[i]], num_cnt[a[i]])
    print(ans - pat_cnt[a[i]] + ((num_cnt[a[i]]-1) * (num_cnt[a[i]]-2))//2)

Submission status: Accepted

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

def main():
     n = int(input())
     a = list(map(int, input().split()))
     num_a = dict()
     com_a = dict()

     for i in a:
          if not i in num_a.keys():
               num_a[i] = 1
          else:
               num_a[i] += 1

     for i in num_a.keys():
          value = num_a[i]
          com_a[i] = com(value)

     # print(num_a)
     # print(com_a)

     pre_ans = sum(com_a.values())
     # print('pre_ans:{}'.format(pre_ans))

     for i in range(n):
          k = a[i]
          ans = pre_ans - com_a[k] + com(num_a[k] - 1)
          print(ans)

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys 

input = sys.stdin.readline
# sys.setrecursionlimit(25000)

import collections as c

n = int(input())
al = list(map(int, input().split(' ')))

c = c.Counter(al)

s = 0
for k, v in c.most_common():
    s += (v) * (v - 1) // 2

for i in range(n):
    temp = c[al[i]]
    a = temp * (temp - 1) // 2
    b = (temp - 2) * (temp - 1) // 2
    
    print(s - a + b)

Submission status: Accepted

Solution code:
import collections

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

mx = 0

for item, value in c.items():
    mx += value * (value-1) // 2

for i in range(len(a)):
    ans = mx

    t = c[a[i]]
    ans -= t * (t-1) //2
    ans += (t-1)*(t-2) //2
    print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)


def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    num_lists = [0] * (N + 1)
    for a in A:
        num_lists[a] += 1
    A_C = [int(c * (c - 1) / 2) for c in num_lists]
    sum_ = sum(A_C)

    for a in A:
        temp_sum = sum_ - A_C[a]
        temp_sum += (num_lists[a] - 1) * (num_lists[a] - 2) / 2
        print(int(temp_sum))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
L = [int(s) for s in input().rstrip().split(' ')]
D = {}
for x in L:
  if x not in D.keys():
    D[x] = 1
  else:
    D[x] += 1
S = 0
for y in D.values():
  if y > 1:
    S += y*(y-1) // 2
for i in range(N):
  print(S-D[L[i]]+1)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
# print(C)
ans = sum([C[key]*(C[key]-1) // 2 for key in C.keys() if C[key] >= 2])
#print([C[key]*(C[key]-1) // 2 for key in C.keys() if C[key] >= 2])

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

Submission status: Accepted

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

c = Counter(arr)

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

ans = {}
tot = 0
for k,v in c.items():
    ans[k] = co(v)
    tot += ans[k]

for it in arr:
    a = tot - ans[it] + co(c[it]-1)
    print(a)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    ans = 0
    for v in c.values():
        ans += v*(v-1)//2
    for k in range(N):
        if 2 <= c[A[k]]:
            print(ans - (c[A[k]] - 1))
        else:
            print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter
AC=Counter(A)
ans=int(sum(map(lambda x: x*(x-1)/2, AC.values())))
for a in A:
    print(max(ans-AC[a]+1,0))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.q = l
        heapq.heapify(self.q)
        return

    def push(self, n):
        heapq.heappush(self.q, n)
        return

    def pop(self):
        return heapq.heappop(self.q)


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    A = Scanner.map_int()
    M = Counter(A)
    MAX = 0
    for v in M.values():
        MAX += v * (v - 1) // 2
    for i in range(N):
        a = M[A[i]]
        ans = MAX - a * (a - 1) // 2 + (a - 1) * (a - 2) // 2
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = [0]*(n+1)
    c = [0]*(n+1)
    d = [0]*(n+1)
    e = [0]*(n+1)
    cmb = lambda x: (x*(x-1))//2
    for ai in a:
        b[ai] += 1
    for i, bi in enumerate(b):
        if bi:
            c[i] = cmb(bi)
            d[i] = cmb(bi-1)
    ans = sum(c)
    for i, (ci, di) in enumerate(zip(c, d)):
        e[i] = ans-ci+di
    for ai in a:
        print(e[ai])

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
from sys import stdin

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

cnt = Counter(A)

tmp = cnt.most_common()


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


sumall = 0

for i in range(len(cnt)):
    sumall += comb(tmp[i][1])

for a in A:
    ans = sumall - (comb(cnt[a]) - comb(cnt[a] - 1))
    print(ans)

Submission status: Accepted

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

def main():
    n=int(input())
    a=list(map(int,input().split()))
    l=[0]*(n+1)
    s=0
    for i in range(n):
        l[a[i]]+=1
    for i in range(n+1):
        if l[i]==0:
            continue
        else:
            s+=l[i]*(l[i]-1)//2
    for i in range(n):
        print(s-(l[a[i]]-1))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

d = dict()
for e in a:
    if e in d:
        d[e] += 1
    else:
        d[e] = 1

s = 0
for e in d:
    s += d[e]*(d[e]-1)//2

for e in a:
    if d[e] <= 1:
        print(s)
    else:
        k = d[e]
        res = s - k*(k-1)//2 + (k-1)*(k-2)//2
        print(res)

Submission status: Accepted

Solution code:
from copy import deepcopy

n = int(input().strip())
a = list(input().split())
b = deepcopy(a)
counts = {}
tmp = b[0]
for elem in b:
    if elem in counts:
        counts[elem] += 1
    else:
        counts[elem] = 1
    if tmp != elem:
        tmp = elem
c = deepcopy(counts)
for k, v in counts.items():
    c[k] = v * (v - 1) // 2
su = sum(c.values())
for elem in a:
    print(su - c[elem] + ((counts[elem] - 1) * (counts[elem] - 2) // 2))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt_dict = defaultdict(int)
for a in A:
    cnt_dict[a] += 1

total = 0
for v in cnt_dict.values():
    total += v * (v - 1) // 2

for a in A:
    print(total - (cnt_dict[a] - 1))

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
tmp = {}
sum = 0
for j in c:
  tmp[j] = c[j] * (c[j] - 1) // 2
  sum += c[j] * (c[j] - 1) // 2
for i in range(n):
  print((c[a[i]] - 1) * (c[a[i]] - 2) // 2 + sum - tmp[a[i]])

Submission status: Accepted

Solution code:
import math
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)


N = ni()
A  = nl()

dict ={}

for a in A:
    if a in dict:
        x = dict[a]
        dict[a] = [ x[0]+1, -1,-1]        
    else:
        dict[a]=[1,-1,-1]

#print(dict)
sum=0
for a in A:
    x = dict[a]
    if x[1] == -1:
        n = x[0]
        dict[a]=[n,n*(n-1)//2,n-1]
        sum = sum + n*(n-1)//2

#print(dict)

#print(sum)
for a in A:
    print(sum-dict[a][2])

Submission status: Accepted

Solution code:
n=int(input())
inp=list(map(int,input().split()))
hoge=[0 for i in range(n)]
huga=[0 for i in range(n)]
for i in inp:
  huga[i-1]+=1
  hoge[i-1]=(huga[i-1]-1)*huga[i-1]
some=sum(hoge)//2
for i in inp:
  if(huga[i-1]==2):
    print(some-1)
  else:
    print(some-int(hoge[i-1]/huga[i-1]))

Submission status: Accepted

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

s = 0
d = defaultdict(int)
for e in A:
    d[e] += 1
k = len(set(d))

for v in d.values():
    s += v*(v-1)//2

for e in A:
    if d[e] > 1:
        res = s - (d[e]-1)
        print(res)
    else:
        print(s)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter, defaultdict

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

C = Counter(A)
Normal = defaultdict(int)
Banned = defaultdict(int)

for i in A:
    Normal[i] = C[i] * (C[i]-1) // 2
    Banned[i] = (C[i]-1) * (C[i]-2) // 2
    
Normal_sum = sum(Normal.values())
for i in A:
    print(Normal_sum - (Normal[i] - Banned[i]))

Submission status: Accepted

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

B = list(set(A))
counter = Counter(A)
total = 0

for i in range((len(B))):
    n = counter[B[i]]
    total += n*(n-1)/2

for i in range((len(A))):
    n = counter[A[i]]
    ans = total -(n-1)
    print ans

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def solv(N, A):
    d = dict()
    for a in A:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    total = 0
    for a, n in d.items():
        total += (n * (n-1)) // 2
    for a in A:
        n = d[a]
        print(total - (n-1))


if __name__ == "__main__":

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

    solv(N, A)

Submission status: Accepted

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): #startline-------------------------------------------
    n = int(input())
    a = list(map(int,input().split()))
    d = {}
    for i in range(n):
        if not a[i] in d:
            d[a[i]] = 1
        else:
            d[a[i]] += 1
    subans = 0
    for i in d.values():
        if i >= 2:
            subans += i*(i-1)//2
        else:
            continue
    for i in range(n):
        if d[a[i]] >= 2:
            print(subans-(d[a[i]]*(d[a[i]]-1)//2)+((d[a[i]]-1)*(d[a[i]]-2)//2))
        else:
            print(subans)

if __name__ == "__main__":
    main() #endline===============================================

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
N = [0 for i in range(2*(10**5)+3)]
b = a.copy()
b.sort()
sum = 0
count = 1
for i in range(1,n):
    if b[i] != b[i-1]:
        sum += count*(count-1)//2
        N[b[i-1]]=count
        count=1
    else:
        count+=1
N[b[n-1]] = count
sum += count*(count-1)//2
for i in range(n):
    print(sum + (N[a[i]]-1) * (N[a[i]]-2) // 2 - N[a[i]] * (N[a[i]]-1)//2)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

cA = Counter(A)
pairsumc = 0
for k, v in cA.items():
    #print(k, v, v*(v-1)/2)
    pairsumc += v*(v-1)/2
#print(pairsumc)
for a in A:
    print(int(pairsumc - cA[a]+1))

Submission status: Accepted

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

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

Q = {}
for a in A:
    if not(a in Q):
        Q[a] = 0

    Q[a] += 1

S = sum(q*(q-1)//2 for q in Q.values())

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

Submission status: Accepted

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

a=sorted(aa)
sum=0
mae=0
index=[0 for i in range(n)]
for i in range(n-1):
    if a[i]!=a[i+1]:
        index[a[i]-1]=i-mae+1
        sum+=(i-mae+1)*(i-mae)/2
        mae=i+1
index[a[n-1]-1]=n-mae
sum+=(n-mae)*(n-mae-1)/2

for ii in aa:
    ans=sum-index[ii-1]+1
    print(int(ans))

Submission status: Accepted

Solution code:
# coding: utf-8

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

val_cnt = [0] * N

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


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


base_sum = 0
for i in range(N):
    base_sum += comb2(val_cnt[i])

# print(base_sum)
# print(val_cnt)
# print()

for i in range(N):
    print(base_sum + comb2(val_cnt[A[i]-1]-1) - comb2(val_cnt[A[i]-1]))

Submission status: Accepted

Solution code:
# ABC159d


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

    from collections import Counter
    import math

    def c_count(n, r):  # 組み合わせ
        if n - r < 0:
            return 0
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    class Combination:  # メモ化組み合わせ
        def __init__(self):
            super().__init__()
            self.dp = dict()

        def count(self, n, r):
            if self.dp.get((n, r)) != None:
                return self.dp[(n, r)]
            self.dp[(n, r)] = c_count(n, r)
            return self.dp[(n, r)]

    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    d = Combination()
    e = dict()

    ss = 0

    for i in c:
        #print(i, c[i])
        e[i] = d.count(c[i], 2)
        # print(e[i])
        ss += e[i]

    for i in range(n):
        t = e[a[i]]
        w = t * (c[a[i]] - 2) // c[a[i]]
        #print(a[i], t, w)
        w = w if w >= 0 else 0
        print(ss-t+w)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    num_list = [0 for _ in range(n + 1)]
    for _a in a:
        num_list[_a] += 1
    
    ans_plus_n = sum([_num * (_num - 1) // 2 for _num in num_list])
    
    for _a in a:
        ans = ans_plus_n - (num_list[_a] - 1)
        print(ans)

    return

if __name__ == "__main__":
    solve()