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

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

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

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

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
x=set([n for n in range(1,N+1)])
x=list(permutations(x,N))

a,b=0,0
for num in range(len(x)):
    if p==list(x[num]):
        a=num+1
    elif q==list(x[num]):
        b=num+1

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

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

Submission status: Wrong Answer

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

import itertools

p = 0
q = 0
n = list(itertools.permutations(range(1,N+1)))
for i in range(len(n)):
    if list(n[i]) == P:
        p = i
    elif list(n[i]) == Q:
        q = i

print(abs(p-q))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
y = [num for num in range(1, n+1)]
permutation_n = list(itertools.permutations(y, 3))
a=b=0
count=1
for permu in permutation_n:
  if permu == p:
    a = count
  if permu == q:
    b = count
  if a != 0 and b != 0 :
    break
  count+=1
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()))

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

a, b, cnt = 0, 0, 1
for p in permutations(li, N):
  if list(p) == P:
    a = cnt
  elif list(p) == Q:
    b = cnt
  cnt += 1
  
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()))
x=set([n for n in range(1,N+1)])
x=list(permutations(x,N))

a,b=0,0
for num in range(len(x)):
    if p==list(x[num]):
        a=num
    elif q==list(x[num]):
        b=num

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

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

print(P)
print(Q)

# Get all permutations of [1, 2, 3]
p = list(permutations([x for x in range(1, N+1)]))

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

if a > b:
    print(a-b)
else:
    print(b-a)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
y = [num for num in range(1, n+1)]
permutation_n = list(itertools.permutations(y, 3))

a=b=0
count=1
for permu in permutation_n:
  if permu == p:
    a = count
  if permu == q:
    b = count
  count+=1
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())

permu = [i+1 for i in range(N)]
s = 0
flag = False

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

# print(P)
for i, value in enumerate(itertools.permutations(permu)):
    
    if P == Q:
        break

    if value == P or value == Q:
        # print(i)
        if flag:
            s -= i
        else:
            s += i
            flag = True

Submission status: Wrong Answer

Solution code:
from itertools import permutations

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

print(P)
print(Q)

# Get all permutations of [1, 2, 3]
p = list(permutations([x for x in range(1, N+1)]))

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

if a > b:
    print(a-b)
else:
    print(b-a)

Submission status: Wrong Answer

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

import itertools

p = 0
q = 0
n = list(itertools.permutations(range(1,N+1)))
for i in range(len(n)):
    if list(n[i]) == P:
        p = i
    elif list(n[i]) == Q:
        q = i

print(abs(p-q))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())

permu = [i+1 for i in range(N)]
s = 0
flag = False

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

# print(P)
for i, value in enumerate(itertools.permutations(permu)):
    

    if value == P or value == Q:
        # print(i)
        if flag:
            s -= i
        else:
            s += i
            flag = True

print(abs(s))

Submission status: Wrong Answer

Solution code:
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
def find_power(n):
    # 0!からn!までのびっくりを出してくれる関数
    powlist=[0]*(n+1)
    powlist[0]=1
    powlist[1]=1
    for i in range(2,n+1):
        powlist[i]=powlist[i-1]*i%(10**9+7)
    return powlist
powl=find_power(10)
import math
a=1
b=1
for i in range(n):
    c=1
    d=1
    subp=p[i:]
    subq=q[i:]
    for j in subp:
        if j<subp[0]:
            c+=1
    for k in subq:
        if k<subq[0]:
            d+=1

    a=a+c*int(powl[n-1-i])
    b=b+d*int(powl[n-1-i])
print(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()))

cnt = 0
k, l = 0, 0
for i in itertools.permutations(list(range(1,n+1))):
    cnt += 1
    if i == p:
        k = cnt
    elif i == q:
        l = cnt

print(abs(k-l))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())

permu = [i+1 for i in range(N)]
s = 0
flag = False

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

print(P)
for i, value in enumerate(itertools.permutations(permu)):
    

    if value == P or value == Q:
        # print(i)
        if flag:
            s -= i
        else:
            s += i
            flag = True

