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:
def fact(n):
  ret=1
  for i in range(2.n+1):
    ret=ret*i
  return ret

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
cnt1=0
cnt2=0
checked1=[0]*(n+1)
checked2=[0]*(n+1)
for i in range(n):
  checked1[arr1[i]]=1
  checked2[arr2[i]]=1
  cnt1+=(arr1[i]-1-sum(checked1[:arr1[i]]))*fact(n-i-1)
  cnt2+=(arr2[i]-1-sum(checked2[:arr2[i]]))*fact(n-i-1)
print(abs(cnt1-cnt2))

Submission status: Runtime Error

Solution code:
def fact(n):
  ret=1
  for i in range(2.n+1):
    ret=ret*i
  return ret

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
cnt1=0
cnt2=0
checked1=[0]*(n+1)
checked2=[0]*(n+1)
for i in range(n):
  checked1[arr1[i]]=1
  checked2[arr2[i]]=1
  cnt1+=(arr1[i]-1-sum(checked1[:arr1[i]]))*fact(n-i-1)
  cnt2+=(arr2[i]-1-sum(checked1[:arr2[i]]))*fact(n-i-1)
print(abs(cnt1-cnt2))

Submission status: Runtime Error

Solution code:
def fact(n):
  ret=1
  for i in range(2.n+1):
    ret=ret*i
  return ret

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
cnt1=0
cnt2=0
for i in range(n):
  cnt1+=(arr1[i]-1)*fact(n-i-1)
  cnt2+=(arr2[i]-1)*fact(n-i-1)
print(abs(cnt1-cnt2))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
int jc(int n){
	if(n==0) return 0;
	if(n==1) return 1;
	return n*jc(n-1); 
}
int a[8],b[8],c[8],aa[9],bb[9];
bool d[9];
void init(){
	for(int i=0;i<=7;i++){
		c[i]=jc(i);
	}
}
int main(){
	init();
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&b[i]);
	}
	memset(d,0,sizeof(d));
    for(int i=0;i<n;i++){
    	int ans=1;
    	for(int j=1;j<=a[i];j++){
    		if(!d[j])
    		ans++;
		}
    	aa[a[i]]=ans-1;
    	d[a[i]]=true;
	}
	memset(d,0,sizeof(d));
	for(int i=0;i<n;i++){
    	int ans=1;
    	for(int j=1;j<=b[i];j++){
    		if(!d[j])
    		ans++;
		}
    	bb[b[i]]=ans-1;
    	d[b[i]]=true;
	}
	int sum1=0,sum2=0;
	for(int i=0;i<n;i++){
		sum1+=c[n-1-i]*aa[a[i]];
		sum2+=c[n-1-i]*bb[b[i]];
	}
	cout<<abs(sum1-sum2);
	return 0;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from itertools import permutations

n = int(input())
nums1 = list(map(int,input(),split("")))
nums2 = list(map(int,input(),split("")))

t1 = tuple(nums1)
t2 = tuple(nums2)

index = 1
a = b = -1

for perm in permutations(list(range(1,n+1))):
    if perm == t1:
        a = index
    if perm == t2:
        b = index
    index +=  1

print(abs(a-b))

Submission status: Runtime Error

Solution code:
from itertools import permutations

n = int(input())
nums1 = list(map(int,input(),split("")))
nums2 = list(map(int,input(),split("")))

t1 = tuple(nums1)
t2 = tuple(nums2)

index = 1
a = b = -1

for perm in permtations(list(range(1,n+1))):
    if perm == t1:
        a = index
    if perm == t2:
        b = index
    index +=  1

print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools

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

temp = list(itertools.permutations(P))
print(temp.find(Q))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <cstdlib>
 
bool compare(int* a, int* b, int length){
  for(int i = 0; i < length; i++){
    if(a[i] != b[i])
      return false;
  }
  return true;
}
 
