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: Accepted

Solution code:
import sys
n=int(input())
l=[i for i in range(1,n+1)]
a=tuple(map(int,sys.stdin.readline().strip().split()))
b=tuple(map(int,sys.stdin.readline().strip().split()))
import itertools
List=[]
for i in itertools.permutations(l):
  List+=[i]
print(abs(List.index(a)-List.index(b)))

Submission status: Accepted

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

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

jyunretulist=[]
for i in p:
    jyunretulist.append(list(i))
    
p=input().split()
q=input().split()

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

Submission status: Accepted

Solution code:
import itertools

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

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

Submission status: Accepted

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

Submission status: Accepted

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


for i,j in enumerate(permutations(range(1,n+1), n)):
    if j==p:
        a = i+1
    if j==q:
        b = i+1

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
p = tuple(int(i) - 1 for i in input().split())
q = tuple(int(i) - 1 for i in input().split())
per = list(permutations(range(n)))
print(abs(per.index(p) - per.index(q)))

Submission status: Accepted

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

l = range(1, n+1)
count = 0
for elem in itertools.permutations(l, n):
    count += 1
    if elem == p:
        p_count = count
    if elem == q:
        q_count = count

print(abs(p_count-q_count))

Submission status: Accepted

Solution code:
import math
from functools import reduce
import itertools
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))





N=input1()
P=input_array()
Q=input_array()

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


a=nums.index(tuple(P))
b=nums.index(tuple(Q))
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

