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:
def factorial(n):
  if n in [0, 1]:
    return 1
  else:
    return factorial(n - 1) * n

def rank_of_list(A):
  rank = 0
  for i in range(len(A)):
    rank += (A[i] - 1) * factorial(len(A) - i - 1)
    A = list(map(lambda a: a - 1 if a > A[i] else a, A))
  return rank

def main():
  N = int(input())
  P = list(map(lambda p: int(p), input().split(" ")))
  Q = list(map(lambda q: int(q), input().split(" ")))
  print(abs(rank_of_list(P) - rank_of_list(Q)))

main()

Submission status: Accepted

Solution code:
import math
from itertools import permutations

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

ls = permutations(range(1, N+1), N)

Ns = []

for l in ls:
    s = ''.join([str(n) for n in l])
    Ns.append(int(s))

p = ''.join(map(str, P))
q = ''.join(map(str, Q))

a = Ns.index(int(p)) + 1
b = Ns.index(int(q)) + 1

print(abs(a-b))

Submission status: Accepted

Solution code:
import math
n = int(input())
def pos_num(l):
    l_sort = l.copy()
    l_len = len(l)
    l_sort.sort()
    if l_len == 1:
        an = 0
    else:
        #print(l_sort.index(l[0])+1)
        an = (l_sort.index(l[0]))*math.factorial(l_len-1)
    #print(an)
    return an 

    

ls = list(map(int, input().split()))
an = 0
for i in range(n-1):
    b = pos_num(ls[i:])
    an += b
    #print(an)
    

ll = list(map(int, input().split()))
bn = 0
for i in range(n-1):
    b = pos_num(ll[i:])
    bn += b
    #print(bn)

print(abs(an-bn))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
a=[]
for i in range(n):
    a.append(i+1)
b=[]
for per in itertools.permutations(a):
    b.append(list(per))
c=b.index(list(map(int,input().split())))
d=b.index(list(map(int,input().split())))
print(abs(c-d))

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
p = LIST()
q = LIST()
count = 0
for x in permutations(range(1, n+1), n):
    count += 1
    if all( x[j] == p[j] for j in range(n)):
        a = count
    if all( x[j] == q[j] for j in range(n)):
        b = count
print(abs(a-b))

Submission status: Accepted

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

jun = []
a = [(k+1) for k in range(N)]
def make(now):
  if len(now) == N:
    jun.append(now)
    return 0
  for item in a:
    if item not in now:
      make(now+ [item])
make([])

for k in range(len(jun)):
  if jun[k] == P:
    p = k
  if jun[k] == Q:
    q = k
  if p >=0 and q >=0:
    break
print(abs(p-q))

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    P = tuple(NLI())
    Q = tuple(NLI())
    
    ls = [x for x in range(1,N+1)]        

    p_list = list(itertools.permutations(ls))
    
    a,b = 0,0
    for x in range(len(p_list)):
        if P == p_list[x]:
            a = x
        if Q == p_list[x]:
            b = x
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

def same(l1, l2, n):
    for i in range(n):
        if l1[i] != l2[i]:
            return False
    return True

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

P = [p-1 for p in P]
Q = [q-1 for q in Q]

for i,pattern in enumerate(permutations(range(N))):
    if same(P, pattern, N):
        i_p = i
    if same(Q, pattern, N):
        i_q = i

print(abs(i_p - i_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()))

for i,j in enumerate(permutations(range(1, n+1))):
    if j == p:
        a = i
        
    if j == q:
        b = i
        
print(abs(a-b))

Submission status: Accepted

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

def fact(n):
    #階乗を求める
    res = 1
    for i in range(1, n+1):
        res *= i
    return res

a = 0
#aについて
f = [False] * N
for i in range(N):
    a += (P[i]-1-f[:P[i]-1].count(True)) * fact(N-i-1) + 1
    f[P[i]-1] = True

b = 0
#bについて
f = [False] * N
for i in range(N):
    b += (Q[i]-1-f[:Q[i]-1].count(True)) * fact(N-i-1) + 1
    f[Q[i]-1] = True

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

