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 math

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

candidate = [[i] for i in range(1, N + 1)]
while len(candidate) < math.factorial(N):
    temp = []
    for c in candidate:
        for i in range(1, N + 1):
            if not i in c:
                temp.append(c + [i])
    candidate = temp

a = candidate.index(P[:-1])
b = candidate.index(Q[:-1])

print(abs(a - b))

Submission status: Accepted

Solution code:
def make(nums, now):
    # print(nums, now)
    if len(nums) == 0:
        dic.append(now)

    for i in range(len(nums)):
        tmp = nums[:i]
        if i+1 < len(nums):
            tmp += nums[i+1:]
        make(tmp, now+[nums[i]])

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
dic = []
original_nums = [i for i in range(1, n+1)]
make(original_nums, [])
for i in range(len(dic)):
    if dic[i] == p:
        num_p = i
    if dic[i] == q:
        num_q = i
# print(num_p, num_q)
print(abs(num_q - num_p))

Submission status: Accepted

Solution code:
from itertools import permutations


def main():
    N = int(input())
    P = ''.join(input().split(' '))
    Q = ''.join(input().split(' '))
    p_rank = 1
    q_rank = 1
    for perm in permutations(map(str, range(1, N + 1)), N):
        perm_str = ''.join(perm)
        if perm_str < P:
            p_rank += 1
        if perm_str < Q:
            q_rank += 1
    print(abs(p_rank - q_rank))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():

    import itertools
    n = int(input())
    permutation_lis = []
    a = 0
    b = 0
    ans_a = 0
    ans_b = 0

    p_lis = list(input().split())
    q_lis = list(input().split())

    permutation_lis = list(itertools.permutations(p_lis))
    permutation_lis.sort()
    #print(permutation_lis)

    for wordorder in permutation_lis:
        a += 1
        b += 1
        #print(p_lis, list(wordorder), p_lis == list(wordorder))
        if p_lis == list(wordorder):
            ans_a = a
        if q_lis == list(wordorder):
            ans_b = b
    print(abs(ans_a-ans_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()))

l = list(itertools.permutations(range(1,n+1),n))
# print(l)
a = l.index(p)
b = l.index(q)


print(abs(b-a))

Submission status: Accepted

Solution code:
#1:50

import itertools

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

ln = list()
for i in range(1, N+1):
    ln.append(i)

all = list(itertools.permutations(ln))

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

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
x = tuple(map(int, input().split()))#(7, 3, 5, 4, 2, 1, 6, 8)
y = tuple(map(int, input().split()))
# y = (3, 8, 2, 5, 4, 6, 7, 1)
for idx, num in enumerate(list(permutations(range(1, N+1), len(range(N)))),start=1):
    if num == x:
        a = idx
    if num == y:
        b = idx
