結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
shinchan
|
| 提出日時 | 2019-03-17 18:48:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,471 bytes |
| コンパイル時間 | 1,427 ms |
| コンパイル使用メモリ | 175,596 KB |
| 実行使用メモリ | 37,300 KB |
| 最終ジャッジ日時 | 2024-07-07 14:04:32 |
| 合計ジャッジ時間 | 15,270 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 TLE * 1 -- * 17 |
ソースコード
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
#define era(t) t.erase(unique(be(t)),t.end())
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
typedef long long ll;
using namespace std;
const ll mod=1000000007,mod9=1000000009;
template <class T>inline T lcm(T a,T b){return (a*b/__gcd(a,b));}
queue<pair<int,int> > q;
vector<pair<bool,int> > v;
int ans=1e9;
int n;
void solve(pair<int,int> p){
int maki=p.first;
int num=0;
v[p.first]=make_pair(true,p.second);
if(p.first==n){
ans=min(ans,p.second);
return;
}
while(maki){
if(maki%2==1){
num++;
}
maki/=2;
}
int pm=p.first-num;
int pp=p.first+num;
p.second++;
if(ans>p.second&&pm>0&&!(v[pm].first==true&&v[pm].second<=p.second)){
q.push(make_pair(pm,p.second));
}
if(ans>p.second&&pp<=n&&!(v[pp].first==true&&v[pp].second<=p.second)){
q.push(make_pair(pp,p.second));
}
return;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin>>n;
if(n==1){
cout<<1<<endl;
return 0;
}
for(int i=0;i<=n;i++){
v.pb(make_pair(false,1e9));
}
v[1]=make_pair(true,1);
q.push(make_pair(1,1));
while(q.size()){
solve(q.front());
q.pop();
}
if(v[n].first==true){
cout << ans<<endl;
}
else{
cout << -1<<endl;
}
return 0;
}
shinchan