Problem:


Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0



Sample input:
3
1 3 2
3 1 2


Sample output:
3

Submission status: Accepted

Solution code:
import itertools


def checkArrays(arr1, arr2):
    if len(arr1) != len(arr2):
        return False

    for i in range(len(arr1)):
        if arr1[i] != arr2[i]:
            return False

    return True


n = int(input())

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

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

a = b = 1
for i in range(len(arrays)):
    ar = list(arrays[i])
    if checkArrays(ar, p):
        a = i + 1
    if checkArrays(ar, q):
        b = i + 1

print(abs(a - b))

Submission status: Accepted

Solution code:
# ABC150C - Count Order
from itertools import permutations


def main():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    perms = tuple(permutations(range(1, N + 1)))
    ans = abs(perms.index(P) - perms.index(Q))
    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()))
l=list(itertools.permutations(range(1,N+1)))
Xp=l.index(P)+1
Xq=l.index(Q)+1
print(abs(Xp-Xq))

Submission status: Accepted

Solution code:
from itertools import permutations

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

l=[_ for _ in range(1,n+1)]
x=list(permutations(l, len(p)))
y=x.index(tuple(p))
z=x.index(tuple(q))
print(abs(y-z))

Submission status: Accepted

Solution code:
from itertools import combinations_with_replacement as cwr , combinations , permutations
n=int(input())
a=tuple([int(i) for i in input().split()])
b=tuple([int(i) for i in input().split()])
ans=list(permutations(list(range(1,n+1))))
print(abs(ans.index(a)-ans.index(b)))

Submission status: Accepted

Solution code:
import itertools

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

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

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

p_n = func1(n_list, p)
q_n = func1(n_list, q)

print(abs(p_n[0] - q_n[0]))

Submission status: Accepted

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
P = LIST()
Q = LIST()

import itertools

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

print(abs(a-b))

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()))
a, b = 0, 0
for i, x in enumerate(permutations(range(1, n + 1))):
    if x == p:
        a = i
    if x == 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()))

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

ans = abs(a - b)

print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

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

for i, pat in enumerate(permutations(range(1, n + 1), n)):
    if pat == p:
        a = i

    if pat == q:
        b = i

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

Submission status: Accepted

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

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(lambda x: int(x)-1, input().split()))
Q = tuple(map(lambda x: int(x)-1, input().split()))
permu_list = itertools.permutations(range(N))
p,q = 0,0
c = 0
for l in sorted(permu_list):
  if l == P:
    p = c
  if l == Q:
    q = c
  c += 1
print(abs(p-q))

Submission status: Accepted

Solution code:
n=int(input())
P=[int(i) for i in input().split()]
Q=[int(i) for i in input().split()]
import itertools
ct=1
for c in itertools.permutations(range(n),n):
  c=list(c)
  for i in range(n):
    if P[i]-1==c[i]:
      continue
    else:
      break
  else:
    a=ct
  for i in range(n):
    if Q[i]-1==c[i]:
      continue
    else:
      break
  else:
    b=ct
  ct+=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()))

X = [tuple(p) for p in permutations(range(1, N + 1), r=N)]
X.sort()

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

Submission status: Accepted

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


