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: Wrong Answer

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

import itertools
a = 0
b = 0
for i,nums in enumerate(itertools.permutations(range(1,N+1))):
  if list(nums) == P:
    a = i
  if list(nums) == Q:
    b = i
print(b - a)

Submission status: Wrong Answer

Solution code:
import itertools
import math

n = int(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: Wrong Answer

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(len(alist)):
  if alist[i] == p:
    alen += i
blen = 0
for i in range(len(alist)):
  if alist[i] == q:
    blen += i
print(abs(blen-alen))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
a = -1
b = -1
count = 0
for per in permutations(range(1,n+1)):
    count += 1
    arr = [i for i in per]
    if arr==p:
        a = count
    elif arr==q:
        b = count

print(abs(a-b))

Submission status: Wrong Answer

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

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


a = 0
b = 0

for x in pattern:
  new_x = list(x)
  if new_x == p:
    a = pattern.index(x)
  elif new_x == q:
    b = pattern.index(x)


print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
p = map(int, input().split())
q = map(int, input().split())
d = dict()
for i, v in enumerate(itertools.permutations([x for x in range(1, n+1, 1)]), 1):
  d[str(v)] = i

print(d[str(tuple(p))] - d[str(tuple(q))])

Submission status: Wrong Answer

Solution code:
from itertools import permutations


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

p = q = 0
for i, x in enumerate(permutations(range(1, N + 1))):
    x = list(x)
    if x == P:
        p = i
    elif x == Q:
        q = i
print(abs(p - q))

Submission status: Wrong Answer

Solution code:
import itertools as it

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

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

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

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
def main():
    n = int(input())
    p_list = list(itertools.permutations ([i+1 for i in range(n)]))
    print(type(p_list[0]))
    p = tuple(map(int, input().split(" ")))
    q = tuple(map(int, input().split(" ")))
    pn = 0
    qn = 0
    for i in range(len(p_list)):
        if p == p_list[i]:
            pn = i
        if q == p_list[i]:
            qn = i
    print(abs(pn-qn))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools

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

# Nが10程度で小さい
# 階乗の全探索をしても間に合う
# Pythonならitertoolsが使える
# permutations（関数）にlistを渡すと、順列を生成！
l = [i for i in range(1, N+1)]
permutations_l = list(itertools.permutations(l))

a, b = 0, 0
for i, R in enumerate(permutations_l):
  if R == P: a = i
  elif R == Q: b = i
    
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
group=[]
for i in range(1,n+1):
  group.append(i)
p=tuple(list(map(int,input().split())))
q=tuple(list(map(int,input().split())))
a=0
b=0
count=1
for v in itertools.permutations(group):
    count+=1
    if v==p:
        a=count
    elif v==q:
        b=count
        exit()
print(abs(a-b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import factorial
from itertools import permutations

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

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

allnum = list(i for i in permutations(num, n))

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

Submission status: Wrong Answer

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

a = 0
b = 0
cnt = 0
def dfs(A):
    global cnt
    global a
    global b
    print(A)
    if len(A) == N:
        cnt += 1
        if A == P:
            a = cnt
        if A == Q:
            b = cnt
        return   
    for v in range(1,N+1):
        if v in A:
            continue
        A.append(v)
        dfs(A)
        A.pop()
dfs([])
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
#C - Count Order
#DFS
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

a = 0
b = 0
cnt = 0
def dfs(A):
    global cnt
    global a
    global b
    if len(A) == N:
        cnt += 1
        if A == P:
            a = cnt
        elif A == Q:
            b = cnt
        return   
    for v in range(1,N+1):
        if v in A:
            continue
        A.append(v)
        dfs(A)
        A.pop()
dfs([])
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
#C - Count Order
#DFS
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

a = 0
b = 0
cnt = 0
def dfs(A):
    global cnt
    global a
    global b
    if len(A) == N:
        cnt += 1
        if A == P:
            a = cnt
        elif A == Q:
            b = cnt
            print(b)
        return   
    for v in range(1,N+1):
        if v in A:
            continue
        A.append(v)
        dfs(A)
        A.pop()
dfs([])
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools as it
N=int(input())
W=list(range(1,N+1))
c1=-1
c2=-1
c=0
L=tuple(map(int,input().split()))
M=tuple(map(int,input().split()))
for i in it.permutations(W,N):
  c+=1
  if i==L:
    c1=c
  elif i==M:
    c2=c
print(abs(c1-c2))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
N =[i+1 for i in range(int(input()))]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
index = 0
x,y = 0,0
for n in permutations(N):
    if p==n:
        x = index
    elif q ==n:
        y = index

    index += 1

print(abs(x-y))

Submission status: Wrong Answer

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
list1 = [i for i in range(1, n + 1)]
ans_a = 0
ans_b = 0
for i in range(n):
    if p[i] != 1:
        ans_a += (p[i]-1)** (n-i-1)
    if q[i] != 1:
        ans_b += (q[i]-1) ** (n-i-1)
print(ans_a)
print(ans_b)

Submission status: Wrong Answer

Solution code:
import itertools
import math
N=int(input())
R=list(range(1,N+1))
a=tuple(map(int,input().split()))
ai=0
b=tuple(map(int,input().split()))
bs=0
c=0
K=list(itertools.permutations(R))
for i in range(math.factorial(N)):
  if K[i]==a:
    ai=i
    c+=1
  elif K[i]==b:
    bs=i
    c+=1
  elif c==2:
    break
print(abs(ai-bs))

Submission status: Wrong Answer

Solution code:
import itertools
import math
N=int(input())
R=list(range(1,N+1))
a=tuple(map(int,input().split()))
ai=0
b=tuple(map(int,input().split()))
bs=0
c=0
i=0
K=list(itertools.permutations(R))
for i in range(math.factorial(N)):
  if K[i]==a:
    ai=i
    c+=1
  elif K[i]==b:
    bs=i
    c+=1
  elif c==2:
    break
print(abs(ai-bs))

Submission status: Wrong Answer

Solution code:
import itertools

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

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

print(f.index(P) - f.index(Q))

Submission status: Wrong Answer

Solution code:
import itertools

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

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
from itertools import *
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
a = 0
b = 0
n = 0

for R in permutations(range(1,N+1)):
  R = list(R)
  if P==R:
    a=n
  elif Q==R:
    b=n
  n+=1

print(abs(b-a))

Submission status: Wrong Answer

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

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

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

i = 0
s = 0
t = 0
#print(p)
for v in itertools.permutations(l):
    #print(v)
    if v == p:
        s = i
    elif v == q:
        t = i
    
    i += 1

print(abs(t - s))

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())

a = list(input().split())
b = list(input().split())
str_a = ''.join(a)
str_b = ''.join(b)
li = [x for x in range(1, n+1)]
map = map(str, li)

p = itertools.permutations(map)
sorted_list = []
for v in p:
    tmp = ''.join(v)
    sorted_list.append(tmp)
sorted_list.sort()

ai = 0
bi = 0
for v in range(len(sorted_list)):
    if str_a == sorted_list[v]:
        ai = v
    elif str_b == sorted_list[v]:
        bi = v
print(abs(ai-bi))

Submission status: Wrong Answer

Solution code:
import itertools

N = list(range(1, int(input()) + 1))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

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

Submission status: Wrong Answer

Solution code:
N=int(input())
P= list(map(int, input().split()))
Q= list(map(int, input().split()))
dic=[]
Pn=0
Qn=0
import itertools
import numpy
l=numpy.arange(1,N+1,1,int)
for v in itertools.permutations(l, N):
  dic.append(v)
for i in range(len(dic)):
  if list(dic[i])==P:
    Pn=i
  elif list(dic[i])==Q:
    Qn=i
  elif Pn!=0 and Qn!=0:
    break
print(abs(Pn-Qn))

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
p = list(map(int,input().split()))
z = list(map(int,input().split()))
p_n = 0
q_n = 0
for i in range(len(p)):
    if i == 0:
        p_n = p[i]
        q_n = z[i]
    else:
        p_n = p[i]+(p_n*10)
        q_n = z[i]+(q_n*10)


q = deque()
ans_list = []
n_list = [0 for i in range(n)]


for i in range(n):
    n_list[i] = 1
    tmp = []
    for j in n_list:
        tmp.append(j)
    q.append([i+1,tmp])
    n_list[i] = 0

while len(q) != 0:
    tmp = q.popleft()
    s = str(tmp[0])
    if len(s) == n:
        ans_list.append(tmp[0])
        continue
    for i in range(n):
        tmp_n = tmp[0]
        if tmp[1][i] == 0:
            tmp_l = []
            for j in range(n):
                tmp_l.append(tmp[1][j])
            tmp_l[i] = 1
            q.append([(i+1)+tmp_n*10,tmp_l])

ans_list = sorted(ans_list)

p_i = 0
q_i = 0

for i in range(len(ans_list)):
    if ans_list[i] == p_n:
        p_i = i
    elif ans_list[i] == q_n:
        q_i = i

print(abs(p_i-q_i))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import itertools

n = int(stdin.readline().rstrip())
p = [x for x in stdin.readline().rstrip().split()]
q = [x for x in stdin.readline().rstrip().split()]

x = ()
y = ()
for pp in p:
    x = x + (pp,)
for qq in q:
    y = y + (qq,)

print(x)

c = []
for i in itertools.permutations([str(j) for j in range(1,n+1)]):
    c.append(i)

print(c)

a = c.index(x)
b = c.index(y)

print (abs(b-a))

Submission status: Wrong Answer

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

A = list(itertools.permutations(list(range(1, n+1))))
A.sort()
p = None
q = None
for i in range(math.factorial(n)):
    if P == list(A[i]):
        p = i+1
    elif Q == list(A[i]):
        q = i+1
    if p and q:
        print(abs(p - q))
        exit()

Submission status: Wrong Answer

Solution code:
import itertools

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

for i,v in enumerate(itertools.permutations(range(1,N+1),3)):
  if tuple(P)==v:
    a=i
  if tuple(Q)==v:
    b=i
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
tmp = list(itertools.permutations(range(1,n+1)))
tmp = sorted(tmp)
ans = []
for index,t in enumerate(tmp):
  if list(t) == P :
    ans.append(index+1)
  elif list(t) == Q :
    ans.append(index+1)

if len(ans) ==1:
  print(*ans)
else:
  print(abs(ans[0]-ans[1]))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
tmp = list(itertools.permutations(range(1,n+1)))
tmp = sorted(tmp)
ans = []
for index,t in enumerate(tmp):
  if list(t) == P or list(t) == Q :
    ans.append(index)

if len(ans) ==1:
  print(*ans)
else:
  print(abs(ans[0]-ans[1]))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
tmp = list(itertools.permutations(range(1,n+1)))
tmp = sorted(tmp)
ans = []
for index,t in enumerate(tmp):
  if  list(t) == P and list(t) == Q :
    ans.append(index)
  elif list(t) == P or list(t) == Q :
    ans.append(index)

if len(ans) ==1:
  print(*ans)
else:
  print(abs(ans[0]-ans[1]))

Submission status: Wrong Answer

Solution code:
import itertools

def countOrder(n, plist, qlist):

    ans = 0
    p = 0
    q = 0

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

    for index, i in enumerate(irelist):
        if i == plist:
            p = index
        elif i == qlist:
            q = index

    ans = abs(p-q)

    return ans

def main():
    n = int(input())
    plist = tuple(map(int, input().split()))
    qlist = tuple(map(int, input().split()))
    print(countOrder(n, plist, qlist))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#25
import itertools
N = int(input())
n = [i for i in range(1,N+1)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))
cou = 0
a,b = 0,0

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

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import statistics
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
per_lis = itertools.permutations(list(range(1,n+1)))
a=0
b=0
i=0
for elem in per_lis:
    i+=1
    if elem==p:
        a=i
    elif elem==q:
        b=i
print(abs(a-b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
p_num=0
q_num=0
mylist=list(range(1,n+1))
jyunretsu=list(itertools.permutations(mylist,n))

for i in range(len(jyunretsu)):
    if list(jyunretsu[i])==p:
        p_num=i
    elif list(jyunretsu[i])==q:
        q_num=i
print(abs(p_num-q_num))

Submission status: Wrong Answer

Solution code:
import itertools

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

l = []
a, b,num = 0,0,0

for m in range(1,n+1):
	l.append(m)

for i in itertools.permutations(l):
	num += 1
	if i == p:
		a = num
	elif i == q:
		b = num
print(abs(a-b))

Submission status: Wrong Answer

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

kaijo = 1
for i in range(N):
  kaijo *= (N - i)
A = list(itertools.permutations(a, N))
ansP = 0
for i in range(kaijo):
  if(list(A[i]) == P):
    ansP = i
ansQ = 0
for i in range(kaijo):
  if(list(A[i]) == Q):
    ansQ = i
print(ansQ-ansP)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
cnt = 0
a = -1
b = -1
for per in itertools.permutations(range(1,n+1)):
  cnt += 1
  if list(per) == P and a == -1:
    a = cnt
  if list(per) == Q and a != -1:
    b = cnt

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N = int(input())

p = ''.join(input().split())
q = ''.join(input().split())

l = permutations(range(1, N+1))
l2 = []
for i in l:
    l2.append(''.join(map(str, i)))
l = sorted(l2)

p_idx = 0
q_idx = 0
for n, i in enumerate(l):
    if i == p:
        p_idx = n
    elif i == q:
        q_idx = n
print(abs(p_idx-q_idx))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N = int(input())

p = ''.join(input().split())
q = ''.join(input().split())

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

p_idx = 0
q_idx = 0
for n, i in enumerate(l):
    if ''.join(map(str, i)) == p:
        p_idx = n
    elif ''.join(map(str, i)) == q:
        q_idx = n
print(abs(p_idx-q_idx))

Submission status: Wrong Answer

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


def dfs(x, d, res):
    if len(x) == N + 1:
        d[0] += 1
        if x[1:] == P:
            res["P"] = d[0]
        elif x[1:] == Q:
            res["Q"] = d[0]
        return

    for v in range(1, N + 1):
        if v not in x:
            x.append(v)
            dfs(x, d, res)
            x.pop()

res = {"P": 0, "Q": 0}
dfs([0], [0], res)
print(abs(res["P"] - res["Q"]))

Submission status: Wrong Answer

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

nn = list(range(1,n + 1))

c = itertools.permutations(nn, n)

p_p = 0
q_p = 0
for i, j in enumerate(c):
    if list(j) == p:
        p_p = i
    elif list(j) == q:
            q_p = i
print(abs(q_p - p_p))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
N = int(input())
ps = permutations([i for i in range(1, N+1)])
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = None
b = None
for i,p in enumerate(ps):
    if P == p:
        a = i+1
    elif Q == p:
        b = i+1
    if a and b:
        print(abs(a-b))
        exit(0)

Submission status: Wrong Answer

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

dict_list = list(itertools.permutations(sorted(a), len(a)))

a_pos=0
b_pos=0
for i,dic in enumerate(dict_list):
    if list(dic) == a:
        a_pos=i
    elif list(dic) == b:
        b_pos=i

print(abs(a_pos-b_pos))

Submission status: Wrong Answer

Solution code:
import itertools

n = sorted([x+1 for x in range(int(input()))])
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
count = 0
pv = 0
qv = 0
for i in itertools.permutations(n,len(n)):
  count += 1
  if i == p:
    pv = count
  elif i == q:
    qv = count

print(abs(pv-qv))

Submission status: Wrong Answer

Solution code:
import itertools

n = [x+1 for x in range(int(input()))]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
count = 0
pv = 0
qv = 0
for i in itertools.permutations(n):
  count += 1
  if i == p:
    pv = count
  elif i == q:
    qv = count

print(abs(pv-qv))

Submission status: Wrong Answer

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

from itertools import permutations as per
tuples = per(range(1, N+1))

a = 0
b = 0
for i, t in enumerate(tuples):
    if t == P:
        a = i
    elif t == Q:
        b = i
print(abs(a-b))

Submission status: Wrong Answer

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

from itertools import permutations as per
tuples = per(range(1, N+1))

pi = 0
qi = 0
for i, p in enumerate(tuples):
    if p == P:
        pi = i
    elif p == Q:
        qi = i
print(abs(pi-qi))

Submission status: Wrong Answer

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

from itertools import permutations as per
tuples = per(range(1, N+1), N)

pi = 0
qi = 0
for i, p in enumerate(tuples):
    if p == P:
        pi = i
    elif p == Q:
        qi = i
print(abs(pi-qi))

Submission status: Wrong Answer

Solution code:
N = int(input())
P = ''.join(map(str, input().split()))
Q = ''.join(map(str, input().split()))

from itertools import permutations as per

pi = 0
qi = 0
for i, p in enumerate(per(range(1, N+1), N)):
    s = ''.join(map(str, list(p)))
    if s == P:
        pi = i
    elif s == Q:
        qi = i
print(abs(pi-qi))

Submission status: Wrong Answer

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

from itertools import permutations as per

pi = 0
qi = 0
for i, p in enumerate(per(range(1, N+1), N)):
    if p == P:
        pi = i
    elif p == Q:
        qi = i
print(abs(pi-qi))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import permutations

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

if p == q:
    print(0)

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

Submission status: Wrong Answer

Solution code:
import itertools

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

lis = [i for i in range(1, N+1)]
m = []

for i in itertools.permutations(lis, len(lis)):
    m.append(i)

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

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=[]
for i in range(1,n+1):
    l.append(i)
l=list(itertools.permutations(l))
l=sorted(l)
k=1
for i in range(1,n+1):
    k*=i
pp=0
qq=0
for j in range(k):
    if l[j]==p:
        pp=j+1
    elif l[j]==q:
        qq=j+1
print(abs(pp-qq))

Submission status: Wrong Answer

Solution code:
import itertools

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

for i in range(1,N+1):
    order.append(i)

#print(coordinate)

count = 0
print(P)
print(Q)
for vect in itertools.permutations(order):
    print(vect)
    count +=1
    if vect == P:
        a = count
    if vect ==Q:
        b = count

print(abs(a-b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import permutations

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

p = 0
q = 0
i = 0
for n in permutations(range(1,N+1)):
    if n == P:
        p = i
    elif n == Q:
        q = i
    i += 1

print(abs(p-q))

Submission status: Wrong Answer

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: Wrong Answer

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


import itertools
a = (list(itertools.permutations(list(range(1,n+1)))))
#print(a)

b = 0
c = 0
cnt = 1
for i in a:
    if c != 0 and b!=0:
        break
    if i == p:
        c += cnt
    if i == q:
        b += cnt
    else:
        cnt += 1
#print(c)
#print(b)
print(abs(c-b))

Submission status: Wrong Answer

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


import itertools
a = (list(itertools.permutations(list(range(1,n+1)))))

b = 0
c = 0
cnt = 1
for i in a:
    if i == p:
        c += cnt
    elif i == q:
        b += cnt
    if c!= 0 and b!=0:
        break
    else:
        cnt += 1
print(abs(c-b))

Submission status: Wrong Answer

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


import itertools
a = (list(itertools.permutations(list(range(1,n+1)))))

b = 0
c = 0
cnt = 0
for i in a:
    if i == p:
        c += cnt
    elif i == q:
        b += cnt
    if c!= 0 and b!=0:
        break
    else:
        cnt += 1
print(abs(c-b))

Submission status: Wrong Answer

Solution code:
import itertools

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

print(abs(L.index(tuple(Q))-L.index(tuple(P))))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
permutation = list(itertools.permutations([x for x in range(1, N + 1)]))
a, b = 0, 0

for i in range(len(permutation)):
    if permutation[i] == P:
        a = i
    elif permutation[i] == Q:
        b = i

print(abs(a - b))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
permutation = list(itertools.permutations([x for x in range(1, N + 1)]))
a, b = 0, 0

for i in range(len(permutation)):
    if permutation[i] == P:
        a = i
    elif permutation[i] == Q:
        b = i
    if a and b:
        break
print(abs(a - b))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P  = list(map(int,input().split()))
Q  = list(map(int,input().split()))
left = 0
right = 0
left_is_ok = 0
right_is_ok = 0
per_list = [list(i) for i in itertools.permutations(range(1,N + 1))]
for i,per in enumerate(per_list):
    if P == per:
        left = i
        left_is_ok = 1
    elif Q == per:
        right = i
        right_is_ok = 1
    if left_is_ok and right_is_ok:
        break

print(abs(left - right))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P  = tuple(map(int,input().split()))
Q  = tuple(map(int,input().split()))
left = 0
right = 0
left_is_ok = 0
right_is_ok = 0
per_list = list(itertools.permutations(range(1,N + 1)))
for i,per in enumerate(per_list):
    if P == per:
        left = i
        left_is_ok = 1
    elif Q == per:
        right = i
        right_is_ok = 1
    if left_is_ok and right_is_ok:
        break

print(abs(left - right))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P  = tuple(map(int,input().split()))
Q  = tuple(map(int,input().split()))
left = 0
right = 0
per_list = list(itertools.permutations(range(1,N + 1)))
for i,per in enumerate(per_list):
    if P == per:
        left = i
    elif Q == per:
        right = i
    if left and right:
        break

print(abs(left - right))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
p = str(P[0])
q = str(Q[0])
for i in range(1,N):
  p += str(P[i])
  q += str(Q[i]) 
p = int(p)
q = int(q)
c = 0
for v in itertools.permutations(P, N):
  v = list(v)
  k = str(v[0])
  for i in range(1,N):
    k += str(v[i])
  k = int(k)
  if p<k<q or q<k<p:
    c += 1
if c == 0:
  print(c)
else:
  print(c+1)

Submission status: Wrong Answer

Solution code:

import itertools


def main():
    N = int(input())
    PArry = list(map(int, input().split()))
    QArry = list(map(int, input().split()))

    a = 0
    b = 0
    comb = [i for i in range(1, N+1)]
    for i, iter in enumerate(itertools.permutations(comb)):
        listIter = list(iter)
        if PArry == listIter:
            a = i+1
        if QArry == listIter:
            a = i+1

    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import itertools


def main():
    N = int(input())
    *PArry, = list(map(int, input().split()))
    *QArry, = list(map(int, input().split()))

    a = 0
    b = 0
    comb = [i for i in range(1, N+1)]
    for i, iter in enumerate(itertools.permutations(comb)):
        listIter = list(iter)
        if PArry == listIter:
            a = i+1
        if QArry == listIter:
            a = i+1

    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

a = 0
b = 0
for i in range(len(ans)) :
  if ans[i] == p:
    a = i+1
  elif ans[i]== q:
    b = i+1
      
print(abs(a-b))