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 = list(map(int,input().split()))
b = list(map(int,input().split()))
data = list(range(1,n+1))
x = list(itertools.permutations(data, n))
c1 = 0
c2 = 0
for i in range(len(x)):
    if list(x[i]) == a:
        c1 = i
    if list(x[i]) == b:
        c2 = i
print(abs(c1-c2))

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools

#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
ans = 0
a = 0
b = 0
ac = 0
bc = 0
NL = [0]*N
for i in range(N):
    NL[i] = i+1

N_list = list(itertools.permutations(NL, N))

for i in range(math.factorial(N)):
    for j in range(N):
        if(N_list[i][j]==P[j]):
            ac += 1
        if(N_list[i][j]==Q[j]):
            bc += 1
    if(ac == N):
        a = i+1
    if(bc == N):
        b = i+1
    ac = bc = 0
#print(a,b)
ans = abs(a-b)


print(ans)

Submission status: Accepted

Solution code:
import itertools

def main():
    n = int(input())
    nums = []
    for i in range(2):
        nums.append(list(map(str, input().split())))
    kumi = list(itertools.permutations([str(i) for i in range(1, n+1)], n))
    f, t = 0, 0
    for i, s in enumerate(kumi):
        if ''.join(nums[0]) == ''.join(s):
            f = i
        if ''.join(nums[1]) == ''.join(s):
            t = i
    print(abs(f - t))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

fact = [1] *(N+1)
for i in range(1,N+1):
    fact[i] = fact[i-1] *i
a = 1
b = 1
P2 = sorted(P)
Q2 = sorted(Q)
for i,(p,q) in enumerate(zip(P,Q)):
    a += P2.index(p)*fact[N-i-1]
    b += Q2.index(q)*fact[N-i-1]
    P2.remove(p)
    Q2.remove(q)
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()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
a = 0
b = 0
s = 1
for i in ite:
    if i == p:
        a = s
    if i == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

Submission status: Accepted

Solution code:
# 12:55
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)
a = list(itertools.permutations(a))
b = 0
c = 0
for i, v in enumerate(a):
    if list(v) == P:
        b = i
    if list(v) == Q:
        c = i

print(abs(b-c))

Submission status: Accepted

Solution code:
import itertools
s = 1
a = 0
b = 0
N = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == p:
        a = s
    s += 1
s = 1
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

Submission status: Accepted

Solution code:
import itertools

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

all_comb_list = []

for i, comb in enumerate(itertools.permutations(range(1, N + 1))):
    comb = list(comb)
    if P_list == comb:
        P_index = i
    if Q_list == comb:
        Q_index = i

ans = abs(P_index - Q_index)

print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

p = permutations.index(P)
q = permutations.index(Q)

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()))
per = list(permutations(range(1, N+1)))

p = per.index(P)
q = per.index(Q)

print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools

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

l = sorted(map(tuple,list(itertools.permutations(p))))
count  = []

for num, i in enumerate(l):
  if(i == p or i == q):
    count.append(num + 1)
if(len(count) == 1):
  print(0)
else:
  print(abs(count[0] - count[1]))

Submission status: Accepted

Solution code:
import math
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
def num(R):
  _R = R
  ans = 0
  for i in range(N):
    if _R[i] != 1:
      ans += (_R[i]-1)*math.factorial(N-i-1)
    if i == N-1:
      ans += _R[-1]
    _R = [_R[j]-1 if R[j]>R[i] else _R[j] for j in range(N)]
    
  return ans
print(abs(num(P)-num(Q)))

Submission status: Accepted

Solution code:
import itertools

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

st = sorted(p)
x = list(itertools.permutations(st, n))

num1 = x.index(tuple(p))
num2 = x.index(tuple(q))
print(abs(num1 - num2))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
n = list(range(1,n+1))

flag = 0
for i, p in enumerate(itertools.permutations(n), start=1):
    if p == P:
        a = i
        flag += 1
    if p == Q:
        b = i
        flag += 1
    if flag == 2:
        break
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()))

r=list(permutations(range(1,n+1)))				#1~n
																					#permutationsの結果はタプルであらわれる
print(abs(r.index(p) - r.index(q)))

Submission status: Accepted

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

print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools

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

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

p_num = permutations.index(P) + 1
q_num = permutations.index(Q) + 1

print(abs(p_num - q_num))

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

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

Submission status: Accepted

Solution code:
import itertools
import math

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

nums = [i for i in range(1,n+1)]
lists = list(itertools.permutations(nums,n))

a = lists.index(p)
b = lists.index(q)

