Problem:


Score : 300 points

Problem Statement

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|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0



Sample input:
3
1 3 2
3 1 2


Sample output:
3

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(1,n+1):
    a.append(i)
p=list(map(int,input().split()))
q=list(map(int,input().split()))
from itertools import permutations
perm=permutations(a)
aa=0
bb=0
count=0
for i in perm:
    f=True
    for j in range(n):
        if(i[j]!=p[j]):
            f=False
    if(f):
        aa=count
    f=True
    for j in range(n):
        if(i[j]!=q[j]):
            f=False
    if(f):
        bb=count
    if(aa and bb):
        break
    count+=1
    # print(count,aa,bb)
print(abs(aa-bb))

Submission status: Accepted

Solution code:
import itertools

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

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

a = 0
b = 0
for v in itertools.permutations(li, N):
    a += 1
    b += 1
    aaa = 1
    bbb = 1
    for i in range(N):
        if v[i] != P[i]:
            aaa = 0
            break
    if aaa == 1:
        ansa = a
    for i in range(N):
        if v[i] != Q[i]:
            bbb = 0
            break
    if bbb == 1:
        ansb = b
print(abs(ansa-ansb))

Submission status: Accepted

Solution code:
from itertools import permutations
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))

    per = tuple(permutations(range(1, N + 1)))
    a = per.index(P)
    b = per.index(Q)
    print(abs(b - a))


main()

Submission status: Accepted

Solution code:
import itertools

N = int(input())

Ps = tuple(map(int, input().split()))
Qs = tuple(map(int, input().split()))

Ns = [i for i in range(1, N + 1)]
perms = itertools.permutations(Ns, N)

p_found = False
q_found = False
for i, p in enumerate(perms):
    if Ps == p:
        a = i
        p_found = True
    if Qs == p:
        b = i
        q_found = True
    if p_found and q_found:
        break

print(abs(a - b))

Submission status: Accepted

Solution code:
import bisect
import itertools

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

A = []
for i in range(1,N+1):
  A.append(i)
B = list(itertools.permutations(A))
C = []
for b in B:
  d = 0
  for bb in b:
    d *= 10
    d += bb
  C.append(d)
C.sort()

p = 0
for pp in P:
  p *= 10
  p += pp
q = 0
for qq in Q:
  q *= 10
  q += qq
a = bisect.bisect_left(C,p)
b = bisect.bisect_left(C,q)
print(abs(a-b))

Submission status: Accepted

Solution code:
N = int(input())
Ps = int("".join(input().split()))
Qs = int("".join(input().split()))

import itertools

l = list(range(1, N + 1))
l = itertools.permutations(l)
ln = []
for L in l:
    ln.append(int("".join(map(str, L))))
a = 0
b = 0
for i in range(len(ln)):
    if Ps == ln[i]:
        a = i
        break
for j in range(len(ln)):
    if Qs == ln[j]:
        b = j
        break

print(abs(a - b))

Submission status: Accepted

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

Q_list=list(itertools.permutations(sorted(Q)))
for i,q in enumerate(Q_list):
  q=list(q)
  if q==P:
    a=i
  if q==Q:
    b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
p = q = 0
for i,v in enumerate(permutations([i+1 for i in range(N)],N)):
    if P == v:
        p = i
    if Q == v:
        q = i
print(abs(q - p))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
p = tuple(int(v) for v in input().split())
q = tuple(int(v) for v in input().split())

def fact(n):
    return fact(n - 1) * n if n > 1 else 1

ap = dict(zip(permutations(range(1, n + 1), n), range(fact(n))))
print(abs(ap[p] - ap[q]))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))

l = sorted(list(permutations(list(range(1,N+1)))))


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

Submission status: Accepted

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

l = list(range(1, N + 1))
lst = []

for v in itertools.permutations(l, N):
	lst.append(v)
	
print(abs(lst.index(Q) - lst.index(P)))

Submission status: Accepted

Solution code:
from itertools import permutations as perm
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a,b = 0,0

for i,tmp in enumerate(sorted(perm(range(1,n+1)))):
    if tmp[0] != p[0] and  tmp[0] != q[0]:
        continue
    for s,t in zip(tmp, p):
        if s != t:
            break
    else:
        a = i+1
    for s,t in zip(tmp, q):
        if s != t:
            break
    else:
        b = i+1
print(abs(a-b)) 
# print(a,b)

Submission status: Accepted

Solution code:
import itertools

N = int(input())
seq = list(range(1,N + 1))

