TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from itertools import permutations

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

i = 0
a = 0
b = 0

for s in permutaions(range(1,n + 1)):
  for j in range(n):
    if p[j] != s[j]:
      break
  else:
    a = i
  for j in range(n):
    if q[j] != s[j]:
      break
  else:
    b = i
    i += 1
    
print(abs(a-b))

from itertools import permutations
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
 
a,b = -1,-1
for i,x in enumerate(permutations([i+1 for i in range(n)])):
    c,d = True,True
    for j in range(n):
        if x[j] != p[j]:
            c = False
        if x[j] != q[j]:
            d = False
    if c:a = i+1
    if d:b = i+1

import math
 
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
 
a = 0
b = 0
 
numcheck = [1 for i in range(N)]
numcheck[P[0]-1] = 0
a = math.factorial(N-1)*(P[0]-1) 
for n in range(1,N):
  num = P[n]
  numcheck[num-1] = 0

import math
 
N = int(input())
Pi = list(map(int,input().split()))
Qi = list(map(int,input().split()))
 
a = 0
b = 0
 
numcheck = [1 for i in range(N)]
numcheck[Pi[0]-1] = 0
a = math.factorial(N-1)*(Pi[0]-1) 
for n in range(1,N):
  num = Pi[n]
  numcheck[num-1] = 0

import itertools
N = int(input())
P,Q = input().rstrip().split(" "),input().rstrip().split(" ")
 
for i in range(N):
  P[i],Q[i] = int(P[i]),int(Q[i])
 
seq=list(range(1,N+1))
a = list(itertools.permutations(seq, N))
 
for i in range(len(a)):
  if a[i] == tuple(P):
    pindex= i+1
  if a[i] == tuple(Q):
    qindex= i+1

from itertools import permutations
 
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
 
i = 0
a = 0
b = 0
for s in permutations(range(1, n+1)):
    for j in range(n):
        if p[j] != s[j]:
            break
    else:
        a = i
    for j in range(n):
        if q[j] != s[j]:
            break
    else:
        b = i
    i += 1
 
print(abs(a-b))

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math
from collections import deque
def main():
    n = int(input())
    p = [int(t)for t in input().split()]
    q = [int(t)for t in input().split()]

    p_num = 0
    q_num = 0

    fact = [1]*n

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

    fact = fact[::-1]
    fact.append(1)

    print(fact)

    temp = set([])
    for i, p_ in enumerate(p):
        t = p_-len([x for x in temp if x<p_])-1
        p_num += fact[i+1]*t
        temp.add(p_)

    temp = set([])
    for i, q_ in enumerate(q):
        t = q_-len([x for x in temp if x<q_])-1
        q_num += fact[i+1]*t
        temp.add(q_)

    print(abs(p_num-q_num))

if __name__ == "__main__":
    main()

import math
from collections import deque
def main():
    n = int(input())
    p = [int(t)for t in input().split()]
    q = [int(t)for t in input().split()]

    p_num = 0
    q_num = 0

    fact = [1]*n

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

    fact = fact[::-1]
    fact.append(1)

    temp = set([])
    for i, p_ in enumerate(p):
        t = p_-len([x for x in temp if x<p_])-1
        p_num += fact[i+1]*t
        temp.add(p_)

    temp = set([])
    for i, q_ in enumerate(q):
        t = q_-len([x for x in temp if x<q_])-1
        q_num += fact[i+1]*t
        temp.add(q_)

    print(abs(p_num-q_num))

if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans1 = 0
ans2 = 0
for i in range(n):
  k = math.factorial(n-1-i)
  ans1 += k*p[i] - 1
  ans1 += k*p[i] - 1
print(abs(ans1 - ans2))

import bisect
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

def ans(l, con):
  if con == n - 1:
    return 0
  k = bisect.bisect_right(sorted(l[con:]),l[con])
  return k*math.factorial(n - 1 - con) + ans(l, con + 1)
print(abs(ans(p, 0)-ans(q,0)))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

perm = list(permutations(range(1, N+1), N))
print(abs(perm.index(P) - perm.index(Q)))

from itertools import permutations 

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

perm = list(permutations(range(1, N+1), N))
print(abs(perm.index(P) - perm.index(Q)))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools

N = int(input())
P = tuple((int(i) for i in input().split()))
Q = tuple((int(i) for i in input().split()))

f = list(itertools.permutations(range(1, N+1)))

print(f.index(P) - f.index(Q))

