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):
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
ans_p = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(p):
    
    index = defa.index(x)
    ans_p += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
ans_q = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(q):
    index = defa.index(x)
    ans_q += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
print(abs(ans_p-ans_q))

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

ans_p = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(p):
    index = defa.index(x)
    ans_p += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
ans_q = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(q):
    index = defa.index(x)
    ans_q += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
print(abs(ans_p-ans_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()))

A = itertools.permutation(range(1,N))
for i in N:
  if P == A[i]:
    cnt_a = i
  if Q == A[i]:
    cnt_b = i

print(abs(a-b))

import itertools

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

A = list(itertools.permutations(range(1,N+1)))
cnt_a = 0
cnt_b = 0
for i in range(len(A)):
  if P == A[i]:
    cnt_a += i
  if Q == A[i]:
    cnt_b += i

print(abs(cnt_a - cnt_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

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

N_list = [i for i in range(1,N)]
conbination = list(itertools.permutations(N_list, N))

a = conbination.index(tuple(P))
b = conbination.index(tuple(Q))

print(abs(a-b))


import itertools

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

N_list = [i for i in range(1,N)]
conbination = list(itertools.permutations(N_list, 8))

a = conbination.index(tuple(P))
b = conbination.index(tuple(Q))

print(abs(a-b))


import itertools

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

N_list = [1, 2, 3, 4, 5, 6, 7, 8]
conbination = list(itertools.permutations(N_list, 8))

a = conbination.index(tuple(P))
b = conbination.index(tuple(Q))

print(abs(a-b))


import itertools

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

N_list = [i for i in range(1,N+1)]
conbination = list(itertools.permutations(N_list, N))

a = conbination.index(tuple(P))
b = conbination.index(tuple(Q))

print(abs(a-b))

Verdicts: Runtime Error

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):
import math

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

p_sort = sorted(p)
q_sort = sorted(q)

p_number = 0
q_number = 0

k = 0
for i in p:
  k += 1
  i_index = p_sort.index(i)
  p_number += i_index * math.factorial(n-k)
  p.remove(i)
  p_sort.remove(i)

k = 0
for i in q:
  k += 1
  i_index = q_sort.index(i)
  q_number += i_index * math.factorial(n-k)
  q.remove(i)
  q_sort.remove(i)

answer = abs(p_number - q_number)
print(answer)

import math
 
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
 
p_sort = sorted(p)
q_sort = sorted(q)
 
p_number = 1
q_number = 1
 
k = 0
for i in p:
  k += 1
  i_index = p_sort.index(i)
  p_number += i_index * math.factorial(n-k)
  p.remove(i)
  p_sort.remove(i)
k = 0
for i in q:
  k += 1
  i_index = q_sort.index(i)
  q_number += i_index * math.factorial(n-k)
  q.remove(i)
  q_sort.remove(i)
answer = abs(p_number - q_number)
print(answer)

import math
 
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
 
p_sort = sorted(p)
q_sort = sorted(q)
 
p_number = 1
q_number = 1
 
k = 0
for i in p:
  k += 1
  i_index = p_sort.index(i)
  p_number += i_index * math.factorial(n-k)
  p_sort.remove(i)
k = 0
for i in q:
  k += 1
  i_index = q_sort.index(i)
  q_number += i_index * math.factorial(n-k)
  q_sort.remove(i)
answer = abs(p_number - q_number)
print(answer)

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):
n = int(input())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
x, y = 0, 0
perm = list(permutations(list(range(1, n+1))))
# print(perm)
for i in perm:
    if i == a:
        x = perm.index(i)
    if i == b:
        y = perm.index(i)
print(abs(x-y))


from itertools import permutations

n = int(input())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
x, y = 0, 0
perm = list(permutations(list(range(1, n+1))))
# print(perm)
for i in perm:
    if i == a:
        x = perm.index(i)
    if i == b:
        y = perm.index(i)
print(abs(x-y))

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

if __name__ == '__main__':

	n = int(input())

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

    C  = [i for i in range(1,n+1)]
	D = [v for v in itertools.permutations(C, n)]

	a = D.index(A)
	b = D.index(B)

	print(abs(a-b))


