結果
問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める |
ユーザー | morimario |
提出日時 | 2021-01-29 14:35:49 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 7,399 bytes |
コンパイル時間 | 2,509 ms |
コンパイル使用メモリ | 214,080 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-27 01:59:20 |
合計ジャッジ時間 | 3,168 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,944 KB |
testcase_14 | AC | 2 ms
6,940 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using pll = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using pdd = pair<double, double>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using pbb = pair<bool, bool>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using pss = pair<string, string>; using vs = vector<string>; using vvs = vector<vs>; using vvvs = vector<vvs>; #define repeat(counter, start, goal, step) for (int counter = (int)(start); counter < (int)(goal); counter += step) #define rep(counter, times) repeat(counter, 0, times, 1) #define rep2(counter, start, goal) repeat(counter, start, goal, 1) #define rep0(times) repeat(counter, 0, times, 1) #define rrepeat(counter, start, goal, step) for (int counter = (int)(start); counter > (int)(goal); counter -= step) #define rep3(counter, start, goal) rrepeat(counter, start, goal, 1) #define rrep(counter, start, goal) rrepeat(counter, start - 1, goal - 1, 1) #define all(container) begin(container), end(container) template <typename T> bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template <typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } void yn(bool flag) { cout << (flag ? "yes" : "no") << "\n"; } void Yn(bool flag) { cout << (flag ? "Yes" : "No") << "\n"; } void YN(bool flag) { cout << (flag ? "YES" : "NO") << "\n"; } void set_prec(const int &digits) { cout << fixed << setprecision(digits); cerr << fixed << setprecision(digits); } template <typename T> void pr(const T &obj) { cerr << obj; } template <typename T, typename ...Ts> void pr(const T &first, const Ts &...rest) { pr(first); pr(", "); pr(rest...); } template <typename S, typename T> void pr(const pair<S, T> &pair) { pr("("); pr(pair.first); pr(", "); pr(pair.second); pr(")"); } template <typename T> void pr(const vector<T> &vec) { pr("{"); for (T obj : vec) { pr(obj); pr(", "); } pr("}"); } template <typename T> void pr(const vector<vector<T>> &vv) { pr("\n"); rep(index, vv.size()) { pr("["); pr(index); pr("]: "); pr(vv[index]); pr("\n"); } } template <typename T> void pr(const set<T> &vec) { pr("{"); for (T obj : vec) { pr(obj); pr(", "); } pr("}"); } template <typename S, typename T> void pr(const map<S, T> &map) { pr("{"); for (pair<S, T> pair : map) { pr("("); pr(pair.first); pr(": "); pr(pair.second); pr("), "); } pr("}"); } #define db(obj) cerr << #obj << ": "; pr(obj); cerr << " " #define dl(obj) db(obj); cerr << "\n"; #define dm(...) cerr << "(" << #__VA_ARGS__ << "): ("; pr(__VA_ARGS__); cerr << ") " #define dml(...) dm(__VA_ARGS__); cerr << "\n" // #include <atcoder/all> // using namespace atcoder; // const ll MOD = 1000000007; // const ll MOD = 998244353; long long MOD; template <typename T> struct Matrix { int row, column; vector<vector<T>> val; // val[i][j]: i行j列成分 Matrix(const int &_row, const int &_column) { assert(_row > 0 && _column > 0); row = _row; column = _column; val = vector<vector<T>>(row, vector<T>(column)); } Matrix(const vector<vector<T>> &_val) { row = _val.size(); column = _val[0].size(); assert(row > 0 && column > 0); val = _val; } Matrix operator-() const { Matrix res{row, column}; for (int i = 0; i < res.row; ++i) { for (int j = 0; j < res.column; ++j) { res.val[i][j] = -val[i][j]; } } return res; } }; template <typename T> Matrix<T> zero(const int &m, const int &n) { Matrix<T> res{m, n}; return res; } template <typename T> Matrix<T> zero(const int &n) { Matrix<T> res{n, n}; return res; } template <typename T> Matrix<T> unit(const int &n) { Matrix<T> res{n, n}; for (int k = 0; k < n; ++k) { res.val[k][k] = 1; } return res; } // スカラー倍 template <typename T> Matrix<T> operator*(const T &k, const Matrix<T> &A) { Matrix<T> res{A.row, A.column}; for (int i = 0; i < res.row; ++i) { for (int j = 0; j < res.column; ++j) { res.val[i][j] = k * A.val[i][j]; } } return res; } template <typename T> Matrix<T> operator*(const Matrix<T> &A, const T &k) { return k * A; } template <typename T> void operator*=(Matrix<T> &A, const T &k) { A = A * k; } // 加法 template <typename T> Matrix<T> operator+(const Matrix<T> &left, const Matrix<T> &right) { assert(left.row == right.row && left.column == right.column); Matrix<T> res{left.row, left.column}; for (int i = 0; i < res.row; ++i) { for (int j = 0; j < res.column; ++j) { res.val[i][j] = left.val[i][j] + right.val[i][j]; } } return res; } template <typename T> Matrix<T> operator+=(Matrix<T> &left, const Matrix<T> &right) { left = left + right; } template <typename T> Matrix<T> operator-(const Matrix<T> &left, const Matrix<T> &right) { return left + (-right); } template <typename T> Matrix<T> operator-=(Matrix<T> &left, const Matrix<T> &right) { left = left - right; } // 行列積 template <typename T> Matrix<T> operator*(const Matrix<T> &left, const Matrix<T> &right) { assert (left.column == right.row); Matrix<T> res{left.row, right.column}; for (int i = 0; i < res.row; ++i) { for (int j = 0; j < res.column; ++j) { for (int k = 0; k < left.column; ++k) { res.val[i][j] += left.val[i][k] * right.val[k][j]; } } } return res; } template <typename T> Matrix<T> operator*=(Matrix<T> &left, const Matrix<T> &right) { left = left * right; } // 行列累乗 template <typename T> Matrix<T> pow(Matrix<T> A, long long m) { assert(A.row == A.column); assert(m >= 0); Matrix<T> res = unit<T>(A.row), X = A; while (m > 0) { if (m & 1) { res = res * X; } X = X * X; m >>= 1; } return res; } // 特別な積 template <typename T> Matrix<T> my_prod(Matrix<T> left, Matrix<T> right) { assert(left.column == right.row); Matrix<T> res{left.row, right.column}; for (int i = 0; i < res.row; ++i) { for (int j = 0; j < res.column; ++j) { for (int k = 0; k < left.column; ++k) { // 演算例 long long temp = left.val[i][k] * right.val[k][j] % MOD; res.val[i][j] = (res.val[i][j] + temp) % MOD; } } } return res; } // 特別な積についての行列累乗 ※単位行列はE? template <typename T> Matrix<T> my_pow(Matrix<T> A, long long m) { assert(A.row == A.column); assert(m >= 0); Matrix<T> res = unit<T>(A.row), X = A; while (m > 0) { if (m & 1) { res = my_prod(res, X); } X = my_prod(X, X); m >>= 1; } return res; } // debug // void show(Matrix<mint> A) { // for (int i = 0; i < A.row; ++i) { // cout << "("; // for (int j = 0; j < A.column; ++j) { // cout << A.val[i][j].val() << ", "; // if (j < A.val[0].size() - 1) { // cout << ", "; // } // } // cout << ")\n"; // } // cout << "\n"; // } template <typename T> void show(Matrix<T> A) { for (int i = 0; i < A.row; ++i) { cout << "("; for (int j = 0; j < A.column; ++j) { cout << A.val[i][j]; if (j < A.val[0].size() - 1) { cout << ", "; } } cout << ")\n"; } cout << "\n"; } int main() { int N; ll M; cin >> N >> M; MOD = M; Matrix<ll> A{{{1, 1}, {1, 0}}}; Matrix<ll> B = my_pow(A, N - 1); Matrix<ll> F = {{{1}, {0}}}; Matrix<ll> X = my_prod(B, F); cout << X.val[1][0] << endl; }