import itertools

N = int(input())
P = tuple((int(i) for i in input().split()))
Q = tuple((int(i) for i in input().split()))

f = list(itertools.permutations(range(1, N+1)))

print(abs(f.index(P) - f.index(Q)))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools

N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
order = []

for i in range(1,N+1):
    order.append(i)

#print(coordinate)

count = 0
print(P)
print(Q)
for vect in itertools.permutations(order):
    print(vect)
    count +=1
    if vect == P:
        a = count
    if vect ==Q:
        b = count

print(abs(a-b))

import itertools

N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
order = []

for i in range(1,N+1):
    order.append(i)

#print(coordinate)

count = 0
for vect in itertools.permutations(order):
    
    count +=1
    if vect == P:
        a = count
    if vect ==Q:
        b = count

print(abs(a-b))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from itertools import*
(n,),a,b=eval('eval(input().replace(" ",",")),'*3)
i=list(permutations(range(1,n+1))).index
print(abs(i(a)-i(b)))

from itertools import*
n,a,b=eval('eval(input().replace(" ",",")),'*3)
i=list(permutations(range(n+1))).index
print(abs(i(a)-i(b)))

from itertools import*
n,a,b=eval('eval(input().replace(" ",",")),'*3)
i=list(permutations(range(1,n+1))).index
print(abs(i(a)-i(b)))

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
np = [i for i in range(1, n+1)]

import itertools

all_list = list(itertools.permutations(np))

cnt=0
for seq in all_list:
  cnt += 1
  if seq == p:
    a = cnt
  if seq == q:
    b = cnt

print(abs(a-b))

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
np = [i for i in range(1, n+1)]

import itertools

all_list = list(itertools.permutations(np))

cnt=0
a, b = 0, 0
for seq in all_list:
  cnt += 1
  if list(seq) == p:
    a = cnt
  if list(seq) == q:
    b = cnt

print(abs(a-b))


Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from itertools import permutations
N = int(input())

o = {}
for i, p in enumerate(permutation(range(1,N+1))):
  	o[p] = i
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
print(abs(o[P] - o[Q]))

from itertools import permutations
N = int(input())

o = {}
for i, p in enumerate(permutations(range(1,N+1))):
  	o[p] = i
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
print(abs(o[P] - o[Q]))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
lis = range(1,n+!)

a = list(itertools.permulations(lis))


print(abs(a.index(p) - a.index(q)))

import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
lis = range(1, n+1)


a = list(itertools.permutations(lis))

print(abs(a.index(p) - a.index(q)))


Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools 
n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = list(itertools.permutation([i for i in range(1, n+1)]))
print(abs(l.index(P) - l.index(Q)))

import itertools 
n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = list(itertools.permutations([i for i in range(1, n+1)]))
print(abs(l.index(P) - l.index(Q)))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
for i,v in enumerate(list(itertools.permutations(range(1,N+1))), 1):
    if list(v) == P:
        A = i
    elif list(v) == Q:
        B = i
    else:
        pass
try:
    print(abs(A-B))

import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
A = B = 0
for i,v in enumerate(list(itertools.permutations(range(1,N+1))), 1):
    if list(v) == P:
        A = i
    elif list(v) == Q:
        B = i
        continue
    else:
        pass
print(abs(A-B) if A != 0 and B != 0 else 0)

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools

def AllPatern(n, all):
    numbers = []
    all = []
    for i in range(n):
        numbers.append(i+1)
    for i in itertools.permutations(numbers):
        all.append(list(i))

    return all


def WhichNumber(p, q, all):
    val_p = 0
    val_q = 0
    for i in range(len(all)):
        val_p += 1
        if p == all[i]:
            break
    for i in range(len(all)):
        val_q += 1
        if q == all[i]:
            break

    return val_p, val_q

def main():
    n = int(input())
    p = [int(i) for i in input().split()]
    q = [int(i) for i in input().split()]
    all_numbers=[]

    all_numbers = AllPatern(n, all_numbers)
    val_p, val_q = WhichNumber(p, q, all)
    print(abs(val_p - val_q))



if __name__ == '__main__':
    main()


import itertools

def AllPatern(n, all):
    numbers = []
    all = []
    for i in range(n):
        numbers.append(i+1)
    for i in itertools.permutations(numbers):
        all.append(list(i))

    return all


