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 sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    P=LI()
    Q=LI()

    a = 0
    b = 0
    for c,it in enumerate(permutations([i+1 for i in range(N)])):
        if P==list(it):
            a = c
        if Q==list(it):
            b = c
    print(abs(a-b))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
from itertools import permutations
sys.setrecursionlimit(300000)


def solve(N: int, P: "List[int]", Q: "List[int]"):
    def equals(x, y):
        for i in range(len(x)):
            if x[i] != y[i] - 1:
                return False
        return True
    perm = list(permutations(list(range(N))))
    a = 0
    b = 0
    for i, p in enumerate(perm):
        if equals(p, P):
            a = i
        if equals(p, Q):
            b = i
    print(abs(a - b))
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
P,Q = [tuple(map(int,input().split())) for i in range(2)]
dic = sorted(itertools.permutation(P,N))
print(abs(dic.index(P)-dic.index(Q)))

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
P,Q = [list(map(int,input().split())) for i in range(2)]
dic = sorted(itertools.permutation(P,N))
print(abs(dic.index(P)-dic.index(Q)))

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
P,Q = [list(map(int,input().split())) for i in range(2)]
dic = sorted(itertools.permutation(P,N))
print(abs(dic.index(P)-dic(index(Q)))

Submission status: Runtime Error

Solution code:
import itertools
import math

n = input()
p = [map(int, input().split())]
q = [map(int, input().split())]
p_no, q_no = 0, 0

per = [c+1 for c in range(n)]
per_list = list(itertools.permutations(per))

for i in range(math.factorial(n)):
    if p == per_list[i]: p_no = i
    if q == per_list[i]: q_no = i

print(abs(p_no - q_no))

Submission status: Runtime Error

Solution code:
import itertools

n = input()
p = [map(int, input().split())]
q = [map(int, input().split())]
p_no, q_no = 0, 0

per = [c+1 for c in range(n)]
per_list = list(itertools.permutations(per))

for i in range(n):
    if p == per_list[i]: p_no = i
    if q == per_list[i]: q_no = i

print(abs(p_no - q_no))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n = I()
lis = list(range(1,n+1))
a,b = None,None
P = readInts()
Q = readInts()
i = 0
for A in permutations(lis,n):
    if list(A) == P:
        a = i
    elif list(A) == Q:
        b = i
    i += 1
print(abs(a-b))

Submission status: Runtime Error

Solution code:
from itertools import permutations
N = int(input())
P = tuple(list(map(int,input().split())))
Q = tuple(list(map(int,input().split())))
cnt = 0
for x in permutations(range(1,N+1),N):
    if P==x:
        indP = cnt
    elif Q==x:
        indQ = cnt
    cnt += 1
print(abs(indP-indQ))

Submission status: Runtime Error

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

    s = itertools.permutations(range(1, n+1))
    l = []
    for v in s:
        l.append(v)
    p_index = l.index(p)
    q_index = l.index(q)

    print(abs(p_index - q_index))

Submission status: Runtime Error

Solution code:
def func(x, y):
  if len(y)==1:
    dic.append(i*10+int(y))
  else:
    for j in range(len(y)):
      func(i*10+int(y[j]), y[:j]+y[j+1:])
dic = []

N = int(input())
func(0, "".join([i in range(1,N+1)]))

A = int(input().replace(" ", ""))
B = int(input().replace(" ", ""))
ans = abs(dic.index(A)-dic.index(B))
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import permutations as p

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

j = 1

for i in p(range(1, N + 1)):
  if i == P: a = j
  elif i == Q: b = j
  j += 1

if a - b >= 0: print(a - b)
else: print(b - a)

Submission status: Runtime Error

Solution code:
def main():
    number = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    num_list = list(range(1, number+1))
    permu = list(itertools.permutations(num_list, number))
    where_p = permu.index(p)
    where_q = permu.index(q)
    print(abs(where_p-where_q))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

void printVector(const vector<int>& vec) {
    for (int value : vec) {
        cout << value << " ";
    }
    cout << endl;
}
int main() {
    int N;
    cin >> N;
    vector<int> P(N);
    vector<int> Q(N);
    vector<int> nums(N);
    int a=0,b=0,j=0;
    for(int i=0;i<N;i++) {
        cin >> P.at(i);
    }
    for(int i=0;i<N;i++) {
        cin >> Q.at(i);
    }
    for(int i=0;i<N;i++) {
        nums.at(i) = i+1;
    }
    do{
        j++;
        if(nums == P) {
            a = j;
        }
        if(nums == Q) {
            b = j;
        }
    }while(next_permutation(nums.begin(), nums.end()));
    cout << abs(b-a) << endl;
}

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int N;
    cin >> N;
    vector<int> P(N), Q(N);
    for (int i = 0; i < N; i++) cin >> P[i];
    for (int i = 0; i < N; i++) cin >> Q[i];
    vector<int> perm(N);
    for (int i = 0; i < N; i++) perm[i] = i + 1;
    int n = 0;
    int a, b;
    do {
        if (perm == P) a = n;
        if (perm == Q) b = n;
        n++;
    } while (next_permutation(perm.begin(), perm.end()));
    cout << abs(a - b) << endl;
}

