結果

問題 No.1598 4×4 Grid
ユーザー t98slidert98slider
提出日時 2023-04-24 22:34:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 4,000 ms
コード長 968 bytes
コンパイル時間 1,720 ms
コンパイル使用メモリ 164,604 KB
実行使用メモリ 171,776 KB
最終ジャッジ日時 2024-04-26 09:06:03
合計ジャッジ時間 5,907 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 321 ms
171,636 KB
testcase_01 AC 324 ms
171,648 KB
testcase_02 AC 324 ms
171,520 KB
testcase_03 AC 328 ms
171,776 KB
testcase_04 AC 326 ms
171,776 KB
testcase_05 AC 322 ms
171,776 KB
testcase_06 AC 325 ms
171,708 KB
testcase_07 AC 323 ms
171,648 KB
testcase_08 AC 327 ms
171,648 KB
testcase_09 AC 325 ms
171,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int K;
    cin >> K;
    vector<array<ll, 329>> dp(1 << 16, array<ll, 329>({}));
    dp[0][112] = 1;
    auto fb = [&](int S, int y, int x){ return (S >> (y * 4 + x) & 1 ? 1 : -1); };
    auto f = [&](int S, int pos){
        int y = pos >> 2, x = pos & 3, res = 0;
        if(y > 0) res += fb(S, y - 1, x);
        if(y < 3) res += fb(S, y + 1, x);
        if(x > 0) res += fb(S, y, x - 1);
        if(x < 3) res += fb(S, y, x + 1);
        return res;
    };
    for(int i = 0; i < (1 << 16); i++){
        int p = __builtin_popcount(i);
        for(int j = 0; j < 329; j++){
            if(dp[i][j] == 0) continue;
            for(int k = 0; k < 16; k++){
                if(i >> k & 1) continue;
                dp[i | (1 << k)][j + f(i, k) * p] += dp[i][j];
            }
        }
    }
    cout << dp.back()[K + 112] << '\n';
}
0