結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー kakira9618kakira9618
提出日時 2015-04-27 14:56:18
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,073 bytes
コンパイル時間 883 ms
コンパイル使用メモリ 94,864 KB
実行使用メモリ 7,172 KB
最終ジャッジ日時 2023-09-18 13:51:25
合計ジャッジ時間 2,587 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 6 ms
4,376 KB
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 17 ms
6,840 KB
testcase_21 AC 19 ms
7,172 KB
testcase_22 AC 17 ms
6,956 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 10 ms
4,888 KB
testcase_25 AC 9 ms
4,536 KB
testcase_26 AC 10 ms
4,652 KB
testcase_27 AC 11 ms
5,068 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 16 ms
6,672 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 4 ms
4,376 KB
testcase_39 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
 
using namespace std;
 
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
 
const int INF=1<<29;
const double EPS=1e-9;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

vvi mul_(vvi &A, vvi &B) {
	vector<vector<ll> > C(A.size(), vector<ll>(B[0].size(), 0));
	for (int i = 0; i < A.size(); ++i) {
		for (int k = 0; k < B.size(); ++k) {
			for (int j = 0; j < B[0].size(); ++j) {
				C[i][j] = ((ll)A[i][k] * B[k][j] + C[i][j] + 1000000007ll * 1000000007) % 1000000007;
			}
		}
	}
	vvi D(A.size(), vi(B[0].size(), 0));
	for (int i = 0; i < A.size(); ++i) {
		for (int j = 0; j < B[0].size(); ++j) {
			D[i][j] = C[i][j] % 1000000007;
		}
	}
	return D;
} 

vvi mul(vvi A, long long int n) {
	vvi B(A.size(), vi(A[0].size(), 0));

	for (int i = 0; i < B.size(); ++i) {
		B[i][i] = 1;
	}
	while(n > 0) {
		if (n & 1) {
			B = mul_(B, A);
		}
		A = mul_(A, A);
		n >>= 1;
	}
	return B;
}

int main(int argc, char const *argv[]) {
    
    long long int N, K;
    cin >> N >> K;

    if (N <= 10000 && K <= 1000000) {
        std::vector<int> fib(K);
        int sum = 0;
        for (int i = 0; i < N; ++i) {
            int temp;
            cin >> temp;
            sum += temp;
            fib[i] = temp;
        }
        for (int i = N; i < K; ++i) {
            fib[i] = sum;
            sum = sum + 1000000007 - fib[i - N];
            sum %= 1000000007;
            sum += fib[i];
            sum %= 1000000007;
        }
        int ret = 0;
        for (int i = 0; i < K; ++i) {
            ret += fib[i];
            ret %= 1000000007;
            //cout << fib[i] << " ";
 	 	}
 	 	//cout << endl;
 	 	cout << fib[K - 1] << " " << ret << endl;
 
    } else {

    	vvi G(N + 1, vi(N + 1, 0));
		G[0][0] = 2;
		G[0][G[0].size() - 1] = 1000000007 - 1;
    	for (int i = 0; i < G.size() - 1; ++i) {
    		G[i + 1][i] = 1;
    	}

    	G = mul(G, K - N);

    	vi v(N + 1);
    	int sum = 0;
    	v[v.size() - 1] = 0;
    	for (int i = 0; i < v.size() - 1; ++i) {
    		int temp;
    		cin >> temp;
    		sum += temp;
    		v[v.size() - i - 2] = sum;
    	}

    	long long int Sk = 0, Sk_ = 0;
		for (int i = 0; i < N; ++i) {
			Sk += ((ll)G[0][i] * v[i] + 1000000007ll * 1000000007) % 1000000007;
		}

    	for (int i = 0; i < N; ++i) {
    		Sk_ += ((ll)G[1][i] * v[i] + 1000000007ll * 1000000007) % 1000000007;
    	}

    	cout << (Sk - Sk_ + 1000000007ll) % 1000000007ll << " " << Sk % 1000000007 << endl;

    }


    return 0;
}
0