結果

問題 No.3 ビットすごろく
ユーザー chrom_shchrom_sh
提出日時 2015-01-14 00:12:43
言語 Perl
(5.38.2)
結果
AC  
実行時間 30 ms / 5,000 ms
コード長 1,230 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 5,208 KB
実行使用メモリ 5,932 KB
最終ジャッジ日時 2023-09-13 22:58:37
合計ジャッジ時間 2,462 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,084 KB
testcase_01 AC 5 ms
5,068 KB
testcase_02 AC 5 ms
4,976 KB
testcase_03 AC 10 ms
5,128 KB
testcase_04 AC 6 ms
5,188 KB
testcase_05 AC 17 ms
5,584 KB
testcase_06 AC 11 ms
5,216 KB
testcase_07 AC 8 ms
5,044 KB
testcase_08 AC 15 ms
5,524 KB
testcase_09 AC 22 ms
5,572 KB
testcase_10 AC 25 ms
5,660 KB
testcase_11 AC 20 ms
5,652 KB
testcase_12 AC 17 ms
5,520 KB
testcase_13 AC 9 ms
5,280 KB
testcase_14 AC 25 ms
5,748 KB
testcase_15 AC 30 ms
5,912 KB
testcase_16 AC 27 ms
5,932 KB
testcase_17 AC 29 ms
5,736 KB
testcase_18 AC 8 ms
5,144 KB
testcase_19 AC 29 ms
5,776 KB
testcase_20 AC 5 ms
4,964 KB
testcase_21 AC 4 ms
5,176 KB
testcase_22 AC 25 ms
5,752 KB
testcase_23 AC 29 ms
5,888 KB
testcase_24 AC 30 ms
5,896 KB
testcase_25 AC 29 ms
5,792 KB
testcase_26 AC 4 ms
5,076 KB
testcase_27 AC 9 ms
5,252 KB
testcase_28 AC 25 ms
5,904 KB
testcase_29 AC 20 ms
5,628 KB
testcase_30 AC 5 ms
5,208 KB
testcase_31 AC 6 ms
5,084 KB
testcase_32 AC 18 ms
5,500 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.pl syntax OK

ソースコード

diff #

# http://yukicoder.me/problems/11
use strict;
use warnings;
use 5.010;


my $n   = <STDIN>;
chomp($n);

say solve($n);


sub solve {
    my ($n) = @_;

    return 1 if $n == 1;

    my $move    = 1;
    my $cur     = 1;
    my $shortest    = {
        1   => 1,
    };

    my $points  = [1];
    my $ans = -1;
    while (my $p = shift(@$points)) {
        my $step    = bi_count($p);
        my $cnt     = $shortest->{ $p };
        #say "  $p, step = $step";                                                                      

        if ($p + $step == $n) {
            $ans    = $cnt + 1;
            last;
        }

        if (not $shortest->{ $p + $step } and $p + $step < $n) {
            $shortest->{ $p + $step }   = $cnt + 1;
            push @$points, $p + $step;
        }
        if (not $shortest->{ $p - $step } and $p - $step > 0) {
            $shortest->{ $p - $step }   = $cnt + 1;
            push @$points, $p - $step;
        }
    }
    return $ans;

}
sub bi_count {
    my ($num)   = @_;

    my $cnt = 0;
    while ($num > 1) {
        if ($num % 2) {
            $cnt++;
            $num    = ($num-1)/2;
        }
        else {
            $num    = $num/2;
        }
    }
    return $cnt+1;
} 
0