x = list(itertools.permutations(R,N))
print(abs(x.index(P)-x.index(Q)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = permutations(list(range(1, n+1)))
    if p == q:
        print(0)
    else:
        for idx, item in enumerate(l):
            if item == p:
                idp = idx
            elif item == q:
                idq = idx
        print(abs(idp - idq))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
pl = list(map(int,input().split()))
ql = list(map(int,input().split()))
li = [i for i in range(1,n+1)]
it = list(itertools.permutations(li, n))
a = 0
b = 0
for i,t in enumerate(it):
    #print(i,list(t))
    if list(t) == pl:
        a = i
    if list(t) == ql:
        b = i
print(abs(a-b))

Submission status: Accepted

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

import itertools

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

cnt = 0
t = []
for i in itertools.permutations(a, n):
    cnt += 1
    if list(i) == p:
        t.append(cnt)
    if list(i) == q:
        t.append(cnt)
print(abs(t[0] - t[1]))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
P = LIST()
Q = LIST()
 
def hoge(A, m):
  c = -1
  for j in range(m):
    if A[j] == 1  :
      c += 1   
  return c
 
def Numth(R, N):
  ORDER = [1]*N
  c = 0
  for i in range(N-1):
    c += hoge(ORDER, R[i]) *factorial(N-i-1)
    ORDER[R[i]-1] = 0 
  return c+1
 
print(abs(Numth(P, N)-Numth(Q, N)))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
P = LIST()
Q = LIST()
 
def Numth(R, N):
  ORDER = [1]*N
  c = 0
  for i in range(N-1):
    c += (ORDER[:R[i]].count(1) - 1) *factorial(N-i-1)
    ORDER[R[i]-1] = 0 
  return c+1

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
def fac(N):
	if N==0:
		return 1
	else:
		return(fac(N-1)*N)
def count(a):
	co=0
	for i in range(len(a)):
		b=sorted(a)
		for j in range(len(a)):
			if b[j]==a[0]:
				p=j
		co+=fac(len(a)-1)*p
		a=a[1:]
	return co
print(abs(count(p)-count(q)))

Submission status: Accepted

Solution code:
import math
n=int(input())
p=[int(c) for c in input().split()]
q=[int(c) for c in input().split()]

l=0
num=list(range(1,n+1))
for i in range(n):
  l+=(num.index(p[i]))*(math.factorial(n-i-1))
  num.remove(p[i])
  
a=l+1

num=list(range(1,n+1))
r=0
for i in range(n):
  r+=(num.index(q[i]))*(math.factorial(n-i-1))
  num.remove(q[i])
  
b=r+1

print(str(abs(a-b)))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
P = LIST()
Q = LIST()

def hoge(A, m):
  c = -1
  for j in range(m):
    if A[j] == 1  :
      c += 1   
  return c

def Numth(R, N):
  ORDER = [1]*N
  c = 0
  for i in range(N-1):
    order = sum(ORDER[:i+1])
    c += hoge(ORDER, R[i]) *factorial(N-i-1)
    ORDER[R[i]-1] = 0 
  return c+1

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

Submission status: Accepted

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(abs(ans1-ans2))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
L = [i+1 for i in range(N)]
orders =list(itertools.permutations(L))
for i, order in enumerate(orders):
    if order == P:
        a = i
    if order == Q:
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
import math

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

num = list(range(1,n+1))
p_id = 0
q_id = 0

for i in range(n):
    p_id += num.index(p[i]) * math.factorial(len(num)-1)
    num.remove(p[i])

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

for i in range(n):
    q_id += num.index(q[i]) * math.factorial(len(num)-1)
    num.remove(q[i])

print(abs(p_id-q_id))

Submission status: Accepted

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

ans_p = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(p):
    index = defa.index(x)
    ans_p += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
ans_q = 0
defa = [_ for _ in range(1,n+1)]
for i,x in enumerate(q):
    index = defa.index(x)
    ans_q += math.factorial(n-i-1)*len(defa[:index])
    defa.remove(x)
    
print(abs(ans_p-ans_q))

Submission status: Accepted

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

l = list(itertools.permutations(range(1, n+1)))
dict = {}

index = 1
for i in l:
    dict[i] = index
    index += 1

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

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = [int(i)-1 for i in input().split()]
q = [int(i)-1 for i in input().split()]
m = sorted(p)
a = 0
b = 0
cnt = 1
#print(p,q)
for i in itertools.permutations(m, n):
  #print(list(i))
  if list(i) == p:
    a += cnt
  if list(i) == q:
    b += cnt
  if a!=0 and b!=0:
    break
  else:
    cnt += 1
    
print(abs(a-b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

import itertools

def solve(N: int, P: "List[int]", Q: "List[int]"):
    num = [i for i in range(1,N+1)]
    c=1
    a=b=0
    for p in itertools.permutations(num):
        if list(p)==P:
            a=c
        if list(p)==Q:
            b=c
        c+=1

    print(abs(a-b))

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#ABC150-A
if False:
    K,X=map(int,input().split())
    print('Yes' if 500*K>=X else 'No')

#ABC150-B
if False:
    N=int(input())
    S=input()
    print(S.count('ABC'))

#ABC150-C
if True:
    import itertools
    N=int(input())
    dic=list(itertools.permutations(list(range(1,N+1))))
    # print(dic)
    P=tuple(int(i) for i in input().split())
    Q=tuple(int(i) for i in input().split())

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

Submission status: Accepted

Solution code:
import itertools as it

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

a = 0
b = 0
n = it.permutations(range(1, N + 1), N)
for i, e in enumerate(n):
    if P == e:
        a = i
    if Q == e:
        b = i

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

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

for p in itertools.permutations(lis):
  for j in range(N):
    if p[j] != P[j]:
      break
    j += 1
  if j == N:
    pp = i
  for j in range(N):
    if p[j] != Q[j]:
      break
    j += 1
  if j == N:
    pq = i
  i += 1

print(abs(pp-pq))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = ' '.join(list(input().split()))
Q = ' '.join(list(input().split()))
perm = [1 + i for i in range(N)]
for i, v in enumerate(itertools.permutations(perm)):
    v = ' '.join(list(map(str, v)))
    if v == P:
        p = i
    if v == Q:
        q = i
print(abs(p - q))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
l = [(i+1) for i in range(n)]
tupleperm = tuple(itertools.permutations(l))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
print(abs(tupleperm.index(p)-tupleperm.index(q)))

Submission status: Accepted

Solution code:
import itertools
import sys
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
if p == q:
    print('0')
    sys.exit(0)
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: Accepted

Solution code:

import itertools
def myAnswer(N:int,P:list,Q:list) -> int:
   l = [i for i in range(1,N+1)]
   a = 0
   b = 0
   for n,i in enumerate(itertools.permutations(l,N)):
      if(i == tuple(P)):
         a = n
      if(i == tuple(Q)):
         b = n
   return abs(a - b)

   

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   P = list(map(int,input().split()))
   Q = list(map(int,input().split()))
   print(myAnswer(N,P,Q))

if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

a = -1
b = -1
base = list(range(1, N + 1))
for i, l in enumerate(permutations(base, N)):
    s = ' '.join([str(x) for x in l])
    if (P == s):
        a = i
    if (Q == s):
        b = i
print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations as pmt

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

a,b = None,None
i = 0
for od in pmt([i+1 for i in range(N)]):
    order = list(od)
    if P == order:
        a = i
    if Q == order:
        b = i
    i += 1
print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    P=tuple(map(int,input().split()))
    Q=tuple(map(int,input().split()))
    p_list=list(permutations(range(1,n+1)))
    print(abs(p_list.index(Q)-p_list.index(P)))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    P=tuple([int(_) for _ in input().split()])
    Q=tuple([int(_) for _ in input().split()])
    p_list=list(permutations(list(range(1,n+1))))
    print(abs(p_list.index(Q)-p_list.index(P)))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
per = list(permutations([i+1 for i in range(n)]))
ans = abs(per.index(p)-per.index(q))
print(ans)

Submission status: Accepted

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

p = np.arange(N) + 1

def dict_num(arr, N):
    n = 1
    num_used = []
    for i in range(N):
        k = 0
        for a in range(1, arr[i]):
            if a in num_used:
                k += 1
        n += (arr[i] - 1 - k) * math.factorial(N - i - 1)
        num_used.append(arr[i])
    return n

p_n = dict_num(P, N)
q_n = dict_num(Q, N)
print(abs(p_n - q_n))

Submission status: Accepted

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

p#rint((ans))
num_p = 0
num_q = 0
for i in range(len(ans)):
    #print(list(ans[i]))
    if list(ans[i]) == p: num_p = i
    if list(ans[i]) == q: num_q = i
print(abs(num_p-num_q))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
ps = tuple(map(int, input().split()))
qs = tuple(map(int, input().split()))

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

pl = [v for v in itertools.permutations(l)]

a = pl.index(ps)
b = pl.index(qs)

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

N = [ i+1 for i in range(int(input()))]
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
V = []

for v in itertools.permutations(N, len(N)):
    V.append(v)
P_ind = V.index(P)
Q_ind = V.index(Q)

print(abs(P_ind - Q_ind))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
ps = tuple(map(int, input().split()))
qs = tuple(map(int, input().split()))

perm = list(sorted(itertools.permutations(range(1, n + 1))))
for i in range(len(perm)):
    p = perm[i]
    if p == ps:
        a = i
    if p == qs:
        b = i

print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

p = tuple(p)
q = tuple(q)

A = sorted(list(permutations(p,N)))

a = A.index(p)
b = A.index(q)

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
# itertools順列の中身はタプル
p = tuple(list(map(int, input().split())))
q = tuple(list(map(int, input().split())))
# listにする
lista = list(itertools.permutations(range(1, n + 1)))
print(abs(lista.index(p) - lista.index(q)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(str, input().split()))
q = tuple(map(str, input().split()))
pc = 0
qc = 0
ans_l = list(itertools.permutations(p, n))
ans_l = sorted(ans_l)
for i in ans_l:
    pc += 1
    if i == p:
        break
for i in ans_l:
    qc += 1
    if i == q:
        break
print(abs(pc - qc))

Submission status: Accepted

Solution code:
def kaijo(n):
    if n == 1 or n == 0:
        return 1
    else:
        return n * kaijo(n-1)

def counter(P):
    c = 0
    i = 0
    while i < len(P):
        c += (P[0]-1) * kaijo(len(P)-1)
        q = 1
        while q < len(P):
            if P[q] > P[0]:
                P[q] -= 1
            q += 1
        P = P[1:]
    return c

n = int(input())
P = list(map(int,input().split(' ')))
Q = list(map(int,input().split(' ')))
print(abs(counter(P)-counter(Q)))

Submission status: Accepted

Solution code:
import math
import itertools

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

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

a = 0
b = 0

for i in range(math.factorial(n)):
    if iter[i] == p:
        a = i
    if iter[i] == q:
        b = i

print(abs(a-b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
l = list(itertools.permutations([i for i in range(1, N + 1)]))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
P_c = 0
Q_c = 0
for i in range(len(l)):
    if l[i] == P:
        P_c = i
    if l[i] == Q:
        Q_c = i
print(abs(P_c - Q_c))

Submission status: Accepted

Solution code:
import math
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
a,b = 0,0
seq = [i for i in range(1,n+1)]
k = list(itertools.permutations(seq))
for i in range(math.factorial(n)):
    if k[i] == p:
        a = i
    if k[i] == q:
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
per = []
for v in itertools.permutations(P):
    per.append(''.join(str(n) for n in v))
per.sort()
# print(per)
a = per.index(''.join(str(n) for n in P))
b = per.index(''.join(str(n) for n in Q))
ans = abs(a - b)
print(ans)

Submission status: Accepted

Solution code:
import itertools
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)

nlist=[]
for i in itertools.permutations(l,r=n):
        nlist.append(list(i))

#po=0
#qo=0

for i in range(len(nlist)):
    if nlist[i]==p:
        po=i
    if nlist[i]==q:
        qo=i

print(abs(qo-po))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
ls = [i+1 for i in range(N)]
per = []
for j in itertools.permutations(ls,N):
    per.append(j)
print(abs(per.index(P) - per.index(Q)))

Submission status: Accepted

Solution code:
import itertools 
n = int(input())
s = list(map(int,input().split()))
q = list(map(int,input().split()))
z = 0
l = [i for i in  range(1,n+1)]
l1 = itertools.permutations(l)
for i in l1:
  z += 1
  if list(i)==s:
    a =z
  if list(i)==q:
    c = z
print(abs(c-a))

Submission status: Accepted

Solution code:
import math

def rank_checker(P):
  rank = 1
  N = len(P)
  L = list(range(1, N + 1))
  for i in range(N):
    weight = L.index(P[i])
    penalty = math.factorial(len(L) - 1)
    rank += weight * penalty
    L.pop(weight)
  return rank


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

rankP = rank_checker(P)
rankQ = rank_checker(Q)
print(abs(rankP - rankQ))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
from fractions import gcd
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
a = 0
b = 0
k = 0

for i in itertools.permutations(range(1,n+1)):
    k += 1
    if i == p:
        a = k
    if i == q:
        b = k
ans = abs(a-b)
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations
import bisect

N = int(input())
P = input().replace(' ', '')
Q = input().replace(' ', '')

t = [''.join(list(map(str, i))) for i in permutations(range(1, N + 1))]
a = bisect.bisect_left(t, P)
b = bisect.bisect_left(t, Q)

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import itertools

n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
ans = list(itertools.permutations([(i + 1) for i in range(n)]))
print(abs(ans.index(p) - ans.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations 
n = int(input())
p = list(map(int, input().split())) 
q = list(map(int, input().split()))
 
i = 0
for x in permutations(range(1, n+1)):
    i += 1
    if list(x) == p:
        a = i
    if list(x) == q:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
import math
import itertools

N=int(input())
N_fac=math.factorial(N)
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
p=0
q=0

S=[_ for _ in range(1,N+1)]
A=[]
for i in itertools.permutations(S,len(S)):
    A.append(list(i))

for j in range(N_fac):
    if P==A[j]:
        p=j
    if Q==A[j]:
        q=j

print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
num1 = list(map(int, input().split()))
num2 = list(map(int, input().split()))

i = 0
for x in itertools.permutations(range(1,n+1)):
  i += 1
  if list(x)==num1:
    a = i
  if list(x)==num2:
    b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
from collections import deque 
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
p_que = deque()
q_que = deque()
for i in range(n):
    p_que.appendleft(p[i])
    q_que.appendleft(q[i])

def culc_num(n, que):
    li = []
    count = 0
    for i in range(n):
        li.append(i+1)
    for i in range(n):
        num = que.pop()
        index= li.index(num)
        dif = index
        for i in range(1,len(li)):
            dif*=i
        count+=dif
        li.remove(num)
    return count

print(abs(culc_num(n, q_que) - culc_num(n, p_que)))

Submission status: Accepted

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


def permutation(l: list, n: int) -> list:
    return [p for p in permutations(l, n)]

def main():
    comb = permutation([i for i in range(1, N+1)], N)
    a = comb.index(P)
    b = comb.index(Q)
    print(abs(a - b))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

i = 0

for x in permutations(range(1, n + 1)):
    i += 1
    if list(x) == p:
        a = i
    if list(x) == q:
        b = i

print(abs(a - b))