TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: 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|.

Submission attempts (in order):
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=i+1
from itertools import permutations
templ=list(permutations(temp,n) 
for i in range(0,len(templ)):
  if templ[i]==p:
    a=i
  if templ[i]==q:
    b=i
print(abs(a-b))

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

p = 0
q = 0
if P == Q:
  ans = 0
  print(ans)
  quit()
  
x = 0
for one_case in permutations_lis:
  x += 1
  if P == list(one_case):
    p = x
  elif Q == list(one_case):
    q = x

print(abs(q-p))  

Verdicts: Runtime Error

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

Submission attempts (in order):
import itertools

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

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

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

import itertools

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

ls = list(itertools.permutations(range(1,n))) 

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

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fac = [0] * 8

fac[1] = 1
for i in range(2, N):
    
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))


for i in range(N):
    a += 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))

Verdicts: Runtime Error

Runtime Error

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

Submission attempts (in order):
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))

pFlag, qFlag = False, False

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

    aaa = list(val)
    # print(aaa)
    
    if (aaa == p):
        pp = i + 1
        pFlag = True
    if (aaa == q):
        qq = i + 1
        qFlag = True

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


# -- not using buffer ----------------------
import itertools
import sys
import math
'''
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

a, b = map(int, readline().split())
print(a)

lis = [int(readline().rstrip('\n')) for _ in range(b)]
print(lis)
c = int(read())
print(c)
'''

# --------------------------------------------
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
'''
a, b = map(int, readline().split())
print(a)

lis = [int(readline()) for _ in range(b)]
print(lis)
c = int(read())
print(c)
'''
# -----------------------
# -----------------------------------
n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
ansList = [i for i in range(1, n + 1)]

div = n // 2
#com = list(itertools.product(ansList, repeat=n))
com = list(itertools.permutations(ansList))


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

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


Verdicts: Wrong Answer

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

Submission attempts (in order):
from itertools import math
N = int(input())
P = tuple(map(int(input().split())))
Q = tuple(map(int(input().split()))
# Find all possible and allow the length
perm = permutations((range(1,N+1)),N)
print(abs(perm.index(P)-perm.index(Q)))


from itertools import permutations
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
# Find all possible
perm = list(permutations(range(1,N+1),N))
print(abs(perm.index(P)- perm.index(Q)))

Verdicts: Runtime Error

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

Submission attempts (in order):
import sys
N = int(sys.stdin.readline())
P,Q = sys.stdin.readlines()

print(0)

import itertools

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

a = 0
b = 0
deck = list(map(str, range(1,N+1)))
cnt = 0
for s in itertools.permutations(deck):
	ss = "".join(s)
	if ss == P:
		a = cnt
	if ss == Q:
		b = cnt

	cnt += 1

print(abs(a-b))


Verdicts: Wrong Answer

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

Submission attempts (in order):
import sys
import itertools

INF = 1001001001

def resolve():
  n = int(input())
  p = list(map(int, input().split()))
  q = list(map(int, input().split()))
  arr = []
  for i in range(1, n+1):
    arr.append(i)

  x = itertools.permutations(arr, n)

  a = 0
  b = 0
  for i, y in enumerate(x):
    print(type(y))
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1

  print(abs(a - b))
resolve()


import sys
import itertools

INF = 1001001001

def resolve():
  n = int(input())
  p = list(map(int, input().split()))
  q = list(map(int, input().split()))
  arr = []
  for i in range(1, n+1):
    arr.append(i)

  x = itertools.permutations(arr, n)

  a = 0
  b = 0
  for i, y in enumerate(x):
    if (p == list(y)):
      a = i + 1
    if (q == list(y)):
      b = i + 1

  print(abs(a - b))
resolve()


Verdicts: Wrong Answer

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

Submission attempts (in order):
n = int(input())
a = input()
b = input()

import itertools
l = list(itertools.permutations(range(1,n+1))
print(abs(l.index(a)-l.index(b)))

from itertools import permutations
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
cnt = 1
for num in permutations(range(1,n+1)):
    cnt += 1
    if list(num) == p:
        p_num = cnt
    if list(num) == q:
        q_num = cnt
print(abs(q_num-p_num))

Verdicts: Runtime Error

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

Submission attempts (in order):
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))

def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

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


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

def mul(n):
  s=1
  for t in range(n):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

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

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

