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 sys
from math import pi, cos, sqrt
from collections import defaultdict
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)
from itertools import permutations


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n = gete(int)
    p = "".join(list(geta()))
    q = "".join(list(geta()))

    base = "".join(map(str, range(1, n + 1)))
    for i, it in enumerate(permutations(base)):
        if p == "".join(it):
            a = i

        if q == "".join(it):
            b = i
    print(abs(b - a))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

all_combination = permutations(range(1, N + 1), N)
dict_order = {}
for i, c in enumerate(all_combination):
    dict_order[c] = i
print(abs(dict_order[P] - dict_order[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()))

l=[ i+1 for i in range(N) ]
Per=itertools.permutations(l,N)
Per=list(Per)
num=[-1,-1]
for i in range(math.factorial(N)):
  if Per[i]==P:
    num[0]=i
  if Per[i]==Q:
    num[1]=i
print(abs(num[0]-num[1]))

Submission status: Accepted

Solution code:
from itertools import permutations
from collections import defaultdict

N = int(input())

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

d = defaultdict(int)

for perm in permutations(range(1,N+1)):
    d[perm] = loop
    loop += 1

res = abs(d[P] - d[Q])

print(res)

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
p=tuple(map(lambda x:int(x)-1,input().split()))
q=tuple(map(lambda x:int(x)-1,input().split()))
perms=list(permutations(range(n)))
for i in range(len(perms)):
    if perms[i]==p:
        pidx=i
    if perms[i]==q:
        qidx=i
print(abs(pidx-qidx))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
l = sorted(itertools.permutations([i for i in range(1,n+1)]))
print(abs(l.index(tuple(p))-l.index(tuple(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()))
a=list(permutations(range(1,n+1)))
A=a.index(p)
B=a.index(q)
ans=abs(A-B)
print(ans)

Submission status: Accepted

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

from collections import deque
tuples=[]
def f(que):
    if len(que)==n:
        tuples.append(tuple(que))
        return
    
    for v in range(1,n+1):
        if v not in que:
            que.append(v)
            f(que)
            que.pop()
f(deque([]))
#print(tuples)
#print(tuples.index(p))
#print(tuples.index(q))
print(abs(tuples.index(p)-tuples.index(q)))

Submission status: Accepted

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    N = int(readline())
    P = ''.join(readline().decode().split()).rstrip()
    Q = ''.join(readline().decode().split()).rstrip()
    str_num = ''.join([str(i) for i in range(1, N+1)])
    all_list = list(itertools.permutations(list(str_num), N))
    all_list = [''.join(a) for a in all_list]
    all_list.sort()
    a = all_list.index(P)
    b = all_list.index(Q)
    print(abs(a - b))


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 = 0
b = 0
for i, x in  enumerate(itertools.permutations(range(1, N + 1))):
    #print(i, x)
    if x == p:
        a = i
    if x == q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def nextPermutations(X):
  buf = X[-1]
  for i in range(N):
    if X[N-i-1]<buf:
      break
    buf = X[N-i-1]
  pov = N-i-1
  v = X[pov]
  mina = 8
  answer = 0
  for i in range(pov,N):
    if X[i]>v and X[i]<=mina:
      answer = i
      mina = X[i]
  X[pov],X[answer] = X[answer],X[pov]
  X = X[:pov+1]+sorted(X[pov+1:])
  return X
  
if P[::]==Q[::]:
  print(0)
else:
  for i in range(N):
    if P[i]<Q[i]:
      min = P
      max = Q
      break
    if Q[i]<P[i]:
      min = Q
      max = P
      break
  count = 0
  while min[::]!=max[::]:
    min = nextPermutations(min[::])
    count+=1
  print(count)

Submission status: Accepted

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

from itertools import permutations

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

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

Submission status: Accepted

Solution code:
import itertools
n=int(input())
ap=0
aq=0
p=list(map(int,input().split()))
q=list(map(int,input().split()))
list1=list(itertools.permutations(list(range(1,n+1))))
list1.sort()
for i,w in enumerate(list1):
    v=list(w)
    if p==v:
        ap=i+1
    if q==v:
        aq=i+1
print(abs(ap-aq))

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

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
num_p = 0
num_q = 0
num = [0]*N

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

seq = list(itertools.permutations(num))

for j in range(len(seq)):
  if list(seq[j]) == P:
    num_p = j+1
  if list(seq[j]) == Q:
    num_q = j+1
    
print(abs(num_p - num_q))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
X = list(itertools.permutations(range(1, N+1)))
a = X.index(A)
b = X.index(B)
print(abs(a-b))

Submission status: Accepted

Solution code:
import math
import numpy as np

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

num_P, num_Q = 0, 0
for i in range(N):
  val = P[0]
  tmp_P = sorted(P)
  idx_P = tmp_P.index(val)
  del P[0]
  if i == N-1:
    num_P += 1
  else:
    num_P += idx_P * math.factorial(N-(i+1))	# 一つ値が決まるごとに使える数値が減ることを考慮していない
    
for i in range(N):
  val = Q[0]
  tmp_Q = sorted(Q)
  idx_Q = tmp_Q.index(val)
  del Q[0]
  if i == N-1:
    num_Q += 1
  else:
    num_Q += idx_Q * math.factorial(N-(i+1))

print(abs(num_P-num_Q))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
def kaijou(x):
    h = 1
    for i in range(1,x+1):
        h *= i
    return h
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
R = [0 for _ in range(N)]
S = [0 for _ in range(N)]
for i in range(N):
    R[P[i]-1] = 1
    s = P[i]-1-sum(R[:(P[i]-1)])
    a += s*kaijou(N-1-i)
for j in range(N):
    S[Q[j]-1] = 1
    t = Q[j]-1-sum(S[:(Q[j]-1)])
    b += t*kaijou(N-1-j)
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=sorted(list(itertools.permutations(range(1,n+1))))
print(abs(l.index(p)-l.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations as pm
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
i = 0

for l in pm([i+1 for i in range(n)],n):
    if l == p:
        a = i
    if l == q:
        b = i
    i += 1
print(abs(a-b))

Submission status: Accepted

Solution code:

import itertools
N = int(input())
LIST = [str(n) for n in range(1, N+1)]
LIST = list(itertools.permutations(LIST, N))

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


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

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
c = list(itertools.permutations(p,n))
c.sort()
for i in range(len(c)):
    if list(c[i]) == p:
        one = i
    if list(c[i]) == q:
        two = i
print(abs(one-two))

Submission status: Accepted

Solution code:
import itertools

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

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

def turn_num(list_num, arr, m):
    for num ,v in enumerate(itertools.permutations(list_num, m)):
        if arr == list(v):
            res = num + 1
            break
    return res

ans = abs(turn_num(list_, p, n) - turn_num(list_, q, n))

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

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

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        break
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1
        break
ans = abs(res_p - res_q)

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

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

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1

ans = abs(res_p - res_q)

print(ans)

Submission status: Accepted

Solution code:
import itertools as iter

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = list(iter.permutations(range(1,n+1)))
    print(abs(l.index(p)-l.index(q)))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools as it
n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = (range(1, n + 1))
V = list(it.permutations(A))
print(abs(V.index(P) - V.index(Q)))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())

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

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

P = tuple(P)


permuts = list(permutations(copy))

p = 0
q = 0

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

print(abs(p - q))

Submission status: Accepted

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

for j in range(len(all)):
    if all[j] == P:
        ans1 = j
    if all[j] == Q:
        ans2 = j
print(abs(ans1 - ans2))

Submission status: Accepted

Solution code:
from itertools import permutations

N=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))
a=[i for i in range(1,N+1)]
tmp=list(permutations(a))
#print(tmp)
x=0
y=0

for i in range(len(tmp)):
    if tmp[i]==A:
        x=i
        break
for i in range(len(tmp)):
    if tmp[i]==B:
        y=i
        break
print(abs(x-y))

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

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

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
keep = []
p = tuple((map(int, input().split())))
q = tuple((map(int, input().split())))
for i in range(1, n + 1):
    keep.append(i)
all = list(itertools.permutations(keep))
for j in range(len(all)):
    if all[j] == p:
        ans1 = j
    if all[j] == q:
        ans2 = j
print(abs(ans1 - ans2))

Submission status: Accepted

Solution code:
import itertools

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

lst = list(itertools.permutations(range(1,N+1)))
#print(lst)
Pi = lst.index(P)
Qi = lst.index(Q)
print(abs(Pi-Qi))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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


for i, v in enumerate(itertools.permutations(range(1,N+1))):
    if list(v) == P_list:
        a = i + 1
    if list(v) == Q_list:
        b = i + 1

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

m=list(itertools.permutations(p))
m.sort()

p=tuple(p)
q=tuple(q)
a=m.index(p)
b=m.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())) # 横入力
seq = [0]*n
for i in range(1,n+1):
    seq[i-1] = i
