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:
import itertools
import math
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))

l=[ i+1 for i in range(N) ]
Per=itertools.permutations(l,N)
Per=list(Per)
num=[-1,-1]
for i in range(math.factorial(N)):
  if Per[i]==P:
    num[0]=i
  elif Per[i]==Q:
    num[1]=i
print(abs(num[0]-num[1]))

Submission status: Wrong Answer

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

a = 0
b = 0
for i, x in  enumerate(itertools.permutations(range(1, N + 1))):
    print(i, x)
    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 = list(map(int, input().split()))
Q = list(map(int, input().split()))
num_p = 0
num_q = 0
num = [0]*N

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

seq = list(itertools.permutations(num))

for j in range(len(seq)):
  if list(seq[j]) == P:
    num_p = j+1
  elif list(seq[j]) == Q:
    num_q = j+1
    
print(abs(num_p - num_q))

Submission status: Wrong Answer

Solution code:
import math

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

def turn(arr, m):
    sum_ = 0
    for i in range(m):
        res =  math.factorial(m - (i + 1)) * (arr[i] - (i + 1))
        if res > 0:
            sum_ =+ res
    
    return sum_

ans = abs(turn(p, n) - turn(q, n))
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))


elems = range(1, n+1)
res = {}
c = [0]
def perms(i, elems,res, c):

	if i == len(elems):
		res[tuple(elems)] = c[0]
		c[0]+=1
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res, c)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, elems, res, c)

print abs((res[tuple(p)] - res[tuple(q)]))

Submission status: Wrong Answer

Solution code:
from math import factorial
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P_0 = list(range(0, N + 1))
Q_0 = list(range(0, N + 1))
num_P = 0
num_Q = 0
for i in range(N):
  for j in range(N):
    if P[i] == P_0[j]:
      num_P += j * factorial(N - 1 - i)
      P_0.remove(P_0[j])
      break
for i in range(N):
  for j in range(N):
    if Q[i] == Q_0[j]:
      num_Q += j * factorial(N - 1 - i)
      Q_0.remove(Q_0[j])
      break
print(abs(num_Q - num_P))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
x = [i+1 for i in range(n)]
ans = 0
c = 1
for j in itertools.permutations(x,n):
    y = list(j)
    if p == y:
       ans += c
    elif q == y:
        ans -= c
    
    c += 1

print(abs(ans))

Submission status: Wrong Answer

Solution code:
N=int(input())
P=tuple(map(str,input().split()))
Q=tuple(map(str,input().split()))
count=0
a,b=0,0
import itertools
for i in itertools.permutations(sorted(P),len(P)):
    count+=1
    if i==P:
        a=count
    elif i==Q:
        b=count
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
n = int(input())

def dfs(nn):
  if n == nn:
    print("".join(ans))
    return 
  
  for i in ["a", "b", "c"]:
    ans[nn] = i
    dfs(nn+1)

ans = ["0" for i in range(n)]
dfs(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
a=list(range(1,n+1))
import itertools
a_list=list(itertools.permutations(a))
import math
p_=0
q_=0
for i in range(math.factorial(n)):
  if p==a_list[i]:
    p_=i
  elif q==a_list[i]:
    q_=i

print(abs(p_-q_))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=map(int,input().split())
import copy
import itertools
c = itertools.permutations(a, n)
#c=copy.copy(a).sort()
b=map(int,input().split())
print(a)
print(b)
print(c)

Submission status: Wrong Answer

Solution code:
import itertools

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

P_id = 0
Q_id = 0

for i,l in enumerate(m):
  if l == tuple(P):
    P_id = i
  elif l == tuple(Q):
    Q_id = i

    
print(abs(Q_id - P_id))

Submission status: Wrong Answer

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

lst = itertools.permutations(range(1,N+1))

for i, L in enumerate(lst):
    print(i,L)
    if L == tuple(P):
        a = i + 1
    if L == tuple(Q):
        b = i + 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()))

a,b = 0,0
flag = 0

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

for i in range(len(ix)):
    t = list(ix[i])
    if (p == t):
        a = i
        flag += 1
    elif (q == t):
        b = i
        flag += 1
    if (flag == 2):
        break
print(abs(a-b))

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

