結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-01-14 00:12:43 |
| 言語 | Perl (5.40.0) |
| 結果 |
AC
|
| 実行時間 | 32 ms / 5,000 ms |
| コード長 | 1,230 bytes |
| コンパイル時間 | 206 ms |
| コンパイル使用メモリ | 6,688 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-01 07:10:18 |
| 合計ジャッジ時間 | 1,811 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
コンパイルメッセージ
Main.pl syntax OK
ソースコード
# 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;
}