結果

問題 No.822 Bitwise AND
ユーザー tottoripapertottoripaper
提出日時 2019-04-27 17:14:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,167 bytes
コンパイル時間 1,451 ms
コンパイル使用メモリ 165,344 KB
実行使用メモリ 39,428 KB
最終ジャッジ日時 2023-09-08 01:13:14
合計ジャッジ時間 2,556 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
39,292 KB
testcase_01 AC 13 ms
39,248 KB
testcase_02 AC 16 ms
39,248 KB
testcase_03 AC 16 ms
39,220 KB
testcase_04 AC 12 ms
39,232 KB
testcase_05 AC 11 ms
39,300 KB
testcase_06 AC 11 ms
39,200 KB
testcase_07 AC 12 ms
39,256 KB
testcase_08 AC 11 ms
39,200 KB
testcase_09 AC 12 ms
39,252 KB
testcase_10 AC 13 ms
39,216 KB
testcase_11 AC 13 ms
39,232 KB
testcase_12 AC 11 ms
39,428 KB
testcase_13 AC 12 ms
39,332 KB
testcase_14 AC 16 ms
39,216 KB
testcase_15 AC 11 ms
39,336 KB
testcase_16 AC 14 ms
39,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

constexpr ll B = 135000, INF = std::numeric_limits<ll>::max() / 2;
int N, K;
ll dp[17][270000];

ll add(ll x, ll y){
    if(x < INF && y < INF){
        return x + y;
    }

    return INF;
}

ll rec(int i, int d){
    if(i == 17){
        d -= B;

        if(0 <= d && d <= K){
            return 1;
        }else if(d <= - (1 << 17) + K){
            return INF;
        }

        return 0;
    }

    if(dp[i][d] != -1){
        return dp[i][d];
    }

    ll res;

    if(N >> i & 1){
        res = rec(i + 1, d);
    }else{
        res = add(add(rec(i + 1, d + (1 << i)), rec(i + 1, d - (1 << i))), rec(i + 1, d));
    }

    return dp[i][d] = res;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N >> K;

    memset(dp, -1, sizeof(dp));

    ll res = rec(0, B);

    if(res < INF){
        std::cout << res << std::endl;
    }else{
        std::cout << "INF" << std::endl;
    }
}
0