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:
A=[] #appendのために宣言が必要
while True:
    try:
        A.append(list(map(int,input().split())))
    except:
        break;
import itertools
B = list(itertools.permutations(range(1,A[0][0]+1)))
a = 0
b = 0
for l in range(len(B)):
  if a != 0 and b != 0:
    break;
  else:
    if A[1] == list(B[l]):
      a = l

    elif A[2] == list(B[l]):
      b = l
    else:
      continue
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
a=int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C=[0,1,2,6,24,120,720,5040,40320]
d=0
e=0

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

Solution code:
import sys
import itertools

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

seq = (1, 2, 3, 4, 5, 6, 7, 8)
arrays0 = list(itertools.permutations(seq, n))

arrays1 = p
arrays2 = q

print(arrays0, arrays1, arrays2)
print(arrays0 in arrays1)

sys.exit()

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
p = list(map(int, input().split()))
for i in range(len(p)):
	p[i] = p[i]-1
p = tuple(p)
q = list(map(int, input().split()))

a = list(itertools.permutations(range(n)))

for i in range(len(q)):
	q[i] = q[i]-1
q = tuple(q)
print(a.index(p) - a.index(q))

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

p = input()
q = input()
#print(n)
#print(p)
i = 0
a = 0 
b = 0

for x in itertools.permutations(range(1,n+1), n):
	#print(x)
	x = " ".join(map(str,x))
	#print(x)
	i += 1
	if x == p:
		a = i
		#print(i)
	elif x == q:
		b = i
		#print(i)
        
print(abs(a-b))

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

p = input()
q = input()
#print(n)
#print(p)
i = 0
a = 0 
b = 0

for x in itertools.permutations(range(1,n+1), n):
	#print(x)
	x = " ".join(map(str,x))
	#print(x)
	i += 1
	if x == p:
		a = i
		print(i)
	elif x == q:
		b = i
		print(i)
        
print(abs(a-b))

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

p = input()
q = input()
print(n)
print(p)
i = 0
a = 0 
b = 0

for x in itertools.permutations(range(1,n+1), n):
	#print(x)
	x = " ".join(map(str,x))
	#print(x)
	i += 1
	if x == p:
		a = i
		#print(i)
	elif x == q:
		b = i
		#print(i)
        
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import math
 
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
 
p_sort = sorted(p)
q_sort = sorted(q)
 
p_number = 1
q_number = 1
 
k = 0
for i in p:
  k += 1
  i_index = p_sort.index(i)
  p_number += i_index * math.factorial(n-k)
  p.remove(i)
  p_sort.remove(i)
k = 0
for i in q:
  k += 1
  i_index = q_sort.index(i)
  q_number += i_index * math.factorial(n-k)
  q.remove(i)
  q_sort.remove(i)
answer = abs(p_number - q_number)
print(answer)

Submission status: Wrong Answer

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

def calc_bango(lst):
  ban = 0
  for i in range(N):
    ban += lst[i]*math.factorial(N-i-1)  
  return(ban)

a = calc_bango(P)
b = calc_bango(Q)
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
N = int(input())
p = tuple(int(x) for x in input().split())
q = tuple(int(x) for x in input().split())
r = []
a=0
c1 =0
c2 =0
import itertools

