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

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

ans1 = []
ans2 = []
for i in itertools.permutations(p):
    ans1.append(i)
for i in itertools.permutations(q):
    ans2.append(i)   
    
ans1 = sorted(ans1)
ans2 = sorted(ans2)

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

for i in range(len(ans1)):
    if ans1[i] == p:
        break
for j in range(len(ans2)):
    if ans2[j] == q:
        break

print(abs(i - j))

Submission status: Accepted

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

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

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

if __name__ == '__main__':
    n = int(input())
    s1 = [int(i) for i in input().split()]
    s2 = [int(i) for i in input().split()]
    l = []
    for i in range(n):
        l.append(i+1)
    g = []

    for v in itertools.permutations(l, n):
        h = ""
        for i in v:
            h=h+str(i)
        g.append(h)
    g.sort()

    index1 = ""
    index2 = ""
    for i in s1:
        index1 = index1 +str(i)
    for i in s2:
        index2 = index2 +str(i)

    print(abs(g.index(index1) - g.index(index2)))

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)))
Y = 1 
for i in range(1,N+1):
    Y *= i
for i in range(Y):
    if list(X[i]) == P:
        p = i
    elif list(X[i]) == Q:
        q = i
if P == Q:
    ans = 0
else:
    ans = abs (p - q)
print(ans)

Submission status: Accepted

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

set_p = set()
set_q = set()
factorial = [1, 1, 2, 6, 24, 120, 720, 5040]
for j in range(N):
    set_p.add(j + 1)
    set_q.add(j + 1)

order_p = 0
order_q = 0
for i in range(N):
    set_p.discard(P[i])
    set_q.discard(Q[i])
    new_set_p = set()
    new_set_q = set()
    new_set_p = set() | set_p
    new_set_q = set() | set_q
    numofbig_p = 0
    numofbig_q = 0

    for k in new_set_p:
        if k > P[i]:
            numofbig_p += 1
    for h in new_set_q:
        if h > Q[i]:
            numofbig_q += 1
    order_p += numofbig_p * factorial[N - i - 1]
    order_q += numofbig_q*factorial[N-i-1]

ans = abs(order_p-order_q)
print(ans)

Submission status: Accepted

Solution code:
import itertools

n=int(input())

a="".join(input().split())
b="".join(input().split())
#print(a,b)

nums=[str(i) for i in range(1,n+1)]
#print(nums)
ary=[]
cnt=1
a1=0
b1=0
for balls in itertools.permutations(nums, n):
    #print(int("".join(list(balls))))
    if str(a) == str(int("".join(list(balls)))):
        a1=cnt
        #print("a1")
    if str(b) == str(int("".join(list(balls)))):
        b1=cnt
        #print("b1")
    cnt+=1

print(abs(a1-b1))

Submission status: Accepted

Solution code:
import math
import numpy as np

N = int(input())
list_p = list(map(int, input().split(" ")))
list_q = list(map(int, input().split(" ")))
a = 0
b = 0
for i in range(N) :
  a += math.factorial(N-i-1) * list(np.argsort(list_p)).index(0)
  b += math.factorial(N-i-1) * list(np.argsort(list_q)).index(0)
  list_p = list_p[1:]
  list_q = list_q[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()))

def fact(n):
    x = 1
    for i in range(1,n+1):
        x *= i
    return x

a = [1,2,3,4,5,6,7,8]
pj = 0
for i in range(0,n-1):
    pj += fact(n-i-1)*a[p[i]-1]
    for j in range(p[i]-1,8):
        a[j] -= 1

a = [1,2,3,4,5,6,7,8]
qj = 0
for i in range(0,n-1):
    qj += fact(n-i-1)*a[q[i]-1]
    for j in range(q[i]-1,8):
        a[j] -= 1


print(abs(pj - qj))

Submission status: Accepted