order = [list(i) for i in permutations(range(1, n + 1), n)]
p_, q_ = 0, 0
for i, o in enumerate(order):
    if p == o:
        p_ = i + 1
    elif q == o:
        q_ = i + 1
print(abs(p_ - 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()))

order = [list(map(int,i)) for i in permutations(''.join(map(str, range(1, n + 1))), n)]
p_, q_ = 0, 0
for i, o in enumerate(order):
    if p == o:
        p_ = i
    elif q == o:
        q_ = i
print(abs(p_ - q_))

Submission status: Wrong Answer

Solution code:
N = int(input())
P = list(map(int, input().split()))
Q=list(map(int, input().split()))
l = [i for i in range(N)]

fac = [0] * 8
fac[1] = 1
for i in range(2, N):
    fac[i] = i * fac[i - 1]
print(fac)
a = 0
l = list(range(1, N + 1))
for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    print(l.index(P[i]), fac[len(l) - 1])
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[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
cmb=list(permutations(range(1,n+1),n))
# print(cmb[0])

a=0
b=0

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

# print(a,b)
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
Ps = list(map(int,input().split()))
Qs = list(map(int,input().split()))
a,b,cnt,flag = 0,0,0,0
for p in itertools.permutations(list(range(1,N+1))):
#     print(p)
    if list(p) == Ps:
        a = cnt
        flag += 1
    elif list(p) == Qs:
        b = cnt
        flag += 1
    if flag == 2:
        break
    cnt += 1
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools


N = int(input())

P = "".join(input().split())
Q = "".join(input().split())

print(P)

p_index = -1
q_index = -1
permutation_list = list(itertools.permutations([x + 1 for x in range(N)]))
# print(permutation_list)
for i in range(len(permutation_list)):
    # print(permutation_list[i])
    param = "".join(map(str, permutation_list[i]))
    if P == param:
        p_index = i
    if Q == param:
        q_index = i
    if p_index != -1 and q_index != -1:
        result = p_index - q_index
        if result < 0:
            result = -result
        print(result)
        exit(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p_mojiretu = ""
q_mojiretu = ""

for x in p:
    p_mojiretu += x
for x in q:
    q_mojiretu += x

print(p_mojiretu)
print(q_mojiretu)

cnt = 1
index_a = 0
index_b = 0

for i in itertools.permutations(range(1, n+1)):
    s = "".join(str(x) for x in i)
    print(s)
    if s == p_mojiretu:
        index_a = cnt
    if s == q_mojiretu:
        index_b = cnt
    cnt += 1
print(abs(index_a - index_b))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
p=list(input().split())
q=list(input().split())
h=[]
p1=[]
q1=[]
for i in range(n):
    h+=str(i+1)
    p1+=p[i]
    q1+=q[i]
k=list(itertools.permutations(h))
p2=-1
q2=-1
for i in range(len(k)):
    if p1==k[i]:
        p2=i
    if q1==k[i]:
        q2=i
    if p1 != -1 and q1 !=-1:
        break
print(abs(q2-p2))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
pp=input().split()
qq=input().split()
p=[]
q=[]
p.append(pp)
q.append(qq)
a=[]
for v in itertools.permutations(pp,n):
    a.append(v)
a=sorted(a)
for j in range(len(a)):
  a[j]=list(a[j])

b=0
c=0
for i in range(len(a)):
  if a[i]==pp:
    b+=i
    
for k in range(len(a)):
  if a[k]==qq:
    c+=i

print(max(b-c,c-b))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
pp=input().split()
qq=input().split()
p=[]
q=[]
p.append(pp)
q.append(qq)
a=[]
for v in itertools.permutations(pp,n):
    a.append(v)
a=sorted(a)
for j in range(len(a)):
  a[j]=list(a[j])

b=0
c=0
for i in range(len(a)):
  if a[i]==pp:
    b+=i
  elif a[i]==qq:
    c+=i
print(max(b-c,c-b))

Submission status: Wrong Answer

Solution code:
import numpy as np
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
A=[]
B=[]
import math
for i in range(1,N+1):
  A.append(i)
  B.append(i)
a=1
b=1
for i in range(1,N+1):
  if P[i-1]<len(A):
    a+=math.factorial(N-i)*(P[i-1]-1)
  else:
    a+=math.factorial(N-i)*(len(A)-1)
  A.remove(P[i-1])
for i in range(1,N+1):
  if Q[i-1]<len(B):
    b+=math.factorial(N-i)*(Q[i-1]-1)
  else:
    b+=math.factorial(N-i)*(len(B)-1)
  B.remove(Q[i-1])
print(abs(a-b))

Submission status: Wrong Answer

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N = readint()
    P = tuple(readnums())
    Q = tuple(readnums())

    a = b = 0
    for i, v in enumerate(permutations(range(1, N + 1))):
        if v == P:
            a = i + 1
        elif v == Q:
            b = i + 1

    print(abs(a - b))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N = readint()
    P = tuple(readnums())
    Q = tuple(readnums())

    a = b = 0
    for i, v in enumerate(permutations(range(1, N + 1))):
        if v == P:
            a = i
        elif v == Q:
            b = i

    print(abs(a - b))


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

index = []

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

print(len(index))

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

Submission status: Wrong Answer

Solution code:
import itertools

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

index = []

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

if not index:
    print(abs(index[0]-index[1]))
else:
    print('0')

Submission status: Wrong Answer

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

import math
import itertools
count = 0
a,b = 0,0
l = list(range(1,N+1))
for v in itertools.permutations(l):
  count += 1
  if v == P:
    a = count
  elif v == Q:
    b = count

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
def f(x):
    return x-1
import itertools
import math
N = int(input())
P = list(map(int, input().split()))
P = list(map(f, P))
Q = list(map(int, input().split()))
Q = list(map(f, Q))
S = list(range(N))
permutation = list(itertools.permutations(S))
a,b = 0,0
for i in range(len(permutation)):
    if P == list(permutation[i]):
        a = i
    elif Q == list(permutation[i]):
        b = i
print(int(math.fabs(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()))
nums = list(range(1,n+1))
d= [ list(i) for i in itertools.permutations(nums,n)]
a=0
b=0
for i in range(len(d)):
  if p==d[i]:
    a=i
  elif q==d[i]:
    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()))
nums = list(range(1,n+1))
d= [ list(i) for i in itertools.permutations(nums)]
a=0
b=0
for i in range(len(d)):
  if p==d[i]:
    a=i
  elif q==d[i]:
    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()))
