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



N=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))
before=0
after=0
count=0

T=[]
for i in range(1,N+1):
    T.append(i)


D=list(itertools.permutations(T))
for Z in D:
    count+=1
    if A==Z:
        
        before=count
    if B==Z:
       
        after=count
    
print(abs(before-after))

Submission status: Accepted

Solution code:
import itertools
 
N = int(input())
p_list = tuple(map(int, input().split()))
q_list = tuple(map(int, input().split()))
 
permu_list = list(itertools.permutations(range(1,N+1)))
p_index = permu_list.index(p_list)
q_index = permu_list.index(q_list)
ans = abs(p_index - q_index)
print(ans)

Submission status: Accepted

Solution code:
def abc150_c_count_order_2():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    factorial = [1]
    for i in range(1, N+1):
        factorial.append(factorial[i-1] * i)

    def find_index(permutation, lst):
        indices = []
        for p in permutation:
            i = lst.index(p)
            indices.append(i)
            lst.pop(i)
        # print(indices)
        idx = 0
        for i in range(N):
            idx += factorial[N - i - 1] * (indices[i])
        # print(idx)
        return idx

    a = find_index(P, list(range(1, N+1)))
    b = find_index(Q, list(range(1, N+1)))
    print(abs(a-b))


##########
if __name__ == "__main__":
    abc150_c_count_order_2()

Submission status: Accepted

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

    def solve(items, current):
        if items == []:
            permutations.append(current)
            return
        for item in items:
            next = current + [item]
            new_items = [i for i in items if i != item]
            solve(new_items, next)

    permutations = []
    items = list(range(1, N+1))
    current = []
    solve(items, current)

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


##########
if __name__ == "__main__":
    abc150_c_count_order()

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))

def number(A):
  s=[0]*N
  s[0]=1
  for n in range(1,N):
    s[n]=s[n-1]*(n)
  s[0]=0
  s.reverse()
  t=[i for i in range(N+1)]
  count=0
  for i in range(N):
    count+=s[i]*(t.index(A[i])-1)
    t.pop(t.index(A[i]))
  return count
print(abs(number(q)-number(p)))

Submission status: Accepted

Solution code:
import itertools as it
N = int(input())
Q = []
P = []
Q = list(map(int, input().split()))
P = list(map(int, input().split()))
Jun = []
box = [i+1 for i in range(N)]

for num in it.permutations(Q):
    num = list(num)
    Jun.append(num)
Jun = sorted(Jun)
for i, num in enumerate(Jun):
    if Q == num:
        a = i
    if P == num:
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    import itertools
    all = list(itertools.permutations(range(1, N + 1)))
    ans = all.index(P) - all.index(Q)
    print(abs(ans))

    return
resolve()

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 = 0
q = 0
i = 0
for n in permutations(range(1,N+1)):
    if n == P:
        p = i
    if n == Q:
        q = i
    i += 1
 
print(abs(p-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()))

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

print(abs(p.index(P)-p.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()))

if p == q:
    print(0)
else:
    cnt = 1
    a,b = 0,0
    for i in permutations(list(range(1,n+1))):
        if i == p:
            a = cnt
        elif i == q:
            b = cnt
        else:
            pass
    
        cnt += 1
    
    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()))

a = 0
b = 0
ans = 0

for i in permutations(range(1, N + 1)):
    a += 1
    b += 1

    if i == P:
        ans = abs(ans - a)

    if i == Q:
        ans = abs(ans - b)

print(ans)

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(input().split())
Q = list(input().split())
p = itertools.permutations(P)

P = int(''.join(P))
Q = int(''.join(Q))

a = 0
b = 0
for v in p:
    if P >= int(''.join(v)): a += 1
    if Q >= int(''.join(v)): b += 1
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()))

perms = list(permutations([i for i in range(1, N+1)]))

a = perms.index(P) + 1
b = perms.index(Q) + 1

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

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

permu_list = list(itertools.permutations(range(1,N+1)))
p_index = permu_list.index(p_list)
q_index = permu_list.index(q_list)
ans = abs(p_index - q_index)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fac = [0] * 8
# 1番目の桁
fac[1] = 1
for i in range(2, N):
    # 最上位の桁が7の場合7!の6周目終わりが30240番目が先頭になる。
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))
# l=3の時fac[2] 2!の2周目終わった時点え次のスタートが5になる
# 5の1番目のルートとなる
for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[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()))
m = sorted(p)
p = tuple(p)
q = tuple(q)

