結果

問題 No.3 ビットすごろく
ユーザー DrakkonDrakkon
提出日時 2018-02-20 17:50:19
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,651 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 29,972 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-27 10:42:52
合計ジャッジ時間 1,659 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 0 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 1 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 0 ms
4,376 KB
testcase_22 WA -
testcase_23 AC 0 ms
4,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
4,380 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);

/*S[ɒڍs}X߁Ao͂*/
    if(j == n){
        printf("%d\n", i);
    }
    else{
        t = explore1(n);
        i++;
        if(t == -1){
            printf("%d\n", -1);
        }
        else{
            if(explore2(t, n) != j){
                do{
                    if(explore1(t) == -1){
                        printf("%d\n", -1);
                        break;
                    }
                    else if(explore2(t,n) == j){
                        i++;;
                        break;
                    }
                    else{
                        t = explore1(t);
                        i += 2;
                    }
                }while(0);
                printf("%d\n", i);
            }
            else{
                i++;
                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;
    digits = numberOfDigits(e2);
    while(i <= digits || t < n){
        t = e2 + i;
        if(t - bitCount(t) == e2){
            return t;
            break;
        }
        i++;
    }
    return -1;
}
0