結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー hirakich1048576hirakich1048576
提出日時 2017-04-15 15:36:57
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 3,054 bytes
コンパイル時間 1,824 ms
コンパイル使用メモリ 30,604 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 15:11:34
合計ジャッジ時間 1,861 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 5 ms
4,352 KB
testcase_02 AC 7 ms
4,348 KB
testcase_03 AC 8 ms
4,348 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 5 ms
4,348 KB
testcase_06 AC 5 ms
4,352 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 5 ms
4,352 KB
testcase_14 AC 5 ms
4,352 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 6 ms
4,352 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 10 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MOD 1000000007

typedef unsigned long long llu;
typedef   signed long long lld;

llu n, m;

void powmtr(/* const */ llu* mtr[], llu n, int size, llu* resultVar[]){
	int i, j;

	llu prevmtr[2][size];
	llu resultdata[2][size];

	for (i = 0; i < 2; i++) {
		for (j = 0; j < size; j++) {
			resultdata[i][j] = (i == j ? 1 : 0);
			prevmtr[i][j] = mtr[i][j];
		}
	}

	while (n) {
		if (n & 1) {
			const llu rd00 = resultdata[0][0];
			const llu rd01 = resultdata[0][1];
			const llu rd10 = resultdata[1][0];
			const llu rd11 = resultdata[1][1];

			// puts("resultdata update!");

			for (i = 0; i < size; i++) {
				if (i != size - 1) {
					resultdata[0][i] = resultdata[1][i] = 0;
				}
				resultdata[0][i] += (rd00 * prevmtr[0][i] + rd01 * prevmtr[1][i]) % MOD;
				resultdata[0][i] %= MOD;
				resultdata[1][i] += (rd10 * prevmtr[0][i] + rd11 * prevmtr[1][i]) % MOD;
				resultdata[1][i] %= MOD;

				// printf("%llu %llu\n", resultdata[0][i], resultdata[1][i]);
			}
		}

		// putchar('\n');

		n = (n >> 1);

		// puts("prevmtr update!");

		{
			const llu pm00 = prevmtr[0][0];
			const llu pm01 = prevmtr[0][1];
			const llu pm10 = prevmtr[1][0];
			const llu pm11 = prevmtr[1][1];
			for (i = 0; i < size; i++) {
				const llu pm0i = prevmtr[0][i];
				const llu pm1i = prevmtr[1][i];
				if (i != size - 1) {
					prevmtr[0][i] = prevmtr[1][i] = 0;
				}
				prevmtr[0][i] += (pm00 * pm0i + pm01 * pm1i) % MOD;
				prevmtr[0][i] %= MOD;
				prevmtr[1][i] += (pm10 * pm0i + pm11 * pm1i) % MOD;
				prevmtr[1][i] %= MOD;

				// printf("%llu %llu\n", prevmtr[0][i], prevmtr[1][i]);
			}
		}

		// putchar('\n');
		// puts("=========================");
	}

	for (i = 0; i < 2; i++) {
		for (j = 0; j < size; j++) {
			resultVar[i][j] = resultdata[i][j];
		}
	}
}

llu solve(llu m){
	int i;

	llu leftMatrix[m + 1];
	llu rightMatrixPeace[2][m + 1];

	llu* argRightm[2] = {rightMatrixPeace[0], rightMatrixPeace[1]};

	llu answer;

	llu fibPrev = 0, fibNext = 1;
	for (i = m - 1; i >= 0; i--) {
		leftMatrix[i] = rightMatrixPeace[1][i] = fibNext;

		fibPrev += fibNext;
		fibPrev %= MOD;
		fibPrev ^= fibNext;
		fibNext ^= fibPrev;
		fibPrev ^= fibNext;
	}
	for (i = 0; i < m; i++) {
		rightMatrixPeace[0][i + 1] = rightMatrixPeace[1][i];
	}
	leftMatrix[m] = 0;
	rightMatrixPeace[0][0] = (rightMatrixPeace[0][1] + rightMatrixPeace[1][1]) % MOD;
	rightMatrixPeace[1][m] = 0;

	powmtr(argRightm, n, m + 1, argRightm);

	// for (i = 0; i < m + 1; i++) printf("%d %llu\n", i, leftMatrix[i]);
	// for (i = 0; i < 2; i++) {
	// 	for (int j = 0; j <= m; j++) {
	// 		printf("%d:%d %llu\n", i, j, rightMatrixPeace[i][j]);
	// 	}
	// }

	answer = (leftMatrix[0] * rightMatrixPeace[0][m] + leftMatrix[1] * rightMatrixPeace[1][m]) % MOD;

	return answer;

}

int main(void){
	llu answer;
	scanf("%llu %llu", &n, &m);

	printf("%llu\n", solve(m));

	return 0;
}
0