結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー moyaffmoyaff
提出日時 2020-12-31 23:56:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,660 bytes
コンパイル時間 1,982 ms
コンパイル使用メモリ 171,132 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 07:42:31
合計ジャッジ時間 2,410 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();};
ld PI=asin(1)*2;
//using namespace atcoder;

// ll m; // 遷移行列のサイズ
// // dpの更新
// vector<ll> matmul(vector<ll> &dp, vector<vector<ll>> &mt){
//     vector<ll> ret(m,0);
//     rep(i,m)rep(j,m) ret[i] += mt[i][j]*dp[j];
//     return ret;
// }
// // 遷移行列の更新
// vector<vector<ll>> update(vector<vector<ll>> &mt){
//     vector<vector<ll>> ret(m, vector<ll>(m,0));
//     rep(i,m)rep(j,m)rep(k,m) ret[i][j] += mt[i][k] + mt[k][j];
//     return ret;
// }

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

void matpow(vector<ll> &dp, vector<vector<ll>> &mt, ll k){
    ll m = dp.size();
    while(k){
        if(k&1LL) dp = matmul(dp,mt);
        mt = update(mt);
        k /= 2;
    }
}
int main(){
    ll N;
    m = 2;
    cin >> N >> M;
    vector<ll> dp(2);
    vector<vector<ll>> mt(2, vector<ll>(2));
    dp[0] = 0; dp[1] = 1;
    mt[0][0] = mt[0][1] = mt[1][0] = 1;
    mt[1][1] = 0;
    matpow(dp,mt,N);
    cout << dp[1] << endl;
}
/*
int main(){ 
    ll N,M;
    cin >> N >> M;
    vector<ll> F(100);
    F[0] = 0;
    F[1] = 1;
    rep(i,N){
        F[(i+2)%100] = F[(i+1)%100] + F[i%100];
        F[(i+2)%100] %= M;
    }
    cout << F[(N-1)%100] << endl;
}*/
0