int main() {
  // int myints[] = {1,2,3};
 
  int n;
  std::cin >> n;
 
  int a[n];
  int per[n];
  int b[n];
  int rank1 = 0;
  int rank2 = 0;
  int rankCounter = 1;
 
 
  for(int i = 0; i < n; i++){
    int x;
    std::cin >> x;
    a[i] = x;
    per[i] = x;
  }
 
  for(int i = 0; i < n; i++){
    std::cin >> b[i];
  }
 
 
 
  std::sort (per,per+n);
  do {
    rankCounter++;
    if(compare(per, a, n)){
      rank1 = rankCounter;
    }
    if(compare(per, b, n)){
      rank2 = rankCounter;
    }
    if(rank1 != 0 && rank2 != 0){
      break;
    }
  } while(std::next_permutation(per, per+n));
 
 
  std::cout << abs(rank1-rank2) << std::endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
R = [itertools.permutations(range(1,N+1))]
print(abs(r.idex(P)-r.index(Q)))

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), N))
    ans = abs(perms.index(P) - perms.index(Q))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import itertools
n=int(input()
a=map(int,input().split())
b=map(int,input().split())
seq=range(n)
list(itertools.permutations(seq))
print(a)
print(list[0])

Submission status: Runtime Error

Solution code:
def get_order(numbers, originals):
    
    from math import factorial
    from copy import copy
    numbers = copy(numbers)
    originals = copy(originals)
    
    total = 0
    while numbers:
        number = numbers.pop(0)
        number_index = originals.index(number)
        originals.pop(number_index)
        count = (number_index ) * factorial(len(originals))
        total += count
    return total
        
def get_next_int():
    return int(float(input()))
def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])

def main(print=__builtins__.print):
    n = get_next_int()
    
    a = get_next_ints()
    b = get_next_ints()
    originals = list(map(int, range(n)))
    print(get_order(a, originals) - get_order(b, originals))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import itertools
num = int(input())
array1 = tuple(map(int, input().split()))
array2 = tuple(map(int, input().split()))
all_list = list(itertools.permutations(range(1, num + 1)))
 
array1_index = all_list.index(array1) + 1
array2_index = all_list.index(array2) + 1
 
if array1_index > array2_index:
  print(array1_index-array2_index)
else:
  print(array2_index-array1_index)ar

Submission status: Runtime Error

Solution code:
import itertools
num = int(input())
array1 = tuple(map(int, input().split()))
array2 = tuple(map(int, input().split()))
all_list = list(itertools.permutations(range(1, num + 1)))
 
array1_index = all_list.index(array1) + 1
array2_index = all_list.index(array2) + 1
 
if array1 > array2:
  print(array1-array2)
else:
  print(array2-array1)

Submission status: Runtime Error

Solution code:
import itertools
num = int(input())
array1 = tuple(map(int, input().split()))
array2 = tuple(map(int, input().split()))
all_list = list(itertools.permutations(range(num)))
 
array1_index = all_list.index(array1) + 1
array2_index = all_list.index(array2) + 1
 
if array1 > array2:
  print(array1-array2)
else:
  print(array2-array1)

Submission status: Runtime Error

Solution code:
import itertools
num = int(input())
array1 = list(map(int, input().split()))
array2 = list(map(int, input().split()))
all_list = list(itertools.permutations(range(num)))

array1_index = all_list.index(array1) + 1
array2_index = all_list.index(array2) + 1

if array1 > array2:
  print(array1-array2)
else:
  print(array2-array1)

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

allp = p.copy()
allp.sort()
allperms = list(map(list, permutations(allp)))
print(abs(allperms.index(p) - allperms.index(q)))

Submission status: Runtime Error

Solution code:
import itertools

if __name__ == '__main__':
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(input())
    p = list(map(int, input().split(' ')))
    q = list(map(int, input().split(' ')))

    realnums = p.copy()

    realnums.sort()

    perm = list(itertools.permutations(realnums))
    
    print(abs(perm.index(p) - perm.index(q)))

Submission status: Runtime Error

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

count_p = 0
count_q = 0

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

