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

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

import itertools

A = list(itertools.permutations(list(range(1, n+1))))
 
a = A.index(P)
b = A.index(Q)
print(abs(a-b))

Submission status: Runtime Error

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

print('=========')

#p[0]が1の時1番目or２番目,2の時3番目or4番目、3の時5番目or6番目

for i in range(n):
    for j in range(n):
        for k in range(n):
            if 
                print(i+1,j+1,k+1)

Submission status: Runtime Error

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)]
B = list(itertools.permutations(A))
ans = abs(B.find(P)-B.find(Q))
print(ans)

Submission status: Runtime Error

Solution code:
import itertools

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

p_index = -1
q_index = -1
combs = itertools.permutations(range(1, n + 1), n)
for index, each in enumerate(combs, start=1):
#    print(index, each)
    if P[0] == each[0] and P[1] == each[1] and P[2] == each[2]:
        p_index = index

    if Q[0] == each[0] and Q[1] == each[1] and Q[2] == each[2]:
        q_index = index
print(abs(p_index - q_index))

Submission status: Runtime Error

Solution code:
from math import factorial
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans = 0
while n > 1:
    o = s[0]
    ans += (o - 1) * factorial(n - 1, True)
    s.pop(0)
    n = len(s)
    if n == 0:
        break
    s = [i if i < o else i - 1 for i in s]
print(ans)

Submission status: Runtime Error

Solution code:
# ABC150C

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans = 0
while n > 1:
    o = s[0]
    ans += (o - 1) * factorial(n - 1, True)
    s.pop(0)
    n = len(s)
    if n == 0:
        break
    s = [i if i < o else i - 1 for i in s]
return ans

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 = [i for i in range(1,n+1)]

for i,per in enumerate(list(permutations(A))):
  if per == P:
    x = i
  if per == Q:
    y = i
  
print(abs(x-y))

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 = [i+1 for i in range(n)]



for i,per in enumerate(list(permutations(A))):
  if per == P:
    x = i
  if per == Q:
    y = i
  
print(abs(x-y))

Submission status: Runtime Error

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

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

from itertools import permutations

for i,per in enumerate(list(permutations(A))):
  if per == P:
    x = i
  if per == Q:
    y = i
  
print(abs(x-y))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;

template<typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T> p) {
    out << "(" << p.first << "," << p.second << ")";
    return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
    for (auto a: v)
        out << a << " ";
    return out;
}

template<typename T>
ostream& operator<<(ostream& out, const set<T>& S) {
    for (auto a: S)
        cout << a << " ";
    return out;
}

template<typename T>
ostream& operator<<(ostream& out, const multiset<T>& S) {
    for (auto a: S)
        cout << a << " ";
    return out;
}

template<typename S, typename T>
ostream& operator<<(ostream& out, const map<S, T>& M) {
    for (auto m: M)
        cout << "(" << m.first << "->" << m.second << ") ";
    return out;
}

template<typename S, typename T>
pair<S, T> operator+(pair<S, T> a, pair<S, T> b) {
    return make_pair(a.first + b.first, a.second + b.second);
}

template<typename S, typename T>
pair<S, T> operator-(pair<S, T> a, pair<S, T> b) {
    return make_pair(a.first - b.first, a.second - b.second);
}

struct StringPermutation {
    vector<long long> fact;

    StringPermutation() {
        fact.resize(21);
        fact[0] = 1;
        for (int i = 1; i <= 20; i++)
            fact[i] = fact[i-1] * i;
    }

    long long query_multiple_combination_value(const vector<int>& A) {
        int total = 0;
        for (auto a: A)
            total += a;
        long long ans = fact[total];
        for (auto a: A)
            ans /= fact[a];
        return ans;
    }

    long long query_string_combination_value(const string& str, int left, int right) {
        map<char, int> M;
        vector<int> A;
        for (int i = left; i <= right; i++)
            M[str[i]]++;
        for (auto p: M)
            A.push_back(p.second);
        return query_multiple_combination_value(A);
    }

