結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2017-10-05 09:56:43 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,326 bytes |
コンパイル時間 | 815 ms |
コンパイル使用メモリ | 86,584 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 20:58:17 |
合計ジャッジ時間 | 2,121 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 RE * 1 |
ソースコード
#include <iostream> #include <vector> #include <string> #include <cstring> #include <math.h> #include <cmath> #include <limits.h> #include <map> #include <set> #include <queue> #include <algorithm> #include <functional> #include <stdio.h> using namespace std; long long MOD = 1000000007; int bitcount( int a ) { int ret = 0; for ( int i = 0; (1 << i ) <= a; i++ ) { if ((1<<i) & a) { ret++; } } return ret; } int main() { int N; cin >> N; vector< vector<int> > path(N+1); vector<int> dist(N+1); fill( dist.begin(), dist.end(), -1 ); dist[1] = 1; for ( int i = 1; i <= N; i++ ) { int b = bitcount(i); if ( i+b <= N ) { path[i].push_back(i+b); } if ( i-b >= 1 ) { path[i].push_back(i-b); } } queue<int> Q; Q.push( path[1][0] ); dist[path[1][0]] = dist[1]+1; while ( !Q.empty() ) { int c = Q.front(); Q.pop(); if ( c == N ) { break; } for ( int i = 0; i < path[c].size(); i++ ) { if ( dist[path[c][i]] == -1 ) { Q.push( path[c][i] ); dist[path[c][i]] = dist[c]+1; // cout << dist[path[c][i]] << endl; } } } cout << dist[N] << endl; return 0; }