結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー sugim48sugim48
提出日時 2015-04-26 23:37:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,123 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 98,696 KB
実行使用メモリ 11,104 KB
最終ジャッジ日時 2023-09-18 11:39:28
合計ジャッジ時間 4,839 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
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 AC 6 ms
4,380 KB
testcase_21 AC 39 ms
11,104 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 19 ms
6,408 KB
testcase_27 AC 21 ms
7,160 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 1 ms
4,380 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <fstream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

int INF = INT_MAX / 10;
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

vector< vector<ll> > matmul(vector< vector<ll> >& A, vector< vector<ll> >& B) {
	int n = A.size();
	vector< vector<ll> > C(n, vector<ll>(n));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
	return C;
}

vector< vector<ll> > matpow(vector< vector<ll> > A, ll x) {
	int n = A.size();
	vector< vector<ll> > B(n, vector<ll>(n));
	for (int i = 0; i < n; i++) B[i][i] = 1;
	while (x > 0) {
		if (x % 2) B = matmul(B, A);
		x /= 2;
		A = matmul(A, A);
	}
	return B;
}

int main() {
	int N, K; cin >> N >> K;
	vector<ll> A(N);
	for (int i = 0; i < N; i++)
		cin >> A[i];
	if (N <= 30) {
		vector<vector<ll> > a(N * 2, vector<ll>(N * 2));
		for (int i = 0; i < N; i++)
			a[0][i] = a[N + i][i] = a[N + i][N + i] = 1;
		for (int i = 0; i + 1 < N; i++)
			a[i + 1][i] = 1;
		vector<vector<ll> > b = matpow(a, K - 1);
		int f = 0, s = 0;
		for (int i = 0; i < N; i++) {
			f = (f + b[N - 1][i] * A[N - i - 1]) % MOD;
			s = (s + b[N * 2 - 1][i] * A[N - i - 1]) % MOD;
		}
		cout << f << ' ' << (f + s) % MOD << endl;
	}
	else {
		vector<ll> a(K);
		ll sum = 0, s = 0;
		for (int i = 0; i < N; i++) {
			a[i] = A[i];
			sum = (sum + A[i]) % MOD;
		}
		for (int i = N; i < K; i++) {
			a[i] = sum;
			sum = (sum + a[i] - a[i - N]) % MOD;	
		}
		for (int i = 0; i < K; i++)
			s = (s + a[i]) % MOD;
		cout << a[K - 1] << ' ' << s << endl;
	}
}
0