結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
kazuppa
|
| 提出日時 | 2024-07-29 12:23:28 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 793 bytes |
| コンパイル時間 | 4,608 ms |
| コンパイル使用メモリ | 279,984 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-29 12:23:34 |
| 合計ジャッジ時間 | 6,123 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
//dist[i]=移動手数
//現段階で行けないときは-1
vector<int> dist(n+1,-1);
dist[1]=1;
queue<int> que;
que.push(1);
while(!que.empty()){
int u=que.front();que.pop();
//ここからは遷移
//bit.count()=bitの桁が1になっている数==__builtinpop.count
//uから1手で行けるのはu+bit.count()かu-bit.count()
if(u+__builtin_popcount(u)<=n&&dist[u+__builtin_popcount(u)]==-1){
dist[u+__builtin_popcount(u)]=dist[u]+1;
que.push(u+__builtin_popcount(u));
}
if(u-__builtin_popcount(u)>0&&dist[u-__builtin_popcount(u)]==-1){
dist[u-__builtin_popcount(u)]=dist[u]+1;
que.push(u-__builtin_popcount(u));
}
}
cout<<dist[n]<<endl;
}
kazuppa