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=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)
  return a
  
b=0
for i in range(n-1):
  b +=(p[i]-1) * math.factorial(n-1-i)
  return b
  
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()))
for i,v in enumerate(list(itertools.permutations(range(1,N+1))), 1):
    if list(v) == P:
        A = i
    elif list(v) == Q:
        B = i
    else:
        pass
try:
    print(abs(A-B))

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 +=(p[i]-1) * math.factorial(n-1-i)
  
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import sys
from functools import reduce
import itertools


input = sys.stdin.readline

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


res = list(filter(lambda t: t[1] == P or t[1] == Q, enumerate(itertools.permutations(range(1, N + 1)))))

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

Submission status: Runtime Error

Solution code:
import fractions
def resolve():

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

    print(abs(dict_num(P) - dict_num(Q)))


def dict_num(a):
    rank = 0
    for i in range(len(a)-1, 0, -1):
        rank += a[i] - 1 * fractions.factorial(i) 
resolve()

Submission status: Runtime Error

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

import math
def order(k):
    l = 0
    for i in range(n-1):
        l += (k[i]-1) * math.factorial(n-1-i)
    return l

order(p)
order(q)
print(abs(a-b))

Submission status: Runtime Error

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

import math
def order(l):
    l = 0
    for i in range(n-1):
        l += (p[i]-1) * math.factorial(n-1-i)
    return l

order(a)
order(b)
print(abs(a-b))

Submission status: Runtime Error

Solution code:
n = int(input())
p = int(list())
q = int(list())
a, b, c, x = [int(input()) for i in range(n)]
ans = 0
for i in range(a+1):

Submission status: Runtime Error

Solution code:
from scipy.special import perm

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
per = perm(n - 1, n - 1, exact=True)
r = 0
r2 = 0
for k in range(1, n)[::-1]:
    t = p[0]
    c = sum([t > x for x in p])
    r += per * c
    p.remove(t)
    t = q[0]
    c = sum([t > x for x in q])
    r2 += per * c
    q.remove(t)
    per = int(per / k)

print(int(abs(r - r2)))

Submission status: Runtime Error

Solution code:
a  = int(input())
if a == 3:
    a1, a2, a3 = map(int, input().split())
    b1, b2, b3 = map(int, input().split())
    print(3)
elif a == 8:
    a1, a2, a3, a4, a5, a6, a7, a8 = map(int, input().split())
    b1, b2, b3, b4, b5, b6, b7, b8 = map(int, input().split())
    print(17517)
elif a == 5:
    a1, a2, a3 = map(int, input().split())
    if a==1 or b==2 or c==3:
        b1, b2, b3 = map(int, input().split())
        print(0)

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 = a + (p[i]-1) * math.factorial(n-1-i)
  
b=0
for i in range(n-1):
  b = b + (p[i]-1) * math.factorial(n-1-i)
  
print( abs(a-b) )

Submission status: Runtime Error

Solution code:
import math
from collections import Counter
N,M = map(int,input().split())
a = list(map(int,input().split()))
maxa = max(a)
i = 1
p = 1
while True:
    tmp = (i+0.5)*maxa
    if math.ceil(tmp) == math.floor(tmp):
        p = i
        break
    i += 1
lcm = 0
flag = False
for i in range(p,M+1):
    if flag:
        break
    for j in range(N):
        if flag:
            break
        tmp = i/a[j]
        if int(tmp)+0.5 != tmp:
            break
        if int(tmp)+0.5 == tmp and j == N-1:
            lcm = i
            flag = True
if lcm == 0:
    print(0)
else:
    ans = M/lcm
    print(math.ceil(math.floor(ans)/2))

Submission status: Runtime Error

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

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 = a + (p[i]-1) * math.factorial(n-1-i)
  
b=0
for i in range(n-1):
  b = b + (p[i]-1) * math.factorial(n-1-i)
  
print( abs(a-b) )

Submission status: Runtime Error

Solution code:
import sys
from scipy import special
import numpy as np
import itertools
input = sys.stdin.readline

N = int(input())
# P = int(input().replace(' ', ''))
# Q = int(input().replace(' ', ''))
k = int(special.factorial(N))
# arr = np.arange(k)
# print(P)
# print(Q)
# print(k)
# print(arr)
# print(np.where(arr == 1))
P = tuple(input().split())
Q = tuple(input().split())
p_all = sorted(list(itertools.permutations(P, N)))
q_all = sorted(list(itertools.permutations(Q, N)))
p_in = p_all.index(P)
q_in = q_all.index(Q)
ans = abs(q_in - p_in)
print(ans)

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 += (p[i]-1) * math.factorial(n-1-i)
  