print(abs(s))

Submission status: Wrong Answer

Solution code:
import itertools

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

r = list(itertools.permutations(range(1,n+1)))
a, b = 0, 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
import math
N = int(input())
P = tuple([int(i) for i in input().split()][0:N])
Q = tuple([int(i) for i in input().split()][0:N])
l = list(itertools.permutations(range(1,N+1)))
print(l.index(Q) - l.index(P))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N=int(input())
l=[i+1 for i in range(N)]
P=tuple(map(int, input().split()))
Q=tuple(map(int, input().split()))
counter=0
print(P)
for t in permutations(l):
  if P==t:
    a=counter
    
  if Q==t:
    b=counter
    
  counter+=1
  
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
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


import itertools
for x in itertools.permutations(PER):
    #print(*x)
    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_a == N:
            a = tmp
        elif flag_b == N:
            b = tmp

    #print(tmp)
    tmp += 1

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import permutations
n = int(stdin.readline().rstrip())
ps = tuple(map(int, stdin.readline().rstrip().split()))
qs = tuple(map(int, stdin.readline().rstrip().split()))
a = 0
b = 0
per = permutations([i for i in range(1, n + 1)])
for i, v in enumerate(per):
    if ps == v:
        a = i
    elif qs == v:
        b = i

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
li=[]
for i in range(n):
    li.append(i+1)
a=list(itertools.permutations(li))
tmp=[0,0]

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

Submission status: Wrong Answer

Solution code:
import itertools
num = int(input())
p = tuple(input().split())
q = tuple(input().split())
lister = [str(s) for s in range(1,num+1)]
comb = list(itertools.permutations(lister))
memory1 = 0
memory2 = 0
for t in range(len(comb)):
    if p == comb[t]:
        memory1 = t
    elif q == comb[t]:
        memory2 = t
print(abs(memory1-memory2))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
N = int(input())
P = "".join(list(map(str,input().split())))
Q = "".join(list(map(str,input().split())))
ls = permutations(list(range(1,N+1)))
ans = []
for i in list(ls):
    ans.append("".join(list(map(str,i))))
ans = sorted(ans)
a,b = 0,0
for i in range(len(ans)):
    if ans[i] == P:
        a = i
    elif ans[i] == Q:
        b = i
print(abs(int(a)-int(b)))

Submission status: Wrong Answer

Solution code:
from itertools import permutations
N = int(input())
P = "".join(list(map(str,input().split())))
Q = "".join(list(map(str,input().split())))
ls = permutations(list(range(1,N+1)))
ans = []
for i in list(ls):
    ans.append("".join(list(map(str,i))))
a,b = 0,0
for i in range(len(ans)):
    if ans[i] == P:
        a = i
    elif ans[i] == Q:
        b = i
print(abs(int(a)-int(b)))

Submission status: Wrong Answer

Solution code:
import math
import bisect
import collections
import itertools
import heapq
import string
import sys
sys.setrecursionlimit(10**9)
def gcd(a,b):return math.gcd #最大公約数
def lcm(a,b):return (a*b) // math.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = iin()
P = iln()
Q = iln()

PS = sorted(list(perm(P,N)))

p = 0
q = 0
for i in range(len(PS)):
    if P == list(PS[i]):
        p = i
    elif Q == list(PS[i]):
        q = i
print(abs(p-q))

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

a = range(1, n+1)
t, u = 0, 0
for i, s in enumerate(permutations(a)):
    if s == p: t = i
    elif s == q: u = i

print(abs(t - u))

Submission status: Wrong Answer

Solution code:
# 2020/01/10

from itertools import permutations

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

P=list(permutations(range(1,n+1)))


ans=0
for idx,e in enumerate(P,1):
    if e==p:
        ans+=idx
    elif e==q:
        ans-=idx

print(abs(ans))

Submission status: Wrong Answer

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys

sys.setrecursionlimit(10**9)
mod = 998244353
inf = 10**20

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

n = I()
p = LI()
q = LI()