import itertools

if __name__ == '__main__':

	n = int(input())

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

	C  = [i for i in range(1,n+1)]
	D = [v for v in itertools.permutations(C, n)]

	a = D.index(A)
	b = D.index(B)

	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())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
res = list(permutations(range(1,n+1)))
ans = abs(res.index(p) - res.index(q))
print(ans)

Not found

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
res = list(permutations(range(1,n+1)))
ans = abs(res.index(p) - res.index(q))
print(ans)

from itertools import permutations
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
res = list(permutations(range(1,n+1)))
ans = abs(res.index(p) - res.index(q))
print(ans)

Verdicts: Runtime Error

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):
import itertools

n = int(input())
p = map(int, input().split())
q = map(int, input().split())
d = dict()
for i, v in enumerate(itertools.permutations([x for x in range(1, n+1, 1)]), 1):
  d[str(v)] = i

print(d[str(tuple(p))] - d[str(tuple(q))])


import itertools

n = int(input())
p = map(int, input().split())
q = map(int, input().split())
d = dict()
for i, v in enumerate(itertools.permutations([x for x in range(1, n+1, 1)]), 1):
  d[str(v)] = i

print(abs(d[str(tuple(p))] - d[str(tuple(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 sys
import math
import bisect
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]


n = ni()
p = na()
q = na()
def check(pq):
    ans = factorial(n)
    a = list(range(1,n+1))
    for i in range(n):
        l = n - i
        pqi = pq[i]
        index = a.index(pqi)
        ans -= factorial(max(l-1, 0)) * max(l-index, 0)
        a.remove(pqi)
    return ans
print(abs(check(p) - check(q)))

import sys
import math
import itertools
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
p = tuple(na())
q = tuple(na())

permutations = list(itertools.permutations(range(1,n+1)))
#print(permutations)
ans = abs(permutations.index(p) - permutations.index(q))
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 fractions
import math

N, M = map(int, input().split())
a = list(map(int, input().split()))
a_uniq = list(set(a))

len_a = len(a_uniq)

# find lcm
gcd_a = a_uniq[0]
for i in range(1, len_a):
    gcd_a = fractions.gcd(gcd_a, a_uniq[i])

lcm = 1
for j in range(len_a):
    lcm *= a_uniq[j]
    
lcm = lcm / gcd_a
lcm /= 2

print(math.ceil((M / lcm) / 2), lcm, M)

import math
import numpy as np

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

a = 1
a_list = list(np.arange(N) + 1)
for i in range(N - 1):
    a += a_list.index(P[i]) * math.factorial(N - (i + 1))
    a_list.remove(P[i])
    
b = 1
b_list = list(np.arange(N) + 1)
for j in range(N):
    b += b_list.index(Q[j]) * math.factorial(N - (j + 1))
    b_list.remove(Q[j])
    
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):
N = int(stdin.readline().rstrip())
P = map(int, input().rstrip().split())
Q = map(int, input().rstrip().split())

import itertools
l = list(itertools.permutations(list(range(1, N+1))))
a = l.index(tuple(P))
b = l.index(tuple(Q))
print(abs(a-b))



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

import itertools
l = list(itertools.permutations(list(range(1, N+1))))
a = l.index(tuple(P))
b = l.index(tuple(Q))
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):
n = int(input())
def bub(n,l):
  tot = 0
  for _ in range(n):
    for i in range(n-1):
      if l[i+1] > l[i]:
        l[i],l[i+1] = l[i+1],l[i]
        tot += 1
  return tot
p = list(map(int, input().split()))
q = list(map(int, input().split()))
print(abs(bub(n,p)-bub(n,q)))

n = int(input())
fac = [i for i in range(10)]
for i in range(2,10):
  fac[i] *= fac[i-1]
def bub(n,l):
  ret = 0
  use = [False] + [True]*n
  for i in range(n):
    ret += sum(use[:l[i]])*fac[n-1-i]
    use[l[i]] = False
  return ret

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


n = int(input())
def bub(x,l):
  if x == n:
    return [tuple(l)]
  ret = list()
  for i in range(1,n+1):
    if i not in l:
      ret += bub(x+1,l+[i])
  return ret
a = bub(0,list())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
print(abs(a.index(p) - a.index(q)))


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 math

N = int(input())

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

def check(A):
    L = [j+1 for j in range(N)]
    ans = 0
    for i in range(N):
        a = L.index(A[i])
        ans += a*math.factorial(N-1-i)
        L.pop(a)
    return ans
p = check(P)
q = check(Q)
print(math.abs(P-Q))

import math

N = int(input())

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

def check(A):
    L = [j+1 for j in range(N)]
    ans = 0
    for i in range(N):
        a = L.index(A[i])
        ans += a*math.factorial(N-1-i)
        L.pop(a)
    return ans
p = check(P)
q = check(Q)
print(abs(P-Q))

import math

N = int(input())

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

def check(A):
    L = [j+1 for j in range(N)]
    ans = 0
    for i in range(N):
        a = L.index(A[i])
        ans += a*math.factorial(N-1-i)
        L.pop(a)
    return ans
p = check(P)
q = check(Q)
print(abs(p-q))

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 = 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)))

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

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()))
ls = list(itertools.parmutations(range(1,n+1)))
print(abs(ls.index(p)-ls.index(q)))

