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:
from itertools import permutations
import math

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

np = int(''.join(P))
nq = int(''.join(Q))

lst = []
lstn = ['1', '2', '3', '4', '5', '6', '7', '8']

for i in permutations(lstn[:N]):
    lst.append(int(''.join(i)))
lst.sort()

for i in range(1, math.factorial(N)):
    if lst[i] == np: np = i
    if lst[i] == nq: nq = i

print(abs(np - nq))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P,Q = input().rstrip().split(" "),input().rstrip().split(" ")
 
for i in range(N):
  P[i],Q[i] = int(P[i]),int(Q[i])
 
seq=list(range(1,N+1))
a = list(itertools.permutations(seq, N))
 
for i in range(len(a)):
  if a[i] == tuple(P):
    pindex= i+1
  if a[i] == tuple(Q):
    qindex= i+1

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()))
 
a,b = -1,-1
for i,x in enumerate(permutations([i+1 for i in range(n)])):
    c,d = True,True
    for j in range(n):
        if x[j] != p[j]:
            c = False
        if x[j] != q[j]:
            d = False
    if c:a = i+1
    if d:b = i+1

Submission status: Wrong Answer

Solution code:
import math
 
N = int(input())
Pi = list(map(int,input().split()))
Qi = list(map(int,input().split()))
 
a = 0
b = 0
 
numcheck = [1 for i in range(N)]
numcheck[Pi[0]-1] = 0
a = math.factorial(N-1)*(Pi[0]-1) 
for n in range(1,N):
  num = Pi[n]
  numcheck[num-1] = 0

Submission status: Wrong Answer

Solution code:
import math
 
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
 
a = 0
b = 0
 
numcheck = [1 for i in range(N)]
numcheck[P[0]-1] = 0
a = math.factorial(N-1)*(P[0]-1) 
for n in range(1,N):
  num = P[n]
  numcheck[num-1] = 0

Submission status: Wrong Answer

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

for i, v in enumerate(itertools.permutations(range(1,n+1))):
    if p==v:
        s=i
    if q==v:
        t=i
print(abs(s-t))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
l = tuple(x+1 for x in range(n))
#print(a,b,l)
c,d = 0,0
cnt = 0
from itertools import permutations
for k in permutations(l):
    #print(k)
    cnt += 1
    if a ==k:
        c = cnt
    elif b ==k:
        d = cnt
print(abs(c-d))

Submission status: Wrong Answer

Solution code:
import itertools

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

list_range = list(range(1,n+1))
pattern_list = list(itertools.permutations(list_range, n))
pattern_list.sort()
ans = [0] * 2
for i in range(len(pattern_list)):
  if p == list(pattern_list[i]):
    ans[0] = i + 1
  elif q == list(pattern_list[i]):
    ans[1] = i + 1
print(abs(ans[0] - ans[1]))

Submission status: Wrong Answer

Solution code:
import itertools

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

list_range = list(range(1,n+1))
pattern_list = list(itertools.permutations(list_range, n))
pattern_list.sort()
ans = [0] * 2
for i in range(len(pattern_list)):
  if p == list(pattern_list[i]):
    ans[0] = i
  elif q == list(pattern_list[i]):
    ans[1] = i
print(abs(ans[0] - ans[1]))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
v = P[:]
v.sort()
p = tuple(P)
q = tuple(Q)
ans_a = 0
ans_b = 0
i = 0
for l in itertools.permutations(v):
    if p == l:
        ans_a = i
        i += 1
    elif q == l:
        ans_b = i
        i += 1
    else:
        i += 1
print(abs(ans_a - ans_b))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
v = P[:]
v.sort()
p = tuple(P)
q = tuple(Q)
ans_a = 0
ans_b = 0
i = 1
for l in itertools.permutations(v):
    if p == l:
        ans_a = i
        i += 1
    elif q == l:
        ans_b = i
        i += 1
    else:
        i += 1
