Problem:


Score : 300 points

Problem Statement

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

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

Notes

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

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

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

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

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

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0



Sample input:
3
1 3 2
3 1 2


Sample output:
3

Submission status: Accepted

Solution code:
from bisect import bisect_left
from itertools import permutations
n=int(input())
p=''.join(map(str,input().split()))
q=''.join(map(str,input().split()))
l=list(range(1,n+1))
A=list(permutations(l,n))
A=[''.join(map(str,i)) for i in A]
A.sort()
a=bisect_left(A,p)
b=bisect_left(A,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()))

kaijolist = [1]
kaijo = 1

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

for i, p in enumerate(j):
    if p == P:
        a = i + 1
                                  
for i, q in enumerate(j):
    if q == Q:
        b = i + 1

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

if __name__ == "__main__":
    N = int(input())
    P = [int(x) for x in input().split(" ")]
    Q = [int(x) for x in input().split(" ")]

    a = 1
    b = 1
    for x in itertools.permutations(range(1, N + 1)):
        if list(x) == P:
            break
        a += 1

    for x in itertools.permutations(range(1, N + 1)):
        if list(x) == Q:
            break
        b += 1
    print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

n = [x+1 for x in range(int(input()))]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

N = list(itertools.permutations(n))
print(abs(N.index(p)-N.index(q)))

Submission status: Accepted

Solution code:
import itertools

n = sorted([x+1 for x in range(int(input()))])
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
count = 0
pv = 0
qv = 0
for i in itertools.permutations(n,len(n)):
  count += 1
  if i == p:
    pv = count
  if i == q:
    qv = count
print(abs(pv-qv))

Submission status: Accepted

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

pn = 0
qn = 0

a = list(itertools.permutations(range(1, n+1)))
# print(a)

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

Submission status: Accepted

Solution code:
#<順列全探索>
#n!通りの順列の組み合わせを全探索する方法。
#pythonにはitertoolsが存在するからそれを利用
#<ex.1(ABC150-C)>
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:
def resolve():
    N = int(input())
    P = input().split()
    Q = input().split()
    Fac = [1] * (N+1)
    for i in range(1,N+1):
        Fac[i] = Fac[i-1] * i
    A = 0
    Alist = list(range(0,N))
    B = 0
    Blist = list(range(0,N))
    for i in range(N): 
        A += Alist[int(P[i]) - 1] * Fac[N-1-i]
        for j in range(int(P[i])-1, N):
            Alist[j] -= 1
        B += Blist[int(Q[i]) - 1] * Fac[N-1-i]
        for j in range(int(Q[i])-1, N):
            Blist[j] -= 1
    print(abs(A-B))
resolve()

Submission status: Accepted

Solution code:
from itertools import permutations

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

a = 0
b = 0

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

counter = 0
a = 0
b = 0
for i in permutations(range(1,N+1)):
    if i == P:
        a = counter
    if i == Q:
        b = counter
    counter += 1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
X = sorted(itertools.permutations(range(1,N+1)))#1~Nの順列をソートしたもの
#itertools.permutations(range(n,m))ではリストの中にタプルが入った構造になる

qn = 0
num = 0
for i in X:
    num += 1
    if i == P:
        pn = num
    if i == Q:
        qn = num
print(abs(pn-qn))

Submission status: Accepted

Solution code:
import itertools

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P_n = tuple(map(int, input().split(' ')))
Q_n = tuple(map(int, input().split(' ')))

a = list(itertools.permutations(range(1, N+1))).index(P_n)
b = list(itertools.permutations(range(1, N+1))).index(Q_n)
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()))
ls = list(itertools.permutations(range(1,N+1)))
print(abs(ls.index(P) - ls.index(Q)))

Submission status: Accepted

