結果

問題 No.1598 4×4 Grid
ユーザー e869120e869120
提出日時 2021-07-09 21:19:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 4,000 ms
コード長 951 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 64,224 KB
実行使用メモリ 120,388 KB
最終ジャッジ日時 2023-09-14 07:04:23
合計ジャッジ時間 2,673 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
120,104 KB
testcase_01 AC 127 ms
120,388 KB
testcase_02 AC 127 ms
120,136 KB
testcase_03 AC 127 ms
120,064 KB
testcase_04 AC 126 ms
120,112 KB
testcase_05 AC 127 ms
120,112 KB
testcase_06 AC 128 ms
120,060 KB
testcase_07 AC 130 ms
120,140 KB
testcase_08 AC 128 ms
120,116 KB
testcase_09 AC 128 ms
120,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
using namespace std;

long long K;
long long score_increase[1 << 16];
long long dp[1 << 16][227];

int main() {
	// Step #1. Input
	cin >> K;
	assert(1 <= K && K <= 216);

	// Step #2. Prepare
	for (int i = 0; i < (1 << 16); i++) {
		int mask[4][4], cnt = 0;
		for (int j = 0; j < 16; j++) mask[j / 4][j % 4] = (i / (1 << j)) % 2;
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < 4; k++) {
				if (k < 3 && mask[j][k] != mask[j][k + 1]) cnt++;
				if (j < 3 && mask[j][k] != mask[j + 1][k]) cnt++;
			}
		}
		score_increase[i] = cnt;
	}

	// Step #3. Dynamic Programming
	dp[0][0] = 1;
	for (int i = 0; i < (1 << 16); i++) {
		for (int j = 0; j <= 216; j++) {
			if (dp[i][j] == 0) continue;
			for (int k = 0; k < 16; k++) {
				if ((i & (1 << k)) != 0) continue;
				dp[i + (1 << k)][j + score_increase[i]] += dp[i][j];
			}
		}
	}

	// Step #4. Output
	cout << dp[(1 << 16) - 1][K] << endl;
	return 0;
}
0