x = [tuple(x) for x in itertools.permutations(range(1, N + 1), N)]
x.sort()
a = x.index(P)
b = x.index(Q)
ans = abs(a - b)
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

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

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

N = I()
P = tuple(LI())
Q = tuple(LI())

ls = list(itertools.permutations(range(1,N+1)))
print(abs(ls.index(P) - ls.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()))
for i, t in enumerate(permutations(range(1, N+1))):
    if t == P:
        p = i
    if t == Q:
        q = i
print(abs(p-q))

Submission status: Accepted

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

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


P = sorted(p)
P = list(itertools.permutations(P))

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

array=[]

for i in range(1,n+1):
	array.append(i)

z=list(permutations(array,n))

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

print(abs((a+1)-(b+1)))

Submission status: Accepted

Solution code:
import itertools

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

cnt = 0
p_cnt = 0
q_cnt = 0

for a in itertools.permutations(range(n)): #順列を生成して、それらに対して処理
    cnt += 1
    for i in range(len(a)):
        if a[i]+1 != p_list[i]:
            break
        if i == len(a)-1:
            p_cnt = cnt
    for j in range(len(a)):
        if a[j]+1 != q_list[j]:
            break
        if j == len(a)-1:
            q_cnt = cnt

print(abs(p_cnt - q_cnt))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
import math
import time
import statistics
import math
import itertools

# a = get_int()
def get_int():
    return int(input())
# a = get_string()
def get_string():
    return input()
# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
    return input().split()
# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))
# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():
    start = time.time()
    n = get_int()
    p = get_int_list()
    q = get_int_list()

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

    count = 0
    count_p = 0
    count_q = 0
    for keiro in list(itertools.permutations(a)):
        if list(keiro) == p:
            count_p = count
        if list(keiro) == q:
            count_q = count
        count += 1

    print(abs(count_p - count_q))


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

Submission status: Accepted

Solution code:
# ABC150C
"""
大きさ N の順列 ((1, 2, ..., N) を並び替えてできる数列) P, Qがあります。
大きさ Nの順列は N! 通り考えられます。このうち、P が辞書順で a 番目に小さく、Q が辞書順で b 番目に小さいとして、|a−b|を求めてください。
2つの数列 X, Y について、ある整数 k が存在して Xi=Yi(1≤i<k) かつ Xk<Yk が成り立つとき、X は Y より辞書順で小さいと定義されます。
"""

from itertools import permutations 

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations

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

s = sorted(permutations(str(P)))
pi = s.index(tuple(str(P)))
qi = s.index(tuple(str(Q)))

print(abs(pi - qi))

Submission status: Accepted

Solution code:
# ABC_150_C_Count_Order.py

from itertools import permutations

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

pp = sorted(p)
qq = sorted(q)
P = list(permutations(pp))
Q = list(permutations(qq))
# print(P, Q)

a=P.index(tuple(p))
b=Q.index(tuple(q))
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
l = []
a = 0
b = 0
for i in itertools.permutations(range(1,N+1)):
    l.append(list(i))

for j in l:
    if j == P:
        a = l.index(j)+1
    if j == Q:
        b = l.index(j)+1
print(abs(a-b))

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
all_order={}
def dfs(a):
  if len(a)==n:
    all_order[tuple(a)]=len(all_order.keys())+1
  else:
    for nv in range(n):
      nv+=1
      if nv in a:continue
      dfs(a+[nv])
dfs([])
print(abs(all_order[tuple(p)]-all_order[tuple(q)]))

Submission status: Accepted

Solution code:
import math
n = int(input())
p_ls = list(map(int, input().split()))
q_ls = list(map(int, input().split()))

def dict_num(ls):
    m = len(ls)
    ans = 0
    already = []
    for i in range(m):
        top = ls[i]
        for j in range(1,top):
            if j not in already:
                ans += math.factorial(m-i-1)
        already.append(top)
    return ans+1
print(abs(dict_num(q_ls)-dict_num(p_ls)))

Submission status: Accepted

Solution code:
from collections import deque

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

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

