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

Solution (Python):
N=int(input())
L=[[int(x) for x in input().split()] for i in range(2)]
t=[0]*2

n=1
for i in range(2,N+1):
  n*=i

for i in range(2):
  o=n
  D=[x for x in range(1,N+1)]
  for j in range(N):
    o//=len(D)
    t[i]+=o*D.index(L[i][j])
    D.remove(L[i][j])

print(abs(t[1]-t[0]))

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

def make_val(suretsu):
    val = 0
    for index in range(n):
        val += suretsu[n - index - 1] * pow(10, index)
    return val

p_val = make_val(p)
q_val = make_val(q)
min_val = min(p_val, q_val)
max_val = max(p_val, q_val)

count = 0
counting = False
p_rank = 0
q_rank = 0
for suretsu in itertools.permutations(range(1, n+1)):
    val = make_val(suretsu)
    if val < p_val:
        p_rank += 1
    if val < q_val:
        q_rank += 1
    p_rank += 1
    q_rank += 1

print(abs(p_rank-q_rank))

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

Solution (Python):
import math
import itertools
n=int(input())

a=list(map(str,input().split()))
b=list(map(str,input().split()))

a_str = [''.join(v) for v in itertools.permutations(a)]
a_int = [int(i) for i in a_str]
a_int.sort()

x="".join(a)
y="".join(b)

p=a_int.index(int(x))+1
q=a_int.index(int(y))+1

print(abs(p-q))

Solution (Python):
import itertools


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




so = [x + 1 for x in range(N)]



a = 0
b = 0
count = 1
for v in itertools.permutations(so, N):
    if v == P:
        a = count

    if v == Q:
        b = count
    
    count = count + 1

print(abs(a - b))

Solution (Python):
import itertools
n=int(input())
p,q=[tuple(map(int,input().split())) for i in (1,1)]
j=itertools.permutations
l=list(j(range(1,n+1)))

for i,s in enumerate(l):
  if s==p:a=i
  if s==q:b=i
print(abs(a-b))

Solution (Python):
import sys

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

Solution (Python):
from itertools import permutations
n=int(input())
p=[]
for i in range(1,n+1):
    p.append(i)
a=list(map(int,input().split()))
b=list(map(int,input().split()))
perm=list(permutations(p))
for i in perm:
    if a==list(i):
        res=perm.index(i)
    if b==list(i):
        temp=perm.index(i)
print(abs(res-temp))

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



from itertools import permutations

N = I()
P = tuple(III())
Q = tuple(III())

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

for i in range(len(A)):
    if A[i]==P:
        p = i
    if A[i]==Q:
        q = i

print(abs(p-q))

Solution (Python):
import itertools

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

permutations = list(itertools.permutations(range(1, N + 1)))
a = permutations.index(P)
b = permutations.index(Q)

print(abs(a - b))

Solution (Python):
from itertools import permutations

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

P = []
for perm in permutations(range(1, N + 1), r=N):
    P.append(tuple(perm))

P.sort()
print(abs(P.index(A) - P.index(B)))

Solution (Python):
import itertools

N = int(input())

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

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

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

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
for i, x in enumerate(itertools.permutations(range(1, N+1))):
    if x == P:
        p_idx = i
    if x == Q:
        q_idx = i
print(abs(p_idx - q_idx))

Solution (Python):
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
pp = sorted(p)
qq = sorted(q)

x = 0
y = 0
l = 1
m = 1
for i in p[:n-1]:
    x += math.factorial(n-l)*pp.index(i)
    l += 1
    pp.remove(i)
    del p[0]
    
for j in q[:n]:
    y += math.factorial(n-m)*qq.index(j)
    m += 1
    qq.remove(j)
    del q[0]

print(abs((x+1)-(y+1)))

Solution (Python):

from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = list(range(1,N+1))
count = 0
ans_p = 0
ans_q = 0
for i in permutations(l):
    count += 1


    if i == P:
        ans_p = count
    else:
        pass
    if i == Q:
        ans_q = count
    else:
        pass

print(abs(ans_p - ans_q))

Solution (Python):
def main():
    from itertools import permutations
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))

    a = 0
    b = 0
    for i, l in enumerate(permutations(list(range(1,n+1)))):
        if p == l:
            a = i
        if q == l:
            b = i
        if a != 0 and b!= 0:
            break
    print(abs(a-b))
if __name__ == '__main__':
    main()

