結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー sugarrrsugarrr
提出日時 2018-03-10 16:49:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 3,030 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 88,660 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-22 15:13:08
合計ジャッジ時間 2,510 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<set>
#include<map>
#include<bitset>

using namespace std;
typedef long long ll;
#define i_7 1000000007
#define i_5 1000000005

ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    else return c+i_7;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
#define inf 100000000/*10^8*/
#define rep(i,l,r) for(int i=l;i<=r;i++)
const double EPS=1E-8;

////////////////////////////////////////
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat a,mat b){/*行列aと行列bの積*/
    mat c(a.size(),vec(b[0].size()));
    for(int i=0;i<a.size();i++){
        for(int j=0;j<b[0].size();j++){
            for(int k=0;k<a[0].size();k++){
                c[i][j]=mod(c[i][j]+a[i][k]*b[k][j]);
            }
        }
    }
    return c;
}

mat pow(mat a,ll n){/*行列aのn乗*/
    mat b(a.size(),vec(a.size()));
    for(int i=0;i<a.size();i++){
        b[i][i]=1;
    }
    while(n>0){
        if(n&1)b=mul(b,a);
        a=mul(a,a);
        n>>=1;
    }
    return b;
}
//////////////////////////////////////////
void print(mat q){
    cout<<endl;
    rep(i,0,q.size()-1){
        rep(j,0,q[0].size()-1){
            cout<<q[i][j]<<" ";
        }cout<<endl;
    }
}

int main() {
    ll n,k;cin>>n>>k;
    ll a[n+1];rep(i,0,n-1)cin>>a[i];
    if(k<=1000000){
        ll b[k];
        ll all=0,sum=0;
        rep(i,0,n-1){
            b[i]=a[i];
            all=mod(all+b[i]);
            sum=mod(sum+b[i]);
            /*cout<<all<<" "<<sum<<endl;*/
        }
        rep(i,n,k-1){
            b[i]=sum;
            sum=mod(sum+b[i]-b[i-n]);
            all=mod(all+b[i]);
            /*cout<<all<<" "<<sum<<endl;*/
        }
        cout<<b[k-1]<<" "<<all<<endl;
    }else{
        for(int i=n;i>=1;i--)a[i]=a[i-1];
        mat x(n,vec(n));
        rep(i,0,n-1){
            rep(j,0,n-1){
                if(i==0){
                    x[i][j]=1;
                }else{
                    if(i==j+1)x[i][j]=1;
                    else x[i][j]=0;
                }
            }
        }
        mat xx(n,vec(1));
        rep(i,0,n-1)xx[i][0]=a[n-i];
        xx=mul(pow(x,k-n),xx);
        cout<<xx[0][0]<<" ";
        
        
        mat y(n+1,vec(n+1));
        rep(i,0,n){
            rep(j,0,n){
                if(i==0){
                    if(j==0)y[i][j]=2;
                    else if(j==n)y[i][j]=-1;
                    else y[i][j]=0;
                }else{
                    if(i==j+1)y[i][j]=1;
                    else y[i][j]=0;
                }
            }
        }
        /*print(y);*/
        mat yy(n+1,vec(1));
        yy[n][0]=a[1];
        rep(i,1,n-1)yy[n-i][0]=mod(yy[n-i+1][0]+a[i+1]);
        yy[0][0]=0;
        rep(i,1,n)yy[0][0]=mod(yy[0][0]+2*a[i]);
       /* print(yy);*/
        mat z=pow(y,k-n-1);
        yy=mul(z,yy);
        cout<<yy[0][0]<<endl;
        
        
    }
    return 0;
}
0