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:
#https://atcoder.jp/contests/abc150/tasks/abc150_c
import itertools
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))

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

Submission status: Accepted

Solution code:
from itertools import permutations

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

for i, j in enumerate(permutations(range(1, n + 1))):
    if list(j) == p:
        p_index = i
    if list(j) == q:
        q_index = i

print(abs(p_index - q_index))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, j in enumerate(itertools.permutations(range(1, n+1), n)):
  #print(j)
  if j == p:
    a=i
  if j == q:
    b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
cnt = 0
a = -1
b = -1
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P and a == -1:
    a = cnt
  elif list(per) == Q and a == -1:
    a = cnt
  if list(per) == P and a != -1:
    b = cnt
  elif list(per) == Q and a != -1:
    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()))
cnt = 0
a = -1
b = -1
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P:
    a = cnt
  if list(per) == Q:
    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()))
cnt = 0
ans = []
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P:
    ans.append(cnt)
  elif list(per) == Q:
    ans.append(cnt)

print(max(ans)-min(ans))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
p = ''.join(input().split())
q = ''.join(input().split())

l = permutations(range(1, N+1))
l2 = []
for i in l:
    l2.append(''.join(map(str, i)))
l = sorted(l2)

p_idx = 0
q_idx = 0
for n, i in enumerate(l):
    if i == p:
        p_idx = n
    if i == q:
        q_idx = n
print(abs(p_idx-q_idx))

Submission status: Accepted

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


def dfs(x, d, res):
    if len(x) == N + 1:
        d[0] += 1
        if x[1:] == P:
            res["P"] = d[0]
        if x[1:] == Q:
            res["Q"] = d[0]
        return

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

res = {"P": 0, "Q": 0}
dfs([0], [0], res)
print(abs(res["P"] - res["Q"]))

Submission status: Accepted

Solution code:
import sys
import itertools

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    per = list(itertools.permutations(range(1, n+1)))
    t = list(map(lambda x: "".join(str(i) for i in x), per))
    p = "".join(sys.stdin.readline().split())
    q = "".join(sys.stdin.readline().split())
    a = 0
    b = 0
    for i, x in enumerate(t):
        if p == x:
            a = i
        if q == x:
            b = i
    ans = abs(a - b)
    print(ans)

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

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

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())

A = tuple([int(x) for x in input().split()])
B = tuple([int(x) for x in input().split()])

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

print(abs(all_list.index(A)-all_list.index(B)))

Submission status: Accepted

Solution code:
import itertools
def main6():
    n = int(input())
    p=[str(i) for i in input().split()]
    q=[str(i) for i in input().split()]
    l = [str(i) for i in range(1,n+1)]
    k = ''.join(p)
    k1 = ''.join(q)
    p_l = list(itertools.permutations(l))
    for v in range(len(p_l)):
        u = (''.join(p_l[v]))
        if(u == k):
            a = v
        if(u == k1):
            b = v

    print(abs(a-b))

if __name__ == '__main__':
    main6()

Submission status: Accepted

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

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

c = itertools.permutations(nn, n)

p_p = 0
q_p = 0
for i, j in enumerate(c):
    if list(j) == p:
        p_p = i
    if list(j) == q:
            q_p = i
print(abs(q_p - p_p))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools as itool
N=int(input())
plist=list(map(int, input().split()))
qlist=list(map(int, input().split()))
mylist=list(itool.permutations(range(1,N+1)))

print(abs(mylist.index(tuple(plist))-mylist.index(tuple(qlist))))

Submission status: Accepted

Solution code:
table = [1, 1, 2, 6, 24, 120, 720, 5040]

def factorial(n):
    return table[n]

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        # print(a, index, factorial(len_l - 1), order)
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def factorial(n):
    table = [1, 1, 2, 6, 24, 120, 720, 5040]
    return table[n]

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        # print(a, index, factorial(len_l - 1), order)
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= (i + 1)
    return ret

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        # print(a, index, factorial(len_l - 1), order)
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = len(l)
        index = l.index(a)
        # print(a, index, math.factorial(len_l - 1), order)
        order += index * math.factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= (i + 1)
    return ret

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = len(l)
        index = l.index(a)
        # print(a, index, factorial(len_l - 1), order)
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    P = tuple(map(int,input().split(' ')))
    Q = tuple(map(int,input().split(' ')))
    Pnum = dictNum(P,N)
    Qnum = dictNum(Q,N)
    ans = (abs(Pnum-Qnum))
    print(ans)

def dictNum(A, N):
    ans = 0
    restNum = [1]*N
    for i in range(N):
        restNum[A[i]-1]=0
        ans += count(sum(restNum[:A[i]]),N-i)
    return ans + 1