print(p)
data = [0] * n
for i in range(n):
    data[i] = i + 1

target = list(itertools.permutations(data))
'''
pIndex = target.index(p)
qIndex = target.index(q)
二重配列のインデックスが取れない
'''
pFlag, qFlag = False, False

for i, val in enumerate(target):
    if (pFlag is True) and (qFlag is True):
        break

    aaa = list(val)
    # print(aaa)
    # list & list の＝ができない
    if (aaa == p):
        pp = i + 1
        pFlag = True
    if (aaa == q):
        qq = i + 1
        qFlag = True

ans = abs(pp - qq)
print(ans)

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()))
pi = 0
qi = 0
cnt = 0
for p in permutations(range(1,1+N)):
    cnt += 1
    #print(p)
    if p == P:
        pi = cnt
    elif p == Q:
        qi = cnt
        
print(abs(qi-pi))

Submission status: Wrong Answer

Solution code:
import itertools

def main():
    n = int(input())
    nums = []
    for i in range(2):
        nums.append(list(map(str, input().split())))
    kumi = list(itertools.permutations([str(i) for i in range(1, n+1)], n))
    f, t = 0, 0
    for i, s in enumerate(kumi):
        if ''.join(nums[0]) == ''.join(s):
            f = i
        elif ''.join(nums[1]) == ''.join(s):
            t = i
    print(abs(f - t))

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

permutation_list = []

# nまでの順列要素のリスト作成
for i in range(1, n+1):
    permutation_list.append(int(i))

index_p = 0
index_q = 0

# 順列のペアを全て含んだリストの作成
# ただし各ペアはタプルであることに注意
permutations_pair = list(itertools.permutations(permutation_list))

for index, pair in enumerate(permutations_pair):
    # タプルのペアをリストに変換
    pair = list(pair)
    if pair == p:
        index_p = index
    elif pair == q:
        index_q = index