for i in range(n):
    count_p += (p[i]-1)*factorial(n-(i+1))
for j in range(n):
    count_q += (q[i]-1)*factorial(n-(i+1))

print(abs(count_p - count_q))

Submission status: Runtime Error

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

count_p = 0
count_q

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

for i in range(n):
    count_p += (p[i]-1)*factorial(n-(i+1))
for j in range(n):
    count_q += (q[i]-1)*factorial(n-(i+1))

print(abs(count_p - count_q))

Submission status: Runtime Error

Solution code:
import itertools
import sys

N = int(input())

T = []
T.append(tuple(map(int, input().split())))
T.append(tuple(map(int, input().split())))

if T[0] == T[1]:
    print(0)
    sys.exit(0)

M = [0]*N
for i in range(N):
    M[i] = i+1

ans = []
for i, v in enumerate(itertools.permutations(M, N)):
    if v in T:
        ans.append(i)

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

Submission status: Runtime Error

Solution code:
import itertools

N = int(input())
T = []
T.append(tuple(map(int, input().split())))
T.append(tuple(map(int, input().split())))

M = [0]*N
for i in range(N):
    M[i] = i+1

ans = []
for i, v in enumerate(itertools.permutations(M, N)):
    if v in T:
        ans.append(i)

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

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
length = [i+1 for i in range(n)]
permut = list(itertools.permutations(length))
print(max((permut.index(p)) - (permut.index(q)) , (mm.index(q)) - mm.index(p)))

Submission status: Runtime Error

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

ans = []
l = range(1, N + 1)
import itertools


for v in itertools.permutations(l, N):
    if list(v) == P:
      a = list(v).index(P)
        
    if list(v) == Q:
      b = list(v).index(Q)
    

print(abs(b - a))

Submission status: Runtime Error

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

ans = []
l = range(1, N + 1)
import itertools


for v in itertools.permutations(l, N):
    if list(v) == P:
      a = v.index(P)
        
    if list(v) == Q:
      b = v.index(Q)  
    

print(abs(b - a))

Submission status: Runtime Error

Solution code:
# 問題文：長さNの偶数からなる正の整数列A=a1,a2,...,aNと、整数Mが与えられます。
#        任意のk(1≤k≤N)に対して以下の条件を満たす正の整数XをAの「半公倍数」と定義します。
#        X=ak×(p+0.5)を満たす負でない整数pが存在する。
#        1以上M以下の整数のうちのAの半公倍数の個数を求めてください。
# 入力 : 以下の形式で標準入力から与えられる。
#       N M
#       a1 a2 ... aN
# 出力 : 1 以上 M 以下の整数のうちの A の半公倍数の個数を出力せよ。

# 最小半公倍数を求める
def min_hankobaisu(A, M):
    result = 1
    for a in A:
        result = result * a / 2
        if result > M:
            print(0)
            exit(0)
    return int(result)

# 正の整数列Aの長さN, 半公倍数の上限M
N, M = map(int, input().split())

# 正の整数列A
A = list(set(list(map(int, input().split()))))
A.sort(reverse=True)
# hankobaisu_list = []

# 与えられた数値が2*奇数と2*偶数なのであれば半公倍数はない
# → その場で終了
for i in range(0, len(A) - 1):
    if (A[i] / 2 % 2) != (A[i + 1] / 2 % 2):
        print(0)
        exit(0)

# A[i] が2*偶数の場合はだめ？
if (A[0] / 2) % 2 == 0:
    print(0)
    exit(0)

# 半公倍数の最小値を求める
min_hankobaisu = min_hankobaisu(A, M)
#print('最小半公倍数：{}'.format(min_hankobaisu))
print(int(M/min_hankobaisu+1)/2)

Submission status: Runtime Error

Solution code:
import math


N = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

def CO(l):
    n = len(l)
    if n >= 1:
        m = sorted(l)
        a = l[0]
        s = m.find(a)
        return((s+1)*math.factorial(n-1)+CO(l.pop(0)))
    else:
        return 1

