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 solve():
    def backtrack(nums, cur_state, permutations):
        if not nums:
            permutations[' '.join(cur_state)] = len(permutations)
        for i in range(len(nums)):
            cur_state.append(nums[i])
            backtrack(nums[:i]+nums[i+1:], cur_state, permutations)
            cur_state.pop()

    num = int(input())
    P = str(input())
    Q = str(input())
    # print(num, P, Q)
    nums = [str(i) for i in list(range(1, num+1))]
    permutations = {}
    backtrack(nums, [], permutations)
    # print(permutations)
    print(abs(permutations[Q]-permutations[P]))


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

a = 0
b = 0
for i, n in enumerate(itertools.permutations(N)):
    if n == P:
        a = i
    if n == 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()))

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

#  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:
from itertools import permutations
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

l=[i+1 for i in range(n)]
perm = permutations(l)
perm=list(perm)
p=[]
for i in perm:
    k=(''.join(map(str,i)))
    p.append(int(k))
a=''.join(map(str,a))
b=''.join(map(str,b))
idx1=p.index(int(a))
idx2=p.index(int(b))
print(abs(idx1-idx2))

Submission status: Accepted

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


def main():

    input = sys.stdin.readline

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools

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

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

a = 0
b = 0
for i in range(len(p_list)):
  if p_list[i] == P:
    a = i+1
  if p_list[i] == Q:
    b = i+1

#print(a,b)
print(abs(a-b))

Submission status: Accepted

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

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:
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
import itertools

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




def num(b,t,n):
  c = 0
  for i in range(len(t)):
    if list(t[i]) != b:
      c += 1
    else:
      break
  return(c)


 
x = num(p,a,n)
y = num(q,a,n)
print(abs(x-y))

Submission status: Accepted

Solution code:
from itertools import permutations
 
N = int(input())
Nlist = [i for i in range(1, N+1)] 
Nlist = list(permutations(Nlist)) 
 
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
print(abs(Nlist.index(P) - Nlist.index(Q)))

Submission status: Accepted

Solution code:
import math

NN = int(input())
AA = list(map(int,input().split()))
BB = list(map(int,input().split()))


AA_converted = 0
BB_converted = 0
LL = list(range(1,NN+1))
for ii in range(0,NN):
    AA_converted += math.factorial(NN-1-ii)*LL.index(AA[ii])
    LL.remove(AA[ii])
#    print(AA_converted)
#    print(LL)

LL = list(range(1,NN+1))
for ii in range(0,NN):
    BB_converted += math.factorial(NN-1-ii)*LL.index(BB[ii])
    LL.remove(BB[ii])
#    print(BB_converted)
#    print(LL)

print(abs(AA_converted-BB_converted))

Submission status: Accepted

Solution code:
import itertools

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

Psort = sorted(P)
Pp = list(itertools.permutations(Psort))

a = Pp.index(P)
b = Pp.index(Q)

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
j = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
j_i, q_i = None, None
for i, t in enumerate(itertools.permutations(range(1,n+1), n), start=1):
    if t == j:
        j_i = i
    if t == q:
        q_i = i
    if j_i and q_i:
        break
print(abs(j_i - q_i))

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
s = list(map(int, input().split(' ')))
t = list(map(int, input().split(' ')))
hoge = list(itertools.permutations(range(1, n+1)))
p = 0
q = 0
for i in range(len(hoge)):
  if list(hoge[i]) == s:
    p = i
  if list(hoge[i]) == t:
    q = i
print(abs(p - q))

Submission status: Accepted

Solution code:
from itertools import permutations


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

    A = [i for i in range(1, N+1)]
    As = list(permutations(A))
    for i in range(len(As)):
        a = As[i]
        for j in range(len(a)):
            if a[j] != P[j]:
                break
        else:
            p_rank = i
            break

    for i in range(len(As)):
        a = As[i]
        for j in range(len(a)):
            if a[j] != Q[j]:
                break
        else:
            q_rank = i
            break
    print(abs(p_rank-q_rank))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 入力
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

# ライブラリ
import itertools
l = [i+1 for i in range(n)]
perms = itertools.permutations(l, n)