def WhichNumber(p, q, all):
    val_p = 0
    val_q = 0
    for i in range(len(all)):
        val_p += 1
        if p == all[i]:
            break
    for i in range(len(all)):
        val_q += 1
        if q == all[i]:
            break

    return val_p, val_q

def main():
    n = int(input())
    p = [int(i) for i in input().split()]
    q = [int(i) for i in input().split()]
    all = []

    all = AllPatern(n, all)
    val_p, val_q = WhichNumber(p, q, all)
    print(abs(val_p - val_q))



if __name__ == '__main__':
    main()


Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from itertools import permutations
n = int(input())
P = list(map(int, input().split()))
Q = list(map, int(input().split()))

A = list(itertools.permutations(P))
A.sort()
print(abs(A.index(tuple(P)) - A.index(turple(Q))))

from itertools import permutations
n = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

A = list(permutations(P))
A.sort()
print(abs(A.index(tuple(P)) - A.index(tuple(Q))))


Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
import re

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

print(p, q)

n_list = list(range(1, n + 1))

conv =[]
for v in itertools.permutations(n_list, n):
    conv.append(v)
print(conv)

p_ind = 0
q_ind = 0
for i, j in enumerate(conv):
    print(p, j)
    if p == j:
        p_ind = i + 1
        print('yes')
    elif q == j:
        q_ind = i + 1

print(abs(p_ind - q_ind))

import itertools
import re

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

n_list = list(range(1, n + 1))

conv =[]
for v in itertools.permutations(n_list, n):
    conv.append(v)

for i, j in enumerate(conv):
    if p == j:
        p_ind = i + 1

for i, j in enumerate(conv):
    if q == j:
        q_ind = i + 1

print(abs(p_ind - q_ind))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
a=int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C=[0,1,2,6,24,120,720,5040,40320]
d=0
e=0

for i in range(a,1,-1):
  d1=C[i-1]*(A[a-i]-1)
  e1=C[i-1]*(B[a-i]-1)
  d=d+d1
  e=e+e1
print(abs(d-e))







a=int(input())
A=[]
B=[]
C=[0,1,2,6,24,120,720,5040,40320]
d=0
e=0
for p in range(a):
  s=int(input())
  A.append(s)
for q in range(a):
  b=int(input())
  B.append(b)
for i in range(a,1,-1):
  d1=C[i-1]*(A[a-i]-1)
  e1=C[i-1]*(B[a-i]-1)
  d=d+d1
  e=e+e1
print(abs(d-e))

import itertools
a=int(input())
z=tuple(map(int,input().split()))
x=tuple(map(int,input().split()))
y=list(itertools.permutations(z))
y.sort()
ans1=y.index(z)+1
ans2=y.index(x)+1
print(abs(ans1-ans2))

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math

def dic(s):
  len(s)= l
  tmp = 1
  for i,c in enumerate(s):
    tmp += (int(c) - i -1) * math.factorial(l - i -1)
  return tmp

n = input()
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))