print(abs(ans_a - ans_b))

Submission status: Wrong Answer

Solution code:
import itertools

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

if N == Q:
    print(0)
else:
    l = []
    for i in range(1, N+1):
        l.append(i)

    t = tuple(l)
    p = 0
    q = 0
    for i, v in enumerate(itertools.permutations(t)):
        if P == v:
            p = i
        elif Q == v:
            q = i
    print(abs(p - q))

Submission status: Wrong Answer

Solution code:
#16　AtCoder Beginner Contest 150 C - Count Order
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
ans = 0
j = 0
for i in itertools.permutations(range(1, n+1)):
    j += 1
    if p == i: ans += j
    elif q == i: ans -= j
print(abs(ans))

Submission status: Wrong Answer

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

dic = itertools.permutations(range(1, N + 1), N)
count = [False, 0]
for i in dic:
    if list(i) == P or list(i) == Q:
        if count[0]:
            break
        else:
            count[0] = True
    if count[0]:
        count[1] += 1

print(count[1])

Submission status: Wrong Answer

Solution code:
# import math
# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
from itertools import permutations
# import numpy as np


# k,x = map(int, input().split())

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
m = [i + 1 for i in range(n)]
c = 0
for i in permutations(m):
    c += 1
    print(i)
    if list(i) == p:
        a = c
    if list(i) == q:
        b = c
print(abs(a - b))

Submission status: Wrong Answer

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

p = [int(s) for s in input().split()]
q = [int(s) for s in input().split()]

num_p = 1
for i in range(n):
    cnt = 0
    for j in range(i+1,n):
        if(p[i] > p[j]):
            cnt += 1
    
    num_p += math.factorial(n-i-1)*cnt

num_q = 1
for i in range(n):
    cnt = 0
    for j in range(i+1,n):
        if(q[i] > q[j]):
            cnt += 1
    #print(cnt)
    num_q += math.factorial(n-i-1)*cnt
    
#print(num_p)
#print(num_q)
print(num_p-num_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, b = 0, 0
perms = list(permutations(range(1, N + 1)))

for per in perms:
    if P == per:
        a = perms.index(per) + 1
    elif Q == per:
        b = perms.index(per) + 1
    if a != 0 and b != 0:
        break
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())
print(P)

P = tuple(P)
Q = tuple(Q)
print(P) 
dlist = range(1, N+1)
permlist = list(itertools.permutations(dlist))

a = 0
b = 0
for i, p in enumerate(permlist):
	if P == p:
		a = i
	if Q == p:
		b = i
    
print(abs(a-b))

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()))
if p == q:
    print(0)
    exit()
base = sorted(p)
pi = 0
qi = 0
for i, v in enumerate(permutations(base, n)):
    print(v)
    if p == v:
        pi = i + 1
    elif q == v:
        qi = i + 1
        
    if pi * qi != 0:
        break
print(abs(pi-qi))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
A  = tuple(map(int,input().split()))
B  = tuple(map(int,input().split()))
Z = list(itertools.permutations(range(1,n+1)))
a = 0
b = 0
for i,z in enumerate(Z):
  #print(z,A)
  if(z == A):
    a = i+1
  elif(z == B):
    b = i+1
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
A  = tuple(map(int,input().split()))
B  = tuple(map(int,input().split()))
Z = list(itertools.permutations(range(1,n+1)))
a = 0
b = 0
for i,z in enumerate(Z):
  #print(z,A)
  if(z == A):
    a = i
  elif(z == B):
    b = i
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import itertools

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

X = sorted(list(itertools.permutations(P)))

a = 0
b = 0

for i, v in enumerate(X):
    if v == P:
        a = i
    elif v == Q:
        b = i

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import itertools

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

X = sorted(P)

a = 0
b = 0

for i, v in enumerate(itertools.permutations(X)):
    if v == P:
        a = i
    elif v == Q:
        b = i

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import sys