print(abs(a-b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
li = list(range(1, n+1))
ans = []

for i in itertools.permutations(li):
    ans.append(list(i))

print(abs(ans.index(x)-ans.index(y)))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = input().split()
Q = input().split()
N_list = []
ka = 0
kb = 0
k = 1
for i in range(1,N+1):
    N_list.append(i)

for list in itertools.permutations(N_list,N):
    for i in range(N):
        if int(P[i]) == list[i]:
            #print(P[i],list[i])
            ka+=1
        if int(Q[i]) == list[i]:
            kb+=1
        if ka == N:
            a = k
        if kb == N:
            b = k
        #print('++++++++++++')
        #print(a,b)
        #print('++++++++++++')
    k+=1
    ka = 0
    kb = 0
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):
  ans = n
  for i in range(1, n):
    ans *= i
  return ans

def number(lst, n):
  numlst = []
  ans = 1
  for i in range(n):
    numlst.append(i+1)
  for i in range(n):
    for j in range(len(numlst)):
      if lst[i] == numlst[j]:
        ans += j*fact(n-i-1)
        numlst.pop(j)
        break
  return ans

res = number(q, n) - number(p, n)
print(abs(res))

Submission status: Accepted

Solution code:
from itertools import permutations


def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    dic = list(permutations(range(1, N+1), N))
    print(abs(dic.index(tuple(P)) - dic.index(tuple(Q))))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

from itertools import permutations

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

a = l.index(tuple(p))
b = l.index(tuple(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()))
cands = itertools.permutations(range(1,N+1))
cands = [list(e) for e in cands]
print(abs(cands.index(P) - cands.index(Q)))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
t=[i+1 for i in range(N)]
S=list(itertools.permutations(t))
a=0
b=0
for i in range(len(S)):
    if list(S[i])==P:
        a=i
    if list(S[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()))

import math
import itertools

if P == Q:
  print(0)
  exit(0)
count = 0
a,b = 0,0
l = list(range(1,N+1))
for v in itertools.permutations(l):
  count += 1
  if v == P:
    a = count
  elif v == Q:
    b = count

print(abs(a-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()))

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

check_list = list(itertools.permutations(tem_list,N))

for i in range(0,len(check_list)):
    if check_list[i] == P_list:
        tem_1 = i
    if check_list[i] == Q_list:
        tem_2 = i

print(abs(tem_2-tem_1))

Submission status: Accepted

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

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

check_list = list(itertools.permutations(tem_list,N))

for i in range(0,len(check_list)):
    if check_list[i] == P_list:
        tem_1 = i
for j in range(0,len(check_list)):
    if check_list[j] == Q_list:
        tem_2 = j

print(abs(tem_2-tem_1))

Submission status: Accepted

Solution code:
def f(x):
    return x-1
import itertools
import math
N = int(input())
P = list(map(int, input().split()))
P = list(map(f, P))
Q = list(map(int, input().split()))
Q = list(map(f, Q))
S = list(range(N))
permutation = list(itertools.permutations(S))
a,b = 0,0
for i in range(len(permutation)):
    if P == list(permutation[i]):
        a = i
    if Q == list(permutation[i]):
        b = i
print(int(math.fabs(a-b)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

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

print(solve())

Submission status: Accepted

Solution code:
def perm(N):
    if N==0:return 0
    a=1
    for i in range(1,N+1):
        a*=i
    return a

def getposi(P):
    N=len(P)
    l=list(range(1,N+1))
    a = 0
    for i,num in enumerate(P):
        a+=perm(N-i-1)*l.index(num)
        l.remove(num)
    return a

stdin = open(0).read().split('\n')
N = int(stdin[0])
P = list(map(int,stdin[1].split()))
Q = list(map(int,stdin[2].split()))

a=getposi(P)
b=getposi(Q)

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
stdin = open(0).read().split('\n')
N = int(stdin[0])

P=int("".join(list(stdin[1].split())))
Q=int("".join(list(stdin[2].split())))

l = []
for pp in permutations(range(1, N+1)):
    ll = []
    for _p in pp:
        ll.append(str(_p))
    l.append(int("".join(ll)))
a = l.index(P)
b = l.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()))

def kai(n):
  if(n==1 or n==0):
    return 1
  return n*kai(n-1)

op = 1
oq = 1

for i in range(N):
  tp = P[i] - 1
  tq = Q[i] - 1
  for j in range(i+1):
    if (P[j]<P[i]):
      tp -= 1
    if (Q[j]<Q[i]):
      tq -= 1

  op += tp*kai(N-(i+1))
  oq += tq*kai(N-(i+1))

print (abs(oq-op))

Submission status: Accepted

Solution code:
import itertools

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

a = -1
b = -1
ind = 0
for i in itertools.permutations(list(range(1, N+1))):

    if p == i:
        a = ind
    if q == i:
        b = ind
    ind += 1

if a == -1 or b == -1:
    print(a, b)
else:
    print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    a=tuple(map(int, input().split()))
    b=tuple(map(int,input().split()))
    l=list(itertools.permutations(range(1,n+1)))
    aa=l.index(a)
    bb=l.index(b)
    print(abs(aa-bb))
resolve()

Submission status: Accepted

Solution code:
import itertools
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(range(1,n+1))
    a=tuple(map(int, input().split()))
    b=tuple(map(int,input().split()))
    cnt=0
    aa=0
    bb=0
    for i in itertools.permutations(l,n):
        cnt+=1
        if a==i:
            aa=cnt
        if b==i:
            bb=cnt
    print(abs(aa-bb))
resolve()

Submission status: Accepted

Solution code:
from functools import reduce
def dfs(num):
    global count
    if len(str(num)) != len(set(str(num))):
        return

    if len(str(num)) == N:
        if len(set(str(num))) == len(str(num)):
            count += 1
        if num == P:
            ans.append(count)
        if num == Q:
            ans.append(count)
        return

    else:
        for i in range(1, N + 1):
            dfs(num * 10 + i)


N = int(input())
P = int("".join(list(input().split())))
Q = int("".join(list(input().split())))
ans = []
count = 0

dfs(0)
print(abs(ans[0] - ans[1]))

Submission status: Accepted

Solution code:
import itertools
def I(): return int(input())
def LI(): return list(map(int,input().split()))
N = I()
P = LI()
Q = LI()
sequence = [list(x) for x in itertools.permutations(range(1,N+1))]
print(abs(sequence.index(P)-sequence.index(Q)))

Submission status: Accepted

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

n = int(input())
pn = list(map(int, input().split()))
qn = list(map(int, input().split()))

n_list = [x for x in range(1, n+1)]
count = 0 
ans = 0
for i in itertools.permutations(n_list, n):
    count += 1
    if list(i) == pn:
        ans += count
    if list(i) == qn:
        ans -= count

print(abs(ans))

Submission status: Accepted

Solution code:
import itertools


def main():
    # 入力
    n = int(input())

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

    lis = [x for x in range(1, n+1)]
    permutations_lis = itertools.permutations(lis)

    ap = -1
    aq = -1
    count = 0
    for perlist in permutations_lis:
        count += 1
        is_p = True
        is_q = True
        for i in range(n):
            pi = perlist[i]
            if is_p and pi != p[i]:
                is_p = False
            if is_q and pi != q[i]:
                is_q = False
            if not is_p and not is_q:
                break

        if is_p:
            ap = count
        if is_q:
            aq = count

        if ap != -1 and aq != -1:
            break

    print(abs(ap-aq))


main()

Submission status: Accepted

Solution code:
import itertools
def I(): return int(input())
def TI(): return tuple(map(int,input().split()))
N = I()
P = TI()
Q = TI()
sequence = [x for x in itertools.permutations(range(1,N+1))]
print(abs(sequence.index(P)-sequence.index(Q)))

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


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

Submission status: Accepted

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


n_new=list(itertools.permutations(n,N))
map(list,n_new)
a=n_new.index(P)
b=n_new.index(Q)
print(abs(a-b))

Submission status: Accepted

Solution code:
cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))
q = list(map(int, cin[2].split(' ')))

