結果
問題 | No.194 フィボナッチ数列の理解(1) |
ユーザー | kei |
提出日時 | 2018-09-17 00:14:12 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,016 bytes |
コンパイル時間 | 1,781 ms |
コンパイル使用メモリ | 177,884 KB |
実行使用メモリ | 18,944 KB |
最終ジャッジ日時 | 2024-07-18 07:34:02 |
合計ジャッジ時間 | 3,187 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 19 ms
5,376 KB |
testcase_03 | AC | 4 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 | 12 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 | 4 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 15 ms
5,376 KB |
testcase_16 | AC | 13 ms
5,376 KB |
testcase_17 | AC | 5 ms
5,376 KB |
testcase_18 | AC | 14 ms
5,376 KB |
testcase_19 | AC | 19 ms
5,376 KB |
testcase_20 | AC | 16 ms
18,688 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 9 ms
9,792 KB |
testcase_27 | AC | 10 ms
11,520 KB |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | AC | 18 ms
5,376 KB |
testcase_31 | AC | 2 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 | 2 ms
5,376 KB |
testcase_38 | AC | 16 ms
5,376 KB |
testcase_39 | AC | 7 ms
5,376 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:https://yukicoder.me/problems/no/194> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ const ll MOD = 1e9+7; typedef vector<ll> vec; typedef vector<vec> mat; mat mul(mat&A,mat&B,const ll M){ mat C(A.size(),vec(B[0].size())); for(int i = 0; i < (int)A.size();i++){ for(int k = 0; k < (int)B.size();k++){ for(int j = 0; j < B[0].size();j++){ C[i][j] = (C[i][j] + A[i][k]*B[k][j])%M; } } } return C; } mat pow(mat A,ll n,const ll M){ 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,M); A = mul(A,A,M); n>>=1; } return B; } vector<ll> solve(){ vector<ll> res; ll N,K; cin >> N >> K; vector<ll> A(N); for(auto& in:A) cin >> in; if(K<=1e6){ // testcase01~10 vector<ll> F(K+1),S(K+1); for(int i = 0; i < N;i++){ (F[i+1] = A[i])%=MOD; (S[i+1] = S[i] + A[i])%=MOD; } for(ll i = N+1; i <= K;i++){ (F[i] = S[i-1] - S[i-1-N])%=MOD; (S[i] = S[i-1] + F[i])%=MOD; } res = vector<ll>{F[K],S[K]}; }else{ // testcase11~20 vector<vector<ll>> Mat(N+1,vector<ll>(N+1)); for(int i = 0; i <= N;i++) Mat[0][i] = 1; for(int i = 1; i <= N;i++) Mat[1][i] = 1; for(int i = 2; i <= N;i++) Mat[i][i-1] = 1; auto retMat = pow(Mat,K-N,MOD); vector<vector<ll>> B(N+1,vector<ll>(1)); B[0][0] = accumulate(A.begin(),A.end(),0LL)%MOD; for(int i = 1; i <= N;i++) B[i][0] = A[N-i]; auto ansMat = mul(retMat,B,MOD); res = vector<ll>{ansMat[1][0],ansMat[0][0]}; } return res; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); cout << solve() << endl; return 0; }