結果
問題 |
No.44 DPなすごろく
|
ユーザー |
|
提出日時 | 2014-10-27 08:55:57 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,235 bytes |
コンパイル時間 | 1,917 ms |
コンパイル使用メモリ | 170,624 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-30 13:46:14 |
合計ジャッジ時間 | 2,897 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; template<class _Tc>class Matrix{private:unsigned int r,c;vector<vector<_Tc>>v;public:unsigned int rows(){return r;}unsigned int columns(){return c;}unsigned int size(){return r*c;}Matrix(unsigned int rows,unsigned int columns):r(rows),c(columns){v.resize(rows);for(unsigned int i=0;i<rows;++i)v[i].resize(columns);}vector<_Tc>&operator[](unsigned int row){return v[row];}const Matrix&operator*=(const Matrix&m){if(c!=m.r)throw;Matrix n(r,m.c);for(unsigned int i=0;i<r;++i)for(unsigned int j=0;j<m.c;++j)for(unsigned int k=0;k<c;++k)n.v[i][j]+=v[i][k]*m.v[k][j];return*this=n;}const Matrix&operator*=(const _Tc &m){for(unsigned int i=0;i<r;++i)for(unsigned int j=0;j<c;++j)v[i][j]*=m;return*this;}static const Matrix unit(unsigned int s,_Tc v=1){Matrix n(s,s);for(unsigned int i=0;i<s;++i)n.v[i][i]=v;return n;}const Matrix pow(unsigned int p)const{if(c!=r)throw;Matrix n=Matrix::unit(r),m=*this;while(p>0){if(p&1)n*=m;m*=m;p>>=1;}return n;}}; long long fib(int n){ Matrix<long long> a(2,2); a[0][0] = a[0][1] = a[1][0] = 1; a[1][1] = 0; a = a.pow(n); return a[1][0]; } int main(){ int n; cin >> n; cout << fib(n+1) << endl; return 0; }