結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー kakira9618kakira9618
提出日時 2015-04-27 19:51:32
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,764 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 92,580 KB
実行使用メモリ 6,992 KB
最終ジャッジ日時 2023-09-18 13:53:53
合計ジャッジ時間 2,675 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 5 ms
4,384 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 19 ms
6,944 KB
testcase_21 AC 21 ms
6,912 KB
testcase_22 AC 19 ms
6,992 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 12 ms
4,932 KB
testcase_25 AC 10 ms
4,584 KB
testcase_26 AC 11 ms
4,716 KB
testcase_27 AC 12 ms
5,168 KB
testcase_28 AC 5 ms
4,380 KB
testcase_29 AC 18 ms
6,888 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 4 ms
4,380 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};

const long long int MOD = 1000000007ll;

vvi mul_(vvi &A, vvi &B) {
	vvi C(A.size(), vi(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]) % MOD;
			}
		}
	}
	
	return C;
} 

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 + MOD - fib[i - N];
            sum %= MOD;
            sum += fib[i];
            sum %= MOD;
        }
        int ret = 0;
        for (int i = 0; i < K; ++i) {
            ret += fib[i];
            ret %= MOD;
            //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] = MOD - 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] % MOD;
		}


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

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

    }


    return 0;
}
0