結果
問題 | No.3 ビットすごろく |
ユーザー |
![]() |
提出日時 | 2021-01-18 21:54:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 715 bytes |
コンパイル時間 | 1,642 ms |
コンパイル使用メモリ | 170,264 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-01 10:02:09 |
合計ジャッジ時間 | 2,655 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>#define rep(i, n) for (int i=0; i<(n); i++)using namespace std;template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }int dp[10101];int main(){int n; cin>>n;rep(i, n+1) dp[i] = 1e9;queue<int> q;q.push(1);dp[1] = 1;while (!q.empty()){int x = q.front(); q.pop();int d = __builtin_popcount(x);if (x+d <= n) if (chmin(dp[x+d], dp[x]+1)) q.push(x+d);if (x-d > 0) if (chmin(dp[x-d], dp[x]+1)) q.push(x-d);}if (dp[n]>=1e9) cout<<-1<<endl;else cout << dp[n] << endl;}