結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー nikollsonnikollson
提出日時 2015-04-28 21:24:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 2,197 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 84,012 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-18 14:12:25
合計ジャッジ時間 2,512 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<deque>

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)
#define pb push_back

using namespace std;

#ifdef __BORLANDC__
	typedef __int64 ll;
#else
	typedef long long ll;
#endif

const ll MOD = 1000000007;

class P:public vector<vector<ll> >{
	public:
	P(int n){
		rep(i,n){
			vector<ll> dat(n);
			this->pb(dat);
		}
	}
};

class V:public vector<ll>{
	public:
	V(int n){
		rep(i,n)this->pb(0);
	}
};


P multi(P& a, P& b){
	
	int n = a.size();
	P ret(n);
	
	rep(i,n){
		rep(j,n){
			int num = 0;
			rep(k,n){
				num = (num+a[i][k]*b[k][j])%MOD;
			}
			ret[i][j] = num;
		}
	}
	return ret;
}

V multi(P& a, V& b){
	
	int n = a.size();
	V ret(n);
	rep(i,n){
		int num = 0;
		rep(j,n){
			num = (num+a[i][j]*b[j])%MOD;
		}
		ret[i] = num;
	}
	
	return ret;
}

P getP(int n){
	P ret(n+1);
	
	rep(i,n-1){
		rep(j,n){
			if(j-1==i)ret[i][j]=1;
			else ret[i][j] = 0;
		}
	}
	rep(i,n)ret[n-1][i]=1;
	
	rep(i,n)ret[i][n]=0;
	rep(i,n+1)ret[n][i]=1;
	
	return ret;
}

void print(P& a){
	rep(i,a.size()){
		rep(j,a.size()){
			printf("%d ",a[i][j]);
		}puts("");
	}
}
void print(V& a){
	rep(i,a.size()){
		printf("%d ",a[i]);
	}puts("");
}


int main(){
	
	
	ll n,k;
	
	cin>>n>>k;
	vector<ll> a(n);
	rep(i,n)cin>>a[i];
	
	ll ansF = -1;
	ll ansS = -1;
	
	if(n<=30){
		const int T = 50;
		vector<P> b;
		b.pb(getP(n));
		
		reps(i,1,T){
			b.pb(multi(b[i-1], b[i-1]));
		}
		
		V vec(n+1);
		rep(i,n)vec[i] = a[i];
		rep(i,n)vec[n] = (vec[n]+a[i])%MOD;
		
		ll tk = k-n;
		
		ll sc = 1;
		rep(i,T){
			if((tk&sc)>0){
				vec = multi(b[i], vec);
			}
			sc*=2;
		}
		
		ansF = vec[n-1];
		ansS = vec[n];
		
	}else{
		deque<ll> b;
		rep(i,n)b.pb(a[i]);
		
		ll sum = 0;
		rep(i,n)sum += b[i];
		
		ll allsum = sum;
		rep(i,k-n){
			ll next = sum;
			allsum = (allsum+sum)%MOD;
			
			ll front = b[0];
			sum = (sum + next - front + MOD)%MOD;
			//rep(j,b.size())cout<<b[j]<<" ";cout<<endl;
			
			b.pop_front();
			b.pb(next);
		}
		
		ansF = b[n-1];
		ansS = allsum;
		
	}
	cout<<ansF<<" "<<ansS<<endl;
}


/*
3 6
1 1 1

5 8
1 2 3 4 5

*/
0