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:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
#import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
from datetime import datetime as dt

def main():
  n = int(input())
  p = tuple(map(int, input().split()))
  q = tuple(map(int, input().split()))
  l = [i for i in range(1,n+1)]
  for i, junretsu in enumerate(permutations(l, n)):
    if junretsu==p:
      a = i
    if junretsu==q:
      b=i
  print(abs(b-a))
  
  
if __name__ == '__main__':
  main()

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

Submission status: Accepted

Solution code:
import itertools


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

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

num1=(y.index(P))
num2=(y.index(Q))

print(abs(num1-num2))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
a = [i+1 for i in range(n)]
a = permutations(a)
p = list(map(int, input().split()))
q = list(map(int, input().split()))

dict_order = sorted(a)
pi = dict_order.index(tuple(p))
qi = dict_order.index(tuple(q))

print(abs(pi - qi))

Submission status: Accepted

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


count = 0
a, b = 0, 0
for index, i in enumerate(permutations(range(1, n+1))):
    if i in [P, Q]:
        if count == 0:
            count += 1
            a = index
        else:
            b = index
            break
if P != Q:
    print(abs(a - b))
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
P = tuple(lr())
Q = tuple(lr())
a = -1; b = -1
for i, x in enumerate(itertools.permutations(range(1, N+1), N)):
    if x == P:
        a = i
    if x == Q:
        b = i

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

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

permutation = list(permutations(range(1, n+1)))
a = bisect_left(permutation, p)
b = bisect_left(permutation, q)

print(abs(a - b))

Submission status: Accepted

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


def solve(N: int, P: "List[int]", Q: "List[int]"):
    index = 0
    p = -1
    q = -1
    P = tuple(P)
    Q = tuple(Q)
    for perm in itertools.permutations(range(1, N+1)):
        if perm == P:
            p = index
        if perm == Q:
            q = index
        index += 1
    print(abs(p-q))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
arr=[i for i in range(1,n+1)]
res_p=0
for i in range(n-1):
  res_p+=math.factorial(n-i-1)*(arr.index(p[i]))
  arr.remove(p[i])
arr=[i for i in range(1,n+1)]
res_q=0
for i in range(n-1):
  res_q+=math.factorial(n-i-1)*(arr.index(q[i]))
  arr.remove(q[i])
print(abs(res_p-res_q))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
res = list(permutations(range(1,n+1)))
ans = abs(res.index(p) - res.index(q))
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()))
res = list(permutations(range(1,N+1)))
ans = abs(res.index(P) - res.index(Q))
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
P=tuple(P)
Q=tuple(Q)
n=[]
for i in range(N):
    n.append(i+1)
nn=list(itertools.permutations(n))#全リスト

a=nn.index(P)+1
b=nn.index(Q)+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()))

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

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

Submission status: Accepted

Solution code:
# define
BIT = [0] * 20
frac = [1] * 20
ckn = [[0] * 20] * 20
maxN = 10
ans = [0] * 2

# BIT
def upd(pos, val) :
    i = pos
    while i <= maxN:
        BIT[i] += val
        i += (i & (-i))
def get(pos):
    res = 0
    i = pos
    while i > 0:
        res += BIT[i]
        i -= (i & (-i))
    return res
# calculation
def prepare():
    for i in range(1, maxN): frac[i] = frac[i - 1] * i
    for i in range(0, maxN):
        ckn[0][i] = 1
    for i in range(1, maxN):
        for j in range(i, maxN):
            ckn[i][j] = ckn[i - 1][j - 1] + ckn[i][j - 1]

# Main
prepare()
n = int(input())

for i in range(0, 2):
    for j in range(0, maxN): BIT[j] = 0
    s = input()

    for j in range(0, n) :
        x = int(s.split()[j])
        ans[i] += (x - 1 - get(x)) * frac[n - j - 1]
        upd(x, 1)

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

Submission status: Accepted

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

Submission status: Accepted

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

num = []
for i in range(1,n+1):
    num.append(i)
ans = []
for i in itertools.permutations(num,n):
    ans.append(i)

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
pl = tuple([int(i) for i in input().split()])
ql = tuple([int(i) for i in input().split()])

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