Solution code:
# -- not using buffer ----------------------
import itertools
import sys
import math
'''
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

a, b = map(int, readline().split())
print(a)

lis = [int(readline().rstrip('\n')) for _ in range(b)]
print(lis)
c = int(read())
print(c)
'''
# -- using buffer ------------------
# --------------------------------------------
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
'''
a, b = map(int, readline().split())
print(a)

lis = [int(readline()) for _ in range(b)]
print(lis)
c = int(read())
print(c)
'''
# -----------------------
# -----------------------------------
n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
ansList = [i for i in range(1, n + 1)]

div = n // 2
#com = list(itertools.product(ansList, repeat=n))
com = list(itertools.permutations(ansList))


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

ans = abs(a - b)
print(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 as per
tuples = per(range(1, N+1))
 
a = 0
b = 0
for i, t in enumerate(tuples):
    if t == P:
        a = i
    if t == 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()))
 
from itertools import permutations as per
tuples = per(range(1, N+1))
 
a = 0
b = 0
for i, t in enumerate(tuples):
    if t == P:
        a = i
    if t == Q:
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
f=lambda:tuple(map(int,input().split()))
n=f()[0]
from itertools import *
p=[*permutations(range(1,n+1))].index
print(abs(p(f())-p(f())))

Submission status: Accepted

Solution code:
n=int(input())
from itertools import *
p=[*permutations(range(1,n+1))]
f=lambda:p.index(tuple(map(int,input().split())))
print(abs(f()-f()))

Submission status: Accepted

Solution code:
f=lambda:tuple(map(int,input().split()))
n=f()[0]
from itertools import *
p=[*permutations(range(1,n+1))]
print(abs(p.index(f())-p.index(f())))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
Ps = tuple(map(int, input().split()))
Qs = tuple(map(int, input().split()))

noP, noQ = 0, 0
for no, Rs in enumerate(permutations(range(1, N+1), r=N), start=1):
    if Ps == Rs:
        noP = no
    if Qs == Rs:
        noQ = no

print(abs(noP-noQ))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
p=P.copy()
p.sort()
p=itertools.permutations(p)
j=0
for i in p:
    j+=1
    if list(i)==P:
        pcount=j
    if list(i)==Q:
        qcount=j
print(abs(pcount-qcount))

Submission status: Accepted

Solution code:
import itertools

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

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

pi = 0
for i, a in enumerate(p):
    if a == tuple(P):
        pi = i
        break
qi = 0
for i, a in enumerate(q):
    if a == tuple(Q):
        qi = i
        break
print(abs(pi-qi))

Submission status: Accepted

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

Submission status: Accepted

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

ansp=1
ansq=1
f=1
for i in range(1,n):
    t=0
    f*=i
    for j in range(1,i+1):
        if p[-i-1]>p[-j]:
            t+=1
    ansp+=t*f
f=1
for i in range(1,n):
    t=0
    f*=i
    for j in range(1,i+1):
        if q[-i-1]>q[-j]:
            t+=1
    ansq+=t*f

print(abs(ansp-ansq))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

perms = []
x = [i for i in range(1, N + 1)]
def perm(start):
    if start == N:
        perms.append(x.copy())
        return
    for i in range(start, N):
        x[i], x[start] = x[start], x[i]
        perm(start + 1)
        x[i], x[start] = x[start], x[i]
perm(0)
perms = sorted(perms)

a, b = 0, 0
for i in range(len(perms)):
    R = perms[i]
    for j in range(len(P)):
        if P[j] != R[j]:
            break
    else:
        a = i

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

Submission status: Accepted

Solution code:
import math
N = int(input())
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
OrderP = 1
OrderQ = 1
for T in range(0,N-1):
    OrderP += sorted(P[T:]).index(P[T])*math.factorial(N-1-T)
    OrderQ += sorted(Q[T:]).index(Q[T])*math.factorial(N-1-T)
print(abs(OrderP-OrderQ))

Submission status: Accepted

Solution code:
import itertools

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

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

x1 = pls.index(a)
x2 = pls.index(b)
print(abs(x1 - x2))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
pp = int("".join(map(str, p)))
qq = int("".join(map(str, q)))
ppp = 0
qqq = 0