print(abs(a-b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import itertools

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

st = sorted(P)
x = list(itertools.permutations(st, N))
num_p = x.index(P)
num_q = x.index(Q)
print(abs(num_p - num_q))

Submission status: Accepted

Solution code:
import itertools
cnt = 0
n = int(input())
p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
    
combination = list(itertools.permutations(range(1,n+1)))
combination_list = [list(x) for x in combination]

p_i = combination_list.index(p_list)
q_i = combination_list.index(q_list)


print(abs(p_i - q_i))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
#print("P:\t" + str(P))
#print("Q:\t" + str(Q))

n = [int(x) for x in range(1,N+1)]
itr = list(itertools.permutations(n))
#print("itr" + str(itr))

for i in range(len(itr)):
#    print("======================================")
#    print("i:\t" + str(i))
#    print(itr[i])
    if (list(itr[i]) == P):
        a = i+1
    if (list(itr[i]) == Q):
        b = i+1

#print("a:\t" + str(a))
#print("b:\t" + str(b))

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

Submission status: Accepted

Solution code:
import itertools
def main():
    N = int(input())
    P = input()
    Q = input()
    per = [ "".join(map(str, p)) for p in itertools.permutations(range(1, N+1))]
    print(abs(per.index(P.replace(" ", "")) - per.index(Q.replace(" ", ""))))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import permutations as perms

n = int(input().strip())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
lis = [(i + 1) for i in range(n)]
a = -1
b = -1
cnt = 0
for elem in perms(lis, n):
    cnt += 1
    if elem == p:
        a = cnt
    if elem == q:
        b = cnt
    if a != -1 and b != -1:
        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()))

st = sorted(p)
x = list(itertools.permutations(st, n))
num1 = x.index(p)
num2 = x.index(q)
print(abs(num1 - num2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ma = lambda : map(int,input().split())
ni = lambda : int(input())
import itertools
n = ni()
l = [i+1 for i in range(n)]
P = list(ma())
Q = list(ma())
cp = 0
cq = 0
c = 0
for per in itertools.permutations(l):
    c+=1
    pf =True
    qf =True
    for i in range(n):
        if per[i] == P[i]:
            pass
        else:pf =False
        if per[i]==Q[i]:
            pass
        else:
            qf=False
    if pf:
        cp=c
    if qf:cq=c
print(abs(cp-cq))

Submission status: Accepted

Solution code:
import itertools

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

permutation_list = []

# nまでの順列要素のリスト作成
for i in range(1, n+1):
    permutation_list.append(int(i))

# 順列のペアを全て含んだリストの作成
# ただし各ペアはタプルであることに注意
permutations_pair = list(itertools.permutations(permutation_list))

for index, pair in enumerate(permutations_pair):
    # タプルのペアをリストに変換
    pair = list(pair)
    if pair == p:
        index_p = index
    # elifだと分岐になって、同じindexのときに定義されなくなるのでifにする
    if pair == q:
        index_q = index

print(abs(index_p - index_q))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

# N = int(input())
# N = int(input().split())
# N, K = [int(x) for x in input().split()]

from itertools import *

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

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

print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools

N=int(input())
l=[int(x) for x in range(1,N+1)]
P=[int(x) for x in input().rstrip().split()]
Q=[int(x) for x in input().rstrip().split()]

p=itertools.permutations(l,N)
full=[]
for v in p:
  v=list(v)
  full.append(v)

print(abs(full.index(P)-full.index(Q)))
# print(full)

Submission status: Accepted

Solution code:
from itertools import permutations, combinations,combinations_with_replacement,product
import sys
input = sys.stdin.readline

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

dic = list(permutations(p))
dic.sort()

tp = tuple(p)
tq = tuple(q)

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

Submission status: Accepted

Solution code:
import itertools

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

st = sorted(p)
x = list(itertools.permutations(st, n))

num1 = x.index(tuple(p))
num2 = x.index(tuple(q))
print(abs(num1 - num2))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
data = input().split()
p = [int(s) for s in data]
data = input().split()
q = [int(s) for s in data]
combination = list(itertools.permutations(range(1,n+1)))

combination_list = [list(x) for x in combination]

p_i = combination_list.index(p)
q_i = combination_list.index(q)

ans = abs(p_i - q_i)

print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
dum_list = [i for i in range(1,n+1)]
count = 0
ans = 0
for i in permutations(dum_list,n):
    count += 1
    if p_list == list(i):
        ans += count
    if q_list == list(i):
        ans -= count


print(abs(ans))

Submission status: Accepted

Solution code:
import math

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

List = [i for i in range(N)]

for i in range(N-1):
    if i == 0:
        countP += (P[i]-1) * math.factorial(N-1)
    elif i == 1:
        if P[0]<P[1]:
            countP += (P[i]-2) * math.factorial(N-2)
        else:
            countP += (P[i]-1) * math.factorial(N-2)
    else:
        count = 0
        for j in range(i):
            if P[i]>P[j]:
                count+=1
        countP += (P[i]-count-1) * math.factorial(N-1-i)

for i in range(N-1):
    if i == 0:
        countQ += (Q[i]-1) * math.factorial(N-1)
    elif i == 1:
        if Q[0]<Q[1]:
            countQ += (Q[i]-2) * math.factorial(N-2)
        else:
            countQ += (Q[i]-1) * math.factorial(N-2)
    else:
        count = 0
        for j in range(i):
            if Q[i]>Q[j]:
                count+=1
        countQ += (Q[i]-count-1) * math.factorial(N-1-i)


if countP > countQ:
    print(countP-countQ)
else:
    print(countQ-countP)

Submission status: Accepted

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

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

dict = (list(itertools.permutations(arr)))
print(abs(dict.index(a)-dict.index(b)))

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

perm = list(permutations(range(1, N + 1), N))
i = 0
a = 0; b = 0;
for i in range(len(perm)):
    if a > 0 and b > 0: break
    if perm[i] == P:  a = i + 1
    if perm[i] == Q:  b = i + 1

print(abs(b - a))

Submission status: Accepted

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

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

p_list = itertools.permutations(L)

a = -1
b = -1

for i, p in enumerate(p_list):
    if P == list(p):
        a = i
    if Q == list(p):
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
from collections import deque

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

st = deque([[i] for i in range(N, 0, -1)])
ord = 0
a = 0
b = 0
while(len(st) > 0):
    if a > 0 and b > 0: break
    now = st.pop()
    if len(now) == N:
        ord += 1
    if P == now:
        a = ord
    if Q == now:
        b = ord
    for i in range(N, 0, -1):
        if i in now: continue
        st.append(now + [i])

print(abs(b - a))

Submission status: Accepted

Solution code:
import itertools

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

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

cnt = 1
for i in itertools.permutations(tmp, N):
    if i == p_list:
        a = cnt

    if i == q_list:
        b = cnt
    cnt += 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()))