def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    c1 = 0
    for per in permutations(range(1, n+1)):
        c1 += 1
        if per == p:
            break
    c2 = 0
    for per in permutations(range(1, n+1)):
        c2 += 1
        if per == q:
            break
    print(abs(c1-c2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
list1 = []
for i in range(1, N+1):
    list1.append(i)
cnt = 1
for i in itertools.permutations(list1):
    i = list(i)
    if A==i:
        a = cnt
    if B==i:
        b = cnt
    cnt += 1
print(abs(b-a))

Submission status: Accepted

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
import collections
import itertools
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = []
for i in range(2):
    input_data = input().split()
    tmp = [int(n) for n in input_data]
    data.append(tuple(tmp))

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

index_d0 = -1
index_d1 = -1

for i in range(len(perm)):
    if perm[i] == data[0]:
        index_d0 = i+1
    if perm[i] == data[1]:
        index_d1 = i+1
    if index_d0 > -1 and index_d1 > -1:
        break

smaller_num = min(index_d0, index_d1)
larger_num = max(index_d0, index_d1)

ans = abs(smaller_num - larger_num)
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

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

iP, iQ = None, None
for idx, perm in enumerate(permutations(range(1, N + 1))):
    if perm == P:
        iP = idx
    if perm == Q:
        iQ = idx
print(abs(iP - iQ))

Submission status: Accepted

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

s = [0]*(n+1)
s[1] = 1
for i in range(2, n+1):
  s[i] = s[i-1]*i

x = 0
l = [i for i in range(1, n+1)]
for i in p:
  x += l.index(i) * s[len(l) - 1]
  l.remove(i)

y = 0
l = [i for i in range(1, n+1)]
for i in q:
  y += l.index(i) * s[len(l) - 1]
  l.remove(i)
  
print(abs(x-y))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
n = list(range(1, N+1))
a_tuple = tuple(map(int, input().split()))
b_tuple = tuple(map(int, input().split()))
a = 0
b = 0
for i, tup in enumerate(sorted(list(permutations(n))), 1):
    if tup == a_tuple:
        a = i
    if tup == b_tuple:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
N_str = []
for i in range(N):
    N_str.append(i+1)
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
a = sorted(list(itertools.permutations(N_str)))
for i in range(len(a)):
    if(P == a[i]):
        cnt_P = i
    if(Q == a[i]):
        cnt_Q = i
print(abs(cnt_Q-cnt_P))

Submission status: Accepted

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

a,b = -1,-1
for i,x in enumerate(permutations([i+1 for i in range(n)])):
    c,d = True,True
    for j in range(n):
        if x[j] != p[j]:
            c = False
        if x[j] != q[j]:
            d = False
    if c:a = i+1
    if d:b = i+1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
a  = sorted(p)
q = list(map(int, input().split()))
cntp = 0
cntq = 0
cnt=0
for i in itertools.permutations(a):
  cnt+=1
  if p==list(i):
    cntp=cnt
  if q==list(i):
    cntq = cnt
print(abs(cntp-cntq))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = [int(x) for x in input().split()]
a  = sorted(p)
q = [int(x) for x in input().split()]
cntp = 0
cntq = 0
cnt=0
for i in itertools.permutations(a):
  cnt+=1
  if p==list(i):
    cntp=cnt
  if q==list(i):
    cntq = cnt
print(abs(cntp-cntq))

Submission status: Accepted

Solution code:
import itertools

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

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

p = 0
q = 0
for i in range(len(l)):
    if P == list(l[i]):
        p = i
    if Q == list(l[i]):
        q = i
        
print(abs(p-q))

Submission status: Accepted

Solution code:
import math
from collections import deque
def main():
    n = int(input())
    p = [int(t)for t in input().split()]
    q = [int(t)for t in input().split()]

    p_num = 0
    q_num = 0

    fact = [1]*n

    for i in range(n):
        fact[i] = fact[i-1]*(i+1)

    fact = fact[::-1]
    fact.append(1)

    temp = set([])
    for i, p_ in enumerate(p):
        t = p_-len([x for x in temp if x<p_])-1
        p_num += fact[i+1]*t
        temp.add(p_)

    temp = set([])
    for i, q_ in enumerate(q):
        t = q_-len([x for x in temp if x<q_])-1
        q_num += fact[i+1]*t
        temp.add(q_)

    print(abs(p_num-q_num))

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()))
li = [i+1 for i in range(N)]
for i, v in enumerate(permutations(li, N)):
    if v == p: a = i
    if v == q: b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

N=int(input())
P=[int(x) for x in input().split()]
Q=[int(x) for x in input().split()]
L=tuple(permutations([i for i in range(1,N+1)], N))

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

Submission status: Accepted

Solution code:
import math
import itertools
N = int(input())
nums = []
for i in range(1, N + 1):
    nums.append(i)
permutaions_list = list(itertools.permutations(nums, N))
Ps = tuple(map(int, input().split()))
Qs = tuple(map(int, input().split()))