print(abs(index_p - 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()))
C = [i+1 for i in range(N)]
a = 0
b = 0
p = list(itertools.permutations(C))
for i in range(len(p)):
  if p[i] == P:
    a = i
  elif p[i] == Q:
    b = i
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
n = int(input())
p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
dum_list = [i for i in range(1,n+1)]
count = 0
ans = 0
for i in permutations(dum_list,n):
    count += 1
    if p_list == list(i):
        ans += count
    elif q_list == list(i):
        ans -= count


print(abs(ans))

Submission status: Wrong Answer

Solution code:
import math

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

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

for i in range(N-1):
    if math.factorial(N-1-i) == 1:
        if P[i]<P[i+1]:
            countP += 1
        else:
            countP += 2
    else:
        countP += (P[i]-1) * math.factorial(N-1-i)
for i in range(N-1):
    if math.factorial(N-1-i) == 1:
        if Q[i]<Q[i+1]:
            countQ += 1
        else:
            countQ += 2
    else:
        countQ += (Q[i]-1) * math.factorial(N-1-i)


if countP > countQ:
    print(countP-countQ)
else:
    print(countQ-countP)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())

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

a = [i+1 for i in range(n)]
cnt = 0
ans = 0
for x in itertools.permutations(a):
    cnt += 1
    if list(x) == p: ans -= cnt
    if list(x) == q:
        ans += cnt
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def is_prime(n):
    if n<2: return False
    for p in range(2, n):
        if n%p==0:
            return False
    return True
x=int(input())
while not is_prime(x):
    x+=1
print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())
P = ''.join([str(v) for v in input().split()])
Q = ''.join([str(v) for v in input().split()])

items = [str(v) for v in range(1,N+1)]
used = [False for _ in range(N+1)]
junretu = set()

def dfs(now):
    if len(now) == N:
        junretu.add(now)
        return
    for i,v in enumerate(items):
        if used[int(v)]:
            continue
        
        used[int(v)] = True
        dfs(now+v)
        used[int(v)] = False
    return

dfs('')
sorted_junretu = sorted(list(junretu))

p_d,q_d = 0,0

for i,s in enumerate(sorted_junretu):
    if s == P:
        p_d = i
    elif s == Q:
        q_d = i
print(abs(p_d-q_d))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = [int(x) for x in input().split()]
P = tuple(P)
Q = [int(x) for x in input().split()]
Q = tuple(Q)
N_ = list(range(1, N+1))

R = list(itertools.permutations(N_))

Pflg = Qflg = False
Pcnt = Qcnt = -1
for i, r in enumerate(R):
    print(r)
    if P==r:
        Pflg = True
        Pcnt = i
    if Q==r:
        Qflg = True
        Qcnt = i
    if Pflg and Qflg:
        break

print(abs(Pcnt - Qcnt))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = [int(x) for x in input().split()]
P = tuple(P)
Q = [int(x) for x in input().split()]
Q = tuple(Q)
N_ = list(range(1, N+1))

R = list(itertools.permutations(N_))

Pflg = Qflg = False
Pcnt = Qcnt = -1
for i, r in enumerate(R):
    if P==r:
        Pflg = True
        Pcnt = i+1
    if Q==r:
        Qflg = True
        Qcnt = i+1
    if Pflg and Qcnt:
        break

print(abs(Pcnt - Qcnt))

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()))
l=[]
for i in range(n):
    l.append(i+1)
dic= sorted(list(permutations(l,n)))
r=len(dic)
dicp=0
dicq=0
for x in range(r):
    dic[x]=list(dic[x])
    if dic[x]==p:
        dicp=x+1
    elif dic[x]==q:
        dicq=x+1
print(max(dicp-dicq,dicq-dicp))

Submission status: Wrong Answer

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

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

P = it.permutations([i for i in range(1,n+1)],n)

a = 0
b = 0
cnt = 1
for i in P:
  if p==i:
    a = cnt
    cnt +=1
  elif q==i :
    b = cnt
    cnt +=1
  else:
    cnt +=1
ans = abs(a-b)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import itertools

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

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

