結果

問題 No.3 ビットすごろく
ユーザー jjjj
提出日時 2016-07-23 06:02:05
言語 Fortran
(gFortran 13.2.0)
結果
MLE  
実行時間 -
コード長 1,359 bytes
コンパイル時間 651 ms
コンパイル使用メモリ 31,104 KB
実行使用メモリ 816,640 KB
最終ジャッジ日時 2024-04-24 07:12:16
合計ジャッジ時間 2,519 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

logical function is_reacheable(n)
  implicit none
  integer::i, n
  logical::is_reacheablef=.false.

  if (n.eq.1) then
     is_reacheablef = .true.
     return
  end if

  do i=max(1,n-(31-LEADZ(10000))), n
     if (n.eq.POPCNT(i)+i) then
        is_reacheable = .true.
        exit
     end if
  end do
end function is_reacheable

recursive function progress(pos, goal, step) result(total_step)
  implicit none
  logical,save::reached=.false.
  integer::pos, goal, step
  integer::popcount
  integer::total_step

  if (pos .eq. goal) then
     total_step = step
     return
  else
     popcount = POPCNT(pos)
     if (pos + popcount .le. goal) then
        total_step = progress(pos + popcount, goal, step + 1)
     else if (pos - popcount .ge. 1) then
        total_step = progress(pos - popcount, goal, step + 1)
     end if
  end if

end function progress

program main
  implicit none
  interface
     logical function is_reacheable(n)
       integer::n
     end function is_reacheable
     recursive function progress(pos, goal, step) result(total_step)
       integer::pos, goal, step
       integer::total_step
     end function progress
  end interface
  integer::N,total_step
  read *,N

  if(is_reacheable(N).eqv..false.) then
     print '(a)',"-1"
     return
  endif

  total_step = progress(1, N, 1)

  print '(i0)', total_step
end program main
0