for i in itertools.permutations(p,n):
  j = int("".join(map(str, i)))
  if pp > j:
    ppp += 1
for i in itertools.permutations(q,n):
  j = int("".join(map(str, i)))
  if qq > j:
    qqq += 1
    
print(abs(ppp-qqq))

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

Submission status: Accepted

Solution code:
import itertools

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

if P == Q:
  print(0)
  exit()

cnt = a = b = 0

for v in list(itertools.permutations(L)):
  cnt += 1
  if list(v) == P:
    a = cnt
  elif list(v) == Q:
    b = cnt

print(abs(a-b))

Submission status: Accepted

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

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

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

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
 
N = int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
 
for v_idx,v in enumerate(itertools.permutations(range(1,N+1))):
    if v==P:
        a=v_idx
    if v==Q:
        b=v_idx
        
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations


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

    a = None
    b = None

    perms = tuple(permutations(range(1, n + 1), n))

    for i in range(len(perms)):
        if a is None and tuple(perms[i]) == p:
            a = i
        if b is None and tuple(perms[i]) == q:
            b = i
        if a is not None and b is not None:
            print(abs(a - b))
            exit()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(N-1):
  p+=sum(list(1 if P[i]>P[k] else 0 for k in range(i,N)))*math.factorial(N-i-1)
  q+=sum(list(1 if Q[i]>Q[k] else 0 for k in range(i,N)))*math.factorial(N-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())))

l=[]
for i in range(n):
    l.append(i+1)
al=list(itertools.permutations(l))

for i in range(len(al)):
    if al[i]==p:
        p_ord=i
    if al[i]==q:
        q_ord=i

print(abs(p_ord-q_ord))

Submission status: Accepted

Solution code:
import itertools

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

r = sorted(list(itertools.permutations(p,n)))
a = r.index(p)
b = r.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()))

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
nums = [i+1 for i in range(n)]

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

i_a = 0
i_b = 0

for i, p in enumerate(itertools.permutations(nums)):
    if a == list(p):
        i_a = i+1
    if b == list(p):
        i_b = i+1

    if i_a and i_b:
        break

print(abs(i_a - i_b))

Submission status: Accepted

Solution code:
from copy import deepcopy
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions
from operator import itemgetter
import itertools
import copy


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

n = int(input())
p = map(str, input().split())
q = map(str, input().split())
p_int = int(''.join(p))
q_int = int(''.join(q))
#print(p_int, q_int)
seq = list(range(1, n+1))
# print(seq)
x = list(itertools.permutations(seq))
xx = sorted(x)
# print(xx)
for i in range(math.factorial(n)):
    a = map(str, list(xx[i]))
    b = "".join(map(str, a))
    c = int("".join(map(str, b)))
    # print(c)
    if c == p_int:
        p_ans = i+1
    if c == q_int:
        q_ans = i+1
print(abs(p_ans-q_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
for i,x in enumerate(permutations([i+1 for i in range(n)],n)):
  if x==p:a=i
  if x==q:b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import groupby, accumulate, product, permutations, combinations
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

n = 1
for p in permutations(range(1,N+1),N):
    if list(p)==P:
        a = n
    if list(p)==Q:
        b = n
    n += 1
ans = abs(a-b)
print(ans)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from fractions import gcd
from itertools import combinations,permutations # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
nya = [i+1 for i in range(n)]

jun = []
for a in permutations(nya):
    jun.append(a)
for i in range(len(jun)):
    if jun[i] == P:
        p = i
    if jun[i] == Q:
        q = i
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()))
p = 0
q = 0
c = 0
for e in itertools.permutations([k+1 for k in range(N)], N):
    c += 1
    if e == P:
        p = c
    if e == Q:
        q = c
    if p*q > 0:
        break
print(abs(p-q))

Submission status: Accepted

Solution code:
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lisp=tuple(LI())
lisq=tuple(LI())
x=[i for i in range(1,n+1)]
u=permutations(x)
count=0
for i in u:
    if i==lisp:
        P=count
    if i==lisq:
        Q=count
    count+=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()))
