結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-26 22:40:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 5,000 ms
コード長 1,992 bytes
コンパイル時間 1,966 ms
コンパイル使用メモリ 156,396 KB
実行使用メモリ 7,408 KB
最終ジャッジ日時 2023-09-18 12:12:00
合計ジャッジ時間 3,733 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 31 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 13 ms
4,380 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 12 ms
4,384 KB
testcase_07 AC 20 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 16 ms
4,376 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 24 ms
4,380 KB
testcase_16 AC 20 ms
4,376 KB
testcase_17 AC 7 ms
4,380 KB
testcase_18 AC 21 ms
4,380 KB
testcase_19 AC 30 ms
4,376 KB
testcase_20 AC 38 ms
7,228 KB
testcase_21 AC 39 ms
7,232 KB
testcase_22 AC 38 ms
7,408 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 20 ms
5,408 KB
testcase_25 AC 18 ms
4,988 KB
testcase_26 AC 18 ms
4,996 KB
testcase_27 AC 21 ms
5,472 KB
testcase_28 AC 6 ms
4,376 KB
testcase_29 AC 35 ms
6,976 KB
testcase_30 AC 30 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 10 ms
4,380 KB
testcase_33 AC 14 ms
4,380 KB
testcase_34 AC 12 ms
4,380 KB
testcase_35 AC 10 ms
4,380 KB
testcase_36 AC 23 ms
4,380 KB
testcase_37 AC 4 ms
4,376 KB
testcase_38 AC 27 ms
4,380 KB
testcase_39 AC 11 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int dp[1000010];

long long mod = (int)1e9 + 7;

void calc(int N, long long K, vector<int> A){
	long long sum2 = 0;
	long long sum = 0;
	for (int i = 0; i < N; i++)
	{
		dp[i] = A[i];
		sum += A[i];
		sum2 += A[i];
	}
	sum %= mod;
	sum2 %= mod;
	for (int i = N; i < K; i++)
	{
		dp[i] = sum;
		sum -= dp[i - N];
		sum += dp[i];
		sum %= mod;
		sum += mod;
		sum %= mod;

		sum2 += dp[i];
		sum2 %= mod;
	}
	cout << dp[K - 1] << " " << sum2 << endl;
}

vector<vector<long long>> E(int N){
	vector<vector<long long>> V(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++)
	{
		V[i][i] = 1ll;
	}
	return V;
}

vector<vector<long long>> matrixmul(vector<vector<long long>>  a, vector<vector<long long>>  b){
	int N = a[0].size();
	vector<vector<long long>> V(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			for (int k = 0; k < N; k++)
			{
				V[i][j] += a[i][k] * b[k][j];
				V[i][j] %= mod;
			}
			V[i][j] %= mod;
		}
	}
	return V;
}

vector<vector<long long>> matrixpow(vector<vector<long long>> a, long long p){
	if (p == 0) return E(a[0].size());
	if (p % 2 == 1){
		return matrixmul(a, matrixpow(a, p - 1));
	}
	else{
		auto c = matrixpow(a, p / 2);
		return matrixmul(c, c);
	}
}



void calc2(int N, long long K, vector<int> A){
	vector<vector<long long>> V(N + 1, vector<long long>(N + 1, 0));
	for (int i = 0; i < N; i++)
	{
		if(i != 0) V[i - 1][i] = 1;
		V[N - 1][i] = 1;
		V[N][i] = 1;
	}
	V[N][N] = 1;
	long long sum = 0;
	for (int i = 0; i < N; i++)
	{
		sum += A[i];
		sum %= mod;
	}
	long long ans = 0;
	auto next = matrixpow(V, K - N);

	for (int i = 0; i < N; i++)
	{
		ans += A[i] * next[N - 1][i];
		sum += A[i] * next[N][i];
	}
	ans %= mod;
	sum %= mod;
	cout << ans << " " << sum << endl;
}

int main() {
	int N;
	long long K;
	cin >> N >> K;
	vector<int> A(N);
	for (int i = 0; i < N; i++)
	{
		cin >> A[i];
	}
	if (K <= 1000000) calc(N, K, A);
	else calc2(N, K, A);
}
0