l = [i for i in range(1,n+1)]
a=0
b=0
for i,x in enumerate(itertools.permutations(l)):
    if p==list(x):
        a = i
    elif q==list(x):
        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()))
nums = [i for i in range(1,n+1)]
a = [0,0]
now = 0
for k,i in enumerate(itertools.permutations(nums)):
  if i == p or i == q:
    a[now] = k
    now += 1
print(abs(a[1]-a[0]))

Submission status: Wrong Answer

Solution code:
import itertools as it

N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
a, b = 0, 0
count = 0
for perm in it.permutations(range(1, N + 1)):
    count += 1
    if list(perm) == P:
        a = count
    elif list(perm) == Q:
        b = count
print(abs(a - b))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    from itertools import permutations
    input = sys.stdin.readline

    N = int(input())
    P = input().rstrip('\n')
    Q = input().rstrip('\n')

    l = list(range(1, N+1))
    a = b = 0
    for i, s in enumerate(permutations(l)):
        s_ = ' '.join(map(str, s))
        if s_ == P:
            a = i
        elif s_ == Q:
            b = i
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
p = list(input().split())
q = list(input().split())
p = ''.join(p)
q = ''.join(q)
n_list = [str(i) for i in range(1, n + 1)]
n_comp = list(itertools.permutations(n_list, n))
n_join = []
for i in n_comp:
    n_join.append(''.join(i))
n_join.sort()

a = 0
b = 0
for i in range(len(n_join)):
    if n_join[i] == p:
        a = i
    elif n_join[i] == 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()))

import itertools
A = itertools.permutations([i+1 for i in range(n)], n)
B=list(A)

B.sort()

# print(B)

p=-1
q=-1
for i in range(len(B)):
    # print(P,[j for j in B[i]])
    if P == [j for j in B[i]]:
        
        p=i
    elif Q== [j for j in B[i]]:
        q=i
print(abs(p-q))

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
p,q=0,0
for i,R in enumerate(permutations(range(1,N+1))):
    if P==R:
        p=i
    elif Q==R:
        q=i
print(abs(p-q))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    P = tuple([int(i) for i in input().split()])
    Q = tuple([int(i) for i in input().split()])
    a, b = -1, -1
    from itertools import permutations
    for i, per in enumerate(permutations(range(1, N+1), N)):
        if per == P:
            a = i+1
        elif per == Q:
            b = i+1
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import permutations

n = int(input())
p = [int(x) for x in input().split(' ')]
q = [int(x) for x in input().split(' ')]

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

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

a = 0
b = 0

for i, x in enumerate(permutations(d)):
    if x == p:
        a = i
    elif x == q:
        b = i

print(abs(a - b))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
count = 0
if n == 2:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            wk = [i, j]
            wkset = set(wk)
            if len(wk) == len(wkset):
                count += 1
                if p == [i, j]:
                    start = count
                if q == [i, j]:
                    end = count
elif n == 3:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                wk = [i, j, k]
                wkset = set(wk)
                if len(wk) == len(wkset):
                    count += 1
                    if p == [i, j, k]:
                        start = count
                    if q == [i, j, k]:
                        end = count
elif n == 4:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                for l in range(1, n + 1):
                    wk = [i, j, k, l]
                    wkset = set(wk)
                    if len(wk) == len(wkset):
                        count += 1
                        if p == [i, j, k, l]:
                            start = count
                        if q == [i, j, k, l]:
                            end = count
elif n == 5:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                for l in range(1, n + 1):
                    for m in range(1, n + 1):
                        wk = [i, j, k, l, m]
                        wkset = set(wk)
                        if len(wk) == len(wkset):
                            count += 1
                            if p == [i, j, k, l, m]:
                                start = count
                            if q == [i, j, k, l, m]:
                                end = count
elif n == 6:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                for l in range(1, n + 1):
                    for m in range(1, n + 1):
                        for o in range(1, n + 1):
                            wk = [i, j, k, l, m, o]
                            wkset = set(wk)
                            if len(wk) == len(wkset):
                                count += 1
                                if p == [i, j, k, l, m, o]:
                                    start = count
                                if q == [i, j, k, l, m, o]:
                                    end = count