Solution code:
import math

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
li=list(range(1,n+1))
def f(id,i):
    idd = li.index(id)
    li.pop(li.index(id))
    ar = n - 1 - i
    return idd * math.factorial(ar)

sum1=1
sum2=1
for i in range(n):
    sum1+=f(p[i],i)
li=list(range(1,n+1))
for i in range(n):
    sum2+=f(q[i],i)
print(abs(sum1-sum2))

Submission status: Accepted

Solution code:
import itertools

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

result = list(itertools.permutations([i for i in range(1, n+1)], n))
# 中身がtuppleなのでlistに変換
result = list(map(lambda x: list(x), result))
a = result.index(p_list)
b = result.index(q_list)
print(abs(a-b))

Submission status: Accepted

Solution code:
def permutation_rank(p):
    n = len(p)
    fact = 1
    for i in range(2, n):
        fact *= i
    r = 0
    digits = list(range(n))
    for i in range(n-1):
        q = digits.index(p[i])
        r += fact * q
        del digits[q]
        fact //= (n - 1 - i)
    return r


N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P = [x - 1 for x in P]
Q = [x - 1 for x in Q]

print(abs(permutation_rank(P) - permutation_rank(Q)))

Submission status: Accepted

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

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

p = list(itertools.permutations(l, N)) 

print(abs((p.index(tuple(P)) + 1) - (p.index(tuple(Q)) + 1 )))

Submission status: Accepted

Solution code:
import sys, math
ip = sys.stdin.readline

def get_key(L:list):
    if len(L) == 0:
        return 1
    else:
        s = L[0]
        l_sorted = sorted(L, reverse=False)
        p = l_sorted.index(s)
        return p * math.factorial(len(l_sorted)-1) + get_key(L[1:])

N = int(ip().rstrip())
P = list(map(int, ip().rstrip().split()))
Q = list(map(int, ip().rstrip().split()))
key_P, key_Q = get_key(P), get_key(Q)
print(abs(key_P-key_Q))

Submission status: Accepted

Solution code:
import itertools

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

N_arr = []
for i in range(N):
    N_arr.append(i+1)

index_P = 0
index_Q = 0    

tmp = 1
for i in itertools.permutations(N_arr):

    if list(i) == P:
        index_P = tmp
    if list(i) == Q:
        index_Q = tmp 
    tmp += 1  
print(abs(index_P-index_Q))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
a, b = tuple(map(int, input().split())), tuple(map(int, input().split()))
table = [x for x in range(1, n + 1)]
table2 = list(itertools.permutations(tuple(table)))


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

Submission status: Accepted

Solution code:
import math

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

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

p = 0
for i in range(N):
  p += v.index(P[i]) * math.factorial(N-i-1)
  v.remove(P[i])

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

q = 0
for i in range(N):
  q += v.index(Q[i]) * math.factorial(N-i-1)
  v.remove(Q[i])

print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
a=[]
for i in range(1,n+1):
  a.append(i)
aa=list(itertools.permutations(a))

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

ap=aa.index(p)
aq=aa.index(q)

print(abs(ap-aq))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(input().split())
q = tuple(input().split())
count_p = 0
count_q = 0
count = 1
for i in itertools.permutations(sorted(p)):
  if i == p:
    count_p = count
  if i == q:
    count_q = count
  count += 1
print(abs(count_p - count_q))

Submission status: Accepted

Solution code:
import math


def index(array, indexes):
    if not len(array):
        return 0
    s = array.pop()
    count = 0
    for i in range(s - 1):
        if indexes[i]:
            count += 1
    indexes[s - 1] = False
    return count * math.factorial(len(array)) + index(array, indexes)


n = int(input())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
p.reverse()
q.reverse()
pi = index(p, [True for _ in range(n)])
qi = index(q, [True for _ in range(n)])
print(abs(pi - qi))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p_L = list(map(int, input().split()))
q_L = list(map(int, input().split()))
p_val = 0
q_val = 0

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