def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  M=len(x)
  for i in range (x):
    k=0
    for j in range(i,M):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(M-i-1)
  return result

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


def mul(n):
  s=1
  for t in range(1,n+1):
    s=s*t
  return s

def f(x):
  result=0
  N=len(x)
  for i in range (N):
    k=0
    for j in range(i,N):
      if x[i]>x[j]:
        k=k+1
    result=result+k*mul(N-i-1)
  return result

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

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

Verdicts: Runtime Error

Runtime Error

Runtime Error

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

Submission attempts (in order):
import itertools as itool
N=int(input())
plist=list(map(int, input().split()))
qlist=list(map(int, input().split()))
mylist=list(itool.permutations(range(1,n+1)))
print(abs(mylist.index(p)-mylist.index(q)))


import itertools as itool
N=int(input())
plist=list(map(int, input().split()))
qlist=list(map(int, input().split()))
mylist=list(itool.permutations(range(1,N+1)))

print(abs(mylist.index(tuple(plist))-mylist.index(tuple(qlist))))


Verdicts: Runtime Error

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

Submission attempts (in order):
import itertools

N= int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))
p = list(itertools.permutations(range(1, N + 1)))
a = p.index(P)
b = p.index(Q)
print(abs(a - b))

import itertools
 
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
U = list(itertools.permutations(range(1, N+1)))
a = U.index(P)
b = U.index(Q)
print(abs(a-b))

import itertools

N= int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))
p = list(itertools.permutations(P))
a = p.index(P)
b = p.index(Q)
print(abs(a - b))

import itertools

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
U = list(itertools.permutations(range(1, N+1)))
a = U.index(P)
b = U.index(Q)
print(abs(a-b))

Verdicts: Runtime Error

Runtime Error

Runtime Error

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

Submission attempts (in order):
import sys
import itertools

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

l = list(itertools.permutations([i for i in range(1, n+1)], 3))
x = sorted([l.index(p)+1, l.index(q)+1], reverse=True)
print(x[0] - x[1])

import sys
import itertools

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

l = list(itertools.permutations([i for i in range(1, n+1)], n))
x = sorted([l.index(p)+1, l.index(q)+1], reverse=True)
print(x[0] - x[1])

Verdicts: Runtime Error

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

Submission attempts (in order):
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
import itertools
J=(list(itertools.permutations(range(n))))

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

n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
import itertools
J=(list(itertools.permutations(range(1,n+1))))
#print(J)
for i, j in enumerate(J):
    #print(j,p,q)
    if j == p:
        a=i
    if j==q:
        b=i
print(abs(a-b))

Verdicts: Runtime Error

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

Submission attempts (in order):
import math
from collections import Counter
N,M = map(int,input().split())
a = list(map(int,input().split()))
maxa = max(a)
i = 1
p = 1
while True:
    tmp = (i+0.5)*maxa
    if math.ceil(tmp) == math.floor(tmp):
        p = i
        break
    i += 1
lcm = 0
flag = False
for i in range(p,M+1):
    if flag:
        break
    for j in range(N):
        if flag:
            break
        tmp = i/a[j]
        if int(tmp)+0.5 != tmp:
            break
        if int(tmp)+0.5 == tmp and j == N-1:
            lcm = i
            flag = True
if lcm == 0:
    print(0)
else:
    ans = M/lcm
    print(math.ceil(math.floor(ans)/2))

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+1
    if ans[i] == Q:
        b = i+1
print(abs(int(a)-int(b)))

Verdicts: Runtime Error

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

Submission attempts (in order):
import itertools
N = input()
P = input()
Q = input()
string1 = tuple(map(int,list(P.split(""))))
string2 = tuple(map(int,list(Q.split(""))))
num =list(itertools.permutations(list(range(1, int(N)+1))))
count = 1
for i in num:
  if i==string1:
    a=count
  if i==string2:
    b=count
  count+=1
print(abs(a-b))

import itertools

N = input()
P = input()
Q = input()
string1 = tuple(map(int, list(P.split(" "))))
string2 = tuple(map(int, list(Q.split(" "))))

num =list(itertools.permutations(list(range(1, int(N)+1))))
count = 1

for i in num:
    if i == string1:
        a = count
    if i == string2:
        b = count
    count += 1

print(abs(a-b))