print(abs(all_list.index(pl) - all_list.index(ql)))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
l = [i+1 for i in range(N)]
cnt = 0
l = list(itertools.permutations(l))
li = []
for i in l:
  li.append(list(i))
p_l = list(map(int,input().split()))
q_l = list(map(int,input().split()))

print(abs(li.index(q_l) -li.index(p_l)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=[]
for i in range(n):
    l.append(i+1)
import itertools
c=1
for v in itertools.permutations(l,n):
    if p==v:
        pc=c
    if q==v:
        qc=c
    c+=1
print(abs(pc-qc))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
figlist = list(itertools.permutations(range(1,n+1)))
for i in range(len(figlist)):
  if a == figlist[i]:anum = i
  if b == figlist[i]:bnum = i
print(abs(anum-bnum))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x = int(input())
l1 = tuple(map(int,input().split()))
l2 = tuple(map(int,input().split()))
li =[]
for i in l1:
    li.append(i)
li.sort()
from collections import defaultdict
dd = defaultdict(int)
from itertools import product, permutations,combinations
c = 1
for i in permutations(li,x):
    dd[i] = c
    c += 1
print(abs(dd[l1]-dd[l2]))

Submission status: Accepted

Solution code:
import math

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

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

ord1 = 0
flag = [0] * (n+1)
for i in range(n-1):
	ln = 0
#	print(type(p[i]), p[i])
	for c in range(1,p[i]):
		if flag[c] == 0:
			ln = ln + 1 
	flag[p[i]] = 1
	ord1 = ord1 + ln * math.factorial(n-1-i)
#	print(ln,  math.factorial(ln))
ord1 = ord1+1 
#print(ord1)
  
ord2 = 0
flag = [0] * (n+1)
for i in range(n-1):
	ln = 0
#	print(type(q[i]), q[i])
	for c in range(1,q[i]):
		if flag[c] == 0:
			ln = ln + 1 
	flag[q[i]] = 1
	ord2 = ord2 + ln * math.factorial(n-1-i)
#	print(ln,  math.factorial(ln))
ord2 = ord2+1 
#print(ord2)

print(abs(ord2 - ord1))

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

Submission status: Accepted

Solution code:

import math
import itertools
import statistics
#import numpy as np
import collections

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
s = []
for i in range(1,n+1):
    s.append(i)

l = list(itertools.permutations(s, n))
l.sort()

start = -1
end = -1
for i in range(len(l)):
    if l[i]==tuple(p):
        start = i+1
    if l[i]==tuple(q):
        end = i+1

print(abs(end-start))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p_li = tuple(map(int,input().split()))
q_li = tuple(map(int,input().split()))

n_number = [i for i in range(1,n+1)]
permutations_number = itertools.permutations(n_number)

check_li = []

for number in permutations_number:
    check_li += [number]

check_li.sort()

position_1 = 0
position_2 = 0

for x in range(len(check_li)):
    if check_li[x] == p_li:
        position_1 = x
    if check_li[x] == q_li:
        position_2 = x
        
print(abs(position_1 - position_2))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
numbers1k=[]
numbers2k=[]
s=[]
for i in range(n):
    s.append(i+1)
p=itertools.permutations(s,n)
u=list(p)
#print(u)
numbers1=input().rstrip().split(" ")
for num1 in numbers1:
    numbers1k.append(int(num1))
numbers2=input().rstrip().split(" ")
for num2 in numbers2:
    numbers2k.append(int(num2))
com1=tuple(numbers1k)
com2=tuple(numbers2k)
for i in range(len(u)):
    if u[i]==com1:
        a=i+1
for j in range(len(u)):
    if u[j]==com2:
        b=j+1
#print(a)
#print(b)
print(abs(b-a))

Submission status: Accepted

Solution code:
from itertools import permutations
import math

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)
#print(A)

B = []
for i in permutations(A,N):
    B.append(list(i))
#print(B)

a = b = 0
for i in range(len(B)):
    if P == Q:
        break
    elif B[i] == P:
        a = i
    elif B[i] == 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()))

for idx, perm in enumerate(permutations(range(1, n+1))):
    # perm は intからなるタプル
    res = True
    for ip in range(n):
        if p[ip] != perm[ip]:
            res = False
    if res:
        idx_p = idx
    res = True
    for iq in range(n):
        if q[iq] != perm[iq]:
            res = False
    if res:
        idx_q = idx