Submission status: Runtime Error

Solution code:
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
cnt, a, b = 0, 0, 0
for i in itertools.permutations(range(1, N+1)):
    if P == i:
        a = cnt
        if b != 0:
            break
    if Q == i:
        b = cnt
        if a != 0:
            break
    cnt += 1
print(abs(a-b))

Submission status: Runtime Error

Solution code:
n = int(input())
def pos_num(l):
    l_sort = l.copy()
    l_len = len(l)
    l_sort.sort()
    if l_len == 1:
        an = 0
    else:
        #print(l_sort.index(l[0])+1)
        an = (l_sort.index(l[0]))*math.factorial(l_len-1)
    #print(an)
    return an 

    

ls = list(map(int, input().split()))
an = 0
for i in range(n-1):
    b = pos_num(ls[i:])
    an += b
    #print(an)
    
print()
ll = list(map(int, input().split()))
bn = 0
for i in range(n-1):
    b = pos_num(ll[i:])
    bn += b
    #print(bn)

print(abs(an-bn))

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
a=[]
for i in range(n):
    a.append(i+1)
b=[]
for per in itertools.permutatiton(a):
    b.append(list(per))
c=b.index(list(map(int,input().split())))
d=b.index(list(map(int,input().split())))
print(abs(c-d))

Submission status: Runtime Error

Solution code:
import itertools

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

Submission status: Runtime Error

Solution code:
import copy
def func(n):
    result = []
    result.append([1,2])
    result.append([2,1])
    if n == 2:
        return result
    else:
        #for i in range(3,n+1):
        res_list = func(n-1)
        res = []
        for j in range(n):
            for s in res_list:
                st = copy.copy(s)
                st.insert(j,n)
                res.append(st)
        return res

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

s = func(n)
s.sort()
p_i = 0
q_i = 0
for i in range(n):
    #print(s[i])
    if s[i]==p:
        p_i = i
    if s[i]==q:
        q_i = i
print(abs(p_i,q_i))

Submission status: Runtime Error

Solution code:
import copy
def func(n):
    result = []
    result.append([1,2])
    result.append([2,1])
    if n == 2:
        return result
    else:
        #for i in range(3,n+1):
        res_list = func(n-1)
        res = []
        for j in range(n):
            for s in res_list:
                st = copy.copy(s)
                st.insert(j,n)
                res.append(st)
        return res

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

s = func(n)
srt = sorted(s)
p_i = 0
q_i = 0
for i in range(n):
    print(srt[i])
    if srt[i]==p:
        p_i = i
    if srt[i]==q:
        q_i = i
print(abs(p_i,q_i))

Submission status: Runtime Error

Solution code:
from itertools import permutations

N = int(input())
Nlist = [i for i in range(1, int(input())+1)] 
Nlist = list(permutations(Nlist)) 

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

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

Submission status: Runtime Error

Solution code:
# abc150_c.py
# https://atcoder.jp/contests/abc150/tasks/abc150_c