import itertools

a = 0
b = 0
for idx, case in enumerate(itertools.permutations(range(1, n+1))):
    if case == tuple(p):
        a = idx+1
    if case == tuple(q):
        b = idx+1

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools


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

    if P == Q:
        print(0)
        return

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

    print(abs(a-b))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

P_answer = 0
Q_answer = 0
for i, permu in enumerate(itertools.permutations(Nl)):
    if list(permu) == P:
        P_answer = i+1
    if list(permu) == Q:
        Q_answer = i+1

print(abs(P_answer-Q_answer))

Submission status: Accepted

Solution code:
import math
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
total = 1
cnt = 0
p = 0
q = 0
for v in itertools.permutations(range(1, N+1)):
    cnt += 1
    if v == P:
        p = cnt
    if v == Q:
        q = cnt
print(abs(p - q))

Submission status: Accepted

Solution code:
import itertools
def main(n,p,q):
    d = sorted(list(itertools.permutations(p)))
    return abs(d.index(tuple(p)) - d.index(tuple(q)))
    
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

print(main(n,p,q))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
permutation_list = list(itertools.permutations(p, n))
sort_lst = sorted(permutation_list)
x = tuple(p)
y = tuple(q)
a = sort_lst.index(x)
b = sort_lst.index(y)
print(abs((a+1)-(b+1)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math

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

ans = 0


def calc(data):
    ans = 0
    flag = [1]*n
    for i in range(len(data)):
        num = data[i]
        sum_ = sum(flag[:max(0, num-1)])
        ans += sum_*math.factorial(len(data)-i-1)
        flag[num-1] = 0
    return ans


ans = calc(p)-calc(q)

print(abs(ans))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
P = int(''.join(input().split()))
Q = int(''.join(input().split()))

perms = []
for order in permutations(list(map(lambda x:x + 1, range(N)))):
    order = map(str, list(order))
    perms.append(int(''.join(order)))

def binary_search(x):
    left_index = 0
    right_index = len(perms)
    while right_index - left_index > 1:
        mid_index = (left_index + right_index) // 2
        if perms[mid_index] <= x:
            left_index = mid_index
        else:
            right_index = mid_index
    return left_index + 1 #インデックスTo番目は＋１する必要がある
            
print(abs(binary_search(P) - binary_search(Q)))

Submission status: Accepted

Solution code:
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
def count(m):
    num = 1
    for i in range(n-1):
        num += (m[i]-1)*(math.factorial(n-i-1))
        for j in range(i+1,n):
            if m[i] < m[j]:
                m[j] -= 1
    return num
print(abs(count(p)-count(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()))
pi = 0
qi = 0
cnt = 0
for p in permutations(range(1,1+N)):
    cnt += 1
    #print(p)
    if p == P:
        pi = cnt
    if p == Q:
        qi = cnt
        
print(abs(qi-pi))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p_tuple=tuple(map(int,input().split()))
q_tuple=tuple(map(int,input().split()))
sort_list=list(range(1,n+1))
all_list=list(itertools.permutations(sort_list, n))
p_index=all_list.index(p_tuple)
q_index=all_list.index(q_tuple)
print(abs(p_index-q_index))

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
g = [i for i in range(1,n+1)]
m = [i for i in range(1,n+1)]
ansp = 0
ansq = 0
def kai(n):
    k = 1
    for i in range(1,n+1):
        k = k*i
    return k
for i in range(n-1):
    r = g.index(p[i])
    ansp += r*kai(n-1-i)
    g.pop(r)
for i in range(n-1):
    r = m.index(q[i])
    ansq += r*kai(n-1-i)
    m.pop(r)
print(abs(ansp-ansq))

Submission status: Accepted

Solution code:
import itertools

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


    cnt = 1
    ans = 0
    for i in itertools.permutations(range(1, n+1)):
        if i == p:
            ans += cnt
        if i == q:
            ans -= cnt
        cnt += 1


    return abs(ans)

    
print(solve())

Submission status: Accepted

Solution code:
# abc150_c.py
'''
全パターン作ってソートで順番みる
'''
import itertools
N = int(input())
P = tuple(list(map(int,input().split())))
Q = tuple(list(map(int,input().split())))

l = [i+1 for i in range(N)]
target =  itertools.permutations(l)
cnt=0
pcnt = 0
qcnt = 0
for i in target:
    cnt += 1
    if i == P:
        pcnt=cnt
    if i == Q:
        qcnt=cnt

print(abs(pcnt-qcnt))

Submission status: Accepted

Solution code:
import itertools as itr
n = int(input())
first = list(range(1, n + 1))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
posp, posq = 0, 0
num = 0
for i in itr.permutations(first):
  num += 1
  i = list(i)
  if i == p:
    posp = num
  if i == q:
    posq = num
print(abs(posp - posq))

Submission status: Accepted

Solution code:
N=int(input())
P=[int(i) for i in input().split()]
Q=[int(i) for i in input().split()]
from itertools import permutations as perm
lis=[list(i) for i in perm(range(1,N+1),N)]
print(abs(lis.index(P)-lis.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()))

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

ans = 0
k = 0
for A in permutations(l):
    a = list(A)
    k += 1
    pf = True
    qf = True
    for i in range(n):
        if a[i] != p[i]:
            pf = False
        if a[i] != q[i]:
            qf = False
    if pf:
        ans = abs(ans - k)
    if qf:
        ans = abs(ans - k)

print(ans)

Submission status: Accepted

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

from itertools import permutations
from math import factorial

X=[i+1 for i in range(N)]
X_list = list(permutations(X))

P=tuple(P)
Q=tuple(Q)

ans=[0,0]
for j in range(factorial(N)):
    if X_list[j]==P:
        ans[0] = j
        break
for k in range(factorial(N)):
    if X_list[k]==Q:
        ans[1] = k
        break
print(abs(ans[0]-ans[1]))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
# https://atcoder.jp/contests/abc150/tasks/abc150_c

def solve():
    import itertools
    import bisect

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

    lst = [i for i in range(1, N+1)]
    origins = list(itertools.permutations(lst, r=N))
    origins.sort()

    for i, p in enumerate(origins):
        if p == Ps:
            a = i
        if p == Qs:
            b = i

    print(abs(a-b))



if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())

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

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

numbers = list(permutations(N_list,N))
sor_numbers = sorted(numbers)

P_answer = sor_numbers.index(P)
Q_answer = sor_numbers.index(Q)


print(abs(P_answer - Q_answer ))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

import itertools

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

for i in range(len(p)):
    p[i] = list(p[i])


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

Submission status: Accepted

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

import math

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

d1=[i+1 for i in range(n)]
d2=[i+1 for i in range(n)]
mp=[math.factorial(i) for i in reversed(range(n))]

np=0
nq=0
for i in range(n):
    np+=d1.index(p[i])*mp[i]
    d1.remove(p[i])
    nq+=d2.index(q[i])*mp[i]
    d2.remove(q[i])
print(abs(np-nq))

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)))
print(abs(p.index(tuple(P)) - p.index(tuple(Q))))

