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 collections


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

    counter = collections.Counter(A)
    ans = 0

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

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


resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
An = [int(x) for x in input().split()]  
 
# ボールを全部使えるときの合計を求める
s = 0
for i in range(1,N+1):
    b = An.count(i)
    if b > 1:
        s += b*(b-1) / 2

for k in range(N):
    print(s - (An.count(An[k])-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
C = [0 for i in range(N)]
for value in A:
    C[value - 1] += 1
print(C)
ways_all = 0
for i in range(N):
    ways_all += (C[i] * (C[i] - 1)) // 2
print(ways_all)
for value in A:
    print(ways_all - C[value - 1] + 1)

Submission status: Wrong Answer

Solution code:
import collections


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

    counter = collections.Counter(A)
    ans = 0

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

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

resolve()

Submission status: Wrong Answer

Solution code:
n=int(input())
S=list(map(int,input().split()))
set_S=list(set(S))
ans_stock={}
number={}
for i in set_S:
  number[i] = S.count(i)
comb = 0
for i in set_S:
  comb += number[i]*(number[i]-1)/2

for i in S:
  if i in list(ans_stock.keys()):
    print(ans_stock[i])
  else:
    ans = comb -  number[i]*(number[i]-1)/2 +  (number[i]-1)*(number[i]-2)/2
    print(ans)
    ans_stock[i]=ans

Submission status: Wrong Answer

Solution code:
import collections

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

c=collections.Counter(a)
num=0
for i in c.values():
    num+=i*(i-1)/2
    
for i in range(x):
    print(num-c[a[i]]+1)

Submission status: Wrong Answer

Solution code:
import math
from collections import Counter

def cmb(n,r):
    if n < r:
        return 0
    return math.factorial(n)

def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter(A)
    sum_ = sum([cmb(v,2) for v in count.values()])

    for i in range(len(A)):
        remain = A[:i] + A[i+1:]
        took_count = count[A[i]]
        sub = took_count - 1
        print(int(sum_ - sub))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

b=np.zeros(N)

for i in range(N):
  b=np.zeros(N)
  b[i]=A.count(i+1)
  tmp=0
  for j in range(N):
    if(i==j):
      tmp=tmp+(b[j]-1)*(b[j]-2)/2
    else:
      tmp=tmp+b[j]*(b[j]-1)/2
  print(int(tmp))

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
lin = [0]*(n+1)
for i in li:
    lin[i] += 1
liv = [0]*(n+1)
point = 0
for j,k in enumerate(lin):
    point += k*(k-1)//2
    liv[j] += k*(k-1)//2
for i in li:
    if lin[i] < 2:
        print(0)
        continue
    print(point-liv[i]+(lin[i]-1)*(lin[i]-2)//2)

Submission status: Wrong Answer

Solution code:
from collections import*
d={}
n,*a=map(int,open(0).read().split())
b=Counter(a)
for i in range(n):
    c=(b-Counter([a[i]]))
    if a[i]not in d:
        ans=sum([v*(v-1)//2 for k,v in c.items()if v>=2])
        d[a[i]]=ans
    else:
        print(d[a[i]])

Submission status: Wrong Answer

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

z = []
for i in range(len(a3)):
     z.append(a.count(a3[i]))

suma = sum([i*(i-1)/2 for i in z])

for j in range(n):
    print(int(suma - z[a3.index(a[j])]))

Submission status: Wrong Answer

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
l=[0]*(n+1)
ans=0
for i in range(n):
    l[a[i]]+=1
for j in range(n):
    if l[j]>1:
        ans+=(l[j])*(l[j]-1)//2
for k in range(n):
    s=l[a[k]]
    if s<2:
        print(ans)
    else:
        print(ans-(s+1)*s//2+s*(s-1)//2)

Submission status: Wrong Answer

Solution code:
import math

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

C = list(set(A))
M = len(C)
t = []
anst = 0
for i in range(M):
  t.append(A.count(C[i]))
  temp = A.count(C[i])
  if temp >=2:
    anst += math.factorial(temp)/(math.factorial(temp-2)*2)

#print(t)
#print(anst)
#print(jisyo[1])
for i in range(N):
  print(t[C.index(A[i])])
  print(int(anst-t[C.index(A[i])]+1))

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

# kごとに、
# 異なる数がいくつあるかを調べる
# (2以上要素が存在する)異なる数ごとに、
# (要素数)C2を解く。

np_A = np.array(A)

# 辞書作成
dic = {}


# 全体を求める
tmp_all = np.array(A)
unique = np.unique(tmp_all)
print("unique:", unique)
ans_all = 0
for u in unique:
    count_u = np.count_nonzero(tmp_all==u)
    # print(f"u:{u}, count_u:{count_u}")
    if count_u < 2:
        continue
    # (count_u) C 2
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp
# print("all", ans_all)


# kごとのケースを求めて、全体から引く
for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)

    k_unique = np.unique(tmp_k)
    ans = 0

    k_num = A[k]
    count_u = np.count_nonzero(tmp_k==k_num)
    if count_u < 2:
        ans_k = 1
    else:
        ans_k = int(count_u*(count_u-1)/2)
    
    print(ans_all-ans_k)

Submission status: Wrong Answer

Solution code:
import math

def countComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
NN = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, NN + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

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

memo = [0 for _ in range(N)]
for a in A:
  memo[a-1] += 1

memo1 = [0 for _ in range(N)]
for n in range(N):
  memo1[n] = countComb(memo[n], 2, p)

total = sum(memo1)
for k in range(N):
  a = A[k]-1
  answer = total - memo1[a] + 1
  print(answer)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())

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

c = Counter(a_list)

all_count = 0
for value in c.values():
    if value == 1:
        continue
    else:
        tmp = int((value * (value - 1))/2)
        all_count += tmp

if all_count == 0:
    all_count = 1

for i in a_list:
    print(all_count - c[i] + 1)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    N = int(input())
    A = input().split()
    all = len(tuple(filter(lambda t: t[0] == t[1], itertools.combinations(A, 2))))
    for x in range(N):
        count = A.count(A[x])
        tmp = tuple(["0" for _ in range(count)])
        result = tuple(filter(lambda t: t[0] == t[1], itertools.combinations(tmp, 2)))
        print(all-len(result))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
import collections
import math
def combinations_count(n):
    return math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))

ans = {}
c = collections.Counter(a)
max_value = int(sum([combinations_count(i) for i in c.values() if i>1]))
for k in range(n):
    key = a[k]
    if key in ans:
        print(ans[key])
    else:
        value = 1
        if c[key]>2:
            value = combinations_count(c[key]-1)
        ans[key] = max_value - value
        print(ans[key])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
nCr = {}
def cmb(n):
    if n == 1:
        return 0
    else:
        return n * n-1 /2
mydict = {}
for i in range(N):
    a = A[i]
    if a in mydict.keys():
        mydict[a] += 1
    else:
        mydict[a] = 1
s = 0
for i in range(N):
    a = A[i]
    s += cmb(a)
for i in range(N):
    a = A[i]
    x = mydict[a] - 1
    print(s-x)

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())
num_list = list(map(int,input().split()))
memo = [0]*(N+1)
memo_flg = [False]*(N+1)
counter = [0] * (N+1)
counter_ans = [0] * (N+1)

for i in num_list:
    counter[i] += 1
for i in range(N):
    counter_ans[i] = (counter[i]*(counter[i]-1))//2

all_sum = sum(counter_ans)
def calc(k):
    num = num_list[k]
    
    if(memo_flg[num] is True):
        return memo[num]
    ans = 0
    
    wrong = counter_ans[num]
    c = counter[num]-1
    right = (c*(c-1))//2
    
    ans = all_sum - wrong + right
    memo[num] = ans
    memo_flg[num] = True
    return ans

[print(calc(i)) for i in range(N)]

Submission status: Wrong Answer

Solution code:
import collections

def check(A):
    count = 0
    for j in A:
        n = A[j]
        if n > 1:
            count += n * (n - 1) / 2
            
    return int(count)


N = int(input())
A = list(map(int, input().split(" ")))
A2 = collections.Counter(A)
print(A2)
Nsum = check(A2)
# print(Nsum)
for i in range(0,N):
    num = A[i]
    count = A2[num]
    out = Nsum - ( count - 1 )
    print(out)

Submission status: Wrong Answer

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

c = []
sum = 0

for j in A:
    _ = A.count(j)
    c.append(_)
    sum += int(_ * (_ - 1) / 2)

s = ""
for k in range(N):
    s += str(sum - c[k] + 1) + "\n"
print(s)

Submission status: Wrong Answer

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
a = list(set(ao))
ac = 0
acount = []
for i in a:
    c = ao.count(i)
    ac += sum([j for j in range(1, c)])
    acount.append(c+1)
for i in ao:
    ind = a.index(i)
    print(ac-acount[ind])

Submission status: Wrong Answer

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

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

for k in range(N):
    print(tmp-B[A[k]]+1)

Submission status: Wrong Answer

Solution code:
def comb2(a):
  if a < 2: return 0
  return a * (a - 1) / 2
N = int(input())
A = list(map(int, input().split()))
dp = [[0, 0] for i in range(N+1)]
for i in range(N):
  dp[A[i]][0] += 1
x = 0
for i in range(N):
  dp[i][1] = comb2(dp[i][0])
  x += dp[i][1]
for i in range(N):
  print(int(x - (dp[A[i]][0]-1)))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int, input().split()))