print(abs(permutaions_list.index(Ps) - permutaions_list.index(Qs)))

Submission status: Accepted

Solution code:
import itertools

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

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

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

permutations = sorted(P)
count = 0
p_index = 0
q_index = 0
for i in itertools.permutations(permutations,N):
    count+=1
    if "".join((str(j) for j in i)) == "".join([str(i) for i in P]):
        p_index = count
    if "".join((str(j) for j in i)) == "".join([str(i) for i in Q]):
        q_index = count
print(abs(p_index-q_index))

Submission status: Accepted

Solution code:
from math import factorial
n = int(input())

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

numlist = list(range(1,n+1))
for i in range(n):
    P_order += numlist.index(P[i]) * factorial(n-i-1)
    numlist.remove(P[i])

numlist = list(range(1,n+1))
for i in range(n):
    Q_order += numlist.index(Q[i]) * factorial(n-i-1)
    numlist.remove(Q[i])

print(abs(P_order - Q_order))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = tuple([int(i) for i in input().split()])
b = tuple([int(i) for i in input().split()])

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

pattern_list = list(itertools.permutations(range(1,N+1))) # 順列の生成

cnt = 1
for pattern in pattern_list:
    if pattern == p_list:
        a = cnt
    if pattern == q_list:
        b = cnt
    cnt+=1


