結果

問題 No.3 ビットすごろく
ユーザー DrakkonDrakkon
提出日時 2018-02-21 18:15:12
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,487 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 31,752 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 23:23:38
合計ジャッジ時間 2,266 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1 ms
4,348 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
4,348 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
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){
/*n:S[ i:萔 j:S[Õ}X*/
    int n, i = 1, j = 1;
    scanf("%d", &n);

/*S[O܂ł̎萔𐔂*/
    do{
        if(j + bitCount(j) <= n){
            j += bitCount(j);
            i++;
        }
        else{
            break;
        }
    }while(j < n);

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

    return 0;
}

//iɂƂ1̐߂֐
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;
}

//iɂƂ̌߂֐
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;
}

//nɂ}X납炳֐(nĂ)
//digits: t:e1 - iۑ
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;
}

//nɂ}Xɂ}XOT֐
//digits: t:e2 + iۑ n:C֐n
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