set_l = set(l)

dictionary = {ll: l.count(ll) for ll in set_l}
ll_dict = {}
ll2_dict = {} # 1つ抜け版 

for k, v in dictionary.items():
  ll_dict[k] = v*(v-1)//2
for k, v in dictionary.items():
  ll2_dict[k] = (v-1)*(v-2)//2 if v!=2 else 1
sum_num = sum([v for v in ll_dict.values()])

for n in range(N):
  print(sum_num-ll2_dict[l[n]])

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())
num_list = list(map(int,input().split()))
memo = [0]*(N+1)
memo_flg = [False]*(N+1)
counter = [0] * (N+1)
counter_ans = [0] * (N+1)

for i in range(N):
    counter_ans[i] = (counter[i]*(counter[i]-1))//2

for i in num_list:
    counter[i] += 1

all_sum = sum(counter_ans)
def calc(k):
    num = num_list[k]
    
    if(memo_flg[num] is True):
        return memo[num]
    ans = 0
    
    wrong = counter_ans[num]
    c = counter[num]-1
    right = (c*(c-1))//2
    
    ans = all_sum - wrong + right
    memo[num] = ans
    memo_flg[num] = True
    return ans

[print(calc(i)) for i in range(N)]

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
from collections import defaultdict
d = defaultdict(int)
for i in a:
  d[i] += 1