    string query_kth_string(string str, long long m) {
        int n = str.size();
        long long cnt = 0;
        vector<long long> A(n);
        for (int i = 0; i < n; i++) {
            sort(str.begin() + i, str.end());
            for (int j = i; j < n; j++) {
                if (j == i || str[j] != str[j-1]) {
                    swap(str[i], str[j]);
                    long long val = query_string_combination_value(str, i + 1, n - 1);
                    A[j] = val;
                    swap(str[i], str[j]);
                } else {
                    A[j] = 0;
                }
            }
            for (int j = i + 1; j < n; j++) {
                A[j] += A[j-1];
            }
            auto itr = lower_bound(A.begin() + i, A.end(), m - cnt);
            int j = itr - A.begin();
            if (str[j] != str[i])
                cnt += A[j-1];
            swap(str[i], str[j]);
        }
        return str;
    }

    long long query_string_index(string str) {
        long long ans = 1;
        int n = str.size();
        for (int i = 0; i < n; i++) {
            set<char> S;
            for (int j = i + 1; j < n; j++) {
                if (str[j] < str[i]) {
                    S.insert(str[j]);
                }
            }
            for (int j = i + 1; j < n; j++) {
                if (S.count(str[j])) {
                    swap(str[i], str[j]);
                    ans += query_string_combination_value(str, i + 1, n - 1);
                    swap(str[i], str[j]);
                    S.erase(str[j]);
                }
            }
        }
        return ans;
    }
};

StringPermutation string_perm;