# C - Count Order /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# 大きさ Nの順列 ((1, 2, ..., N) を並び替えてできる数列) P, Qがあります。
# 大きさ Nの順列は N! 通り考えられます。
# このうち、P が辞書順で a 番目に小さく、Q が辞書順で b 番目に小さいとして、|a−b|を求めてください。

# 注記
# 2つの数列 X, Y について、ある整数 k が存在して Xi=Yi (1≤i<k) かつ Xk<Yk が成り立つとき、X は Yより辞書順で小さいと定義されます。

# 制約
#     2≤N≤8
#     P, Qは大きさ Nの順列である。
#     入力は全て整数である。

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N
# P1 P2 ... PN
# Q1 Q2 ... QN

# 出力
# |a−b|を出力せよ。

# 入力例 1
# 3
# 1 3 2
# 3 1 2

# 出力例 1
# 3

# 大きさ 3の順列は、(1, 2, 3)、(1, 3, 2)、(2, 1, 3)、(2, 3, 1)、(3, 1, 2)、(3, 2, 1) の 6 個あります。
# このうち (1, 3, 2) は辞書順で 2 番目、(3, 1, 2) は辞書順で 5 番目なので、答えは |2−5|=3です。

# 入力例 2
# 8
# 7 3 5 4 2 1 6 8
# 3 8 2 5 4 6 7 1

# 出力例 2
# 17517

# 入力例 3
# 3
# 1 2 3
# 1 2 3

# 出力例 3
# 0


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    N = int(lines[0])
    # N, M = list(map(int, lines[0].split()))
    values_p = list(map(int, lines[1].split()))
    values_q = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    # # e.g.1
    # lists = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
    # lists = get_lists(values_p)
    lists = get_lists_simple(values_p)
    log(f'lists=[{lists}]')

    for i in range(len(lists)):
        if lists[i] == values_p:
            p = i
        if lists[i] == values_q:
            q = i
    result = abs(p-q)

    return [result]


# 3 only
def get_lists_simple(values):
    tmps = values.copy()
    tmps.sort()
    N = len(tmps)
    lists = list()
    for n1 in range(N):
        for n2 in range(N):
            if n2 not in [n1]:
                for n3 in range(N):
                    if n3 not in [n1, n2]:
                        log(f'x=[{n1}], y=[{n2}], z=[{n3}]')
                        lists.append([values[n1], values[n2], values[n3]])
    lists.sort()
    return lists


# 8 only
def get_lists_simple(values):
    tmps = values.copy()
    tmps.sort()
    N = len(tmps)
    lists = list()
    for n1 in range(N):
        for n2 in range(N):
            if n2 not in [n1]:
                for n3 in range(N):
                    if n3 not in [n1, n2]:
                        for n4 in range(N):
                            if n4 not in [n1, n2, n3]:
                                for n5 in range(N):
                                    if n5 not in [n1, n2, n3, n4]:
                                        for n6 in range(N):
                                            if n6 not in [n1, n2, n3, n4, n5]:
                                                for n7 in range(N):
                                                    if n7 not in [n1, n2, n3, n4, n5, n6]:
                                                        for n8 in range(N):
                                                            if n8 not in [n1, n2, n3, n4, n5, n6, n7]:
                                                                lists.append([tmps[n1], tmps[n2], tmps[n3], tmps[n4], tmps[n5], tmps[n6], tmps[n7], tmps[n8]])
    return lists
    lists.sort()


def get_lists(values):
    tmps = values.copy()
    tmps.sort()
    n = len(tmps)
    _, _, lists = get_lists_internal(n, values, tmps)
    lists.sort()
    return lists


def get_lists_internal(n, values, args):
    tmps = list()
    for a in range(len(args)):
        for value in values:
            tmp = args.copy()
            tmp.append(value)
            tmps.append(tmp)
    return get_lists_internal(n-1, values, tmps)


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['3', '1 3 2', '3 1 2']
        lines_export = [3]
    if pattern == 2:
        lines_input = ['8', '7 3 5 4 2 1 6 8', '3 8 2 5 4 6 7 1']
        lines_export = [17517]
    if pattern == 3:
        lines_input = ['1', '1 2 3', '1 2 3']
        lines_export = [0]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(3)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys
import time
import math
import itertools as it
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------

N = int(input())
P = inpl()
Q = inpl()
ls = P.copy()
ls.sort()
cnt = 0
for l in it.permutations(ls):
    cnt += 1
    if P == l:
        a = cnt
    if Q == l:
        b = cnt
print(abs(a-b))
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Runtime Error

Solution code:
from itertools import permutations as pe
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    ns=[]
    for i in range(1,n+1):
        ns.append(str(i))
    p=LISTS()
    q=LISTS()
    ct=0
    for v in pe(ns,n):
        v=list(v)
        #print(v,p,q)
        ct+=1
        if p==v:
            a=ct
        elif q==v:
            b=ct

    print(abs(a-b))
if __name__=='__main__':
    do()

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
lst=[i for i in itertools.permutations(range(1,n+1))]
cnt=0
for i in lst:
  if a==i:
    a=cnt
  elif b==i:
    b=cnt
  cnt+=1
print(abs(a-b))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
    int n;
    cin >> n;
    vector<int> p(n), q(n);
    rep(i,n) cin >> p[i];
    rep(i,n) cin >> q[i];
    vector<int> a(n);
    rep(i,n) a[i] = i+1;

    map<vector<int>, int> mp;
    do {
        mp[a] = mp.size();
    } while (next_permutation(a.begin(), a.end()));
    int ans = abs(mp[p]-mp[q]);
    cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
from itertools import permutations

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

l = permutations(range(1,N+1))

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

Submission status: Runtime Error

Solution code:
from itertools import permutations
n = 8
a = map(str, range(1, n+1))
a = "".join(a)
l = list(permutations(a, n))

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

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

Submission status: Runtime Error

Solution code:
import math
import itertools
#k,x = list(list(map(int, input().split())))
n = int(input())
a = list(list(map(int, input().split())))
b = list(list(map(int, input().split())))
ans = 0

for i in itertools.permutations(range(1,n+1,1),n):

    ans +=1
    if list(i) == a:
        ansa = ans
    elif list(i) == b:
        ansb = ans


print(abs(ansa-ansb))

Submission status: Runtime Error

Solution code:
import itertools as itool
N=int(input())
plist=list(map(int, input().split()))
qlist=list(map(int, input().split()))
mylist=list(itool.permutations(range(1,n+1)))
print(abs(mylist.index(p)-mylist.index(q)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c,d=0,0
def kaijyou(x):
  y=1
  for i in range(1,x+1):
    y*=i
  return y
for i in range(n):
  c+=kaijyou(n-i-1)*(max(0,a[i]-1))
for i in range(n):
  d+=kaijyou(n-i-1)*(max(0,b[i]-1))
print(abs(a-b))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
from itertools import permutations

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

l1=list()

for i in permutations(s):
  l1.append(i)
  
print(abs(l1.find(s)-l1.find(h)))

Submission status: Runtime Error

Solution code:
import itertools

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


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

Submission status: Runtime Error

Solution code:
import itertools

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


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

Submission status: Runtime Error

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

Submission status: Runtime Error

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
    elif i == q:
        b = count
    count += 1
print(abs(a - b))

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
ls = list(itertools.permutations(range(1,N+1)))
print(abs(ls.index(P) - ls.index(Q)))

Submission status: Runtime Error

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
    elif v==Q:
        b=v_idx
        
print(abs(a-b))

Submission status: Runtime Error

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: Runtime Error

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(math.abs(P-Q))

Submission status: Runtime Error

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: Runtime Error

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

A = list(itertools.permutations(P))
A.sort()
print(abs(A.index(tuple(P)) - A.index(turple(Q))))

Submission status: Runtime Error

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

def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

print(abs(f(P)-f(Q)))

Submission status: Runtime Error

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

def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

print(f(P)-f(Q))

Submission status: Runtime Error

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

def mul(n):
  s=1
  for t in range(n):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

print(f(P)-f(Q))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
total1=0
total2=0
def pon(p):
  s=1
  for i in range(1,p+1):
  s*=i
  return s
x=1
y=1
for i in a:
  total1+=(i-1)*pon(n-x)
  x+=1
for i in b:
  total2+=(i-1)*pon(n-y)
  y+=1
print(abs(total1-total2))

Submission status: Runtime Error

Solution code:
import itertools

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
# 多分1~4で3!の順列になる
ls = list(itertools.permutations(range(1,n))) #順列生成
# 絶対値absで辞書順のpからqをひく
print(abs(ls.index(p)-ls.index(q)))

Submission status: Runtime Error

Solution code:
import itertools

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

ls = list(itertools.permutations(range(n))) #順列生成
# 絶対値absで辞書順のpからqをひく
print(abs(ls.index(p)-ls.index(q)))

Submission status: Runtime Error

Solution code:
import itertools

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

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

for i in X:
    if list(i) == P:
        a = X.index(i) + 1
    elif list(i) == Q:
        b = X.index(i) + 1

print(abs(a - b))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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)]
count = 0