def count(a,N):
    ans = factorial(N-1)*a
    if N == 1:
        ans = 0
    return ans
def factorial(num):
    if num == 0:
        return 1
    else:
        return num*factorial(num-1)
main()

Submission status: Accepted

Solution code:
def next_permutation(lst) :
    length = len(lst);
    #print(lst);
    for i in range(-1,-n,-1) :
        if lst[i-1] < lst[i] :
            lst[i:]=sorted(lst[i:]);
            #print(i,lst);
            for j in range(i,0) :
                if lst[j] > lst[i-1] :
                    tmp = lst[i-1];
                    lst[i-1] = lst[j];
                    lst[j] = tmp;
                    break;
            lst[i:]=sorted(lst[i:]); 
            break;   
    return
n = int(input());
p = [int(x) for x in input().split()];
q = [int(x) for x in input().split()];

init = [x for x in range(1,n+1)];
cnt = 1;
a = 0;
b = 0;

while a == 0 or b == 0 :
    if p == init :
        a = cnt;
    if q == init :
        b = cnt;
    cnt += 1;
    next_permutation(init);
    #print(init);

print(abs(a-b));

#next_permutation(init);
#print(init);

Submission status: Accepted

Solution code:
def LI():
  return list(map(int, input().split()))

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

result = []
def permutations(N, cur):
  if len(cur) == N:
    result.append(cur)
    return

  for i in range(1, N+1):
    if i not in cur:
      permutations(N, cur+[i])

permutations(N,[])
result.sort()
print(abs(result.index(P) - result.index(Q)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(map(str, input().split()))
Q = list(map(str, input().split()))
permutation = list(itertools.permutations(list(range(1, N+1))))
sum_list = [int("".join(map(str,x))) for x in permutation]
sorted(sum_list)

P_comb = int("".join(P))
Q_comb = int("".join(Q))

P_poji = sum_list.index(P_comb)
Q_poji = sum_list.index(Q_comb)

print(abs(P_poji-Q_poji))

Submission status: Accepted

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

J = list(itertools.permutations(Nlist, N))

count = 0
for i in range(len(J)):
    if J[i] == P:
        a = i+1
        count += 1
        if count == 2:
            break
    if J[i] == Q:
        b = i+1
        count += 1
        if count == 2:
            break

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()))
ls = [i for i in range(1,N+1)]
A = list(permutations(ls))

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

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

ans = 0
for i, check_i in enumerate(check_list, 1):
  
  if list(check_i) == P:
    ans += i
  
  if list(check_i) == Q:
    ans -= i
    
print(abs(ans))

Submission status: Accepted

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

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

rank_P = rank_Q = 0

for i, t in enumerate(permutations(range(1, N + 1)), start=1):
    if t == P:
        rank_P = i
        if rank_Q:
            break

    if t == Q:
        rank_Q = i
        if rank_P:
            break

print(abs(rank_P - rank_Q))

Submission status: Accepted

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

arr = list(itertools.permutations(range(1,n+1),n))
arr = sorted(arr)
for i in range(len(arr)):
    if list(arr[i]) == p:
        a = i
    if list(arr[i]) == q:
        b = i
print(abs(b-a))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

iter = list(itertools.permutations(range(1,N+1)))
a = iter.index(A)
b = iter.index(B)

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from itertools import permutations
inf = 10**17
mod = 10**9+7

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

cnt = 1
a = 0
b = 0
a = [i for i in range(1, n + 1)]
for v in permutations(a):
    v = list(v)
    if p == v: a = cnt
    if q == v: b = cnt
    cnt += 1

print(abs(a - b))

Submission status: Accepted

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

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

print(abs(a-b))

Submission status: Accepted

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

import itertools

seq = list(range(1, N+1))
ptr = list(itertools.permutations(seq, N)) #順列列挙

#print(seq)
#print(ptr)

tmp1=ptr.index(P) #要素番号を格納する
tmp2=ptr.index(Q) #要素番号を格納する

print(abs(tmp1-tmp2))

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

#print(P, Q, li)
print(abs(li.index(P) - li.index(Q)))

Submission status: Accepted

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

retu=range(1,n+1)
cnt=0
cp=0
cq=0
for r in itertools.permutations(retu):
    rr = [r[i] for i in range(n)]
    cnt+=1
    if rr == p:
        cp=cnt
    if rr == q:
        cq=cnt
print(abs(cp-cq))

Submission status: Accepted

Solution code:
import itertools

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

for i in range(n):
    p[i] -= 1
    q[i] -= 1

p = tuple(p)
q = tuple(q)

numbers = [i for i in range(n)]
l = list(itertools.permutations(numbers))