nnn = list(itertools.permutations(seq, n))
print(abs(nnn.index(q) - nnn.index(p)))

Submission status: Accepted

Solution code:
N=int(input())

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

import itertools
itertools.permutations(P)
l=list(itertools.permutations(P))
l.sort()
P=tuple(P)
Q=tuple(Q)

p=l.index(P)
q=l.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()))
dict=sorted(permutations(range(1,N+1)))
print(abs(dict.index(P)-dict.index(Q)))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import bisect
from collections import deque


def read():
    N = int(input().strip())
    P = list(map(int, input().strip().split()))
    Q = list(map(int, input().strip().split()))
    return N, P, Q


def next_permutation(a):
    l = -1
    for i in range(len(a)-1):
        if a[i] < a[i+1]:
            l = i
    if l == -1:
        return False
    for j in range(len(a)-1, -1, -1):
        if a[l] < a[j]:
            break
    tmp = a[l]
    a[l] = a[j]
    a[j] = tmp
    j = l + 1
    k = len(a) - 1
    while j < k:
        tmp = a[k]
        a[k] = a[j]
        a[j] = tmp
        j += 1
        k -= 1
    return True


def solve(N, P, Q):
    a, b = 0, 0
    while next_permutation(P):
        a += 1
    while next_permutation(Q):
        b += 1
    return abs(b - a)
            
    
