結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2019-07-25 10:44:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 218 ms / 5,000 ms |
コード長 | 932 bytes |
コンパイル時間 | 1,577 ms |
コンパイル使用メモリ | 167,476 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 09:26:59 |
合計ジャッジ時間 | 5,156 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(i,n) for(int i=0;i<(n);i++) #define MOD 1000000007 #define all(x) (x).begin(),(x).end() template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;} template<class T> inline T LCM(T a,T b){return a/GCD(a,b)*b;} int n; int dp[10010]; int sht(int num){ int s=num; int cnt=0; while(s>0){ if(s&1)cnt++; s>>=1; } return cnt; } void d(int a){ int b=sht(a); if(a-b>0){ if(dp[a-b]>dp[a]+1){ dp[a-b]=dp[a]+1; d(a-b); } } if(a+b<n+1){ if(dp[a+b]>dp[a]+1){ dp[a+b]=dp[a]+1; d(a+b); } } } int main(){ FOR(i,10005){ dp[i]=MOD; } cin >> n; dp[1]=1; d(1); if(dp[n]==MOD){ cout << -1 << endl; }else{ cout << dp[n] << endl; } }