結果
問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める |
ユーザー | morimario |
提出日時 | 2022-10-28 16:45:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 5,835 bytes |
コンパイル時間 | 4,574 ms |
コンパイル使用メモリ | 269,380 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 20:36:11 |
合計ジャッジ時間 | 5,220 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 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 | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 1 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
ソースコード
// 2022.10.21 // #define _GLIBCXX_DEBUG // // #define __USE_MATH_DEFINES // const double PI = acos(-1.0) #include <bits/stdc++.h> using namespace std; // (*) snipet // type name using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vpii = vector<pii>; using vpil = vector<pil>; using vpli = vector<pli>; using vpll = vector<pll>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; template <typename T> using pq = priority_queue<T, vector<T>>; template <typename T> using pql = priority_queue<T, vector<T>, greater<T>>; // rep #define rep0(goal) for (int cnt = 0; cnt < int(goal); ++cnt) #define rep(cnt, goal) for (int cnt = 0; cnt < int(goal); ++cnt) #define rep2(cnt, start, goal) for (int cnt = int(start); cnt < int(goal); ++cnt) #define rep3(cnt, start, goal) for (int cnt = int(start); cnt > int(goal); --cnt) // all #define all(ctn) begin(ctn), end(ctn) // chmax, chmin template <typename T> bool chmax(T &x, const T y) { if (x < y) { x = y; return true; } else { return false; }} template <typename T> bool chmin(T &x, const T y) { if (x > y) { x = y; return true; } else { return false; }} // etc. void Yn(const bool &exp) { if (exp) cout << "Yes" << endl; else cout << "No" << endl;} void set_prec(const int &dig) { cout << fixed << setprecision(dig); cerr << fixed << setprecision(dig);} // template <typename T> T INF() { return numeric_limits<T>::max();} // template <typename T> T MIN_INF() { return numeric_limits<T>::min();} #include <atcoder/all> using namespace atcoder; // using mint = atcoder::modint1000000007; using mint = atcoder::modint998244353; // using mint = atcoder::modint; void pr(const mint &MINT) { cerr << MINT.val(); } // mint using pmm = pair<mint, mint>; using pim = pair<int, mint>; using pmi = pair<mint, int>; using plm = pair<ll, mint>; using pml = pair<mint, ll>; using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; // debug template <typename T> void pr(const T &obj) { cerr << obj; } // single template <typename T, typename ...Ts> void pr(const T &first, const Ts &...rest) { pr(first); cerr << ", "; pr(rest...); } // multi(plural) template <typename S, typename T> void pr(const pair<S, T> &PAIR) { cerr << "("; pr(PAIR.first); cerr << ", "; pr(PAIR.second); cerr << ")"; } // pair template <typename S, typename T, typename U> void pr(const tuple<S, T, U> &trp) { cerr << "("; pr(get<0>(trp)); cerr << ", "; pr(get<1>(trp)); cerr << ", "; pr(get<2>(trp)); cerr << ")"; } // tuple(3) template <typename T> void pr(const vector<T> &vec) { cerr << "{"; for (T obj : vec) { pr(obj); cerr << ", "; } cerr << "}"; } // vector template <typename T> void pr(const vector<vector<T>> &vv) { rep(row, vv.size()) { cerr << endl; cerr << "[" << row << "]: "; pr(vv[row]); }} // vector(multi-D) template <typename T> void pr(const set<T> &SET) { cerr << "{"; for (T obj : SET) { pr(obj); cerr << ", "; } cerr << "}"; } // set template <typename T> void pr(const multiset<T> &MS) { cerr << "{"; for (T obj : MS) { pr(obj); cerr << ", "; } cerr << "}"; } // multiset template <typename S, typename T> void pr(const map<S, T> &MAP) { cerr << "{"; for (pair<S, T> p : MAP) { pr(p.first); cerr << ": "; pr(p.second); cerr << ", "; } cerr << "}";} // map template <typename T> void pr(const queue<T> &que) { queue<T> q = que; cerr << "{"; while (!q.empty()) { pr(q.front()); q.pop(); cerr << ", ";} cerr << "}";} // queue template <typename T> void pr(const deque<T> &deq) { deque<T> d = deq; cerr << "{"; while (!d.empty()) { pr(d.front()); d.pop_front(); cerr << ", ";} cerr << "}";} // deque template <typename T> void pr(const stack<T> &stc) { stack<T> s = stc; vector<T> v; while (!s.empty()) { v.push_back(s.top()); s.pop();} reverse(all(v)); cerr << "{"; for (T obj : v) cerr << obj << ", "; cerr << "}";} // stack template <typename T> void pr(const priority_queue<T> &pq) { priority_queue<T> p = pq; cerr << "{"; while (!p.empty()) { pr(p.top()); p.pop(); cerr << ", "; } cerr << "}";} // priority_queue template <typename T> void pr(const priority_queue<T, vector<T>, greater<T>> &pq) { priority_queue<T, vector<T>, greater<T>> p = pq; cerr << "{"; while (!p.empty()) { pr(p.top()); p.pop(); cerr << ", ";} cerr << "}";} // priority_queue(from less) #define db(obj) cerr << #obj << ": "; pr(obj); cerr << " "; #define dl(obj) db(obj); cerr << endl; #define dm(...) cerr << "(" << #__VA_ARGS__ << "): ("; pr(__VA_ARGS__); cerr << ") "; #define dml(...) dm(__VA_ARGS__); cerr << endl; // // global int N; ll M; ll m; vl p; vvl A; void input() { cin >> N >> M; } vvl mul(vvl A, vvl B) { int n = A.size(); assert(A[0].size() == n && B.size() == n && B[0].size()); vvl res(n, vl(n)); rep(i, n) rep(j, n) { rep(k, n) res[i][j] = (res[i][j] + A[i][k] * B[k][j] % m) % m; } return res; } vvl pow(vvl A, ll N) { int n = A.size(); assert(A[0].size() == n); vvl res(n, vl(n)), X = A; rep(i, n) res[i][i] = 1; while (N) { if (N & 1) { res = mul(res, X); } N >>= 1; X = mul(X, X); } return res; } vl mul(vvl A, vl v) { // vは列ベクトル int n = A.size(); assert(A[0].size() == n && v.size() == n); vl res(n); rep(i, n) { rep(k, n) res[i] = (res[i] + A[i][k] * v[k] % m) % m; } return res; } vvl cm(vl p) { int n = p.size(); vvl res(n, vl(n)); rep(i, n - 1) res[i][i + 1] = 1; rep(i, n) res[n - 1][i] = p[i] % m; return res; } void init() { m = M; p = {1, 1}; A = cm(p); // dl(A); } void solve() { cout << mul(pow(A, N - 1), {0, 1})[0] << endl; }; int main() { input(); init(); solve(); }