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: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n=ii()
    a=lmis()
    nlis=[0]*(n+1)
    cnt=0
    x=0
    for i in range(n): nlis[a[i]]+=1
    for i in range(n): cnt+=nlis[i]*(nlis[i]-1)//2
    for i in range(n):
        total=cnt
        x=a[i]
        cnt-=nlis[x]*(nlis[x]-1)//2
        cnt+=(nlis[x]-2)*(nlis[x]-1)//2
        print(cnt)
        cnt=total

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def nCk(n, k, m=10**9+7):
    a = b = 1
    for i in range(k):
        a = (a * (n-i)) % m
        b = (b * (i+1)) % m
    return (a * pow(b, m-2, m)) % m

n = int(input())
a = list(map(int, input().split()))
b = {}
c = {}
for i in a:
    if i not in b:
        b[i] = 0
    b[i] += 1
for k, v in b.items():
    if v != 1:
        c[k] = nCk(v, 2)
for i in range(n):
    ans = 0
    for k, v in c.items():
        if a[i] != k:
            ans += v
        else:
            ans += nCk(b[k] - 1, 2)
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from itertools import groupby

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    
    MOD = 10 ** 9 + 7
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]
    
    for i in range(2, N+10**5):
        fact.append((fact[-1] * i) % MOD)
        inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
        factinv.append((factinv[-1] * inv[-1]) % MOD)
    
    def cmb(n, r):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return fact[n] * factinv[r] * factinv[n-r] % MOD
    
    B = sorted(A)
    d = defaultdict(int)
    gr = groupby(B)
    
    s = 0
    for key, group in gr:
        c = len(list(group))
        d[key] = c
        s += cmb(c, 2)

    for a in A:
        ans = s - cmb(d[a], 2) + cmb(d[a]-1, 2)
        print(ans)
    
# print(solve())

# def solve()が@return voidのとき
solve()

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())
A = list(map(int,input().split()))
AS = set(A)
print(AS)
def comb(n):
    return n*(n-1)//2

L = collections.Counter(A)

for i in range(N):
    ans = 0
    for a in AS:
        if A[i] == a:
            ans += comb(L[a]-1)
        else:
            ans += comb(L[a])
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from collections import Counter