print(abs(dic(s1)-dic(s2))

import math
 
def dic(s):
  l = len(s)
  if l == 0:
    return 0
  c = s[0]
  tmp = ((sorted(s)).index(c)-1) * math.factorial(l -1)
  return tmp + dic(s[1:])
 
n = input()
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
 
print(abs(dic(s1)-dic(s2)))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from itertools import permutations

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
if p == q:
    print(0)
    exit()
base = sorted(p)
pi = 0
qi = 0
for i, v in enumerate(permutations(base, n)):
    print(v)
    if p == v:
        pi = i + 1
    elif q == v:
        qi = i + 1
        
    if pi * qi != 0:
        break
print(abs(pi-qi))


from itertools import permutations

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
if p == q:
    print(0)
    exit()
base = sorted(p)
pi = 0
qi = 0
for i, v in enumerate(permutations(base, n)):
    if p == v:
        pi = i + 1
    elif q == v:
        qi = i + 1
        
    if pi * qi != 0:
        break
# print(f"{pi} {qi}")
print(abs(pi-qi))




Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
n=int(input())
p=input().split()
q=input().split()
N=[i for i in range(1,n+1)]
def kaijou(x):
    if x==1:
        return 1
    if x==2:
        return 2
    if x==3:
        return 6
    if x==4:
        return 24
    if x==5:
        return 120
    if x==6:
        return 720
    if x==7:
        return 5040

s1=1
s2=1
j=1
for i in p:
    s1 += kaijou(n-j)*(i-1)
    j+=1
j=1
for i in q:
    s2 += kaijou(n-j)*(i-1)
    j+=1
k=s1-s2
if k<0:
    print(-k)
else:
    print(k)


n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
N=[i for i in range(1,n+1)]
p1=[i for i in range(1,n+1)]
q1=[i for i in range(1,n+1)]
def kaijou(x):
    if x==0:
        return 0
    elif x==1:
        return 1
    elif x==2:
        return 2
    elif x==3:
        return 6
    elif x==4:
        return 24
    elif x==5:
        return 120
    elif x==6:
        return 720
    elif x==7:
        return 5040

s1=1
s2=1
j=1
for i in p:
    s1 += kaijou(n-j)*int(p1.index(i))
    j+=1
    p1.remove(i)
j=1
for i in q:
    s2 += kaijou(n-j)*int(q1.index(i))
    j+=1
    q1.remove(i)
k=s1-s2
if k<0:
    print(-k)
else:
    print(k)


Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
N = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

s = []
for i in range(N):
  s.append(i)
p_list = list(itertools.permutations(s))

p_n = 0
q_n = 0
for i in range(len(p_list)):
  if p == p_list[i]:
    p_n = i
  if q == p_list[i]:
    q_n = i
    
print(p_n - q_n) if p_n >= q_n else print(q_n - p_n)


import itertools
N = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

s = []
for i in range(N):
  s.append(i+1)
p_list = list(itertools.permutations(s))

p_n = 0
q_n = 0
for i in range(len(p_list)):
  if p == p_list[i]:
    p_n = i
  if q == p_list[i]:
    q_n = i
    
print(p_n - q_n) if p_n >= q_n else print(q_n - p_n)



Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
def find_power(n):
    
    powlist=[0]*(n+1)
    powlist[0]=1
    powlist[1]=1
    for i in range(2,n+1):
        powlist[i]=powlist[i-1]*i%(10**9+7)
    return powlist
powl=find_power(10)
import math
a=1
b=1
for i in range(n):
    c=1
    d=1
    subp=p[i:]
    subq=q[i:]
    for j in subp:
        if j<subp[0]:
            c+=1
    for k in subq:
        if k<subq[0]:
            d+=1

    a=a+c*int(powl[n-1-i])
    b=b+d*int(powl[n-1-i])
print(a-b)

n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
def find_power(n):
    
    powlist=[0]*(n+1)
    powlist[0]=1
    powlist[1]=1
    for i in range(2,n+1):
        powlist[i]=powlist[i-1]*i%(10**9+7)
    return powlist
powl=find_power(10)
import math
a=1
b=1
for i in range(n):
    c=1
    d=1
    subp=p[i:]
    subq=q[i:]
    for j in subp:
        if j<subp[0]:
            c+=1
    for k in subq:
        if k<subq[0]:
            d+=1

    a=a+c*int(powl[n-1-i])
    b=b+d*int(powl[n-1-i])
print(abs(a-b))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
N=int(input())
P_=list(map(int, input().split()))
Q_=list(map(int, input().split()))
import numpy as np

def stair(N):
  ans=1
  for i in range(1,N+1):
    ans*=i
  return ans

def calc_dict(Q):
  re=list(range(1,len(Q)+1))
  ans=1
  for j in range(len(Q)-1):
    
    if Q[j]!=re[0]:
      a=np.where(np.array(re)<Q[j], 1, 0).sum()
      #print(a)
      ans+=a*stair(len(Q[(j+1):]))
      
      re.remove(Q[j])
    else:
      re.remove(Q[j])
  if ans==0:
    ans+=1
  return ans




N=int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))
base=sorted(Q.copy())
import itertools
count=1
for L in itertools.permutations(base):
    isp=True
    isq=True
    for i in range(N):
        if L[i]!=P[i]:
            isp=False
        if L[i]!=Q[i]:
            isq=False
    if isp:
        p=count
    
    if isq:
        q=count
    #print(L)
    count+=1

print(abs(p-q))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

def calculate(n,p,q):
    x = 0
    y = 0

    for index,ar in enumerate(itertools.permutations([i for i in range(1,n+1)],n)):
        if list(ar) == list(p):
            x = index

        if list(ar) == list(q):
            y = index

    if x >= y:
        print(x-y)
    else:
        print(y-x)
calculate(N,P,Q)

import itertools
# N = 3
# P = [1, 3, 2]
# Q = [3, 1, 2]
#
# N = 8
# P = [7, 3, 5, 4, 2, 1, 6, 8]
# Q = [3, 8, 2, 5, 4, 6, 7, 1]
#
#
# N = 3
# P = [1, 2, 3]
# Q = [1, 2, 3]