junretu = list(itertools.permutations(n_list))
sorted(junretu)
idx_p = 0
idx_q = 0
for i in range(math.factorial(n)):
    if list(junretu[i]) == p:
        idx_p = i
        continue
    elif list(junretu[i]) == q:
        idx_q = i
        continue
    elif idx_p != 0 and idx_q != 0:
        break

print(abs(idx_q - idx_p))

Submission status: Wrong Answer

Solution code:
import math
import itertools

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

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

junretu = list(itertools.permutations(n_list))
sorted(junretu)
p = tuple(p)
q = tuple(q)
idx_p = 0
idx_q = 0
for i in range(math.factorial(n)):
    if junretu[i] == p:
        idx_p = i
        continue
    elif junretu[i] == q:
        idx_q = i
        continue
    elif idx_p != 0 and idx_q != 0:
        break

print(abs(idx_q - idx_p))

Submission status: Wrong Answer

Solution code:
import math
import itertools

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

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

junretu = list(itertools.permutations(n_list))
p = tuple(p)
q = tuple(q)
idx_p = 0
idx_q = 0
for i in range(math.factorial(n)):
    if junretu[i] == p:
        idx_p = i
        continue
    elif junretu[i] == q:
        idx_q = i
        continue
    elif idx_p != 0 and idx_q != 0:
        break

print(abs(idx_q - idx_p))

Submission status: Wrong Answer

Solution code:
import itertools

num = int(input())
c = [i + 1 for i in range(num)]

a = tuple([int(i) for i in input().split(" ")])
b = tuple([int(i) for i in input().split(" ")])

a_num = 0
b_num = 0
a_flag = False
b_flag = False

for num,i in enumerate(itertools.permutations(c)):
    if a == i:
        a_num = num
        a_flag = True
    elif b == i:
        b_num = num
        b_flag = True
    if a_flag and b_flag:
        break

print(abs(a_num - b_num))

Submission status: Wrong Answer

Solution code:
import itertools

num = int(input())
c = [i + 1 for i in range(num)]

a = tuple([int(i) for i in input().split(" ")])
b = tuple([int(i) for i in input().split(" ")])

a_num = 0
b_num = 0

for num,i in enumerate(itertools.permutations(c)):
    if a == i:
        a_num = num
    elif b == i:
        b_num = num

print(abs(a_num - b_num))

Submission status: Wrong Answer

Solution code:
from math import factorial

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

# P = ''.join(P)
# Q = ''.join(Q)

f_N_1 = factorial(N - 1)

def foo(digits, numbers, nth):
    if digits == 0: return 0
    return factorial(digits - 1) * (int(numbers[nth]) - 1) + foo(digits - 1, numbers, nth + 1)

a = foo(N, P, 0)
b = foo(N, Q, 0)

answer = abs(a - b)
# print(a, b)
print(answer)

Submission status: Wrong Answer

Solution code:
# うんちーこんぐ

import itertools

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

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

ans1 = 1
ans2 = 1

for i in b:
    if i == P:
        break
    ans1 += 1

b = itertools.permutations(a)

for i in b:
    if i == Q:
        break
    ans2 += 1

print(ans1, ans2)

print(abs(ans1-ans2))

Submission status: Wrong Answer

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

p = sum([(P[i]-1)**(N-i-1) for i in range(N)])
q = sum([(Q[i]-1)**(N-i-1) for i in range(N)])

print(abs(p-q))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
num_p = num_q = 0
counter = 0
# permutations(list a) a!を表す
for pairs in itertools.permutations((range(1, n + 1))):
    lp = list(pairs)
    counter += 1
    if p == lp:
        num_p = counter
    elif q == lp:
        num_q = counter
print(abs(num_p - num_q))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = "".join(list(map(str,input().split())))
q = "".join(list(map(str,input().split())))
a=0
b=0
for i in range(n):
  a += 1+(n-(i+1))*(int(s[i])-1)
for i in range(n):
  b += 1+(n-(i+1))*(int(q[i])-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()))

L = [i for i in range(1, N+1)]
a = 0
b = 0

#順列作成
L_list = list(itertools.permutations(L))

for i in range(len(L_list)):
  if list(L_list[i]) == P:
    a = i+1
  elif list(L_list[i]) == Q:
    b = i+1
  else:
    continue

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