print(abs(CO(list1)-CO(list2)))

Submission status: Runtime Error

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

import itertools
X = list(itertools.permutations(range(1,N+1),N))
for i in range(len(X)):
  x = X[i]
  if P == x:
    a = i 
  elif Q == x:
    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()))
import itertools
J=(list(itertools.permutations(range(n))))

for i, j in enumerate(J):
    if j == p:
        a=i
    if j==q:
        b=i
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools

N = int(sys.stdin.readline())
P = [int(v) for v in sys.stdin.readline().split()]
Q = [int(v) for v in sys.stdin.readline().split()]
Ps = [list(v) for v in itertools.permutations(range(1, N + 1),N)]
Ps = sorted(Ps)

def find(l1):
    for i, l3 in enumerate(Ps):
        if l3 == l1:
            return i
    return 0


print(abs(find(P) - find(Q)))

Submission status: Runtime Error

Solution code:
n = int(input())
l = sorted(list(itertools.permutations([i for i in range(1,n+1)])))

al = tuple([int(i) for i in input().split()])
bl = tuple([int(i) for i in input().split()])


a,b = 0,0


if al == bl:
    print(0)
else:
    for idx,i in enumerate(l):
        if i == al:
            a = idx
        elif i == bl:
            b = idx
        else:
            pass
        
    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()))
l = list(range(1, n + 1))
ll = list(itertools.permutations(l, n))

for i in ll:
    if i == p:
        p_n = ll.index(i) + 1
    elif i == q:
        q_n = ll.index(i) + 1
ans = abs(p_n - q_n)
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()))
perms = []
for i in itertools.permutations([j+1 for j in range(n)]):
    perms.append(list(i))
perms.sort()
for i in range(len(perms)):
    if perms[i]==p:
        rp = i
    elif perms[i] ==q:
        rq = i
print(abs(rq-rp))

Submission status: Runtime Error

Solution code:
from fractions import gcd
A, B = map(int, input().split())

def lcm(x,y):
    return (x*y) // gcd(x,y)

ans = lcm(A, B)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
#s=list(input().split())
#s,t=list(input().split())
#a=list(map(int,input().split()))
#k,x=map(int,input().split())
p=list(input().split())
q=list(input().split())
p=[int(i) for i in p]
q=[int(i) for i in q]
import itertools
import math
fulllist=list(itertools.permutations([i for i in range(1,n+1)]))
for i in range(math.factorial(n)):
    if p==list(fulllist[i]):
        p_index=i+1
    elif q==list(fulllist[i]):
        q_index=i+1
print(abs(p_index-q_index))

Submission status: Runtime Error

Solution code:
n=int(input())
#s=list(input().split())
#s,t=list(input().split())
#a=list(map(int,input().split()))
#k,x=map(int,input().split())
p=list(map(int,input()))
q=list(map(int,input()))
import itertools
import math
fulllist=list(itertools.permutations([i for i in range(1,n+1)]))
for i in range(math.factorial(n)):
    if p==fulllist[i]:
        p_index=i+1
    elif q==fulllist[i]:
        q_index=i+1
print(abs(p_index-q_index))

Submission status: Runtime Error

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

l=list(itertools.permutations(list(range(1,n+1))))
print(abs(l.index(p)-L.index(q)))

Submission status: Runtime Error

Solution code:
n=int(n)
m=int(m)
x=[false for i in range(n)]
count=[0 for i in range(n)]
for i in range(m):
  p,s=input().split()
  p=int(p)
  if(not x[p-1]):
    if(s=="AC"):
      x[p-1]=true
    else:
      count[p-1]=count[p-1]+1
ans=0
ans2=0
for i in range(ansn):
  if(x[i]):
    ans+=1
    ans2+=count[i]
print(ans,ans2)

Submission status: Runtime Error

Solution code:
 int(input())
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
 
data = [i+1 for i in range(N)]
 
def listExcludedIndices(data, indices=[]):
  return [x for i, x in enumerate(data) if i not in indices]
 
