結果
問題 | No.1598 4×4 Grid |
ユーザー |
![]() |
提出日時 | 2021-07-09 22:35:20 |
言語 | C++17 (gcc 11.2.0 + boost 1.78.0) |
結果 |
AC
|
実行時間 | 493 ms / 4,000 ms |
コード長 | 2,437 bytes |
コンパイル時間 | 1,060 ms |
使用メモリ | 313,296 KB |
最終ジャッジ日時 | 2023-02-02 00:36:17 |
合計ジャッジ時間 | 7,862 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 使用メモリ |
---|---|---|
testcase_00 | AC | 490 ms
313,236 KB |
testcase_01 | AC | 489 ms
313,248 KB |
testcase_02 | AC | 489 ms
313,180 KB |
testcase_03 | AC | 493 ms
313,252 KB |
testcase_04 | AC | 491 ms
313,296 KB |
testcase_05 | AC | 490 ms
313,268 KB |
testcase_06 | AC | 489 ms
313,176 KB |
testcase_07 | AC | 493 ms
313,224 KB |
testcase_08 | AC | 491 ms
313,220 KB |
testcase_09 | AC | 492 ms
313,292 KB |
ソースコード
#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; }