Verdicts: Runtime Error

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

Submission attempts (in order):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

def f(n):
    if n == 1:
        return(1)
    else:
        return(n * f(n-1))

def g(a):
    if a == []:
        return(1)
    else:
        b = sorted(a)
        for i in range(0,len(a)):
            if a[0] == b[i]:
                a.pop(0)
                return(i * f(len(a)) + g(a))

print(abs(g(P)-g(Q)))

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

def f(n):
    if n == 1:
        return(1)
    else:
        return(n * f(n-1))

def g(a):
    if a == []:
        return(1)
    else:
        b = sorted(a)
        for i in range(0,len(a)):
            if a[0] == b[i]:
                a.pop(0)
                return(i * f(len(a)) * g(a))

print(abs(g(P)-g(Q)))

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

def g(a):
    if a == []:
        return(1)
    else:
        b = sorted(a)
        for i in range(0,len(a)):
            if a[0] == b[i]:
                a.pop(0)
                return(i * math.factorial(len(a)) + g(a))

print(abs(g(P)-g(Q)))

Verdicts: Runtime Error

Runtime Error

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

Submission attempts (in order):
import itertools

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

A=list(itertools.permutations(range(1,N+1)))
print(A)
pindex=A.index(P)+1
qindex=A.index(Q)+1

print(abs(pindex-qindex))


import itertools

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

A=list(itertools.permutations(range(1,N+1)))
#print(A)
pindex=A.index(P)+1
qindex=A.index(Q)+1

print(abs(pindex-qindex))


Verdicts: Wrong Answer

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

Submission attempts (in order):
import math

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

#p
p_n=0
q_n=0
for i in range(n):
    count_p=p[i]-1
    count_q=q[i]-1
    for j in range(i):
        if p[j]<p[i]:
            count_p-=1
        if q[j]<q[i]:
            count_q-=1
    p_n+=math.factorial(n-i-1)*count_p
    q_n+=math.factorial(n-i-1)*count_q

print(p_n-q_n)

import math

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

#p
p_n=0
q_n=0
for i in range(n):
    count_p=p[i]-1
    count_q=q[i]-1
    for j in range(i):
        if p[j]<p[i]:
            count_p-=1
        if q[j]<q[i]:
            count_q-=1
    p_n+=math.factorial(n-i-1)*count_p
    q_n+=math.factorial(n-i-1)*count_q

print(abs(p_n-q_n))


Verdicts: Wrong Answer

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

Submission attempts (in order):
from itertools import permutations

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

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

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

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

Verdicts: Runtime Error

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

Submission attempts (in order):
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))

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

Verdicts: Wrong Answer

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

Submission attempts (in order):
import math
import numpy as np

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

    print(getal(p)+1)
    print(getal(q)+1)

def getal(arr):
    tarr = arr.copy()
    arg = [None]*len(arr)
    
    c = 0
    for i in range(len(arr)):
        arg[arr.index(min(tarr))] = c
        tarr.remove(min(tarr))
        c += 1

    x = math.factorial(len(arr)-1)

    v = arg[0]*x
    # print(arr, arg, v)

    if len(arr) == 2:
        return v
    else:
        ne = arr[1:]
        return v + getal(ne)

if __name__ == "__main__":
    main()

import math
import numpy as np

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

    pe = getal(p)+1
    qe = getal(q)+1
    print(abs(pe-qe))

def getal(arr):
    tarr = arr.copy()
    arg = [None]*len(arr)
    
    c = 0
    for i in range(len(arr)):
        arg[arr.index(min(tarr))] = c
        tarr.remove(min(tarr))
        c += 1

    x = math.factorial(len(arr)-1)

    v = arg[0]*x
    # print(arr, arg, v)

    if len(arr) == 2:
        return v
    else:
        ne = arr[1:]
        return v + getal(ne)

if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

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

Submission attempts (in order):

def dfs(node, paths, visited, n):
  
  if not False in visited:
    return 1

  count = 0

  for nn in paths[node]:
    
    if(visited[nn]):
      continue

    visited[nn] = True
    count += dfs(nn, paths, visited, n)
    visited[nn] = False

  return count