def count(res,p):
    count = 0
    for i in range(len(res)):
        count +=1
        if res[i] == p:
            break
    return count
result = []
for i in range(len(data)):
  for j in range(len(data)-1):
    for k in range(len(data)-2):
      jData = listExcludedIndices(data, [i])
      kData = listExcludedIndices(jData, [j])
      result.append([data[i], jData[j], kData[k]])
print(abs(count(result,P) - count(result,Q)))

Submission status: Runtime Error

Solution code:
N = int(input())
P = [int(input().split())]
Q = [int(input().split())]
 
data = [i+1 for i in range(N)]
 
def listExcludedIndices(data, indices=[]):
  return [x for i, x in enumerate(data) if i not in indices]
 
def count(res,p):
    count = 0
    for i in range(len(res)):
        count +=1
        if res[i] == p:
            break
    return count
result = []
for i in range(len(data)):
  for j in range(len(data)-1):
    for k in range(len(data)-2):
      jData = listExcludedIndices(data, [i])
      kData = listExcludedIndices(jData, [j])
      result.append([data[i], jData[j], kData[k]])
print(abs(count(result,P) - count(result,Q)))

Submission status: Runtime Error

Solution code:
N = int(input())
P = [int(input().split()) for i in range(N)]
Q = [int(input().split()) for i in range(N)]
 
data = [i+1 for i in range(N)]
 
def listExcludedIndices(data, indices=[]):
  return [x for i, x in enumerate(data) if i not in indices]
 
def count(res,p):
    count = 0
    for i in range(len(res)):
        count +=1
        if res[i] == p:
            break
    return count
result = []
for i in range(len(data)):
  for j in range(len(data)-1):
    for k in range(len(data)-2):
      jData = listExcludedIndices(data, [i])
      kData = listExcludedIndices(jData, [j])
      result.append([data[i], jData[j], kData[k]])
print(abs(count(result,P) - count(result,Q)))

Submission status: Runtime Error

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

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

def listExcludedIndices(data, indices=[]):
  return [x for i, x in enumerate(data) if i not in indices]

def count(res,p):
    count = 0
    for i in range(len(res)):
        count +=1
        if res[i] == p:
            break
    return count

result = []
for i in range(len(data)):
  for j in range(len(data)-1):
    for k in range(len(data)-2):
      jData = listExcludedIndices(data, [i])
      kData = listExcludedIndices(jData, [j])
      result.append([data[i], jData[j], kData[k]])