N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

def calculate(n,p,q):
    x = 0
    y = 0

    for index,ar in enumerate(itertools.permutations([i for i in range(1,n+1)],n)):
        if list(ar) == list(p):
            x = index

        if list(ar) == list(q):
            y = index

    if x >= y:
        print(x-y)
    else:
        print(y-x)
calculate(N,P,Q)

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from sys import stdin
import sys
import math
from functools import reduce
import itertools

n = int(stdin.readline().rstrip())
p = [x for x in stdin.readline().rstrip().split()]
q = [x for x in stdin.readline().rstrip().split()]

x = ()
y = ()
for pp in p:
    x = x + (pp,)
for qq in q:
    y = y + (qq,)

print(x)

c = []
for i in itertools.permutations([str(j) for j in range(1,n+1)]):
    c.append(i)

print(c)

a = c.index(x)
b = c.index(y)

print (abs(b-a))

from sys import stdin
import sys
import math
from functools import reduce
import itertools

n = int(stdin.readline().rstrip())
p = [x for x in stdin.readline().rstrip().split()]
q = [x for x in stdin.readline().rstrip().split()]

x = ()
y = ()
for pp in p:
    x = x + (pp,)
for qq in q:
    y = y + (qq,)

c = []
for i in itertools.permutations([str(j) for j in range(1,n+1)]):
    c.append(i)
    
a = c.index(x)
b = c.index(y)

print (abs(b-a))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math
n = int(input())

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

num_p = 1
for i in range(n):
    cnt = 0
    for j in range(i+1,n):
        if(p[i] > p[j]):
            cnt += 1
    
    num_p += math.factorial(n-i-1)*cnt

num_q = 1
for i in range(n):
    cnt = 0
    for j in range(i+1,n):
        if(q[i] > q[j]):
            cnt += 1
    #print(cnt)
    num_q += math.factorial(n-i-1)*cnt
    
#print(num_p)
#print(num_q)
print(num_p-num_q)       


import math
n = int(input())

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

num_p = 1
for i in range(n):
    cnt = 0
    for j in range(i+1,n):
        if(p[i] > p[j]):
            cnt += 1
    
    num_p += math.factorial(n-i-1)*cnt

num_q = 1
for i in range(n):
    cnt = 0
    for j in range(i+1,n):
        if(q[i] > q[j]):
            cnt += 1
    #print(cnt)
    num_q += math.factorial(n-i-1)*cnt
    
#print(num_p)
#print(num_q)
print(abs(num_p-num_q))       


Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from math import factorial
from itertools import permutations

n = int(input())
p = tuple(map(str, input().split()))
q = tuple(map(str, input().split()))

num = [str(i) for i in range(1, n+1)]

allnum = list(i for i in permutations(num, n))

print(allnum)
print(abs(allnum.index(p) - allnum.index(q)))

from math import factorial
from itertools import permutations

n = int(input())
p = tuple(map(str, input().split()))
q = tuple(map(str, input().split()))

num = [str(i) for i in range(1, n+1)]

allnum = list(i for i in permutations(num, n))

print(abs(allnum.index(p) - allnum.index(q)))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math

def count_index(list, k):
    count = 0
    for i in range(k):
        if (list[i] < list[k]):
            count = count + 1
    return list[k]-count-1

n = int(input())-1
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a = (p[0]-1) * math.factorial(n)+1
b = (q[0]-1) * math.factorial(n)+1
print(a)
print(b)

for i in range(1,n):
    a = a + count_index(p, i) * math.factorial(n-i)
    b = b + count_index(q, i) * math.factorial(n-i)

ans = a-b
print(abs(ans))

import math

def count_index(list, k):
    count = 0
    for i in range(k):
        if (list[i] < list[k]):
            count = count + 1
    return list[k]-count-1

n = int(input())-1
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a = (p[0]-1) * math.factorial(n)+1
b = (q[0]-1) * math.factorial(n)+1

for i in range(1,n):
    a = a + count_index(p, i) * math.factorial(n-i)
    b = b + count_index(q, i) * math.factorial(n-i)

ans = a-b
print(abs(ans))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from math import factorial

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

def f(X):
    c = 0
    a = [i+1 for i in range(n)]
    for i in range(n):
        a.sort()
        # print(X, a, c)
        if X[i]!=min(a):
            c += factorial(n-i-1)*(a.index(X[i]))
        del a[a.index(X[i])]
    return c+1