if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))

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()))
p = tuple(p_list)
q = tuple(q_list)
l = list(range(1,n+1))
s = list(permutations(l))
ans = abs(s.index(p)-s.index(q))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC150C
import itertools
n = int(input())
ls = [i+1 for i in range(n)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))
v = [ list(v) for v in itertools.permutations(ls, n)]
a, b = v.index(p), v.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()))
numP = 1
numQ = 1
for i in range(N-1):
    cou = 1
    time = P[i]-1
    for j in range(1,N-i):
        cou = cou*j
    for j in range(0,i):
        if P[j]< P[i]:
            time -= 1
    numP += time*cou
for i in range(N-1):
    cou = 1
    time = Q[i]-1
    for j in range(1,N-i):
        cou = cou*j
    for j in range(0,i):
        if Q[j]< Q[i]:
            time -= 1
    numQ += time*cou
print(abs(numP-numQ))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#Favorite Answers
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)))
print(abs(l.index(p)-l.index(q)))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
candi=[str(i) for i in range(1,N+1)]
P="".join(input().split())
Q="".join(input().split())
count=0
#候補列挙
for j in itertools.permutations(candi,N):
  k="".join(list(j))
  count+=1
  if k==P:
    a=count
  if k==Q:
    b=count
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()))
l = list(permutations(range(1,n+1)))
print(abs(l.index(p)-l.index(q)))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
S=list(itertools.permutations(P, n))
T=list(itertools.permutations(P, n))
S.sort()
T.sort()
a=0
b=0
for i in range(len(S)):
  if list(S[i])==P:
    a=i+1
for j in range(len(T)):
  if list(T[j])==Q:
    b=j+1
print(abs(a-b))

Submission status: Accepted

