結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー sugim48sugim48
提出日時 2015-04-26 23:42:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 212 ms / 5,000 ms
コード長 2,153 bytes
コンパイル時間 1,014 ms
コンパイル使用メモリ 97,092 KB
実行使用メモリ 10,956 KB
最終ジャッジ日時 2023-09-18 11:53:50
合計ジャッジ時間 4,783 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 212 ms
4,380 KB
testcase_03 AC 17 ms
4,380 KB
testcase_04 AC 73 ms
4,384 KB
testcase_05 AC 59 ms
4,384 KB
testcase_06 AC 77 ms
4,380 KB
testcase_07 AC 126 ms
4,384 KB
testcase_08 AC 11 ms
4,384 KB
testcase_09 AC 101 ms
4,380 KB
testcase_10 AC 33 ms
4,384 KB
testcase_11 AC 36 ms
4,380 KB
testcase_12 AC 64 ms
4,380 KB
testcase_13 AC 22 ms
4,384 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 159 ms
4,384 KB
testcase_16 AC 138 ms
4,380 KB
testcase_17 AC 32 ms
4,380 KB
testcase_18 AC 143 ms
4,384 KB
testcase_19 AC 205 ms
4,380 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 38 ms
10,956 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 19 ms
6,636 KB
testcase_25 AC 18 ms
6,328 KB
testcase_26 AC 18 ms
6,356 KB
testcase_27 AC 20 ms
7,232 KB
testcase_28 AC 7 ms
4,384 KB
testcase_29 AC 34 ms
10,160 KB
testcase_30 AC 211 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 55 ms
4,380 KB
testcase_33 AC 87 ms
4,384 KB
testcase_34 AC 66 ms
4,380 KB
testcase_35 AC 55 ms
4,380 KB
testcase_36 AC 150 ms
4,384 KB
testcase_37 AC 11 ms
4,380 KB
testcase_38 AC 170 ms
4,380 KB
testcase_39 AC 62 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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; ll 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);
		ll 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] + MOD) % MOD << ' ' << (s + MOD) % MOD << endl;
	}
}
0