cnt = 0
for v in itertools.permutations(list(range(1,n+1))):
    cnt += 1
    if p == v: a = cnt
    if q == v: b = cnt
print(abs(a-b))

Submission status: Accepted

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

lst = []
for v in itertools.permutations(range(1,n+1)):
    lst.append(''.join([str(i) for i in v]))

print(abs(lst.index(''.join([str(i) for i in pl]))-lst.index(''.join([str(i) for i in ql]))))

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 = sorted(P)
cnt = 0
for x in permutations(A,N):
    x = list(x)
    if x==P:
        a = cnt
    if x==Q:
        b = cnt
    cnt += 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()))

import itertools
X= []
a = 0
b = 0
for p in itertools.permutations(range(1, n+1)):
    for i in range(n):
        if p[i] < P[i]:
            a += 1
            break
        elif p[i] > P[i]:
            break
        else:
            continue
    for i in range(n):
        if p[i] < Q[i]:
            b += 1
            break
        elif p[i] > Q[i]:
            break
        else:
            continue

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

Submission status: Accepted

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

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

for t in permutations(range(1, N+1)):
    if list(t)==P:
        a = c
    
    if list(t)==Q:
        b = c
    
    c += 1

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

for i,j in enumerate(permutations(li)):
    if j == p:
        num_p = i
    if j == q:
        num_q = i

print(abs(num_p-num_q))

Submission status: Accepted

Solution code:
import itertools

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

a=0
b=0
for num,i in enumerate(j):
    if(list(i)==p):a=num
    if(list(i)==q):b=num

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

def check(A):
    L = [j+1 for j in range(N)]
    ans = 0
    for i in range(N):
        a = L.index(A[i])
        ans += a*math.factorial(N-1-i)
        L.pop(a)
    return ans
p = check(P)
q = check(Q)
print(abs(p-q))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

for i in range(n):
  orderp+=(p[i]-1)*math.factorial(n-i-1)
  orderq+=(q[i]-1)*math.factorial(n-i-1)
  for j in range(i+1,n):
    if p[j]>p[i]:
      p[j]-=1
    if q[j]>q[i]:
      q[j]-=1
  #print(orderp,orderq)
  #print(p,q)


print(abs(orderp-orderq))

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:
        a=i
    if p==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()))
lis=[x for x in range(1,n+1)]
permutation=itertools.permutations(lis)
cnt=0
a=0
b=0
for  case in permutation:
  case=list(case)
  cnt+=1
  if case == p:
    a=cnt
  if case == q:
    b=cnt
 
    
print(abs(a-b))

Submission status: Accepted

Solution code:
#<C>
import math
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
ans1=n
ans2=n
x = [i for i in range(1, n+1)]
for i in itertools.permutations(x,n):
    if p < list(i):
        ans1 -= 1
    if q < list(i):
        ans2 -= 1

print(abs(ans1-ans2))

Submission status: Accepted

Solution code:
# 16
# ABC150 C Count Order

import itertools

n = int(input())
p_l = [int(x) for x in input().split()]
q_l = [int(x) for x in input().split()]

per = itertools.permutations(list(range(1,n+1)))
a = 0
b = 0

for i,order in enumerate(per):
    if p_l == list(order):
        a = i
    if q_l == list(order):
        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()))
a=1
b=1
n1=1
for i in range(n):
    n1=n1*(i+1)
for i in range(n):
    t=0
    i1=1
    for j in range(i+1):
        i1=i1*(n-j)
    for j in range(n):
        if i<j and p[i]>p[j]:
            t+=1
    a+=t*n1//i1
for i in range(n):
    t=0
    i1=1
    for j in range(i+1):
        i1=i1*(n-j)
    for j in range(n):
        if i<j and q[i]>q[j]:
            t+=1
    b+=t*n1//i1
c=a-b
if c>0:
    print(c)
else:
    print(-c)