結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-06-18 13:02:58 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,783 bytes | 
| コンパイル時間 | 4,043 ms | 
| コンパイル使用メモリ | 234,328 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-10-10 01:27:33 | 
| 合計ジャッジ時間 | 4,713 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
#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;
}
            
            
            
        