all = itertools.permutations(seq)
all = list(all)
Ps =  list(map(int,input().split(" ")))
Ps = tuple(Ps)
Qs =  list(map(int,input().split(" ")))
Qs = tuple(Qs)
for i,li in  enumerate(all):
    if li == Ps:
        a = i
        break


for i,li in  enumerate(all):
    if li == Qs:
        b = i
        break
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

pn = l.index(P)
qn = l.index(Q)
print(abs(pn-qn))

Submission status: Accepted

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

from itertools import permutations
li = [i for i in range(1, n + 1)]
ptn = list(permutations(li))

for itr, val in enumerate(ptn):
  if p == val:
    a = itr
  if q == val:
    b = itr

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = ''.join(input().split())
b = ''.join(input().split())

perm = list(itertools.permutations( list(str(i) for i in range(1, n+1)) ))
perm = list(map(''.join, perm))

print( abs(perm.index(a) - perm.index(b)))

Submission status: Accepted

Solution code:
import itertools

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

all_list = list(itertools.permutations(range(1, N + 1)))
p_index = all_list.index(P)
q_index = all_list.index(Q)
print(abs(p_index - q_index))

Submission status: Accepted

Solution code:
def main():
    from itertools import permutations
    n=int(input())
    a=tuple(map(int,input().split()))
    b=tuple(map(int,input().split()))
    
    p=list(permutations(range(1,n+1)))
    d={i:j for j,i in enumerate(p)}
    print(abs(d[a]-d[b]))


main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

for i, pe in enumerate(per, start=1):
    if P == pe:
        a = i
    if Q == pe:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    num_list = []
    for a in permutations(range(1,N+1)):
        num_list.append(a)
    num_list.sort
    b = -1
    c = -1
    for i, a in enumerate(num_list):
        if list(a) == P:
            b = i
        if list(a) == Q:
            c = i

        if b != -1 and c != -1:
            break

    print(abs(b-c))

    
            



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

x = sorted(p)
ab = []
for i, per in enumerate(permutations(x, n)):
    if per == p or per == q:
        ab.append(i) 

if len(ab) < 2:
    print(0)
else:
    print(abs(ab[0]-ab[1]))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
per = list(permutations(range(1,N+1)))
for i in range(len(per)):
    if list(per[i]) == P:
        a = i
    if list(per[i]) == Q:
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = list()
for i in range(1, n+1):
    l.append(i)
a = list(itertools.permutations(l))
print(abs(a.index(p)-a.index(q)))

Submission status: Accepted

Solution code:
from sys import stdin
import math

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
lin = list(map(int,stdin.readline().rstrip().split()))
from itertools import permutations
lis = permutations(li)

liv = []

for i in lis:
    liv.append(list(i))
liv.sort()
a = liv.index(li)
b = liv.index(lin)

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
X = sorted(list(permutations(list(range(1,N+1)))))
for i,x in enumerate(X):
    if x==P: a=i
    if x==Q: b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

base = tuple([i for i in range(1, N+1)])

a = 0
b = 0
L = list(itertools.permutations(base))
for i, l in enumerate(L):
  if l == P:
    a = i
  if l == Q:
    b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=list(itertools.permutations(range(1,n+1)))
x=0
y=0
for i in range(len(l)):
    if p==l[i]:
        x=i
    if q==l[i]:
        y=i
print(abs(x-y))

Submission status: Accepted

Solution code:
import itertools






from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


def main():
    n = ii()
    a = list(range(1,n+1))
    p = li()
    q = li()
    pc = 0
    qc = 0
    for e,i in enumerate(itertools.permutations(a)):
        
        lli = list(i)
        if lli == p:
            pc = e

        if lli == q:
            qc = e

    print(abs(pc-qc))


    
main()

Submission status: Accepted

Solution code:
import itertools

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

index=0
for i in itertools.permutations(range(1,N+1), N):   
    if P==list(i):
        a=index
    if Q==list(i):
        b=index
    index+=1
print(abs(b-a))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
inputs = sys.stdin.readlines()

n = int(inputs[0])
P = list(map(int, inputs[1].split()))
Q = list(map(int, inputs[2].split()))

p, q = ''.join(map(str, P)), ''.join(map(str, Q))

p_index = q_index = -1
for i, x in enumerate(permutations(range(1, n+1))):
    x = ''.join(map(str, x))
    if x == p:
        p_index = i
    if x == q:
        q_index = i
    if p_index != -1 and q_index != -1:
        break
print(abs(p_index - q_index))

Submission status: Accepted

Solution code:
import itertools

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

alls = list(itertools.permutations(seed))

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

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

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

Submission status: Accepted

Solution code:
import sys
import itertools
import math

input = sys.stdin.readline

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

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

p_list = list(itertools.permutations(l, N))

