結果
| 問題 |
No.621 3 x N グリッド上のドミノの置き方の数
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-08-25 05:18:21 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 3,000 ms |
| コード長 | 2,999 bytes |
| コンパイル時間 | 155 ms |
| コンパイル使用メモリ | 31,104 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-15 04:38:14 |
| 合計ジャッジ時間 | 1,729 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 66 |
ソースコード
// 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;
}
bal4u