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 math
import itertools

n = int(input())
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
num_list = []
num = 0
a = 0
b = 0

for i in itertools.permutations(range(1,n+1)):
    num_list = list(i)
    num += 1
    if num_list == p_list:
        a = num
    if num_list == q_list:
        b = num

print(math.fabs(a-b))

Submission status: Wrong Answer

Solution code:
def get_order(numbers, originals):
    
    from math import factorial
    from copy import copy
    numbers = copy(numbers)
    originals = copy(originals)
    
    total = 0
    while numbers:
        number = numbers.pop(0)
        number_index = originals.index(number)
        originals.pop(number_index)
        count = (number_index ) * factorial(len(originals))
        total += count
    return total
        
def get_next_int():
    return int(float(input()))
def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])

def main(print=__builtins__.print):
    n = get_next_int()
    
    a = list(get_next_ints())
    b = list(get_next_ints())
    originals = list(map(int, range(1, n+1)))
    print(get_order(a, originals) - get_order(b, originals))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

nAllList=[]
for i in itertools.permutations(nList):
  nAllList.append(i)
nAllList.sort()

pNum=0
qNum=0

for i in range(len(nAllList)):
  if(nAllList[i]==P):
    pNum=i+1
  elif(nAllList[i]==Q):
    qNum=i+1

print(abs(pNum-qNum))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == '__main__':
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(input())
    p = input().split(' ')
    q = input().split(' ')

    for i in range(0, len(q)):
        p[i] = int(p[i])
        q[i] = int(q[i])

    realnums = p.copy()

    realnums.sort()

    perm = list(itertools.permutations(realnums))

    for per in perm:
        print(per)

    it = 0

    qplace = 0
    pplace = 0

    for i in perm:
        if list(i) == q:
            qplace = it
        if list(i) == p:
            pplace = it
        it += 1
    print(abs(qplace - pplace))

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == '__main__':
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(input())
    p = input().split(' ')
    q = input().split(' ')

    for i in range(0, len(q)):
        p[i] = int(p[i])
        q[i] = int(q[i])

    perm = list(itertools.permutations(p))

    it = 0

    qplace = 0
    pplace = 0

    for i in perm:
        if list(i) == q:
            qplace = it
        if list(i) == p:
            pplace = it
        it += 1
    print(abs(qplace - pplace))

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == '__main__':
    # P and Q are both rearrangements of (1, 2, ..., N)).
    # N! possible permutations of size N
    # among them, let P and Q be the a-th and the b-th
    n = int(input())
    p = input().split(' ')
    q = input().split(' ')

    for i in range(0, len(q)):
        p[i] = int(p[i])
        q[i] = int(q[i])

    perm = list(itertools.permutations(p))

    it = 0

    qplace = 0
    pplace = 0

    for i in perm:
        it += 1
        if list(i) == q:
            qplace = it
        if list(i) == p:
            pplace = it
    print(abs(pplace - qplace))

Submission status: Wrong Answer

Solution code:
import itertools
import sys

N = int(input())
T = []
T.append(tuple(map(int, input().split())))
T.append(tuple(map(int, input().split())))

if T[0] == T[1]:
    sys.exit(0)

M = [0]*N
for i in range(N):
    M[i] = i+1

ans = []
for i, v in enumerate(itertools.permutations(M, N)):
    if v in T:
        ans.append(i)

print(abs(ans[0] - ans[1]))

Submission status: Wrong Answer

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

ans = []
l = range(1, N + 1)
import itertools

c = 0
for v in itertools.permutations(l, N):
    if list(v) == P:
        a = c
    if list(v) == Q:
        b = c
    c += 1

print(b - a)

Submission status: Wrong Answer

Solution code:
import math
import itertools

N = int(input())
Plist = list(map(int, input().split()))
Qlist = list(map(int, input().split()))

a = 1
b = 1

arr = list()
for i in range(N):
    arr.append(i+1)

