結果

問題 No.3 ビットすごろく
ユーザー DrakkonDrakkon
提出日時 2018-02-22 22:41:32
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,088 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 32,384 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 22:35:51
合計ジャッジ時間 1,942 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 WA -
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 WA -
testcase_21 AC 1 ms
6,940 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
6,944 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1 ms
6,944 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int bitCount(int m);
int numberOfDigits(int d);
int explore1(int e1);
int explore2(int e2, int n);

int main(void){
    int n, i = 1, j = 1;
    scanf("%d", &n);

    do{
        if(j + bitCount(j) <= n){
            j += bitCount(j);
            i++;
        }
        else{
            break;
        }
    }while(j < n);

    if(j == n){
        printf("%d\n", i);
    }
    else{
        do{
            if(explore1(n) == -1){
                j = -1;
                break;
            }
            if(explore1(n) != j){
                j -= bitCount(j);
                i++;
            }
            if(explore1(n) == j + bitCount(j)){
                i += 2;
                break;
            }
            if(j <= 1){
                break;
            }
        }while(0);
        if(j <= 1){
            printf("%d\n", -1);
        }
        else{
            printf("%d\n", i);
        }
    }

    return 0;
}

int bitCount(int m){
    int i = 1, count = 0;
    if(m % 2 != 0){
        count++;
        m--;
    }
    while(i < m){
        i *= 2;
    }
    if(i > m){
        i /= 2;
    }
    while(m > 0){
        if(m - i >= 0){
            m -= i;
            count++;
        }
        i /= 2;
    }
    return count;
}

int numberOfDigits(int d){
    int i = 1, j = 0;
    while(i < d){
        i *= 2;
    }
    if(i > d){
        i /= 2;
    }
    while(i > 0){
        i /= 2;
        j++;
    }
    return j;
}

int explore1(int e1){
    int i = 1, digits, t;
    digits = numberOfDigits(e1);
    while(i <= digits){
        t = e1 - i;
        if(t + bitCount(t) == e1){
            return t;
            break;
        }
        i++;
    }
    return -1;
}

int explore2(int e2, int n){
    int i = 1, digits, t = 0;
    digits = numberOfDigits(e2);
    while(i <= digits || t <= n){
        t = e2 + i;
        if(t - bitCount(t) == e2){
            return t;
            break;
        }
        i++;
    }
    return -1;
}
0