print(f(P)-f(Q))

from itertools import permutations

N = int(input())
P = tuple([int(i) for i in input().split()])
Q = tuple([int(i) for i in input().split()])

a, b = 0, 0
for i, v in enumerate(permutations(range(1, N+1), N)):
    if v == P:
        a = i
    if v == Q:
        b = i

print(abs(a-b))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import sys
import math
import itertools
import numpy

rl = sys.stdin.readline

r = map(int, rl().split())
n1 = int(rl().strip())
n2 = int(rl().strip())
li = itertools.combinations(n1, r)
print(abs(li.index(n1) - li.index(n2)))

import sys
import math
import itertools
import numpy

rl = sys.stdin.readline

r = int(rl())
n1 = tuple(map(int, rl().split()))
n2 = tuple(map(int, rl().split()))
li = list(itertools.permutations(range(1, r+1)))
print(abs(li.index(n1) - li.index(n2)))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
 
N = int(input())
P = map(int, input().split())
Q = map(int, input().split())
print(P)

P = tuple(P)
Q = tuple(Q)
print(P) 
dlist = range(1, N+1)
permlist = list(itertools.permutations(dlist))

a = 0
b = 0
for i, p in enumerate(permlist):
	if P == p:
		a = i
	if Q == p:
		b = i
    
print(abs(a-b))

import itertools
 
N = int(input())
P = map(int, input().split())
Q = map(int, input().split())

P = tuple(P)
Q = tuple(Q)
dlist = range(1, N+1)
permlist = list(itertools.permutations(dlist))

a = 0
b = 0
for i, p in enumerate(permlist):
	if P == p:
		a = i
	if Q == p:
		b = i
    
print(abs(a-b))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

def calc_bango(lst):
  ban = 0
  for i in range(N):
    ban += lst[i]*math.factorial(N-i-1)  
  return(ban)

a = calc_bango(P)
b = calc_bango(Q)
print(abs(a-b))

import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

def count_junretsu(retsu):
	
	for i,l in enumerate(itertools.permutations(range(1,N+1))):
		if retsu == list(l):
			return(i+1)

a = count_junretsu(P)
b = count_junretsu(Q)
print(abs(a-b))

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math
import numpy as np
N = int(input())
A = np.array(list(input().split()))
B = np.array(list(input().split()))
a = 0
b = 0 
for i in range(N):
    arank = np.where(A[i:].argsort()==0)[0]
    brank = np.where(B[i:].argsort()==0)[0]
    a += arank*math.factorial(N-(i+1))
    b += brank*math.factorial(N-(i+1))
    print(a,b,arank,brank,A[i:].argsort(),B[i:].argsort())
print(int(abs(a-b)))

import math
import numpy as np
N = int(input())
A = np.array(list(input()))
B = np.array(list(input()))
a = 0
b = 0 
for i in range(N):
    arank = np.where(A[i:].argsort()==0)[0]
    brank = np.where(B[i:].argsort()==0)[0]
    a += arank*math.factorial(N-(i+1))
    b += brank*math.factorial(N-(i+1))
    print(a,b,arank,brank,A[i:].argsort(),B[i:].argsort())
print(int(abs(a-b)))

import math
import numpy as np
N = int(input())
A = np.array(list(input().split()))
B = np.array(list(input().split()))
a = 0
b = 0 
for i in range(N):
    arank = np.where(A[i:].argsort()==0)[0]
    brank = np.where(B[i:].argsort()==0)[0]
    a += arank*math.factorial(N-(i+1))
    b += brank*math.factorial(N-(i+1))
    #print(a,b,arank,brank,A[i:].argsort(),B[i:].argsort())
print(int(abs(a-b)))

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
def func(x, y):
  if len(y)==1:
    dic.append(i*10+int(y))
  else:
    for j in range(len(y)):
      func(i*10+int(y[j]), y[:j]+y[j+1:])
dic = []

N = int(input())
func(0, "".join([i in range(1,N+1)]))

A = int(input().replace(" ", ""))
B = int(input().replace(" ", ""))
ans = abs(dic.index(A)-dic.index(B))
print(ans)

def func(x, y):
  if len(y)==1:
    dic.append(x*10+int(y))
  else:
    for j in range(len(y)):
      func(x*10+int(y[j]), y[:j]+y[j+1:])