total = 0
for i in set(a):
  x = d[i]
  total += x*(x-1)/2

for i in a:
  x = d[i]
  print(int(total-(x*(x-1)/2)))

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())

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

c = Counter(a_list)

all_count = 0
for value in c.values():
    if value == 1:
        continue
    else:
        tmp = int((value * (value - 1))/2)
        all_count += tmp

if all_count == 0:
    all_count = 1

for i in a_list:
    print(all_count - c[i])

Submission status: Wrong Answer

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

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

print(c)
num = 0
for v in c.values():
  if v > 1:
      num += comb(v, 2,exact=True)
print(num)
for k in range(N):
  print(int(num - (c[A[k]]-1)))

Submission status: Wrong Answer

Solution code:
import sys
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

n = int(input())
l = [int(x) for x in input().split()]

# cl = [0 for i in range(n)]
cl = []
ll = [x for x in set(l) if l.count(x) > 1]

# print(ll)

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (m - 2) / m))

# print(sum)
# print(cl)

def my_index(l, x, default=n):
    if x in l:
        return l.index(x)
    else:
        return default

for i in l:
    # print(sum - cl[i-1])
    idx = my_index(ll, i)
    if idx == n:
        print(0)
    else:
        print(sum - cl[idx])

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())

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

c = Counter(a_list)

all_count = 0
for value in c.values():
    if value == 1:
        continue
    else:
        tmp = int((value * (value - 1))/2)
        all_count += tmp

if all_count == 0:
    all_count = 1

for i in range(n):
    print(all_count - c[i])

Submission status: Wrong Answer

Solution code:
l = int(input())
A = input().split(" ")
unique = set(A)
def combination(number):
  if number <= 1: return 0
  else: return int(number * (number - 1) / 2)

def caluculate(string):
  data = memo[string]
  if data <= 1: return(0)
  else: return(data - 1) 
total = 0
memo = {}
for i in unique:
  times = A.count(i)
  memo[i] = times
  total += combination(times)

Submission status: Wrong Answer

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

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

num = 0
for v in c.values():
  if v > 1:
      num += comb(v, 2)

for k in range(N):
  print(num - (c[k]-1))

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())

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

c = Counter(a_list)

all_count = 0
for value in c.values():
    if value == 1:
        continue
    else:
        tmp = int((value * (value - 1))/2)
        all_count += tmp

for i in range(n):
    print(all_count - c[i])

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())

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

c = Counter(a_list)

all_count = 0
for value in c.values():
    if value == 1:
        continue
    else:
        tmp = int((value * (value - 1))/2)
        all_count += tmp