def resolve():
    # S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    N = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    # N, D = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    a_list = [int(x) for x in sys.stdin.readline().split()]  # 複数int

    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    # v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)]
    # grid = [[int(x) for x in sys.stdin.readline().split()]
    #         for _ in range(N)]  # int grid

    logger.debug('{}'.format([]))

    def cmb(n, r, mod):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return g1[n] * g2[r] * g2[n - r] % mod

    mod = 10**12 + 7  #出力の制限
    g1 = [1, 1]  # 元テーブル
    g2 = [1, 1]  #逆元テーブル
    inverse = [0, 1]  #逆元テーブル計算用テーブル

    for i in range(2, N + 1):
        g1.append((g1[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod // i)) % mod)
        g2.append((g2[-1] * inverse[-1]) % mod)

    counts = Counter(a_list)

    d = dict()
    for k in range(N):
        to_remove = a_list[k]
        retval = d.get(to_remove, 0)
        if retval != 0:
            print(retval)
            continue
        for a, c in counts.items():
            if a == to_remove:
                c -= 1

            retval += cmb(c, 2, mod)
        print(retval)


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)

Submission status: Wrong Answer

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


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

sumN = 0
for i in range(N):
    sumN = sumN + int(count[i]*(count[i]-1)/2)

#print answer
for i in range(N):
    id = A[i]
    sumz = sumN - int(count[id]*(count[id]-1)/2)
    sumz = sumz + int((count[id] - 1)*(count[id] - 2)/2)
    print(sumz)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

dic = defaultdict(int)
ans1 = defaultdict(int)
ans2 = defaultdict(int)

def calc_patern1(n, ans, dic):
    for k, v in dic.items():
        ans[k] = v * (v - 1) / 2


def calc_patern2(n, ans, dic):
    for k, v in dic.items():
        ans[k] = (v - 1) * (v - 2) / 2

for i in range(n):
    dic[a[i]] += 1
for v in dic.values():
    calc_patern1(a[i], ans1, dic)
    calc_patern2(a[i] - 1, ans2, dic)
print(ans1, ans2)
for i in range(n):
    ans = 0
    for k, v in dic.items():
        #print(ans)
        if k == a[i]:
            ans += ans2[k]
        else:
            ans += ans1[k]
    print(int(ans))

Submission status: Wrong Answer

Solution code:
import math

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

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

memo = [-1] * (N+1)
dic = dict()

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

tmp = 0
for d in dic.values():
    if d >= 2:
        if memo[d] != -1:
            t = memo[d]
        else:
            memo[d] = combinations_count(d,2)
            t = memo[d]
        tmp += t 
        #print(f'{tmp=}')

for a in A:
    ans = 0
    t = dic[a]
    if t >= 2:
        if memo[t] != -1:
            ans = tmp - memo[t]
        else:
            memo[t] = combinations_count(t,2)
            ans = tmp - memo[t]
    if t-1 >= 2:
        if memo[t-1] != -1:
            ans += memo[t-1]
        else:
            memo[t-1] = combinations_count(t-1,2)
            ans += memo[t-1]

    print(ans)

Submission status: Wrong Answer

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

ok=[0]*(N+1)

# print(ok)
for i in range(N):
    ok[A[i]]+=1
# print(ok)

ans=0
sum=0
for i in range(N):
    if ok[i]!=0:
        sum+=int(ok[i]*(ok[i]-1)/2)
# print("sum ",sum)
for i in range(N):
    # print(i,A[i])
    if ok[A[i]]<=1:
        ans=sum
    elif ok[A[i]]==2:
        ans=sum-1
    else:
        ans=sum-int(ok[A[i]]*(ok[A[i]]-1)/2)+int((ok[A[i]]-1)*(ok[A[i]]-2)/2)
    print(ans)

Submission status: Wrong Answer

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(N):
    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: Wrong Answer

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

    cnt = [0] * (2 * 10 ** 5 + 1)
    for i in range(N):
        cnt[A[i]] += 1

    cnt_comb = 0
    for i in range(N):
        cnt_comb += calcComb(cnt[i])
    
    for i in range(N):
        print(max(0,cnt_comb - (calcComb(cnt[A[i]]) - calcComb(cnt[A[i]]-1))))

def calcComb(n):
    if n == 0:
        return 0
    return (n*(n-1)) // (2*1)
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]

N = I()
A = LI()

def ncr(n, r, p=MOD):
    a = 1
    b = 1
    for i in range(r):
        a = (a * (n - i) % p)
        b = (b * (r - i) % p)

    return a * pow(b, p - 2, p) % p

tmp = 0
AA = collections.Counter(A)
As = AA.items()
for n, t in As:
    tmp += ncr(t, 2)

for a in A:
    an = AA[a]
    print(tmp - ncr(an, 2) + ncr(an - 1, 2))

Submission status: Wrong Answer

Solution code:
M=int(input())
N=list(input().split())
print(N)
check=[0]*M
for i in range(M):
    check[int(N[i])-1]+=1
num=0
for i in range(M):
    num+=check[i]*(check[i]-1)//2
for i in range(M):
    print(num-(check[int(N[i])-1]-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    print(A.count(A[i])-1)

Submission status: Wrong Answer

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):
    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: Wrong Answer

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

a = list(set(A))
a_n = []
c = []
if len(a) != N:
  for i in range(len(a)):
    x = A.count(a[i])
    a_n.append(x)
    c.append(x*(x-1)//2)

  ans = sum(c)
  print(c)
  for i in range(N):
    n = a.index(A[i])
    new = (a_n[n]-1)*(a_n[n]-2)//2
    print(ans-c[n]+new)
else:
  for _ in range(N):
    print(0)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def cmb(n, r, mod=10**9+7):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**5 * 2 + 1
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



N = int(input())
ball_com = [0] * (N + 1)
balls = list(map(int, input().split()))
balls_counter = Counter(balls)
for key , val in balls_counter.items():
    ball_com[key - 1] = cmb(val, 2)

cmb_sum = sum(ball_com)
for ball in balls:
    print(cmb_sum - ball_com[ball - 1] + cmb(balls_counter[ball] - 1, 2))

Submission status: Wrong Answer

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):
    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: Wrong Answer

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):
  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: Wrong Answer

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

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

for c in C.values():
    if c < 1:
        continue
    S.setdefault(c, c * (c - 1) // 2)
    if c < 2:
        continue
    S.setdefault(c - 1, c - 1 * (c - 2) // 2)

for i, a in enumerate(A):
    C[a] -= 1
    sum_ = sum([S[n] for n in C.values() if n > 1])
    C[a] += 1
    print(sum_)

Submission status: Wrong Answer

Solution code:
import math

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

n = int(input())
a = sorted(map(int,input().split()))
cnt = []
cnt1 = 1
for i in range(1,n):
    if a[i] == a[i-1]:
        cnt1 += 1
        if i == n-1:
            cnt.append(cnt1)
    else:
        cnt.append(cnt1)
        cnt1 = 1
        if i == n-1:
            cnt.append(cnt1)

cmb = {}

for i in range(len(cnt)):
    if cnt[i] >= 2:
        cmb[cnt[i]] = combinations_count(cnt[i],2)

cmb_all = sum(cmb.values())

for i in range(len(cnt)):
    if cnt[i] == 1:
        print(cmb_all)
    elif cnt[i] == 2:
        print(cmb_all - 1)
        print(cmb_all - 1)
    else:
        ans = cmb_all - cmb[cnt[i]] + combinations_count(cnt[i]-1, 2)
        for j in range(cnt[i]):
            print(ans)

Submission status: Wrong Answer

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


def Comb(S):
    n = len(S)
    number = 0
    for i in range(n):
        for j in range(i, n):
            if S[i] == S[j]:
                number += 1
    return number


for k in range(N):
    print(Comb(A[0:k]+A[k+1:]))

Submission status: Wrong Answer

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] = 0

for a in a_list:
  print(a_dict[a])

Submission status: Wrong Answer

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):
  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: Wrong Answer

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):
    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: Wrong Answer

