結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー snteasntea
提出日時 2016-10-10 12:00:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 4,284 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 176,676 KB
実行使用メモリ 11,096 KB
最終ジャッジ日時 2024-05-01 18:53:42
合計ジャッジ時間 3,473 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 19 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 8 ms
6,940 KB
testcase_05 AC 6 ms
6,940 KB
testcase_06 AC 8 ms
6,940 KB
testcase_07 AC 12 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 9 ms
6,944 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 5 ms
6,940 KB
testcase_12 AC 7 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 15 ms
6,940 KB
testcase_16 AC 12 ms
6,940 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 13 ms
6,944 KB
testcase_19 AC 18 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 19 ms
11,096 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 11 ms
6,940 KB
testcase_25 AC 10 ms
6,944 KB
testcase_26 AC 9 ms
6,944 KB
testcase_27 AC 12 ms
7,456 KB
testcase_28 AC 5 ms
6,940 KB
testcase_29 AC 17 ms
10,284 KB
testcase_30 AC 17 ms
6,944 KB
testcase_31 AC 1 ms
6,944 KB
testcase_32 AC 7 ms
6,940 KB
testcase_33 AC 8 ms
6,940 KB
testcase_34 AC 7 ms
6,940 KB
testcase_35 AC 6 ms
6,944 KB
testcase_36 AC 14 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 15 ms
6,940 KB
testcase_39 AC 7 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOCAL111
#else
	#define NDEBUG
#endif
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RBP(i,a) for(auto& i : a)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
#define rangej(a,b,c) ((a) <= (c) and (c) < (b))
#define rrangej(b,c) rangej(0,b,c)
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);	
}

const LL mod = 1e9+7;

//library

template<typename T>
class Matrix {
public:
	vector<vector<T> > v;
	Matrix(int n){
		v.resize(n,vector<T>(n,0));
	}

	Matrix operator+ (Matrix x){
		Matrix res(v.size());
		for(int i = 0; i < (int)v.size(); i++){
			for(int j = 0; j < (int)v.size(); j++){
				res.v[i][j] = (x.v[i][j]+v[i][j])%mod;
			}
		}
		return res;
	}

	Matrix operator-(Matrix x){
		Matrix res(v.size());
		for(int i = 0; i < (int)v.size(); i++){
			for(int j = 0; j < (int)v.size(); j++){
				res.v[i][j] = (v[i][j]-x.v[i][j])%mod;
			}
		}
	}

	Matrix operator*(Matrix x){
		Matrix res(v.size());
		for(int i = 0;i < (int)v.size(); i++){
			for(int j = 0; j < (int)v.size(); j++){
				for(int k = 0; k < (int)v.size(); k++){
					res.v[i][j] += v[i][k]*x.v[k][j]%mod;
					res.v[i][j] %= mod;
				}
			}
		}
		return res;
	}

	vector<T> operator*(vector<T> x){
		assert(x.size() == v.size());
		vector<T> res(v.size());
		for(int i = 0; i < (int)v.size(); i++){
			T tmp = 0;
			for(int j = 0; j < (int)v.size(); j++){
				tmp += v[i][j]*x[j]%mod;
				tmp %= mod;
			}
			res[i] = tmp;
		}
		return res;
	}

	vector<T>& operator[](int x){
		return v[x];
	}
};

template<typename T>
Matrix<T> pow(Matrix<T> x,long long n){
	Matrix<T> tmp = x, res(SZ(x.v));
	for(int i = 0; i < SZ(x.v); i++)	res.v[i][i] = 1;
	while(n != 0){
		if(n&1){
			res = res*tmp;
		}
		tmp = tmp*tmp;
		n >>= 1;
	}
	return res;
}
//libary

int main()
{
	ios_init();
	int n;
	LL k;
	while(cin >> n >> k) {
		DEBUG(n);
		vector<LL> a(n);
		REP(i,n) cin >> a[i];
		function<void()> solvemat = [&](){
			Matrix<LL> A(n);
			REP(i,n) A[0][i] = 1;
			REP(i,n-1) A[i+1][i] = 1;
			Matrix<LL> B(n+1);
			B[0][0] = B[0][1] = 1;
			REP(i,n) B[1][i+1] = 1;
			REP(i,n-1) B[i+2][i+1] = 1;
			REP(i,n+1) dpite(ALL(B[i]));
			if(k <= n){
				k--;
				cout << a[k] << ' ' << accumulate(a.begin(),a.begin()+k,0LL) << endl;
			}else{
				A = pow(A,k-n);
				vector<LL> b = a;
				b.push_back(accumulate(a.begin(),--a.end(),0LL));
				reverse(ALL(a));
				auto ans = A*a;
				// reverse(ALL(a));
				reverse(ALL(b));
				dpite(ALL(b));
				B = pow(B,k-n+1);
				auto ans2 = B*b;
				dpite(ALL(ans2));
				cout << ans[0] << ' ' << ans2[0] << endl;
			}

		};
		function<void()> solvedp = [&](){
			if(k <= n){
				cout << a[k-1] << endl;
			}else{
				vector<LL> dp(k,0);
				LL sum = 0;
				REP(i,SZ(a)){
					sum = (sum+a[i])%mod;
					dp[i]= a[i];
				}
				FOR(i,n,k){
					dp[i] = sum;
					sum = ((sum-dp[i-n]+mod)%mod+dp[i])%mod;
				}
				LL ans = 0;
				REP(i,k){
					ans = (ans+dp[i])%mod;
				}
				cout << dp[k-1] << ' ' << ans << endl;
			}
		};
		if(n <= 31){
			solvemat();
		}else{
			solvedp();
		}
	}
	return 0;
}
0