Solution code:
import math 
n = int(input())
lp = list(map(int,input().split()))
lq = list(map(int,input().split()))
def fun(l):
    a = 1 #取り出す
    count = 1
    li = []
    for i in range(n):
        ika = 0
        for j in li:
            if j < l[i]:
                ika += 1
        if l[i] == i+1 and ika == l[i]:
                c = 0
                a = l[i]
        else:
                c = (l[i]-ika-1)*math.factorial(n-i-1)
                a = l[i]
            
        count += c
        li.append(l[i])
    return count
   

print(abs(fun(lp)-fun(lq)))

Submission status: Accepted

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

from itertools import permutations as per

i=0
for nn in per(map(str,range(1,n+1))):
    i+=1
    st=' '.join(nn)
    if p==st:a=i
    if q==st:b=i

print(abs(b-a))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
num_list = [list(map(int, input().split())) for _ in range(2)]

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

ans = abs(all_list.index(tuple(num_list[0])) - all_list.index(tuple(num_list[1])))
print(ans)

Submission status: Accepted

Solution code:
import itertools
N=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
lis=list(itertools.permutations(list(range(1,N+1))))
tmp1=0
tmp2=0
a=0
b=0
for v in lis:
    tmp=list(v)
    if tmp==p:
        a=tmp1+1
    if tmp==q:
        b=tmp2+1
    tmp1+=1
    tmp2+=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()))

l=[i for i in range(1,N+1)]
p =list(itertools.permutations(l, N))
P_n=p.index(P)
Q_n=p.index(Q)
print(abs(P_n-Q_n))

Submission status: Accepted

Solution code:
# import sys
# import math
import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()
# sys.setrecursionlimit(100000)
# INF = 1001001001001
# MOD = 1000000007


def main():

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

    a, b = -1, -1
    for i, c in enumerate(comb):
        if c == P:
            a = i
        if c == Q:
            b = i

    print(abs(a - b))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import product, permutations,combinations
def main():
    N = int(input())
    P = input()
    Q = input()
    Nlis = list(range(1,N+1))
    for i in range(len(Nlis)):
        Nlis[i] = str(Nlis[i])

    lis = []
    for i in permutations(Nlis):
        lis.append(' '.join(i))
    sorted(lis)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
#a,b,x=map(int,input().split())
pl=list(map(str,input().split()))
ql=list(map(str,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

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

import itertools
p=("".join(pl))
q="".join(ql)

per=[]

for tup in itertools.permutations(l):
    per.append("".join(list(tup)))

per.sort()

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
a = 0
b = 0
A = [0] * (N - 1)
A[N - 2] = 1
for i in range(N):
    for j in range(i,N):
        if P[j] > P[i]:
            P[j] -= 1
    for j in range(i,N):
        if Q[j] > Q[i]:
            Q[j] -= 1
for i in range(1,N -1):
    A[N - 2 - i] = (i + 1)* A[N - 2 - i + 1]
for i in range(N -1):
    a += P[i]*A[i]
    b += Q[i]*A[i]
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

n = int(input().strip())
p = int(''.join(input().strip().split()))
q = int(''.join(input().strip().split()))

cnt_l = 0
cnt_r = 0
for it in itertools.permutations(list(map(str,range(1, n+1)))):
  tmp = int(''.join(it))
  if tmp<=p:
    cnt_l += 1
  if tmp<=q:
    cnt_r += 1
print(abs(cnt_r-cnt_l))

Submission status: Accepted

Solution code:
import itertools

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

seq = [i for i in range(1,N+1)]
perm = [int(''.join(map(str,p))) for p in itertools.permutations(seq)]
perm.sort()

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

Submission status: Accepted

Solution code:
n = int(input())
p = int(input().replace(" ",""))
q = int(input().replace(" ",""))
dic = []

import itertools
n_dic = range(1,n+1)
for v in itertools.permutations(n_dic, n):
    a = ""
    for i in range(n):
        a += str(v[i])
    dic.append(int(a))

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

Submission status: Accepted

Solution code:
import itertools

n=int(input())
s=tuple(map(int,input().split()))
t=tuple(map(int,input().split()))

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

num1=(y.index(s))
num2=(y.index(t))

print(abs(num1-num2))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
cnt = 0
for n in itertools.permutations(range(1, N+1)):
    if P == list(n):
        a = cnt
    if Q == list(n):
        b = cnt
    cnt += 1
total = abs(a-b)
print(total)

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
p,q = map(int,raw_input().split(' ')), map(int,raw_input().split(' '))
res = []

def perms(i, elems,res):

	if i == len(elems):
		res.append(tuple(elems)) 
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, range(1, n+1), res)
tp,tq = tuple(p),tuple(q)
res.sort()
for i in range(len(res)):
	if res[i] == tp: ii = i
	if res[i] == tq: jj = i

print abs(jj - ii)

Submission status: Accepted

Solution code:
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))