for i in range(len(all_list)):
    if all_list[i] == p_L:
        p_val = i
        
    if all_list[i] == q_L:
        q_val = i


        
print(max(p_val-q_val, q_val-p_val))

Submission status: Accepted

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

fac = [0] * 8
fac[1] = 1
for i in range(2, N):
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))
for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[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()))
r = sorted(p)
i = 0
for v in itertools.permutations(r):
  i+=1
  if list(v) == p:
    a = i
  if list(v) == 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()))

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

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

Submission status: Accepted

Solution code:

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

def calc(xs):
    used = [False] * len(xs)
    ans = 0
    for i in range(len(xs)-1):
        #print(used)
        j = 0
        for k in range(xs[i]):
            #print(k)
            if not used[k]:
                j+=1
        #print(i, xs[i], j, len(xs)-i-1, (j-1))
        ans += factorial(len(xs)-i-1) * (j-1)
        used[xs[i]-1] = True
    return ans

words = lambda t : list(map(t, input().split()))
n = int(input())
a = words(int)
b = words(int)
#print(calc(b))
print(abs(calc(b) - calc(a)))

Submission status: Accepted

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

pjun=[i+1 for i in range(n)]
qjun=[i+1 for i in range(n)]

pord=0
qord=0
for i in range(n):
    pi=p[i]
    pord+=pjun.index(pi)*math.factorial(len(pjun)-1)
    pjun.remove(pi)

    qi=q[i]
    qord+=qjun.index(qi)*math.factorial(len(qjun)-1)
    qjun.remove(qi)

print(abs(pord-qord))

Submission status: Accepted

Solution code:
import itertools

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

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

per = itertools.permutations(list, n)

cnt = 0
for i in per:
	now = [i[j] for j in range(n)]
	if now == p:
		a = cnt
	if now == q:
		b = cnt
	cnt += 1

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

a,b=0,0
for num in range(len(x)):
    if p==list(x[num]):
        a=num+1
        if p==q:
            b=num+1
            print(abs(a-b))
            exit()
    elif q==list(x[num]):
        b=num+1

print(abs(a-b))

Submission status: Accepted

Solution code:
n = int(input())
arrp = tuple(map(int,input().split()))
arrq = tuple(map(int,input().split()))

import itertools
for i,j in enumerate(itertools.permutations(range(1,n+1),n)):
    if j == arrp:
        a = i
    if j == arrq:
        b = i
        
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
num=list(range(1,n+1))
c=list(itertools.permutations(num))
aa=c.index(tuple(a))
bb=c.index(tuple(b))
Ans=(aa+1)-(bb+1)
if Ans>=0:
  print(Ans)
else:
  print(-Ans)

Submission status: Accepted

Solution code:
import itertools 

def main():
    N = int(input())
    P = [int(i) for i in input().split()]
    Q = [int(i) for i in input().split()]
    P = tuple(P)
    Q = tuple(Q)

    num = [i for i in range(1,N+1)]
    num_ = list(itertools.permutations(num, N))
    
    a = num_.index(P)
    b = num_.index(Q)

    print(abs(a - b))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

import itertools

p = 0
q = 0
n = list(itertools.permutations(range(1,N+1)))
#print(n)
for i in range(len(n)):
    if list(n[i]) == P:
        p = i+1
    if list(n[i]) == Q:
        q = i+1

print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools
import numpy as np

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

A = [int(x+1) for x in range(N)]

array = list(itertools.permutations(A))

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

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

n = int(input())

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

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

a = 0
b = 0

for i, j in enumerate(itertools.permutations(n_list)):
    if j == p:
        a = i
    if q == j:
        b = i
        
print(abs(a - b))

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

Submission status: Accepted

Solution code:
import math
import itertools


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

    # n_iterals = math.factorial(N)
    # iterals = list(range(1, n_iterals+1))

    combinations = []
    for c in itertools.permutations(range(1, N+1), N):
        c_s = map(str, c)
        combinations.append("".join(c_s))

    a = combinations.index("".join(P))
    b = combinations.index("".join(Q))

    print(abs(a-b))


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