while que:
    seq = que.popleft()
    if len(seq) == N:
        dic.append(seq)
        continue
    else:
        for i in range(1, N+1):
            if i in seq:
                continue
            seq_next = seq + [i]
            que.append(seq_next)

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

Submission status: Accepted

Solution code:

import itertools

def solve():
    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 = tuple(itertools.permutations(l))
    P_index = p.index(P)
    Q_index = p.index(Q)
    print(abs(Q_index - P_index))


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import itertools

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

count = 1
for p in itertools.permutations(range(1, N+1)):

    if p == P:
        count_P = count
    if p == Q:
        count_Q = count

    count += 1

ans = abs(count_P - count_Q)
print(ans)

Submission status: Accepted

Solution code:
def getN(P,i):
    n=0
    P1=sorted(P)
    for p in range(i-1):
        n *=(i-p)
        n += (P1.index(P[p]))
        P1.remove(P[p])
    return n

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

p = getN(P,n)
q = getN(Q,n)

print(abs(p-q))

Submission status: Accepted

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

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
d = list(itertools.permutations(range(1, n + 1)))
d.sort()

a = -1
b = -1
for i in range(len(d)):
    if d[i] == p:
        a = i
    if d[i] == q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix
# CreatedDate:  2020-08-08 21:55:54 +0900
# LastModified: 2020-08-08 22:01:55 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from itertools import permutations


def main():
    n = int(input())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    permu = list(map(list, permutations(range(1, n+1))))
    for index, per in enumerate(permu):
        if per == p:
            a = index
        if per == q:
            b = index
    print(abs(a-b))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math


def resolve():
    import sys
    input = sys.stdin.readline
    # row = [int(x) for x in input().rstrip().split(" ")]
    # n = int(input().rstrip())
    n = int(input().rstrip())

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

    import itertools

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

    for i, v in enumerate(perm):
        if v == p:
            a = i
        if v == q:
            b = i

    print(abs(a-b))



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
a = list()
for i in range(1,n+1):
    a.append(i)
b = list(input().split())
c = list(input().split())
for i in range(0,n):
    b[i]=int(b[i])
    c[i]=int(c[i])
cnt = 1
for i in itertools.permutations(a):
    #print(i)
    if(tuple(b)==i):
        cnta = cnt
        #print('p is '+str(cnta))
    if(tuple(c)==i):
        cntb = cnt
        #print('q is '+str(cntb))
    cnt +=1
print(abs(cnta-cntb))

Submission status: Accepted

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

l = [int(x) for x in range(1,n+1)]
i = 0
p_i = 0
q_i = 0
for v in itertools.permutations(l, len(l)):
    i += 1
    if p==list(v):
        p_i = i
        #print(list(v))
    if q==list(v):
        q_i = i
        #print(i,list(v))
print(abs(p_i-q_i))

Submission status: Accepted

Solution code:
from itertools import permutations

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