Submission status: Accepted

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

from itertools import groupby, accumulate, product, permutations, combinations

def solve(P,Q):
    if P==Q:
        return 0
    ans = []
    for i,p in enumerate(permutations(range(1,N+1),N)):
        if list(p)==P or list(p)==Q:
            ans.append(i)
    ans = ans[1]-ans[0]
    return ans
print(solve(P,Q))

Submission status: Accepted

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

a=0
b=0
k=0
for j in itertools.permutations(L):
    
    if list(j)==P:
        a=k
    if list(j)==Q:
        b=k
    k+=1

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

count = int(input())

l1 = tuple(map(int, input().split()))
l2 = tuple(map(int, input().split()))


diff = []
for i in range(count):
    diff.append(i + 1)
data = itertools.permutations(diff)

q1, q2 = 1, 1
ans = 0
for i in data:
    if i == l1:
        ans += q1
    else:
        q1 += 1
    
    if i == l2:
        ans -= q2
    else:
        q2+= 1
    
print(abs(ans))

Submission status: Accepted

Solution code:
import itertools
n = int(input())

p = list(map(int, input().split()))
q = list(map(int, input().split()))
nums = []
for i in range(n):
    nums.append(i + 1)
p_index, q_index = 0, 0
data = itertools.permutations(nums)
index = 0
for i in data:
    
    if list(i) == p:
        p_index = index
    if list(i) == q:
        q_index = index
    index += 1
    