print(all_count)

for i in range(n):
    print(all_count - c[i])

Submission status: Wrong Answer

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

def combo(n):
  if n<1:
    return 0
  r = min(n-2,2)
  if r == 0: return 1
  over = reduce(mul, range(n, n - 2, -1))
  under = reduce(mul, range(1,2 + 1))
  return over // under

N=int(input())
A=list(map(int,input().split()))
count=Counter(A)
com={}

for k,v in count.items():
  com[k]=combo(v)
  s=sum(com.values())
  for i in range(N):
    su=s-combo(count[A[i]])+combo(count[A[i]]-1)
    print(su)

Submission status: Wrong Answer

Solution code:
import collections

def count_defaultdict(it):
    counter = collections.defaultdict(int)
    for x in it:
        counter[x] += 1
    return dict(counter)


def resolve():
    n = int(input())
    l = list(map(int, input().split()))
    s = list(set(l))
    all_sum = 0
    count = {}
    for i in range(len(s)):
        v = l.count(s[i])
        all_sum += v * (v - 1) // 2
    count = count_defaultdict(l)
    for i in range(len(l)):
        if count[l[i]] > 1:
            v = count[l[i]]
            print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
        else:
            print(all_sum)

Submission status: Wrong Answer

Solution code:
import collections
def count_defaultdict(it):
    counter = collections.defaultdict(int)
    for x in it:
        counter[x] += 1
    return dict(counter)


def main():
    n = int(input())
    l = list(map(int, input().split()))
    s = list(set(l))
    all_sum = 0
    count = {}
    for i in range(len(s)):
        v = l.count(s[i])
        all_sum += v * (v - 1) // 2
    count = count_defaultdict(l)
    for i in range(len(l)):
        if count[l[i]] > 1:
            v = count[l[i]]
            print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
        else:
            print(all_sum)

Submission status: Wrong Answer

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

for i in range(N):
    flag = 0
    for j in range(len(T)):
        if A[i] == T[j][0]:
            T[j][1] += 1
            flag = 1
            break
    if flag == 0:
        T.append([A[i],1])

T2 = []

for i in range(len(T)):
    T2.append(T[i][1])


def hoge(List):
    ans = 0
    for i in range(len(List)):
        ans += (List[i]-1)*List[i]//2
    return ans

T3 = []
Ans = hoge(T2)

print(T[0][1])

for i in range(len(T2)):
    T3.append(Ans-(T2[i]-1))

for i in range(N):
    for j in range(len(T3)):
        if T[j][0] == A[i]:
            print(T3[j])
            break

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())

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

c = collections.Counter(a)
counter = c.most_common()
ans = 0

for j in range(len(counter)):
    ans += int(counter[j][1] * (counter[j][1] - 1) / 2)

for i in range(N):
    sliced_chr = a[i]
    a_slice = a[:i] + a[i+1:]
    
    print(ans - a_slice.count(sliced_chr)+1)

Submission status: Wrong Answer

Solution code:
"""
"""
from math import factorial
from collections import defaultdict


def int_as_array(num): return list(map(int, [y for y in str(num)]))


def array_as_int(arr): return int(''.join(map(str, arr)))


def read_int(): return int(input())


def read_array(): return list(map(int, input().split(' ')))


def array_to_string(arr, sep=' '): return sep.join(map(str, arr))


def matrix_to_string(arr, sep=' '): return '[\n' + '\n'.join(
    [sep.join(map(str, row)) for row in arr]) + '\n]'


cache = {}


def fac(x):
    if cache.get(x) is not None:
        return cache[x]
    ans = factorial(x)
    cache[x] = ans
    return ans


def choose_combination(n, r):
    if r > n:
        return 0
    return int((fac(n) / fac((n - r))) * (1 / fac(r)))


def solve(n, array):
    counts = defaultdict(int)
    for x in array:
        counts[x] += 1
    ways_cache = {}
    for k in array:
        if ways_cache.get(k) is not None:
            return ways_cache[k]
        ways = 0
        for x, count in counts.items():
            adjusted_count = counts[x]
            if x == k:
                adjusted_count -= 1
            ways += choose_combination(adjusted_count, 2)
        ways_cache[k] = ways
        print(ways)


N = read_int()
array = read_array()
solve(N, array)

Submission status: Wrong Answer

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

ans1=0
s=0
for i in range(1,N+1):
    a=A.count(i)
    C.append(a)
