AcWing 1303. 斐波那契前 n 项和
题意:
题解:
这个题解分析的太到位了
代码:
#include<iostream>
#include<cstring>#define ll long longusing namespace std;const int N=3;int n,m;
ll f1[N][N]={{1,1,1}};
ll A[N][N]={{0,1,0},{1,1,1},{0,0,1}
};void mul(ll a[N][N],ll b[N][N])
{ll c[N][N]={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)%m;memcpy(a,c,sizeof c);
}void quick_pow(ll a[N][N],int k)
{ll E[N][N]={ //单位阵{1,0,0},{0,1,0},{0,0,1}};while(k){if(k&1) mul(E,a);mul(a,a);k>>=1;}memcpy(a,E,sizeof E);
}int main()
{cin>>n>>m;quick_pow(A,n-1);mul(f1,A);cout<<f1[0][2]<<endl;return 0;
}