結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 眼精疲労がやばい眼精疲労がやばい
提出日時 2020-07-04 08:19:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,674 bytes
コンパイル時間 1,013 ms
コンパイル使用メモリ 101,760 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 16:21:13
合計ジャッジ時間 2,592 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <bitset>
#include <string>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<tuple>
#include<sstream>
#include<functional>
#include<list>
#include<queue>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
using graph = vector<vector<ll>>;

const int inf = 1e9+7;
const long long MOD = 1000000007;
ll M;

int m;//遷移行列のサイズ
//dpの更新
vec matmul(vec& dp, Graph& G) {
    vec ret(m, 0);
    rep(i, m) {
        rep(j, m) {
            ret[i] += (G[i][j] * dp[j])%M;
        }
        ret[i] %= M;
    }
    return ret;
}
//遷移行列の更新
Graph update(Graph& G) {
    Graph ret(m, vec(m, 0));
    rep(i, m) {
        rep(j, m) {
            rep(k, m)ret[i][j] += (G[i][k] * G[k][j])%M;
            ret[i][j] %= M;
        }
    }
    return ret;
}
void matpow(vec& dp, Graph& G, int k) {
    m = dp.size();
    while (k) {
        if (k & 1)dp = matmul(dp, G);
        G = update(G);
        k /= 2;
    }
}

int main() {
    ll n;
    cin >> n >> M;
    vec dp{ 1,0 };
    Graph G(2, vec(2));
    G[0][0] = 1; G[0][1] = 1; G[1][0] = 1; G[1][1] = 0;
    matpow(dp, G, n-1);
    cout << dp[1] << endl;
    return 0;
}
0