結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー hirokazu1020hirokazu1020
提出日時 2015-04-26 23:41:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 3,059 bytes
コンパイル時間 747 ms
コンパイル使用メモリ 81,624 KB
実行使用メモリ 7,480 KB
最終ジャッジ日時 2023-09-18 11:53:29
合計ジャッジ時間 2,536 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 12 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 8 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 10 ms
4,376 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 13 ms
4,376 KB
testcase_20 AC 22 ms
7,296 KB
testcase_21 AC 23 ms
7,256 KB
testcase_22 AC 22 ms
7,480 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 13 ms
5,208 KB
testcase_25 AC 11 ms
4,996 KB
testcase_26 AC 12 ms
5,012 KB
testcase_27 AC 13 ms
5,496 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 21 ms
6,880 KB
testcase_30 AC 12 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 6 ms
4,376 KB
testcase_34 AC 5 ms
4,376 KB
testcase_35 AC 5 ms
4,376 KB
testcase_36 AC 9 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 11 ms
4,376 KB
testcase_39 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int F2(int, long long int, int*)’:
main.cpp:45:10: warning: ‘x.Matrix<>::_col’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(_row*_col!=r*c){
      ~~~~^~~~~
main.cpp:91:25: note: ‘x.Matrix<>::_col’ was declared here
   Matrix res(size,size),x(*this);
                         ^
main.cpp: In function ‘int S2(int, long long int, int*)’:
main.cpp:45:10: warning: ‘x.Matrix<>::_col’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(_row*_col!=r*c){
      ~~~~^~~~~
main.cpp:91:25: note: ‘x.Matrix<>::_col’ was declared here
   Matrix res(size,size),x(*this);
                         ^

ソースコード

diff #

#include<sstream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<cassert>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())
#define indexOf(v,x) (find(all(v),x)-v.begin())

#define MOD 1000000007



template<class T=long long>
class Matrix{
	int _row,_col;
	T* p;
public:
	Matrix():_row(0),_col(0),p(0){}
	Matrix(int row,int col):_row(row),_col(col){
		p=new T[_row*_col];
	}
	Matrix(const Matrix& a):_row(-1),p(0){
		copy(a);
	}
	~Matrix(){
		delete[] p;
	}
	int row()const{return _row;}
	int col()const{return _col;}
	void resize(int r,int c){
		if(_row*_col!=r*c){
			delete[] p;
			p=new T[r*c];		
		}
		_row=r;
		_col=c;
	}
	void swap(Matrix& a){
		std::swap(_row,a._row);
		std::swap(_col,a._col);
		std::swap(p,a.p);
	}
	void copy(const Matrix& a){
		resize(a._row,a._col);
		for(int i=0;i<row();i++)
			for(int j=0;j<col();j++)
				p[i*col()+j]=a[i][j];
	}
	T* operator[](int i)const{return p+(i*_col);}
	Matrix& operator =(const Matrix &a){
		copy(a);
		return *this;
	}
	Matrix& operator *=(const Matrix &a){
		(*this*a).swap(*this);
		return *this;
	}
	Matrix operator *(const Matrix &a)const{
		assert(col()==a.row());
		Matrix res(row(),a.col());
		for(int i=0;i<row();i++){
			for(int j=0;j<a.col();j++)res[i][j]=0;
			for(int k=0;k<col();k++){
				T da=p[i*col()+k];
				const T * const __restrict pb=a[k];
				T * const __restrict pc=res[i];
				for(int j=0;j<a.col();j++){
					pc[j] = ((pc[j]+da*pb[j])%MOD+MOD)%MOD;
				}
			}
		}
		return res;
	}
	Matrix pow(long long n)const{
		assert(row()==col());
		int size=row();
		Matrix res(size,size),x(*this);
		for(int i=0;i<size;i++){
			for(int j=0;j<size;j++)res[i][j]=i==j;
		}
		while(n){
			if(n&1){
				(res*x).swap(res);
			}
			(x*x).swap(x);
			n>>=1;
		}
		return res;
	}
};


int F1(int n,int k,int a[]){
	int s=0;
	rep(i,n)(s+=a[i])%=MOD;
	for(int i=n;i<k;i++){
		a[i]=s;
		s=((s-a[i-n]+s)%MOD+MOD)%MOD;
	}
	return a[k-1];
}

int S1(int n,int k,int a[]){
	int s=0,ans=0;
	rep(i,n)(s+=a[i])%=MOD,(ans+=a[i])%=MOD;
	for(int i=n;i<k;i++){
		a[i]=s;
		(ans+=a[i])%=MOD;
		s=((s-a[i-n]+s)%MOD+MOD)%MOD;	
	}
	return ans;
}

int F2(int n,long long k,int a[]){
	Matrix<> mat(n,n),b(1,n);
	rep(i,n)rep(j,n)mat[i][j]=j==0||i+1==j;
	rep(i,n)b[0][i]=a[n-1-i];
	return (b*mat.pow(k-n))[0][0];
}

int S2(int n,long long k,int a[]){
	Matrix<> mat(n+1,n+1),b(1,n+1);
	rep(i,n+1)rep(j,n+1)mat[i][j]=0;
	rep(i,n)rep(j,n)mat[i][j]=j==0||i+1==j;
	rep(i,n+1)mat[i][n]=1;

	rep(i,n)b[0][i]=a[n-1-i];
	b[0][n]=0;
	int ans=0;
	rep(i,n)(ans+=a[i])%=MOD;
	ans+=(b*mat.pow(k-n))[0][n];
	return ans%MOD;
}

int a[1000000];

int main(){
	long long n,k;
	cin>>n>>k;
	rep(i,n)cin>>a[i];
	if(n<=10000&&k<=1000000)cout<<F1(n,k,a)<<' '<<S1(n,k,a)<<endl;
	else cout<<F2(n,k,a)<<' '<<S2(n,k,a)<<endl;
	return 0;
}
0