結果

問題 No.1598 4×4 Grid
ユーザー ecotteaecottea
提出日時 2023-10-27 19:44:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 4,000 ms
コード長 12,209 bytes
コンパイル時間 5,390 ms
コンパイル使用メモリ 290,792 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-25 13:05:53
合計ジャッジ時間 5,875 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif


//【ハッシュ(unordered 用)】
/*
* unordered_set[map] の第二[三] 引数に Hash を渡して使う.
*/
struct Hash {
	size_t operator()(const array<int, 4>& a) const {
		return a[0] + 16 * (a[1] + 16 * (a[2] + 16 * a[3]));
	}
}; 


// メモリが足りない
void RE() {
	vector<vector<unordered_map<array<int, 4>, ll, Hash>>>
		dp(1LL << 16, vector<unordered_map<array<int, 4>, ll, Hash>>(217));

	rep(i0, 16) {
		rep(i1, 16) {
			if (i1 == i0) continue;
			rep(i2, 16) {
				if (i2 == i0 || i2 == i1) continue;
				rep(i3, 16) {
					if (i3 == i0 || i3 == i1 || i3 == i2) continue;

					int sc = abs(i0 - i1) + abs(i1 - i2) + abs(i2 - i3);
					int set = (1 << i0) + (1 << i1) + (1 << i2) + (1 << i3);
					dp[set][sc][{i0, i1, i2, i3}]++;
				}
			}
		}
	}

	repb(set, 16) {
		int pc = popcount(set);
		if (pc < 4) continue;

		repi(sc, 0, 216) {
			for (auto& [x, cnt] : dp[set][sc]) {
				if (pc % 4 == 0) {
					rep(i, 16) {
						if (get(set, i)) continue;

						int nsc = sc + abs(x[0] - i);
						int nset = set + (1 << i);
						dp[nset][nsc][{i, x[1], x[2], x[3]}] += cnt;
					}
				}
				else if (pc % 4 == 1) {
					rep(i, 16) {
						if (get(set, i)) continue;

						int nsc = sc + abs(x[0] - i) + abs(x[1] - i);
						int nset = set + (1 << i);
						dp[nset][nsc][{x[0], i, x[2], x[3]}] += cnt;
					}
				}
				else if (pc % 4 == 2) {
					rep(i, 16) {
						if (get(set, i)) continue;

						int nsc = sc + abs(x[1] - i) + abs(x[2] - i);
						int nset = set + (1 << i);
						dp[nset][nsc][{x[0], x[1], i, x[3]}] += cnt;
					}
				}
				else {
					rep(i, 16) {
						if (get(set, i)) continue;

						int nsc = sc + abs(x[2] - i) + abs(x[3] - i);
						int nset = set + (1 << i);
						dp[nset][nsc][{x[0], x[1], x[2], i}] += cnt;
					}
				}
			}
		}
	}

	int k;
	cin >> k;

	ll res = 0;
	for (auto& [x, cnt] : dp[(1 << 16) - 1][k]) {
		res += cnt;
	}

	cout << res << endl;
}


void zikken() {
	int a11 = 0, a12 = 15, a21 = 14, a22 = 1, a00 = 7;
	
	vl cnt(217);

	vi num;
	rep(a, 16) if (a != a11 && a != a12 && a != a21 && a != a22 && a != a00) num.push_back(a);
	dump(num);

	int sc0 = abs(a11 - a12) + abs(a12 - a22) + abs(a22 - a21) + abs(a21 - a11);

	vvvl dp(1LL << 11, vvl(11, vl(217 - sc0)));
	rep(a, 11) dp[1LL << a][a][abs(a00 - num[a]) + abs(a11 - num[a])]++;

	repb(set, 11) {
		int pc = popcount(set);

		rep(a, 11) {
			if (!get(set, a)) continue;

			repi(sc, 0, 216 - sc0) {
				if (!dp[set][a][sc]) continue;

				rep(na, 11) {
					if (get(set, na)) continue;

					int nset = set + (1 << na);
					int nsc = sc + abs(num[a] - num[na]);

					if (pc == 1 || pc == 3) nsc += abs(a12 - num[na]);
					else if (pc == 4 || pc == 6) nsc += abs(a22 - num[na]);
					else if (pc == 7 || pc == 9) nsc += abs(a21 - num[na]);
					else if (pc == 10) nsc += abs(a11 - num[na]) + abs(a00 - num[na]);

					dp[nset][na][nsc] += dp[set][a][sc];
				}
			}
		}
	}

	rep(a, 11) repi(sc, 0, 216 - sc0) cnt[sc0 + sc] += dp[(1 << 11) - 1][a][sc];

	dump(cnt);


	vl cnt2(217);

	repp(num) {
		array<array<int, 4>, 4> a;
		a[0][0] = a00;
		a[0][1] = num[0];
		a[0][2] = num[1];
		a[0][3] = num[2];
		a[1][0] = num[3];
		a[1][1] = a11;
		a[1][2] = a12;
		a[1][3] = num[4];
		a[2][0] = num[5];
		a[2][1] = a21;
		a[2][2] = a22;
		a[2][3] = num[6];
		a[3][0] = num[7];
		a[3][1] = num[8];
		a[3][2] = num[9];
		a[3][3] = num[10];

		int sc = 0;
		rep(i, 4) rep(j, 3) sc += abs(a[i][j] - a[i][j + 1]);
		rep(i, 3) rep(j, 4) sc += abs(a[i][j] - a[i + 1][j]);

		cnt2[sc]++;
	}

	dump(cnt2);

	Yes(cnt == cnt2);

	exit(0);
}