# 処理
a = 0
b = 0
for i, perm in enumerate(perms):
    perm = list(perm)
    if perm == p:
        a = i
    if perm == q:
        b = i

import numpy as np
print(np.abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

seqs = []
for i, x in enumerate(permutations(range(1, N + 1), N)):
    if x == P:
        p = i + 1
    
    if x == Q:
        q = i + 1
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()))

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

Submission status: Accepted

Solution code:
import os, sys, re, math
import itertools

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

nums = [n for n in range(1, N + 1)]
patterns = itertools.permutations(nums, N)
i = 0
for pattern in patterns:
    if pattern == P:
        pi = i
    if pattern == Q:
        qi = i
    i += 1

print(abs(pi - qi))

Submission status: Accepted

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

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

a=None
b=None
for i, x in enumerate(X):
    if list(x) == p:
        a=i
        
    if list(x) == q:
        b=i
        
print(abs(a-b))

Submission status: Accepted

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

pos = [None, None]


def fn(digits, maxlen, index, pos):
    for x in range(1, maxlen + 1):
        if x in digits:
            continue
        ndigits = digits + [x]
        if len(ndigits) < maxlen:
            index = fn(digits + [x], maxlen, index, pos)
        else:
            if ndigits == p:
                pos[0] = index
            if ndigits == q:
                pos[1] = index
            index = index + 1
            # print(f"{index}:" + " ".join(str(x) for x in ndigits))
    return index


fn([], n, 0, pos)
print(abs(pos[0] - pos[1]))

Submission status: Accepted

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

# 順列のリスト
pmt = list(itertools.permutations(range(1, N + 1), N))

a = 0
b = 0

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

Submission status: Accepted

Solution code:
N=int(input())
P= list(map(int, input().split()))
Q= list(map(int, input().split()))
if P==Q:
  print(0)
  exit()
dic=[]
Pn=0
Qn=0
import itertools
import numpy
l=numpy.arange(1,N+1,1,int)
for v in itertools.permutations(l, N):
  dic.append(v)
for i in range(len(dic)):
  if list(dic[i])==P:
    Pn=i
  elif list(dic[i])==Q:
    Qn=i
  elif Pn!=0 and Qn!=0:
    break
print(abs(Pn-Qn))

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,p in enumerate(permutations(range(1,N+1))):
    if p == P:
        pi = i
    if p == Q:
        qi = i

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

order = list(permutations(list(range(1,n+1))))
turn_p = order.index(p)
turn_q = order.index(q)

print(abs(turn_p-turn_q))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
p = list(map(int,input().split()))
z = list(map(int,input().split()))
p_n = 0
q_n = 0
for i in range(len(p)):
    if i == 0:
        p_n = p[i]
        q_n = z[i]
    else:
        p_n = p[i]+(p_n*10)
        q_n = z[i]+(q_n*10)


q = deque()
ans_list = []
n_list = [0 for i in range(n)]


for i in range(n):
    n_list[i] = 1
    tmp = []
    for j in n_list:
        tmp.append(j)
    q.append([i+1,tmp])
    n_list[i] = 0

while len(q) != 0:
    tmp = q.popleft()
    s = str(tmp[0])
    if len(s) == n:
        ans_list.append(tmp[0])
        continue
    for i in range(n):
        tmp_n = tmp[0]
        if tmp[1][i] == 0:
            tmp_l = []
            for j in range(n):
                tmp_l.append(tmp[1][j])
            tmp_l[i] = 1
            q.append([(i+1)+tmp_n*10,tmp_l])

ans_list = sorted(ans_list)

p_i = 0
q_i = 0

for i in range(len(ans_list)):
    if ans_list[i] == p_n:
        p_i = i
    if ans_list[i] == q_n:
        q_i = i

print(abs(p_i-q_i))

Submission status: Accepted

Solution code:
from itertools import *
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
dic = dict()
i = 0
for a in permutations(range(1,N+1),N):
    dic[a] = i
    i += 1
print(abs(dic[P]-dic[Q]))

Submission status: Accepted

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

all_num_list = list(it.permutations(range(1,N+1)))
all_num_list.sort()

P_num = all_num_list.index(P)
Q_num = all_num_list.index(Q)

print (abs(P_num-Q_num))

Submission status: Accepted

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

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