import itertools

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
ls = list(itertools.permutations(range(1,n+1)))
print(abs(ls.index(p)-ls.index(q)))
print(ls)


import itertools

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

dict=list(itertools.permutations(range(1,n+1)))
print(abs(dict.index(p)-dict.index(q)))


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 math

# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
from itertools import permutations
# import numpy as np


# k,x = map(int, input().split())

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
m = [i + 1 for i in range(n)]
c = 0
for i in permutations(m):
    c += 1
    print(i)
    if list(i) == p:
        a = c
    if list(i) == q:
        b = c
print(abs(a - b))


# import math

# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
from itertools import permutations
# import numpy as np


# k,x = map(int, input().split())

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

for i in permutations(m):
    c += 1
    # print(i)
    if list(i) == p:
        a = c
    if list(i) == q:
        b = c

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 itertools

n = int(input())
p_list = tuple(map(int, input().split()))
q_list = tuple(map(int, input().split()))

seq = [i for i in range(1,n+1)]
seq_list = list(itertools.permutations(seq))
sorted_seq = sorted(seq_list)

for i in range(1, len(sorted_seq)):
  if p_list == sorted_seq[i]:
    a = i
    print(a)
  if q_list == sorted_seq[i]:
    b = i
    print(b)
print(abs(a - b))

import itertools

n = int(input())
p_list = tuple(map(int, input().split()))
q_list = tuple(map(int, input().split()))

seq = [i for i in range(1,n+1)]
seq_list = list(itertools.permutations(seq))
sorted_seq = sorted(seq_list)

for i in range(len(sorted_seq)):
  if p_list == sorted_seq[i]:
    a = i
  if q_list == sorted_seq[i]:
    b = i
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 collections import deque
n = int(input())
p_que = deque(map(int, input().split()))
q_que = deque(map(int, input().split()))

que = deque(i for i in range(1, n+1))
cnt = 0
a = 0
b = 0

def dfs(d, q):
  
  
  for i in list(q):
    
    d.append(i)
    q.remove(i)
    
    if q == deque([]):
      global cnt
      cnt += 1
      
      if d == p_que:
        global a
        a = cnt
      if d == q_que:
        global b
        b = cnt
      # print('return')
      d.remove(i)
      q.appendleft(i)
      return
    # print('tansaku')
    dfs(d, q)
    # print('>returned')
    print('---', i, d, q)
    d.remove(i)
    if i > q[0]:
      q.append(i)
    else:
      q.appendleft(i)
    sorted(q)

dfs(deque(), que)
# print(a, b)
print(abs(a-b))

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

n_list = [x for x in range(1, n+1)]
# print(list(permutations(n_list, n)))
a = b = 0
for n, i in enumerate(permutations(n_list, n)):
  # print(n, i)
  if i == P:
    a = n
  if i == Q:
    b = n
    
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()))

ans = []
l = range(1, N + 1)
import itertools