import math
import itertools

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
p = na()
q = na()
l = list(itertools.permutations(list(range(1, n+1))))
print(abs(l.index(tuple(p))) - abs(l.index(tuple(q))))

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = tuple(map(lambda x: int(x)-1, input().split()))
Q = tuple(map(lambda x: int(x)-1, input().split()))
permu_list = itertools.permutations(range(N))
p,q = 0,0
c = 0
for l in sorted(permu_list):
  if l == P:
    p = c
  elif l == Q:
    q = c
  c += 1
print(abs(p-q))

Submission status: Wrong Answer

Solution code:
n=int(input())
P=[int(i) for i in input().split()]
Q=[int(i) for i in input().split()]
import itertools
ct=1
for c in itertools.permutations(range(n),n):
  c=list(c)
  for i in range(n):
    if P[i]-1==c[i]:
      continue
    else:
      break
  else:
    a=ct
  for i in range(n):
    if Q[i]-1==c[i]:
      continue
    else:
      break
  else:
    b=ct
  ct+=1
print(a-b)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
# import numpy as np
from functools import reduce
# import statistics
import heapq
import collections
import itertools
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = []
for i in range(2):
    input_data = input().split()
    tmp = [int(n) for n in input_data]
    data.append(tuple(tmp))

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

index_d0 = -1
index_d1 = -1

for i in range(len(perm)):
    if perm[i] == data[0]:
        index_d0 = i+1
    elif perm[i] == data[1]:
        index_d1 = i+1
    if index_d0 > -1 and index_d1 > -1:
        break

smaller_num = min(index_d0, index_d1)
larger_num = max(index_d0, index_d1)

ans = abs(smaller_num - larger_num)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
from collections import deque
def main():
    n = int(input())
    p = [int(t)for t in input().split()]
    q = [int(t)for t in input().split()]

    p_num = 0
    q_num = 0

    fact = [1]*n

    for i in range(n):
        fact[i] = fact[i-1]*(i+1)

    fact = fact[::-1]
    fact.append(1)

    print(fact)

    temp = set([])
    for i, p_ in enumerate(p):
        t = p_-len([x for x in temp if x<p_])-1
        p_num += fact[i+1]*t
        temp.add(p_)

    temp = set([])
    for i, q_ in enumerate(q):
        t = q_-len([x for x in temp if x<q_])-1
        q_num += fact[i+1]*t
        temp.add(q_)

    print(abs(p_num-q_num))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

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

A, B = 0, 0
for i, p in enumerate(l):
    if p == a:
        A = i
    if p == b:
        B = i

print(abs(A-B))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
l=list(itertools.permutations(range(1,n+1),n))

a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
a_index=0
b_index=0

for i in range(len(l)):
    if l[i]==a:
        a_index=i+1
    elif l[i]==b:
        b_index=i+1
    else:
        continue

result=abs(a_index-b_index)
print(result)

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
l=list(itertools.permutations(range(1,n+1),n))

a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
a_index=0
b_index=0

for i in range(len(l)):
    print(l[i])
    if l[i]==a:
        a_index=i+1
    elif l[i]==b:
        b_index=i+1
    else:
        continue

result=abs(a_index-b_index)
print(result)

Submission status: Wrong Answer

Solution code:
import itertools
N = int(input())
P = [ int(i) for i in input().split()]
Q = [ int(i) for i in input().split()]

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

print(abs(p-q))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
diff = 0
for i in range(n-1):
  diff += (p[i] - q[i] ) * math.factorial(n-i-1)

print(abs(diff))

Submission status: Wrong Answer

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

from itertools import permutations

order = list(range(1, n+1))
a = 0
b = 0
cnt = 0
flag = False
for i in permutations(order, n):
    if list(i) == p:
        a = cnt
    elif list(i) == q:
        b = cnt
    cnt += 1

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