num_p = 0
num_q = 0
for i in range(n-1) :
    sort_p = sorted(p[i:],reverse=True)
    index_p = sort_p.index(p[i])
    mp = n - (index_p + 1)
    num_p += mp * factorial(n-(i+1))

    sort_q = sorted(q[i:],reverse = True)
    index_q = sort_q.index(q[i])
    mq = n - (index_q + 1)
    num_q += mq * factorial(n-(i+1))

print(abs(num_p - num_q))

Submission status: Accepted

Solution code:
from itertools import permutations
X = [i for i in range(1, int(input())+1)] 
X = list(permutations(X))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
print(abs(X.index(P)-X.index(Q)))

Submission status: Accepted

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

import itertools

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

cnt = 0
for i in li2:
    cnt +=1
    if i == p:
        a = cnt
    if i == q:
        b = cnt

print(abs(a-b))

Submission status: Accepted

Solution code:
bit=[0]*9
fa=[1]*9
for n in range(1,9):fa[n]=fa[n-1]*n
def up(id):
    while id<9:
        bit[id]+=1
        id+=id&(-id)
def qr(id):
    res=0
    while id:
        res+=bit[id]
        id-=id&(-id)
    return res

x=int(input())
s=list(map(int,input().split()))
z=list(map(int,input().split()))

v=''

for n in s:
    a=qr(n)
    vl=n-1-a
    v+=str(vl)
    up(n)
v=v[::-1]
r1=0
for n in range(len(v)):
    r1+=int(v[n])*fa[n]

r2=0
v=''
bit=[0]*9
for n in z:
    a=qr(n)
    vl=n-1-a
    v+=str(vl)
    up(n)
v=v[::-1]
for n in range(len(v)):
    r2+=int(v[n])*fa[n]

print(abs(r1-r2))

Submission status: Accepted

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

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

cases = list(itertools.permutations(list(range(1, n + 1))))
#print(cases)
count = 0
for case in cases:
  count += 1
  if p == case:
    p_c = count
  if q == case:
    q_c = count
print(abs(p_c - q_c))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
*p,=map(int,input().split())
*q,=map(int,input().split())
if p==q:
    print(0)
else:
    import itertools
    l2=[]
    for i in itertools.permutations([i+1 for i in range(n)]):
        l2.append(list(i))
    print(abs(l2.index(p)-l2.index(q)))

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

s = list(itertools.permutations(n_li, len(n_li)))
s.sort()

p = 0
q = 0

for i in range(len(s)):
    if s[i] == p_li:
        p = i
    if s[i] == q_li:
        q = i

print(abs(p-q))

Submission status: Accepted

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

Submission status: Accepted

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

r = [i+1 for i in range(n)]
from itertools import permutations

count = 0
a = 0
b = 0
for rr in list(permutations(r)):
    count +=1
    if p == rr:
        a = count
    if q == rr:
        b = count

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
import math
import sys

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

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

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

li = list(itertools.permutations(x))

for i in range(len(li)):
    if list(li[i]) == p:
        a = i
    elif list(li[i]) == q:
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p = ''.join([x for x in input().split()])
q = ''.join([x for x in input().split()])

a = [str(x) for x in range(1, n+1)]
a = [''.join(list(x)) for x in permutations(a)]

p_idx = -1
q_idx = -1
for i, x in enumerate(a):
  if p == x:
    p_idx = i
  if q == x:
    q_idx = i

print(abs(p_idx - q_idx))

Submission status: Accepted

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

nP = 0
nQ = 0
xs = []
def suretsu(x):
    if len(x) == N:
        xs.append(x)
        return
    else:
        for n in range(1, N+1):
            if n not in x:
                new_x = copy.deepcopy(x)
                new_x.append(n)
                suretsu(new_x)

suretsu([])
for i, x in enumerate(xs):
    is_P = True
    is_Q = True
    for j in range(N):
        if P[j]!=x[j]:
            is_P = False
        if Q[j]!=x[j]:
            is_Q = False
    if is_P:
        nP = i+1
    if is_Q:
        nQ = i+1

print(abs(nP-nQ))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
S = sorted(P)
for i, C in enumerate(permutations(S)):
    if C == P:
        a = i
    if C == Q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

