結果
| 問題 | No.194 フィボナッチ数列の理解(1) |
| コンテスト | |
| ユーザー |
btk
|
| 提出日時 | 2017-05-20 17:49:44 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 18 ms / 5,000 ms |
| コード長 | 3,072 bytes |
| 記録 | |
| コンパイル時間 | 1,761 ms |
| コンパイル使用メモリ | 175,488 KB |
| 実行使用メモリ | 19,200 KB |
| 最終ジャッジ日時 | 2024-09-19 00:28:53 |
| 合計ジャッジ時間 | 3,226 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define CIN_ONLY if(1)
struct cww{cww(){
CIN_ONLY{
ios::sync_with_stdio(false);cin.tie(0);
}
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if(0)
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
for(auto &it:v)is>>it;
return is;
}
const int mod=1e9+7;
typedef LL D;
#define REP4(i,n) for(int i=0;i<n;i+=4)
#define SZ 32
int sz=SZ;
struct M{
D v[SZ][SZ];
D& operator()(int i,int j){
return v[i][j];
}
}mat[64];
void mat_mul(M &a,M& b,M &c){
REP(i,sz)REP(j,sz)c(i,j)=0;
D sum[4],tmp;
REP(k,sz){
REP4(i,sz){
sum[0]=a(i,k);
sum[1]=a(i+1,k);
sum[2]=a(i+2,k);
sum[3]=a(i+3,k);
REP(j,sz){
tmp=b(k,j);
c(i,j)+=sum[0]*tmp%mod;
c(i+1,j)+=sum[1]*tmp%mod;
c(i+2,j)+=sum[2]*tmp%mod;
c(i+3,j)+=sum[3]*tmp%mod;
}
}
}
REP(i,sz)REP(j,sz)c(i,j)%=mod;
}
LL N,K;
LL S[1123456];
LL A[1123456];
typedef pair<LL,LL> P;
P case1(){
FOR(i,N,K){
A[i] = (mod + S[i] - S[i - N]) % mod;
S[i+1] = (A[i] + S[i])%mod;
}
return P(A[K-1],S[K]);
}
P case2(){
P res;
{
REP(i,N)REP(j,N)mat[0](i,j)=0;
REP(i,N-1)mat[0](i+1,i)=1;
REP(i,N)mat[0](0,i)=1;
REP(i,50)mat_mul(mat[i],mat[i],mat[i+1]);
LL bit=K-N;
vector<LL> f(N);
REP(i,N)f[i]=A[i];
reverse(ALL(f));
for(int l=0;bit;l++,bit>>=1){
if(bit&1){
vector<LL> g(N);
REP(i,N)REP(j,N)g[i]+=mat[l](i,j)*f[j]%mod;
REP(i,N)g[i]%=mod;
swap(f,g);
}
}
res.fi=f.front();
}
{
REP(i,N+1)REP(j,N+1)mat[0](i,j)=0;
REP(i,N)mat[0](i+1,i)=1;
mat[0](0,0)=2;
mat[0](0,N)=mod-1;
REP(i,50)mat_mul(mat[i],mat[i],mat[i+1]);
LL bit=K-N;
vector<LL> f(N+1);
REP(i,N+1)f[i]=S[i];
reverse(ALL(f));
for(int l=0;bit;l++,bit>>=1){
if(bit&1){
vector<LL> g(N+1);
REP(i,N+1)REP(j,N+1)g[i]+=mat[l](i,j)*f[j]%mod;
REP(i,N+1)g[i]%=mod;
swap(f,g);
}
}
res.se=f.front();
}
return res;
}
int main(void){
cin >> N >> K;
S[0]=0;
REP(i,N){
cin >> A[i];
S[i+1] = S[i] + A[i];
S[i] %= mod;
}
pair<LL, LL> res;
if (K <= 1000000)res= case1();
else res = case2();
cout << res.fi << " " << res.se << endl;
return 0;
}
btk