counter = 1
for i in itertools.permutations(arr):

    if list(i) == Plist:
        a = counter
    elif list(i) == Qlist:
        b = counter
    counter += 1

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

Submission status: Wrong Answer

Solution code:
import math


N = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

def CO(l):
    n = len(l)
    if n != 1:
        m = sorted(l)
        a = l[0]
        s = m.index(a)
        l.remove(a)
        return((s)*math.factorial(n-1)+CO(l))
    else:
        return 1

print(abs(CO(list1)+CO(list2)))

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
l = sorted(list(itertools.permutations([i for i in range(1,n+1)])))

al = tuple([int(i) for i in input().split()])
bl = tuple([int(i) for i in input().split()])


a,b = 0,0

for idx,i in enumerate(l):
    if i == al:
        a = idx
    elif i == bl:
        b = idx
    else:
        pass
    
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
# 英大文字のみからなる長さ N の文字列 S があります。
# S の連続する部分列 (入出力例をご覧ください) として ABC がいくつ含まれるかを求めてください。
# 制約： 3≤N≤50, Sは英大文字のみからなる。
# 入力: N
#       S

# 文字列長を受け取る
N = input()

# 文字列を受け取る
S = input()

print(S.count('ABC'))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
p=[str(i) for i in input().split()]
p=','.join(p)
p=p.replace(",", "")
q=[str(i) for i in input().split()]
q=','.join(q)
q=q.replace(",", "")

r='1'
for i in range(2,n+1):
  r+=str(i)

List = [r]

for x in itertools.permutations(r):
  y=','.join(x)
  y=y.replace(",", "")
  List.append(y)
  
P=List.index(p)
Q=List.index(q)
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()))

s = []
for i in range(N):
  s.append(i)
p_list = list(itertools.permutations(s))

p_n = 0
q_n = 0
for i in range(len(p_list)):
  if p == p_list[i]:
    p_n = i
  if q == p_list[i]:
    q_n = i
    
print(p_n - q_n) if p_n >= q_n else print(q_n - p_n)

Submission status: Wrong Answer

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

l = 0
r = 0

for i, j in enumerate(itertools.permutations(range(1,N+1)), start=1):
    if j == P:
        l = i
    elif j == Q:
        r = i
    else:
        pass

print(abs(l-r))

Submission status: Wrong Answer

Solution code:
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
import itertools
li=[i for i in range(1,n+1)]
cnt=0
a=-1
for x in itertools.permutations(li):
    x=list(x)
    if x==p or x==q:
        if a>=0:
            print(abs(cnt-a))
            exit()
        else:
            a=cnt    
    cnt+=1

Submission status: Wrong Answer

Solution code:
N = int(input())
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
 
data = [i+1 for i in range(N)]
 
def listExcludedIndices(data, indices=[]):
  return [x for i, x in enumerate(data) if i not in indices]
 
def count(res,p):
    count = 0
    for i in range(len(res)):
        count +=1
        if res[i] == p:
            break
    return count
result = []
for i in range(len(data)):
  for j in range(len(data)-1):
    for k in range(len(data)-2):
      jData = listExcludedIndices(data, [i])
      kData = listExcludedIndices(jData, [j])
      result.append([data[i], jData[j], kData[k]])
print(abs(count(result,P) - count(result,Q)))

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())

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

per = list(itertools.permutations(seq))


#print(type(per[0]))

A = tuple(map(int, input().split()))
#print(A)

#print(per.index(A))

B = tuple(map(int, input().split()))
#print(B)

#print(per.index(B))

print(per.index(B)-per.index(A))

Submission status: Wrong Answer

Solution code:
import itertools


