結果
| 問題 | No.1598 4×4 Grid | 
| コンテスト | |
| ユーザー |  milanis48663220 | 
| 提出日時 | 2021-07-09 22:35:20 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 618 ms / 4,000 ms | 
| コード長 | 2,437 bytes | 
| コンパイル時間 | 1,636 ms | 
| コンパイル使用メモリ | 122,808 KB | 
| 最終ジャッジ日時 | 2025-01-22 22:17:59 | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 7 | 
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template <typename T>
class OffsetVec{
    public:
	int n;
    vector<T> v;
	T& operator[](int x) {
        return v[x+n];
	}
    void print(){
        for(int i = -n; i <= n; i++) cout << v[i+n] << ' ';
        cout << endl;
    }
    OffsetVec(int _n){
        n = _n;
        v = vector<T>(2*n+1);
    }
    OffsetVec(int _n, T x){
        n = _n;
        v = vector<T>(2*n+1, x);
    }
};
const int N_MAX = 1<<16;
const int m = 300;
int popcount(int n){
    int ans = 0;
    for(int i = 0; i < 16; i++){
        if(n&(1<<i)) ans++;
    }
    return ans;
}
int dh[4] = {0, 0, -1, 1};
int dw[4] = {-1, 1, 0, 0};
bool is_in_field(int i, int j){
    return i >= 0 && i < 4 && j >= 0 && j < 4;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int k; cin >> k;
    vector<OffsetVec<ll>> dp(N_MAX, OffsetVec<ll>(m, 0));
    dp[0][0] = 1;
    ll sum = 0;
    for(int s = 0; s < (1<<16); s++){
        int nx = popcount(s)+1;
        vector<vector<bool>> used(4, vector<bool>(4));
        for(int j = 0; j < 16; j++){
            if(s&(1<<j)) used[j/4][j%4] = true;
        }
        for(int j = 0; j < 16; j++){
            if(s&(1<<j)) continue;
            int x = j/4, y = j%4;
            int cnt = 0;
            for(int k = 0; k < 4; k++){
                int xto = x+dh[k], yto = y+dw[k];
                if(is_in_field(xto, yto)){
                    if(used[xto][yto]) cnt++;
                    else cnt--;
                }
            }
            for(int k = -m; k <= m; k++){
                if(dp[s][k] == 0) continue;
                assert(k+cnt*nx >= -m);
                assert(k+cnt*nx <= m);
                dp[s+(1<<j)][k+cnt*nx] += dp[s][k];
            }
        }
    }
    cout << dp[(1<<16)-1][k] << endl;
}
            
            
            
        