import itertools
b = 0
c = 0
cnt = 1
for i in itertools.permutations(m,n):
    if i == p:
        c += cnt
    if i == q:
        b += cnt
    if c != 0 and b!=0:
        break
    else:
        cnt += 1
#print(c)
#print(b)
print(abs(c-b))

Submission status: Accepted

Solution code:
n = int(input())
p = input()
q = input()

import itertools
x = [str(i) for i in range(1,n+1)]

y = []
for i in itertools.permutations(x):
    y.append(''.join(i))

p = list(p)
q = list(q)
p = [a for a in p if a != ' ']
q = [a for a in q if a != ' ']
p = ''.join(p)
q = ''.join(q)
#print(p,q,y)
print(abs(y.index(p) - y.index(q)))

Submission status: Accepted

Solution code:
import itertools

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


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

x = [j for j in itertools.permutations(l)]

a = x.index(tuple(p))
b = x.index(tuple(q))

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

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

a = 0
b = 0
for i in range(len(perms)):
  if P == list(perms[i]):
    a = i+1
  if Q == list(perms[i]):
    b = i+1
  if a != 0 and b != 0:
    break


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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
# 多分1~4で3!の順列になる
ls = list(itertools.permutations(range(1,n+1))) #順列生成
# 絶対値absで辞書順のpからqをひく
print(abs(ls.index(p)-ls.index(q)))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
L = sorted(list(itertools.permutations(P)))

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

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
lis = [[] for _ in range(math.factorial(N))]
l = list(range(1,N+1))
lis = list(itertools.permutations(l,N))
a = lis.index(P)
b = lis.index(Q)
print(abs(a - b))

Submission status: Accepted

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

a=1
b=1

import math

def dfs(i):
    global a
    global b
    if i==n-1:
        return abs(a-b)

    else:
        N=math.factorial(n-i)

        p_list=p[:]
        q_list=q[:]
        del p_list[0:i]
        del q_list[0:i]
        p_list.sort()
        q_list.sort()
        for j in range(n-i):
            if p[i]==p_list[j]:
                a+=j*N/(n-i)
            if q[i]==q_list[j]:
                b+=j*(N/(n-i))
        dfs(i+1)
dfs(0)
print(abs(int(a-b)))

Submission status: Accepted

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

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

#print(d_list)

#print(type(d_list[0]))
Pindex = d_list.index(Ps)
Qindex = d_list.index(Qs)


print(abs(Pindex - Qindex))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p = list(map(int,input().split()))	
q = list(map(int,input().split()))
i=1
for lis in list(itertools.permutations(list(range(1,n+1)))):
  if list(lis)==p:
    pin=i
  if list(lis)==q:
    qin=i
  i+=1

print(abs(pin-qin))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
for i in range(N):
    P[i] -= 1
    Q[i] -= 1

R = list(itertools.permutations(range(N)))
length = len(R)
for i in range(length):
    r = list(R[i])
    if P == r:
        a = i
    if Q == r:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple([i-1 for i in map(int, input().split())])
q = tuple([i-1 for i in map(int, input().split())])

for i, item in enumerate(itertools.permutations(range(n))):
    if item == p:
        a = i
    if item == q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
num = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

lista=[]

for i in range(1,num+1): lista.append(i)

tudo=list(itertools.permutations(lista))

a,b,cont=0,0,0

for i in tudo:
    if list(i) == p: a=cont
    if list(i) == q: b=cont
    cont+=1
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
p1 = [int(x) for x in input().split()]
p2 = [int(x) for x in input().split()]
p1 = tuple(p1)
p2 = tuple(p2)
first = [x for x in range(1, n+1)]

for i, p in enumerate(permutations(first)):
    if p == p1:
        a = i
    if p == p2:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
# ABC150C

import itertools

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

l=[i+1 for i in range(N)]
cnt=0
Pno=0
Qno=0
for i in itertools.permutations(l):
    if i==Ps:
        Pno=cnt
    if i==Qs:
        Qno=cnt
    cnt+=1
    
print(abs(Qno-Pno))