a, b = 0, 0
for i, _p in enumerate(itertools.permutations(list(range(1, n+1)))):
    if p == _p:
        a = i
    elif q == _p:
        b = i

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()

p=limss()
q=limss()

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

pans=0
qans=0

for i,j in enumerate(range(factorial(n))):

    if "".join(p)=="".join(list(map(str,arr[i]))):
        pans=j
    elif "".join(q)=="".join(list(map(str,arr[i]))):
        qans=j
    
print(abs(pans-qans))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

n = int(input())
p = tuple(map(lambda x: int(x) - 1, input().split()))
q = tuple(map(lambda x: int(x) - 1, input().split()))

perm = list(permutations(range(n)))
print(perm.index(p) - perm.index(q))

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
lists=[]
listt=[]
sums=0
sumt=0

def rank(n,l):
  for i in range(len(l)):
    if n==l[i]:
      l.remove(l[i])
      return i+1
    
for i in range(n):
  lists.append(i+1)
  listt.append(i+1)
for i in range(n):
  sums+=(rank(s[i],lists)-1)*(math.factorial(7-i))
  sumt+=(rank(t[i],listt)-1)*(math.factorial(7-i))
if sums<sumt:
  tmp=sums
  sums=sumt
  sumt=tmp
print(sums-sumt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def main():
    N = int(input())
    p = tuple(map(int,input().split()))
    q = tuple(map(int,input().split()))
    p_i = 0
    q_i = 0
    perm = list(permutations(list(range(1,N+1))))
    for i in range(len(perm)):
        print(perm[i])
        if perm[i] == p:
            p_i = i
        if perm[i] == q:
            q_i = i
    print(abs(p_i-q_i))
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    N = int(input())
    p = tuple(map(int,input().split()))
    q = tuple(map(int,input().split()))
    p_i = 0
    q_i = 0
    perm = list(permutations(list(range(1,N+1))))
    for i in range(len(perm)):
        if perm[i] == p:
            p_i = i
        elif perm[i] == q:
            q_i = i
    print(abs(p_i-q_i))
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools
N=int(input())
I=[str(i) for i in range(1,N+1)]
P=''.join(input().split())
Q=''.join(input().split())
li=list(itertools.permutations(I,N))

a=0
b=0
for i in range(len(li)):
    if(''.join(li[i])==P):
        a=i+1
    elif(''.join(li[i])==Q):
        b=i+1
print(abs(b-a))

Submission status: Wrong Answer

Solution code:
import itertools
N=int(input())
I=[str(i) for i in range(1,N+1)]
P=''.join(input().split())
Q=''.join(input().split())
li=list(itertools.permutations(I,N))

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

Submission status: Wrong Answer

Solution code:
import itertools


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

l = list(itertools.permutations(p))

l.sort()

p_i = 0
q_i = 0

for i in range(len(l)):
    if list(l[i]) == p:
        p_i = i
    elif list(l[i]) == q:
        q_i = i

print(abs(p_i - q_i))

Submission status: Wrong Answer

Solution code:
import itertools

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

count = 0
p_count = 0
q_count = 0

for v in itertools.permutations(sorted(p), n):
    if list(v) == p:
        p_count = count
    elif list(v) == q:
        q_count = count

    count += 1

print(abs(p_count - q_count))

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

ret = [i for i in range(1, n + 1)]
a = 0
b = 0
perm_list = permutations(ret)
for i, j in enumerate(perm_list):
    if p == list(j):
        a = i
    elif q == list(j):
        b = i

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

perm_list = list(permutations(sorted(p)))
a, b = 0, 0
for i in range(len(perm_list)):
    if p == list(perm_list[i]):
        a = i
    elif q == list(perm_list[i]):
        b = i
    else:
        pass
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
#C - Count Order
import math
import itertools

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

li = list(itertools.permutations(P,N))
li.sort()

a = 0
b = 0

for i in range(math.factorial(N)):
    if li[i] == P:
        a = i
    elif li[i] == Q:
        b = i
    else:
        a = a
        b = b
        
print(abs(a-b))

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

left = 0
r = 0
gen = [x for x in range(1, N+1)]
i = 1
for perm in permutations(gen):
    if perm == P:
        left = i
    elif perm == Q:
        r = i
    i += 1

print(abs(left-r))

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

left = 0
r = 0

for idx, perm in enumerate(permutations(range(1, N+1), N)):
    if perm == P:
        left = idx
    elif perm == Q:
        r = idx

print(abs(left-r))

Submission status: Wrong Answer

Solution code:
import itertools

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

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

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
p = input().replace(" ", "")
q = input().replace(" ", "")
m = ""
for i in range(1, n + 1):
    m += str(i)
anagram_list = ["".join(v) for v in itertools.permutations(m)]

pa = 0
qa = 0

for i in range(len(anagram_list)):
    if anagram_list[i] == p:
        pa = i
    elif anagram_list[i] == q:
        qa = i
print(abs(pa-qa))

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
k = list(itertools.permutations(range(1,n+1), n))
ans_1 = 0
ans_2 = 0
for i in range(len(k)):
    if k[i] == a:
        ans_1 = i
    elif k[i] == b:
        ans_2 = i
print(abs(ans_1 - ans_2))

Submission status: Wrong Answer

Solution code:
N = input()
S = input()
ABCCount  = 0

for n in range(int(N)):
    if S[n] == "A" and S[n+1] == "B" and S[n+2] == "C":
        ABCCount = ABCCount + 1

print(ABCCount)

Submission status: Wrong Answer

Solution code:
N = input()
S = input()
ABCCount  = 0

for n in range(int(N)-2):
    if S[n] == "A" and S[n+1] == "B" and S[n+2] == "C":
        ABCCount = ABCCount + 1

print(ABCCount)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
p_que = deque(map(int, input().split()))
q_que = deque(map(int, input().split()))

que = deque(i for i in range(1, n+1))
cnt = 0
a = 0
b = 0

def dfs(d, q):
  # print('-'*10, q)
  # sorted(q)
  for i in list(q):
    # print(i, d, q)
    d.append(i)
    q.remove(i)
    # print('>', d, q)
    if q == deque([]):
      global cnt
      cnt += 1
      # print('>>>', cnt, d, q)
      if d == p_que:
        global a
        a = cnt
      if d == q_que:
        global b
        b = cnt
      # print('return')
      d.remove(i)
      q.appendleft(i)
      return
    # print('tansaku')
    dfs(d, q)
    # print('>returned')
    print('---', i, d, q)
    d.remove(i)
    if i > q[0]:
      q.append(i)
    else:
      q.appendleft(i)
    sorted(q)

dfs(deque(), que)
# print(a, b)
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
p_que = deque(map(int, input().split()))
q_que = deque(map(int, input().split()))

que = deque(i for i in range(1, n+1))
cnt = 0
a = 0
b = 0

def dfs(d, q):
  # print('-'*10, q)
  for i in list(q):
    # print(i, d, q)
    d.append(i)
    q.remove(i)
    # print('>', d, q)
    if q == deque([]):
      global cnt
      cnt += 1
      # print(d, p_que)
      if d == p_que:
        global a
        a = cnt
      if d == q_que:
        global b
        b = cnt
      # print('return')
      d.remove(i)
      q.append(i)
      return
    # print('tansaku')
    dfs(d, q)
    # print('>returned')
    # print('---', i, d, q)
    d.remove(i)
    q.append(i)

dfs(deque(), que)
print(abs(a-b))

Submission status: Wrong Answer

Solution code:
def fact(n):
  ret=1
  for i in range(2,n+1):
    ret=ret*i
  return ret

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
cnt1=0
cnt2=0
for i in range(n):
  cnt1+=(arr1[i]-1)*fact(n-i-1)
  cnt2+=(arr2[i]-1)*fact(n-i-1)
print(abs(cnt1-cnt2))

Submission status: Wrong Answer

Solution code:
import itertools

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

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

print(abs(pindex-qindex))

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, b = 0, 0

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

print(abs(a-b))

Submission status: Wrong Answer

Solution code:
def permutationIndex(A):
  if A is None or len(A) ==0:
    return 0
  index = 1
  factor = 1
  for i in range(len(A)-1,-1,-1):
    rank = 0
    for j in range(i+1,len(A)):
      if A[i] > A[j]:
        rank+=1
    index += rank*factor
    factor *= (len(A)-i)
    return index
n = int(input())
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
print(abs(permutationIndex(p)-permutationIndex(q)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_value_list = []
a_real_list = [1]
a_real = []
b_value_list = []
b_real_list = [1]
b_real = []
a_ans = 1
b_ans = 1
c = []

#value_list
for i in range(n):
    a_value_list.append(1)
    b_value_list.append(1)

""""
x = a[0]-1
value_list[x] = 0
print(value_list)
"""
#rael_list
for i in range(n):
    x = i + 1
    a_real_list.append(a_real_list[i]*x)
    a_real.append(x)
    b_real_list.append(b_real_list[i] * x)
    b_real.append(x)

a_real_list.pop(0)
b_real_list.pop(0)

for i in range(n-1):
    m = n-i-1
    x = a[i] - 1
    a_value_list[x] = 0
    c = a_value_list[:x+1]
    y = sum(c)
    a_ans += a_real_list[m]*y/a_real[m]


for i in range(n-1):
    m = n-i-1
    x = b[i] - 1
    b_value_list[x] = 0
    c = b_value_list[:x+1]
    y = sum(c)
    b_ans += b_real_list[m]*y/b_real[m]

print(abs(a_ans-b_ans))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N = int(input())
P = list(map(int, input().split()))
S = list(map(int, input().split()))
J = list(permutations(sorted(S) , N))

print(J.index(tuple(P)) - J.index(tuple(S)))

Submission status: Wrong Answer

Solution code:
from itertools import permutations

N = int(input())
p = q = 0

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

for idx, a in enumerate(permutations([i for i in range(1, N+1)])):
    if P == a: p = idx
    elif Q == a: q = idx

print(abs(p-q))

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
P,Q = sys.stdin.readlines()

print(0)

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
seq=range(1,n+1)
l=list(itertools.permutations(seq))
#print(l[0])

a_num=0
b_num=0
for i in range(len(l)):
  if l[i]==a:
    a_num=i
  elif l[i]==b:
    b_num=i
#print(a_num)
print(abs(a_num-b_num))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
seq=range(n)
l=list(itertools.permutations(seq))
#print(l[0])
a_num=0
b_num=0
for i in range(len(l)):
  if l[i]==a:
    a_num=i
  elif l[i]==b:
    b_num=i
print(abs(a_num-b_num))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
N = int(input())
A = np.array(list(input().split()))
B = np.array(list(input().split()))
a = 0
b = 0 
for i in range(N):
    arank = np.where(A[i:].argsort()==0)[0]
    brank = np.where(B[i:].argsort()==0)[0]
    a += arank*math.factorial(N-(i+1))
    b += brank*math.factorial(N-(i+1))
    print(a,b,arank,brank,A[i:].argsort(),B[i:].argsort())
print(int(abs(a-b)))

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
N = int(input())
A = np.array(list(input()))
B = np.array(list(input()))
a = 0
b = 0 
for i in range(N):
    arank = np.where(A[i:].argsort()==0)[0]
    brank = np.where(B[i:].argsort()==0)[0]
    a += arank*math.factorial(N-(i+1))
    b += brank*math.factorial(N-(i+1))
    print(a,b,arank,brank,A[i:].argsort(),B[i:].argsort())
print(int(abs(a-b)))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()

print(s.count("ABC"))