int solve(string& str1, string& str2) {
    int a = string_perm.query_string_index(str1);
    int b = string_perm.query_string_index(str2);
    return abs(a - b);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n;
    string str1, str2;
    cin >> n;
    for (int i = 0; i < n; i++) {
        char ch;
        cin >> ch;
        str1.push_back(ch);
    }
    for (int i = 0; i < n; i++) {
        char ch;
        cin >> ch;
        str2.push_back(ch);
    }
    cout << solve(str1, str2) << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
pn =0
qn =0
count =0
for x in itertools.permutations(range(1,N+1,1)):
    count+=1
    tmp = " ".join(list(map(str,x)))
    if tmp == P:
        pn = count
    if tmp == Q:
        qn = count
print(abs(qn-pn))

Submission status: Runtime Error

Solution code:
from math import *
from itertools import permutations
n = int(input())
p = list(permutations(list(range(1,n+1))))
a = (int(i) for i in input().split())
b = (int(i)for i in input().split())
print(abs(p.index(a) - p.index(b)))

Submission status: Runtime Error

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

import itertools

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

C = list(itertools.permutations(a))

idxs = []

for i, c in enumerate(C):
    l = list(c)
    if l == p or l == q:
        idxs.append(i)

ans = idxs[1]-idxs[0]

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
p=''.join(list(input().split()))
q=''.join(list(input().split()))
l=[]
for a in itertools.permutations(range(1,n+1),n):
  l.append(a)
ip=l.index(p)
iq=l.index(q)
print(abs(ip-iq))

Submission status: Runtime Error

Solution code:
import math
n=int(input()
p=list(input().split())
q=list(input().split())
cntp=0
cntq=0
for i in range(n-2):
  cntp+=(p[0]-1)*math.factorial(n-i-1)
  cntq+=(q[0]-1)*math.factorial(n-i-1)
print(abs(cntp-cntq))

Submission status: Runtime Error

Solution code:
import itertools,math
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(math.factorial(n)):
    if all(l[i][j]==p[j] for j in range(n)):
        pj=i
    elif all(l[i][j]==q[j] for j in range(n)):
        qj=i
print(abs(pj-qj))

Submission status: Runtime Error

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


def fun(x):
    
    count=0
    
    for k in range(len(x)-1):
        count+=math.factorial(len(x)-1)*sum(i<x[0] for i in x)
        del x[0]
        
    return count+1

a=fun(p)
b=fun(q)

print(abs(a-b))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 03:09:14 2020

@author: Kanaru Sato
"""
import itertools as it

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

count = 0
for sequence in it.permutations(l,n):
    if list(sequence) == p:
        pnum = count
    elif list(sequence) ==q:
        qnum = count
    count += 1

print(abs(pnum-qnum))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:

def dfs(node, paths, visited, n):
  # 全て訪問済みだったら全部を訪問できる経路として1を返す
  if not False in visited:
    return 1

  count = 0

  for nn in paths[node]:
    # 既に訪問済みなら処理をしない
    if(visited[nn]):
      continue

    visited[nn] = True
    count += dfs(nn, paths, visited, n)
    visited[nn] = False

  return count

def main():
  N, M = list(map(int, input().split()))
  paths = [[] for i in range(N)]
  visited = [False] * N

  for i in range(M):
    a, b = list(map(int, input().split()))
    paths[a-1].append(b-1)
    paths[b-1].append(a-1)

  visited[0] = True
  print(dfs(0, paths, visited, N))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import itertools

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

index = []

for i, s in enumerate(itertools.permutations(range(1, n+1),n)):
    if s == p or s == q:
        index.append(i)

print(len(index))

if len(index) != 2:
    print(abs(index[0]-index[1]))
else:
    print('0')

Submission status: Runtime Error

Solution code:
import itertools

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

index = []

for i, s in enumerate(itertools.permutations(range(1, n+1),n)):
    if s == p or s == q:
        index.append(i)

if len(index) != 0:
    print(abs(index[0]-index[1]))
else:
    print('0')

Submission status: Runtime Error

Solution code:
import itertools

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

index = []

for i, s in enumerate(itertools.permutations(range(1, n+1),n)):
    if s == p or s == q:
        index.append(i)

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

Submission status: Runtime Error

Solution code:
import itertools
import math
N = input()
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
permutation = list(itertools(N))
for i in range(len(permutation)):
  if P == permutation[i]:
    a = i
  elif Q == permutation[i]:
    b = i
print(math.fabs(a-b))

Submission status: Runtime Error

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

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

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

Solution code:
N=int(input())
P = input()
P=P.replace(' ','')
P=int(P)
Q = input()
Q=Q.replace(' ','')
Q=int(Q)
A=[]
for i in range(N):
    for j in range(N):
        for k in range(N):
            if k != i and k != j and i != j:
                A += [100*(i+1)+10*(j+1)+(k+1)]
for i in range(len(A)):
    if A[i]==P:
        a = i
    elif A[i]==Q:
        b = i
print(abs(a-b))

Submission status: Runtime Error

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

def calculate(n,p,q):
    x = 0
    y = 0

    for index,ar in enumerate(itertools.permutations([i for i in range(1,n+1)],n)):
        if list(ar) == list(p):
            x = index

        if list(ar) == list(q):
            y = index

    if x >= y:
        print(x-y)
    else:
        print(y-x)
calculate(N,P,Q)

Submission status: Runtime Error

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

for i, j in enumerate(itertools.permutations(N)):
    if j == P:
        a = i + 1
    elif j == Q:
        b = i + 1
print(abs(a - b))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
t = [i for i in range(n)]
li = list(itertools.permutations(t))
p = list(map(int,input().split())
q = list(map(int,input().split())
p = tuple(p)
q = tuple(q)
pp = li.index(p)
qq = li.index(q)
print(abs(pp-qq))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from math import ceil, floor, gcd
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = permutations(list(range(1, n+1)))
    if p == q:
        print(0)
    else:
        for idx, item in enumerate(l):
            if item == p:
                idp = idx
            elif item == q:
                idq = idx
        print(abs(idp - idq))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from math import ceil, floor, gcd
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = permutations(list(range(1, n+1)))
    if p == q:
        print(0)
        exit()
    for idx, item in enumerate(l):
        if item == p:
            idp = idx
        elif item == q:
            idq = idx
    print(abs(idp - idq))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
ans_p = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(p):
    
    index = defa.index(x)
    ans_p += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
ans_q = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(q):
    index = defa.index(x)
    ans_q += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
print(abs(ans_p-ans_q))

Submission status: Runtime Error

Solution code:
 from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
from fractions import gcd
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
a = 0
b = 0
k = 0

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

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)] 
 
x = 0
y = 0
 
m = list(itertools.permutations(l))
 
for i in range(len(m)):
	if m[i] == p:
  		x = i+1
	if m[i] == q:
    	y = i+1
        
print(abs(x-y))

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)] 
 
pi = 0
qi = 0
 
m = list(itertools.permutations(l))
 
for i in range(len(m)):
	if m[i] == p:
  		pi = i+1
	if m[i] == q:
    	qi = i+1
        
print(abs(pi-qi))

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

pi = 0
qi = 0
 
m = list(itertools.permutations(l))


#for i in range(len(m)):
#	if m[i] == p:
#  		pi = i+1
#	if m[i] == q:
#    	qi = i+1
 
#print(abs(pi-qi))

print(m[1])

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

pi = 0
qi = 0
 
m = list(itertools.permutations(l))
 
for i in range(len(m)):
	if m[i] == p:
  		pi = i+1
	if m[i] == q:
    	qi = i+1
 
print(abs(pi-qi))

Submission status: Runtime Error

Solution code:
import math
import itertools

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

pi = 0
qi = 0

for i in range(n):
	l[i] = i+1
  
m = itertools.permutations(l, n)
 
for i in range(n):
	if m[i] == p:
  		pi = i+1
	if m[i] == q:
    	qi = i+1

print(abs(pi-qi))

Submission status: Runtime Error

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

import itertools

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

m = list(itertools.permutations(l))

for i in range(1,math.factorial(n)+1):
	if m[i]==p:
		a=i
        break

for i in range(1,math.factorial(n)+1):
	if m[i]==q:
		b=i
        break        

print(abs(a-b))

Submission status: Runtime Error

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

import itertools

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

m = itertools.permutations(l, n)

m = list(m)

for i in range(1,math.factorial(n)):
	if m[i]==p:
		a=i
        break

for i in range(1,math.factorial(n)):
	if m[i]==q:
		b=i
        break        

print(abs(a-b))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(input())
pi = int(input())
qi = int(input())
 
for i in range(n):
	l[i] = i+1
 
p = list(map(int, input().split()))
q = list(map(int, input().split()))
 
m = itertools.permutations(l, n)
 
for i in range(n):
	if l[i] == p
  	pi = i
    
    if l[i] == q
    qi = i
 
print(abs(pi-qi))

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(input())
pi = int(input())
qi = int(input())

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

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

m = itertools.permutations(l, n)

for i in range(n):
	if l[i] = p
  	pi = i
    
    if l[i] = q
    qi = i

print(abs(pi-qi))

Submission status: Runtime Error

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

A = itertools.permutation(range(1,N))
for i in N:
  if P == A[i]:
    cnt_a = i
  if Q == A[i]:
    cnt_b = i

print(abs(a-b))

Submission status: Runtime Error

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

l = list(range(1,n+1))
dic = {}

for i,j in enumerate(itertools.permutations(l)):
    dic[j] = i+1
print(abs(dic[p]-dic[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, pi in enumerate(permutations(range(1, n+1))):
    if pi == p:
        a = i
    elif pi == q:
        b = i
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()))
U = list(itertools.permutations(range(1, N+1)))
a = U.index(P)
b = U.index(Q)
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()))
p = list(itertools.permutations(P))
a = p.index(P)
b = p.index(Q)
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()))
p = list(itertools.permutations(range(1, N + 1)))
a = p.index(P)
b = p.index(Q)
print(abs(a - b))

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
plis = list(map(int, input().split()))
qlis = list(map(int, input().split()))
lis = list(itertools.permutations(range(1, n+1)))
n = 1
for i in lis:
    if list(i) == plis:
        a = n
        n += 1
    elif list(i) == qlis:
        b = n
        n += 1
    else:
        n += 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()))

cnt=0
for v in itertools.permutations(range(1,n+1), n):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

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

cnt=0
for v in itertools.permutations(range(1,n+1), n):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

print(abs(a-b))

Submission status: Runtime Error

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

cnt=0
for v in itertools.permutations(range(1,N+1), N):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

print(abs(a-b))

Submission status: Runtime Error

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

cnt=0
for v in itertools.permutations(range(1,N+1), N):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

print(abs(a-b))

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

cnt=0
for v in permutations(range(1,N+1), N):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

print(abs(a-b))

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

cnt=0
for v in permutations(range(1,N+1), N):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

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

cnt=0
for v in permutations(range(1,N+1), N):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

print(abs(a-b))

Submission status: Runtime Error

Solution code:
from itertools import permutations

n = int(input())
pq=[list(map(int,list(input()))) for i in range(h)] 

cnt=0
for v in permutations(range(1,N+1), N):
    cnt +=1
    if(pq[0]==v):
        a = cnt
    if(pq[1]==v):
        b = cnt

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

cnt=0
for v in permutations(range(1,N+1), N):
    cnt +=1
    if(p==v):
        a = cnt
    if(q==v):
        b = cnt

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()))
res = list(permutations(range(1,n+1)))
ans = abs(res.index(p) - res.index(q))
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
Not found

Submission status: Runtime Error

Solution code:
n = int(input())
s1 = list(map(int,input().split()))
#print(type(s1))
val1 = 0
for i in range(n):
	val1 = val1 + s1[i] * math.factorial(10-i-1) // math.factorial(10-n)
#	print(math.factorial(10-i-1) // math.factorial(10-n))
print(val1)

Submission status: Runtime Error

Solution code:
n = input()
s1 = map(int,input().split())
print(type(sl))

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

i = 0
a = 0
b = 0

for s in permutaions(range(1,n + 1)):
  for j in range(n):
    if p[j] != s[j]:
      break
  else:
    a = i
  for j in range(n):
    if q[j] != s[j]:
      break
  else:
    b = i
    i += 1
    
print(abs(a-b))

Submission status: Runtime Error

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
np = [i for i in range(1, n+1)]

import itertools

all_list = list(itertools.permutations(np))

cnt=0
for seq in all_list:
  cnt += 1
  if seq == p:
    a = cnt
  if seq == q:
    b = cnt

print(abs(a-b))

Submission status: Runtime Error

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

R = list(itertools.permutations(list(range(1,N+1))))
a = 0
b = 0

for i in range(len(R)):
    if list(R[i]) == P:
        a = i+1
    elif list(R[i]) == Q:
        b = i+1
    if a!=0 and b!=0:
        break
        
print(abs(a-b))

Submission status: Runtime Error

Solution code:
n = int(input())
a = input()
b = input()

import itertools
l = list(itertools.permutations(range(1,n+1))
print(abs(l.index(a)-l.index(b)))

Submission status: Runtime Error

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

int ide(ul n,ll *p,ll *q) {
  for (size_t i = 0; i < n; i++)
  {
    if (p[i]!=q[i]) return 0;
  }
  return 1;
  
}

int main() {
  ul n;
  cin>>n;
  ll p[8],q[8],s[8]={1,2,3,4,5,6,7,8};

  for (size_t i = 0; i < n; i++)
  {
    cin >> p[i];
  }
  for (size_t i = 0; i < n; i++)
  {
    cin >> q[i];
  }
  ll a=-1,b=-1,ct=0;
  do {
    ct++;
    if (a < 0 && ide(n, p, s))
      a = ct;
    if (b < 0 && ide(n, q, s))
      b = ct;

  } while (next_permutation(s,s+n));
  cout<<abs(a-b)<<endl;

  return 0;
}

Submission status: Runtime Error

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

junretu = [i + 1 for i in range(N)]
cnt = 0
for i in itertools.permutations(junretu, N):
    cnt += 1
    if i == p:
        a = cnt
        #cnt += 1
    elif i == q:
        b = cnt
        #cnt += 1
    #cnt += 1
print(abs(a - b))

Submission status: Runtime Error

Solution code:
import numpy as np

def monji(a) :
    D = []
    STR = ''
    for i in range(0,len(a)) :
        for j in range(len(a[i])) :
            if a[i][j] == " " :
                D.append(int(STR))
                STR = ''
            else :
                STR = STR + a[i][j]
    return D

def kaijo(a) :
    k = 1
    for i in range(1,a+1) :
        k = k * i
    return k
  
def zyunretsu(N) :
    len_N = len(N) - 1
    c = 1
    for i in range(0,len(N)) :
        if len_N == 0 :
            break
        a = N[0]
        N_C = sorted(N)
        b = N_C.index(a)
        c = c + kaijo(len_N) * b
        N.remove(a)
        len_N = len_N - 1
    return c
  
a_1 = input()
a_2 = input()
a_3 = input()
b = monji(a_2)
c = monji(a_3)
print(abs(b-c))

Submission status: Runtime Error

Solution code:
import itertools


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

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

    return True


n = int(input())

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

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

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

print(abs(a, b))

Submission status: Runtime Error

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


def main():
    N, *PQ = map(int, open(0).read().split())
    P, Q = PQ[:N], PQ[N:]
    perms = list(permutations(range(1, N + 1)))
    ans = abs(perms.index(P) - perms.index(Q))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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


def main():
    N, *PQ = map(int, open(0).read().split())
    P, Q = PQ[:N], PQ[N:]
    perms = tuple(permutations(range(1, N + 1)))
    ans = abs(perms.index(P) - perms.index(Q))
    print(ans)


if __name__ == "__main__":
    main()