for i in itertools.permutations(l, n):
    count += 1
    if i == p:
        a = count
    elif i == q:
        b = count

print(max(a, b) - min(a, b))

Submission status: Runtime Error

Solution code:
import math
n,p,q=[list(map(int, i.split())) for i in open(0)]
def ps(l):
    s=0
    t=list(range(1,n+1))
    for i in range(n):
        s+=t.index(l[i])*math.factorial(n-i-1)
        t.remove(l[i])
print(abs(ps(p)-ps(q)))

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
X = itertools.permutations(range(1, N+1))
a = X.index(A)
b = X.index(B)
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools
N=int(input())
candi=[str(i) for i in range(1,N+1)]
P="".join(input().split())
Q="".join(input().split())
count=0

#候補列挙
for j in itertools.permutations(candi,N):
  k="".join(list(j))
  count+=1
  if k==P:
    a=count
  elif k==Q:
    b=count
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))


factorial[0] = 1
for i in range(1,9):
	cumul *= i
	factorial[i] = cumul


def f(p):
	if len(p) == 1: return 0
	sp = sorted(p)
	for i,e in enumerate(sp):
		if e == p[0]:
			hm = i
			break 

	return i * factorial[len(p) -1] + f(p[1:])


print abs(f(p) - f(q))

Submission status: Runtime Error

Solution code:
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))

factorial = {}
factorial[0] = 1
for i in range(9):
	cumul *= i
	factorial[i] = cumul


def f(p):
	if len(p) == 1: return 0
	sp = sorted(p)
	for i,e in enumerate(sp):
		if e == p[0]:
			hm = i
			break 

	return i * factorial[len(p) -1] + f(p[1:])


print abs(f(p) - f(q))

Submission status: Runtime Error

Solution code:
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))


factorial[0] = 1
for i in range(9):
	cumul *= i
	factorial[i] = cumul


def f(p,n):
	if len(p) == 1: return 0
	sp = sorted(p)
	for i,e in enumerate(sp):
		if e == p[0]:
			hm = i
			break 

	return i * factorial[len(p) -1] + f(p[1:],n)


print abs(f(p,n) - f(q,n))

Submission status: Runtime Error

Solution code:
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=i+1
from itertools import permutations
templ=list(permutations(temp,n) #長さnのtmpの順列リストを取る　どうやら昇順で取ってくれるらしい
for i in range(0,len(templ)):
  if templ[i]==p:
    a=i#そのまま大きさになる
  if templ[i]==q:
    b=i
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools


def main():
    N = int(input())
    tup1 = tuple(map(int, input().split))
    tup2 = tuple(map(int, input().split))
    factli = list(itertools.permutations(list(range(1, N + 1))))
    print(abs(factli.index(tup1) - factli.index(tup2)))



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import numpy

rl = sys.stdin.readline

r = map(int, rl().split())
n1 = int(rl().strip())
n2 = int(rl().strip())
li = itertools.combinations(n1, r)
print(abs(li.index(n1) - li.index(n2)))