結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー hirakich1048576hirakich1048576
提出日時 2017-04-15 15:36:32
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 1,300 ms
コンパイル使用メモリ 30,988 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 15:11:24
合計ジャッジ時間 2,309 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 0 ms
4,348 KB
testcase_05 AC 0 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 0 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 0 ms
4,348 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 0 ms
4,348 KB
testcase_15 AC 0 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 0 ms
4,348 KB
testcase_19 AC 0 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 0 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;

void mtrMultiple(
	/* const */ llu* left[],
	/* const */ llu* right[],
	int sizeA,
	int sizeB,
	int sizeC,
	llu* resultVar[]){
	
	int i, j, k;
	llu resultdata[sizeA][sizeC];

	for (i = 0; i < sizeA; i++) {
		for (j = 0; j < sizeC; j++) {
			llu cell = 0;
			for (k = 0; k < sizeB; k++) {
				cell += (left[i][k] * right[k][j]) % MOD;
				cell %= MOD;
			}

			resultdata[i][j] = cell;
		}
	}
	for (i = 0; i < sizeA; i++) {
		for (j = 0; j < sizeC; j++) {
			resultVar[i][j] = resultdata[i][j];
		}
	}
}

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

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

	llu* argMtr[size];
	llu* argPrm[size];
	llu* argResd[size];

	for (i = 0; i < size; i++) {
		argMtr[i] = mtr[i];
		argPrm[i] = prevmtr[i];
		argResd[i] = resultdata[i];
	}

	for (i = 0; i < size; 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) {
			mtrMultiple((llu**)argResd, (llu**)argPrm, size, size, size, (llu**)argResd);
		}

		n = (n >> 1);
		mtrMultiple((llu**)argPrm, (llu**)argPrm, size, size, size, (llu**)argPrm);
	}

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

void solve(){
	int i;

	llu leftMatrix[1][2] = {
		{1, 0}
	};
	llu rightMatrix[2][2] = {
		{1, 1},
		{1, 0}
	};
	llu resultMatrix[1][2];

	llu answer;

	llu* argLeftm[1] = {leftMatrix[0]};
	llu* argRightm[2] = {rightMatrix[0], rightMatrix[1]};
	llu* argResm[1] = {resultMatrix[0]};

	powmtr((llu**)argRightm, n, 2, (llu**)argRightm);
	mtrMultiple((llu**)argLeftm, (llu**)argRightm, 1, 2, 2, (llu**)argResm);

	answer = resultMatrix[0][0] * resultMatrix[0][1] % MOD;
	printf("%llu\n", answer);

	return;

}

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

	return 0;
}
0