for v in itertools.permutations(l, N):
    if list(v) == P:
      a = list(v).index(P)
        
    if list(v) == Q:
      b = list(v).index(Q)
    

print(abs(b - a))


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

ans = []
l = range(1, N + 1)
import itertools


for v in itertools.permutations(l, N):
    if list(v) == P:
      a = v.index(P)
        
    if list(v) == Q:
      b = v.index(Q)  
    

print(abs(b - a))


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

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

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

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

Verdicts: Runtime Error

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):
from itertools import permutations

N = int(input())
Nlist = [i for i in range(1, int(input())+1)] 
Nlist = list(permutations(Nlist)) 

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

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

from itertools import permutations
 
N = int(input())
Nlist = [i for i in range(1, N+1)] 
Nlist = list(permutations(Nlist)) 
 
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
print(abs(Nlist.index(P) - Nlist.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 copy
def func(n):
    result = []
    result.append([1,2])
    result.append([2,1])
    if n == 2:
        return result
    else:
        
        res_list = func(n-1)
        res = []
        for j in range(n):
            for s in res_list:
                st = copy.copy(s)
                st.insert(j,n)
                res.append(st)
        return res

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

s = func(n)
s.sort()
p_i = 0
q_i = 0
for i in range(n):
    #print(s[i])
    if s[i]==p:
        p_i = i
    if s[i]==q:
        q_i = i
print(abs(p_i-q_i))


import copy
def func(n):
    result = []
    result.append([1,2])
    result.append([2,1])
    if n == 2:
        return result
    else:
        
        res_list = func(n-1)
        res = []
        for j in range(n):
            for s in res_list:
                st = copy.copy(s)
                st.insert(j,n)
                res.append(st)
        return res

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

s = func(n)
s.sort()
p_i = 0
q_i = 0
for i in range(n):
    #print(s[i])
    if s[i]==p:
        p_i = i
    if s[i]==q:
        q_i = i
print(abs(p_i,q_i))


import copy
def func(n):
    result = []
    result.append([1,2])
    result.append([2,1])
    if n == 2:
        return result
    else:
        
        res_list = func(n-1)
        res = []
        for j in range(n):
            for s in res_list:
                st = copy.copy(s)
                st.insert(j,n)
                res.append(st)
        return res

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

s = func(n)
srt = sorted(s)
p_i = 0
q_i = 0
for i in range(n):
    print(srt[i])
    if srt[i]==p:
        p_i = i
    if srt[i]==q:
        q_i = i
print(abs(p_i,q_i))


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

l = [int(x) for x in range(1,n+1)]
i = 0
p_i = 0
q_i = 0
for v in itertools.permutations(l, len(l)):
    i += 1
    if p==list(v):
        p_i = i
        #print(list(v))
    if q==list(v):
        q_i = i
        #print(i,list(v))
print(abs(p_i-q_i))


Verdicts: Wrong Answer

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 = input()
s1 = map(int,input().split())
print(type(sl))

import math

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

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

ord1 = 0
for i in range(n-1):
#	print((p[i]-1) * math.factorial(n-1-i) )
	ord1 = ord1 + (p[i]-1) * math.factorial(n-1-i) 
  
ord2 = 0
for i in range(n-1):
	ord2 = ord2 + (q[i]-1) * math.factorial(n-1-i) 

print(abs(ord2 - ord1))

n = int(input())
s1 = list(map(int,input().split()))
#print(type(s1))
val1 = 0
for i in range(n):
	val1 = val1 + s1[i] * math.factorial(10-i-1) // math.factorial(10-n)
#	print(math.factorial(10-i-1) // math.factorial(10-n))
print(val1)

import math

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

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

ord1 = 0
flag = [0] * (n+1)
for i in range(n-1):
	ln = 0
#	print(type(p[i]), p[i])
	for c in range(1,p[i]):
		if flag[c] == 0:
			ln = ln + 1 
	flag[p[i]] = 1
	ord1 = ord1 + ln * math.factorial(n-1-i)
#	print(ln,  math.factorial(ln))
ord1 = ord1+1 
#print(ord1)
  
ord2 = 0
flag = [0] * (n+1)
for i in range(n-1):
	ln = 0

	for c in range(1,q[i]):
		if flag[c] == 0:
			ln = ln + 1 
	flag[q[i]] = 1
	ord2 = ord2 + ln * math.factorial(n-1-i)
#	print(ln,  math.factorial(ln))
ord2 = ord2+1 
#print(ord2)

print(abs(ord2 - ord1))

Verdicts: Runtime Error

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):
from itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

l=list(itertools.permutations(list(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(list(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())
L1 = tuple(map(int,input().split()))
a = 0
L2 = tuple(map(int,input().split()))
b = 0
L = [i for i in range(1,N+1)]
Base = list(itertools.permutations(L))
print(Base)
for i in range(len(Base)):
    if Base[i] == L1:
        a = i+1
for i in range(len(Base)):
    if Base[i] == L2:
        b = i+1
print(abs(a-b))

import itertools
N = int(input())
L1 = tuple(map(int,input().split()))
a = 0
L2 = tuple(map(int,input().split()))
b = 0
L = [i for i in range(1,N+1)]
Base = list(itertools.permutations(L))
for i in range(len(Base)):
    if Base[i] == L1:
        a = i+1
for i in range(len(Base)):
    if Base[i] == L2:
        b = i+1
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 itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
lists = list(itertools.permutations(list(range(1, n+1)), n))
print(abs(lists.index(p) - lists.index()))

import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
lists = list(itertools.permutations(list(range(1, n+1)), n))
print(abs(lists.index(p) - lists.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):
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
list1 = [i for i in range(1, n + 1)]
ans_a = 0
ans_b = 0
for i in range(n):
    if p[i] != 1:
        ans_a += (p[i]-1)** (n-i-1)
    if q[i] != 1:
        ans_b += (q[i]-1) ** (n-i-1)
print(ans_a)
print(ans_b)

import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
num = [i for i in range(1,n+1)]
lst = list(itertools.permutations(num))
pos_p = 0
pos_q = 0
for i in range(len(lst)):
    temp = list(lst[i])
    if temp == p:
        pos_p = i
    if temp == q:
        pos_q = i
print(abs(pos_p - pos_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()))

import itertools
a = list(itertools.permutations([i for i in range(1, n+1)]))
print(abs(a.index(p)-a.index(q)))


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

import itertools
a = list(itertools.permutations([i for i in range(1, n+1)]))
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
import math
N=int(input())
P=list(map,int,input().split())
Q=list(map,int,input().split())
L=list(map(list,list(itertools.permutations(list(i for i in range(1,N+1)),N))))
a=L.index(P)
b=L.index(Q)
print(abs(a-b))




import itertools
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = list(map(list,list(itertools.permutations(list(i for i in range(1,N+1)),N))))
a = L.index(P)
b = L.index(Q)
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):
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
ls = [i for i in range(1,N+1)]
A = list(permutations(ls))
 
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()))
ls = [i for i in range(1,N+1)]
A = list(itertools.permutations(ls))
 
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):
n=map(int,input())
fact=[1,1,2,6,24,120,720,5040,40320]
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
for i in range(n):
	a=a+((p[i]-1)*(fact[n-i-1]))
	b=b+((q[i]-1)*(fact[n-i-1]))
	for j in range(i+1,n):
		if(p[j]>p[i]):
			p[j]=p[j]-1
		if(q[j]>q[i]):
			q[j]=q[j]-1
print(abs(a-b))

n=int(input())
fact=[1]*13
for i in range(2,13):
	fact[i]=i*fact[i-1]
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
for i in range(n):
	a=a+((p[i]-1)*(fact[n-i-1]))
	b=b+((q[i]-1)*(fact[n-i-1]))
	for j in range(i+1,n):
		if(p[j]>p[i]):
			p[j]=p[j]-1
		if(q[j]>q[i]):
			q[j]=q[j]-1
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):
count = 1
tmpp, tmpq = 0, 0
li = []

for i in range(1, n + 1):
    li.append(i)

pattern = list(itertools.permutations(li))

for i in pattern:
    if i == p:
        tmpp = count

    if i == q:
        tmpq = count

    count += 1

print(abs(tmpp - tmpq))


import itertools

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

count = 1
tmpp, tmpq = 0, 0
li = []

for i in range(1, n + 1):
    li.append(i)

pattern = list(itertools.permutations(li))

for i in pattern:
    if i == p:
        tmpp = count

    if i == q:
        tmpq = count

    count += 1

print(abs(tmpp - tmpq))


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())
    p = tuple(input().split())
    q = tuple(input().split())

    a = list(itertools.permutations([str(i+1) for i in range(n)]))
    print(a)
    print(abs(a.index(q)-a.index(p)))