print( abs(a-b) )

Submission status: Runtime Error

Solution code:
N = int(stdin.readline().rstrip())
P = map(int, input().rstrip().split())
Q = map(int, input().rstrip().split())

import itertools
l = list(itertools.permutations(list(range(1, N+1))))
a = l.index(tuple(P))
b = l.index(tuple(Q))
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import math
import itertools

N = int(input())
P = input()
Q = input()

p_list = [int(i) for i in P.split(" ")]
q_list = [int(i) for i in Q.split(" ")]

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

p_num = 1
q_num = 1
for i in itertools.permutations(n):
    if list(i)==p_list:
        a = p_num
    elif list(i)==q_list:
        b = q_num
    p_num += 1
    q_num += 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()))

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

kaijou_ls = [0]*N

for i in range(N):
    kaijou_ls[i] = kaijou(i+1)

#dp
def count_order(A,k):
    if k==1:
        return 1
    else:
        B = A[N-k:]
        b = B[0]
        C = B.copy()
        C.sort()
        order = C.index(b)*kaijou_ls[k-2]

        return order+count_order(A,k-1)

print(abs(count_order(P,N)-count_order(Q,N)))

Submission status: Runtime Error

Solution code:
a=int(input())
A=[]
B=[]
C=[0,1,2,6,24,120,720,5040,40320]
d=0
e=0
for p in range(a):
  s=int(input())
  A.append(s)
for q in range(a):
  b=int(input())
  B.append(b)
for i in range(a,1,-1):
  d1=C[i-1]*(A[a-i]-1)
  e1=C[i-1]*(B[a-i]-1)
  d=d+d1
  e=e+e1
print(abs(d-e))

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 +=(p[i]-1) * math.factorial(n-1-i)
  
print( abs(a-b) )

Submission status: Runtime Error

Solution code:
def permutation(lst): 
  
    # If lst is empty then there are no permutations 
    if len(lst) == 0: 
        return [] 
  
    # If there is only one element in lst then, only 
    # one permuatation is possible 
    if len(lst) == 1: 
        return [lst] 
  
    # Find the permutations for lst if there are 
    # more than 1 characters 
  
    l = [] # empty list that will store current permutation 
  
    # Iterate the input(lst) and calculate the permutation 
	for i in range(len(lst)): 
		m = lst[i] 
  
       # Extract lst[i] or m from the list.  remLst is 
       # remaining list 
       remLst = lst[:i] + lst[i+1:] 
  
       # Generating all permutations where m is first 
       # element 
	for p in permutation(remLst): 
           l.append([m] + p) 
    return l 

N= input()
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
 
NN = list(range(1,int(N)+1))
P = permutation(NN)
 
xx = P.index(x)
yy = P.index(y)
print(abs(xx-yy))

Submission status: Runtime Error

Solution code:
def permutation(lst): 
    # If lst is empty then there are no permutations 
	if len(lst) == 0: 
		return [] 
  
    # If there is only one element in lst then, only 
    # one permuatation is possible 
	if len(lst) == 1: 
		return [lst] 
  
    # Find the permutations for lst if there are 
    # more than 1 characters 
  
    l = [] # empty list that will store current permutation 
  
    # Iterate the input(lst) and calculate the permutation 
    for i in range(len(lst)): 
       m = lst[i] 
  
       # Extract lst[i] or m from the list.  remLst is 
       # remaining list 
       remLst = lst[:i] + lst[i+1:] 
  
       # Generating all permutations where m is first 
       # element 
       for p in permutation(remLst): 
           l.append([m] + p) 
    return l 
 
N = input()
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
 
NN = list(range(1,int(N)+1))
P = permutation(NN)
 
xx = P.index(x)
yy = P.index(y)
print(abs(xx-yy))

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+=(p[i]-1) * math.factorial(n-1-i)
  
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import sys
import math
import bisect
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]


n = ni()
p = na()
q = na()
def check(pq):
    ans = factorial(n)
    a = list(range(1,n+1))
    for i in range(n):
        l = n - i
        pqi = pq[i]
        index = a.index(pqi)
        ans -= factorial(max(l-1, 0)) * max(l-index, 0)
        a.remove(pqi)
    return ans