dic = []
 
N = int(input())
func(0, "".join([str(i) for i in range(1,N+1)]))
 
A = int(input().replace(" ", ""))
B = int(input().replace(" ", ""))
ans = abs(dic.index(A)-dic.index(B))
print(ans)

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
lists=[]
listt=[]
sums=0
sumt=0

def rank(n,l):
  for i in range(len(l)):
    if n==l[i]:
      l.remove(l[i])
      return i+1
    
for i in range(n):
  lists.append(i+1)
  listt.append(i+1)
for i in range(n):
  sums+=(rank(s[i],lists)-1)*(math.factorial(7-i))
  sumt+=(rank(t[i],listt)-1)*(math.factorial(7-i))
if sums<sumt:
  tmp=sums
  sums=sumt
  sumt=tmp
print(sums-sumt)

import math
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
lists=[]
listt=[]
sums=0
sumt=0

def rank(n,l):
  for i in range(len(l)):
    if n==l[i]:
      l.remove(l[i])
      return i+1
    
for i in range(n):
  lists.append(i+1)
  listt.append(i+1)
for i in range(n):
  sums+=(rank(s[i],lists)-1)*(math.factorial(n-1-i))
  sumt+=(rank(t[i],listt)-1)*(math.factorial(n-1-i))
if sums<sumt:
  tmp=sums
  sums=sumt
  sumt=tmp
print(sums-sumt)

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Nl = list(range(1, N+1))
p = list(itertools.permutations(Nl,N))
c1 = 1
c2 = 1
print(math.factorial(N))
for v in range(math.factorial(N)):
    if list(p[v]) == P:
        break
    else:
        c1 += 1
for v in range(math.factorial(N)):
    if list(p[v]) == Q:
        break
    else:
        c2 += 1
print(abs(c1-c2))

import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Nl = list(range(1, N+1))
p = list(itertools.permutations(Nl,N))
c1 = 1
c2 = 1
for v in range(math.factorial(N)):
    if list(p[v]) == P:
        break
    else:
        c1 += 1
for v in range(math.factorial(N)):
    if list(p[v]) == Q:
        break
    else:
        c2 += 1
print(abs(c1-c2))

import itertools
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Nl = list(range(1, N+1))
p = list(itertools.permutations(Nl,N))
NN = math.factorial(N)
c1 = 1
c2 = 1
for v in range(NN):
    if list(p[v]) == P:
        break
    else:
        c1 += 1
for v in range(NN):
    if list(p[v]) == Q:
        break
    else:
        c2 += 1
print(abs(c1-c2))

Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
n = int(input())

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

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


a = 0
b = 0
r = 0
for i in range(n):
    if ps[i] == qs[i]:
        continue
    else:
        r = i
        break

ts = ps[r:]
ts.sort()

ns = list(itertools.permutations(ts))
m = len(ns)

for j in range(m):
    if list(ns[j]) == ps[r:]:
        a = j
        break

for l in range(m):
    if list(ns[l]) == qs[r:]:
        b = l
        break
    

print(abs(a-b))


import itertools
n = int(input())

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

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


a = 0
b = 0
r = 0
for i in range(n):
    if ps[i] == qs[i]:
        continue
    else:
        r = i
        break

ts = ps[r:]
ts.sort()

ns = list(itertools.permutations(ts))
m = len(ns)

for j in range(m):
    if list(ns[j]) == ps[r:]:
        a = j
        break

for l in range(m):
    if list(ns[l]) == qs[r:]:
        b = l
        break
    

print(abs(a-b))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):

import itertools


def main():
    N = int(input())
    *PArry, = list(map(int, input().split()))
    *QArry, = list(map(int, input().split()))

    a = 0
    b = 0
    comb = [i for i in range(1, N+1)]
    for i, iter in enumerate(itertools.permutations(comb)):
        listIter = list(iter)
        if PArry == listIter:
            a = i+1
        if QArry == listIter:
            a = i+1

    print(abs(a-b))


if __name__ == '__main__':
    main()



import itertools


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

    a = 0
    b = 0
    comb = [i for i in range(1, N+1)]
    for i, iter in enumerate(itertools.permutations(comb)):
        listIter = list(iter)
        if PArry == listIter:
            a = i+1
        if QArry == listIter:
            a = i+1

    print(abs(a-b))


if __name__ == '__main__':
    main()