elems = range(1, n+1)
res = []
c = [0]
def perms(i, elems,res):

	if i == len(elems):
		res.append(tuple(elems)) 
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, elems, res)

tp = tuple(p)
tq = tuple(q)
res.sort()
for i in range(len(res)):
	if res[i] == tp:
		ii = i

	if res[i] == tq:
		jj = i

print abs(jj - ii)

Submission status: Accepted

Solution code:
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))

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


def f(p,n):

	if len(p) == 1:
		return 0
	sp = sorted(p)
	for i,e in enumerate(sp):
		if e == p[0]:
			hm = i
			break 

	return i * factorial(len(p) -1) + f(p[1:],n)


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

Submission status: Accepted

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


import itertools
import math
A=[x for x in range(1,N+1)]
lis=itertools.permutations(A)

c=0
d=0
for s in lis:

  judge=True
  c+=1
  for i in range(N):
    if P[i] != s[i]:
      judge=False
  if judge:
    break
    
li=itertools.permutations(A)
  
for s in li:
  judge=True
  d+=1
  for i in range(N):
    if Q[i] != s[i]:
      judge=False
  if judge:
    break

print(abs(c-d))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
l = list(range(1,N+1))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
i=0
a=0
b=0
for p in itertools.permutations(l,N):
	i+=1
	if a==0:
		for j in range(N):
			if P[j]!=p[j]:break
		else:
			a=i
	if b==0:
		for j in range(N):
			if Q[j]!=p[j]:break
		else:
			b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc150/tasks/abc150_c
import sys
from itertools import *


def S(): return sys.stdin.readline().rstrip()


def I(): return int(sys.stdin.readline().rstrip())


def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))


def LS(): return list(sys.stdin.readline().rstrip().split())


N = I()
p = tuple(LI())
q = tuple(LI())

all = list(range(1, N+1))

all = list(permutations(all, N))


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

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

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

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

Submission status: Accepted

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

p = 0
q = 0
if P == Q:
  ans = 0
  print(ans)
  quit()
  
x = 0
for one_case in permutations_lis:#順列が昇順であること　順列リストをforで回す
  x += 1
  if P == list(one_case):
    p = x
  elif Q == list(one_case):
    q = x

print(abs(q-p))

Submission status: Accepted

Solution code:
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=i+1
from itertools import permutations
templ=list(permutations(temp,n))#長さnのtmpの順列リストを取る　どうやら昇順で取ってくれるらし
     
for i in range(0,len(templ)):
  if templ[i]==p:
    a=i#大きさの差を取るので，ずれは問題なし
  if templ[i]==q:
    b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

a=list(itertools.permutations(p))
aa=sorted(a)
b=abs(aa.index(tuple(p))-aa.index(tuple(q)))
print(b)

Submission status: Accepted

Solution code:
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=i+1
from itertools import permutations
templ=list(permutations(temp,n))
for i in range(0,len(templ)):
  if templ[i]==p:
    a=i
  if templ[i]==q:
    b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
def dfs(l,N):
    if len(l)==N:
        x.append(int("".join(map(str,l))))
        return
    else:
        for n in range(1,N+1):
            if n not in l:
                l.append(n)
                dfs(l,N)
                l.pop()

def main():
    N=int(input())
    P=list(input().strip().split())
    Q=list(input().strip().split())
    global x
    x=[]
    dfs([],N)
    x.sort()
    a=x.index(int("".join(P)))
    b=x.index(int("".join(Q)))
    return abs(a-b)

if __name__=="__main__":
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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