N = []
[N.append(num) for num in range(1, int(input())+1)]
Pn = tuple(map(int, input().split()))
Qn = tuple(map(int, input().split()))

permutation = []
for x in itertools.permutations(N):
    permutation.append(x)

print(abs(permutation.index(Pn) - permutation.index(Qn)))

Submission status: Accepted

Solution code:
n = int(input())
#k, x = map(int, input().split())
pl = list(map(str, input().split())); p = int("".join(pl))
ql = list(map(str, input().split())); q = int("".join(ql))
#l=[list(map(int,input().split())) for i in range(n)]
import itertools
l = [str(i) for i in range(1, n+1)]
a, b = 0, 0
for v in list(itertools.permutations(l)):
    num = int("".join(list(v)))
    if p >= num:
        a += 1
    if q >= num:
        b += 1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

N=int(input())
P=int("".join(list(input().split())))
Q=int("".join(list(input().split())))
L=list(itertools.permutations(map(str, range(1,N+1)),N))
for i in range(len(L)):
  L[i]=int("".join(L[i]))
sorted(L)
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()))

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

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import itertools

n = int(stdin.readline().rstrip())
p = [x for x in stdin.readline().rstrip().split()]
q = [x for x in stdin.readline().rstrip().split()]

x = ()
y = ()
for pp in p:
    x = x + (pp,)
for qq in q:
    y = y + (qq,)

c = []
for i in itertools.permutations([str(j) for j in range(1,n+1)]):
    c.append(i)
    
a = c.index(x)
b = c.index(y)

print (abs(b-a))

Submission status: Accepted

Solution code:
import sys
import time
import math
import itertools as it
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------

N = int(input())
P = inpl()
Q = inpl()
ls = P.copy()
ls.sort()
cnt = 0
a = 0
b = 0
for l in it.permutations(ls):
    cnt += 1
    l = list(l)
    if P == l:
        a = cnt
    if Q == l:
        b = cnt
print(abs(a - b))
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

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:
from itertools import permutations
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
youso = []
zyunretu =[]
iti1 = 0
iti2 = 0
for x in range(n):
    youso.append(x+1)

for v in list(itertools.permutations(youso)):
    if str(a)[1 :-1] == str(v)[1 :-1] :
        iti1 = len(zyunretu)+1
    if str(b)[1 :-1] == str(v)[1 :-1] :
        iti2 = len(zyunretu)+1
    zyunretu.append(str(v)[1 :-1])

print(abs(iti1 - iti2))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

aa, bb = 0, 0
for i in range(n):
    aa *= 10
    aa += a[i]
    bb *= 10
    bb += b[i]

p = [i for i in range(1, n+1)]
na, nb = 0, 0
cnt = 0
for v in itertools.permutations(p, n):
    cnt += 1
    vv = 0
    for i in range(n):
        vv *= 10
        vv += v[i]
    if aa == vv:
        na = cnt
    if bb == vv:
        nb = cnt

print(abs(na-nb))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
a = list(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 itertools
N=int(input())
P,Q=[tuple(map(int,input().split())) for i in range(2)]
dictionary=sorted(itertools.permutations(P,N))
print(abs(dictionary.index(P)-dictionary.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()))

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())
Pls = list(map(int,input().split()))
Qls = list(map(int,input().split()))

lis = [i for i in range(1,N+1)]
permutations_lis = itertools.permutations(lis)
i = 0
for case in permutations_lis:
    i += 1
    if list(case) == Pls:
        a = i
    if list(case) == Qls:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
from itertools import permutations
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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


    for i, A in enumerate(permutations(range(1,N+1))):
        if A == P:
            p = i
        if A == Q:
            q = i

    print(abs(p-q))




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations
lens = int(input())
a = tuple([int(x) for x in input().split()])
b = tuple([int(x) for x in input().split()])
perms = sorted(list(permutations(range(1, lens + 1))))

print( abs(perms.index(a) - perms.index(b)))

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
n = range(1,N + 1)
import itertools 
l = list(itertools.permutations(n))
a = 0
b = 0
for i in l:
    if i == tuple(P):
        a += 1
        break
    else:
        a += 1
for i in l:
    if  i == tuple(Q):
        b += 1
        break
    else:
        b += 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()))