import itertools


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

    a = 0
    b = 0
    comb = [i for i in range(1, N+1)]
    for i, iter in enumerate(itertools.permutations(comb)):
        listIter = list(iter)
        if PArry == listIter:
            a = i+1
        if QArry == listIter:
            b = i+1

    print(abs(a-b))


if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
def main():
    n = int(input())
    p_list = list(itertools.permutations ([i+1 for i in range(n)]))
    print(type(p_list[0]))
    p = tuple(map(int, input().split(" ")))
    q = tuple(map(int, input().split(" ")))
    pn = 0
    qn = 0
    for i in range(len(p_list)):
        if p == p_list[i]:
            pn = i
        if q == p_list[i]:
            qn = i
    print(abs(pn-qn))

if __name__ == "__main__":
    main()

import itertools
def main():
    n = int(input())
    p_list = list(itertools.permutations ([i+1 for i in range(n)]))
    p = tuple(map(int, input().split(" ")))
    q = tuple(map(int, input().split(" ")))
    pn = 0
    qn = 0
    for i in range(len(p_list)):
        if p == p_list[i]:
            pn = i
        if q == p_list[i]:
            qn = i
    print(abs(pn-qn))

if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import copy

def dic_num(N, s):
    factorial = 1
    cnt = 0
    ss = [0] * N
    for i in range(1, N+1):
        ss[i-1] = i
    
    for i in range(2, N+1):
        factorial *= i
        
    for reN in reversed(range(2, N+1)):
        factorial //= reN
        cnt += (ss.index(s[0]) - ss.index(min(s))) * factorial + 1
        ss.pop(0)
        s.pop(0)
        
        print("{} {}".format(reN, cnt))
    print(cnt)
    return cnt
    

def main():
    N = int(input())
    p_n = list(map(int, input().split()))
    q_n = list(map(int, input().split()))
    
    print(abs(dic_num(N, p_n) - dic_num(N, q_n)))

if __name__ == "__main__":
    main()

from itertools import permutations

def num(a_n, c):
    a_n_number = 0
    for i in range(len(a_n)):
        now = a_n[i]
        cnt= 0
        for k in range(i+1, len(a_n)):
            if a_n[k] <  now:
                cnt += 1
        
        a_n_number += c[i] * cnt
    
    return a_n_number


def main():
    n = int(input())
    p_n = list(map(int,input().split()))
    q_n = list(map(int,input().split()))
    c = [1]
    for i in range(n-1):
        c = [c[0]*(i+1)] + c
        
    p_n_num = num(p_n, c)
    q_n_num = num(q_n, c)
    
    print(abs(p_n_num - q_n_num))
        
    
if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import math


N = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

def CO(l):
    n = len(l)
    if n >= 1:
        m = sorted(l)
        a = l[0]
        s = m.find(a)
        return((s+1)*math.factorial(n-1)+CO(l.pop(0)))
    else:
        return 1

print(abs(CO(list1)-CO(list2)))


import math


N = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

def CO(l):
    n = len(l)
    if n != 1:
        m = sorted(l)
        a = l[0]
        s = m.index(a)
        l.remove(a)
        return((s)*math.factorial(n-1)+CO(l))
    else:
        return 1

print(abs(CO(list1)+CO(list2)))


import math


N = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

def CO(l):
    n = len(l)
    if n != 1:
        m = sorted(l)
        a = l[0]
        s = m.index(a)
        l.remove(a)
        return((s)*math.factorial(n-1)+CO(l))
    else:
        return 1

print(abs(CO(list1)-CO(list2)))


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
import itertools
N = int(input())
P  = list(map(int,input().split()))
Q  = list(map(int,input().split()))
per_list = [list(i) for i in itertools.permutations(range(1,N + 1))]

print(abs(per_list.index(P) - per_list(Q)))

import itertools
N = int(input())
P  = list(map(int,input().split()))
Q  = list(map(int,input().split()))
per_list = [list(i) for i in itertools.permutations(range(1,N + 1))]

print(abs(per_list.index(P) - per_list.index(Q)))

Verdicts: Runtime Error

Accepted
Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
from itertools import permutations

n = int(input())
p = tuple(map(lambda x: int(x) - 1, input().split()))
q = tuple(map(lambda x: int(x) - 1, input().split()))

perm = list(permutations(range(n)))
print(perm.index(p) - perm.index(q))


from itertools import permutations


N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

perm = list(permutations(range(1, N + 1)))
print(abs(perm.index(P) - perm.index(Q)))


Verdicts: Wrong Answer

Accepted