for i in range(N):
    p_list.sort(key = lambda x: x[N - i - 1])

a = p_list.index(P)
b = p_list.index(Q)

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
if p == q:
  print(0)
  exit()
nums = [i for i in range(1,n+1)]
a = [0,0]
now = 0
for k,i in enumerate(itertools.permutations(nums)):
  if i == p or i == q:
    a[now] = k
    now += 1
print(abs(a[1]-a[0]))

Submission status: Accepted

Solution code:

N = int(input())

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

q = [[]]

for i in range(N):

    nq = []
    for j in range(len(q)):

        for k in range(N):
            k += 1

            if k not in q[j]:
                ad = q[j].copy()
                ad.append(k)
                nq.append(ad)

    q = nq

q.sort()

a = None
b = None

for i in range(len(q)):

    if " ".join(map(str,q[i])) == " ".join(map(str,P)):
        a = i
    if " ".join(map(str,q[i])) == " ".join(map(str,Q)):
        b = i

print (abs(a-b))

Submission status: Accepted

Solution code:
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)))
a = A.index(P) + 1
b = A.index(Q) + 1
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

def solve(N, P, Q):
    l = list(permutations(range(1, N+1)))
    
    a = l.index(P)
    b = l.index(Q)
    return abs(a-b)

def main():
    N = int(input())
    P = tuple(int(x) for x in input().split())
    Q = tuple(int(x) for x in input().split())
    ans = solve(N, P, Q)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = list(range(1,N+1))
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
P = tuple(P)
Q = tuple(Q)
cnt = 0
for i in itertools.permutations(A):
  cnt += 1
  if i == P:
    a = cnt
  if i == Q:
    b = cnt
