結果

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

テストケース

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

ソースコード

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];
	
printf("mat_pow p=%lld\n", p);
	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[S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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