def main():
  N, M = list(map(int, input().split()))
  paths = [[] for i in range(N)]
  visited = [False] * N

  for i in range(M):
    a, b = list(map(int, input().split()))
    paths[a-1].append(b-1)
    paths[b-1].append(a-1)

  visited[0] = True
  print(dfs(0, paths, visited, N))


if __name__ == "__main__":
    main()

import itertools

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

pi = 0
qi = 0

def dfs(array, n):
  if(len(array) == n):
    return [tuple(array)]

  ret = []

  
  for i in range(1, N+1):

    if(i in array):
      continue

    array.append(i)
    ret.extend(dfs(array, n))
    array.pop()

  return ret


for index, pattern in enumerate(dfs([], N)):
  if P == pattern:
    pi = index
  if Q == pattern:
    qi = index

print(abs(pi - qi))


Verdicts: Runtime Error

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

Submission attempts (in order):
#C - Count Order
#DFS
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

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

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

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

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

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

Verdicts: Wrong Answer

Wrong Answer

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

Submission attempts (in order):
import math


def recurse(a, n):
    if n > len(a):
        return 1
    x = a[n-1]
    z = x - n
    if z < 0:
        z = 0
    return z * math.factorial(len(a) - n) + recurse(a, n + 1)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a = recurse(a, 1)
    b = recurse(b, 1)
    print(a)
    print(b)
    print(abs(b - a))


import itertools

if __name__ == "__main__":
    n = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    x = list(itertools.permutations(range(1, n + 1)))
    print(abs(x.index(a) - x.index(b)))


Verdicts: Wrong Answer

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

Submission attempts (in order):
import itertools

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

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

import itertools

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

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

Verdicts: Wrong Answer

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

Submission attempts (in order):
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))

import itertools

N = int(input())

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

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

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

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

print(abs(b-a))

Verdicts: Wrong Answer

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

Submission attempts (in order):
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))
     

import itertools
import math

n = input()
p = input()
q = input()

counter = 1
base = list(range(1,n+1))

i = 0
for x in itertools.permutations(range(n), n):
	i += 1
    if p == x:
    	a = i
    elif q == x:
    	b = i
print(math.abs(a-b))


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

import itertools
import math

n = input()
p = input()
q = input()
i==0
for x in itertools.permutations(range(1,n+1), n):
	i += 1
	if p == x:
      a = i
    elif q == x:
      b = i
print(math.abs(a-b))

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

import itertools
#import math

n = int(input())
#n = 2
#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)
	if x == q:
		b = i
		#print(i)

print(abs(a-b))
     

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

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

Submission attempts (in order):
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
def kaijo(n):
  if n== 1:
    return n
  return n * kaijo(n-1)
N_kaijo = kaijo(N)
a = []
for i in range(N):
  a.append(str(i+1))
X = list(itertools.permutations(a,N))
for i in range(len(X)):
  if P == X[i]:
    p_number = i+1
  if Q == X[i]:
    q_number = i+1
if p_number > q_number:
  ans = p_number - q_number
else:
  ans = q_number - p_number
print(ans)


import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
def kaijo(n):
  if n== 1:
    return n
  return n * kaijo(n-1)
N_kaijo = kaijo(N)
a = []
for i in range(N):
  a.append(i+1)
X = list(itertools.permutations(a,N))
p_number = 0
q_number = 0
for i in range(len(X)):
  s = []
  for j in range(N):
    s.append(X[i][j])
  if s == P:
    p_number = i+1
  if s == Q:
    q_number = i+1
if p_number > q_number:
  ans = p_number - q_number
else:
  ans = q_number - p_number
print(ans)

Verdicts: Runtime Error

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

Submission attempts (in order):
# ABC150C

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans = 0
while n > 1:
    o = s[0]
    ans += (o - 1) * factorial(n - 1, True)
    s.pop(0)
    n = len(s)
    if n == 0:
        break
    s = [i if i < o else i - 1 for i in s]
return ans


from math import factorial
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ans = 0
while n > 1:
    o = s[0]
    ans += (o - 1) * factorial(n - 1, True)
    s.pop(0)
    n = len(s)
    if n == 0:
        break
    s = [i if i < o else i - 1 for i in s]
print(ans)


from math import factorial

def ABC150C(s):
    n = len(s)
    ans = 0
    while n > 1:
        o = s[0]
        ans += (o - 1) * factorial(n - 1)
        s.pop(0)
        n = len(s)
        if n == 0:
            break
        s = [i if i < o else i - 1 for i in s]
    return ans

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
print(abs(ABC150C(p) - ABC150C(q)))


