結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  miyajiro | 
| 提出日時 | 2021-05-28 21:50:05 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 2,964 bytes | 
| コンパイル時間 | 2,270 ms | 
| コンパイル使用メモリ | 203,664 KB | 
| 最終ジャッジ日時 | 2025-01-21 19:59:32 | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
#define TO_BE_SUBMITTED
#include <bits/stdc++.h>
// #include <atcoder/fenwicktree>
// #include <atcoder/segtree>
// #include <atcoder/lazysegtree>
// #include <atcoder/string>
// #include <atcoder/math>
// #include <atcoder/convolution>
#include <atcoder/modint>
// #include <atcoder/dsu>
// #include <atcoder/maxflow>
// #include <atcoder/mincostflow>
// #include <atcoder/scc>
// #include <atcoder/twosat>
namespace atcoder{};
using namespace atcoder;
using namespace std;
#define fr first
#define sc second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vp = vector<P>;
using vlp = vector<LP>;
inline int getInt()
{
    int x;
    scanf("%d", &x);
    return x;
}
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
using mint = modint;
using vec = vector<mint>;
using mat = vector<vec>;
ll N, M;
void showMat(mat a){
    rep(h, 3){
        cout << a[h][0].val() << " " << a[h][1].val() << " " << a[h][2].val() << "\n";
    }
    cout << "\n";
}
mat mul(mat a, mat b){
    mat res(3, vec(3, 0));
    rep(h, 3){
        rep(w, 3){
            rep(k, 3){
                res[h][w] += a[h][k] * b[k][w];
            }
        }
    }
    return res;
}
mat nijo(mat a){
    return mul(a, a);
}
void solve()
{
    cin >> N >> M;
    N--;
    modint::set_mod(M);
    ll n = N - 2;
    mat A(3, vec(3, 0));
    A[0][0] = 1;
    A[0][1] = 1;
    A[1][0] = 1;
    A[2][1] = 1;
    // showMat(nijo(nijo(A)));
    mat B(3, vec(3, 0));
    B[0][0] = 1;
    B[1][1] = 1;
    B[2][2] = 1;
    while(n > 0LL){
        if((n & 1LL) == 1LL){
            B = mul(B, A);
        }
        A = nijo(A);
        n /= 2LL;
    }
    cout << (B[0][0] + B[0][1]).val() << "\n";
}
int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}
            
            
            
        