結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー btkbtk
提出日時 2017-05-20 17:43:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,058 bytes
コンパイル時間 1,778 ms
コンパイル使用メモリ 174,924 KB
実行使用メモリ 20,736 KB
最終ジャッジ日時 2023-10-19 04:13:04
合計ジャッジ時間 3,864 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,732 KB
testcase_01 AC 2 ms
5,732 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 15 ms
20,732 KB
testcase_21 AC 16 ms
20,736 KB
testcase_22 AC 16 ms
20,732 KB
testcase_23 AC 4 ms
8,100 KB
testcase_24 AC 9 ms
14,592 KB
testcase_25 AC 9 ms
14,592 KB
testcase_26 AC 9 ms
14,592 KB
testcase_27 AC 10 ms
16,640 KB
testcase_28 AC 5 ms
10,496 KB
testcase_29 AC 15 ms
20,736 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<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)REP(j,N)mat[0](i,j)=0;
        REP(i,N-1)mat[0](i+1,i)=1;
        mat[0](0,0)=2;
        mat[0](0,N-1)=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]=S[i+1];
        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.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;
}
0