n=[]
for i in range(1,N+1):
  n.append(i)
import itertools
n=list(itertools.permutations(n))
a=0
b=0
for i in n:
  if i==tuple(P):
    a+=1
    break
  else:
    a+=1
for i in n:
  if i==tuple(Q):
    b+=1
    break
  else:
    b+=1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools as i
f=lambda:tuple(map(int,input().split()))
x=list(i.permutations(range(1,int(input())+1)))
print(abs(-~x.index(f())--~x.index(f())))

Submission status: Accepted

Solution code:
import itertools as i
input()
f=lambda:tuple(map(int,input().split()))
P=f()
Q=f()
x=sorted(list(i.permutations(P)))
print(abs(-~x.index(P)--~x.index(Q)))

Submission status: Accepted

Solution code:
import itertools as i
input()
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
x=sorted(list(i.permutations(P)))
print(abs(-~x.index(P)--~x.index(Q)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations as pe
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    ns=[]
    for i in range(1,n+1):
        ns.append(str(i))
    p=LISTS()
    q=LISTS()
    ct=0
    for v in pe(ns,n):
        v=list(v)
        #print(v,p,q)
        ct+=1
        if p==v:
            a=ct
        if q==v:
            b=ct

    print(abs(a-b))
if __name__=='__main__':
    do()

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

perm = itertools.permutations(range(1,n+1))
ls = []
for i in perm:
    ls.append(list(i))

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

Submission status: Accepted

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

A = list(itertools.permutations(list(range(1, n+1))))
A.sort()
p = None
q = None
for i in range(math.factorial(n)):
    if P == list(A[i]):
        p = i+1
    if Q == list(A[i]):
        q = i+1
    if p and q:
        print(abs(p - q))
        exit()

Submission status: Accepted

Solution code:
#
# 150C Count Order
#
import itertools
import sys

n = input()
p = input()
q = input()

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

p = p.replace(' ', ', ')
p = "(" + p + ")"

q = q.replace(' ', ', ')
q = "(" + q + ")"

a = [int(i)+1 for i in range(int(n))]
b = list(itertools.permutations(a))

pcount = 0
qcount = 0
pf = False
qf = False

for i in range(len(b)):
    if str(b[i])==p:
        pcount = i+1
        pf = True
    if str(b[i])==q:
        qcount = i+1
        qf = True
    if pf and qf:
        break

print(abs(pcount-qcount))

Submission status: Accepted

Solution code:
import math
import itertools


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

min_num = []
cnt = 0
a = 0
b = 0

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

for v in itertools.permutations(min_num, n):
    cnt += 1
    if list(v) == P:
        a = cnt
    if list(v) == Q:
        b = cnt

print(abs(a-b))

Submission status: Accepted

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

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

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
# import math

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# A, B, X = map(int, input().split())
# P = list(map(int, input().split()))
# Q = list(map(int, input().split()))
P = list(input().split())
Q = list(input().split())
# S = input()

P_str = ''.join(P)
Q_str = ''.join(Q)


all_cases = list(itertools.permutations(P))
all_cases_str = []
for l in all_cases:
    all_cases_str.append(''.join(l))
all_cases_str = sorted(all_cases_str)

a = 0
b = 0
cnt = 0

for s in all_cases_str:
    cnt += 1
    # print(s)
    if P_str == s:
        a = cnt
    if Q_str == s:
        b = cnt

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=0
b=0

for i,v in enumerate(itertools.permutations(range(1,N+1),N)):
  if tuple(P)==v:
    a=i
  if tuple(Q)==v:
    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()))
p=0
q=0
L=[0]*(N+1)
L[0]=1
for i in range(N):
    L[P[i]]=1 
    #print(L)
    
    num=0
    for y in range(1,P[i]+1):
        if L[y]==0:
            num+=1
    p+=num*math.factorial(N-i-1)
    #print(num,math.factorial(N-i-1))
        
L=[0]*(N+1)
L[0]=1           
for i in range(N):
    L[Q[i]]=1 
    #print(L)
    num=0
    for y in range(1,Q[i]+1):
        if L[y]==0:
            num+=1
    q+=num*math.factorial(N-i-1)
        

print(abs(q-p))