結果
問題 | No.720 行列のできるフィボナッチ数列道場 (2) |
ユーザー | algon_320 |
提出日時 | 2018-07-28 04:12:33 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 5,031 bytes |
コンパイル時間 | 1,658 ms |
コンパイル使用メモリ | 183,340 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 18:13:54 |
合計ジャッジ時間 | 2,304 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 1 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 1 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 1 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long using ll=long long; using vi=vector<int>; using pii=pair<int,int>; #define ALL(c) begin(c),end(c) #define RALL(c) rbegin(c),rend(c) #define ITR(i,b,e) for(auto i=(b);i!=(e);++i) #define FORE(x,c) for(auto &x:c) #define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i) #define REP(i,n) REPF(i,0,n) #define REPR(i,n) for(int i=(int)(n);i>=0;--i) #define SZ(c) ((int)c.size()) #define CONTAIN(c,x) (c.find(x)!=end(c)) #define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w)) #define dump(...) #define pb push_back const signed INF_=1001001001; const long long INF=1001001001001001001LL; const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0}; template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;} template<class T> istream& operator>>(istream &is,vector<T> &v) { ITR(i,begin(v),end(v)) is>>*i;return is;} template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) { is>>p.first>>p.second;return is;} template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;} template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;} template<class T> using heap=priority_queue<T,vector<T>,greater<T>>; struct before_main_function { before_main_function() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(15) << fixed; #define endl "\n" } } before_main_function; //------------------8<------------------------------------8<-------------------- template<typename T> void resize_matrix(vector<vector<T>> &A, int h, int w, T fill) { A.resize(h); for (int i = 0; i < h; i++) { A[i].resize(w, fill); } } template<typename T> vector<vector<T>> multiple_matrix(vector<vector<T>> &A, vector<vector<T>> &B, function<T(T, T)> add = [](T a, T b){return a + b;}, function<T(T, T)> mul = [](T a, T b){return a * b;}, T zero = 0) { int m = A.size(); int n = A[0].size(); assert(n == B.size()); int l = B[0].size(); vector<vector<T>> res; resize_matrix(res, m, l, zero); for (int i = 0; i < m; i++) { for (int j = 0; j < l; j++) { T tmp = zero; for (int k = 0; k < n; k++) { T p = mul(A[i][k], B[k][j]); tmp = add(tmp, p); } res[i][j] = tmp; } } return res; } template <typename T> vector<vector<T>> pow_matrix(vector<vector<T>> A, int k, function<T(T, T)> add = [](T a, T b){return a + b;}, function<T(T, T)> mul = [](T a, T b){return a * b;}, T e = 1, T zero = 0) { int n = A.size(); assert(n == A[0].size()); vector<vector<T>> res; resize_matrix(res, n, n, zero); for (int i = 0; i < n; i++) res[i][i] = e; while (k > 0) { if (k & 1) res = multiple_matrix(res, A, add, mul, zero); A = multiple_matrix(A, A, add, mul, zero); k >>= 1; } return res; } template <int mod = 1000000007> class ModInt { public: ModInt() : v(0) {} ModInt(int x) : v((x+mod)%mod) {} int value() const {return v;} const ModInt operator+(const ModInt &r) const { return ModInt(this->v + r.v); } const ModInt operator-(const ModInt &r) const { return ModInt(this->v + mod - r.v); } const ModInt operator*(const ModInt &r) const { return ModInt(this->v * r.v); } const ModInt operator/(const ModInt &r) const { return (*this * (~r)); } const ModInt operator~() const { return ModInt(bpow(this->v, mod-2)); } bool operator==(const ModInt &r) const { return this->v == r.v; } bool operator!=(const ModInt &r) const { return this->v != r.v; } ModInt& operator+=(const ModInt &r) { return *this = *this + r; } ModInt& operator-=(const ModInt &r) { return *this = *this - r; } ModInt& operator*=(const ModInt &r) { return *this = *this * r; } ModInt& operator/=(const ModInt &r) { return *this = *this * (~r); } private: int v; int bpow(int a, int b) const { int ret = 1; while (b > 0) { if (b & 1) ret = (ret * a) % mod; a = (a * a) % mod; b >>= 1; } return ret; } }; using Mint = ModInt<>; signed main() { int N, M; cin >> N >> M; vector<vector<Mint>> A = {{0, 1}, {1, 1}}; A = pow_matrix(A, M); vector<vector<Mint>> B = { {A[0][0], A[0][1], 0}, {A[1][0], A[1][1], 0}, {A[1][0], A[1][1], 1}, }; B = pow_matrix(B, N - 1); auto fib = [](int n) { if (n == 1 || n == 2) return Mint(1); vector<vector<Mint>> A = {{0, 1}, {1, 1}}; auto B = pow_matrix(A, n - 2); vector<vector<Mint>> v = {{1}, {1}}; v = multiple_matrix(B, v); return v[1][0]; }; Mint fib_m_1 = fib(M - 1), fib_m = fib(M); vector<vector<Mint>> v = { {fib_m_1}, {fib_m}, {fib_m} }; v = multiple_matrix(B, v); cout << v[2][0].value() << endl; return 0; }