Solution code:
n=int(input())
As = input().split()

dic = {}
for a in As:
  if a not in dic:
    dic[a] = 0
  dic[a] += 1

tmp = 0
for k, v in dic.items():
  if v > 1:
    tmp += v * (v-1)/2

ans = []
for a in As:
  if dic[a] > 1:
    div = dic[a] * (dic[a] - 1) / 2
    kakeru = (dic[a]-1) * (dic[a]-2)/2
    ans.append(int(tmp - div + kakeru))
  else:
    ans.append(tmp)
for a in ans:
  print(a)

Submission status: Wrong Answer

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):
        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: Wrong Answer

Solution code:
import math

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

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

dic = dict()

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

tmp = 0
for d in dic.values():
    if d >= 2:
        tmp += combinations_count(d,2)
        #print(f'{tmp=}')

for a in A:
    ans = 0
    t = dic[a]
    if t >= 2:
        ans = tmp - combinations_count(t,2)
    if t-1 >= 2:
        ans += combinations_count(t-1,2)

    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
def solve():
    N = int(input())
    A = list(map(int,input().split()))

    cnt = [0] * (2 * 10 ** 5 + 1)
    for i in range(N):
        cnt[A[i]] += 1

    cnt_comb = 0
    for i in range(N):
        cnt_comb += len(list(combinations(range(cnt[i]), 2)))
    
    for i in range(N):
        print(max(0,cnt_comb - (cnt[A[i]]-1)))

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

    class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ['mod', 'factorial', 'inverse']

        def __init__(self, max_val_arg: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_val_arg + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_val_arg, 0, -1):
                inv_append((inv[-1] * i) % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod

    count = Counter(A)
    comb = Combination(N).combination
    foo = sum(comb(c, 2) for c in count.values())

    ans = []
    for a in A:
        v = count[a]
        tmp = foo - comb(v, 2) + comb(v - 1, 2)
        ans.append(tmp)
    return '\n'.join(map(str, ans))

print(d_banned_k())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

count={}
for i in range(len(arr)):
    val=arr[i]
    if val not in count.keys():
        count[val]=1
    else:
        count[val]+=1

big_sum=0
for d in count.keys():
    big_sum+= count[d]*(count[d]-1)/2

for a in arr:
    if a in count.keys():
        if count[a]==1:
            print(0)
        else:
            print(int(big_sum-(count[a]-1)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = {}
aa = [0]
for i in range(1,n):
	aa.append(aa[-1]+i)
for i in a:
	if i not in d:
		d[i] = 1
	else:
		d[i]+=1
s = 0
for i in d:
	s+=aa[d[i]-1]
dd = {}
for i in d:
	if i not in dd:
		if d[i]!=1:
			dd[i] = (s-aa[d[i]-1]+aa[d[i]-2])
		else:
			dd[i] = 0
print(dd)
for i in a:
	print(dd[i])

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    cnt=[0]*n
    for i in range(n):
        cnt[a[i]-1]+=1
    res=0
    for i in range(n):
        if cnt[i]>1:
            res+=cnt[i]*(cnt[i]-1)//2
    ans=[0]*n
    for i in range(n):
        if cnt[i]>1:
            ans[i]=res-(cnt[i]-1)
    for i in range(n):
        print(ans[a[i]-1])
        
if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
freq = [0] * (2 * (10**5) + 5)
for i in l: freq[i] -= -1
count = 0
for i in range(10**5 + 5):
    if freq[i] >= 2:
        temp = (freq[i] * (freq[i] - 1)) // 2
        count += temp
for i in l:
    if freq[i] == 1: print(count)
    else:
        temp = freq[i] - 1
        if temp == 1: print(count - 1)
        else:
            yoyo = ((freq[i] * (freq[i] - 1)) // 2) - ((temp * (temp - 1)) // 2)
            print(count - yoyo)

Submission status: Wrong Answer

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 
for i in range(0,n):
    print(d[a[i]]-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from itertools import groupby

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    
    MOD = 10 ** 9 + 7
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]
    
    # 10^7程度が上限
    # 必要なだけforを回す。N+1をa+1に書き換える
    for i in range(2, N+1):
        fact.append((fact[-1] * i) % MOD)
        inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
        factinv.append((factinv[-1] * inv[-1]) % MOD)
    
    # nCr n個から重複を許さずr個の組み合わせを選ぶ
    # n!を求めている必要があるので、nが大きい場合（n=10^9など）はTLEになる
    # cmb = n! * (n-r)!^-1 * r!^-1
    def cmb(n, r):
        if (r < 0) or (n < r):
            return 0
        r = min(r, n - r)
        return fact[n] * factinv[r] * factinv[n-r] % MOD
    
    B = sorted(A)
    d = defaultdict(int)
    gr = groupby(B)
    
    s = 0
    for key, group in gr:
        c = len(list(group))
        d[key] = c
        s += cmb(c, 2)

    for a in A:
        ans = s - cmb(d[a], 2) + cmb(d[a]-1, 2)
        print(ans)
    
# print(solve())

# def solve()が@return voidのとき
solve()

Submission status: Wrong Answer

Solution code:
n=int(input())

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

X=[0]*(n+1)

for i in range(n):
    X[A[i]]+=1

ans=0

for i in range(n):
    if X[i]>1:
        ans+=X[i]*(X[i]-1)//2      
for i in range(n):
    if X[A[i]]==1:
        print(ans)
    elif X[A[i]]>1:
        print(ans-(X[A[i]]-1))

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int, input().split()))
a2=list(set(a))
a3=sorted(a2)
for i in a:
    p=0
    for j in a3:
        s=a.count(j)
        if j==i:
            s-=1
        p+=(s*(s-1))/2
    print(p)

Submission status: Wrong Answer

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):
    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-sabun)
    else:
        print(zenbu)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from collections import Counter