def for_factorial(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
    return val

a =for_factorial(N)

r =list(itertools.permutations(range(1,N+1)))
for i in range(a):
    if p == r[i]:
        c1 = i
    elif q == r[i]:
        c2 = i

print(abs(c1-c2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import itertools

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
a = b  = count= 0

org = sorted(p)

for v in itertools.permutations(org):
    count += 1
    
    if list(v)==p:
        a = count
    elif list(v)==q:
        b = count

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

def factorial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
    return ans

p_num = factorial(n-1)*p[0]
q_num = factorial(n-1)*q[0]

if n > 3:
    for i in range(1, n-1):
        #print(factorial(n-i))
        p_num -= ((n-i)-p[i])*factorial(n-i-1)
        q_num -= ((n-i)-q[i])*factorial(n-i-1)
        #print(p_num)
        #print(q_num)

if p[-2] < p[-1]:
    p_num -= 1

if q[-2] < q[-1]:
    q_num -= 1

print(abs(p_num-q_num))

Submission status: Wrong Answer

Solution code:
import math

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

p_sort = sorted(p)
q_sort = sorted(q)

p_number = 0
q_number = 0

k = 0
for i in p:
  k += 1
  i_index = p_sort.index(i)
  p_number += i_index * math.factorial(n-k)
  p.remove(i)
  p_sort.remove(i)

k = 0
for i in q:
  k += 1
  i_index = q_sort.index(i)
  q_number += i_index * math.factorial(n-k)
  q.remove(i)
  q_sort.remove(i)

answer = abs(p_number - q_number)
print(answer)

Submission status: Wrong Answer

Solution code:
def dictionary_order(Xs, n):
  ascend = [i+1 for i in range(n)]

  order = 1
  for i, x in enumerate(Xs):
    order += ascend.index(x) * factorial(n-1-i)
    ascend.remove(x)
  return order

def factorial(n):
  out = 1
  for i in range(2, n+1):
    out *= i
  return out

def main():
  N = int(input())
  Ps = list(map(int, input().split()))
  Qs = list(map(int, input().split()))
  if Ps == Qs:
    print(0)
  else:
    P_order = dictionary_order(Ps, N)
    Q_order = dictionary_order(Qs, N)
    print(P_order, Q_order, abs(P_order-Q_order))
  
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
import itertools

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

all_list = list(itertools.permutations(p))
all_list.sort()

p_ans = 0
q_ans = 0

for i in range(len(all_list)):
    if list(all_list[i]) == p:
        p_ans = i
    elif list(all_list[i]) == q:
        q_ans = i
print(abs(p_ans - q_ans))

Submission status: Wrong Answer

Solution code:
def dictionary_order(Xs, n):
  ascend = [i+1 for i in range(n)]

  order = 1
  for i, x in enumerate(Xs):
    order += ascend.index(x) * factorial(n-1-i)
    ascend.remove(x)
    print(ascend)
  return order

def factorial(n):
  out = 1
  for i in range(2, n+1):
    out *= i
  return out

def main():
  N = int(input())
  Ps = list(map(int, input().split()))
  Qs = list(map(int, input().split()))
  if Ps == Qs:
    print(0)
  else:
    P_order = dictionary_order(Ps, N)
    Q_order = dictionary_order(Qs, N)
    print(P_order, Q_order, abs(P_order-Q_order))
  
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


def LI(): return list(map(int, input().split()))


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    K, X= LI()

    if (500*K>=X):
        print('Yes')
    else:
        print('No')


    return

# B


def B():
    N=II()
    S=input().rstrip()
    ans=0
    for i in S:
        if i=="A":
            t=1
            continue
        if i=="B" and t==1:
            t=2
            continue
        if i=="C" and t==2:
            t=0
            ans+=1
            continue
        t=0
    print(ans)



    return

# C


def true():
    S = input()
    S = S[:-1]

    tmp = 0
    for x in range(1 << (len(S)-1)):
        func = S[-1]
        for i in range(len(S)-1):
            if(x & (1 << i)):
                tmp += int(func)
                func = S[-2-i]
            else:
                func = S[-2-i]+func
        tmp += int(func)
    print(tmp)

    return


def flagcounter(x):
    if (x == 0):
        return 0
    return flagcounter(x >> 1)+(x & 1)

def jisyo(P):
    i=1
    S = sorted(P)
    X=0
    N=len(P)
    for p in P:
    
        X+=((S.index(p)+1) * (math.factorial(N-i)))-1
        
        S.remove(p)
        if len(S)==1:
            break
        i+=1
    return X

def C():
    N=II()
    P=LI()
    Q=LI()

    X=jisyo(P)
    Y=jisyo(Q)


    print(X-Y)
        
    return


# D
def D():
    N, K = LI()
    R, S, P = LI()
    T=list(input().rstrip())
    ans = 0
    for j in range(K):
        if T[j] == 'r':
            ans += P
        if T[j] == 'p':
            ans += S
        if T[j] == 's':
            ans += R
    j = 0
    rps=['r','p','s']

    for i in range(K, N):
        if T[j] == T[i]:
            if i+K>=N:
                j += 1
                continue
            T[i]=rps[rps.index(T[i+K])-1]
            j += 1
            continue
        if T[i] == 'r':
            ans += P
        if T[i] == 'p':
            ans += S
        if T[i] == 's':
            ans += R
        j += 1
    print(ans)
    return


def Dsyakutori():

    N, K = LI()
    A = LI()
    MAX = sum(A)
    if MAX < K:
        print(0)
        return
    Temp = MAX
    ans = 1
    for i in range(N):
        Temp = Temp-A[i]
        if (Temp >= K):
            ans += 1
        else:
            T = Temp+A[i]
            break
    Temp = T
    ckeep = N
    for k in range(N):
        c = N-1
        i -= 1
        Temp = T

        for j in range(N):
            Temp = Temp-A[c]
            if (Temp >= K):
                ans += 1

            elif(c < ckeep and i >= 0):
                ans += i
                ckeep = c
                break
            else:
                ckeep = c
                break
            c -= 1
        if i < 0:
            break
        T = T+A[i]
    print(ans)

    return

# E


def E():
    N, M= LI()
    A = LI()
    A.sort(reverse=True)
    B=[0]*(N+1)
    for i in range(1,N+1):
        B[i]=B[i-1]+A[i-1]
    D=[0]*(2*10**5+1)
    for i in range(N):
        D[A[i]]+=1
    for i in range(len(D)-1,0,-1):
        D[i-1]+=D[i]
    l=-1
    r=2*10**5+1
    while r-l>1:
        m=(l+r)//2
        s=0
        for i in range(N):
            s+=D[max(1,m-A[i])]
        if s>=M:
            l=m
        else:
            r=m
    ans=0
    s=0
    for i in range(N):
        v=max(0,r-A[i])
        t=max(0,min(D[v],M-s))
        ans+=B[t]+t*A[i]
        s+=t
    ans+=l*(M-s)
    print(ans)

    
    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    N = len(S)

    pass


# Solve
if __name__ == '__main__':

    C()

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=[]
for i in range(1,n+1):
    a.append(i)
c=tuple(p)
d=tuple(q)
cnt=0
ans=0
ansl=0
for i in itertools.permutations(a):
    cnt+=1
    if c==i:
        ans=cnt
    elif d==i:
        ansl=cnt
print(abs(ans-ansl))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
data = range(1,N+1)
p_list = list(itertools.permutations(data, N))

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

P_num = 0
Q_num = 0


def equal_list(lst1, lst2):
    lst = lst1.copy()
    for element in lst2:
        try:
            lst.remove(element)
        except ValueError:
            break
    else:
        if not lst:
            return True
    return False

for index,data in enumerate(p_list):
  if list(data) == P:
    P_num = index
  elif list(data) == Q:
    Q_num = index
    
print(abs(P_num - Q_num))

Submission status: Wrong Answer

Solution code:
import itertools
def main():
    n=int(input())
    P=tuple(map(int,input().split()))
    Q=tuple(map(int,input().split()))
    L=[i for i in range(1,n+1)]
    S=itertools.permutations(L,n)
    ans=0
    flag=1
    i=0
    for s in S:
        i+=1
        if s==P or s==Q:
            if flag:
                ans -= i
                flag =0
            else:
                ans += i
                break
    print(ans)
main()

Submission status: Wrong Answer

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

permutation = list(itertools.permutations(seq))

P_num = 0
Q_num = 0

for i in range(len(permutation)):
  if permutation[i]==P:
    P_num += i
  elif permutation[i]==Q:
    Q_num += i
  else:
    continue
    
answer = abs(P_num-Q_num)

print(int(answer))

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
cnt = 0

tmp = sorted(P)

print(P)

for x in itertools.permutations(tmp):
    cnt += 1

    if P == list(x):
        a = cnt

    if Q == list(x):
        b = cnt

print(abs(a - b))

Submission status: Wrong Answer

Solution code:
A=[] #appendのために宣言が必要
while True:
    try:
        A.append(list(map(int,input().split())))
    except:
        break;
import itertools
B = list(itertools.permutations(range(1,A[0][0]+1)))
a = 0
b = 0
for l in range(len(B)):
  if a != 0 and b != 0:
    break;
  else:
    if A[1] == list(B[l]):
      a = l
    elif A[2] == list(B[l]):
      b = l
    else:
      continue
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import math

def count_index(list, k):
    count = 0
    for i in range(k):
        if (list[i] < list[k]):
            count = count + 1
    return list[k]-count-1

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

a = (p[0]-1) * math.factorial(n)+1
b = (q[0]-1) * math.factorial(n)+1
print(a)
print(b)

for i in range(1,n):
    a = a + count_index(p, i) * math.factorial(n-i)
    b = b + count_index(q, i) * math.factorial(n-i)

ans = a-b
print(abs(ans))

Submission status: Wrong Answer

Solution code:
A=[] #appendのために宣言が必要
while True:
    try:
        A.append(list(map(int,input().split())))
    except:
        break;
import itertools
B = list(itertools.permutations(range(1,A[0][0]+1)))
a = 0
b = 0
for l in range(len(B)):
  if a != 0 and b != 0:
    break;
  else:
    if A[1] == list(B[l]):
      a = l

    elif A[2] == list(B[l]):
      b = l
    else:
      continue

print(a-b)

Submission status: Wrong Answer

Solution code:
A=[] #appendのために宣言が必要
while True:
    try:
        A.append(list(map(int,input().split())))
    except:
        break;
import itertools
B = list(itertools.permutations(range(1,A[0][0]+1)))
print(list(B[0]))
a = 0
b = 0
for l in range(len(B)):
  if a != 0 and b != 0:
    break;
  else:
    if A[1] == list(B[l]):
      a = l

    elif A[2] == list(B[l]):
      b = l
    else:
      continue

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
L1 = tuple(map(int,input().split()))
a = 0
L2 = tuple(map(int,input().split()))
b = 0
L = [i for i in range(1,N+1)]
Base = list(itertools.permutations(L))
print(Base)
for i in range(len(Base)):
    if Base[i] == L1:
        a = i+1
for i in range(len(Base)):
    if Base[i] == L2:
        b = i+1
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import itertools

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a_count = b_count  = count= 1

org = sorted(a)

for v in itertools.permutations(org, n):
    if list(v)==a:
        a_count = count
    elif list(v)==b:
        b_count = count     
    count += 1

print(abs(a_count - b_count))

Submission status: Wrong Answer

Solution code:
import itertools

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

a = 0
b = 0
i = 0
for v in itertools.permutations(lists):
    if v == P:
        a = i
    elif v == Q:
        b = i
    elif a !=0 and b != 0:
        break
    i += 1

print(abs(b - a))

Submission status: Wrong Answer

Solution code:
import itertools

Submission status: Wrong Answer

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

import itertools

pi = 0
count = 1
for i in itertools.permutations(range(1, n+1), n):
    if list(i) == p:
        pi = count
        break
    else:
        count += 1

qi = 0
count = 1
for i in itertools.permutations(range(1, n+1), n):
    if list(i) == q:
        qi = count
        break
    else:
        count += 1


print(pi, qi)
ans = abs(pi - qi)
print(ans)

Submission status: Wrong Answer

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

permutation = list(itertools.permutations(seq))

P_num = 0
Q_num = 0

for i in range(len(permutation)):
  if permutation[i]==P:
    P_num += i
  elif permutation[i]==Q:
    Q_num += i
  else:
    continue
    
answer = abs(P_num-Q_num)

print(answer)

Submission status: Wrong Answer

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

import itertools

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

zyu = itertools.permutations(l, n)

for t, child in enumerate(zyu):
    p_flag = True
    q_flag = True
    print(t,child)
    for check_idx in range(0, n):
        if list(child)[check_idx] == p[check_idx]:
            pass
        else:
            p_flag = False
            break

    for check_idx in range(0, n): 
        print(check_idx)
   
        if list(child)[check_idx] == q[check_idx]:
            pass
        else:
            q_flag = False
            break

    if p_flag:
        p_idx = t+1

    if q_flag:
        q_idx = t+1



print(abs(q_idx-p_idx))

Submission status: Wrong Answer

Solution code:
import itertools

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


pos = 0
pos_p = 0
pos_q = 0
for i in itertools.permutations(range(1,N)):
    pos += 1
    if i == P:
        pos_p = pos
    if i == Q:
        pos_q = pos

print(abs(pos_p - pos_q))

Submission status: Wrong Answer

Solution code:
import itertools
def main():
    N = int(input())
    A = list(map(int , input().split()))
    B = list(map(int , input().split()))
    #B = input()
    #print (A)
    #print (S)
    #list_N = []
    list_N = list(range(1,N+1))
    #print (list_N)
    #AA = A.index(N) + 1
    #BB = B.index(N) + 1
    #print (AA)
    n1 = 0
    n2 = 0
    count_n = 0
    for i in itertools.permutations(list_N) :
        list_i = list(i)
        #print(list_i)
        #print(A)
        count_n = count_n + 1
        if A == list_i :
            n1 = count_n
        elif B == list_i :
            n2 = count_n
        #print (count_n)
    #print (n1)
    #print (n2)
    print (str(abs(n1 - n2)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

import itertools
tmp = [i for i in range(1,N+1)]
#print(tmp)
tmp2 = itertools.permutations(tmp)
a,b = 0,0
for n,j in enumerate(tmp2):
  #print(n,j)
  if list(j)==P:
    a = n+1
  elif list(j)==Q:
    b = n+1
res=abs(a-b)

print(res)

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
if P == Q:
  print(0)
  exit()
a = []
for i in range(N):
  a.append(i + 1)
plist = list(itertools.permutations(a))
Pi = -1
Qi = -1
for i, p in enumerate(plist):
  if Pi != -1 and Qi != -1:
    print(abs(Pi - Qi))
    break
  if p == P:
    Pi = i + 1
  if p == Q:
    Qi = i + 1

Submission status: Wrong Answer

Solution code:
import itertools

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


pos = 0
pos_p = 0
pos_q = 0
for i in itertools.permutations(range(1,4)):
    pos += 1
    print(type(i), i)
    if i == P:
        pos_p = pos
    if i == Q:
        pos_q = pos

print(abs(pos_p - pos_q))

Submission status: Wrong Answer

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

import itertools

s = []

for j in range(N):
    s.append(j + 1)

n_P = 0
n_Q = 0

list_seed = list(itertools.permutations(s, N))
list_seed = [list(list_seed[i]) for i in range(len(list_seed))]

for i in range(len(list_seed)):
    if list_seed[i] == P:
        n_P = i + 1
    elif list_seed[i] == Q:
        n_Q = i + 1

print(abs(n_P - n_Q))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P_ar = list(map(int,input().split()))
Q_ar = list(map(int,input().split()))

List = list(map(list,list(itertools.permutations(range(N)))))

for i in range(N):
  P_ar[i]-=1
  Q_ar[i]-=1

a=-1
b=-1

for i in range(len(List)):
  if List[i]==P_ar:
    a=i
    break

for i in range(len(List)):
  if List[i]==Q_ar:
    b=i
    break

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans1 = 0
ans2 = 0
for i in range(n):
  k = math.factorial(n-1-i)
  ans1 += k*p[i] - 1
  ans1 += k*p[i] - 1
print(abs(ans1 - ans2))

Submission status: Wrong Answer

Solution code:
from math import factorial

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

def f(X):
    c = 0
    a = [i+1 for i in range(n)]
    for i in range(n):
        a.sort()
        # print(X, a, c)
        if X[i]!=min(a):
            c += factorial(n-i-1)*(a.index(X[i]))
        del a[a.index(X[i])]
    return c+1

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

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
L1 = tuple(map(int,input().split()))
a = 0
L2 = tuple(map(int,input().split()))
b = 0
L = [i for i in range(1,N+1)]
Base = list(itertools.permutations(L))
for i in range(len(Base)):
    if Base[i] == L1:
        a = i
    elif Base[i] == L2:
        b = i
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
A = tuple(map(int, input().split()))
if P == A:
  print(0)
  exit()
a = []
for i in range(N):
  a.append(i + 1)
plist = list(itertools.permutations(a))
Pi = -1
Ai = -1
for i, p in enumerate(plist):
  if Pi != -1 and Ai != -1:
    print(abs(Pi - Ai))
    break
  if p == P:
    Pi = i + 1
  if p == A:
    Ai = i + 1
if Pi != -1 and Ai == -1:
  print(Pi)
elif Pi == -1 and Ai != -1:
  print(Ai)

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
A = tuple(map(int, input().split()))
if P == A:
  print(0)
  exit()
a = []
for i in range(N):
  a.append(i + 1)
plist = list(itertools.permutations(a))
Pi = -1
Ai = -1
for i, p in enumerate(plist):
  if Pi != -1 and Ai != -1:
    print(abs(Pi - Ai))
    break
  if p == P:
    Pi = i + 1
  if p == A:
    Ai = i + 1

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()))
Nl = list(range(1, N+1))
p = list(itertools.permutations(Nl,N))
c1 = 1
c2 = 1
print(math.factorial(N))
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: Wrong Answer

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
A = tuple(map(int, input().split()))
if P == A:
  print(0)
  exit()
a = []
for i in range(N):
  a.append(i + 1)
plist = list(itertools.permutations(a))
Pi = -1
Ai = -1
for i, p in enumerate(plist):
  if Pi != -1 and Ai != -1:
    print(abs(Pi - Ai))
    break
  if p == P:
    Pi = i
  if p == A:
    Ai = i

Submission status: Wrong Answer

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

import itertools
tmp = [i for i in range(1,N+1)]
#print(tmp)
tmp2 = itertools.permutations(tmp)
a,b = 0,0
for n,j in enumerate(tmp2):
	#print(j)
    if list(j)==P:
      a = n
    elif list(j)==Q:
      b = n
res=abs(a-b)

print(res)

Submission status: Wrong Answer

Solution code:
import itertools

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

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

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
  from itertools import permutations
  N = int(input())
  P = input()
  P = ''.join(P.split())
  Q = input()
  Q = ''.join(Q.split())
  
  base = ''
  for i in range(N):
    base += str(i+1)
  
  perm = sorted(''.join(chars) for chars in permutations(base))
  x = -1
  y = -1
  for idx, p in enumerate(perm):
    if P == p:
      x = idx
    elif Q == p:
      y = idx
    if not (x == -1) and not (y == -1):
      break
  print(abs(x-y))

Submission status: Wrong Answer

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))
a=0
b=0
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: Wrong Answer

Solution code:
input()
s1 = input().split(" ")
s2 = input().split(" ")
ss1 = [int(i) for i in s1]
ss2 = [int(i) for i in s2]
import itertools
a=0
b=0
c = 0
for i in itertools.permutations([int(i) for i in range(1,9)]):
  c+=1
  if list(i) == ss1:
    a=c
  if list(i) == ss2:
    b=c
print(abs(a-b))

Submission status: Wrong Answer

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

permutation = list(itertools.permutations(seq))

P_num = 0
Q_num = 0

for i in range(len(permutation)):
  if permutation[i]==P:
    P_num+=i
  elif permutation[i]==Q:
    Q_num+=i
    
answer = abs(P_num-Q_num)

print(answer)

Submission status: Wrong Answer

Solution code:
import itertools

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

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

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
PER = []
for i in range(N):
    PER.append(i+1)

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

for x in itertools.permutations(PER):

    flag_a = 0
    flag_b = 0

    for i in range(N):
        if x[i] == P[i]:
            flag_a += 1   
        if x[i] == Q[i]:
            flag_b += 1
    if flag_b == N:
        b = tmp
    elif flag_a == N:
        a = tmp
    tmp += 1

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()))
n_list = [i for i in range(1,N+1)]
n_perm = sorted(list(itertools.permutations(n_list, N)))
a = 0
b = 0

for i in range(N):
    if list(n_perm[i]) == P:
        a = i
        break

for k in range(N):
    if list(n_perm[k]) == Q:
        b = k
        break
        
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

s = int(input())

alist = [int(x) for x in input().split(maxsplit=(s-1))]
blist = [int(x) for x in input().split(maxsplit=(s-1))]

if alist == blist:
    print(0)

perm = permutations(sorted(alist))

count = 1
a = 0
b = 0

for i in list(perm):
    if alist == list(i):
        a = count
        pass
    elif blist == list(i):
        b = count
        pass
    count += 1

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
from itertools import permutations

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

num_list = list(range(1,N+1))

a = 0
b = 0
index = 0
for v in permutations(num_list,N):
    if v == P:
        a = index
    elif v == Q:
        b = index
        
    index += 1

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
A = tuple(map(int, input().split()))
a = []
for i in range(N):
  a.append(i + 1)
PA = list(itertools.permutations(a))
Pi = -1
Ai = -1
for i, pa in enumerate(PA):
  if Pi != -1 and Ai != -1:
    print(abs(Pi - Ai))
    break
  if pa == P:
    Pi = i
  if pa == A:
    Ai = i