def main():
    n = int(input())
    p = tuple(input().split())
    q = tuple(input().split())

    a = list(itertools.permutations([str(i+1) for i in range(n)]))
    print(a)
    print(abs(a.index(q)-a.index(p)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import permutations
n = list(range(1, int(input())+1))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
# p = ''.join(a)
# q = ''.join(b)
pp , qq = 0, 0
for i, j in enumerate(permutations(n)):
    if j == p:
        pp = i+1
    elif j == q:
        qq = i+1
    if pp and qq:
        break
print(abs(pp-qq))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = [int(a) for a in input().split()]
Q = [int(a) for a in input().split()]
print(P, Q)

listN=list(itertools.permutations(range(1,N+1)))
countP = 0
for i in range(0,len(listN)):
    if list(listN[i])==P:
        break
    else:
        countP+=1



countQ = 0
for j in range(0,len(listN)):
    if list(listN[j])==Q:
        break

    else:
        countQ+=1

print(abs(countP-countQ))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
p=list(map(str, input().split()))
p_new=''.join(p)
q=list(map(str, input().split()))
q_new=''.join(q)
a=[]
pc, qc=0,0

for v in itertools.permutations(range(1,n+1)): #Nまでの順列の辞書順リスト生成
    a_new=map(str, v)
    a_i=''.join(a_new)
    a.append(a_i)

for i in range(len(a)): #pとqが辞書順で何番目かを特定する
    if p_new==a[i]:
        pc+=i
    elif q_new==a[i]:
        qc+=i
    else:
        continue
print(abs(pc-qc))

Submission status: Wrong Answer

Solution code:
n = int(input())
fac = [i for i in range(10)]
for i in range(2,10):
  fac[i] *= fac[i-1]
def bub(n,l):
  ret = 0
  use = [False] + [True]*n
  for i in range(n):
    ret += sum(use[:l[i]])*fac[n-1-i]
    use[l[i]] = False
  return ret

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

Submission status: Wrong Answer

Solution code:
n = int(input())
def bub(n,l):
  tot = 0
  for _ in range(n):
    for i in range(n-1):
      if l[i+1] > l[i]:
        l[i],l[i+1] = l[i+1],l[i]
        tot += 1
  return tot
p = list(map(int, input().split()))
q = list(map(int, input().split()))
print(abs(bub(n,p)-bub(n,q)))

Submission status: Wrong Answer

Solution code:
import itertools

# 各種変数の取り込み
num = int(input())
a   = tuple(map(int, input().split()))
b   = tuple(map(int, input().split()))

l = range(1, num + 1)
p = list(itertools.permutations(l))

ans = p.index(b) - p.index(a)
print(ans)

Submission status: Wrong Answer

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

li=[]

for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      if i==k or i==j or j==k:
        continue
      else:
        li.append([i,j,k])
result1=0
result2=0
for i in range(len(li)):
  if li[i]==s:
    result1=i+1
  if li[i]==q:
    result2=i+1
print(abs(result1-result2))

Submission status: Wrong Answer

Solution code:
#C - Count Order
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = list(range(1,N+1))
count = 0
ans_p = 0
ans_q = 0
for i in permutations(l):
    count += 1
#    print(i)
#    print(P)
    if i == P:
        ans_p = count
    elif i == Q:
        ans_q = count
    else:
        pass
#print(ans_p,ans_q)
print(abs(ans_p - ans_q))

Submission status: Wrong Answer

Solution code:
import itertools

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

all_pattern = list(itertools.permutations([i for i in range(1,N+1)]))
print(all_pattern)

for i, node in enumerate(all_pattern):
  print(node)
  if node == P:
    p = i
  if node ==  Q:
    q = i
    
    
print(abs(p-q))

Submission status: Wrong Answer

Solution code:
N=int(input())
ps=input().replace(" ","")
qs=input().replace(" ","")

stack=[]
stack.append("")
res=[]
while stack:
  d=stack.pop()
  if len(d)==N:
    res.append(d)
    continue
  for i in range(1,N+1):
    if str(i) not in d:
      stack.append(d+str(i))
  
res=res[::-1]
a=0
b=0
for i in range(len(res)):
  if res[i]==ps:
    a=i+1
  elif res[i]==qs:
    b=i+1

print(abs(a-b))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:


def permutation(a):
    n = len(a)
    end = True
    for i in range(n-2, -1, -1):
        if a[i] < a[i + 1]:
            end = False
            break

    if end:
        return []

    pivot = i

    for i in range(n - 1, pivot, -1):
        if a[i] > a[pivot]:
            a[i], a[pivot] = a[pivot], a[i]
            break
    a[pivot + 1:] = a[:pivot:-1]
    return a


n = int(input())
f = list(map(int, input().split()))
e = list(map(int, input().split()))
a = sorted(f)
first = 0
end = 0

p = permutation(a)
i = 0
while p:
    if p == f:
        first = i
    if p == e:
        end = i

    i += 1
    p = permutation(p)

print(abs(end-first))

Submission status: Wrong Answer

Solution code:
import itertools

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

a = 0
b = 0

for i, j in enumerate(itertools.permutations(r, 3)):
    if list(j) == p:
        a = i
    if list(j) == q:
        b = i

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

PP = tuple(P)

A = list(itertools.permutations(range(1,N+1)))
PW = (A.index(P))
QW = (A.index(P))

print(abs(PW-QW))

Submission status: Wrong Answer

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



countPP = 0
countQQ = 0
minN = 0
for i in range(N-1):
  countP = 1
  if P[i]!=minN+1:
    for j in range(N-i-1):
      countP*=j+1
    countP*=P[i]-1
    countPP+=countP
  else:
    minN = P[i]

minN = 0
for i in range(N-1):
  countQ = 1
  if Q[i]!=minN+1:
    for j in range(N-i-1):
      countQ*=j+1
    countQ*=Q[i]-1
    countQQ+=countQ
  else:
    minN = Q[i]

if countPP>countQQ:
  print(countPP-countQQ)
else:
  print(countQQ-countPP)

Submission status: Wrong Answer

Solution code:
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Nlist = [0 for i in range(N)] 
for i in range(N):
    Nlist[i] = i+1
Plist = Nlist[:]
Pcount = [0 for i in range(N)]
Qlist = Nlist[:]
Qcount = [0 for i in range(N)]

for i in range(N):
    if P[i]>= sorted(Plist)[0]:
        #print('P',Plist,Pcount)
        Pcount[i] = P[i]-sorted(Plist)[0]
        Plist[P[i]-1] = 2*N
        
for i in range(N):
    if Q[i]>=sorted(Qlist)[0]:
        #print(Qlist,Qcount)
        Qcount[i] = Q[i]-sorted(Qlist)[0]
        Qlist[Q[i]-1] = 2*N

def fac(num):
    if num == 0 or num == 1:
        return 1
    else:
        return (num*fac(num-1))

countp = 0
countq = 0
for i in range(N):
    if Pcount[i] == 0:
        continue
    else:
        countp += Pcount[i]*fac(N-i-1)
for i in range(N):
    if Qcount[i] == 0:
        continue
    else:
        countq += Qcount[i]*fac(N-i-1)
print(abs(countp-countq))

Submission status: Wrong Answer

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

    elif A[2] == list(B[l]):
      b = l + 1
    else:
      continue
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()))
Nlist = [0 for i in range(N)] 
for i in range(N):
    Nlist[i] = i+1
