結果

問題 No.3 ビットすごろく
ユーザー DrakkonDrakkon
提出日時 2018-02-21 16:42:14
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,255 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 30,432 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-14 14:15:01
合計ジャッジ時間 2,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 WA -
testcase_02 AC 0 ms
4,348 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,348 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
4,352 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, t;
    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{
            if(j + bitCount(j) == explore2(j,n)){
                break;
            }
            if(j <= 1){
                break;
            }
            j -= bitCount(j);
            i++;
        }while(j + bitCount(j) != explore2(j,n));
        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){
        m -= i;
        i /= 2;
        count++;
    }
    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