print(abs(count(result,P) - count(result,Q)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import itertool
A=itertool.permutations(range(1,int(input())+1))
print(abs(A.index(list(map(int,input().split())))-A.index(list(map(int,input().split()))))

Submission status: Runtime Error

Solution code:
import itertool
A=itertool.permutation(range(1,int(input())+1))
print(abs(A.index(list(map(int,input().split())))-A.index(list(map(int,input().split()))))

Submission status: Runtime Error

Solution code:
from itertools import permutations
import numpy

N = int(input())
P = int(input().split())
Q = int(input().split())
count = 0

n = numpy.array(range(1, N+1))
for n in permutations(n, N):
    count += 1
    if n == P:
        p = count
    if n == Q:
        q = count

print(abs(p - q))

Submission status: Runtime Error

Solution code:
import itertools

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

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

print(abs(r.index(P)-r.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()))
lis = range(1,n+!)
#permulations(l,n)でリストlの要素のうちn個を選んで並べる
a = list(itertools.permulations(lis))

#abs絶対値
print(abs(a.index(p) - a.index(q)))

Submission status: Runtime Error

Solution code:
import itertools
li = [1,2,3,4,5,6,7,8]
for v in itertools.combinations(li):
  print(v)

Submission status: Runtime Error

Solution code:
n=(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
import itertools as it
x=0
y=0
for i,j in enumerate(it.permutations(range(1,n+1))):
    if a==j:
        x=i
    if b==j:
        y=i
print(abs(x-y))

Submission status: Runtime Error

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

import itertools

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

a = 0

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 itertools
n=int(input())
p,q=[tuple(map(int,input().split())) for i in (1,1)]
j=itertools.permutations
l=list(j(range(1,n+1)))

for i,s in enumerate(l):
  if s==p:a=i
  elif s==q:b=i
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()))

all_pattern = list(itertools.permutations([i for i in range(1,N+1)]))
#print(all_pattern)

for i, node in enumerate(all_pattern):
  if node == P:
    p = i
  elif node  ==  Q:
    q = i
    
    
print(abs(p-q))

Submission status: Runtime Error

Solution code:
# 大きさnの順列をすべて生成する
from itertools import permutations
def generate_permutations(n):
    perms = permutations(range(1, n+1), n)
    return [perm for perm in perms]

n = int(input())
ptuple = tuple(map(int, input().split()))
qtuple = tuple(map(int, input().split()))
 
perms = generate_permutations(n)
a = perms.index(ptuple)
b = perms.index(qtuple)
print(int(math.fabs(a - b)))

Submission status: Runtime Error

Solution code:
N = int(input())
P = (int(i) for i in input().split())
Q = (int(i) for i in input().split())

seq = tuple(range(N))
import itertools
p_list = list(itertools.permutations(seq))
for i,p in enumerate(p_list):
  if p_list == P:
    p_idx = i
  elif p_list == Q:
    q_idx = i
print(abs(p_idx-q_idx))

Submission status: Runtime Error

Solution code:
import itertools as it

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

l=[n for n in range(1,N+1)]
for i,t in enumerate(it.permutations(l)):
    if t==P:
        a=i+1
    elif t==Q:
        b=i+1
ans=abs(b-a)

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import permutations
n=int(input())
a=list(map(int, input().split())) 
b=list(map(int, input().split())) 
l=list(permutations(a)))
print(abs((l.find(a))-(l.find(b))))

Submission status: Runtime Error

Solution code:
from itertools import permutations
a=list(map(int, input().split())) 
b=list(map(int, input().split())) 
l=list(permutations(a)))
print(abs((l.find(a))-(l.find(b))))

Submission status: Runtime Error

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

import math
t = u = 0
for p in range(N):
    t += math.factorial(N-p-1)*(P[p]-1)
    u += math.factorial(N-p-1)*(Q[p]-1)
    if p+3 == N:
        break

if P[p+1]<P[p+2]:
    t+=1
else:
    t+=2
if Q[p+1]<Q[p+2]:
    u+=1
else:
    u+=2

print(abs(t-u))

Submission status: Runtime Error

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

import math
t = u = 0
for p in range(N):
    t += math.factorial(N-p-1)*(P[p]-1)
    u += math.factorial(N-p-1)*(Q[p]-1)
    if p+3 == N:
        break

if P[p+1]<P[p+2]:
    t+=1
else:
    t+=2
if Q[p+1]<Q[p+2]:
    u+=1
else:
    u+=2
print(t, u)
print(abs(t-u))

Submission status: Runtime Error

Solution code:
from typing import Tuple
from itertools import permutations


class Solution:
    def CountOrder(self, sequence1: Tuple[int], sequence2: Tuple[int]):
        l = list(permutations(range(1, n + 1)))
        index1 = l.index(sequence1)
        index2 = l.index(sequence2)
        return abs(index1 - index2)


if __name__ == "__main__":
    n = int(input())
    sequence1 = tuple(map(int, input().split()))
    sequence2 = tuple(map(int, input().split()))
    solution = Solution()
    print(solution.CountOrder(sequence1, sequence2))

Submission status: Runtime Error

Solution code:
from itertools import*
n,a,b=eval('eval(input().replace(" ",",")),'*3)
i=list(permutations(range(n+1))).index
print(abs(i(a)-i(b)))

Submission status: Runtime Error

Solution code:
from itertools import*
(n,),a,b=eval('eval(input().replace(" ",",")),'*3)
i=list(permutations(range(1,n+1))).index
print(abs(i(a)-i(b)))

Submission status: Runtime Error

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

print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools

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

seq = [i for i in range(1,n+1)]
seq_list = list(itertools.permutations(seq))
sorted_seq = sorted(seq_list)

for i in range(1, len(sorted_seq)):
  if p_list == sorted_seq[i]:
    a = i
  if q_list == sorted_seq[i]:
    b = i
print(abs(a - b))

Submission status: Runtime Error

Solution code:
import itertools

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

seq = [i for i in range(1,n+1)]
seq_list = list(itertools.permutations(seq))
sorted_seq = sorted(seq_list)

for i in range(1, len(sorted_seq)):
  if p_list == sorted_seq[i]:
    a = i
    print(a)
  if q_list == sorted_seq[i]:
    b = i
    print(b)
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()))