for j in range(N):
    c=C[j]
    if c>1:
        s=c*(c-1)/2
        ans1=ans1+s
for k in range(N):
    b=A[k]-1
    ans=ans1-(C[b]+1)
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  import numpy as np
  from collections import Counter

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

  tmp = np.array(list(c.values()))
  l1= tmp*(tmp-1)//2
  l2= (tmp-1)*((tmp-1)-1)//2
  S = np.sum(l1)

  for a in A:
    r = list(c).index(a)
    sum = S + l2[r] -l1[r]
    print(int(sum))

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from operator import mul
from functools import reduce
import numpy as np

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()))
B = A[:]
B.sort()
C = [B[0]]
D = [1]
L = 0
for i in range(N-1):
    if B[i] == B[i+1]:
        D[L] += 1
    else:
        C.append(B[i+1])
        D.append(1)
        L += 1
M = len(C)
D1 = D[:]
for i in range(M):
    if D[i] > 1:
        D[i] = cmb(D[i],2)
    else:
        D[i] = 0
for i in range(M):
    if D1[i] > 2:
        D1[i] = cmb(D1[i]-1,2)
    else:
        D1[i] = 0

D = np.array(D)
D1 = np.array(D1)
E = []
E.append(D1[0]+np.sum(D[1:]))
for i in range(1,M-1):
    E.append(D1[i] + np.sum(D[0:i]) + np.sum(D[i+1:M]))
E.append(D1[M-1] + np.sum(D[:M-1]))
for i in range(N):
    print(E[C.index(A[i])])
print(E)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def main():
    N = int(input())
    A = list(map(int, input().split()))
    uq = list(set(A))
    ans_hash = {}
    num_hash = {}
    for u in uq:
        ans = 0
        a = A[:]
        i = a.index(u)
        del a[i]
        val = Counter(a).values()
        print("val: %s", val)
        for num in val:
            if num in num_hash:
                ans += num_hash[num]
            else:
                num_hash[num] = num * (num - 1) // 2
                ans += num_hash[num]
        ans_hash[u] = ans
    for n in A:
        if n in ans_hash:
            print(ans_hash[n])
        else:
            a = A[:]
            i = a.index(n)
            del a[i]
            val = Counter(a).values()
            for num in val:
                if num in num_hash:
                    ans += num_hash[num]
                else:
                    num_hash[num] = num * (num - 1) // 2
                    ans += num_hash[num]
            ans_hash[n] = ans
            print(ans_hash[n])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input().split()
for i in range(n):
  j=0
  count=0
  text=""
  for s0 in s:
    text= s[:j] + s[j+1:]
    if i==j:
      j+=1
    else:
      j=j+1
      i0=len(text)-len("".join(text).replace(s0,""))
      if i0>1:
        count+= i0-1
        
  print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
li = list(map(int, input().split()))
j1=0
#print(li)
li.sort()
#print(li)
for i in range(len(li)):
    goukei = 0
    x = li[i]
    del li[i]
    #print(li)
    for j in li:
        if j != j1:
            goukei += li.count(j) * (li.count(j)-1) / 2
        j1 = j
    li.insert(i, x)
    print(int(goukei))

Submission status: Wrong Answer

Solution code:
import collections 
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
c=defaultdict(int)
for s in a:
    c[s]+=1
print(c)

a1=list(set(a))
print(a1)

total=0
for j in range(len(a1)):
    x=c[a[j]]
    print(x)
    total+=x*(x-1)/2
    print('total=',total)

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

Submission status: Wrong Answer

Solution code:
import numpy
import copy
N = int(input())
E=input().split()
ans = [0 for _ in range(N)]
unique = list(numpy.unique(E))
for s in range(len(unique)):
    unique[s] = int(unique[s])
print(unique)
uniqueNum = [0 for _ in range(len(unique))]

for t in range(len(unique)):
    uniqueNum[t] = int(E.count(str(unique[t])))
print(uniqueNum)
for k in range(N):
    uni = copy.copy(uniqueNum)
    uni[unique.index(int(E[k]))] = int(uni[unique.index(int(E[k]))]) - 1
    for w in range(len(uni)):
        ans[k] = ans[k] + uni[w]* (uni[w]-1)/2
for i in range(N):
    print(int(ans[i]))

Submission status: Wrong Answer

Solution code:
import sys
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

n = int(input())
l = [int(x) for x in input().split()]

# cl = [0 for i in range(n)]
cl = []
ll = [x for x in set(l) if l.count(x) > 1]

# print(ll)

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (m - 2) / m))