Plist = Nlist[:]
Pcount = [0 for i in range(N)]
Qlist = Nlist[:]
Qcount = [0 for i in range(N)]

for i in range(N):
    if P[i]>= sorted(Plist)[0]:
        print('P',Plist,Pcount)
        Pcount[i] = P[i]-sorted(Plist)[0]
        Plist[P[i]-1] = 2*N
        
for i in range(N):
    if Q[i]>=sorted(Qlist)[0]:
        print(Qlist,Qcount)
        Qcount[i] = Q[i]-sorted(Qlist)[0]
        Qlist[Q[i]-1] = 2*N

def fac(num):
    if num == 0 or num == 1:
        return 1
    else:
        return (num*fac(num-1))

countp = 0
countq = 0
for i in range(N):
    if Pcount[i] == 0:
        continue
    else:
        countp += Pcount[i]*fac(N-i-1)
for i in range(N):
    if Qcount[i] == 0:
        continue
    else:
        countq += Qcount[i]*fac(N-i-1)
print(abs(countp-countq))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

indp = 1
indq = 1
# 完全一致しているなら比較しない
if p==q:
  print(0)

else:
  # pの辞書式順列番号
  #nums = [i for i in range(1, n+1)]
  for i in range(n):
    c = 1
    for j in p[:i]:
      if (j < p[i]):
        c += 1
    indp += math.factorial((n-1)-i) * (p[i]-c)
    #print(indp) 
  #print('----')
    # qの辞書式順列番号
  #nums = [i for i in range(1, n+1)]
  for i in range(n):
    c = 1
    for j in q[:i]:
      if (j < q[i]):
        c += 1
      
    indq += math.factorial(n-i-1) * (q[i]-c)
    #print(indq)