void umekomi() {
	vl cnt(217);

	vector<array<int, 5>> ctrs;
	rep(a, 16) repi(b, a + 1, 15) repi(c, b + 1, 15) repi(d, c + 1, 15) {
		rep(e, 16) {
			if (e != a && e != b && e != c && e != d) {
				ctrs.push_back({ a, b, c, d, e });
				ctrs.push_back({ a, b, d, c, e });
				ctrs.push_back({ a, c, b, d, e });
			}
		}
	}

	for (auto [a11, a12, a22, a21, a00] : ctrs) {
		if (a11 < a12 && a12 < a22 && a22 < a21) dump(a11, a12, a22, a21, a00);

		vi num;
		rep(a, 16) if (a != a11 && a != a12 && a != a21 && a != a22 && a != a00) num.push_back(a);

		int sc0 = abs(a11 - a12) + abs(a12 - a22) + abs(a22 - a21) + abs(a21 - a11);

		vvvl dp(1LL << 11, vvl(11, vl(217 - sc0)));
		rep(a, 11) dp[1LL << a][a][abs(a00 - num[a]) + abs(a11 - num[a])]++;

		repb(set, 11) {
			int pc = popcount(set);

			rep(a, 11) {
				if (!get(set, a)) continue;

				repi(sc, 0, 216 - sc0) {
					if (!dp[set][a][sc]) continue;

					rep(na, 11) {
						if (get(set, na)) continue;

						int nset = set + (1 << na);
						int nsc = sc + abs(num[a] - num[na]);

						if (pc == 1 || pc == 3) nsc += abs(a12 - num[na]);
						else if (pc == 4 || pc == 6) nsc += abs(a22 - num[na]);
						else if (pc == 7 || pc == 9) nsc += abs(a21 - num[na]);
						else if (pc == 10) nsc += abs(a11 - num[na]) + abs(a00 - num[na]);

						dp[nset][na][nsc] += dp[set][a][sc];
					}
				}
			}
		}

		rep(a, 11) repi(sc, 0, 216 - sc0) cnt[sc0 + sc] += dp[(1 << 11) - 1][a][sc] * 4 * 2;
	}

	dump_list(cnt);
}


vl ans = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 576, 6336, 32688, 109728, 292360, 690720, 1482192, 2901904, 5290232, 9051184, 14818296, 23208336, 35444224, 52313280, 76165896, 107817376, 151136456, 206538688, 280850520, 373958336, 496563792, 646511552, 841022448, 1073940272, 1373159520, 1724472448, 2173965112, 2693936208, 3356996536, 4112879776, 5069503864, 6145698704, 7500374992, 9002291472, 10886553960, 12950023664, 15534989704, 18330191088, 21826592520, 25556185792, 30214911160, 35115721776, 41213475560, 47528745680, 55374598960, 63355584272, 73235329704, 83088661872, 95252098840, 107100384672, 121728039728, 135636185488, 152817991288, 168730272976, 188461179128, 206185000928, 228279868744, 247485171200, 271619130776, 291810650816, 317478414304, 337980395648, 364517045992, 384542256192, 411098567064, 429692899120, 455298745848, 471402797888, 495019582768, 507574350192, 528077104696, 536056894256, 552532363064, 555126691616, 566752110520, 563549800176, 569825698888, 560753046592, 561635860016, 547051629840, 542801041800, 523561475168, 514791687840, 491843064800, 479502626904, 453886694304, 438919104360, 411796882592, 395006790760, 367318421328, 349592862160, 322244522592, 304279133304, 278166338192, 260606817328, 236415450464, 219798733352, 198011699984, 182673877024, 163585063776, 149759315744, 133360838032, 121291416320, 107364586352, 97039206120, 85460340016, 76744851464, 67244513664, 60024991512, 52289411200, 46383784240, 40172660592, 35384317192, 30414790512, 26601233192, 22681003744, 19659744320, 16621353472, 14263408928, 11954242720, 10179313056, 8450681536, 7142896704, 5909870720, 4978519840, 4116551680, 3467513504, 2870172672, 2417125568, 2001875712, 1681805056, 1384848768, 1159246976, 944311168, 780931392, 633980416, 518132736, 414036352, 332820480, 262237056, 208061056, 159154176, 122112768, 90448896, 67079040, 47692032, 33697536, 22858752, 15363072, 9566208, 5750784, 3115008, 1585152, 663552, 258048, 73728, 18432 };


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");
	
//	zikken();

//	umekomi();

	int k;
	cin >> k;
	
	cout << ans[k] << endl;
}
0