Submission status: Accepted

Solution code:
import itertools, math

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

# main
R = list(itertools.permutations(P))
R.sort()
a = 0
b = 0

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

# output
print(abs(b-a))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
permutation = list(itertools.permutations(range(1, N + 1)))

a = permutation.index(P) + 1
b = permutation.index(Q) + 1
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
X=[str(i) for i in range(1,N+1)]
# X="1234"
s1="".join(input().split())
s2="".join(input().split())
perm=list(map("".join,itertools.permutations(X)))
print(abs(perm.index(s1)-perm.index(s2)))

Submission status: Accepted

Solution code:
import itertools

n = int(input())

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

lst = []

for v in itertools.permutations(map(str, range(1, n+1)), n):
    lst.append("".join(v))

lst.sort()

d = {lst[i]: i for i in range(len(lst))}

print(abs(d[p] - d[q]))

Submission status: Accepted

Solution code:
import itertools

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

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

a = arr1.index(p) + 1
b = arr1.index(q) + 1

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

Submission status: Accepted

Solution code:
import itertools
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

v = list(itertools.permutations(l))

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

Submission status: Accepted

Solution code:
import itertools

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

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

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())

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

for i in range(len(order)):
    if P == list(order[i]):
        P_num = i
    if Q == list(order[i]):
        Q_num = i
        
print(abs(P_num-Q_num))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
a = tuple(map(int ,input().split()))
b = tuple(map(int ,input().split()))

np = [i for i in range(1, n+1)]
p = list(permutations(np))
ai = p.index(a)
bi = p.index(b)
print(abs(ai-bi))

Submission status: Accepted

Solution code:
N=int(input())
*P,=map(int,input().split())
*Q,=map(int,input().split())
nl=[i for i in range(1,N+1)]

import itertools
per=list(itertools.permutations(nl,N))

PP=per.index(tuple(P))+1
QQ=per.index(tuple(Q))+1
print(abs(PP-QQ))

Submission status: Accepted

Solution code:
#ABC_150_C
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = [i for i in range(1,N+1)]
#itertoolsでLを並び替えたリストを作成する。
#ただし、中の各要素はタプルになっているところに注意する。
M = list(itertools.permutations(L))
#[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
#Pはリストなので、タプルに変換してから、indexを求める
p = M.index(tuple(P))
q = M.index(tuple(Q))
print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools

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

if P == Q:
    print(0)

else:
    M = [i for i in range(1, N+1)]
    X = list(itertools.permutations(M, N))

    for i in X:
        if list(i) == P:
            a = X.index(i) + 1
        if list(i) == Q:
            b = X.index(i) + 1

    print(abs(a - b))

Submission status: Accepted

Solution code:
#abc150 c
import itertools 
n = int(input())
lnp = tuple(map(int, input().split()))
lnq = tuple(map(int, input().split()))
ln = list(itertools.permutations(list(range(1,n+1))))
np, nq = (0,0)
for i in range(len(ln)) :
    if lnp == ln[i]:
        np = i
        
        break
for i in range(len(ln)) :
    if lnq == ln[i]:
        nq = i
        
        break
        
print(abs(np - nq))

Submission status: Accepted

Solution code:
n = int(input())
P = [0] + [int(i) for i in input().split()]
Q = [0] + [int(i) for i in input().split()]
L = [0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

Pth = 1
for i in range(1, n+1):
    cnt = 0
    for j in range(i+1, n+1):
        if P[i] > P[j]:
            cnt += 1
    Pth += cnt * L[n - i]
# print(Pth)

Qth = 1
for i in range(1, n+1):
    cnt = 0
    for j in range(i+1, n+1):
        if Q[i] > Q[j]:
            cnt += 1
    Qth += cnt * L[n - i]
# print(Qth)

print(abs(Pth-Qth))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = tuple(input().split())
Q = tuple(input().split())
P_num = -1
Q_num = -1

onetoN = ""
for i in range(1, N+1):
    onetoN += str(i)

permu_list = list(itertools.permutations(onetoN))

for i in range(len(permu_list)):
    if P == permu_list[i]:
        P_num = i+1
    if Q == permu_list[i]:
        Q_num = i+1

print(abs(Q_num-P_num))

Submission status: Accepted

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

import itertools
num = range(1, n + 1)
l = list(itertools.permutations(num, n))

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

Submission status: Accepted

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

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

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)))
a = l.index(p)
b = l.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()))
l = [i for i in range(1, n + 1)]
count = 0