# print(sum)
# print(cl)

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

for i in l:
    # print(sum - cl[i-1])
    idx = my_index(ll, i)
    if idx:
        print(0)
    else:
        print(sum - cl[idx])

Submission status: Wrong Answer

Solution code:

n = int(input())
a = list(map(int,input().split()))
b = a.copy()
b = set(b)
b = list(b)
c = []
ans = []

for i in range(len(b)):
    c.append(a.count(b[i]))
print(b)
print(c)
d = []
for i in range(len(b)):
    num = 0
    for j in range(len(b)):
        if i == j:
            num += int(((c[j]-1)*(c[j]-2))/2)
        else:
            num += int(((c[j])*(c[j]-1))/2)
    d.append(num)
print(d)

for i in range(n):
    for j in range(len(b)):
        if a[i] == b[j]:
            ans.append(d[j])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict
import sys
input = sys.stdin.readline

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 #出力の制限
N = 2*(10**5)
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 )


l = int(input())
A=list(map(int, input().split()))
S = Counter(A)

dd=defaultdict(int)
for j in S.keys():
  S[j] -= 1
  tmp = []
  for k in S.keys():
    tmp.append(cmb(S[k],2,mod))
  dd[j] += sum(tmp)
  S[j] += 1

for i in A:
  print(dd[i])

Submission status: Wrong Answer

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

count=-1

for k in range(len(A_list)):
    print(A_list.count(A_list[k]))

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=[0]*N
for i in range(N):
	for j in range(i+1,N):
		if a[i]==a[j]:
			b[i]+=1
for i in range(N):
	ans=0
	for j in range(N):
		if i==j:
			pass
		else:
			ans+=b[j]
	print(ans)

Submission status: Wrong Answer

Solution code:
import collections

def cal(x):
    b=0
    for i in range(e):
        a=d.most_common()[i]
        c=a[1]
        if a[0]==balls[x]:
            c-=1
        b = c*(c-1)//2
    return b

N = int(input())
balls = list(map(int,input().split()))
new_balls =sorted(balls)
d = collections.Counter(new_balls)
e = len([x for x in set(balls) if balls.count(x) > 1])

for j in range(N):
    print(cal(j))

Submission status: Wrong Answer

Solution code:
from functools import lru_cache
@lru_cache(maxsize=1024)
def nC2(n):
    return n * (n-1)//2

N=int(input())
A=list(map(int, input().split()))
Y = set(A)
memo = {}
Z = set()
#数え上げ
str_count = {}
str_count2 = {}
for i in Y:
    str_count[i] = A.count(i)
    str_count2[i] = nC2(str_count[i])
    if str_count[i] != 1:
        Z.add(i)
for k in range(0,N):
    key = A[k]
    if key in memo:
        print(memo[key])
        continue
    c = 0
    for i in Z:
        if i == A[k]:
            c += str_count2[i] - (2 *str_count[i]) - 1
        else:
            c += str_count2[i]
    memo[key] = c
    print(c)

Submission status: Wrong Answer

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

#output
from collections import  Counter
L = Counter(A)

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

for i in range(N):
    temp = 0
    if L[A[i]] == 1:
        print(original_answer)
    elif L[A[i]] == 2:
        temp -= 1
        print(original_answer + temp)
    elif L[A[i]] > 2:
        temp = temp - L[A[i]]*(L[A[i]]-1)//2 + (L[A[i]]-1)*(L[A[i]]-2)//2 
        print(original_answer + temp)

Submission status: Wrong Answer

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



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

for k in range(N):
  sum_k = 0
  temp_c = c.copy()
  temp_c[A[k]] -= 1
  for v in temp_c.values():
    if v > 1:
      sum_k += comb(v, 2)
  print(int(sum_k))

Submission status: Wrong Answer

Solution code:
import sys
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

n = int(input())
l = [int(x) for x in input().split()]

# cl = [0 for i in range(n)]
cl = []
ll = [x for x in set(l) if l.count(x) > 1]

# print(ll)

if not ll:
    for i in range(n):
        print(0)
    sys.exit()

sum = 0
for i in ll:
    m = l.count(i)
    nc = combinations_count(m, 2)
    sum += nc
    # print(nc)
    # cl[i-1] = nc - int(nc*(m-2)/m)
    cl.append(nc - int(nc * (m - 2) / m))

# print(sum)
# print(cl)

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

for i in l:
    # print(sum - cl[i-1])

    print(sum - cl[my_index(ll, i)])