結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー gucci0512gucci0512
提出日時 2022-06-18 13:02:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,783 bytes
コンパイル時間 4,252 ms
コンパイル使用メモリ 227,760 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 08:15:22
合計ジャッジ時間 4,967 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=b-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x);
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double lld;
using namespace std;
using namespace atcoder;
const ll mod=998244353;
//const ll mod=1e9+7;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const string zton="0123456789";
const string atoz="abcdefghijklmnopqrstuvwxyz";
const string ATOZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll inf=(1ll<<60);
// const int inf=(1ll<<30);
lld dist(lld x1,lld x2,lld y1,lld y2){
    lld res=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    res=sqrt(abs(res));
    return res;
}
ll gcd(ll a,ll b){
    if(a==0||b==0)return a+b;
    ll r;
    r=a%b;
    if(r==0){
        return b;
    }
    else{
        return gcd(b,r);
    }
}
typedef pair<ll,int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M;

mat mul(mat &A,mat &B){
    mat C(A.size(),vec(B[0].size()));
    rep(i,0,A.size()){
        rep(k,0,B.size()){
            rep(j,0,B[0].size()){
                C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
            }
        }
    }
    return C;
}

mat pow_mat(mat A,ll n){
    mat B(A.size(),vec(A.size()));
    rep(i,0,A.size()){
        B[i][i]=1;
    }
    while(n>0){
        if(n&1) B=mul(B,A);
        A=mul(A,A);
        n>>=1;
    }
    return B;
}

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    mat A(2,vec(2));
    A[0][0]=1;
    A[0][1]=1;
    A[1][1]=0;
    A[1][0]=1;
    cin >> N >> M;
    mat B=pow_mat(A,N-2);
    cout << B[0][0] << endl;
}
0