print(abs(idx_p - idx_q))

Submission status: Accepted

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

import itertools

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

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

cnt = 0
for i in itertools.permutations(li):
    if list(i) == P:
        a = cnt
    if list(i) == Q:
        b = cnt
    cnt += 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()))

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


for pos, v in enumerate(itertools.permutations(gen)):
    for i, comp in enumerate(v):
        if P[i] == comp:
            continue
        else:
            break
    else:
        Pp = pos + 1

    for i, comp in enumerate(v):
        if Q[i] == comp:
            continue
        else:
            break
    else:
        Qp = pos + 1

print(abs(Pp - Qp))

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

Submission status: Accepted

Solution code:
import itertools
import math


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

check = list(itertools.permutations(range(1,N+1)))
N_fac = math.factorial(N)

n1 = 0
n2 = 0
countP = 0
countQ = 0
for i in range(0,N_fac):
  for j in range(0,N):
    if(check[i][j] == P[j]):
      countP += 1
      if(countP == N):
        n1 = i
    if(check[i][j] == Q[j]):
      countQ += 1
      if(countQ == N):
        n2 = i
  countP = 0
  countQ = 0
        
num = n1-n2
print(abs(num))

Submission status: Accepted

Solution code:
from itertools import permutations
import math

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

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

lst = []
lstn = ['1', '2', '3', '4', '5', '6', '7', '8']

for i in permutations(lstn[:N]):
    lst.append(int(''.join(i)))
lst.sort()

for i in range(0, math.factorial(N)):
    if lst[i] == np: np = i
    if lst[i] == nq: nq = i

print(abs(np - nq))

Submission status: Accepted

Solution code:
from itertools import permutations as perm

N = int(input())
test = list(perm(range(1, N+1), N))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

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

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


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

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
cntp = 0
cntq = 0
ALL = list(itertools.permutations(range(1, N+1), N))

for i in range(len(ALL)):
    if ALL[i]==P:
        break
    elif ALL[i]!=P:
        cntp += 1
for i in range(len(ALL)):
    if ALL[i]==Q:
        break
    elif ALL[i]!=Q:
        cntq += 1

print(abs(cntq-cntp))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc150/tasks/abc150_c

import itertools

if __name__ == "__main__":
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))

    A = [i for i in range(1, N+1)]
    S = set()
    a = 0
    b = 0
    for i, permutated in enumerate(itertools.permutations(A)):
        if permutated == P:
            a = i
        if permutated == Q:
            b = i

    print(abs(a-b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations


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

    permutation_li = list(permutations([i for i in range(1, length + 1)]))

    p_index = permutation_li.index(p)
    q_index = permutation_li.index(q)

    print(abs(p_index - q_index))


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, v in enumerate(permutations(range(1, N + 1), N)):
    if v == P:
        a = i
    if v == Q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
visit = []
p_num = 1
q_num = 1
for i in range(n):
  visit.append(p[i])
  cnt = 0
  for j in range(p[i]):
    if j not in visit:
      cnt += 1
  p_num += math.factorial(n-i-1)*cnt
visit = []
for i in range(n):
  visit.append(q[i])
  cnt = 0
  for j in range(q[i]):
    if j not in visit:
      cnt += 1
  q_num += math.factorial(n-i-1)*cnt
print(abs(q_num-p_num))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = input().replace(" ", "")
Q = input().replace(" ", "")

nums = []
for x in permutations(map(str, range(1, N + 1)), N):
    nums.append("".join(x))
nums.sort()
ans = abs(nums.index(Q) - nums.index(P))
print(ans)

Submission status: Accepted

Solution code:
import itertools
def resolve():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    i, a, b = 0, 0, 0
    for p in itertools.permutations(range(1, N+1)):
        i += 1
        if P == Q:
            a = b = 0
            break
        elif p == P:
            a = i
        elif p == Q:
            b = i

    print(abs(a-b))

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from itertools import permutations

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

p_index = 0
q_index = 0
for i, perm in enumerate(permutations(range(1, n+1))):
    if perm == p:
        p_index = i
    if perm == q:
        q_index = i
print(abs(p_index - q_index))

Submission status: Accepted

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

lists = []

def dfs(li, nums):
    if len(nums) == 0:
        lists.append(li)
        return
    for num in nums:
        new = li[::]
        new.append(num)
        new_nums = nums[::]
        new_nums.remove(num)
        dfs(new, new_nums)
dfs([], [i for i in range(1, N+1)])

p_idx = 0
q_idx = 0

for i in range(len(lists)):
    li = lists[i]
    if P == li:
        p_idx = i
    if Q == li:
        q_idx = i
print(abs(p_idx - q_idx))

Submission status: Accepted

Solution code:
n = int(input())
list_p = list(map(int,input().split()))
list_q = list(map(int,input().split()))
list1 = sorted(list_p)
list2 = sorted(list_q)

import math

lista=[]
listb=[]

for i in range(n-1):
    lista.append((list1.index(list_p[i]))*math.factorial(n-i-1))
    listb.append((list2.index(list_q[i]))*math.factorial(n-i-1))
    list1.remove(list_p[i])
    list2.remove(list_q[i])
    
print(abs(sum(lista)-sum(listb)))

Submission status: Accepted

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

fac = [0] * 8
fac[1] = 1
for i in range(2, N):
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))
for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[i])

