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())
p=tuple(map(int, input().split()))
q=tuple(map(int, input().split()))

per=itertools.permutations(range(1, n+1), n)
for i, v in enumerate(per):
    if(p==v): pi=i
    if(q==v): qi=i

print(abs(pi-qi))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    # 標準入力を取得
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))

    return N, P, Q


def main(N: int, P: tuple, Q: tuple) -> None:
    """
    メイン処理.

    Args:\n
        N (int): 数列の大きさ
        P (tuple): 順列
        Q (tuple): 順列
    """
    # 求解処理
    p = 0
    q = 0
    order = 1
    for t in itertools.permutations(range(1, N + 1), N):
        if t == P:
            p = order
        if t == Q:
            q = order
        order += 1

    ans = abs(p - q)

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, P, Q = get_input()

    # メイン処理
    main(N, P, Q)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# モジュールのインポート
import itertools

# 標準入力を取得
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

# 求解処理
p = 0
q = 0
order = 1
for t in itertools.permutations(range(1, N + 1), N):
    if t == P:
        p = order
    if t == Q:
        q = order
    order += 1

ans = abs(p - q)

# 結果出力
print(ans)

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(list(range(1,N+1))))
print(abs(ans.index(P)-ans.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(list(range(1, n+1))))
print(abs(l.index(p)-l.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()))

p = sorted(list(permutations(P)))
q = sorted(list(permutations(Q)))

print(abs(p.index(P) -  q.index(Q)))

Submission status: Accepted

Solution code:
from itertools import permutations
from bisect import bisect_left

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

perm = list(permutations(p))
perm.sort()
a = bisect_left(perm, p)
b = bisect_left(perm, q)

print(abs(a- b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

perm = list(permutations(p))
perm.sort()
a = perm.index(p)
b = perm.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()))


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

i = 0
for t in prm:
    i += 1
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
import itertools
import sys
sys.setrecursionlimit(10**9)


INF=10**20
def solve(N: int, P: "List[int]", Q: "List[int]"):
    X = list(range(1,N+1))
    R = []
    for r in itertools.permutations(X,N):
        R.append(int("".join(list(map(str,r)))))
        
    R.sort() 
    P = int("".join(P))
    Q = int("".join(Q))
    a,b = 0,0
    for i,r in enumerate(R):
        if r == P:
            a = i
        if r == Q:
            b = i

    print(abs(a-b))
    


    return

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 = [str(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [str(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)



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

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:
from math import factorial
from itertools import permutations

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

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

allnum = list(i for i in permutations(num, n))

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

Submission status: Accepted

Solution code:
from math import factorial

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


def cnt(x):
  pattern = 0
  numbers = list(i for i in range(1, n+1))
  for i in range(n):
    if x[i] > min(numbers): 
      pattern += numbers.index(x[i]) * factorial(n-i-1)
    numbers.remove(x[i])
  return pattern


print(abs(cnt(p) - cnt(q)))

Submission status: Accepted

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

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

from itertools import permutations

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

# print(p)

a = p.index(tuple(P))
b = p.index(tuple(Q))

print(abs(a-b))

Submission status: Accepted

Solution code:
def generate_arr(a):
    if len(a) == N:
        arr.append(a)
        return
    for i in range(1,N+1):
        if i not in a:
            generate_arr(a+[i])
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
arr = []
generate_arr([])
arr.sort()
ans_p = 0
ans_q = 0
for i in range(len(arr)):
    if arr[i] == P:
        ans_p = i+1
    if arr[i] == Q:
        ans_q = i+1
print(abs(ans_p-ans_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 for i in range(1,N+1)]
cnt = 0
ans_a, ans_b = 0, 0
for target in itertools.permutations(a):
    cnt += 1
    if list(target) == P:
        ans_a = cnt
    if list(target) == Q:
        ans_b = cnt
print(abs(ans_a-ans_b))

Submission status: Accepted

Solution code:
#C - Count Order
#DFS
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

a = 0
b = 0
cnt = 0
def dfs(A):
    global cnt
    global a
    global b
    if len(A) == N:
        cnt += 1
        if A == P:
            a = cnt
        if A == Q:
            b = cnt
        return   
    for v in range(1,N+1):
        if v in A:
            continue
        A.append(v)
        dfs(A)
        A.pop()
dfs([])
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()))

lis = list(itertools.permutations(range(1,N+1)))
a = lis.index(P)+1
b = lis.index(Q)+1
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()))


#print(p, q)

#1,2,3の順列作成
lis = list(itertools.permutations(range(1, n + 1)))

#print(lis)

print(abs(lis.index(p) - lis.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=sorted(p)

for i,j in enumerate(itertools.permutations(lis)):
  if list(j)==p:
    a=i
  if list(j)==q:
    b=i
    
print(abs(a-b))

Submission status: Accepted

Solution code:
#
# abc150 c
#

import sys
from io import StringIO
import unittest
import itertools


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
1 3 2
3 1 2"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1"""
        output = """17517"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """3
1 2 3
1 2 3"""
        output = """0"""
        self.assertIO(input, output)


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

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

    a = b = 0
    for i in range(len(IL)):
        if list(IL[i]) == P:
            a = i
        if list(IL[i]) == Q:
            b = i

    print(abs(a-b))


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
#
# abc150 c
#

import sys
from io import StringIO
import unittest
import itertools


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
1 3 2
3 1 2"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1"""
        output = """17517"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """3
1 2 3
1 2 3"""
        output = """0"""
        self.assertIO(input, output)


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

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

    a = b = 0
    for i in range(len(IL)):
        if list(IL[i]) == P:
            a = i
        if list(IL[i]) == Q:
            b = i

    print(abs(a-b))


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
n=int(input())
P=[int(x) for x in input().split()]
Q=[int(x) for x in input().split()]
p=tuple(P)
q=tuple(Q)
l=list(range(1,n+1))
L=[]
import itertools
for v in itertools.permutations(l):
  L.append(v)
a=L.index(p)
b=L.index(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()))
lis = [i for i in range(1,n+1)]
zisyo = list(itertools.permutations(lis))
print(abs(zisyo.index(tuple(p)) - zisyo.index(tuple(q))))

Submission status: Accepted

Solution code:
from itertools import permutations
if __name__ == "__main__":
    N = int(input())
    P = tuple(map(int,input().split()))
    Q = tuple(map(int,input().split()))
    arr = [i for i in range(1,N+1)]
    pattern_arr = list(permutations(arr))
    a,b = -1,-1
    for i,pattern in enumerate(pattern_arr):
        if pattern == P:
            a = i
        if pattern == Q:
            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()))

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

n = [i+1 for i in range(N)]
ptr = sorted(list(permutations(n)))

p = 0
q = 0
for i in range(fac):
    if list(ptr[i]) == P:
        p = i
    if list(ptr[i]) == Q:
        q = i

print(abs(p-q))

Submission status: Accepted

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

from itertools import permutations

lis = [i+1 for i in range(N)]
per = permutations(lis,N)

index = 0
for i in per:
    if i == P:
        p_index = index
    if i == Q:
        q_index = index
    index += 1
    
print(abs(p_index-q_index))

Submission status: Accepted

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

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

n = int(readline())
a = np.array(readline().split(), np.int64)
b = np.array(readline().split(), np.int64)

n_lst = [i for i in range(1, n+1)]
per_lst = list(permutations(n_lst, n))

ans = abs((per_lst.index(tuple(a))+1) - (per_lst.index(tuple(b))+1))

print(ans)

Submission status: Accepted

Solution code:
import itertools as it
N=int(input())
W=list(range(1,N+1))
c1=-1
c2=-1
c=0
L=tuple(map(int,input().split()))
M=tuple(map(int,input().split()))
if L==M:
  print(0)
  exit()
for i in it.permutations(W,N):
  c+=1
  if i==L:
    c1=c
  elif i==M:
    c2=c
print(abs(c1-c2))

Submission status: Accepted

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

# print(p)
# print(q)

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

cnt = 1
for v in permutations(l,n):
    if p == v:
        a = cnt
    if q == v:
        b = cnt
    cnt += 1
print(abs(a-b))

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

if (p == q):
    print(0)
    exit()

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

t = list(itertools.permutations(seq))

ans = []
for i in range(len(t)):
    if list(t[i]) == p or list(t[i]) == q:
        ans.append(i)

print(ans[1]-ans[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
import itertools
import math

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
n=0
A_list=itertools.permutations(range(1,N+1))
for A in A_list:
  n+=1
  A=list(A)
  if A==P:
    a=n
  if A==Q:
    b=n

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()))
Z_list = [n for n in range(1, N + 1)]
for i,j in enumerate(permutations(Z_list, r = N)):
    if j == P:
        a = i 
    if j == Q:
        b = i
print(abs(b - a))

Submission status: Accepted

Solution code:
import sys
import itertools
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N = I()
    P = LI()
    Q = LI()
    index = 1
    seq = [i+1 for i in range(N)]
    for i in itertools.permutations(seq, r=None):
        if P == i:
            a = index
        if Q == i:
            b = index        
        index += 1
    print(abs(a-b))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))
p = itertools.permutations(range(1,N+1),N)
hashmap = {}
for i, v in enumerate(p):
    hashmap[v]=i+1
print(abs(hashmap[A]-hashmap[B]))

Submission status: Accepted

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

def func(n):
    if n==0 or n==1:
        return 1
    ans = n
    for j in reversed(range(2, n)):
        ans*=j
    return ans

def count(N, P):
    ans = 0
    a = [1]*N
    
    for i in range(N-1):
        ans+=sum(a[:P[i]-1])*func(N-1-i)
        a[P[i]-1]=0
    return ans

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

Submission status: Accepted

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

cnt=1

import itertools
for seq in itertools.permutations(range(1,n+1)):
    p_hantei=True
    q_hantei=True
    for i in range(n):
        if p[i]!=seq[i]:
            p_hantei=False
            break
    for i in range(n):
        if q[i]!=seq[i]:
            q_hantei=False
            break

    if p_hantei:
        p_num=cnt
    if q_hantei:
        q_num=cnt

    cnt+=1
print(abs(p_num-q_num))

Submission status: Accepted

Solution code:
import itertools

n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
num=list(itertools.permutations(range(1,n+1)))
cnt,c1,c2=0,0,0

for i in num:
  cnt+=1
  
  if i==p:
    c1=cnt
    
  if i==q:
    c2=cnt
    
print(abs(c1-c2))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = [int(i)-1 for i in input().split()]
q = [int(i)-1 for i in input().split()]
m = sorted(p)
a = 0
b = 0
cnt = 1
for i in itertools.permutations(m,n):
  if list(i) == p:
    a += cnt
  if list(i) == q:
    b += cnt
  if a!=0 and b!=0:
    break
  else:
    cnt += 1
print(abs(a-b))

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()]
t=[int(i) for i in range(1,n+1)]
a=b=0
for i,j in enumerate(list(itertools.permutations(t,n))):
    w=[1,1]
    for x,y in enumerate(j):
        if p[x]!=y: w[0]=0
        if q[x]!=y: w[1]=0
    if w[0]: a=i
    if w[1]: b=i

print(abs(a-b))

Submission status: Accepted

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

comb = list(perm(range(1, N+1)))
def find(x):
    for i, n in enumerate(comb):
        if x == n:
            return i
a = find(P)
b = find(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()))
cnt, a, b = 0, 0, 0
for i in itertools.permutations(range(1, N+1)):
    if P == i:
        a = cnt
        if b != 0:
            break
    if Q == i:
        b = cnt
        if a != 0:
            break
    cnt += 1
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()))
P=tuple(map(lambda x: x-1,P))
Q=tuple(map(lambda x: x-1,Q))
l=list(permutations(range(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()))

l = list(itertools.permutations(range(1,N+1)))
tp = tuple(P)
tq = tuple(Q)
tpi = l.index(tp)
tqi = l.index(tq)
print(abs(tpi-tqi))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import operator
import sys

INF = float('inf')


def solve(N: int, P: "List[int]", Q: "List[int]"):
    return abs(operator.sub(*[list(itertools.permutations(range(1, N+1))).index(tuple(i)) for i in [P, Q]]))


def main():

    sys.setrecursionlimit(10 ** 6)

    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]"
    print(f'{solve(N, P, Q)}')


if __name__ == '__main__':
    main()

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()))
nums=np.array(tuple(itertools.permutations(range(1, N+1), N))).tolist()

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

Submission status: Accepted

Solution code:
import itertools


def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    lst = sorted(P)

    for i, j in enumerate(itertools.permutations(lst)):
        if list(j) == P:
            a = i
        if list(j) == Q:
            b = i

    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()))
num = 0
for i in itertools.permutations(range(1,n+1),n):
    if i == p:
        a = num
    if i == q:
        b = num
    num += 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 = list(itertools.permutations(range(1,N+1)))

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

print(abs(a-b))

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

init = sorted(p)


# print(p, q, init)
countp, countq, count = 0, 0, 0

for array in itertools.permutations(init, n):
    # print(array)
    count += 1
    if(list(array) == p):
        countp = count
    if(list(array) == q):
        countq = count


print(abs(countp-countq))

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
p=tuple(int(i)for i in input().split())
q=tuple(int(i)for i in input().split())
permutation_n=list(permutations(range(1,n+1)))
a=b=0
for i in permutation_n:
    if i<p:a+=1
    if i<q:b+=1
print(abs(a-b))

Submission status: Accepted

Solution code:
n=int(input())
per=[i+1 for i in range(n)]
from itertools import permutations
per=list(permutations(per,n))

from bisect import bisect_left
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))

print(abs(bisect_left(per,p)-bisect_left(per,q)))

Submission status: Accepted

Solution code:
from itertools import permutations
N =[i+1 for i in range(int(input()))]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
index = 0

for n in permutations(N):
    if p==n:
        x = index
    if q ==n:
        y = index

    index += 1

print(abs(x-y))

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 = [0] + list(permutations(list(range(1,n+1))))
pnum = per.index(p)
qnum = per.index(q)
ans = abs(pnum-qnum)

print(ans)

Submission status: Accepted

Solution code:
import itertools
#itertools.permutations(t)

def main():
    n = int(input())
    p = tuple(map(int, input().split(" ")))
    q = tuple(map(int, input().split(" ")))

    n_list = [i+1 for i in range(n)]
    n_perm = list(itertools.permutations(n_list))
    
    for i, one in enumerate(n_perm):
        i1 = i + 1
        if one==p:
            a = i1
        if one==q:
            b = i1

    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()))
List = list(itertools.permutations(range(1,N+1)))
ans = abs(List.index(Q) - List.index(P))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

n=sorted(b)
m=list(itertools.permutations(n))
m=[list(i) for i in m]
print(abs(m.index(b)-m.index(c)))

Submission status: Accepted

Solution code:
from itertools import permutations


def mapt(fn, *args):
    return tuple(map(fn, *args))
  

def main():
    n = int(input())
    data = permutations(range(1, n+1), n)
    p = mapt(int, input().split(" "))
    q = mapt(int, input().split(" "))

    list1 = []
    for index, i in enumerate(data):
        if p == i or i == q:
            list1.append(index)
    print(abs(list1[0] - list1[-1]))
main()

Submission status: Accepted

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

cnt = 0
p_cnt = 0
q_cnt = 0
n_list = [i for i in range(1, n+1)]
for P in itertools.permutations(n_list):
    cnt += 1
    if p == list(P):
        p_cnt = cnt
    if q == list(P):
        q_cnt = cnt
    if p_cnt and q_cnt:
        print(abs(q_cnt-p_cnt))
        exit()

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 = [i for i in range(1,n+1)]



x = list(permutations(a,n))

y = x.index(p)
z = x.index(q)

print(abs(y-z))

Submission status: Accepted

Solution code:
import math
def order(N,P):
  value=0
  N_ls=list(range(1,N+1))
  score=0
  for i in P:
    score+=N_ls.index(i)*math.factorial(len(N_ls)-1)
    N_ls.remove(i)
  return score

N=int(input())
P=[int(x) for x in input().split()]
Q=[int(x) for x in input().split()]
print(abs(order(N,P)-order(N,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(list(range(1, N+1)), N))
a = l.index(P) + 1
b = l.index(Q) + 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,b = 0,0
for i, perm in enumerate(itertools.permutations(range(1, N+1)), 1):
    if P==perm:
        a = i
    if Q==perm:
        b = i
    if a > 0 and b > 0:
        print(abs(a-b))
        exit()

Submission status: Accepted

Solution code:
import bisect
N = int(input())
Kaijo = [1 for _ in range(N)]
for i in range(2,N+1):
  Kaijo[-i] = Kaijo[1-i]*i
#print(Kaijo)
L = [i+1 for i in range(N)]
P = list(map(int,input().split()))
PN = 0
for i in range(N-1):
  k = bisect.bisect_left(L, P[i])
  L.remove(P[i])
  PN += k*Kaijo[i+1]
#print(PN)
L = [i+1 for i in range(N)]
Q = list(map(int,input().split()))
QN = 0
for i in range(N-1):
  k = bisect.bisect_left(L, Q[i])
  L.remove(Q[i])
  QN += k*Kaijo[i+1]
ans = abs(PN-QN)
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

x = list(itertools.permutations(range(1, n+1)))
print(abs(x.index(p)-x.index(q)))

Submission status: Accepted

Solution code:
import itertools

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

a = list(itertools.permutations(range(1, n+1)))
print(abs(a.index(p)-a.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations
import sys

N = int(sys.stdin.readline().rstrip())
P = [int(x) for x in sys.stdin.readline().rstrip().split()]
Q = [int(x) for x in sys.stdin.readline().rstrip().split()]


def nanba(N, P):
    P = tuple(P)
    for i, p in enumerate(permutations(range(1, N + 1), N)):
        # print(p)
        if P == p:
            return i
    return 0

a = nanba(N, P)
b = nanba(N, Q)

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

def dictnum(l):
  seq=list(itertools.permutations(l))
  seq.sort()

  return seq.index(tuple(l))

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

print(abs(dictnum(p)-dictnum(q)))

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = inpl()
B = inpl()
aa = 0
bb = 0
i = 0
for tmp in itertools.permutations(range(1,n+1)):
    i += 1
    if list(tmp) == A:
        aa = i
    if list(tmp) == B:
        bb = i
print(abs(aa - bb))

Submission status: Accepted

Solution code:
import itertools

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

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

a = 1
for i in list(itertools.permutations(num_list)):
    if P==i:
        break
    else:
        a += 1
b = 1
for i in list(itertools.permutations(num_list)):
    if Q==i:
        break
    else:
        b += 1

if a-b>=0:
    print(a-b)
else:
    print(-a+b)

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
l = [tuple(map(int,input().split())) for _ in range(2)]
a = list(permutations(range(1,n+1)))
print(abs(a.index(l[0]) - a.index(l[1])))

Submission status: Accepted

Solution code:
import itertools 
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
w = 0
x = 0
S = []
T = []
for i in range(N):
    S.append(i+1)
for j in itertools.permutations(S):
    T.append(list(j))
a = T.index(P)
b = T.index(Q)
print(abs(a-b))