結果
| 問題 | No.1598 4×4 Grid | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-04-24 22:34:44 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 325 ms / 4,000 ms | 
| コード長 | 968 bytes | 
| コンパイル時間 | 1,561 ms | 
| コンパイル使用メモリ | 168,600 KB | 
| 実行使用メモリ | 171,648 KB | 
| 最終ジャッジ日時 | 2024-11-08 23:10:33 | 
| 合計ジャッジ時間 | 5,772 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 7 | 
ソースコード
#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';
}
            
            
            
        