p_order = l.index(p)
q_order = l.index(q)

ans = abs(p_order - q_order)
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
in_list = [int(w) for w in input().split()]
p_li = tuple(in_list)
q_li = tuple([int(w) for w in input().split()])


permu = permutations(sorted(in_list))

p_in = 0
q_in = 0

for i, p in enumerate(permu):
    if p == p_li:
        p_in = i
    if p == q_li:
        q_in = i

print(abs(p_in-q_in))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations

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


def main():
    N, *PQ = map(int, read().split())
    P = tuple(PQ[:N])
    Q = tuple(PQ[N:])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
p_permu=sorted(list(permutations(p)))
q_permu=sorted(list(permutations(q)))
print(abs(p_permu.index(tuple(p))-q_permu.index(tuple(q))))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
ps = permutations([i for i in range(1, N+1)])
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = None
b = None
for i,p in enumerate(ps):
    if P == p:
        a = i+1
    if Q == p:
        b = i+1
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
N = input()
P = tuple([int(_) for _ in input().split()])
Q = tuple([int(_) for _ in input().split()])
pl = list(permutations(P))
pl.sort()
ql = list(permutations(Q))
ql.sort()

pi = pl.index(P)+1
qi = ql.index(Q)+1
print(abs(pi-qi))

Submission status: Accepted

Solution code:
import itertools

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

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

a=S.index(P)
b=S.index(Q)
print(abs(a-b))

Submission status: Accepted

Solution code:
def main():
    import itertools

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

    nums = list(range(1,n+1))
    perm = list(itertools.permutations(nums, n))
    a = perm.index(p) + 1
    b = perm.index(q) + 1

    print(abs(a-b))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools

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

X = sorted(itertools.permutations(p, N))

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

x=sorted(itertools.permutations(p,n))

a=x.index(p)
b=x.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()))
temp=list(permutations(range(1,N+1)))
print(abs(temp.index(P)-temp.index(Q)))

Submission status: Accepted

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

dict_list = sorted(list(itertools.permutations(a, len(a))))

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

Submission status: Accepted

Solution code:
import itertools

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

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

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())

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

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

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
p=tuple(input().split())
q=tuple(input().split())
a=sorted(list(permutations(p)))
print(abs(a.index(p)-a.index(q)))

Submission status: Accepted

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
import itertools
import math
input = sys.stdin.readline

N = int(input())
list_P = tuple(map(int, input().split()))
list_Q = tuple(map(int, input().split()))

List_All = list(itertools.permutations(list(range(1, N+1))))
# print(List_All)
Pi = List_All.index(list_P)
Qi = List_All.index(list_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()))

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

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
test=[]
for i in itertools.permutations(range(1,N+1)):
    test.append(''.join(list(map(str,i))))
print(abs(test.index(''.join(list(map(str,P))))-test.index(''.join(list(map(str,Q))))))

Submission status: Accepted

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

o = {}
for i, p in enumerate(permutations(range(1,N+1))):
  	o[p] = i
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())
print(abs(o[P] - o[Q]))

Submission status: Accepted

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

def to_str(string, array):
    for i in array:
        string += str(i)
    return string

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

    p_str = ""
    q_str = ""
    p_str = to_str(p_str, p)
    q_str = to_str(q_str, q)
    
    arr = [i+1 for i in range(n)]

    nums = list(itertools.permutations(arr))
    nums_str = []
    for e in nums:
        temp = ""
        temp = to_str(temp, e)
        nums_str.append(temp)

    nums_str.sort()

    a = nums_str.index(p_str) + 1
    b = nums_str.index(q_str) + 1
    print(abs(a-b))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
N=int(input())
L=list(map(int,input().split()))
R=list(map(int,input().split()))
P = list(itertools.permutations(L, N))
P=sorted(P)
a=P.index(tuple(L))
b=P.index(tuple(R))
print(abs(a-b))

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
#from collections import defaultdict
import math


#input = sys.stdin.readline

def numbering(P,N):

    used = [1]*(N+1)
    all = math.factorial(N)
    ans =0
    for i in range(N):
        p = P.pop(0)
        used[p] =0
        all =int(all/(N-i))
        ans += all *(sum(used[0:p])-1)
 #       print("p {}:  all {} : ans {}".format(p,all,ans))

    return ans+1




def main():
    N = int(input())
    P = list(map(int,input().split()))
    ans = numbering(P,N)
    Q = list(map(int,input().split()))
    ans2 = numbering(Q,N)
    print(abs(ans - ans2))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

a = 0
b = 0

for i, name in enumerate(list(permutations(ls, N))):
    name = list(name)
    if name == P:
        a = i
    if name == Q:
        b = i

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