if __name__ == "__main__":
    main()

import itertools


def main():
    n = int(input())
    p = tuple(input().split())
    q = tuple(input().split())

    a = list(itertools.permutations([str(i+1) for i in range(n)]))
    print(abs(a.index(q)-a.index(p)))


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):
from itertools import permutations
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

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



for i,per in enumerate(list(permutations(A))):
  if per == P:
    x = i
  if per == Q:
    y = i
  
print(abs(x-y))


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

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

from itertools import permutations

for i,per in enumerate(list(permutations(A))):
  if per == P:
    x = i
  if per == Q:
    y = i
  
print(abs(x-y))

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

a = [i+1 for i in range(n)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print(abs(x-y))


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

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

for i,per in enumerate(list(permutations(A))):
  if per == P:
    x = i
  if per == Q:
    y = i
  
print(abs(x-y))


from itertools import permutations
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

a = [i for i in range(1,n+1)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print(abs(x-y))


Verdicts: Runtime Error

Runtime Error

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

R = list(itertools.permutations(list(range(1,N+1))))
a = 0
b = 0

for i in range(len(R)):
    if list(R[i]) == P:
        a = i+1
    elif list(R[i]) == Q:
        b = i+1
    if a!=0 and b!=0:
        break
        
print(abs(a-b))

import sys
input = sys.stdin.readline
import itertools

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

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

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

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):
def get_order(numbers, originals):
    
    from math import factorial
    from copy import copy
    numbers = copy(numbers)
    originals = copy(originals)
    
    total = 0
    while numbers:
        number = numbers.pop(0)
        number_index = originals.index(number)
        originals.pop(number_index)
        count = (number_index ) * factorial(len(originals))
        total += count
    return total
        
def get_next_int():
    return int(float(input()))
def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])