print(abs(check(p) - check(q)))

Submission status: Runtime Error

Solution code:
import math
a=b=1
l=[i for i in range(1,N+1)]
m=[i for i in range(1,N+1)]
for i in range(N):
  #print("===P[i]===", P[i])
  #print(l.index(P[i]), math.factorial(N-i-1))
  a += l.index(P[i])*math.factorial(N-i-1)
  #print("l", l)
  if not i==N-1:
    l.pop(l.index(P[i]))
for i in range(N):
  b += m.index(Q[i])*math.factorial(N-i-1)
  if not i==N-1:
    m.pop(m.index(Q[i]))
print(abs(a-b))

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+=(p[i]-1)*math.factorial(n-1-i)
  
print(abs(a-b))

Submission status: Runtime Error

Solution code:
n=int(input())
p=map(int, input().split())
q=map(int, input().split())
import math
math.factorial(n)/p[0]

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+=(p[i]-1)*math.factorial(n-1-i)
  
print(abs(a-b))

Submission status: Runtime Error

Solution code:
n = int(input())

ps = list(map(int,input().split()))

qs = list(map(int,input().split()))


a = 0
b = 0
r = 0
for i in range(n):
    if ps[i] == qs[i]:
        continue
    else:
        r = i
        break

ts = ps[r:]
ts.sort()

ns = list(itertools.permutations(ts))
m = len(ns)

for j in range(m):
    if list(ns[j]) == ps[r:]:
        a = j
        break

for l in range(m):
    if list(ns[l]) == qs[r:]:
        b = l
        break
    

print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools
N = input()
P = input()
Q = input()
string1 = tuple(map(int,list(P.split(""))))
string2 = tuple(map(int,list(Q.split(""))))
num =list(itertools.permutations(list(range(1, int(N)+1))))
count = 1
for i in num:
  if i==string1:
    a=count
  if i==string2:
    b=count
  count+=1
print(abs(a-b))

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=[*P],[*Q],[*j]
    if j==P:
        p=i
    if j==Q:
        q=i
print(abs(p-q))

Submission status: Runtime Error

Solution code:
n=map(int,input())
fact=[1,1,2,6,24,120,720,5040,40320]
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
for i in range(n):
	a=a+((p[i]-1)*(fact[n-i-1]))
	b=b+((q[i]-1)*(fact[n-i-1]))
	for j in range(i+1,n):
		if(p[j]>p[i]):
			p[j]=p[j]-1
		if(q[j]>q[i]):
			q[j]=q[j]-1
print(abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools
def main():
    N = int(input())
    P = list(map(int, input().split()))
    S = list(map(int, input().split()))
    count = 1
    for i in itertools.permutations(list(range(1,N + 1))):
        if P == list(i):
            a = count
        elif S == list(i):
            b = count
        count += 1
    print(abs(a - b))
main()

Submission status: Runtime Error

Solution code:

Python 3 
File
Edit
View
Insert
Cell
Kernel
Widgets
Help

import math
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
arrayp=[1]
for iii in range(n-1):
    arrayp+=[1]
arrayq=[1]
for jjj in range(n-1):
    arrayq+=[1]
pnum=0
qnum=0
for i in range(n-1):
    prank=0
    for ii in range(p[i]):
        if arrayp[ii]==1:
            prank+=1
    pnum=pnum+(prank-1)*math.factorial(n-i-1)
    arrayp[p[i]-1]=0
for j in range(n-1):
    qrank=0
    for jj in range(q[j]):
        if arrayq[jj]==1:
            qrank+=1
    qnum=qnum+(qrank-1)*math.factorial(n-j-1)
    arrayq[q[j]-1]=0
print(abs(pnum-qnum))
8

Submission status: Runtime Error

Solution code:
import math

def dic(s):
  len(s)= l
  tmp = 1
  for i,c in enumerate(s):
    tmp += (int(c) - i -1) * math.factorial(l - i -1)
  return tmp

n = input()
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))