t = 0
x = 0
y = 0
for i in permutations(range(1,n+1),n):
  t += 1
  if i == p: x = t
  if i == q: y = t

print(abs(x-y))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
L = list(itertools.permutations(list(range(1,n+1))))
p_a = 1
p_b = 1

for i,l in enumerate(L):
    if p == list(l):
        p_a = i+1
    if q == list(l):
        p_b = i+1

print(abs(p_a-p_b))

Submission status: Accepted

Solution code:
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
N = [i + 1for i in range(N)]

import itertools

P_num = 0
Q_num = 0

for num, i in enumerate(itertools.permutations(N)):
    if P == i:
        P_num = num
        
    if Q == i:
        Q_num = num
        
print(abs(P_num-Q_num))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

a = list(permutations(l))
# print(a)
print(abs(a.index(p1) - a.index(p2)))

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)))
a = l.index(p)+1
b = l.index(q)+1

print(abs(a-b))

Submission status: Accepted

Solution code:
from math import factorial

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

plst=[i for i in range(n)]
qlst=[i for i in range(n)]
pls=[]
qls=[]

for i in p:
    a=plst.index(i)
    pls.append(a)
    plst.remove(i)
for i in q:
    a=qlst.index(i)
    qls.append(a)
    qlst.remove(i)

ansp=1
ansq=1

for i in range(n):
    ansp+=pls[i]*(factorial(n-i-1))
for i in range(n):
    ansq+=qls[i]*(factorial(n-i-1))
    
print(abs(ansp-ansq))

Submission status: Accepted

Solution code:

import itertools

n = int(input())

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

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

s = list(itertools.permutations(p, n))

s.sort()

print(abs(s.index(tuple(p))-s.index(tuple(q))))

Submission status: Accepted

Solution code:
n= int(input())
import sys
pn = list(map(int, sys.stdin.readline().split()))
qn = list(map(int, sys.stdin.readline().split()))

a = 0
b= 0
m = [1]*(n+1)
m[0]=0

import math

for i in pn:

    if sum(m)==2:
        a += sum(m[:i]) +1
        break
    m[i] = 0

    a += sum(m[:i])* math.factorial(sum(m))


m = [1]*(n+1)
m[0]=0

for i in qn:

    if sum(m)==2:
        b += sum(m[:i]) +1
        break
    m[i] = 0

    b += sum(m[:i])* math.factorial(sum(m))


print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

n=int(input())
l=list(map(str,input().split()))
t=list(map(str,input().split()))
s="".join(l)
h="".join(t)

l1=list()

for i in permutations(s):
    k="".join(i)
    l1.append(k)
 
l1.sort()

print(abs(l1.index(s)-l1.index(h)))

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

Submission status: Accepted

Solution code:
n = int(input())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))
p_d = 0
q_d = 0
cnt = 0
if ps == qs:
    print(0)
    exit()
from itertools import permutations as perm
perms = list(range(1,n+1))
for p in perm(perms):
    if list(p) == ps:
        p_d = cnt
    elif list(p) == qs:
        q_d = cnt
    cnt += 1
print(abs(p_d-q_d))

Submission status: Accepted

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

l = []
for i in range(1, N+1):
  l.append(i)
  
p_list = list(itertools.permutations(l))
a = p_list.index(P)
b = p_list.index(Q)
ans = abs(b-a)
print(ans)

Submission status: Accepted

Solution code:
import itertools

N=int(input())
p=list(map(str,input().split()))
q=list(map(str,input().split()))
P=''.join(p)
Q=''.join(q)

ns=list(itertools.permutations(range(1,N+1)))
Ns=[]
for i in range(len(ns)):
    temparr=[]
    for j in range(N):
        a=ns[i][j]
        temparr.append(str(a))
    b=''.join(temparr)
    Ns.append(b)

PO=Ns.index(P)
QO=Ns.index(Q)
print(abs(PO-QO))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
ORIGIN = tuple(range(1, N+1))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a , b = 0, 0

for i, group in enumerate(itertools.permutations(ORIGIN, N)):
  if group == P:
    a = i + 1
  if group == Q:
    b = i + 1

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import math

def check(l, N):
    ls = [i for i in range(N)]
    number = 0
    for i in range(N-1):
        number += math.factorial(N-1-i) * ls.index(l[i]-1)
        ls.remove(l[i]-1)
    number += 1
    return number

ans = check(P, N) - check(Q, N)
print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
p=lmp()
q=lmp()
l=list(itertools.permutations([i for i in range(1,n+1)]))
print(abs(l.index(tuple(p))-l.index(tuple(q))))