結果

問題 No.3 ビットすごろく
ユーザー saio4016
提出日時 2019-02-16 17:18:39
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 857 bytes
コンパイル時間 1,227 ms
コンパイル使用メモリ 159,208 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 09:14:50
合計ジャッジ時間 2,241 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int,int> P;
 
const int INF = 1e9;
const int mod = 1e9+7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
 
int dp[10010];
int cnt[10010];

int main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) dp[i] = INF;
    for(int i = 1; i <= n; i++){
        int tmp = i;
        while(tmp){
            if(tmp&1) cnt[i]++;
            tmp /= 2;
        }
    } 
    dp[1] = 1;
    for(int j = 0; j < 100; j++){
        for(int i = 1; i <= n; i++){
            if(dp[i] != INF){
                if(i+cnt[i] <= n) dp[i+cnt[i]] = min(dp[i]+1,dp[i+cnt[i]]);
                if(i-cnt[i] > 0) dp[i-cnt[i]] = min(dp[i]+1,dp[i-cnt[i]]);
            }
        }
    }
    cout << (dp[n] != INF ? dp[n] : -1) << endl;
    return 0;
}
0