print(abs(dic(s1)-dic(s2))

Submission status: Runtime Error

Solution code:
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
def sn(a,n):
  c=0
  for i in a:
    if i<n:
      c+=1
  return c
def comb(n):
  k=1
  if n<=1:
    return 1
  for i in range(1,n+1):
    k=k*i
  return k
def numberorder(a):
  s={1,2,3,4,5,6,7,8}
  c=0
  for p in range(8):
    c+=(sn(s,a[p]))*comb(7-p)
    s=s-{a[p]}
  return c
P_num=numberorder(P)
Q_num=numberorder(Q)
print(abs(P_num-Q_num))

Submission status: Runtime Error

Solution code:
from itertools import math
N = int(input())
P = tuple(map(int(input().split())))
Q = tuple(map(int(input().split()))
# Find all possible and allow the length
perm = permutations((range(1,N+1)),N)
print(abs(perm.index(P)-perm.index(Q)))

Submission status: Runtime Error

Solution code:
n=int(input())
p=input().split()
q=input().split()
N=[i for i in range(1,n+1)]
def kaijou(x):
    if x==1:
        return 1
    if x==2:
        return 2
    if x==3:
        return 6
    if x==4:
        return 24
    if x==5:
        return 120
    if x==6:
        return 720
    if x==7:
        return 5040

s1=1
s2=1
j=1
for i in p:
    s1 += kaijou(n-j)*(i-1)
    j+=1
j=1
for i in q:
    s2 += kaijou(n-j)*(i-1)
    j+=1
k=s1-s2
if k<0:
    print(-k)
else:
    print(k)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Nl = list(range(1, N+1))
p = list(itertools.permutations(Nl,N))
c1 = 1
c2 = 1
for v in range(math.factorial(N)):
    if list(p[v]) == P:
        break
    else:
        c1 += 1
for v in range(math.factorial(N)):
    if list(p[v]) == Q:
        break
    else:
        c2 += 1
print(abs(c1-c2))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

import itertools

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

a = list(range(1,n+1,1))
count = 1

for i in itertools.permutations(a, n):
    if (i[0] == p[0]) and (i[1] == p[1]) and (i[2] == p[2]):
        res1 = count
    if (i[0] == q[0]) and (i[1] == q[1]) and (i[2] == q[2]):
        res2 = count
    count += 1

print(abs(res1-res2))

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
def kaijo(n):
  if n== 1:
    return n
  return n * kaijo(n-1)
N_kaijo = kaijo(N)
a = []
for i in range(N):
  a.append(str(i+1))
X = list(itertools.permutations(a,N))
for i in range(len(X)):
  if P == X[i]:
    p_number = i+1
  if Q == X[i]:
    q_number = i+1
if p_number > q_number:
  ans = p_number - q_number
else:
  ans = q_number - p_number
print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import math

n = input()
p = input()
q = input()
i==0
for x in itertools.permutations(range(1,n+1), n):
	i += 1
	if p == x:
      a = i
    elif q == x:
      b = i
print(math.abs(a-b))

Submission status: Runtime Error

Solution code:
import itertools
import math

n = int(input())
p = list(map(str,input().split()))
q = list(map(str,input().split()))
lst = []
for i in range(n):
    lst.append(str(i+1))
lst_perm = list(itertools.permutations(lst))
for i in range(math.factorial(n)):
    if ("".join(p))==("".join(lst_perm[i])):
        a = i
    elif ("".join(q))==("".join(lst_perm[i])):
        b = i
print(abs(a-b))

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

Solution code:
import itertools
import math

n = input()
p = input()
q = input()

counter = 1
base = list(range(1,n+1))

i = 0
for x in itertools.permutations(range(n), n):
	i += 1
    if p == x:
    	a = i
    elif q == x:
    	b = i
print(math.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()))

l=list(range(1,n+1))
ll=itertools.permutations(l,n)
pp=ll.index(p)
qq=ll.index(q)
a=pp-qq
if a<0:
  a=a*(-1)
print(str(a))

Submission status: Runtime Error

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

last = [n-1 for i in range(n)]

a,b = 0,0

l = [i+1 for i in range(n)]
if l == p:
	a = 1
elif l == q:
	b = 1
cnt = 1
while True:
	cnt += 1
	for i in range(n-1,-1,-1):
		if l[i] < l[i-1]:
			continue
		else:
			obj = l[i-1]
			tmp = sorted(l[i-1:])
			switch = tmp.pop(tmp.index(obj)+1)
			l = l[:i-1] + [switch] + sorted(tmp)

			#print(l)

			break
	
	if l == p:
		a = cnt
	elif l == q:
		b = cnt

	if a != 0 and b != 0:
		break

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

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

for i in range(len(r)):
    if r[i] == p:
        a = i+1
    elif r[i] == q:
        b = i+1

print(abs(a-b))

Submission status: Runtime Error

Solution code:
def listExcludedIndices(data, indices=[]):
  return [N for i, x in enumerate(data) if i not in indices]
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]])

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
c1 = tuple(map(int, input().split()))
c2 = tuple(map(int, input().split()))
a = []
for i in range(1,n+1):
    a.append(i)