l=range(1,n+1)

a=0

b=0

cnt=0

for i in itertools.permutations(l,n):
  i=list(i)
  if i==p:
    a=cnt
  elif i==q:
    b=cnt

  cnt+=1

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools


def main():
    N = [i for i in range(1, int(input()) + 1)]
    P = tuple(map(int, input().split(' ')))
    Q = tuple(map(int, input().split(' ')))
    x, y = 0, 0
    for enum, n in enumerate(itertools.permutations(N)):
        if n == P:
            x = enum + 1
        if n == Q:
            y = enum + 1
        if x + y > x:
            break
    print(abs(x - y))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())

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

p = list(itertools.permutations(l, N))

print(p)

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

a = p.index(P)
b = p.index(Q)

print(abs(b-a))

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

count = 0
a, b = 0, 0
for index, i in enumerate(permutations(range(1, n+1))):
    if i in [P, Q]:
        if count == 0:
            count += 1
            a = index
        else:
            b = index
            break

print(abs(a - b))

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
pl = tuple([int(i) for i in input().split()])
ql = tuple([int(i) for i in input().split()])

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

print(all_list.index(pl) - all_list.index(ql))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
a=int(input())
b=list(map(int,input().split()))
c=list(map(int,input().split()))
c2=sorted(c)
b2=sorted(b)
d=permutations(b2,a)
k=0
b1=0
c1=0
for i in d:
    k+=1
    if list(i)==b:
        b1=k
    elif list(i)==c:
        c1=k
print(abs(c1-b1))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
a=int(input())
b=list(map(int,input().split()))
c=list(map(int,input().split()))
c2=sorted(c)
d=permutations(c2,a)
k=0
b1=0
c1=0
for i in d:
    k+=1
    if list(i)==b:
        b1=k
    elif list(i)==c:
        c1=k
print(abs(c1-b1))

Submission status: Wrong Answer

Solution code:
import math

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

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

ord1 = 0
for i in range(n-1):
#	print((p[i]-1) * math.factorial(n-1-i) )
	ord1 = ord1 + (p[i]-1) * math.factorial(n-1-i) 
  
ord2 = 0
for i in range(n-1):
	ord2 = ord2 + (q[i]-1) * math.factorial(n-1-i) 

print(abs(ord2 - ord1))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
numbers1k=[]
numbers2k=[]
s=[]
for i in range(n):
    s.append(i+1)
p=itertools.permutations(s,n)
u=list(p)
print(u)
numbers1=input().rstrip().split(" ")
for num1 in numbers1:
    numbers1k.append(int(num1))
numbers2=input().rstrip().split(" ")
for num2 in numbers2:
    numbers2k.append(int(num2))
com1=tuple(numbers1k)
com2=tuple(numbers2k)
for i in range(len(u)):
    if u[i]==com1:
        a=i+1
for j in range(len(u)):
    if u[j]==com2:
        b=j+1
#print(a)
#print(b)
print(abs(b-a))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
numbers1k=[]
numbers2k=[]
s=[]
for i in range(n):
    s.append(i+1)
p=itertools.permutations(s,n)
u=list(p)
print(u)
numbers1=input().rstrip().split(" ")
for num1 in numbers1:
    numbers1k.append(int(num1))
numbers2=input().rstrip().split(" ")
for num2 in numbers2:
    numbers2k.append(int(num2))
com1=tuple(numbers1k)
com2=tuple(numbers2k)
for i in range(len(u)):
    if u[i]==com1:
        a=i
for j in range(len(u)):
    if u[j]==com2:
        b=j
#print(a)
#print(b)
print(abs(b-a))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import math

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)
#print(A)

B = []
for i in permutations(A,N):
    B.append(list(i))
#print(B)

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

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

check = list(itertools.permutations(range(1,N+1)))
N_fac = math.factorial(N)

n1 = 0
n2 = 0
countP = 0
countQ = 0
for i in range(0,N_fac):
  for j in range(0,N):
    if(check[i][j] == P[j]):
      countP += 1
      if(countP == 3):
        n1 = i
    if(check[i][j] == Q[j]):
      countQ += 1
      if(countQ == 3):
        n2 = i
num = n1-n2
print(abs(num))