print(abs(a - b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

n_l = [str(i) for i in range(1,N+1)]
N_lists = [''.join(i) for i in itertools.permutations(n_l)]

index_P = N_lists.index(P)
index_Q = N_lists.index(Q)

ans = abs(index_P - index_Q)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

def f(A):
  l = list(range(1,N+1,1))
  a = 0
  for i in range(N):
    a += math.factorial(N-1-i) * (l.index(A[i])-1)
    del l[l.index(A[i])]
  return a

print(abs(f(P)-f(Q)))

Submission status: Accepted

Solution code:
import itertools
import math
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
juretu=list(range(1,N+1))
so = math.factorial(N)
p=[]
for v in itertools.permutations(juretu, N):
  p.append(list(v))

P_v = [False,0]
Q_v = [False,0]
for i,v in enumerate(p):
  if P == v:
    P_v = [True,i+1]
  if Q == v:
    Q_v = [True,i+1]
  if P_v[0] and Q_v[0]:
    print(abs(P_v[1] - Q_v[1]))
    break

Submission status: Accepted

Solution code:
import itertools as it

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

L=[int(i) for i in range(1,N+1)]
A=[]
for v in it.permutations(L):
    A.append(v)

a=0
for i in range(len(A)):
    flag=True
    a=a+1
    for j in range(N):
        if A[i][j]!=P[j]:
            flag=False
            break
    if flag:
        break
b=0
for i in range(len(A)):
    flag=True
    b=b+1
    for j in range(N):
        if A[i][j]!=Q[j]:
            flag=False
            break
    if flag:
        break

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools


def main():
    n = input()
    b = input().split()
    c = input().split()

    b_comb = sorted(list(itertools.permutations(b)))
    c_comb = sorted(list(itertools.permutations(c)))
    for i in range(len(b_comb)):
        if b_comb[i] == tuple(b):
            p = i + 1
            break
    for i in range(len(c_comb)):
        if c_comb[i] == tuple(c):
            q = i + 1
            break 
    print(abs(p - q))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
a = [i+1 for i in range(n)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))

r = list(itertools.permutations(a,len(a)))
r.sort()

for i in range(len(r)):
    if list(r[i]) == p:
        x = i
    if list(r[i]) == q:
        y = i
        
print(abs(x-y))

Submission status: Accepted

Solution code:
import itertools
import math

n=int(input())
lis=[i for i in range(1,n+1)]
P_lis=itertools.permutations(lis)
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))

P=list(P_lis)
P=sorted(P)
#print(P)
#print(a,P.index(a))
#print(b,P.index(b))
print(abs(P.index(a)-P.index(b)))

Submission status: Accepted

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

import sys


def input():
    return sys.stdin.readline()


def decide_position(num_list:list, n):

    if n == 2:
        if num_list[0] < num_list[1]:
            return 1
        else:
            return 2
    else:
        first_num = num_list[0]
        sorted_num_list = sorted(num_list)
        rank_in_p = sorted_num_list.index(first_num)
        n_kaizyou = 1
        for i in range(n-1, 0, -1):
            n_kaizyou *= i

        position = rank_in_p * n_kaizyou
        num_list.remove(first_num)
        position += decide_position(num_list, n-1)

        return position



