結果
問題 | No.194 フィボナッチ数列の理解(1) |
ユーザー | sntea |
提出日時 | 2016-10-10 12:00:16 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 23 ms / 5,000 ms |
コード長 | 4,284 bytes |
コンパイル時間 | 2,606 ms |
コンパイル使用メモリ | 176,360 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-11-22 00:49:46 |
合計ジャッジ時間 | 3,550 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#ifdef LOCAL111#else#define NDEBUG#endif#include <bits/stdc++.h>const int INF = 1e9;using namespace std;template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os;}#define endl '\n'#define ALL(a) (a).begin(),(a).end()#define SZ(a) int((a).size())#define FOR(i,a,b) for(int i=(a);i<(b);++i)#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)#define REP(i,n) FOR(i,0,n)#define RREP(i,n) for (int i=(n)-1;i>=0;i--)#define RBP(i,a) for(auto& i : a)#ifdef LOCAL111#define DEBUG(x) cout<<#x<<": "<<(x)<<endltemplate<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}#else#define DEBUG(x) truetemplate<typename T> void dpite(T a, T b){ return; }#endif#define F first#define S second#define SNP string::npos#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "#define rangej(a,b,c) ((a) <= (c) and (c) < (b))#define rrangej(b,c) rangej(0,b,c)template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}typedef long long int LL;typedef unsigned long long ULL;typedef pair<int,int> P;typedef pair<LL,LL> LP;void ios_init(){//cout.setf(ios::fixed);//cout.precision(12);#ifdef LOCAL111return;#endifios::sync_with_stdio(false); cin.tie(0);}const LL mod = 1e9+7;//librarytemplate<typename T>class Matrix {public:vector<vector<T> > v;Matrix(int n){v.resize(n,vector<T>(n,0));}Matrix operator+ (Matrix x){Matrix res(v.size());for(int i = 0; i < (int)v.size(); i++){for(int j = 0; j < (int)v.size(); j++){res.v[i][j] = (x.v[i][j]+v[i][j])%mod;}}return res;}Matrix operator-(Matrix x){Matrix res(v.size());for(int i = 0; i < (int)v.size(); i++){for(int j = 0; j < (int)v.size(); j++){res.v[i][j] = (v[i][j]-x.v[i][j])%mod;}}}Matrix operator*(Matrix x){Matrix res(v.size());for(int i = 0;i < (int)v.size(); i++){for(int j = 0; j < (int)v.size(); j++){for(int k = 0; k < (int)v.size(); k++){res.v[i][j] += v[i][k]*x.v[k][j]%mod;res.v[i][j] %= mod;}}}return res;}vector<T> operator*(vector<T> x){assert(x.size() == v.size());vector<T> res(v.size());for(int i = 0; i < (int)v.size(); i++){T tmp = 0;for(int j = 0; j < (int)v.size(); j++){tmp += v[i][j]*x[j]%mod;tmp %= mod;}res[i] = tmp;}return res;}vector<T>& operator[](int x){return v[x];}};template<typename T>Matrix<T> pow(Matrix<T> x,long long n){Matrix<T> tmp = x, res(SZ(x.v));for(int i = 0; i < SZ(x.v); i++) res.v[i][i] = 1;while(n != 0){if(n&1){res = res*tmp;}tmp = tmp*tmp;n >>= 1;}return res;}//libaryint main(){ios_init();int n;LL k;while(cin >> n >> k) {DEBUG(n);vector<LL> a(n);REP(i,n) cin >> a[i];function<void()> solvemat = [&](){Matrix<LL> A(n);REP(i,n) A[0][i] = 1;REP(i,n-1) A[i+1][i] = 1;Matrix<LL> B(n+1);B[0][0] = B[0][1] = 1;REP(i,n) B[1][i+1] = 1;REP(i,n-1) B[i+2][i+1] = 1;REP(i,n+1) dpite(ALL(B[i]));if(k <= n){k--;cout << a[k] << ' ' << accumulate(a.begin(),a.begin()+k,0LL) << endl;}else{A = pow(A,k-n);vector<LL> b = a;b.push_back(accumulate(a.begin(),--a.end(),0LL));reverse(ALL(a));auto ans = A*a;// reverse(ALL(a));reverse(ALL(b));dpite(ALL(b));B = pow(B,k-n+1);auto ans2 = B*b;dpite(ALL(ans2));cout << ans[0] << ' ' << ans2[0] << endl;}};function<void()> solvedp = [&](){if(k <= n){cout << a[k-1] << endl;}else{vector<LL> dp(k,0);LL sum = 0;REP(i,SZ(a)){sum = (sum+a[i])%mod;dp[i]= a[i];}FOR(i,n,k){dp[i] = sum;sum = ((sum-dp[i-n]+mod)%mod+dp[i])%mod;}LL ans = 0;REP(i,k){ans = (ans+dp[i])%mod;}cout << dp[k-1] << ' ' << ans << endl;}};if(n <= 31){solvemat();}else{solvedp();}}return 0;}