permute(p, q, n-3)

Submission status: Runtime Error

Solution code:
import fractions
import math

N, M = map(int, input().split())
a = list(map(int, input().split()))
a_uniq = list(set(a))

len_a = len(a_uniq)

# find lcm
gcd_a = a_uniq[0]
for i in range(1, len_a):
    gcd_a = fractions.gcd(gcd_a, a_uniq[i])

lcm = 1
for j in range(len_a):
    lcm *= a_uniq[j]
    
lcm = lcm / gcd_a
lcm /= 2

print(math.ceil((M / lcm) / 2), lcm, M)

Submission status: Runtime Error

Solution code:
import itertools 

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


def permutation(lst): 
    if len(lst) == 0: 
        return [] 
  
    if len(lst) == 1: 
        return [lst] 
    l = [] 
    for i in range(len(lst)): 
       m = lst[i] 
       remLst = lst[:i] + lst[i+1:] 
       for p in permutation(remLst): 
           l.append([m] + p) 
    return l 

P_perm =  permutation(P)
Q_perm = permutation(Q)  
p_count = 0
q_count = 0

if len(P) <= N and len(Q) <= N:
  for i in range(0,len(P_perm)):
    if P_perm[i] == P_perm[i+1]:
      p_count = p_count + 1
  for i in range(0,len(P_perm)):
    if Q_perm[i] == Q_perm[i+1]:
      q_count = q_count + 1

      
    
  
print(p_count - q_count)

Submission status: Runtime Error

Solution code:
import copy

n = int(input())
p = int(input().replace(" ", ""))
q = int(input().replace(" ", ""))

def hoge(numbers, s, n_list):

    for i in range(len(numbers)):
        copy_numbers = copy.deepcopy(numbers)
        ss = s+copy_numbers.pop(i)
        if len(copy_numbers) > 0:
            hoge(copy_numbers, ss, n_list)
        else:
            n_list.append(ss)

n_list = []
for i in range(n):
    number_list = [str(i) for i in range(1, n+1)]
    s = number_list.pop(i)
    hoge(number_list, s, n_list)

n_list.sort()

ans = abs((n_list.index(p) + 1) - (n_list.index(q) + 1))
print(ans)

Submission status: Runtime Error

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

def countup(array):
  pos = 1
  # 一桁ごとみていく
  for i, x in enumerate(array):
    if i == 0:
      pos += (N-1)*x
    else:
      pos += ((N-i)-1)*(x+1)
  return pos

a = countup(P)
b = countup(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()))

permute(n, 2, n-1)

Submission status: Runtime Error

Solution code:
import itertools

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

D=list(range(1,N+1))
dict=[]

for i,j in enumerate(itertools.permutations(D)):
    P,Q,j=list([*P]),list([*Q]),list([*j])
    if j==P:
        p=i
    if j==Q:
        q=i
print(abs(p-q))

Submission status: Runtime Error

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

import math

a=0
for i in range(n-1):
  a +=(p[i]-1) * math.factorial(n-1-i)
  
  
b=0
for i in range(n-1):
  b +=(q[i]-1) * math.factorial(n-1-i)
  
print(abs(a-b))