Solution (Python):
import math

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


def Order(n, s):
    cnt = 1
    num_list = []
    for i in range(n):
        num_list.append(i + 1)
    for i in range(n):
        index = num_list.index(s[i])
        cnt += index * math.factorial(n - i - 1)
        del num_list[index]

    return cnt


ans = Order(N, P) - Order(N, Q)
if ans < 0:
    ans *= -1
print(ans)

Solution (Python):


import itertools

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

perm = list(itertools.permutations(P))
perm.sort()

a = perm.index(tuple(P))
b = perm.index(tuple(Q))
print(abs(a - b))

Solution (Python):
import itertools
def main():
    n = int(input())
    p = tuple(map(int,input().split(' ')))
    q = tuple(map(int,input().split(' ')))
    for i,seq in enumerate(itertools.permutations(range(1,n+1))):
        if p==seq:
            a = i
        if q==seq:
            b = i
    print(abs(a-b))
    return 


if __name__ == "__main__":
    main()

Solution (Python):

from itertools import permutations
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
res = list(range(1, n + 1))
cnt = 0
for i in permutations(res):
    cnt += 1
    if i == p:
        p_tmp = cnt
    if i == q:
        q_tmp = cnt

print(abs(p_tmp - q_tmp))

Solution (Python):
n = int(input())
tp = tuple(map(int, input().split()))
tq = tuple(map(int, input().split()))

import itertools as it

l = list(it.permutations(range(1, n+1)))

print(abs(l.index(tp) - l.index(tq)))

Solution (Python):
import itertools
N=int(input())
P=tuple([int(i)-1 for i in input().split()])
Q=tuple([int(i)-1 for i in input().split()])
SEQ=sorted(list(itertools.permutations(range(N),N)))


a=-1
b=-1
for i in range(len(SEQ)):
    if SEQ[i]==P:
        a=i
    if SEQ[i]==Q:
        b=i
print(abs(a-b))

Solution (Python):
import itertools

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

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


for i, node in enumerate(all_pattern):
  
  if node == P:
    p = i
  if node ==  Q:
    q = i
    
    
print(abs(p-q))

Solution (Python):
N=int(input())
P=list(map(int,input().split()))
ps=""
for i in range(N):
  ps+=str(P[i])
Q=list(map(int,input().split()))
qs=""
for i in range(N):
  qs+=str(Q[i])

stack=[]
stack.append("")
res=[]
while stack:
  d=stack.pop()
  if len(d)==N:
    res.append(d)
    continue
  for i in range(1,N+1):
    if str(i) not in d:
      stack.append(d+str(i))
  
res=res[::-1]
a=0
b=0
for i in range(len(res)):
  if res[i]==ps:
    a=i+1
  if res[i]==qs:
    b=i+1

print(abs(a-b))

Solution (Python):
import itertools
n = int(input())
string = str(1)
for i in range(2, n + 1):
    string += str(i)

    
string_list = []    
a = tuple(input().split(" "))
b = tuple(input().split(" "))
for i in itertools.permutations(string):
    string_list.append(i)


for i,j in enumerate(string_list):
    if j == a:
        A = i + 1
    if j == b:
        B = i + 1
print(abs(A - B))

Solution (Python):

from itertools import permutations
n=int(input())
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]

def f(lis):
  cnt=0
  for i in permutations(range(1,n+1)):
    cnt+=1
    if i == tuple(lis):
      return cnt

print(abs(f(p)-f(q)))

Solution (Python):
from itertools import permutations as perm
import sys
readline = sys.stdin.buffer.readline

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

p_lst = list(perm(list(range(1, n + 1)), n))  
pn = qn = 0
for i,j in enumerate(p_lst, 1):
  if p == j:
    pn = i
  if q == j:
    qn = i
  if pn * qn != 0:
    break
print(abs(pn - qn))

Solution (Python):
import itertools

N = int(input())
T = list(range(1,N+1))
S = list(itertools.permutations(T, N))
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

A = S.index(tuple(P))
B = S.index(tuple(Q))
print(abs(A-B))

Solution (Python):
def factorial(x):
    if x == 0:
        return 1
    else:
        return factorial(x-1)*x

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
"""
2 3 1 4 5
1 2 5 4 3
Go from the lower to higher permutation
if it is starting from k then ord(p)
2 3 4 1 5
2 3 4 5 1
How to find the order of a permutation
new_lowest = 1
3
1 2 3 -- has to give me 1
1 3 2 -- has to give me 2
2 1 3 -- has to give me 3
2 3 1 -- has to give me 4
3 1 2 -- has to give me 5
3 2 1 -- has to give me 6
"""
total = n
order_p = 1
original = []
for i in range(1,n+1):
    original.append(i)