table = list(itertools.permutations([i for i in range(1, n+1)]))
a = table.index(p)
b = table.index(q)

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

Submission status: Accepted

Solution code:
import itertools
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
N=sorted(P)
c=0
a=0
b=0
for i in itertools.permutations(N):
    c+=1
    if i==P:
        a=c
    if i==Q:
        b=c
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

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

L_list = list(itertools.permutations(L))

a = L_list.index(P)
b = L_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()))
l = [i+1 for i in range(n)]
r = list(itertools.permutations(l))
j = 0
flagp = True
flagq = True
while flagp:
  if r[j] == p:
    x = j+1
    flagp = False
  j += 1
j = 0
while flagq:
  if r[j] == q:
    y = j+1
    flagq = False
  j += 1
print(abs(y-x))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p = list(map(str, input().split()))
q = list(map(str, input().split()))
s = "".join(map(str, range(1, (n+1))))
x = list(permutations(s))
x = [list(i) for i in x]
print(abs(x.index(p)-x.index(q)))

Submission status: Accepted

Solution code:
import itertools

n = int(input());
x = list(map(int, input().split()));
y = list(map(int, input().split()));

new = sorted(x);
kaijo = list(itertools.permutations(new, n));

x_num = kaijo.index(tuple(x))
y_num = kaijo.index(tuple(y))

print(abs(x_num - y_num));

Submission status: Accepted

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

import itertools as it
mylist = []
a = 0
for i in it.permutations(range(1,n+1)):
  a+=1
  mylist.append(i)

ans1 = mylist.index(p)
ans2 = mylist.index(q)
print(abs(ans1-ans2))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
Pl = [int(x) for x in input().split()]
Ql = [int(x) for x in input().split()]
P = tuple(Pl)
Q = tuple(Ql)
S = [x for x in range(1, N+1)]
a = 0
b = 0
Slist = list(itertools.permutations(S))
Slist.sort()
for i in range(len(Slist)):
  if P == Slist[i]:
    a = i
  if Q == Slist[i]:
    b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import permutations as p