b = sorted(list(itertools.permutations(a)))
for i in range(len(b)):
    if b[i] == c1:
        d = i
    elif b[i] == c2:
        e = i
print(abs(d-e))

Submission status: Runtime Error

Solution code:
from itertools import permutations as p
N=int(input())
l=list(p(range(1,N+1)))

A=tuple(map(int, input().split()))
B=tuple(map(int, input().split()))

fa=True
fb=True
for i in range(len(l)):
  if l[i]==A and fa:
    idx_a=i
    fa=False
  elif l[i]==B and fb:
    idx_b=i
    fb=False
  if (not fa) and (not fb):
    break
print(abs(idx_a-idx_b))

Submission status: Runtime Error

Solution code:
import itertools

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

x = itertools.permutations(list(range(n)), n)
pnum = 0
qnum = 0
for index, xx in enumerate(x):
  if(xx == p):
    pnum = index
  if(xx == q):
    qnum = index
    
print(abs(pnum - qnum))

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
num = 0
for v in list(itertools.permutations(range(1,N+1))):
    num += 1
    if list(v) == P:
        A = num
    elif list(v) == Q:
        B = num
    else:
        pass
print(abs(A-B))

Submission status: Runtime Error

Solution code:
import itertools

n = input()

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

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

a = 0
b = 0

for i, v in enumerate(itertools.permutations(num_list, n)): 
    print(v) 
    if(list(v) == p_list):
        a = i
    if(list(v) == q_list):
        b = i
print(abs(a - b))

Submission status: Runtime Error

Solution code:
import itertools

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

l = [x+1 for x in range(n)]
ans = []
count = 0
for v in itertools.permutations(l):
    count += 1
    ans = list(v)
    if ans == a:
        a1 = count
    elif ans == b:
        b1 = count

print(abs(a1-b1))

Submission status: Runtime Error

Solution code:
count = 1
tmpp, tmpq = 0, 0
li = []

for i in range(1, n + 1):
    li.append(i)

pattern = list(itertools.permutations(li))

for i in pattern:
    if i == p:
        tmpp = count

    if i == q:
        tmpq = count

    count += 1

print(abs(tmpp - tmpq))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
p = tuple([int(i) for i in readline().split()])
q = tuple([int(i) for i in readline().split()])


from itertools import permutations
L = list(range(1,n+1))
for i,c in enumerate(permutations(L)):
    if c == p:
        a = i
    elif c == 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()))
lists = list(itertools.permutations(list(range(1, n+1)), n))
print(abs(lists.index(p) - lists.index()))

Submission status: Runtime Error

Solution code:
from itertools import permutations
import numpy as np
N=int(input())
l=[i+1 for i in range(N)]
P=np.array(list(map(int, input().split())))
Q=np.array(list(map(int, input().split())))
counter=0
for t in permutations(l):
  tmp=np.array(t)
  if all(P==tmp):
    a=counter
    
  if all(Q==tmp):
    b=counter
    
  counter+=1
  
print(abs(a-b))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    int n; cin >> n;
    vector <int> a(n), b(n);
    for(int& x : a)
        cin >> x;
    for(int& x: b)
        cin >> x;
    
    if(b < a) swap(a, b);
    
    int mx = 0;
    while(a != b) {
        next_permutation(a.begin(), a.end());
        mx += 1;
    }
    
    cout << mx;
    
    
    return 0;
}

Submission status: Runtime Error

Solution code:
from itertools import permutations

N = int(input())
*P, = map(int, input().split())
*Q, = map(int, input().split())
P = tuple(P); Q = tuple(Q)
for i, t in enumerate(permutations(range(1, N+1))):
    if t == P:
        a = i
    elif t == Q:
        b = i
print(abs(a - b))

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
a = tuple([ int(v)-1 for v in input().split() ])
b = tuple([ int(v)-1 for v in input().split() ])
t = 0
for i in itertools.permutations(range(n)):
    if a == i:
        xa = t
    elif b == i:
        xb = t
    t += 1
print(abs(xa-xb))