if p == q:
    print(0)
    exit()
    
for i in itertools.permutations(l, n):
    count += 1
    if i == p:
        a = count
    elif i == q:
        b = count

print(max(a, b) - min(a, b))

Submission status: Accepted

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

# 全探索
import itertools
S = [str(i) for i in range(1,N+1)]
All = itertools.permutations(S) # これですべての要素を選択する場合の順列
I = []
for a in All:
    a = list(a) # tupleだから
    a = ''.join(a)
    I.append(a)
I.sort()
ind_p = I.index(''.join(P))
ind_q = I.index(''.join(Q))
print(abs(ind_p-ind_q))

Submission status: Accepted

Solution code:
from itertools import *

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

x=[i for i in range(1,N+1)]
X=list(permutations(x,N))
a,b=0,0

for index,i in enumerate(X):
    i=list(i)
    a= index+1 if i==P else a
    b= index+1 if i==Q else b
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
 
n = int(input())
lis = [x for x in range(1,n+1)] # 0からn-1までのリスト
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

A = map(str, P)
P = "".join(A)
B = map(str, Q)
Q = "".join(B)

permutations_lis = itertools.permutations(lis)# 全ての場合のリストを生成
# 以下出力
for i, one_case in enumerate(permutations_lis):
    C = map(str, one_case)
    R = "".join(C)
    if P == R:
        a = i + 1
    if Q == R:
        b = i + 1

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

Submission status: Accepted

Solution code:
import math
n=int(input())
def c():
    l=list(map(int, input().split()))
    s=0
    t=list(range(1,n+1))
    for i in range(n):
        s+=t.index(l[i])*math.factorial(n-i-1)
        t.remove(l[i])
    return s
print(abs(c()-c()))

Submission status: Accepted

Solution code:
import math
(n,),p,q=[list(map(int, i.split())) for i in open(0)]
def c(l):
    s=0
    t=list(range(1,n+1))
    for i in range(n):
        s+=t.index(l[i])*math.factorial(n-i-1)
        t.remove(l[i])
    return s
print(abs(c(p)-c(q)))

Submission status: Accepted

Solution code:
import itertools
(n,),p,q=[tuple(map(int, i.split())) for i in open(0)]
j=list(itertools.permutations(range(1, -~n)))
print(abs(j.index(p) - j.index(q)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from itertools import permutations
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
q = list(map(lambda x: x-1, q))
p = list(map(lambda x: x-1, p))
a = [list(i) for i in permutations(range(n))]
print(abs(a.index(p)-a.index(q)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):

    nums = [i for i in range(1, N + 1)]
    perm = list(itertools.permutations(nums, N))
    print(abs(perm.index(tuple(P)) - perm.index(tuple(Q))))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):

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

    p_at = 0
    for i, pr in enumerate(perm):
        if pr == tuple(P):
            p_at = i + 1
        if pr == tuple(Q):
            q_at = i + 1
    print(abs(p_at - q_at))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

S = [0]*N
S[0] = 1
for s in range(1,N-1):
    S[s] = S[s-1]*(s+1)

def where(r):
    R = len(r)
    L = [0]*N
    now = 0
    for i in range(R):
        now += (r[i]-1-sum(L[0:r[i]]))*S[N-i-2]
        L[r[i]-1] = 1
    return now

a = where(p)
b = where(q)

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

P = tuple(P)
Q = tuple(Q)

count = 0
pc = qc = -1
for per in itertools.permutations(range(1,N+1), N):
    if per == P:
        pc = count + 1
    if per == Q:
        qc = count + 1
    if pc != -1 and qc != -1:
        break
    count += 1

print(abs(pc - qc))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
ans = list(itertools.permutations(range(1, n+1)))

a = ans.index(p)
b = ans.index(q)
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
ans = list(itertools.permutations(range(1,n+1)))

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

a = ans.index(P)
b = ans.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()))
l=[i for i in range(1,N+1)]
a=list(itertools.permutations(l))
p=a.index(P)
q=a.index(Q)
print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools
def main():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    perm = list(itertools.permutations([i+1 for i in range(N)]))
    print(abs(perm.index(P)-perm.index(Q)))

