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 math
n=int(input())
A = list(map(int, input().split()))
res=[0]*n
import collections
def com(n, r):
    if n<r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

af=collections.Counter(A)
res2=list(af.items())
res3=[0]*n
s=0
for i in range(0,len(res2)):
    s=s+com(res2[i][1],2)
for j in range(0,len(res2)):
    res3[res2[j][0]-1]=s+(1/2)*(res2[j][1]-1)*(res2[j][1]-1)-(1/2)*(res2[j][1])*(res2[j][1])
for i in range(n):
    res[i]=res3[A[i]-1]

for j in range(n):
    print(res[j])

Submission status: Wrong Answer

Solution code:
m = int(input())
q_list = list(map(int, input().strip().split()))
n = [0] * m
k = [0] * m

for i in q_list:
    n[i - 1] += 1
for j, l in zip(range(m), n):
    k[j] = int(l * (l - 1)/2)
sum_n = sum(k)

for j in q_list:
    print(sum_n - n[j - 1]-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
D = {}
for i in A:
    if not i in D.keys():
        D[i] = 1
    else:
        D[i] += 1
for i in A:
    n = 0
    for j,k in D.items():
        if j != i:
            n += int(((D[i]-1)*k)/2)
        elif j == i:
            n += int(((D[i]-1)*(D[i]-2)/2))
    print(n)

Submission status: Wrong Answer

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

a_2 = set(a)
ans = 0
for i in a_2:
    p = a.count(i)
    ans += int((p * (p-1)) / (2 * 1))
#print(ans)
final = [ans for i in range(n)]
for i in range(n):
    a_2 = copy.copy(a)
    del a_2[i]
    h = a_2.count(a[i])
    final[i] -= int((h-1))
for i in final:
    print(i)

Submission status: Wrong Answer

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

s = collections.Counter(A)

for i in s:
    if s[i] >= 2:
        b = s[i]*(s[i]-1)/2
        cnt += b

print('\n'.join([str(cnt-A.count(A[i])+1) for i in range(N)]))

Submission status: Wrong Answer

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

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

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

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

    return result

N = int(input())
lst = [int(x) for x in input().split(" ")]

s = set(lst)
cnt = []

for i in range(0,N):
  cnt.append("")
  for j in s:
    cnt[i] = cmb(int((lst[0:i]+lst[i+1:N]).count(j)),2)

for i in range(0,N):
  print(cnt[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(0, n):
    ai = list(a)
    del ai[i]
    count = -1
    for x in range(len(a)):
        for y in range(x, len(a)):
            if x == y:
                count += 2
    print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import Counter

lines = stdin.readlines()

n = int(lines[0].rstrip())
a = [int(x) for x in lines[1].rstrip().split()]

count = Counter(a)
c = {k:v for k,v in filter(lambda kv: kv[1]>1, count.items())}
print(c)
res = []
for k in range(n):
    part_res = 0
    for i in c.keys():
        if not i == a[k]:
            quant = c[i]
        else:
            quant = c[i] - 1
        if quant > 1:
            part_res += int((quant * (quant - 1)) / 2)
    res.extend([part_res])

for j in range(len(res)):
    print(res[j])

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A1=sorted(A)
cnt=[0 for _ in range(N+1)] #cnt_table[i]: 数字iの要素の個数
cnt[A1[0]]+=1
for i in range(1,N):
    if A1[i]!=A1[i-1]:
        cnt[A1[i]]+=1
        if i==N-1:
            cnt[A1[i]]+=1
    else:
        cnt[A1[i]]+=1

cmb=[cnt[k]*(cnt[k]-1)//2 for k in range(N+1)]
SUM=sum(cmb)

for j in range(N):
    n=A[j]
    v=cnt[A[j]]
#    print(n,v,SUM,cmb[n])
    print(SUM-cmb[n]+(v-1)*(v-2)//2)

Submission status: Wrong Answer

Solution code:
def comb2(a):
  if a < 2: return 0
  return int(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):
  y = comb2(dp[A[i]][0]-1)
  print(int(x - dp[A[i]][1] + y))

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(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:
from scipy.misc import comb

def twocomb(n):
    return int(comb(n, 2))

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

A_count = {}
for m in A:
    A_count.setdefault(m, 0)
    A_count[m] = A_count[m] + 1

A_comb = 0
for i in A_count.values():
    if(i > 1):
        A_comb += twocomb(i)
for i in range(N):
    q = A_count[A[i]]
    if(q == 1):
        print(A_comb)
    else:
        if(q == 2):
            print(A_comb-1)
        else:
            print(A_comb + twocomb(q-1) - twocomb(q))

Submission status: Wrong Answer

Solution code:
n = int(input())
ao = list(map(int, input().split(' ')))
aval = list(set(ao))
acount = []
for i in aval:
    acount.append(ao.count(i))

for i in ao:
    ind = aval.index(i)
    print(sum(acount)-acount[ind])

Submission status: Wrong Answer

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

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

old_cdict = dict(c)

cdict = dict((key, value) for (key, value) in old_cdict.items() if value >= 2)

# print(cdict)

maxi = 0

for k, v in cdict.items():
    maxi += v * (v - 1) // 2

for a in alist:
    num = 0
    if num in cdict:
        num = cdict[a]
    if num in resdict:
        # print('used')
        print(resdict[num])
        continue
    # print('not used')
    if a in cdict:
        cdict[a] -= 1
        temp = 0
        for k,v in cdict.items():
            temp += v*(v-1)//2
        print(temp)
        resdict[num] = temp
        cdict[a] += 1
    else:
        print(maxi)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from operator import itemgetter

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

B =[]
for i in range(N):
    B = A[0:i] + A[i+1:N]
    #print(B)
    B=Counter(B)
    C=list(B.items())
    C = sorted(C,key=itemgetter(1),reverse = True)
    #print(C)
    ans=(C[0][1])
    print(ans)

Submission status: Wrong Answer

Solution code:
from decimal import *

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

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

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

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

    return result


def main():
    N=int(input())
    number=[0]*(N+2)
    A = list(map(int, input().strip().split()))
    B=sorted(A)
    B.append(-1)
    # print(A)
    # print(B)
    
    for i in range(N-1):
        if B[i]==B[i+1]:
            if number[B[i]]==0:
                number[B[i]]+=1
            number[B[i]]+=1
        # elif B[i-1]==B[i] and B[i] != B[i+1]:
        #     number[B[i]]+=1
    
    # print(number)
    for i in range(N):
        if number[i]==1 or number[i]==0:
            number[i]=0
            continue
        else:
            number[i]=cmb(number[i],2)
    
    # print(number)
    
    result=[0]*(N+1)
    for i in range(N+1):
        if number[i]==1:
            result[i]=sum(number)-1
        else:
            result[i]=sum(number)-number[i]+cmb(number[i]-1,2)
    # print(result)
    
    for i in range(N):
        print(result[A[i]])
    

    
    
    
    
main()

Submission status: Wrong Answer

Solution code:
import collections

def LI():
	return [ int(s) for s in input().split() ]

N = int(input())
A = LI()

c_counter = [0]*(2*(10**5))

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

total = 0
total_dict = [0]*(2*(10**5))

for a in A:
  v = c_counter[a]
  if v >= 2:
    comb = v*(v-1)//2
    total_dict[a] = comb
    total += comb

for a in A:
  if total == 0:
    print(0)
  else:
    print(total - total_dict[a] + (c_counter[a]-1)*(c_counter[a]-2)//2)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

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

N_all = list(itertools.combinations(N_list, len(N_list)-1))
for N_rem in N_all:
    count = 0
    num_list = list(itertools.combinations(N_rem,2))
    for i, j in num_list:
        if i==j:
            count+=1
    print(count)

Submission status: Wrong Answer

Solution code:
import collections

def combination(n, r, mod=10**20+7):
    n1, r = n+1, min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod
    
n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)    
s = list(set(li))
ans = 0
d = collections.defaultdict(int)

for j in range(len(s)):
    if c[s[j]] <= 1:
        continue
    ans += combination(c[s[j]], 2)

for i in range(n):
    if d[li[i]] == 0:
        ans_a = ans
        if c[li[i]] <= 1:
            print(ans_a)
            d[li[i]] = ans_a
            continue
        ans_a -= combination(c[li[i]], 2)
        c[li[i]] -= 1
        if c[li[i]] <= 1:
            print(ans_a)
            c[li[i]] += 1
            d[li[i]] = ans_a
            continue
        ans_a += combination(c[li[i]], 2)
        c[li[i]] += 1
        d[li[i]] = ans_a
        print(ans_a)
    else:
        print(d[li[i]])

Submission status: Wrong Answer

Solution code:
def comb(n, r):
    upper = 1
    for i in range(r):
        upper *= (n-i)
    lower = r*(r-1)*2
    # print(n, r, upper, lower, int(upper/lower))
    return int(upper/lower)

n = int(input())
a = list(map(int, input().split()))
for k in range(n):
    temp = a[:]
    temp[k:k+1] = []
    ms = set(temp)
    total = 0
    for m in ms:
        times = temp.count(m)
        if times < 2:
            continue
        total += comb(times, 2)
    print(int(total))

Submission status: Wrong Answer

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

for index, item in enumerate(a):
    count = 0
    for chk_index, chk_item in enumerate(a):
        if index == chk_index:
            continue
        for cnt_index, cnt_item in enumerate(a[chk_index+1:]):
            if index == cnt_index:
                continue
            if chk_item == cnt_item:
                count += 1
    print(count)

Submission status: Wrong Answer

Solution code:
import operator as op
from functools import reduce
from collections import defaultdict
from math import ceil
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom

n = int(input().strip())
nums = [int(i) for i in input().strip().split()]
ans = [0 for i in range(n)]
num_d = defaultdict(int)
for i in nums:
    num_d[i] += 1

ans_d = defaultdict(int)
s = 0
for each_num in num_d:
    res = ncr(num_d[each_num],2)
    s += res
    ans_d[each_num] = res

for index,i in enumerate(nums):
    if num_d[i] - 1 < 2:
        ans[index] = s - ans_d[i]
    else:
        ans[index] = s - ans_d[i] + ncr(num_d[i] - 1,2)

for each in ans:
    print(each)

Submission status: Wrong Answer

Solution code:
import math
def comb2(t):
    if t <= 1:
        return 0
    else:
        return math.factorial(t) // (math.factorial(t - 2) * math.factorial(2))
n = int(input())
nl = input().split()
nd = {}
ndans = {}
for num in nl:
    if num not in nd.keys():
        nd[num] = 0
        ndans[num] = 0
    nd[num] +=1
    ndans[num] +=1
for i in ndans:
    ndans[i] = 0
    for k, v in nd.items():
        if k == i:
            ndans[k] += comb2(int(v)-1)
        else:
            ndans[k] += comb2(int(v))
for j in nl:
    print(ndans[j])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
k=len(a)
for i in range(k):
  del a[i]
  ans=0
  for j in range(n):
    ac=a.count(j)
    ans+=ac*(ac-1)//2
  print(ans)
  a.insert(k, 'k')

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
for i in range(N):
    s = 0
    a = A[i]
    mydict[a] -= 1
    for j in mydict.values():
        s += cmb(j)
    print(s)
    mydict[a] += 1

Submission status: Wrong Answer

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

ans = 0

k = [0 for _ in range(n+1)]

for i in range(n):
    k[l[i]] += 1
    
ll = len(k)
for i in range(ll):
    a = k[i]
    if a <= 1:
        pass
    else:
        ans += (a * (a - 1))/2
        
for i in range(n):
    a = k[l[i]]
    b = (a * (a - 1))/2
    c = a - 1
    if c <= 1:
        print(int(ans))
    else:
        pans = ans - b
        c = (c * (c - 1))/2
        pans = pans + c
        print(int(pans))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input().split())
for i in range(len(A)-1):
  count=0
  j=i+2
  for j in range(len(A)-1):
    if A[i+1]==A[j]:
      count=count+1
  print(count)

Submission status: Wrong Answer

Solution code:
import copy
import math
import collections
def comb(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
#A
N = int(input())
A = list(map(int, input().split()))
count = collections.Counter(A)
ans = [0]*N
for i in range(2,N):
    ans[i] = comb(i, 2)
#print(ans)
for i in range(N):
    arr = copy.copy(count)
    arr[A[i]] -= 1
    cnt = 0
    #print(count)
    for num in count:
        cnt += ans[arr[num]]
    #print(cnt)

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)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=input().split()
for i in b:
    if b.count(i)<=2:
        print(b.count(i))
    else:
        print('0')

Submission status: Wrong Answer

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

n = int(input())
num = list(map(int, input().split()))
list_num = list(num)
print(list_num)
cntAll = 0

for i in list_num:
    iter_list_num = list_num
    del iter_list_num[0]
    # すうじごとに確認
    for j in iter_list_num:
        # cnt = iter_list_num.count(iter_list_num[j])
        cnt = collections.Counter()
    print(cnt)

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)
        break
    elif L[A[i]] == 2:
        temp -= 1
    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:
n = int(input())
a = list(map(int,input().split()))

for i in range(n):
  tmp = a[i]
  num = a.count(tmp) - 1
  print(num*(num-1)//2)

Submission status: Wrong Answer

Solution code:
import copy
import collections
import math
import sys

def combinations_count(n, r):
    if n <= 1:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * 2)

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

bigC = collections.Counter(A)

maxnum = max(A)
lis = [-1] * maxnum
for i, a in enumerate(A):
  if lis[a-1] < 0:
    z = bigC[a]
    bigC[a] -= 1
    c = copy.deepcopy(bigC)
    total = 0
    for idx in c:
      total += combinations_count(c[idx], 2)
      
    lis[a-1] = total
    for x in bigC:
      if bigC[x] == bigC[a]:
        lis[x-1] = total
    print(total)
    bigC[a] += 1
  else:
    print(lis[a-1])

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)):
    for l in range(len(A_list)):
        if A_list[k] == A_list[l]:
            count+=1
    print(count)
    count=-1

Submission status: Wrong Answer

Solution code:

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

    # print(N)
    # print(A)

    for i in range(N):
        result = 0
        for j in range(N):
            if A[i] == A[j]:
                result += 1
        print(result)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

a = input().split(" ")
ab = a
for k in range(n):
    count = 0
    if not k == 0:
        ab.insert(k-1, a[k-1])
    ab.pop(k)
    for i in range(n-1):
        for j in range(i):
            if ab[i] == ab[j] and not i == j:
                count = count + 1
    print(count)

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 c==1:
            break
        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:
#!/usr/bin/env python3
import collections
import copy
import itertools as it
import math
import numpy as np

# A = input()
N = int(input())
# A = map(int, input().split())
A = list(map(int, input().split()))
# A = [int(input()) for i in range(N)]
#
c = collections.Counter(A)

cache = {}

A_ = copy.deepcopy(A)

count = len(A)
for (k, v) in c.most_common():
    if v == 1:
        count -= 1

for a in A:
    if c[a] == 2:
        print(count - 2)
    if c[a] == 1:
        print(count)
    else:
        print(count)

print()

Submission status: Wrong Answer

Solution code:
N = int(input())
data = input().split(" ")
visit = [0 for i in range(N)]
ans = 0
for i in range(N):
  for j in range(N):
    if (data[j] == data[i])and(visit[i]==0)and(visit[j]==0):
      visit[i] = 1
      visit[j] = 1
      ans += 1
      
  print(ans)

Submission status: Wrong Answer

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

kazuset=list(set(kazu))
atai=0
print(kazuset)
for i in kazuset:
    kosuu=kazu.count(i)
    atai=atai+kosuu*(kosuu-1)/2
  
for i in range(n):
    kotae=atai
    kosuu=kazu.count(kazu[i])
    if (kosuu >= 1):
        kotae=atai-kosuu*(kosuu-1)/2+(kosuu-1)*(kosuu-2)/2
    print(int(kotae))

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

a1=list(set(a))

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


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

Submission status: Wrong Answer

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

ans = 0

k = [0 for _ in range(n+1)]

for i in range(n):
    k[l[i]] += 1
    
ll = len(k)
for i in range(ll):
    a = k[i]
    if a <= 1:
        pass
    else:
        ans += (a * (a - 1))/2
        
for i in range(n):
    a = k[l[i]]
    b = (a * (a - 1))/2
    c = a - 1
    if c <= 1:
        print(ans)
    else:
        pans = ans - b
        c = (c * (c - 1))/2
        pans = pans + c
        print(pans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def nCr(n, r, mod=10**9+7):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod


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

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

count = 0
for k, v in dd.items():
    if v >= 2:
        count += nCr(v, 2)
for i in range(N):
    ans = 0
    if dd[A[i]] >= 3:
        ans = count - nCr(dd[A[i]], 2) + nCr(dd[A[i]]-1, 2)
    elif dd[A[i]] == 2:
        ans = count - 1
    elif dd[A[i]] == 1:
        ans = count
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import math
import sys

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

if N == len(set(A)):
    for _ in range(N):
        print(0)
    sys.exit(0)

c2 = math.factorial(2)
umemory = {}
dmemory = {}
memory = {}

count = Counter(A)

for k, v in count.items():
    umemory[k] = 0 if v < 2 else math.factorial(v) // (math.factorial(v - 2) * c2)
    dmemory[k] = 0 if v < 3 else math.factorial(v-1) // (math.factorial(v - 3) * c2)

for k in range(0, N):
    a = A[k]
    print("DEBUG: ", memory)
    if a in memory:
        result = memory[a]
    else:
        memory[a] = sum(map(lambda kv: kv[1], filter(lambda kv: kv[0] != a, umemory.items())))
        memory[a] += dmemory[a]
        result = memory[a]
    print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = input().split(" ")
ab = a
for k in range(n):
    count = 0
    if not k == 0:
        ab.insert(k-1, a[k-1])
    ab.pop(k)
    for i in range(n-1):
        for j in range(i):
            if a[i] == a[j] and not i == j:
                count = count + 1
    print(count)

Submission status: Wrong Answer

Solution code:

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

    print(N)
    print(A)

    for i in range(N):
        result = 0
        for j in range(N):
            if A[i] == A[j]:
                result += 1
        print(result)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import copy
import collections
import math
import sys

def combinations_count(n, r):
    if n <= 1:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * 2)

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

bigC = collections.Counter(A)

maxnum = max(A)
lis = [-1] * maxnum
for i, a in enumerate(A):
  if lis[a-1] < 0:
    z = bigC[a]
    bigC[a] -= 1
    c = bigC
    total = 0
    for idx in c:
      total += combinations_count(c[idx], 2)
      
    lis[a-1] = total
    for x in bigC:
      if bigC[x] == bigC[a]:
        lis[x-1] = total
    print(total)
    bigC[a] += 1
  else:
    print(lis[a-1])

Submission status: Wrong Answer

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

d = {}
prev = 0

for i in range(N):
    if a[i] not in d:
        d[a[i]] = 1
    else:
        d[a[i]] += 1

for i in range(N):
    if i != 0 and d[a[i]] <= 1:
        print(prev)
        continue

    d[a[i]] -= 1
    su = 0
    for j in d.values():
      su += int(j * (j - 1) / 2)
    prev = su
    print(su)

    d[a[i]] += 1

Submission status: Wrong Answer

Solution code:
l = int(input())
A = input().split(" ")

unique = set(A)

def combination(number):
  if number == 0 or number == 1:
    return 0
  else:
    return int(number * (number - 1) / 2)

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

print(memo)
# print(memo.keys())
# print(total)

for i in A:
  #  print(caluculate(i))
  print(total - caluculate(i))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = input().split(" ")
ab = a
for k in range(n):
    count = 0
    if not k == 0:
        a.insert(k-1, a[k-1])
    ab.pop(k)
    for i in range(n-1):
        for j in range(i):
            if a[i] == a[j] and not i == j:
                count = count + 1
    print(count)

Submission status: Wrong Answer

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

import numpy as np

base = 0
for i in range(len(a)):
    base = 0
    a_del = np.delete(a, int(i), 0)
    set_ = a_del.tolist()
    set_1 = list(set(set_))
    for j in range(len(set_1)):
        cnt = set_.count(set_1[j])
        print(cnt)
        if 2 <= cnt:
            base = base + cnt*(cnt-1)/2
    print(base)

Submission status: Wrong Answer

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

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

old_cdict = dict(c)

cdict = dict((key, value) for (key, value) in old_cdict.items() if value >= 2)

# print(cdict)

maxi = 0

# for k, v in cdict.items():
#     maxi += v * (v - 1) // 2

for a in alist:
    if a in resdict:
        # print('used')
        print(resdict[a])
        continue
    # print('not used')
    if a in cdict:
        cdict[a] -= 1
        temp = 0
        for k,v in cdict.items():
            temp += v*(v-1)//2
        print(temp)
        resdict[a] = temp
        cdict[a] += 1
    else:
        print(maxi)

Submission status: Wrong Answer

Solution code:
N = int(input())
balls = list(map(int, input().split()))
balls_count = {}
for ball_num in balls:
    if ball_num in balls_count.keys():
        balls_count[ball_num] += 1
    else:
        balls_count[ball_num] = 1
# print(balls_count)
indexes = {}
total_costs = []
count = 0
for k, v in balls_count.items():
    if count == 0:
        indexes[k] = count
        cost = int((v) * (v - 1) / 2)
        total_costs.append(cost)
        count += 1
    else:
        indexes[k] = count
        cost = (v) * (v - 1) / 2
        total_costs.append(int(total_costs[count - 1] + cost))
        count += 1
# print(indexes)
# print(total_costs)


for check_ball_num in balls:
    index = indexes[check_ball_num]
    if index == len(total_costs) - 1:
        if index - 1 >= 0:
            prev_cost = total_costs[index - 1]
            now_cost = (balls_count[check_ball_num] - 1) * \
                (balls_count[check_ball_num] - 2) / 2
            print(int(now_cost + prev_cost))
        else:
            now_cost = (balls_count[check_ball_num] - 1) * \
                (balls_count[check_ball_num] - 2) / 2
            print(int(now_cost))
    else:
        next_cost = (total_costs[index + 1] - total_costs[index])
        now_cost = (balls_count[check_ball_num] - 1) * \
            (balls_count[check_ball_num] - 2) / 2
        print(int(now_cost + next_cost))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def nCr(n, r, mod=10**9+7):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod


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

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

count = 0
for k, v in dd.items():
    if v >= 2:
        count += nCr(v, 2)
for i in range(N):
    ans = 0
    if dd[A[i]] >= 3:
        ans = count - nCr(dd[A[i]], 2) + nCr(dd[A[i]]-1, 2)
    elif dd[A[i]] == 2:
        ans = count - 1
    else:
        ans = count
    print(ans)

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(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
    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:
# 200322 D

import collections

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

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

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

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

    return result


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

cmblist = {}
cmblistl = {}
for i in gAc:
    if gAc[i] <= 1:
        cmblist[i] = 0
        cmblistl[i] = 0
    else:
        cmblist[i] = cmb(gAc[i],2)
        if gAc[i] > 1:
            cmblistl[i] = cmb(gAc[i]-1,2)
        else:
            cmblistl[i] = 0
        
for i in range(len(A)):
    bAc = {}
    for k in gAc:
        bAc[k] = gAc[k]
    Ac = bAc
    Ac[A[i]] -= 1
    cmbsum = 0
    for j in Ac:
        if gAc[j] != Ac[j]:
            cmbsum += cmblistl[j]
        else:
            cmbsum += cmblist[j]
    print(cmbsum)