結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー milkcoffeemilkcoffee
提出日時 2021-04-18 00:58:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,937 bytes
コンパイル時間 1,764 ms
コンパイル使用メモリ 173,344 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 07:29:41
合計ジャッジ時間 2,525 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>

#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
//constexpr ll mod = 998244353;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
void pt(T val) {
    cout << val << "\n";
}
template <typename T>
void pt_vll(vector<T> &v) {
    ll vs = v.size();
    rep(i, vs) {
        cout << v[i];

        if (i == vs - 1)
            cout << "\n";
        else
            cout << " ";
    }
}
ll mypow(ll a, ll n) {
    ll ret = 1;
    if(n==0) return 1;
    if(a==0) return 0;
    rep(i, n) {
        if (ret > (ll)(1e18 + 10) / a) return -1;
        ret *= a;
    }
    return ret;
}
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
vector<vector<ll>> mat_mul(vector<vector<ll>> a, vector<vector<ll>> b, ll m)
{
    vector<vector<ll>> c(a.size(), vector<ll>(b[0].size(), 0));
    for (int i = 0; i < a.size(); i++){
        for (int k = 0; k < b[0].size(); k++){
            for (int j = 0; j < b.size(); j++){
                c[i][k] = (c[i][k] + (a[i][j] * b[j][k]) % m) % m;
            }
        }
    }
    return c;
}

vector<vector<ll>> mat_pow(vector<vector<ll>> x, ll n, ll m)
{
    vector<vector<ll>> y(x.size(), vector<ll>(x.size(), 0));
    for (int i = 0; i < x.size(); i++){
        y[i][i] = 1;
        while (n){
            if (n & 1){
                    y = mat_mul(x, y, m);
            }
            x = mat_mul(x, x, m);
            n >>= 1;
        }
    }
    return y;
}
int main() {
    ll n,m,ans=0;
    cin>>n>>m;
    vvll p(2,vll(1));
    vvll a(2,vll(2));
  	p[0][0]=1;
    a[0][0]=1;
    a[0][1]=1;
    a[1][0]=1;
    a=mat_pow(a,n-2,m);
    p=mat_mul(a,p,m);
    ans=p[0][0];
    cout<<ans<<endl;
}
0