結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー bal4ubal4u
提出日時 2019-08-25 05:18:21
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 3,000 ms
コード長 2,999 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 30,396 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-05 06:56:09
合計ジャッジ時間 2,531 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 0 ms
4,380 KB
testcase_04 AC 0 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 0 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 0 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 1 ms
4,380 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 1 ms
4,376 KB
testcase_58 AC 1 ms
4,384 KB
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 1 ms
4,380 KB
testcase_64 AC 1 ms
4,376 KB
testcase_65 AC 1 ms
4,376 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.621 3 x N グリッド上のドミノの置き方の数
// bal4u 2019.8.25
// OEIS A288028: Number of maximal matchings in the grid graph P_3 X P_n.
// a(n) = a(n-1)+5*a(n-2)+11*a(n-3)+5*a(n-4)+14*a(n-5)+8*a(n-6)+3*a(n-7)
//        -5*a(n-9)-11*a(n-10)-a(n-11)+2*a(n-12)+a(n-15) for n>15.
          
#include <stdio.h>
#include <string.h>

typedef long long ll;

#define MOD 1000000007

//// 行列のべき乗 (1-indexedに注意)
#define SZ 17                // 行列の大きさ指定(メモリ確保)
#define MSZ (sizeof(com))    // 行列の大きさ
int com[SZ][SZ];

// (N x N)行列 と (N x 1)行列の積。a, bは書き換えられない
void mat_mul1(int ab[SZ][SZ], int a[SZ][SZ], int b[SZ][SZ], int n) {
	int i, r;
	memset(ab, 0, MSZ);
	for (r = 1; r <= n; r++) for (i = 1; i <= n; i++)
		ab[r][1] = (ab[r][1] + (ll)a[r][i] * b[i][1]) % MOD;
}

// (N x N)行列 と (N x N)行列の積。a, bは書き換えられない
void mat_mul(int ab[SZ][SZ], int a[SZ][SZ], int b[SZ][SZ], int n) {
	int i, r, c;
	memset(ab, 0, MSZ);
	for (r = 1; r <= n; r++) for (i = 1; i <= n; i++) for (c = 1; c <= n; c++)
		ab[r][c] = (ab[r][c] + (ll)a[r][i] * b[i][c]) % MOD;
}

// (N x N)行列のべき乗p。aは書き換えられる!
void mat_pow(int ap[SZ][SZ], int a[SZ][SZ], int n, ll p) {
	int i, tmp[SZ][SZ];
	
	memset(ap, 0, MSZ);
	for (i = 1; i <= n; i++) ap[i][i] = 1;
	while (p > 0) {
		if (p & 1) mat_mul(tmp, ap, a, n), memcpy(ap, tmp, MSZ);
		mat_mul(tmp, a, a, n), memcpy(a, tmp, MSZ);
		p >>= 1;
	}
}

//// 本問題関連
int a[SZ][SZ];
int tbl[16] = {0,2,5,22,75,264,941,3286,11623,40960,
/* 10 */ 144267,508812,1792981,6319994,22277291,78518760};

/*
 * a(n) = a(n-1)+5*a(n-2)+11*a(n-3)+5*a(n-4)+14*a(n-5)+8*a(n-6)+3*a(n-7)
 *        -5*a(n-9)-11*a(n-10)-a(n-11)+2*a(n-12)+a(n-15) for n>15.
 */
int a[SZ][SZ] = {{0},
{ 0, 1, 5,11, 5,14, 8, 3, 0,-5,-11,-1,2, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }};

int main()
{
	ll N; int i, ans;
	int aa[SZ][SZ], b[SZ][SZ], c[SZ][SZ];
	
	scanf("%lld", &N);
	if (N <= 15) ans = tbl[N];
	else {
		mat_pow(aa, a, 15, N-15);  // 行列のべき乗

		memset(b, 0, MSZ);
		for (i = 1; i <= 15; i++) b[16-i][1] = tbl[i];
		mat_mul1(c, aa, b, 15);
		ans = c[1][1];
		if (ans < 0) ans += MOD;
	}
	printf("%d\n", ans);
	return 0;
}
0