elif n == 7:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                for l in range(1, n + 1):
                    for m in range(1, n + 1):
                        for o in range(1, n + 1):
                            for r in range(1, n + 1):
                                wk = [i, j, k, l, m, o, r]
                                wkset = set(wk)
                                if len(wk) == len(wkset):
                                    count += 1
                                    if p == [i, j, k, l, m, o, r]:
                                        start = count
                                    if q == [i, j, k, l, m, o, r]:
                                        end = count
elif n == 8:
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            for k in range(1, n + 1):
                for l in range(1, n + 1):
                    for m in range(1, n + 1):
                        for o in range(1, n + 1):
                            for r in range(1, n + 1):
                                for s in range(1, n + 1):
                                    wk = [i, j, k, l, m, o, r, s]
                                    wkset = set(wk)
                                    if len(wk) == len(wkset):
                                        count += 1
                                        if p == [i, j, k, l, m, o, r, s]:
                                            start = count
                                        if q == [i, j, k, l, m, o, r, s]:
                                            end = count
if p == q:
    print(0)
else:
    print(abs(start - end))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lists_P = list(map(str, input().split()))
lists_Q = list(map(str, input().split()))
lists_P = "".join(lists_P)
lists_Q = "".join(lists_Q)


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

a_ = []
for i in range(10**(N-1), 10**N - 1):
    a = list(str(i))
    if len(set(a)) == N:
        count = 0
        for j in range(N):
            for k in aa:
                if a[j] == str(k):
                    count += 1
                else:
                    continue
        if count == N:
            a_.append("".join(a))
    else:
        continue

# print(a_)

a = 0
b = 0

for i, j in zip(range(len(a_)), a_):
    if j == lists_P:
        a = i
        break
for i, j in zip(range(len(a_)), a_):
    if j == lists_Q:
        b = i
        break


print(abs(a - b))

Submission status: Time Limit Exceeded

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

p_num = int(''.join(map(str, p)))
q_num = int(''.join(map(str, q)))

n_max = ''.join(map(str, reversed(range(1, n+1))))
n_max = int(n_max)

condition_ans = [1 if i<n else 0 for i in range(10)]

k = 0
for num in range(n_max+1):
    condition = [0 for _ in range(10)]
    
    for i in str(num):
        condition[int(i)-1] += 1
        
    if condition_ans == condition:
        if p_num == num:
            a = k
        if q_num == num:
            b = k
            
        k += 1
        
print(abs(a-b))

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
L = int(''.join([str(n) for n in range(1, N+1)]))
R = int(''.join([str(n) for n in range(1, N+1)][::-1]))

P = int(''.join(input().split()))
Q = int(''.join(input().split()))

C = [str(c) for c in range(1, N+1)]
A = []
for i in range(L, R+1):
    flg = True
    for c in C:
        if c not in str(i):
            flg = False
            break
    if flg:
        A.append(i)
print(abs(bisect.bisect(A, P)-bisect.bisect(A, Q)))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
val_list = [False for _ in range(pow(10, n))]


def make_val(suretsu):
    val = 0
    for index in range(n):
        val += suretsu[n - index - 1] * pow(10, index)
    return val

p_val = make_val(p)
q_val = make_val(q)
min_val = min(p_val, q_val)
max_val = max(p_val, q_val)

count = 0
counting = False
if min_val == max_val:
    print(0)
else:
    for suretsu in itertools.permutations(range(1, n+1)):
        val = make_val(suretsu)
        if val == min_val:
            counting = True
        if val == max_val:
            break
        if counting:
            count += 1


    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = int(''.join(input().split()))
y = int(''.join(input().split()))


lst = []

for i in range(10, 9 * 10 ** (n-1)):
  if n != len(set(str(i))) or set(str(x)) != set(str(i)):
    continue
  lst.append(i)
