結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー bal4ubal4u
提出日時 2019-08-25 03:57:07
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 1,971 bytes
コンパイル時間 1,498 ms
コンパイル使用メモリ 30,996 KB
実行使用メモリ 5,764 KB
最終ジャッジ日時 2023-08-05 02:48:45
合計ジャッジ時間 2,702 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 0 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 9 ms
5,764 KB
testcase_22 AC 0 ms
4,384 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 5 ms
4,376 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 6 ms
4,384 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 8 ms
5,304 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 0 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:11:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:27: 備考: in expansion of macro ‘gc’
   17 |         ll n = 0; int c = gc();
      |                           ^~

ソースコード

diff #

// yukicoder: No.194 フィボナッチ数列の理解(1)
// bal4u 2019.8.25

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

typedef long long ll;

//// 入出力関係
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

ll in() { // 非負整数の入力
	ll n = 0; int c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) > ' ');
	return n;
}

#define MOD 1000000007
#define SZ 35
#define MSZ (sizeof(com))
int com[SZ][SZ], a[SZ][SZ], ans[SZ][SZ];

int N; ll K;
int f[1000005];

void mat_mul1(int ab[SZ][SZ], int a[SZ][SZ], int b[SZ][SZ], int n) {
	int i, r;
//	memset(ab, 0, sizeof(std));
	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;
}

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;
}

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 main()
{
	int i, sf, S;
	
	N = (int)in(), K = in(), sf = 0;
	for (i = 1; i <= N; i++) {
		f[i] = gc() & 0xf, gc();
		sf += f[i];
	}
	f[i] = S = sf, S += f[i];
	
	if (N > 30) {
		int k = (int)K;
		while (i < k) {
			sf = ((ll)sf + f[i] - f[i-N]) % MOD;
			f[++i] = sf;
			S = (S + sf) % MOD;
		}
		if (sf < 0) sf += MOD;
		if (S < 0) S += MOD;
	} else {
		for (i = 1; i <= N+1; i++) a[1][i] = 1;
		for (i = 2; i <= N+1; i++) a[2][i] = 1;
		for (i = 3; i <= N+1; i++) a[i][i-1] = 1;
		mat_pow(com, a, N+1, K-N);  // 行列のべき乗
		
		memset(a, 0, MSZ);
		for (i = 1; i <= N; i++) a[N-i+2][1] = f[i];
		a[1][1] = sf;
		mat_mul1(ans, com, a, N+2);
		sf = ans[2][1], S = ans[1][1];
	}

	printf("%d %d\n", sf, S);
	return 0;
}
0