def main(print=__builtins__.print):
    n = get_next_int()
    
    a = get_next_ints()
    b = get_next_ints()
    originals = list(map(int, range(n)))
    print(get_order(a, originals) - get_order(b, originals))

if __name__ == '__main__':
    main()
    

def get_order(numbers, originals):
    
    from math import factorial
    from copy import copy
    numbers = copy(numbers)
    originals = copy(originals)
    
    total = 0
    while numbers:
        number = numbers.pop(0)
        number_index = originals.index(number)
        originals.pop(number_index)
        count = (number_index ) * factorial(len(originals))
        total += count
    return total
        
def get_next_int():
    return int(float(input()))
def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])

def main(print=__builtins__.print):
    n = get_next_int()
    
    a = list(get_next_ints())
    b = list(get_next_ints())
    originals = list(map(int, range(1, n+1)))
    print(get_order(a, originals) - get_order(b, originals))

if __name__ == '__main__':
    main()
    

def get_order(numbers, originals):
    
    from math import factorial
    from copy import copy
    numbers = copy(numbers)
    originals = copy(originals)
    
    total = 0
    while numbers:
        number = numbers.pop(0)
        number_index = originals.index(number)
        originals.pop(number_index)
        count = (number_index ) * factorial(len(originals))
        total += count
    return total
        
def get_next_int():
    return int(float(input()))
def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])

def main(print=__builtins__.print):
    n = get_next_int()
    
    a = list(get_next_ints())
    b = list(get_next_ints())
    originals = list(map(int, range(1, n+1)))
    print(abs(get_order(a, originals) - get_order(b, originals)))

if __name__ == '__main__':
    main()
    

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


Ns=list(itertools.permutations(range(1,N+1)))
PO=Ns.index(P)
QO=Ns.index(Q)
print(abs(Po-QO))


import itertools

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