print(abs(a-b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import itertools
def index(p):
    for i, q in enumerate(itertools.permutations(range(1, len(p) + 1))):
        if p == q:
            return i
    assert False
def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    print(abs(index(p) - index(q)))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
n=int(input())
l=[i for i in range(1,n+1)]
p=list(map(int,input().split()))
q=list(map(int,input().split()))
m=list(itertools.permutations(l))
for i in range(len(m)):
  if p==list(m[i]):
    p=i
  if q==list(m[i]):
    q=i
print(abs(p-q))

Submission status: Accepted

Solution code:
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

import itertools as iter

N = num()
P = tuple(nums())
Q = tuple(nums())
pmts = list(iter.permutations(range(1,N+1)))
pmts.sort()
a = pmts.index(P)
b = pmts.index(Q)
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

x = list(permutations(list(range(1, n + 1))))

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

Submission status: Accepted

Solution code:
import itertools

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

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

l = list(itertools.permutations(nl))
sl = []

for i in l:
    sl.append("".join(list(map(str,i))))

sl.sort()
for i in range(len(sl)):
    if (sl[i] == P):
        a = i
    if (sl[i] == Q):
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
Plist = tuple(map(int, input().split()))
Qlist = tuple(map(int, input().split()))

perm = list(permutations(range(1, N+1)))
a = perm.index(Plist)
b = perm.index(Qlist)

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

l = list(map(list, list(permutations(list(range(1, n+1)), n))))

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

Submission status: Accepted

Solution code:
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
import itertools
seq=tuple([i+1 for i in range(n)])
a=list(itertools.permutations(seq))
import bisect
x=bisect.bisect_left(a,p)
y=bisect.bisect_left(a,q)
print(abs(x-y))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import itertools


def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    Q = [int(x) for x in input().split()]
    y = [int(x) for x in range(1, N + 1)]
    x = list(itertools.permutations(y))
    a = 0
    b = 0
    for i, v in enumerate(x):
        for j in range(N):
            if P[j] != v[j]:
                break
        else:
            a = i
        for j in range(N):
            if Q[j] != v[j]:
                break
        else:
            b = i
    print(abs(a - b))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = list(input().split())
Q = list(input().split())


p = int("".join(P))
q = int("".join(Q))


R = []
for i in range(N):
    R.append(str(i+1))

r = int("".join(R))

count = 1
for i in permutations(R):
    j = int("".join(i))
    if j == p:
        a = count
    if j == q:
        b = count
    count += 1

print(abs(b-a))

Submission status: Accepted

Solution code:
N = int(input())
P = [int(p) for p in input().split()]
Q = [int(q) for q in input().split()]

import itertools

L = []
for temp in itertools.permutations(P):
    L.append(temp)
    
L.sort()
for i in range(len(L)):
    if list(L[i]) == P:
        a = i
    if list(L[i]) == Q:
        b = i
        
print(abs(a-b))

Submission status: Accepted

Solution code:
from sys import stdin
import itertools
N = int(stdin.readline().rstrip())
P = [int(x) for x in stdin.readline().rstrip().split()]
Q = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = 0
ans2 = 0
cnt = 1
for v in itertools.permutations(list(range(1,N+1))):
    if v == tuple(P):
        ans1 = cnt
    if v == tuple(Q):
        ans2 = cnt
    cnt += 1
print(abs(ans1-ans2))

Submission status: Accepted

Solution code:
# 辞書順の差を答える
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = -1
b = -1
count = 1
for x in itertools.permutations(range(1, N+1)):
    if P == list(x):
        a = count
    if Q == list(x):
        b = count
    count += 1

print(abs(a-b))

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
import itertools
l=[]
ans=[]
for i in range(n):
	l.append(i+1)
for v in itertools.permutations(l,n):
	ans.append(list(v))
for i in range(len(ans)):
	if p==ans[i]:
		pp=i
	if q==ans[i]:
		qq=i
print(abs(pp-qq))

Submission status: Accepted

Solution code:
import itertools

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

i = 1
for l in itertools.permutations(list(range(1, n + 1))):
    if l == p:
        x = i
    if l == q:
        y = i
    i += 1

print(abs(x - y))

Submission status: Accepted

Solution code:
n=int(input())
p="".join([i for i in input().split()])
q="".join([i for i in input().split()])
hoge=[]
def dfs(q,st):
    global hoge
    if q==n:
        hoge.append(st)
    else:
        for i in range(1,n+1):
            if str(i) not in st:
                dfs(q+1,st+str(i))
dfs(0,"")
hoge.sort()
print(abs(hoge.index(p)-hoge.index(q)))

Submission status: Accepted

Solution code:
from itertools import *

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

c = 0
for p in permutations(range(1, N+1)):
  if p==P:
    a = c
  if p==Q:
    b = c
  c += 1
print abs(a-b)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ps = ""
    for i in P:
        ps = ps + str(i)
    qs = ""
    for i in Q:
        qs = qs + str(i)
    import itertools
    A = [i+1 for i in range(N)]
    i = 0
    p = q = 0
    for a in itertools.permutations(A):
        s = ""
        for j in a:
            s = s + str(j)
        if ps == s:
            p = i
        if qs == s:
            q = i
        i += 1
    print(abs(p-q))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = tuple([ int(v)-1 for v in input().split() ])
b = tuple([ int(v)-1 for v in input().split() ])
t = 0
for i in itertools.permutations(range(n)):
    if a == i:
        xa = t
    if b == i:
        xb = t
    t += 1
print(abs(xa-xb))

Submission status: Accepted

Solution code:
import itertools as it

N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
a, b = 0, 0
count = 0
for perm in it.permutations(range(1, N + 1)):
    count += 1
    if list(perm) == P:
        a = count
    if list(perm) == Q:
        b = count
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools


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

nums = [0] * N
for i in range(N):
    nums[i] = i+1

count = 1
for i in itertools.permutations(nums):
    if (list(i)==P):
        c1 = count
    if (list(i)==Q):
        c2 = count
    count +=1

print(abs(c2-c1))

Submission status: Accepted

Solution code:
import itertools

N = int(input().strip())
P = ''.join(input().strip().split(' '))
Q = ''.join(input().strip().split(' '))


all = sorted(''.join(p) for p in itertools.permutations(P))
print(abs(all.index(P) - all.index(Q)))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = j = -1
cur = 0
for c in permutations(range(1, n + 1)):
    c = list(c)
    if c == b: j = cur
    if c == a: i = cur
    cur += 1
print(abs(j - i))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
p = tuple([int(x) for x in input().split()])
q = tuple([int(x) for x in input().split()])

a = -1
b = -1
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))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=[i for i in range(1,n+1)]
ans1=0
k=0
ans2=0
for i in itertools.permutations(a):
    if list(i)==p:
        ans1=k
    if list(i)==q:
        ans2=k
    k+=1
print(abs(ans1-ans2))

Submission status: Accepted

Solution code:
from itertools import permutations


def solve():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    
    a, b = -1, -1
    cnt = 0
    for nums in permutations(range(1, N + 1), N):
        nums = list(nums)
        if nums == P:
            a = cnt
        if nums == Q:
            b = cnt
        cnt += 1
    print(abs(a - b))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

from itertools import permutations

for i,p in enumerate(list(permutations(range(1,N+1)))):
    flag = True
    for j in range(N):
        if p[j]!=P[j]:
            flag=False
            break
    if flag:
        break
a = i+1