print(abs(a-b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
P = int(input().replace(' ', ''))
Q = int(input().replace(' ', ''))
N = [i + 1 for i in range(n)]
per = list(itertools.permutations(N))
lis = []
for oc in per:
    lis.append(int(''.join(map(str, list(oc)))))
a = lis.index(P)
b = lis.index(Q)
print(abs(a - b))

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
cnt=0
from  itertools import permutations
for i in permutations(range(1,1+n)):
    cnt+=1
    i=list(i)
    if     i==p:
        a=cnt
        break
cnt=0
for i in permutations(range(1,1+n)):
    cnt+=1
    i=list(i)
    if     i == q:
        b=cnt
        break


print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
int1 = lambda x: int(x) - 1

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
 
n = int(readline())
p = tuple(map(int1, readline().split()))
q = tuple(map(int1, readline().split()))
 
l = list(permutations(list(range(n))))
 
print(abs(l.index(q) - l.index(p)))

Submission status: Accepted

Solution code:
import sys
import os
import itertools


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

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

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

    allcase = list(itertools.permutations(seq))
    ret = []
    for i, case in enumerate(allcase):
        if case == P:
            ret.append(i)
        if case == Q:
            ret.append(i)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def kaijou(x):
    result = 1
    for i in range(1, x+1):
        result *= i
    return result

def nokori(X, y):

    X = sorted(X)
    idx = 0
    for i in X:
        if i == y:
            return idx
        idx += 1
a = 1
tmp_P = P.copy()
for idx, i in enumerate(P):
    if idx < len(P):
        a = a + (nokori(tmp_P,i))*kaijou(N-idx-1)
        tmp_P.remove(i)
b = 1
tmp_Q = Q.copy()
for idx, i in enumerate(Q):
    if idx < len(Q):
        b += (nokori(tmp_Q,i))*kaijou(N-idx-1)
        tmp_Q.remove(i)
if a > b:
    print(a-b)
else:
    print(b-a)

Submission status: Accepted

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

def counter(p):
    c = 0
    used = [0 for _ in range(N)]
    for i in range(N):
        used[p[i] - 1] = 1
        for k in range(1, p[i]):
            if used[k - 1] != 1:
                c += math.factorial(N - (i + 1))
    
    return c

print(abs(counter(p) - counter(q)))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = int(input().replace(' ', ''))
Q = int(input().replace(' ', ''))
l = [i for i in range(1, N+1)]
pl = itertools.permutations(l)
lis = []
for oc in pl:
  lis.append(int(''.join(map(str, list(oc)))))
lis.sort()
a = lis.index(P)
b = lis.index(Q)

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
p_th = 0
for per in itertools.permutations(range(1, 1+N)):
    p_th += 1
    if P == list(per):
        break

q_th = 0
for per in itertools.permutations(range(1, 1+N)):
    q_th += 1
    if Q == list(per):
        break

print(abs(p_th - q_th))

Submission status: Accepted

Solution code:
import itertools
def listtoint(l, n):
    s = 0
    for i in range(n):
        s = s + l[i]*10**(n-1-i)
    return s
def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    n = []
    for v in itertools.permutations(P):
        s = 0
        for i in range(N):
            s = s + v[i]*10**(N-1-i)
        n.append(s)
    p = listtoint(P, N)
    q = listtoint(Q, N)
    a, b = 0, 0
    for num in n:
        if num < p:
            a = a + 1
        if num < q:
            b = b + 1
    print(abs(a-b))
main()

Submission status: Accepted

Solution code:
import sys
import itertools
 
INF = 1001001001
 
def resolve():
  n = int(input())
  p = list(map(int, input().split()))
  q = list(map(int, input().split()))
  arr = []
  for i in range(1, n+1):
    arr.append(i)
 
  x = itertools.permutations(arr, n)
 
  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1
 
  print(abs(a - b))
resolve()

Submission status: Accepted

Solution code:
import itertools

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

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

    p = list(itertools.permutations(N_range,N))
    #print(list(p[0]))
    #print(P)

    start = 0
    end = 0

    for i in range(len(p)):
        if(list(p[i]) == P):
            start = i
        if(list(p[i]) == Q):
            end = i

    if(start > end):
        print(start - end)
    else:
        print(end - start)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools

INF = 1001001001

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

  x = itertools.permutations(arr, n)

  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1

  print(abs(a - b))
resolve()

Submission status: Accepted

Solution code:
def add_num(cur,fut):
  for x in fut:
    ne = cur + str(x)
    rep = fut[:]
    rep.remove(x)
    
    if len(rep) > 0:
      add_num(ne,rep)
    else:
      p_list.append(int(ne))

  
  return p_list

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

plist = ''
qlist = ''

for p in P:
  plist = plist + p

for q in Q:
  qlist = qlist + q
  
pl = int(plist)
ql = int(qlist)

#大きさNの数列を作る
p_list = []
fut = [i for i in range(1,N+1)]
cur = ''

add_num(cur,fut)

p_list.sort()

#print(p_list)

a = p_list.index(pl)
b = p_list.index(ql)

if a-b < 0:
  print(b-a)
else :
  print(a-b)

Submission status: Accepted

Solution code:
import sys
import itertools

INF = 1001001001

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

  x = itertools.permutations(arr, n)

  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1

  print(abs(a - b))

  return

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
d={}
import itertools
l=list(itertools.permutations(range(1,n+1)))
l.sort()
for i,k in enumerate(l):
    d[k]=i

print(abs(d[p]-d[q]))

Submission status: Accepted

Solution code:
import itertools


# N！通りのパターンを全て作る
# それを辞書順にソートして一個ずつ確かめる
# 一致した順番が値

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

candidates = list(itertools.permutations(range(1,N+1)))
a = candidates.index(P)
b = candidates.index(Q)

print(abs(a - b))

Submission status: Accepted

Solution code:
import math
n = int(input())
p = [int(i) for i in input().split(' ')]
q = [int(i) for i in input().split(' ')]


def orders(l, n):
    ini = 0
    s = [int (i) for i in range(1, n+1)]#1 2 3 4 5
    for number in l:
        order = s.index(number)#数字の大きさ-1
        s.remove(number)
        l_num = math.factorial(len(s)) * order#桁数-1! * 大きさ-1
        ini = ini + l_num
    return ini

p_place = orders(p, n)
q_place = orders(q,n)

result = abs(p_place - q_place)
print(result)

Submission status: Accepted

Solution code:
from itertools import permutations

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def kaiseki(nums,res=[],s=""):
    for i in range(len(nums)):
        tmp = nums[:]
        a = str(tmp.pop(i))
        if len(tmp) > 0:
            kaiseki(tmp,res,s+a)
        else:
            res.append(int(s+a))

def resolve():
    readline=sys.stdin.readline

    n=int(readline().strip())
    p = list(map(str,readline().strip().split()))
    q = list(map(str,readline().strip().split()))

    p = int("".join(p))
    q = int("".join(q))

    nums=list(range(1,n+1))
    res=[]
    kaiseki(nums,res,"")
    res.sort()

    i=0
    for r in res:
        if r==p:
            p=i
        if r==q:
            q=i
        i+=1

    diff=p-q
    if diff < 0:
        diff *= -1
    
    print(diff)

    return

resolve()

Submission status: Accepted

Solution code:
def main():
  import sys
  import itertools
  input = sys.stdin.readline
  n=int(input())
  p=int(''.join(input().split()))
  q=int(''.join(input().split()))
  p,q=max(p,q),min(p,q)
  a,b,l=0,0,[str(i) for i in range(1,n+1)]
  for i in itertools.permutations(l):
    i=int(''.join(i))
    if q<i and i<=p:
      a+=1
    if p<i:
      break
  print(a)  
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools 

def minus(n):
    return int(n)-1

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

list = list(itertools.permutations(range(num)))

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

Submission status: Accepted

Solution code:
def permutations(L):
    if L == []:
        return [[]]
    else:
        return [[h] + t for i, h in enumerate(L) for t in permutations(L[:i] + L[i+1:])]


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

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

a = l.index(p) + 1
b = l.index(q) + 1

print(abs(a - b))

Submission status: Accepted

Solution code:
import sys

import numpy as np
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
"""dd = [(-1, 0), (0, 1), (1, 0), (0, -1)];
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)];
ddn9 = ddn + [(0, 0)]
for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""

def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]  # WideIntPoint
def ws(): return sys.stdin.readline().split()
def si(): return int(sys.stdin.readline())  # SingleInt
def ss(): return input()
def hi(n): return [si() for _ in range(n)]
def hs(n): return [ss() for _ in range(n)]  # HeightString
def s_list(): return list(input())
def mi(n): return [wi() for _ in range(n)]  # MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]  # NumberGrid
def grid(n): return [s_list() for _ in range(n)]


def main():

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

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

    perm = list(itertools.permutations(number))
    count = 0
    for L in perm:
        count += 1
        if list(L) == p:
            a = count
        if list(L) == q:
            b = count

    print(abs(a-b))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

X = list(itertools.permutations(x,n))

a = 0
b = 0
for i in range(math.factorial(n)):
    if p == list(X[i]):
        a = i
    if q == list(X[i]):
        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()))
ans = 0
dic = [1, 1, 2, 6, 24, 120, 720, 5040]

for i in range(N-1):
    p = sorted(P[i:])
    q = sorted(Q[i:])
    ans += (p.index(P[i])-q.index(Q[i]))*dic[N-i-1]
    
print(abs(ans))

Submission status: Accepted

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

def permutations(nums, t, path):
    if len(nums) == 0:
        path.append(t)
        return
    for i in range(len(nums)):
        permutations(nums[:i] + nums[i + 1:], t + [nums[i]], path)

path = []
permutations([i for i in range(1, n + 1)], [], path)
path.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import math
from itertools import permutations as permus
# from fractions import gcd

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
arrP = tuple(map(int, input().split()))
arrQ = tuple(map(int, input().split()))

perms_list = list(permus(range(1,n+1)))
a = perms_list.index(arrP)
b = perms_list.index(arrQ)

ans = abs(a-b)
# y = abs(a-b)
print(ans)

Submission status: Accepted

Solution code:
def fac(n):
    ans = 1
    for k in range(n):
         ans *= k+1
    return ans     
def order(n,p):
  count = 1
  for i in range(n):
    pi = sorted(p[i:])
    count += fac(n-i-1)*pi.index(p[i])
  return count
n = int(input())
p = list(input().split())
q = list(input().split())
print(abs(order(n,p)-order(n,q)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
p = "".join(input().split())
q = "".join(input().split())

lst = sorted(map("".join, permutations(map(str, range(1, n+1)), n)))
print(abs(lst.index(p)-lst.index(q)))