結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー mint6421mint6421
提出日時 2019-07-04 15:24:53
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,135 bytes
コンパイル時間 1,550 ms
コンパイル使用メモリ 168,560 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 07:37:16
合計ジャッジ時間 3,437 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 52 ms
4,348 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 20 ms
4,348 KB
testcase_05 AC 16 ms
4,348 KB
testcase_06 AC 20 ms
4,348 KB
testcase_07 AC 31 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 25 ms
4,348 KB
testcase_10 AC 9 ms
4,348 KB
testcase_11 AC 10 ms
4,348 KB
testcase_12 AC 17 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 40 ms
4,348 KB
testcase_16 AC 33 ms
4,348 KB
testcase_17 AC 9 ms
4,348 KB
testcase_18 AC 34 ms
4,348 KB
testcase_19 AC 48 ms
4,348 KB
testcase_20 AC 15 ms
4,348 KB
testcase_21 WA -
testcase_22 AC 15 ms
4,348 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 9 ms
4,348 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 50 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 14 ms
4,348 KB
testcase_33 AC 21 ms
4,348 KB
testcase_34 AC 17 ms
4,348 KB
testcase_35 AC 14 ms
4,348 KB
testcase_36 AC 37 ms
4,348 KB
testcase_37 AC 4 ms
4,348 KB
testcase_38 AC 42 ms
4,348 KB
testcase_39 AC 17 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:127:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
  127 | main(){
      | ^~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll

void init(){
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n,k;
int a[1100];

#define mat vector<vector<int>> 

mat mul_mat(mat &a, mat &b){
  mat res(a.size(), vector<int>(b[0].size(),0));
  rep(i, a.size())rep(k, b.size())rep(j, b[0].size()){
    res[i][j] = (res[i][j] + (a[i][k] * b[k][j])%M)%M;
  }
  return res;
}
mat power_mat(mat &a,int k){
  mat res(a.size(), vector<int>(a.size()));
  rep(i, a.size()) res[i][i] = 1;
  while(k > 0){
    if(k&1) res = mul_mat(res,a);
    a = mul_mat(a,a);
    k >>= 1;
  }
  return res;
}


int f[1100000];

void solve1(){
  
 
  int sum=0,ans=0;
  rep(i,n){

    f[i]=a[i];
    sum+=f[i];
    sum%=M;
    ans+=f[i];
    ans%=M;
  }

  FOR(i,n,k-1){
    int res=sum;
    sum-=f[i%n];
    sum+=res+M;
    sum%=M;
    f[i%n]=res;
    ans+=res;
    ans%=M;
    
  }


  cout<<sum<<' '<<(ans+sum)%M<<endl;
}

void solve2(){
  mat s(n,vector<int>(n,0));
  rep(i,n){
    s[i][0]=1;
  }
  rep(i,n-1){
    s[i][i+1]=1;
  }
  mat t(2*n,vector<int>(2*n,0));
  rep(i,n){
    rep(j,n){
      t[i][j]=s[i][j];
      if(i==j){
        t[i+n][i]=1;
        t[i+n][i+n]=1;
      }
    }
  }
 
  s = power_mat(s,k-n);
  int res=0;
  rep(i,n){
    res+=(a[n-1-i]*s[i][0])%M;
    res%=M;
  }

  
  t = power_mat(t,k-n+1);
  t[n][0]--;
  int ans=0;

  rep(i,n){
    ans+=(a[n-1-i]*t[i+n][0])%M;
    ans%=M;
    ans+=a[i];
    ans%=M;
  }

  cout<<res<<' '<<ans<<endl;
  
}

main(){
  cin>>n>>k;
  rep(i,n){
    cin>>a[i];
  
  }

  if(k<=1000000) solve1();
  else solve2();

}
0