Ns=list(itertools.permutations(range(1,N+1)))
PO=Ns.index(P)
QO=Ns.index(Q)
print(abs(PO-QO))


import itertools

N=int(input())
p=list(map(str,input().split()))
q=list(map(str,input().split()))
P=''.join(p)
Q=''.join(q)

ns=list(itertools.permutations(range(1,N+1)))
Ns=[]
for i in range(len(ns)):
    temparr=[]
    for j in range(N):
        a=ns[i][j]
        temparr.append(str(a))
    b=''.join(temparr)
    Ns.append(b)

PO=Ns.index(P)
QO=Ns.index(Q)
print(abs(PO-QO))


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):
permutations = list(itertools.permutations(range(1, N+1)))
p = permutations.index(P)
q = permutations.index(Q)

import itertools
N = int(input())
LN = []
for i in range(N):
  LN.append(i+1)
LLN=list(itertools.permutations(LN))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
print(LLN.index(tuple(P))-LLN.index(tuple(Q)))

import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
permutations = list(itertools.permutations(range(1, N+1)))
p = permutations.index(P)
q = permutations.index(Q)
print(p-q)

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

Verdicts: Runtime Error

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 itertools


N = int(input())

P = "".join(input().split())
Q = "".join(input().split())

print(P)

p_index = -1
q_index = -1
permutation_list = list(itertools.permutations([x + 1 for x in range(N)]))

for i in range(len(permutation_list)):
    
    param = "".join(map(str, permutation_list[i]))
    if P == param:
        p_index = i
    if Q == param:
        q_index = i
    if p_index != -1 and q_index != -1:
        result = p_index - q_index
        if result < 0:
            result = -result
        print(result)
        exit(0)


import itertools


N = int(input())

P = "".join(input().split())
Q = "".join(input().split())

p_index = -1
q_index = -1
permutation_list = list(itertools.permutations([x + 1 for x in range(N)]))
for i in range(len(permutation_list)):
    param = "".join(map(str, permutation_list[i]))
    if P == param:
        p_index = i
    if Q == param:
        q_index = i
    if p_index != -1 and q_index != -1:
        print(abs(p_index - q_index))
        exit(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=list(map(int,input().split()))
Q=list(map(int,input().split()))
def sn(a,n):
  c=0
  for i in a:
    if i<n:
      c+=1
  return c
def comb(n):
  k=1
  if n<=1:
    return 1
  for i in range(1,n+1):
    k=k*i
  return k
def numberorder(a):
  s={1,2,3,4,5,6,7,8}
  c=0
  for p in range(8):
    c+=(sn(s,a[p]))*comb(7-p)
    s=s-{a[p]}
  return c
P_num=numberorder(P)
Q_num=numberorder(Q)
print(abs(P_num-Q_num))

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
def sn(a,n):
  c=0
  for i in a:
    if i<n:
      c+=1
  return c
def comb(n):
  k=1
  if n<=1:
    return 1
  for i in range(1,n+1):
    k=k*i
  return k
def numberorder(a,n):
  s=set({})
  for i in range(1,n+1):
    s=s|{i}
  c=0
  for p in range(n):
    c+=(sn(s,a[p]))*comb(n-1-p)
    s=s-{a[p]}
  return c
P_num=numberorder(P,N)
Q_num=numberorder(Q,N)
print(abs(P_num-Q_num))

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()))
ls = list(itertools.permutations(range(1,N+1)))
print(abs(ls.index(P) - ls.index(Q)))

from itertools import permutations

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

ls = list(permutations(range(1,N+1)))

print(abs(ls.index(P) - ls.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):
N = int(input())
P = list(map(str,input().split()))
Q = list(map(str,input().split()))

import itertools
 
n_list = [i+1 for i in range(N)]
all_list = list(itertools.permutations(A))
a = all_list.index(P)
b = all_list.index(Q)
print(abs(a - b))


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

import itertools

A = [i+1 for i in range(N)]
all_permutation_list = list(itertools.permutations(A))
a = all_permutation_list.index(P)
b = all_permutation_list.index(Q)
print(abs(a - b))

Verdicts: Runtime Error

Accepted