perm = list(permutations(sorted(p), n))    #; print(perm)
a = perm.index(tuple(p))
b = perm.index(tuple(q))

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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)]
    p = list(itertools.permutations(l, N))

    print(abs(p.index(P) - p.index(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()))
r = 0
for i, x in enumerate(permutations([j for j in range(1,N+1)])):
    if x == P:
        r = abs(r-i)
    if x == Q:
        r = abs(r-i)
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

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

Submission status: Accepted

Solution code:
import itertools
N=int(input())
ListP = tuple(map(int, input().split()))
ListQ = tuple(map(int, input().split()))
s_l = set(ListP)
Per = list(itertools.permutations(s_l))
Per.sort()
mid1 = 0
mid2 = 0
for i in range(len(Per)):
  if Per[i] == ListP:
    mid1 = i+1
  if Per[i] == ListQ:
    mid2 = i+1
res = abs(mid1 -mid2)
print(res)

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
target= range(1,n+1)
p_len=0
q_len=0
for i,v in enumerate(itertools.permutations(target,n)):
    tmp=list(v)
    if p==tmp:
        p_len=i
    if q==tmp:
        q_len=i
print(abs(p_len-q_len))

Submission status: Accepted

Solution code:
from math import factorial

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

orderP = 1 + (P[0]-1)*factorial(N-1)
orderQ = 1 + (Q[0]-1)*factorial(N-1)
for i in range(1,N):
  redP = 0
  redQ = 0
  for j in range(i):
    if P[j] < P[i]: redP += 1
    if Q[j] < Q[i]: redQ += 1
  orderP += (P[i]-redP-1)*factorial(N-i-1)
  orderQ += (Q[i]-redQ-1)*factorial(N-i-1)
print(abs(orderP - orderQ))

Submission status: Accepted

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

T = [''.join(map(str, a)) for a in sorted(list(itertools.permutations(range(1, N + 1))))]

P_i = 0
Q_i = 0
for i, t in enumerate(T):
    if t == P:
        P_i = i
    if t == Q:
        Q_i = i
ans = abs(P_i - Q_i)
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
x = [i+1 for i in range(n)]
x = list(map(str, x))
pt = [''.join(p) for p in permutations(x)]
a = pt.index(''.join(input().split()))
b = pt.index(''.join(input().split()))
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(list(range(1,N+1))))
print(abs(A.index(tuple(P))-A.index(tuple(Q))))

Submission status: Accepted

Solution code:
from itertools import permutations
from bisect import bisect

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = tuple(permutations(range(1, n + 1), n))
print(abs(bisect(l, p) - bisect(l, q)))

Submission status: Accepted

Solution code:
import itertools

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

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

cnt = 0
for v in itertools.permutations(l, len(l)):
    if p == list(v):
        pi = cnt
    if q == list(v):
        qi = cnt 
    cnt += 1

print(abs(pi-qi))

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline

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

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

i = 0
s = 0
t = 0
#print(p)
for v in itertools.permutations(l):
    #print(v)
    if v == p:
        s = i
    if v == q:
        t = i
    
    i += 1

print(abs(t - s))

Submission status: Accepted

Solution code:
N = int(input())

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

#  iの階乗
x = [1] * (N + 1)

for i in range(1, N + 1):
    x[i] = x[i - 1] * i
    

res1 = 1
res2 = 1

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

for p in P:
    for i in range(len(s1)):
        if p == s1[i]:
            s1.pop(i)
            t = i - 1
            break

    res1 += t * x[len(s1)]

for q in Q:
    for i in range(len(s2)):
        if q == s2[i]:
            s2.pop(i)
            t = i - 1
            break

    res2 += t * x[len(s2)]

print(abs(res1 - res2))

Submission status: Accepted

Solution code:
import itertools
import numpy as np
import math

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

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

lis = [x+1 for x in range(n)]

p_list = itertools.permutations(lis)

a, b = 0, 0
f_a, f_b = False, False
i = 0

for one_case in p_list:
  if list(one_case) == p:
    a = i
    i += 1
    f_a = True
  elif list(one_case) == q:
    b = i
    i += 1
    f_b = True
  else:
    i += 1
  if f_a and f_b:
    break
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()))

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

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = list(map(int, input().split()))
q = tuple(map(int, input().split()))
srt = sorted(p.copy())
cnt = 1
cnt1 = 0
cnt2 = 0
for i in itertools.permutations(srt, n):
    if i == tuple(p):
        cnt1 = cnt
    if i == q:
        cnt2 = cnt
    cnt += 1
print(abs(cnt1 - cnt2))

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

def main():
    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)))


if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = sorted(list(itertools.permutations(range(1,n+1), n)))
    ans = []
    for i in range(len(l)):
        if l[i] == p:
            ans.append(i)
        elif l[i] == q:
            ans.append(i)
    if len(ans) == 1:
        print(0)
    else:
        print(abs(ans[0]-ans[1]))







if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    a, b = None, None
    import itertools
    for i, vars in enumerate(itertools.permutations(range(1, N+1))):
        if a is None:
            for j in range(N):
                if vars[j] != P[j]:
                    break
            else:
                a = i
        if b is None:
            for j in range(N):
                if vars[j] != Q[j]:
                    break
            else:
                b = i
        if a is not None and b is not None:
            break
    print(abs(a-b))


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import math
def count(N,i,li):
    a = 1
    for t in range(N):
        if li[t] == i:
            return a
        a += 1