for i,p in enumerate(list(permutations(range(1,N+1)))):
    flag = True
    for j in range(N):
        if p[j]!=Q[j]:
            flag=False
            break
    if flag:
        break

b=i+1

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#

def main():

    N = ii()
    P = tuple(mis())
    Q = tuple(mis())
    for i, perm in enumerate(permutations(range(1,N+1))):
        if perm == P:
            a = i
        if perm == Q:
            b = i
    print(abs(a-b))




main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
import itertools
def ii(): return int(readline())
def mi(): return map(int, readline().rstrip().split())
def li(): return list(readline().rstrip())
def lmi(): return list(map(int, readline().rstrip().split()))
def end(*arg): print(*arg); sys.exit()
# template


def main():
    n = ii()
    P = tuple(lmi())
    Q = tuple(lmi())
    p, q = 0, 0
    a = [i + 1 for i in range(n)]
    for idx, l in enumerate(itertools.permutations(a, n)):
        if P == l:
            p = idx
        if Q == l:
            q = idx
    print(abs(p - q))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations
N=int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l=list(permutations(range(1,N+1)))
for i in range(len(l)):
    if P==l[i]:
        p=i
for i in range(len(l)):
    if Q==l[i]:
        q=i
print(abs(p-q))

Submission status: Accepted

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

W = []

def dfs(checked, L):
    if len(L) == N:
        W.append(L)
        return
    for i in range(1, N+1):
        if not checked[i]:
            checked[i] = True
            dfs(checked, L+[i])
            checked[i] = False
    return

dfs([False]*(N+1), [])


for i, L in enumerate(W):
    if L == P:
        ip = i
    if L == Q:
        iq = i

print(abs(ip-iq))

Submission status: Accepted

Solution code:
import math

n=int(input())
plist = list(map(int, input().split()))
qlist = list(map(int, input().split()))

klist=[]
klist2=[]
for i in range(n):
    klist.append(i+1)
    klist2.append(i+1)

answer1=[]
answer2=[]


for j in range(n):
    numb=klist.index(plist[j])
    answer1.append(numb)
    klist=klist[:numb]+klist[numb+1:]

for j in range(n):
    numb=klist2.index(qlist[j])
    answer2.append(numb)
    klist2=klist2[:numb]+klist2[numb+1:]

#print(answer1)
#print(answer2)

answer3=0
answer4=0

for i in range(n):
    answer3+=math.factorial(n-i-1)*answer1[i]
    answer4+=math.factorial(n-i-1)*answer2[i]

print(abs(answer3-answer4))

Submission status: Accepted

Solution code:
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()])

perms = sorted([p for p in permutations([i+1 for i in range(n)],n)])
print(abs(perms.index(P)-(perms.index(Q))))

Submission status: Accepted

Solution code:
import itertools

n = int(input().strip())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
p_cnt = 0
q_cnt = 0
for i, e in enumerate(sorted(list(itertools.permutations(p)))):
    if p == e:
        p_cnt = i
    if q == e:
        q_cnt = i
print(abs(p_cnt - q_cnt))

Submission status: Accepted

Solution code:
def main():
    import sys
    import itertools
    N=int(sys.stdin.readline())
    PL=list(map(int,sys.stdin.readline().split()))
    QL=list(map(int,sys.stdin.readline().split()))

    for i,L in enumerate(itertools.permutations(range(1,N+1))):
        if tuple(L)==tuple(PL):
            a=i
        if tuple(L)==tuple(QL):
            b=i

    print(abs(a-b))
main()

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
*P, = map(int, input().split())
*Q, = map(int, input().split())
P = tuple(P); Q = tuple(Q)
for i, t in enumerate(permutations(range(1, N+1))):
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

l = list(range(1, n+1))
l_ = list(permutations(l))

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
ps = tuple(map(int, input().split()))
qs = tuple(map(int, input().split()))

perm = []
for i in range(1, n + 1):
    perm.append(i)

for i, x in enumerate(itertools.permutations(perm)):
    if x == ps:
        a = i
    if x == qs:
        b = i

print(abs(a - b))

Submission status: Accepted

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

import itertools

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

T = [list(i) for i in list(itertools.permutations(A, N))]
# print(T)
n1 = T.index(P)
n2 = T.index(Q)
print(abs(n1-n2))

Submission status: Accepted

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

def all_patterns(n, pattern, patterns):
	if len(pattern) == n:
		patterns.append(pattern)
	else:
		for i in range(1, n + 1):
			if not i in pattern:
				all_patterns(n, pattern + [i], patterns)

patterns = []
all_patterns(N, [], patterns)

print(abs(patterns.index(P_list) - patterns.index(Q_list)))