def resolve():
    # S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    N = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    # N, D = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    a_list = [int(x) for x in sys.stdin.readline().split()]  # 複数int

    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    # v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)]
    # grid = [[int(x) for x in sys.stdin.readline().split()]
    #         for _ in range(N)]  # int grid

    logger.debug('{}'.format([]))

    def cmb(n, r, mod):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return g1[n] * g2[r] * g2[n - r] % mod

    mod = 10**9 + 7  #出力の制限
    g1 = [1, 1]  # 元テーブル
    g2 = [1, 1]  #逆元テーブル
    inverse = [0, 1]  #逆元テーブル計算用テーブル

    for i in range(2, N + 1):
        g1.append((g1[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod // i)) % mod)
        g2.append((g2[-1] * inverse[-1]) % mod)

    counts = Counter(a_list)

    for k in range(N):
        to_remove = a_list[k]
        retval = 0
        for a, c in counts.items():
            if a == to_remove:
                c -= 1

            retval += cmb(c, 2, mod)
        print(retval)


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    A=list(map(int,readline().split()))

    li=[[0,0] for _ in range(N+1)]
    di=dict()
    for i in range(N):
        if A[i] not in di:
            di[A[i]]=1
        else:
            di[A[i]]+=1
    
    for k,v in di.items():
        li[k][0]=v*(v-1)//2
        li[k][1]=(v-1)*(v-2)//2

    s=0
    for i in range(N):
        s+=li[i][0]

    for i in range(N):
        print(s-li[A[i]][0]+li[A[i]][1])
if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math

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

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

dic = dict()

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

tmp = 0
for d in dic.values():
    if d >= 2:
        tmp += combinations_count(d,2)
        #print(f'{tmp=}')

for a in A:
    ans = 0
    t = dic[a]
    if t >= 2:
        ans = tmp - combinations_count(t,2)
    if t-1 >= 2:
        ans += combinations_count(t-1,2)

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    A=list(map(int,readline().split()))

    li=[[0,0] for _ in range(N+1)]
    di=dict()
    print(li)
    for i in range(N):
        if A[i] not in di:
            di[A[i]]=1
        else:
            di[A[i]]+=1
    
    for k,v in di.items():
        li[k][0]=v*(v-1)//2
        li[k][1]=(v-1)*(v-2)//2

    s=0
    for i in range(N):
        s+=li[i][0]

    for i in range(N):
        print(s-li[A[i]][0]+li[A[i]][1])
if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

counter = Counter(A)
total = 0

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
A_array = map(int, input().split())
Ai_dict = Counter(A_array)


def calc_comb(Ai, dictionary):
    num = 0
    for k,v in dictionary.items():
        if k == Ai:
            N_Ai = v -1 #1こ抜いた数. 
        else:
            N_Ai = v
        num += N_Ai*(N_Ai-1)/2
    return int(num)

for n in A_array:
    print(calc_comb(n, Ai_dict))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(s) for s in input().split()]
for i in range(0,n):
    print(a.count(a[i])-1)

Submission status: Wrong Answer

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(0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

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

ct = collections.Counter(A)

ret = []
total = 0
for k,count in ct.items():
    if count > 1:
        total += count * (count-1) // 2

for i,v in enumerate(A):
    if ct[v] > 1:
        count = ct[v]
        adjust = (count-1) * (count-2) // 2 - (count-1) * count // 2
        val = total + adjust
    else:
        val = 0
    print(val)

Submission status: Wrong Answer

Solution code:
def fun(a):
    return a*(a-1)//2
import sys
def main():
    n = int(input())
    l = [int(j) for j in sys.stdin.readline().split()]
    d = dict()
    for i in range(n):
        if l[i] in d:
            d[l[i]]+=1
        else:
            d[l[i]]=1
    print(d)
    ans = 0
    for key in d:
        ans+=fun(d[key])
    # a = len(d)*(len(d)-1)//2
    # b = (len(d)-1)*(len(d)-2)//2
    for i in range(n):
        if d[l[i]]==1:
            print(ans)
        else:
            print(ans - fun(d[l[i]]) + fun(d[l[i]]-1))
main()