ptnp = list(permutations(p))
ptnq = list(permutations(q))

ptnp.sort()
ptnq.sort()

a = 0

for i in range(len(ptnp)):
    if ptnp[i] == p:
        a = i
        break

b = 0

for i in range(len(ptnq)):
    if ptnq[i] == q:
        b = i
        break

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

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

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

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
y = [num for num in range(1, n+1)]
permutation_n = list(itertools.permutations(y, n))
a=b=0
count=1
for permu in permutation_n:
  if permu == p:
    a = count
  if permu == q:
    b = count
  if a > 0 and b > 0 :
    break
  count+=1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())
S = tuple(map(int, input().split()))
T = tuple(map(int, input().split()))

input_list = [x + 1 for x in range(N)]


per = list(itertools.permutations(input_list))
f = 0
s = 0
for i, v in enumerate(per):
    if v == S:
        f = i + 1
    if v == T:
        s = i + 1

print(int(math.fabs(f - s)))

Submission status: Accepted

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

if N == 2:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            count += 1
            if [i+1, j+1] == P:
                a = count
            if [i+1, j+1] == Q:
                b = count
elif N == 3:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                count += 1
                if [i+1, j+1, k+1] == P:
                    a = count
                if [i+1, j+1, k+1] == Q:
                    b = count
elif N == 4:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    count += 1
                    if [i+1, j+1, k+1, l+1] == P:
                        a = count
                    if [i+1, j+1, k+1, l+1] == Q:
                        b = count
elif N == 5:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        count += 1
                        if [i+1, j+1, k+1, l+1, m+1] == P:
                            a = count
                        if [i+1, j+1, k+1, l+1, m+1] == Q:
                            b = count
elif N == 6:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        for n in range(N):
                            if n == m or n == l or n == k or n == j or n == i: continue
                            count += 1    
                            if [i+1, j+1, k+1, l+1, m+1, n+1] == P:
                                a = count
                            if [i+1, j+1, k+1, l+1, m+1, n+1] == Q:
                                b = count
elif N == 7:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        for n in range(N):
                            if n == m or n == l or n == k or n == j or n == i: continue
                            for o in range(N):
                                if o == n or o == m or o == l or o == k or o == j or o == i: continue
                                count += 1
                                if [i+1, j+1, k+1, l+1, m+1, n+1, o+1] == P:
                                    a = count
                                if [i+1, j+1, k+1, l+1, m+1, n+1, o+1] == Q:
                                    b = count
elif N == 8:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        for n in range(N):
                            if n == m or n == l or n == k or n == j or n == i: continue
                            for o in range(N):
                                if o == n or o == m or o == l or o == k or o == j or o == i: continue
                                for p in range(N):
                                    if p == o or p == n or p == m or p == l or p == k or p == j or p == i: continue
                                    count += 1
                                    if [i+1, j+1, k+1, l+1, m+1, n+1, o+1, p+1] == P:
                                        a = count
                                    if [i+1, j+1, k+1, l+1, m+1, n+1, o+1, p+1] == Q:
                                        b = count

print(abs(a - b))

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(p)
l.sort()
pp,qq='',''
for i in range(n):
  pp+=str(p[i])
  qq+=str(q[i])
ll=permutations(l)
arr=[]
for i in ll:
  x=''
  for j in i:
    x+=str(j)
  arr.append(x)
print(abs(arr.index(pp)-arr.index(qq)))

Submission status: Accepted

Solution code:
import math

n = input()

p = input().split(" ")
q = input().split(" ")

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

def dictoder(x):
  r = 0
  t = list(range(len(x)))
  t = [i + 1 for i in t]
  
  for i in range(len(x)):
    r = r + math.factorial(n - 1 - i) * t.index(x[i])
    t.remove(x[i])
    
  return(r + 1)