N = int(input())
A = [i for i in range(1, N+1)]
P = tuple(list(map(int, input().split())))
Q = tuple(list(map(int, input().split())))
cntp = 1
cntq = 1
for i in sorted(p(A)):
    if i != P:
        cntp += 1
    if i == P:
        cntp += 1
        break
for i in sorted(p(A)):
    if i != Q:
        cntq += 1
    if i == Q:
        cntq += 1
        break
print(abs(cntp-cntq))

Submission status: Accepted

Solution code:
import itertools

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

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

number = 0
for pattern in patterns:
    number += 1
    if list(pattern) == A:
        tmp1 = number

    if list(pattern) == B:
        tmp2 = number

    
print(abs(tmp1 - tmp2))

Submission status: Accepted

Solution code:
import math
import itertools
num = int(input())
lis = [ i for i in range(1,num+1)]
k = tuple(map(int,input().split()))
t = tuple(map(int,input().split()))
count_k = 0
count_t = 0
count = 0
for i in itertools.permutations(lis):
    count+=1
    if i == k:
        count_k = count
    if i == t:
        count_t = count
    if count_k != 0 and count_t != 0:
        break
print(abs(count_k -count_t))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
l = [i for i in range(1, n + 1)]
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
cnt = 0
for check in itertools.permutations(l, n):
    cnt += 1
    if a == check:
        cnta = cnt
    if b == check:
        cntb = cnt

print(abs(cnta - cntb))

Submission status: Accepted

Solution code:
import math
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
S=[i+1 for i in range(N)]
#R=P
#S=Q
def place(a):
  R=S.copy()
  count=1
  for i in range(0,N):
    count += (R.index(a[i]))*math.factorial(N-1-i)
    del R[R.index(a[i])]
    
  return(count)

print(abs(place(P)-place(Q)))

Submission status: Accepted

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

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

a = [i+1 for i in range(n)]
cnt = 0
cp = 0
cq = 0
for x in itertools.permutations(a):
    cnt += 1
    if list(x) == p: cp = cnt
    if list(x) == q: cq = cnt
    if cq != 0 and cp !=0: break
print(abs(cp - cq))

Submission status: Accepted

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

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

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

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

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

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
nums = [i for i in range(1, n + 1)]
x = [i for i in itertools.permutations(nums)]
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
print(abs(x.index(p) - x.index(q)))

Submission status: Accepted

Solution code:
import math
import itertools
N = int(input())
P = list(map(int,input().split()))
p = ""
for i in range(N):
  p += str(P[i])
Q = list(map(int,input().split()))
q = ""
for i in range(N):
  q += str(Q[i])

s = ""
for i in range(1,N+1):
  s += str(i)
l = [''.join(v) for v in itertools.permutations(s)]

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
P = rl()
Q = rl()

import itertools
a, b = 0, 0
for i, p in enumerate(itertools.permutations(list(range(1,N+1)))):
    if a > 0 and b > 0: break
    if list(p) == P:
        a = i+1
    if list(p) == Q:
        b = i+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()))

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

def cul(n_, lis_):
  l=[]
  for i in range(1,n+1):
  	l.append(i)
  sum_=0
  for i in range(1,n_):
    lenl=len(l)
    #print(lis_[i-1])
    #print(l.index(lis_[i-1])/lenl)
    #print(num_[-i])
    sum_+=l.index(lis_[i-1])/lenl*num_[-i]
    l.remove(lis_[i-1])
    #print(l)
  return sum_
  
k = abs(cul(n,p)-cul(n,q))
print(int(k))

Submission status: Accepted

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

from itertools import permutations 
def main():
    n = int(readline())
    p = tuple([int(i) for i in readline().split()])
    q = tuple([int(i) for i in readline().split()])
    num = [i for i in range(1, n+1)]

    c1 = 0
    c2 = 0
    cnt = 0
    for perm in permutations(num):
        cnt += 1
        if perm == p:
            c1 = cnt
        if perm == q:
            c2 = cnt
    print(abs(c1-c2))   
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = -1
b = -1

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

permutations = list(itertools.permutations(N_list, N))

for i in range(len(permutations)):
    if P == permutations[i]:
        a = i + 1

for j in range(len(permutations)):
    if Q == permutations[j]:
        b = j + 1

print(abs(a-b))

Submission status: Accepted

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

from itertools import permutations

for i, p in enumerate(permutations(sorted(P))):
    if p == P:
        a = i

    if p == Q:
        b = i

print(abs(a - b))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import permutations as perm

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

a = 0
b = 0

for Pi in sorted(perm(P)):
    if P != Pi:
        a += 1
    if P == Pi:
        a += 1
        break
    
