結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  fushime2 | 
| 提出日時 | 2016-09-15 13:17:19 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 500 ms / 5,000 ms | 
| コード長 | 1,026 bytes | 
| コンパイル時間 | 1,273 ms | 
| コンパイル使用メモリ | 159,936 KB | 
| 実行使用メモリ | 394,664 KB | 
| 最終ジャッジ日時 | 2024-07-01 08:05:51 | 
| 合計ジャッジ時間 | 8,461 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:39:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
            
            ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))
const int INF = 114514810;
int cost[10010][10010];
bool used[10010];
int d[10010];
int n;
int calc(int n) {
    int res = 0;
    while(n) {
        if(n & 1) res++;
        n >>= 1;
    }
    return res;
}
void dijkstra(int s) {
    ZERO(used);
    fill(d,d+n+1,INF);
    d[s] = 0;
    while(1) {
        int v = -1;
        FOR(u,1,n+1) {
            if(!used[u] and (v == -1 or d[u] < d[v])) v = u;
        }
        if(v == -1) break;
        used[v] = 1;
        FOR(u,1,n+1) d[u] = min(d[u], d[v] + cost[v][u]);
    }
}
int main() {
    scanf("%d",&n);
    rep(i,n+1) rep(j,n+1) cost[i][j] = INF;
    vector<int> a(n+1);
    rep(i,n+1) a[i] = calc(i);
    FOR(i,1,n+1) {
        if(i + a[i] <= n) cost[i][i+a[i]] = 1;
        if(i - a[i] >= 1) cost[i][i-a[i]] = 1;
    }
    dijkstra(1);
    cout << (d[n]>=INF ? -1 : d[n] + 1) << "\n";
    return 0;
}
            
            
            
        