print(abs(dictoder(p) - dictoder(q)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import itertools

N = int(input())
P_tuple = tuple(list(map(int, input().split())))
Q_tuple = tuple(list(map(int, input().split())))

N_list = [i for i in range(1, N + 1)]
ans = 0
counter = 0
if P_tuple == Q_tuple:
    print(0)
else:
    for v_tuple in itertools.permutations(N_list):
        counter += 1
        if P_tuple == v_tuple:
            ans -= counter
        elif Q_tuple == v_tuple:
            ans += counter
    print(abs(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()))

L=[_+1 for _ in range(n)]

a=1
for i in list(permutations(L)):
    if i==P:
        break
    a+=1

b=1
for i in list(permutations(L)):
    if i==Q:
        break
    b+=1

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
A = tuple(( str(i) for i in range(1, N+1) ))
P = tuple(input().split())
Q = tuple(input().split())

X = list(permutations(A))
count = 0

for x in X:
    if x >= min(P, Q) and x < max(P, Q):
        count = count+1
    elif x >= max(P, Q):
        break

print(count)

Submission status: Accepted

Solution code:
import itertools

n = int(input())
ps = input()
qs = input()

p = list(map(int,ps.split(' ')))
q = list(map(int,qs.split(' ')))
pn = 0
qn = 0

for x,a in enumerate(list(itertools.permutations([i+1 for i in range(n)],n))):
  if list(a)==p:
    pn = x
  if list(a)==q:
    qn = x

print(abs(pn-qn))

Submission status: Accepted

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

import itertools
seq=[i for i in range(1,n+1)]
#print(list(itertools.permutations(seq)))
li=list(itertools.permutations(seq))
p=tuple(pl)
q=tuple(ql)

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

print(abs(a-b))

Submission status: Accepted

Solution code:
def main():
    import sys
    from itertools import permutations
    input = sys.stdin.readline

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

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

    l = list(range(1, N+1))
    a = b = 0
    s_list = []
    for s in permutations(l):
        s_list.append(''.join(map(str, s)))
    s_list.sort()

    for i, s in enumerate(s_list):
        if s == P:
            a = i
        if s == Q:
            b = i
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

def BIT_update(tree, x):
    while x <= N:
        tree[x] += 1
        x += x & (-x)

def BIT_sum(tree, x):
    s = 0
    while x:
        s += tree[x]
        x -= x & (-x)
    return s


tree = [0] * (N + 1)
ans1 = 0
for i, x in enumerate(P):
    rank = x - BIT_sum(tree, x)
    ans1 += fact[N - i - 1] * (rank - 1)
    BIT_update(tree, x)

tree = [0] * (N + 1)
ans2 = 0
for i, x in enumerate(Q):
    rank = x - BIT_sum(tree, x)
    ans2 += fact[N - i - 1] * (rank - 1)
    BIT_update(tree, x)
print(abs(ans1-ans2))

Submission status: Accepted

Solution code:
import copy
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
pp=copy.copy(p)
qq=copy.copy(q)
pp.sort()
qq.sort()

def fact(x):
    if x<1:
        return 1
    return x*fact(x-1)

for i in range(n):
    f=fact(n-(i+1))
    ppp=p[i]
    qqq=q[i]
    ap=1
    bq=1
    for j in range(len(pp)):
        if j<len(pp):
            if ppp==pp[j]:
                pp.pop(j)
                ap=j+1
        if j<len(qq):
            if qqq==qq[j]:
                qq.pop(j)
                bq=j+1
    a+=f*ap
    b+=f*bq
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 count(r):
    c = 0
    li = list(range(1,n+1))
    for i in range(n-1):
        c+=li.index(r[i])*math.factorial(n-i-1)
        li.remove(r[i])
    return c

print(abs(count(p)-count(q)))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools

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

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

answer = abs(p - q)
print(answer)

Submission status: Accepted

Solution code:
import math
n = int(input())
count_p = 1
count_q = 1
p_list = [i+1 for i in range(n)]
q_list = [i+1 for i in range(n)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
for i in range(n):
    count_p += p_list.index(p[i])*math.factorial(n-i-1)
    p_list.remove(p[i])
    count_q += q_list.index(q[i])*math.factorial(n-i-1)
    q_list.remove(q[i])
print(abs(count_p-count_q))

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input());per = [i for i in permutations(range(1,n+1))];
p = tuple(i for i in map(int,input().split()))
q = tuple(i for i in map(int,input().split()))
print(abs(per.index(p)-per.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()))

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

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

base=list(itertools.permutations(P))
base.sort()


ans=abs(base.index(P)-base.index(Q))

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

    a = 0
    b = 0
    cnt = 0
    for R in itertools.permutations(range(1, N+1), N):
        R = [r for r in R]
        cnt += 1
        if R == P:
            a = cnt
        if R == Q:
            b = cnt
        # print(R)
    
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial
n = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
a = 0
b = 0

for i in range(n):
    a+= (p[i]-1)*factorial(n-1-i)
    b+= (q[i]-1)*factorial(n-1-i)
    for j in range(n):
        if p[i] < p[j]:
            p[j] -= 1
        if q[i] < q[j]:
            q[j] -= 1

print(abs(a-b))

Submission status: Accepted

Solution code:
from math import factorial
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P1 = [1,2,3,4,5,6,7,8]
Q1 = [1,2,3,4,5,6,7,8]
Pcnt = 0
Qcnt = 0
for i in range(N-1):
  Pcnt += P1.index(P[i])*(factorial(N-1-i))
  P1.remove(P[i])
  Qcnt += Q1.index(Q[i])*(factorial(N-1-i))
  Q1.remove(Q[i])
print(abs(Pcnt-Qcnt))

Submission status: Accepted

Solution code:
import math
n = int(raw_input())
p = map(int,raw_input().split())
q = map(int,raw_input().split())
p_n = 0
q_n = 0
n_list = [i for i in range(1,n+1)]
f_list = [math.factorial(i) for i in range(n)]
f_list = f_list[::-1]
for a in range(n):
  p_n += n_list.index(p[a])*f_list[0]
  n_list.remove(p[a])
  f_list.pop(0)
  
n_list = [i for i in range(1,n+1)]
f_list = [math.factorial(i) for i in range(n)]
f_list = f_list[::-1]
for a in range(n):
  q_n += n_list.index(q[a])*f_list[0]
  n_list.remove(q[a])
  f_list.pop(0)
#print p_n,q_n
print abs(p_n - q_n)

Submission status: Accepted

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


import itertools

f=list(itertools.permutations(p))

newf=[]

for i in range(len(f)):
    ff=""
    for ii in range(n):
        ff+=f[i][ii]
    newf.append(ff)

newf=[int(i) for i in newf]
newf.sort()

pp=""
for i in p:
    pp+=i
p=int(pp)
qq=""
for i in q:
    qq+=i
q=int(qq)


a=newf.index(p)
b=newf.index(q)
print(abs(a-b))

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()]
w=sorted(p)
p=tuple(p)
q=tuple(q)
ans=[0,0]
for a,i in enumerate(list(itertools.permutations(w))):
    if i==p:
        ans[0]=a
    if i==q:
        ans[1]=a
print(abs(ans[0]-ans[1]))

Submission status: Accepted

Solution code:
import itertools

N = input()
P = input()
Q = input()
PList = P.split()
QList = Q.split()

PQList = []
for i in itertools.permutations(range(1,int(N) + 1)):
    x = list(i)
    x_str = [str(n) for n in x]
    PQList.append(x_str)


a = PQList.index(PList) + 1
b = PQList.index(QList) + 1
print(abs(a - b))