print(abs(indp-indq))

Submission status: Wrong Answer

Solution code:
N = int(input())
print(N)

Submission status: Wrong Answer

Solution code:
import math

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

def narabi(x):
  num = 0
  for i in range(len(x)-1, 1, -1):
    num += (x[len(x)-1-i] -1)*math.factorial(i)
  return num + 1

print(narabi(b)-narabi(a))

Submission status: Wrong Answer

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

for i in range(a,2,-1):
  d1=C[i-1]*(A[a-i]-1)
  e1=C[i-1]*(B[a-i]-1)
  d=d+d1
  e=e+e1
if A[a-2]==a-1 and A[a-1]==a:
  d+=0
elif A[a-2]<A[a-1]:
  d+=1
else:
  d+=2
if B[a-2]==a-1 and B[a-1]==a:
  d+=0
elif B[a-2]<B[a-1]:
  e+=1
else:
  e+=2




print(abs(d-e))

Submission status: Wrong Answer

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



countPP = 0
countQQ = 0

for i in range(N-1):
  countP = 1
  for j in range(N-i-1):
    countP*=j+1
  countP*=P[i]-i-1
  countPP+=countP

for i in range(N-1):
  countQ = 1
  for j in range(N-i-1):
    countQ*=j+1
  countQ*=Q[i]-i-1
  countQQ+=countQ

print(countPP-countQQ)

Submission status: Wrong Answer

Solution code:
import math

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



P_num = 1
Q_num = 1
true_num = 1
for i in range(N):
    if P[i] > true_num:
        remain_len = N - i - 1
        P_num += math.factorial(remain_len) * (P[i]-1)
        for j in range(i, i+remain_len+1):
            if P[j] > P[i]:
                P[j] -= 1
    else:
        for j in range(i, N):
            P[j] -= 1

true_num = 1
for i in range(N):        
    if Q[i] > true_num:
        remain_len = N - i - 1
        for j in range(i, i+remain_len+1):
            if Q[j] > Q[i]:
                Q[j] -= 1
        Q_num += math.factorial(remain_len) * (Q[i]-1)
        for j in range(i, i+remain_len+1):
            if Q[j] > Q[i]:
                Q[j] -= 1
    else:    
        for j in range(i, N):
            Q[j] -= 1
    

print(abs(Q_num - P_num))

Submission status: Wrong Answer

Solution code:
N = int(input()) 
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = list(range(1,N+1))
Alist=[]
import itertools
for v in itertools.permutations(A):
        Alist.append(v)
a_num = Alist.index(P)+1
b_num = Alist.index(Q)+1
print(b_num - a_num)

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
A = B = 0
for i,v in enumerate(list(itertools.permutations(range(1,N+1))), 1):
    if list(v) == P:
        A = i
    elif list(v) == Q:
        B = i
        break
    else:
        pass

print(abs(A-B) if A != 0 and B != 0 else 0)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
S = raw_input()

state = 0
count = 0
#print S
#print len(S)
for i in range(len(S)):
    if S[i] == 'A':
        state = 1
    elif state == 1 and S[i] == 'B':
        state = 2
    elif state == 2 and S[i] == 'C':
        count += 1; state = 0
    else:
        state = 0