for term in p:
    order_p+=factorial(total-1)*(original.index(term))
    original.remove(term)
    total-=1

for i in range(1,n+1):
    original.append(i)
order_q=1
total=n
for term in q:
    order_q+=factorial(total-1)*(original.index(term))
    original.remove(term)
    total-=1
print(abs(order_p-order_q))

Solution (Python):
fact = [1]
for i in range(20):
    fact.append(fact[-1] * (i+1))

def calc(L):
    NN = 17
    BIT=[0]*(2**NN+1)
    def addbit(i):
        while i <= 2**NN:
            BIT[i] += 1
            i += i & (-i)
    def getsum(i):
        ret = 0
        while i != 0:
            ret += BIT[i]
            i -= i&(-i)
        return ret
    re = 0
    for i, a in enumerate(L[::-1]):
        re += getsum(a) * fact[i]
        addbit(a)
    return re

N = int(input())
P = [int(a) for a in input().split()]
Q = [int(a) for a in input().split()]
 
print(abs(calc(P) - calc(Q)))

Solution (Python):
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)
ans=[]
for j in itertools.permutations(a):
    ans.append(list(j))
print(abs(ans.index(P)-ans.index(Q)))

Solution (Python):
import itertools

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

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


for i, node in enumerate(all_pattern):
  if node == P:
    p = i
  if node ==  Q:
    q = i
    
    
print(abs(p-q))

Solution (Python):
from itertools import permutations

N=int(input())

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

Ps="".join(P)

Qs="".join(Q)

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

Per=list(permutations(li,N))
idxP=0
idxQ=0
for idx,x in enumerate(Per):
    temp="".join(x)
    if Ps==temp:
        idxP=idx+1
    if Qs==temp:
        idxQ=idx+1
    if  (idxP!=0 and idxQ!=0):
        break

print(abs(idxP-idxQ))

Solution (Python):
import math
from itertools import permutations
def generate_permutations(n):
    perms = permutations(range(1, n+1), n)
    return [perm for perm in perms]
 
n = int(input())
ptuple = tuple(map(int, input().split()))
qtuple = tuple(map(int, input().split()))
 
perms = generate_permutations(n)
a = perms.index(ptuple)
b = perms.index(qtuple)
print(int(math.fabs(a - b)))

Solution (Python):
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
cp = 1
lp = n
for i in range(n):
  pi = p.pop(0)
  lp -= 1
  cp += (pi-1)*math.factorial(lp)
  for j in range(lp):
    if p[j] > pi:
      p[j] -= 1
cq = 1
lq = n
for i in range(n):
  qi = q.pop(0)
  lq -= 1
  cq += (qi-1)*math.factorial(lq)
  for j in range(lq):
    if q[j] > qi:
      q[j] -= 1
print(abs(cp-cq))

Solution (Python):
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
perm = itertools.permutations(range(1, N+1), N)

cnt = 0
flg = 0
for p in perm:
  cnt = cnt + 1
  if p == P:
    cnt_P = cnt
    flg = flg + 1
  if p == Q:
    cnt_Q = cnt
    flg = flg + 1
  if flg == 2:
    break
print(abs(cnt_P - cnt_Q))

Solution (Python):
import itertools

N = int(input())
lst = [i for i in range(1, N  + 1)]

per = list(itertools.permutations(lst))


a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))


s = per.index(a)
t = per.index(b)

ans = abs(s - t)
print(ans)

Solution (Python):
from itertools import permutations

N = int(input())
Ps = list(map(int, input().split()))
Qs = list(map(int, input().split()))

for i, Rs in enumerate(permutations(range(1, N+1)), 1):
    Rs = list(Rs)
    if Ps == Rs:
        a = i
    if Qs == Rs:
        b = i

print(abs(a-b))

Solution (Python):
import itertools

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

o_p = 0
o_q = 0
count = 0
for i in itertools.permutations(range(1, n + 1)):
    count += 1
    if p == i:
        o_p = count
    if q == i:
        o_q = count
print(abs(o_p - o_q))