print(abs(p_index - q_index))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces


def getOrder(a: list, n):
    numbers = [i for i in range(1,n+1)]
    
    factorial = 1
    for i in range(1,n):
        factorial *= i

    ret = 0
    for i in range(n):
        ai = a[i]
        idx = numbers.index(a[i])
        ret += idx * factorial
        numbers.pop(idx)
        factorial //= max(1,(n-1-i))# Avoid ZeroDivisionError
    return ret


def main():
    n = ni()
    ps = na()
    qs = na()
    print(abs(getOrder(qs,n) - getOrder(ps,n)))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p_li = int("".join(input().split()))
q_li = int("".join(input().split()))

all_list=itertools.permutations(range(1,n+1))
cnt_p,cnt_q=0,0

for i in all_list:
    i_li = int("".join(map(str, i)))
    if p_li > i_li:
        cnt_p += 1
    if q_li > i_li:
        cnt_q += 1
    

print(abs(cnt_p-cnt_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()))
 
def count(x):
    i = 0
    for perm in permutations(range(1, n+1)):
        if perm == x:
            return i
        i += 1
 
print(abs(count(p) - count(q)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = list(map(int,input().split()))
t = list(map(int,input().split()))
ls=list(itertools.permutations(range(1,n+1)))
for i in range(len(ls)):
    a = map(str,ls[i])
    ls[i] = "".join(a)
a = "".join(map(str,s))
b = "".join(map(str,t))
print(abs(ls.index(b)-ls.index(a)))

Submission status: Accepted

Solution code:
from math import factorial
import copy
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Mp = copy.copy(P)
Mq = copy.copy(Q)
Lp = 0
Lq = 0
for i in range(N):
  if P[i] == min(Mp) and i != N:
    Mp.remove(P[i])
  else:
    Lp += factorial(len(Mp)-1)*(sum(x<P[i] for x in Mp))
    Mp.remove(P[i]) 
  if Q[i] == min(Mq) and i != N:
    Mq.remove(Q[i])
  else:
    Lq += factorial(len(Mq)-1)*(sum(x<Q[i] for x in Mq))
    Mq.remove(Q[i])
print(abs(Lp-Lq))

Submission status: Accepted

Solution code:
import math
import copy
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Mp = copy.copy(P)
Mq = copy.copy(Q)
Lp = 0
Lq = 0
for i in range(N):
  if P[i] == min(Mp) and i != N:
    Mp.remove(P[i])
  else:
    Lp += math.factorial(len(Mp)-1)*(sum(x<P[i] for x in Mp))
    Mp.remove(P[i]) 
  if Q[i] == min(Mq) and i != N:
    Mq.remove(Q[i])
  else:
    Lq += math.factorial(len(Mq)-1)*(sum(x<Q[i] for x in Mq))
    Mq.remove(Q[i])
print(abs(Lp-Lq))

Submission status: Accepted

Solution code:
import itertools

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

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

cnt_1=0
cnt_2=0
k=0

for i in itertools.permutations(a):
    if list(i)==p:
        cnt_1=k
    if list(i)==q:
        cnt_2=k
    k+=1

print(abs(cnt_1-cnt_2))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
p = np.array(input().split(),dtype=int)
q = np.array(input().split(),dtype=int)
kaijo = [1,1,2,6,24,120,720,5040]
pn = 0
qn = 0
ans = 0
for i in range(n-1):
    pi = p[i]-1
    qi = q[i]-1
    for j in range(i):
        if p[i] > p[j]:
            pi -= 1
    for j in range(i):
        if q[i] > q[j]:
            qi -= 1
    pn += pi*kaijo[n-i-1]
    qn += qi*kaijo[n-i-1]

print(abs(pn-qn))

Submission status: Accepted

Solution code:
import itertools

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

L = list(itertools.permutations(range(n)))

P = L.index(tuple(p))
Q = L.index(tuple(q))
print(abs(P-Q))

Submission status: Accepted

Solution code:
import math
import copy
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P.append(N+1)
Q.append(N+1)
Mp = copy.copy(P)
Mq = copy.copy(Q)
Lp = 0
Lq = 0
for i in range(N):
  if P[i] == min(Mp) and i != N-1:
    Mp.remove(P[i])
  elif i == N-1:
    Lp += 1
  else:
    Lp += math.factorial(len(Mp)-2)*(sum(x<P[i] for x in Mp))
    Mp.remove(P[i]) 
for i in range(N):
  if Q[i] == min(Mq) and i != N-1:
    Mq.remove(Q[i])
  elif i == N-1:
    Lq += 1
  else:
    Lq += math.factorial(len(Mq)-2)*(sum(x<Q[i] for x in Mq))
    Mq.remove(Q[i])
print(abs(Lp-Lq))

Submission status: Accepted

Solution code:
def kai(a):
    x=a
    for i in range(a-1,1,-1):
        x*=i
    return x

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

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

cnt_1=0
cnt_2=0

for i in range(n-2):
    cnt_1+=kai(n-i-1)*(a.index(p[i]))
    cnt_2+=kai(n-i-1)*(b.index(q[i]))
    a.remove(p[i])
    b.remove(q[i])

if p[-2]>p[-1]:
    cnt_1+=1

if q[-2]>q[-1]:
    cnt_2+=1

print(abs(cnt_1-cnt_2))