print count

Submission status: Wrong Answer

Solution code:
N=int(input())
P_=list(map(int, input().split()))
Q_=list(map(int, input().split()))
import numpy as np

def stair(N):
  ans=1
  for i in range(1,N+1):
    ans*=i
  return ans

def calc_dict(Q):
  re=list(range(1,len(Q)+1))
  ans=1
  for j in range(len(Q)-1):
    
    if Q[j]!=re[0]:
      a=np.where(np.array(re)<Q[j], 1, 0).sum()
      #print(a)
      ans+=a*stair(len(Q[(j+1):]))
      #print((Q[j]-1)*stair(len(Q[(j+1):])))
      re.remove(Q[j])
    else:
      re.remove(Q[j])
  if ans==0:
    ans+=1
  return ans

Submission status: Wrong Answer

Solution code:
n=int(input())
p=list(map(int,input().split(' ')))
q=list(map(int,input().split(' ')))
for h in range(0,n):
    p[h]=p[h]-1
for h in range(0,n):
    q[h]=q[h]-1
a=0
b=0
l=0
for i in range(0,n):
    while q[i]!=i:
        a=a+1
        for h in range(l,n):
            q[h]=(q[h]+1) %n
    l=l+1
l=0
for i in range(0,n):
    while p[i]!=i:
        b=b+1
        for h in range(l,n):
            p[h]=(p[h]+1) %n
    l=l+1
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
def per(n):
    if n == 0:
        return 1
    return n * per(n-1)

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


for i in range(1,n+1):
    a+=(p[i-1]-1)*per(n-i)

for i in range(1,n+1):
    b+=(q[i-1]-1)*per(n-i)
    
print(a)
print(b)
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
N = int(input())
P = list(map(str,input().split()))
Q = list(map(str,input().split()))
p =""
q =""
a = 0
b = 0

for x,y in zip(P,Q):
  p += x
  q += y

permu = []
count = 0

for i in range(1,N+1):
  for j in range(1,N+1):
    for k in range(1,N+1):
      if a == 0 or b == 0:
        if i != j and i != k and j != k:
          count += 1
          perm = str(i)+str(j)+str(k)
          if perm == p:
            a = count
          if perm == q:
            b = count
        else:
          continue
      else:
        break
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
def gosaikutu(nums):
    numbers = list(range(1, len(nums)+1))
    gosa = [0]*len(nums)
    
    for i,number in enumerate(nums):
        gosa[i] = numbers.index(number)
        numbers.remove(number)
    
    return gosa
    
def nanbanme(gosa):
    nanban = 1
    ikutu = len(gosa)
    
    for j,value in enumerate(gosa):
        k = ikutu - j - 1
        keisuu = k * max(k-1, 1)
        
        if value is not 0:
            nanban += keisuu * value
    return nanban

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

banme1 = nanbanme(gosaikutu(nums2))
banme2 = nanbanme(gosaikutu(nums1))
ans = banme1 - banme2
if ans < 0:
    print(-ans)
else:
    print(ans)

Submission status: Wrong Answer

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

    p = 0
    q = 0
    for i in range(N):
        tmp = N - (i+1)
        p_min = min(P)
        q_min = min(Q)
        p_i = P.pop(0)
        q_i = Q.pop(0)

        p_tmp = tmp*(p_i - p_min)
        q_tmp = tmp*(q_i - q_min)

        if p_tmp > 0:
            p += p_tmp
        if q_tmp > 0:
            q += q_tmp
    print(abs(p-q))

    return 0
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def per(n):
    if n == 0:
        return 1
    return n * per(n-1)

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


for i in range(1,n):
  if p[i-1]-1 !=0 and i!=n-1:
    a+=(p[i-1]-1)*per(n-i)
  else:
    a+=1

for i in range(1,n):
  if q[i-1]-1 !=0 and i!=n-1:
    b+=(q[i-1]-1)*per(n-i)
  else:
    b+=1
    

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from itertools import permutations


