結果
問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
ユーザー |
|
提出日時 | 2025-01-28 12:09:55 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 909 bytes |
コンパイル時間 | 3,843 ms |
コンパイル使用メモリ | 282,548 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2025-01-28 12:10:00 |
合計ジャッジ時間 | 4,514 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll=long long; template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;} template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<endl;for(auto i:v)os<<i<<" ";return os;} const ll inf=(1LL<<60); #define all(a) (a).begin(),(a).end() ll n,m; vector<vector<ll>> prod(vector<vector<ll>> a,vector<vector<ll>> b){ assert(a[0].size()==b.size()); vector<vector<ll>> r(a.size(),vector<ll>(b[0].size())); for(ll i=0;i<a.size();i++){ for(ll j=0;j<b[0].size();j++){ for(ll k=0;k<a[0].size();k++)r[i][j]+=a[i][k]*b[k][j]; r[i][j]%=m; } } return r; } int main(){ cin.tie(0)->sync_with_stdio(0); cin>>n>>m; n--; vector<vector<ll>> a={{0,1},{1,1}},ans={{0},{1}}; while(n>0){ if(n%2)ans=prod(a,ans); a=prod(a,a); n/=2; } cout<<ans[0][0]<<endl; char crlf;cin>>crlf;assert(cin.eof()); }