print(lst.index(max(x, y)) - lst.index(min(x, y)))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
val_list = [False for _ in range(pow(10, n))]


def make_val(suretsu):
    val = 0
    for index in range(n):
        val += suretsu[n - index - 1] * pow(10, index)
    return val



for suretsu in itertools.permutations(range(1, n+1)):
    val = make_val(suretsu)
    val_list[val] = True

p_val = make_val(p)
q_val = make_val(q)

count = 0
min_val = min(p_val, q_val)
max_val = max(p_val, q_val)
for val in range(min_val, max_val+1):
    if val_list[val]:
        count += 1
print(count-1)

Submission status: Time Limit Exceeded

Solution code:
def printArr(a, n): 
    m = []
    for i in range(n): 
        m.append(a[i])
    return m
  
# Generating permutation using Heap Algorithm 
def heapPermutation(a, size, n,h): 
      
    # if size becomes 1 then prints the obtained 
    # permutation 
    if (size == 1): 
        h.append(printArr(a, n))
        return h
  
    for i in range(size): 
        h = list(heapPermutation(a,size-1,n,h)) 
  
        # if size is odd, swap first and last 
        # element 
        # else If size is even, swap ith and last element 
        if size&1: 
            a[0], a[size-1] = a[size-1],a[0] 
        else: 
            a[i], a[size-1] = a[size-1],a[i] 
       
    return h
          
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a = list(sorted(p))
h = []
b = list(heapPermutation(a, n, n,h)) 
b.sort()

a1 = 0
b1 = 0

for i in range(len(b)):
    k = b[i]
    if(k == p):
        a1 = i
    if(k == q):
        b1 = i
print(abs(b1-a1))

Submission status: Time Limit Exceeded

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

Submission status: Accepted

Solution code:
import itertools

N = int(input())
N_list = [i for i in range(1,N+1)]

P = list(itertools.permutations(N_list))
P.sort()

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

Q_number = P.index(Q) + 1
O_number = P.index(O) + 1

print(abs(Q_number - O_number))

Submission status: Accepted

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(abs(b - a))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
ls = [ x for x in itertools.permutations(range(1, N + 1)) ]
a = [ key for key, val in enumerate(ls) if P == val ][0] + 1
b = [ key for key, val in enumerate(ls) if Q == val][0] + 1
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()))


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

Submission status: Accepted

Solution code:
import itertools
import copy
n = int(input())
P = list(map(int, input().split()))
Q = tuple(list(map(int, input().split())))
P_ = copy.copy(P)
P_.sort()
p_list = list(itertools.permutations(P_))
P = tuple(P)

a = p_list.index(P)
b = p_list.index(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()))
ls = list(itertools.permutations(range(1, n + 1)))
print(abs(ls.index(p) - ls.index(q)))

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(factorial(n)):
    if p==ls[i]:
        a = i
    if q==ls[i]:
        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
l = list(itertools.permutations(range(1,N+1)))

a = 0
b = 0

for i in range(len(l)):
    s = [i for i in l[i]]

    if s == p:
        a = i 
    if s == 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()))
r = list(itertools.permutations([i for i in range(1,n+1)]))
P , Q = (r.index(p)), (r.index(q))
print(abs(P-Q))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
N = int(input())
target_list = []
for i in range(2):
  target_list.append(tuple(map(int,input().split())))
  
list=[_ + 1 for _ in range(N)]
per=permutations(list,N)

ans = 0
counter = 1
if target_list[0] == target_list[1]:
  print("0")
  sys.exit()
for i in per:
  if i in target_list:
    #print(counter)
    if ans == 0:
      ans -= counter
    else:
      ans += counter
    
  counter += 1
print(ans)

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)]
for i, tpl in enumerate(itertools.permutations(l, N)):
    if tpl == P:
        a = i
    if tpl == 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()))

def library_number(x,n):
  l = list(range(1,n+1))
  tmp = 0
  for i in range(n):
    tmp += math.factorial(n-1-i)*l.index(x[i])
    l.remove(x[i])
  return tmp

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