def lexicographical_permutation(str):
    perm = sorted(''.join(chars) for chars in permutations(str))
    for x in perm:
        order.append(x)


n = input()
order = []
inputStr = ""
check1 = input()
c1 = 0
c2 = 0
check2 = input()
for i in range(1, int(n)+1):
    inputStr = inputStr + str(i)

lexicographical_permutation(inputStr)

for i in order:
    if i == check1.replace(" ", ""):
        c1 = order.index(i)+1
    elif i == check2.replace(" ", ""):
        c2 = order.index(i)+1

answer = c1 - c2
if answer < 0:
    answer = answer * (-1)
check1 = check1.replace(" ", "")
check2 = check2.replace(" ", "")
print(str(answer))

Submission status: Wrong Answer

Solution code:
n=int(input())
p_str=input().split()
q_str=input().split()
p_int=int(''.join(p_str))
q_int=int(''.join(q_str))
p=list(map(int,p_str))
q=list(map(int,q_str))
ans=0

if p_int <= q_int:
    i=1
    j=1
    while p!=q:  
        if p[n-j]>p[n-j-i]:
            temp=p[n-j]
            p[n-j]=p[n-j-i]
            p[n-i-j]=temp
            ans+=1
            i=1
            j=1
        else:
            i+=1
            if i>=n:
                j+=1
                i=1
else:
    i=1
    j=1
    while p!=q:
        if q[n-j]>q[n-j-i]:
            temp=q[n-j]
            q[n-j]=q[n-j-i]
            q[n-j-i]=temp
            ans+=1
            i=1
            j=1
        else:
            i+=1
            if i>=n:
                j+=1
                i=1
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import re

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

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

conv =[]
for v in itertools.permutations(n_list, n):
    conv.append(v)

p_ind = 0
q_ind = 0
for i, j in enumerate(conv):
    if p == j:
        p_ind = i + 1
    elif q == j:
        q_ind = i + 1

print(abs(p_ind - q_ind))

Submission status: Wrong Answer

Solution code:
def per(n):
    if n == 0:
        return 1
    return n * per(n-1)

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


for i in range(1,n):
  if p[i-1]-1 !=0:
    a+=(p[i-1]-1)*per(n-i)
  else:
    a+=1

for i in range(1,n):
  if q[i-1]-1 !=0:
    b+=(q[i-1]-1)*per(n-i)
  else:
    b+=1
    
print(a)
print(b)

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
import re

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

print(p, q)

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

conv =[]
for v in itertools.permutations(n_list, n):
    conv.append(v)
print(conv)

p_ind = 0
q_ind = 0
for i, j in enumerate(conv):
    print(p, j)
    if p == j:
        p_ind = i + 1
        print('yes')
    elif q == j:
        q_ind = i + 1

print(abs(p_ind - q_ind))

Submission status: Wrong Answer

Solution code:
def cycle(total, now, d):
    if d == now:
        return 0
    elif d < now:
        return total - now + d
    else:
        return d - now

def co(p):
    p = [int(_) for _ in p]
    import math
    n = nn = len(p)
    po = 0
    pd = [_ for _ in range(1, n + 1)]
    fact = math.factorial(n)
    for i in range(n):
        fact //= n
        po += cycle(n, min(pd), pd.index(p[i]) + 1) * fact
        pd.pop(pd.index(p[i]))
        n -= 1
    return po

if __name__ == "__main__":
    import sys
    in_str = []
    for line in sys.stdin:
        in_str.append(line)
    print(abs(co([int(_) for _ in in_str[1].split()]) - co([int(_) for _ in in_str[2].split()])))

Submission status: Wrong Answer

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

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)
    
def check_order(k):
    res = 0
    for _ in range(n):
        if len(k) == 1:
            break
        s = k[0]
        c = 0
        for i in k:
            if s >= i:
                 c += 1
        k.pop(0)
        res += math.factorial(c-1)
    return res

p_order = check_order(p)
q_order = check_order(q)

print(abs(p_order-q_order))