結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー cielciel
提出日時 2015-04-27 01:55:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,180 bytes
コンパイル時間 507 ms
コンパイル使用メモリ 64,932 KB
実行使用メモリ 15,976 KB
最終ジャッジ日時 2023-09-18 12:39:45
合計ジャッジ時間 2,128 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
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 32 ms
13,080 KB
testcase_21 AC 32 ms
13,088 KB
testcase_22 AC 31 ms
11,120 KB
testcase_23 AC 4 ms
4,384 KB
testcase_24 AC 16 ms
7,416 KB
testcase_25 AC 16 ms
9,264 KB
testcase_26 AC 16 ms
8,136 KB
testcase_27 AC 19 ms
11,332 KB
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 31 ms
15,976 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <valarray>
#include <vector>
#include <cstdio>
using namespace std;
typedef valarray<__int128_t>V; //lol
int _N,MOD=1000000007;
V z;
V &Me(const V &_x,const V &_y){
	int i=0,j;
	for(;i<_N;i++)for(j=0;j<_N;j++)z[i*_N+j]=(_x[slice(i*_N,_N,1)]*_y[slice(j,_N,_N)]).sum()%MOD;
	return z;
}
V &Mx(const V &_x){
	int i=0,j;
	for(;i<_N;i++)for(j=0;j<_N;j++)z[i*_N+j]=(_x[slice(i*_N,_N,1)]*_x[slice(j,_N,_N)]).sum()%MOD;
	return z;
}

int main(){
	int N;
	long long K;
	scanf("%d%lld",&N,&K);
	vector<long long>A(N);
	for(int i=0;i<N;i++)scanf("%lld",&A[i]);
	long long accum=0;
	for(int i=0;i<N;i++)accum=(accum+A[i])%MOD;
	if(K<=1000000){
		long long s=accum;
		for(int i=N;i<K;i++){
			A.push_back(accum);
			s=(s+accum)%MOD;
			accum=(accum+A[i]-A[i-N]+MOD)%MOD;
		}
		printf("%lld %lld\n",A[K-1],s);
	}else{
		_N=N+1;
		V x(_N*_N);
		V e(_N*_N);
		z.resize(_N*_N);
		for(int i=0;i<_N;i++)e[i*_N+i]=1;
		for(int i=0;i<N;i++)x[_N+i+1]=1;
		for(int i=0;i<_N;i++)x[i]=x[(i+2)*_N+i+1]=1;
		for(K-=N;K;K>>=1){
			if(K&1)e=Me(e,x);
			x=Mx(x);
		}
		long long f=0,s=accum*x[0];
		for(int i=1;i<=N;i++)f=(f+e[_N+i]*A[N-i])%MOD,s=(s+e[i]*A[N-i])%MOD;
		printf("%lld %lld\n",f,s);
	}
}
0