main()

Submission status: Accepted

Solution code:

import itertools


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

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

    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
num = [x for x in range(1, n + 1)]
perm = itertools.permutations(num)
cnt = 0
a = 0
b = 0
for i in perm:
    i = list(i)
    cnt += 1
    if p == i:
        a = cnt
    if q == i:
        b = cnt
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()))

tmp = list(itertools.permutations(P,n))

tmp = sorted(tmp)


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

Submission status: Accepted

Solution code:
import itertools
xx = int(input())

N = []
for i in range(xx):
    N.append(i+1)
    
X = [tuple(map(int,input().split())) for i in range(2)]

l = list(itertools.permutations(N,xx))
print(abs((l.index(X[1])+1) - (l.index(X[0])+1)) )

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
s = list(itertools.permutations(range(1,n+1),n))
print(abs(s.index(p)-s.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations as permu
import numpy as np


def main():
    n = int(input())
    ps = np.array(list(map(int, input().split())))
    qs = np.array(list(map(int, input().split())))

    nums = [i for i in range(1, n+1)]
    nums_array = np.array(list(permu(nums, n)))
    a = np.where(np.all(nums_array == ps, axis=1))[0][0]
    b = np.where(np.all(nums_array == qs, axis=1))[0][0]
    ans = abs(a - b)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

l = list(int(i) for i in range(1,n+1))
test = []
cnt = 0
for i in itertools.permutations(l,n):
	i = list(i)
	cnt += 1
	if p == i:
		cntp = cnt
	if q == i:
		cntq = cnt

print(abs(cntp - cntq))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

l = list(int(i) for i in range(1,n+1))
test = []
cnt = 0
for i in itertools.permutations(l,n):
	i = list(i)
	cnt += 1
	if p == i:
		cntp = cnt
	if q == i:
		cntq = cnt

print(abs(cntp - cntq))

Submission status: Accepted

Solution code:
import itertools

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

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

a = 0
b = 0
i = 1

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

print(abs(a-b))

Submission status: Accepted

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

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

a = 0
b = 0
for i in range(len(ans)) :
  if ans[i] == p:
    a = i+1
  if ans[i]== q:
    b = i+1
      
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 = 0
b = 0

for i, x in enumerate(itertools.permutations(range(1, N+1))):
    if x == P:
        a = i
    if x == 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()))

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

print (abs(a-b))

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def SS(): return map(str,sys.stdin.readline().rstrip().split())
def II(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N=I()
P=LS()
Q=LS()

l = ["".join([str(n) for n in list(i)]) for i in itertools.permutations(range(1,N+1),N)]

ans = abs(l.index("".join(P)) - l.index("".join(Q)))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
P = int(''.join(input().split()))
Q = int(''.join(input().split()))

#dfsで探索
def dfs(s,tmp):
    #nより桁数大きいかP,Qよりも大きいときには打ち切り
    if len(s)>n+1 or int(s)>max(P,Q):
        return tmp
    #桁数がnの時に++1,P,Qと一致するかもチェック
    if len(s)==n+1:
        tmp+=1
        if int(s)==P:
            global a
            a = tmp
        if int(s)==Q:
            global b 
            b = tmp
        return tmp
    for c in [str(i) for i in range(1,n+1)]:
        if c not in s:
            tmp = dfs(s+c,tmp)
    return tmp

tmp = 0
a,b = 0,0
dfs('0',tmp)
print(abs(a-b))

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
P = int(''.join(input().split()))
Q = int(''.join(input().split()))

def dfs(s,ans):
    if len(s)>n+1 or int(s)>max(P,Q):
        return ans
    if len(s)==n+1:
        ans+=1
        if int(s)==P:
            global a
            a = ans
        if int(s)==Q:
            global b 
            b = ans
        return ans
    for c in [str(i) for i in range(1,n+1)]:
        if c not in s:
            ans = dfs(s+c,ans)
    return ans

ans = 0
a,b = 0,0
dfs('0',ans)
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 = []
for pp in permutations(range(1,n+1)):
	l.append(list(pp))

a = l.index(p)
b = l.index(q)
print(abs(a - b))