Solution (Python):
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 = 0
b = 0
cnt = 0
st = 0
for i in permutations(range(1,n+1)):
    cnt += 1
    if i == p:
        a = cnt
        st += 1
    if i == q:
        b = cnt
        st += 1
    if st == 2:
        break
    
print(abs(a-b))

Solution (Python):
from itertools import permutations 

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

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

def kai(n):
  x = 1
  i = 1
  while i <= n:
    x *= i
    i += 1
  return x



def number(X):
  N = len(X)
  s = 1
  num_list = [1] * N
  for i in range(N):
    x = X[i]
    sub = sum(num_list[:x - 1])
    s += sub * kai(N - i - 1)  
    num_list[x - 1] = 0
  return s


print(abs(number(P) - number(Q)))

Solution (Python):

from itertools import permutations

n = int(input())
p = "".join(input().split())
q = "".join(input().split())

l = []
for s in permutations(range(1,n+1)):
    l.append("".join(map(str,s)))

l = sorted(l)

a = 0
b = 0
for i in range(len(l)):
    if l[i]==p:
        a = i+1
    if l[i]==q:
        b = i+1

print(abs(a-b))

Solution (Python):
from itertools import permutations

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

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


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

Solution (Python):
import math

N = int(input())
P = input().split()
Q = input().split()
X = [i+1 for i in range(N)]
Y = [i+1 for i in range(N)]

n = 1
m = 1
for i in range(N):
    for j in range(N-i):
        if int(P[i]) == X[j]:
            l = j
        if int(Q[i]) == Y[j]:
            k = j
    X.remove(int(P[i]))
    Y.remove(int(Q[i]))
    n += l*math.factorial(N-i-1)
    m += k*math.factorial(N-i-1)
    
print(abs(m-n))

Solution (Python):
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

seq =(i for i in range(1,n+1))
l = list(itertools.permutations(seq))
print(abs(l.index(p)-l.index(q)))

Solution (Python):
import math

N = int(input())
P = input().split()
Q = input().split()
X = [i+1 for i in range(N)]
Y = [i+1 for i in range(N)]

n = 1
for i in range(N):
    for j in range(N-i):
        if int(P[i]) == X[j]:
                l = j
    X.remove(int(P[i]))
    n += l*math.factorial(N-i-1)
m = 1
for i in range(N):
    for j in range(N-i):
        if int(Q[i]) == Y[j]:
                l = j
    Y.remove(int(Q[i]))
    m += l*math.factorial(N-i-1)
print(abs(m-n))

Solution (Python):
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
    
print(abs(pindex-qindex))

Solution (Python):
import itertools
import numpy as np

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
N_list = list(range(1, N+1, 1))
N_dictionary = list(itertools.permutations(N_list))
N_dic = [list(n) for n in N_dictionary]
a = N_dic.index(P)
b = N_dic.index(Q)
ans = np.abs(a - b)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


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

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


if __name__ == '__main__':
    main()

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

seq = tuple(range(1,N+1))
import itertools
p_list = list(itertools.permutations(seq))
for i,p in enumerate(p_list):
  if p == P:
    p_idx = i
  if p == Q:
    q_idx = i
print(abs(p_idx-q_idx))

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

ans = abs(permutations.index(p) - permutations.index(q))
print(ans)

Solution (Python):
import itertools as it

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

l=[n for n in range(1,N+1)]
p=list(it.permutations(l))
a=p.index(P);b=p.index(Q)
ans=abs(b-a)

print(ans)

Solution (Python):
import itertools as it

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

l=[n for n in range(1,N+1)]
for i,t in enumerate(it.permutations(l)):
    if t==P:
        a=i+1
    if t==Q:
        b=i+1
ans=abs(b-a)

print(ans)

Solution (Python):
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

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


def f(val):
    for i, x in enumerate(N):
        if x == val:
            return i+1

print(abs(f(p) - f(q)))

Solution (Python):
def main():
    from itertools import permutations
    N = int(input())
    P = tuple([i for i in input().split()])
    Q = tuple([i for i in input().split()])

    if P == Q:
        print(0)
        return

    if ''.join(P) > ''.join(Q):
        T = P[::]
        P = Q
        Q = T

    per = ''.join([str(i) for i in range(1, N + 1)])
    cnt = 0
    p_found = False
    for S in permutations(per):
        if not p_found:
            if S == P:
                p_found = True
        else:
            cnt += 1
            if S == Q:
                break

    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations

n=int(input())
li = list(range(1,n+1))
lis = list(permutations(li))

def solve(x):
	for i in range(len(lis)):
		if list(lis[i]) == x:
			return i

p = list(map(int,input().split()))
q = list(map(int,input().split()))
print(abs(solve(p) - solve(q)))

Solution (Python):
import itertools

N = int(input())

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

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

mp = {}

for x in itertools.permutations(A):
    mp[x] = len(mp)

print(abs(mp[P]-mp[Q]))

Solution (Python):
import itertools

n = int(input())
pl = list(map(int, input().split()))
ql = list(map(int, input().split()))

count = 0
a, b = 0, 0

for v in itertools.permutations(range(1, n+1)):
    count += 1
    if pl == list(v):
        a = count
    if ql == list(v):
        b = count

print(abs(a-b))

Solution (Python):
import itertools

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

a = 0
b = 0
i = 1

for x in itertools.permutations(list(range(1,N+1))):
    l = list(x)
    if P == l:
        a = i
    if Q == l:
        b = i
    i += 1

print(abs(a - b))

Solution (Python):
import itertools as it
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
d = []
ans = []
for i in range(1,n+1):
    d.append(i)
for i in it.permutations(d,n):
    ans.append(list(i))
print(abs(ans.index(p)-ans.index(q)))

Solution (Python):
N=int(input())
p=(int(x) for x in input().split())
q=(int(x) for x in input().split())
import itertools
seq = [(i +1) for i in range(N)]
h=list(itertools.permutations(seq))
print(abs(h.index(tuple(p)) - h.index(tuple(q))))

Solution (Python):
import sys
input = sys.stdin.readline
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
nums = []
for i in range(1, N + 1):
    nums.append(i)
Per = list(itertools.permutations(nums, N))
a = Per.index(P)
b = Per.index(Q)
print(abs(a - b))

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

shff = list(range(1, N + 1))
idx1 = -1
idx2 = -1
for i, perm in enumerate(permutations(shff)):
    if perm == P:
        idx1 = i
    if perm == Q:
        idx2 = i
print(abs(idx1 - idx2))

Solution (Python):
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
cnt = 0
cnt1 = 0
cnt2 = 0
a = []
for i in range(1, n+1):
    a.append(i)

for j in itertools.permutations(a):
    cnt += 1
    jk = list(j)
    if jk == p:
        cnt1 = cnt
    if jk == q:
        cnt2 = cnt
print(abs(cnt1 - cnt2))

Solution (Python):
import itertools
n = int(input())
p = "".join([i for i in input().split(" ")])
q = "".join([i for i in input().split(" ")])
yes = "".join([str(i) for i in range(1, n+1)])
perms = sorted(list(set(["".join(i) for i in list(itertools.permutations(yes))])))
a = perms.index(p)
b = perms.index(q)
print(abs(a-b))

Solution (Python):
import sys
from itertools import permutations
readline = sys.stdin.readline

N = int(readline())

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

cnt = 0
for k in permutations(list(range(1, N+1))):
    cnt += 1
    if P == k:
        p = cnt
    if Q == k:
        q = cnt
print(abs(p-q))

Solution (Python):
from itertools import permutations

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

arr = [p for p in permutations(range(1, n + 1), n)]
print(abs(arr.index(p) - arr.index(q)))

Solution (Python):
from math import factorial


def cntRank(S, n):
    used = set()
    cnt = 0

    for i, s in enumerate(S):
        s = int(s)
        j = 1
        while j < s:
            if j not in used:
                cnt += factorial(n - (i + 1))
            j += 1
        used.add(s)

    return cnt + 1


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

print(abs(cntRank(P, N) - cntRank(Q, N)))

Solution (Python):
import itertools

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

a = 0
b = 0

for i, j in enumerate(itertools.permutations(r)):
    if list(j) == p:
        a = i
    if list(j) == q:
        b = i

print(abs(b-a))

Solution (Python):
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
Ps = ''
Qs = ''
for i in P:
    Ps += str(i)
for i in Q:
    Qs += str(i)
seq = (str(i) for i in range(1,N+1))
per = itertools.permutations(seq)
a = 0
b = 0
for i,s in enumerate(per):
    str1 = ''
    for c in s:
        str1 += c
    if str1 == Ps:
        a = i + 1
    if str1 == Qs:
        b = i + 1

print(abs(a-b))

