AcWing 1303. 斐波那契前 n 项和
原题链接
中等
作者:
最后五分钟
,
2024-04-07 21:31:43
,
所有人可见
,
阅读 4
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define de(x) cout<<#x<<" = "<<x<<" "
#define deg(x) cout<<#x<<" = "<<x<<endl
using namespace std;
const int N=10010,M=10010;
typedef pair<int,int> PII;
int n,m;
void mul(int a[][3],int b[][3])
{
int c[3][3]={0};
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
memcpy(a,c,sizeof c);
}
int work(int n)
{
int a[3][3]={0};
a[0][0]=0,a[0][1]=1%m,a[0][2]=0%m;
int f[3][3]={
0,1,0,
1,1,1,
0,0,1
};
while(n)
{
if(n&1)mul(a,f);
mul(f,f);
n>>=1;
}
return a[0][2];
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
cout<<work(n)<<endl;
return 0;
}