結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-01-02 21:45:22 | 
| 言語 | C (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 8 ms / 5,000 ms | 
| コード長 | 1,070 bytes | 
| コンパイル時間 | 528 ms | 
| コンパイル使用メモリ | 29,952 KB | 
| 実行使用メモリ | 6,948 KB | 
| 最終ジャッジ日時 | 2024-07-01 09:36:46 | 
| 合計ジャッジ時間 | 1,627 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <stdio.h>
#include <stdlib.h>
int bit(int a);
int main(void) {
    int N;
    int s = 1; // step
    int c = 0; // check
    scanf("%d", &N);
    int math[10001] = {0};
    math[1] = 1;
    if (N == 1) {
        printf("1\n");
        exit(EXIT_SUCCESS);
    }
    for (; ; s++) {
        c = 0;
        for (int i = 1; i <= N; i++) {
            if (math[i] == s) {
                int d = bit(i); // distance
                if (i+d == N) {
                    printf("%d\n", s+1);
                    exit(EXIT_SUCCESS);
                }
                if (i+d < N && !math[i+d]) {
                    math[i+d] = s+1;
                    c = 1;
                }
                if (0 < i-d && !math[i-d]) {
                    math[i-d] = s+1;
                    c = 1;
                }
            }  
        }
        if (!c) {
            printf("-1\n");
            exit(EXIT_SUCCESS);
        }
    }
    return EXIT_SUCCESS;
}
int bit(int a) {
    int t = 0;
    for (; a;) {
        if (a % 2) {t++;};
        a /= 2;
    }
    return t;
}
            
            
            
        