N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
li1 = [i+1 for i in range(N)]
li2 = [i+1 for i in range(N)]
x = N-1
a_num = math.factorial(x) * (a[0]-1)
li1.remove(a[0])
b_num = math.factorial(x) * (b[0]-1)
li2.remove(b[0])
x -= 1

for i in range(1,N):
    a_num += math.factorial(x)*(count(N-i,a[i],li1)-1)
    li1.remove(a[i])
    b_num += math.factorial(x)*(count(N-i,b[i],li2)-1)
    li2.remove(b[i])
    x-=1
print(abs((a_num+1)-(b_num+1)))

Submission status: Accepted

Solution code:
import itertools

n = int(input())

a = list(input().split())
b = list(input().split())
str_a = ''.join(a)
str_b = ''.join(b)
li = [x for x in range(1, n+1)]
map = map(str, li)

p = itertools.permutations(map)
sorted_list = []
for v in p:
    tmp = ''.join(v)
    sorted_list.append(tmp)
sorted_list.sort()

ai = 0
bi = 0
for v in range(len(sorted_list)):
    if str_a == sorted_list[v]:
        ai = v
    elif str_b == sorted_list[v]:
        bi = v
res = abs(ai-bi)
if a == b: res = 0
print(res)

Submission status: Accepted

Solution code:
import math
import itertools

N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
ls=[]
for i in range(1,N+1):
    ls.append(i)
ls2 = list(itertools.permutations(ls,N))
#print(ls2)
cnt1 = 0
cnt2 = 0
for j in range(math.factorial(N)):
    for i in range(N):
        #print(j,i,P[i],ls2[j][i])
        if P[i] == ls2[j][i]:
            cnt1 += 1
        if Q[i] == ls2[j][i]:
            cnt2 += 1
    #print("cnt1",cnt1,"cnt2",cnt2)
    if cnt1 == N:
        a = j
    if cnt2 == N:
        b = j
    cnt1 = 0
    cnt2 = 0
#print(a,b)
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
a,b = [tuple(map(int,input().split())) for _ in range(2)]
s = list(permutations(list(range(1,n+1))))
ans =abs(s.index(b) - s.index(a))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

import itertools
a = 0
b = 0
cnt = 0
for perm in itertools.permutations(range(1, N + 1)):
  cnt += 1
  if perm == P:
    a = cnt
  if perm == Q:
    b = cnt
    
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

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

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

parms = list(itertools.permutations(tmp, N))

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

Submission status: Accepted

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

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

now=0
for i in l:
  if i==p:
    a=now
  if i==q:
    b=now
  now+=1
print(abs(a-b))

Submission status: Accepted

Solution code:

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

res = [0, 0]
cnt = [0]


def dfs(x):
    if len(x) == N:
        if all(u == v for u, v in zip(P, x)):
            res[0] = cnt[0]
        if all(u == v for u, v in zip(Q, x)):
            res[1] = cnt[0]
        cnt[0] += 1
        return

    for i in range(1, N + 1):
        if i not in x:
            x.append(i)
            dfs(x)
            x.pop()


dfs([])
print(abs(res[0] - res[1]))

Submission status: Accepted

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

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

kai=1

for i in range(n):
    kai*=i+1

for i in range(kai):
    if all(p[w]==g[i][w] for w in range(n)):
        a=i+1
for i in range(kai):
    if all(q[w]==g[i][w] for w in range(n)):
        b=i+1
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations as per
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
PQ = list(per(list(range(1, N + 1))))

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

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
p = tuple(list(map(int,input().split())))
q = tuple(list(map(int,input().split())))
# <class 'itertools.combinations'>
count=0
l = []
for i in range(1,N+1):
  l.append(i)
for i,v in enumerate(itertools.permutations(l)):
  if p==v:
    a = i+1
  if q==v:
    b=i+1
    
print(abs(a-b))