Verdicts: Runtime Error

Runtime Error

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

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


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

Verdicts: Wrong Answer

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

Submission attempts (in order):
import itertools
P,Q = list(map(int,input().split()))
N = int(input())
L = [list(i) for i in itertools.permutations(range(1, N+1), r=N)]
print(abs(L.index(P) - L.index(Q)))

N = int(input())
P,Q = list(map(int,input().split()))
import itertools
L = [list(i) for i in itertools.permutations(range(1, N+1), r=N)]
print(abs(L.index(P) - L.index(Q)))

N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
import itertools
L = [list(i) for i in itertools.permutations(range(1, N+1), r=N)]
print(abs(L.index(P) - L.index(Q)))

Verdicts: Runtime Error

Runtime Error

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

Submission attempts (in order):
import itertools
li = [1,2,3,4,5,6,7,8]
for v in itertools.combinations(li):
  print(v)

import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
li = [i for i in range(1, n+1)]
j = 1
p_index = 1
q_index = 1
for v in itertools.permutations(li, n):
  if(list(v) == p):
    p_index = j
  if(list(v) == q):
    q_index = j
  j += 1
print(abs(p_index - q_index))

Verdicts: Runtime Error

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

Submission attempts (in order):
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()

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

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(abs(P_order-Q_order))
  
if __name__ == '__main__':
  main()

Verdicts: Wrong Answer

Wrong Answer

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

Submission attempts (in order):
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))

import itertools

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

pmt = list(itertools.permutations(range(1, N+1)))
pmtP = pmt.index(P)
pmtQ = pmt.index(Q)
print(abs(pmtP - pmtQ))

Verdicts: Wrong Answer

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

Submission attempts (in order):
from itertools import permutations

n = int(input())
nums1 = list(map(int,input(),split("")))
nums2 = list(map(int,input(),split("")))

t1 = tuple(nums1)
t2 = tuple(nums2)

index = 1
a = b = -1

for perm in permutations(list(range(1,n+1))):
    if perm == t1:
        a = index
    if perm == t2:
        b = index
    index +=  1

print(abs(a-b))

from itertools import permutations

n = int(input())
nums1 = list(map(int,input(),split("")))
nums2 = list(map(int,input(),split("")))

t1 = tuple(nums1)
t2 = tuple(nums2)

index = 1
a = b = -1

for perm in permtations(list(range(1,n+1))):
    if perm == t1:
        a = index
    if perm == t2:
        b = index
    index +=  1

print(abs(a-b))

from itertools import permutations

n = int(input())
nums1 = list(map(int,input().split(" ")))
nums2 = list(map(int,input().split(" ")))

t1 = tuple(nums1)
t2 = tuple(nums2)

index = 1
a = b = -1

for perm in permutations(list(range(1,n+1))):
    if perm == t1:
        a = index
    if perm == t2:
        b = index
    index += 1

print(abs(a-b))

Verdicts: Runtime Error

Runtime Error

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

Submission attempts (in order):
import itertools

N = int(sys.stdin.readline())
P = [int(v) for v in sys.stdin.readline().split()]
Q = [int(v) for v in sys.stdin.readline().split()]
Ps = [list(v) for v in itertools.permutations(range(1, N + 1),N)]
Ps = sorted(Ps)

def find(l1):
    for i, l3 in enumerate(Ps):
        if l3 == l1:
            return i
    return 0


print(abs(find(P) - find(Q)))



import sys
import itertools

N = int(sys.stdin.readline())
P = [int(v) for v in sys.stdin.readline().split()]
Q = [int(v) for v in sys.stdin.readline().split()]
Ps = [list(v) for v in itertools.permutations(range(1, N + 1),N)]
Ps = sorted(Ps)

def find(l1):
    for i, l3 in enumerate(Ps):
        if l3 == l1:
            return i
    return 0

print(abs(find(P) - find(Q)))



Verdicts: Runtime Error

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

Submission attempts (in order):
n=int(n)
m=int(m)
x=[false for i in range(n)]
count=[0 for i in range(n)]
for i in range(m):
  p,s=input().split()
  p=int(p)
  if(not x[p-1]):
    if(s=="AC"):
      x[p-1]=true
    else:
      count[p-1]=count[p-1]+1
