結果

問題 No.1311 Reverse Permutation Index
ユーザー laneguelanegue
提出日時 2020-12-08 09:41:45
言語 Swift
(5.10.0)
結果
AC  
実行時間 8 ms / 1,500 ms
コード長 559 bytes
コンパイル時間 16,530 ms
コンパイル使用メモリ 146,304 KB
実行使用メモリ 8,812 KB
最終ジャッジ日時 2023-10-17 17:01:14
合計ジャッジ時間 17,365 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
8,812 KB
testcase_01 AC 8 ms
8,812 KB
testcase_02 AC 8 ms
8,812 KB
testcase_03 AC 8 ms
8,812 KB
testcase_04 AC 8 ms
8,812 KB
testcase_05 AC 7 ms
8,812 KB
testcase_06 AC 8 ms
8,812 KB
testcase_07 AC 8 ms
8,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let input=readLine()!.split(separator:" ").map{Int($0)!}
var n=input[0]
let s=input[1]
var a:[Int]=[]
for i in 1...s{
  a.append(n%i)
  n/=i
  //print(n,f)
}
//print(a)
var l:[Int]=[]
for i in 1...s{
  l.append(i)
}
var perm:[Int]=[]
while !a.isEmpty{
  perm.append(l.remove(at:a.removeLast()))
}
//print(perm)
var inv=Array(repeating:0,count:s)
for i in 0..<s{
  inv[perm[i]-1]=i+1
}
//print(inv)
l=[]
var f=1
for i in 1...s{
  l.append(i)
  f*=i
}
var r=0
for i in 0..<s{
  let idx=l.firstIndex(of:inv[i])!
  f/=s-i
  r+=idx*f
  l.remove(at:idx)
}
print(r)
0