for Qi in sorted(perm(Q)):
    if Q != Qi:
        b += 1
    if Q == Qi:
        b += 1
        break

print(abs(a-b))

Submission status: Accepted

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

cnt_p, cnt_q = 1, 1
p = tuple(P)
q = tuple(Q)
for a in sorted(itertools.permutations(P)):
  if a != p:
    cnt_p += 1
  else:
    break
for b in sorted(itertools.permutations(Q)):
  if b != q:
    cnt_q += 1
  else:
    break
print(abs(cnt_p-cnt_q))

Submission status: Accepted

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

L=list(map(list,itertools.permutations(range(1,N+1))))
for a,x in enumerate(L):
    for b,y in enumerate(x):
        L[a][b]=str(y)
L=[int(''.join(x)) for x in L]
L=sorted(L,reverse=False)

print(int(math.fabs((L.index(P)+1)-(L.index(Q)+1))))

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

P_dict = itertools.permutations(sorted(P), N)
P_dict = [i for i in list(P_dict)]
Q_dict = itertools.permutations(sorted(Q), N)
Q_dict = [i for i in list(Q_dict)]

ans = 0
for i in range(len(P_dict)):
  if list(P_dict[i]) == P:
    ans += (i+1)
  if list(Q_dict[i]) == Q:
    ans -= (i+1)

print(ans if ans >= 0 else -ans)

Submission status: Accepted

Solution code:
import itertools

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

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

p = itertools.permutations(n, N)

ans1 = 0
ans2 = 0
for i, v in enumerate(p):
    if (P == v):
        ans1 = i + 1
    if (Q == v):
        ans2 = i + 1

print(abs(ans1 - ans2))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = input().split()
q = input().split()

n_str = ''
for i in range(n):
    n_str += str(i+1)

n_list = []
for order in itertools.permutations(n_str):
    int_str = int(''.join(order))
    n_list.append(int_str)

print(abs(n_list.index(int(''.join(p)))-n_list.index(int(''.join(q)))))

Submission status: Accepted

Solution code:
import itertools

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

for i, j in enumerate(itertools.permutations(N)):
    if j == P:
        a = i + 1
    elif j == Q:
        b = i + 1
if P == Q:
    b = a

print(abs(a - b))

Submission status: Accepted

Solution code:
from collections import deque
import itertools

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

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

D = deque(itertools.permutations(l))

a = 0
b = 0
count = 0
for d in D:
    count += 1
    if d == P:
        a = count
    if d == Q:
        b = count

if a - b < 0:
    print(b - a)
else:
    print(a - b)

Submission status: Accepted

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

import math 
def dict_order(l): # l=list(1,...,nの順列)
    n = len(l)
    order = 0 # lの辞書式順序の番号
    used = [0]*n
    for i in range(n):
        order += (l[i]-1-sum(used[:l[i]]))*math.factorial(N-1-i)
        used[l[i]-1] += 1
    return order # lの辞書式順序の番号
print(abs(dict_order(P)-dict_order(Q)))

Submission status: Accepted

Solution code:
from itertools import permutations as per
n = int(input())
p = tuple(int(i) for i in input().split())
q = tuple(int(i) for i in input().split())

a = b = 0
for i,x in enumerate(per(range(1,n+1)),1):
    if x == p: a = i
    if x == q: b = i
    if a and b: break
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n =int(input())
p =list(map(str ,input().split()))
q =list(map(str ,input().split()))
l =[]
for i in range(1 ,n+1):
    l.append(str(i))
cnt =1
for v in itertools.permutations(l,n):
    if p == list(v):
        a =cnt
    if q == list(v):
        b =cnt
    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()))
 
perm = list(permutations(range(1, N + 1)))
 
for i, pe in enumerate(perm, 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

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

data = P.copy()
data.sort()

for idx, i in enumerate(list(permutations(data, N))):
    if list(i) == P:
        a = idx
    if list(i) == Q:
        b = idx

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()))
ans=sorted([i for i in permutations(range(1, n + 1))])
print(abs(ans.index(p) - ans.index(q)))

Submission status: Accepted

Solution code:
import itertools
import numpy as np

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

length = len(p)

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

index_dict = {}
for index, v in enumerate(sorted(itertools.permutations(n_array, length))):
    index += 1
    if list(v) == list(p):
        index_dict[str(p)] = index
    if list(v) == list(q):
        index_dict[str(q)] = index

    if str(q) in index_dict and str(p) in index_dict:
        break

print(np.abs(index_dict[str(p)] - index_dict[str(q)]))