ans=0
ans2=0
for i in range(ansn):
  if(x[i]):
    ans+=1
    ans2+=count[i]
print(ans,ans2)

import math
import itertools
n=int(input())

a=list(map(str,input().split()))
b=list(map(str,input().split()))

a_str = [''.join(v) for v in itertools.permutations(a)]
a_int = [int(i) for i in a_str]
a_int.sort()

x="".join(a)
y="".join(b)

p=a_int.index(int(x))+1
q=a_int.index(int(y))+1

print(abs(p-q))


Verdicts: Runtime Error

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

Submission attempts (in order):
cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))
q = list(map(int, cin[2].split(' ')))

a = 0
b = 0
for idx, case in enumerate(itertools.permutations(range(1, n+1))):
    if case == tuple(p):
        a = idx+1
    if case == tuple(q):
        b = idx+1
print(abs(a-b))

cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))
q = list(map(int, cin[2].split(' ')))

import itertools

a = 0
b = 0
for idx, case in enumerate(itertools.permutations(range(1, n+1))):
    if case == tuple(p):
        a = idx+1
    if case == tuple(q):
        b = idx+1

print(abs(a-b))

Verdicts: Runtime Error

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

Submission attempts (in order):
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))

    s = itertools.permutations(range(1, n+1))
    l = []
    for v in s:
        l.append(v)
    p_index = l.index(p)
    q_index = l.index(q)

    print(abs(p_index - q_index))


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

s = itertools.permutations(range(1, n+1))
l = []
for v in s:
    l.append(v)
p_index = l.index(p)
q_index = l.index(q)
print(abs(p_index - q_index))


Verdicts: Runtime Error

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

Submission attempts (in order):
import numpy as np
import interpools

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

Num = range(1,N+1)

Number = str(''.join(map(str,Num)))


per_num=list(itertools.permutations(Number))
pernum_arr = np.ones(len(per_num))
for i in range(len(per_num)): 
    pernum_arr[i] = int(''.join(map(str,per_num[i])))
P = int(''.join(map(str,P)))
Q = int(''.join(map(str,Q)))
print(P)
ans = abs(np.where(pernum_arr==P)[0]-np.where(pernum_arr==Q)[0])
print(ans[0])

import numpy as np
import itertools

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

Num = range(1,N+1)

Number = str(''.join(map(str,Num)))


per_num=list(itertools.permutations(Number))
pernum_arr = np.ones(len(per_num))
for i in range(len(per_num)): 
    pernum_arr[i] = int(''.join(map(str,per_num[i])))
P = int(''.join(map(str,P)))
Q = int(''.join(map(str,Q)))

ans = abs(np.where(pernum_arr==P)[0]-np.where(pernum_arr==Q)[0])
print(ans[0])


Verdicts: Runtime Error

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

Submission attempts (in order):
import math
a=b=1
l=[i for i in range(1,N+1)]
m=[i for i in range(1,N+1)]
for i in range(N):
  #print("===P[i]===", P[i])
  #print(l.index(P[i]), math.factorial(N-i-1))
  a += l.index(P[i])*math.factorial(N-i-1)
  #print("l", l)
  if not i==N-1:
    l.pop(l.index(P[i]))
for i in range(N):
  b += m.index(Q[i])*math.factorial(N-i-1)
  if not i==N-1:
    m.pop(m.index(Q[i]))
print(abs(a-b))


import math
N=int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))
a=b=1
l=[i for i in range(1,N+1)]
m=[i for i in range(1,N+1)]
for i in range(N):
  #print("===P[i]===", P[i])
  #print(l.index(P[i]), math.factorial(N-i-1))
  if N-i-1 < 0:
    pass
  else:
    a += l.index(P[i])*math.factorial(N-i-1)
  #print("l", l)
  if not i==N-1:
    l.pop(l.index(P[i]))
for i in range(N):
  if N-i-1 < 0:
    pass
  else:
    b += m.index(Q[i])*math.factorial(N-i-1)
  if not i==N-1:
    m.pop(m.index(Q[i]))
print(abs(a-b))


Verdicts: Runtime Error

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

Submission attempts (in order):
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


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


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

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

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

    print(abs(s))



Verdicts: Wrong Answer

Wrong Answer

Accepted