結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
uliba3
|
| 提出日時 | 2020-05-04 14:54:31 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 754 bytes |
| コンパイル時間 | 1,702 ms |
| コンパイル使用メモリ | 172,976 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-24 09:23:24 |
| 合計ジャッジ時間 | 2,445 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 7 WA * 5 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int M;
vector<int> matmul(vector<int> &dp,vector<vector<int>> &mt)
{
vector<int> ret(2,0);
for(int i=0;i<2;i++)for(int j=0;j<2;j++)ret[i]+=mt[i][j]*dp[j]%M;
return ret;
}
vector<vector<int>> update(vector<vector<int>> &mt)
{
vector<vector<int>> ret(2,vector<int>(2,0));
for(int i=0;i<2;i++)for(int j=0;j<2;j++)for(int k=0;k<2;k++)ret[i][j]+=mt[i][k]*mt[k][j]%M;
return ret;
}
void matpow(vector<int> &dp,vector<vector<int>> &mt,int &N)
{
while(N>0)
{
if(N&1)dp=matmul(dp,mt);
mt=update(mt);
N>>=1;
}
}
int main()
{
int N;scanf("%d %d",&N,&M);
vector<int> dp(2);dp[0]=0;dp[1]=1;
vector<vector<int>> mt(2,vector<int>(2));
mt[0][0]=mt[1][0]=mt[0][1]=1;mt[1][1]=0;
matpow(dp,mt,N);
printf("%d\n",dp[1]%M);
}
uliba3