Solution (Python):
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)]
n_permutations = itertools.permutations(n_list)
n_permu_list = list(map(list,n_permutations))
print(abs(n_permu_list.index(p)-n_permu_list.index(q)))

Solution (Python):
from itertools import permutations
n = int(input())
f = list(map(int, input().split()))
e = list(map(int, input().split()))
a = sorted(f)
first = 0
end = 0
for i, l in enumerate(permutations(a)):
    if list(l) == f:
        first = i
    if list(l) == e:
        end = i

print(abs(end-first))

Solution (Python):
import itertools
n = int(input())
lis = list(map(int,input().split()))
li = list(map(int,input().split()))
su = ""
for i in range(n):
    su += str(1+i)
cnt = 1
for x in itertools.permutations([i for i in range(1,n+1)]):
    if list(x) == lis:
        a1 = cnt
    if list(x) == li:
        a2 = cnt
    cnt += 1
print(abs(a2-a1))

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
class BIT(object):
    def __init__(self,A,dot,e,inv=None):
        n=len(A)
        self.__n=n
        self.__dot=dot
        self.__e=e
        self.__inv=inv
        self.__node=['$']+A 
        for i in range(1,n+1):
            j=i+(i&-i)
            if(j<=n): self.__node[j]=dot(self.__node[i],self.__node[j])

    def add(self,i,w=1):
        i+=1
        while(i<=self.__n):
            self.__node[i]=self.__dot(self.__node[i],w)
            i+=i&-i

    def sum(self,i):
        i+=1
        res=self.__e
        while(i>0):
            res=self.__dot(res,self.__node[i])
            i-=i&-i
        return res

from operator import add
def resolve():
    n=int(input())
    A=list(map(lambda x:int(x)-1,input().split()))
    A.reverse()

    bit=BIT([0]*n,add,0)
    fact=1
    ans=0
    for i in range(n):
        a=A[i]
        s=bit.sum(a)
        ans+=fact*s
        bit.add(a)
        fact*=i+1
    p=ans

    A=list(map(lambda x:int(x)-1,input().split()))
    A.reverse()


    bit=BIT([0]*n,add,0)
    fact=1

    ans=0
    for i in range(n):
        a=A[i]
        s=bit.sum(a)
        ans+=fact*s
        bit.add(a)
        fact*=i+1
    q=ans

    print(abs(p-q))
resolve()

Solution (Python):
fact = [1]
for i in range(20):
    fact.append(fact[-1] * (i+1))

def calc(L):
    n = len(L)
    if n == 1: return 0
    a =  len([l for l in L if l < L[0]])
    return a * fact[n-1] + calc(L[1:])

N = int(input())
P = [int(a)-1 for a in input().split()]
Q = [int(a)-1 for a in input().split()]

print(abs(calc(P) - calc(Q)))

Solution (Python):


import sys
from itertools import permutations

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
P = tuple(LIST())
Q = tuple(LIST())

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

Solution (Python):
import sys
from itertools import permutations

n = int(sys.stdin.readline().rstrip())
p, q = [tuple(map(int, sys.stdin.readline().split())) for _ in range(2)]

def main():
    perms = list(permutations(range(1, n+1)))
    return abs(perms.index(p) - perms.index(q))

if __name__ == '__main__':
    ans = main()
    print(ans)

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

from itertools import permutations

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

M = tuple(permutations(Z,N))

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

print(abs(a-b))

Solution (Python):
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
def ans(r):
  c = 0
  nn = list(range(1,n+1))
  for i in range(n):
    c += (nn.index(r[i])-1) * math.factorial(n-i-1)
    nn.remove(r[i])
  return c
print(abs(ans(p)-ans(q)))

Solution (Python):
import itertools
N = int(input())
P = [int(a)-1 for a in input().split()]
Q = [int(a)-1 for a in input().split()]
i = 0
ans = 0
for a in itertools.permutations(range(N), N):
    a = [b for b in a]
    if a == P:
        ans -= i
    if a == Q:
        ans += i
    i += 1

print(abs(ans))

Solution (Python):
import itertools

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

l = "".join([str(i+1) for i in range(n)])

cnt = 0
a,b = 0,0
for x in itertools.permutations(l):
	y = "".join(x)
	cnt += 1
	if y == p:
		a = cnt
	if y == q:
		b = cnt

	if a != 0 and b != 0:
		break

print(abs(a-b))