def resolve():
    n = int(input())
    p_list = list(map(int, input().split()))
    q_list = list(map(int, input().split()))

    p_rank = decide_position(p_list, n)
    q_rank = decide_position(q_list, n)

    ans_rank = abs(p_rank - q_rank)

    print(ans_rank)






if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import itertools

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

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

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

Submission status: Accepted

Solution code:
import itertools

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

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

Submission status: Accepted

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

pi = 0
qi = 0
for i, l in enumerate(list(permutations(list(range(1, n+1))))):
    l = list(l)
    if p == l:
        pi = i
    if q == l:
        qi = i
print(abs(qi - pi))

Submission status: Accepted

Solution code:
from itertools import permutations
 
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
 
i = 0
a = 0
b = 0
for s in permutations(range(1, n+1)):
    for j in range(n):
        if p[j] != s[j]:
            break
    else:
        a = i
    for j in range(n):
        if q[j] != s[j]:
            break
    else:
        b = i
    i += 1
 
print(abs(a-b))

Submission status: Accepted

Solution code:

import itertools
N = int(input())
P = input()
Q = input()

P = int("".join(P.split(" ")))
Q = int("".join(Q.split(" ")))

X = []
for i in itertools.permutations(range(1, N+1)):
    x = list(map(str, i))
    y = int("".join(x))
    X.append(y)

a = X.index(P)
b = X.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()))

for i, v in enumerate(itertools.permutations(range(1,n+1))):
    if p==v:
        s=i
    if q==v:
        t=i
print(abs(s-t))

Submission status: Accepted

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

from itertools import permutations
dict_num=[]
for i in permutations(range(1,n+1)):
    dict_num.append(i)

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

Submission status: Accepted

Solution code:
from itertools import permutations

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

n = list(permutations(list(range(1, N+1))))
for i in range(len(n)):
    if  list(n[i]) == P:
        p = i+1
    if list(n[i]) == Q:
        q = i+1
print(abs(p-q))

Submission status: Accepted

Solution code:
from itertools import permutations as perm


def inpl():
    return tuple(map(int, input().split()))


N = int(input())
P = inpl()
Q = inpl()

for i, p in enumerate(perm([j+1 for j in range(N)])):
    if p == P:
        pi = i
    if p == Q:
        qi = i
print(abs(pi-qi))

Submission status: Accepted

Solution code:
import math

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

def f(perm, ord):
    n = len(perm)
    if n == 2:
        if perm == ord:
            return 1
        else:
            return 2
    else:
        i = ord.index(perm[0])
        ord.pop(i)
        return math.factorial(n-1)*i + f(perm[1:], ord)

print(abs(f(Q, [i+1 for i in range(N)]) - f(P, [i+1 for i in range(N)])))

Submission status: Accepted

Solution code:
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
cnt1 = 0
cnt2 = 0
for i in range(n-1):
  cnt = 0
  for j in range(i+1,n):
    if p[i] > p[j]:
      cnt += 1
  cnt1 += cnt*math.factorial(n-(i+1))

for i in range(n-1):
  cnt = 0
  for j in range(i+1,n):
    if q[i] > q[j]:
      cnt += 1
  cnt2 += cnt*math.factorial(n-(i+1))

print(abs(cnt1 - cnt2))

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()))
P = tuple(p)
Q = tuple(q)
A = [v for v in itertools.permutations(L,N)]
for i in range(len(A)):
  if A[i] == P:
    ans1 = i+1
  if A[i] == Q:
    ans2 = i+1
print(abs(ans1-ans2))

Submission status: Accepted

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

l = [str(i) for i in range(1,n+1)]
p_list = list(itertools.permutations(l,len(l)))

p_str = [str(i) for i in p]
q_str = [str(i) for i in q]

a = p_list.index(tuple(p_str))
b = p_list.index(tuple(q_str))

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

iter = [i + 1 for i in range(N)]
map = {pattern: i for i, pattern in enumerate(permutations(iter, N))}
print(abs(map[P] - map[Q]))

Submission status: Accepted

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

import itertools

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

l=list(itertools.permutations(seq))
l.sort()

p=tuple(pl)
q=tuple(ql)

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