举例1:Fibonacci
题目传送门
题意
那么输入。
因为:
所以,我们可以发现递推式可以转化为矩阵运算:
那么可得:
代码语言:javascript复制#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define ll long long
using namespace std;
inline ll read(){
ll res=0,f=1;char ch=getchar();
while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10 ch-'0',ch=getchar();
return res*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x '0');
else{
write(x/10);
putchar(x '0');
}
}
ll n,m;
struct node{
ll f[3][3];
}a,f,s;
void build(node &x){
for(ll i=0;i<=1;i ){
for(ll j=0;j<=1;j ){
if(i==j) x.f[i][j]=1;
else x.f[i][j]=0;
}
}
}
void Mul(node &x,node &y,node &z){
memset(z.f,0,sizeof(z.f));
for(ll i=0;i<=1;i ){
for(ll j=0;j<=1;j ){
if(x.f[i][j]!=0){
for(ll k=0;k<=1;k ){
z.f[i][k] =x.f[i][j]*y.f[j][k];
z.f[i][k]%=m;
}
}
}
}
}
node Pow(ll b){
node res;
build(res);
node tmp=f,t;
while(b){
if(b%2==1) Mul(res,tmp,t),res=t;
Mul(tmp,tmp,t),tmp=t,b>>=1;
}
return res;
}
int main(){
n=read();m=read();
if(n<=2){
puts("1");
return 0;
}
f.f[0][0]=1;f.f[1][0]=1;f.f[0][1]=1;f.f[1][1]=0;
node q=Pow(n-2);
write((q.f[0][0] q.f[1][0] m)%m);
return 0;
}
//(f[n] )=(1 1)^(n-2)*(f[2])
//(f[n-1])=(1 0) *(f[1])
举例2:Fibonacci求和
题目传送门
题意
f[n]quad modquad m
以此类推,可得:
那么:
也就是:
所以只需要将上题代码改一改就好了:
代码语言:javascript复制#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define ll long long
using namespace std;
inline ll read(){
ll res=0,f=1;char ch=getchar();
while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10 ch-'0',ch=getchar();
return res*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x '0');
else{
write(x/10);
putchar(x '0');
}
}
ll n,m;
struct node{
ll f[3][3];
}a,f,s;
void build(node &x){
for(ll i=0;i<=1;i ){
for(ll j=0;j<=1;j ){
if(i==j) x.f[i][j]=1;
else x.f[i][j]=0;
}
}
}
void Mul(node &x,node &y,node &z){
memset(z.f,0,sizeof(z.f));
for(ll i=0;i<=1;i ){
for(ll j=0;j<=1;j ){
if(x.f[i][j]!=0){
for(ll k=0;k<=1;k ){
z.f[i][k] =x.f[i][j]*y.f[j][k];
z.f[i][k]%=m;
}
}
}
}
}
node Pow(ll b){
node res;
build(res);
node tmp=f,t;
while(b){
if(b%2==1) Mul(res,tmp,t),res=t;
Mul(tmp,tmp,t),tmp=t,b>>=1;
}
return res;
}
int main(){
n=read();m=read();
if(n<=2){
puts("1");
return 0;
}
f.f[0][0]=1;f.f[1][0]=1;f.f[0][1]=1;f.f[1][1]=0;
node q=Pow(n);
write((q.f[0][0] q.f[1][0] m-1)%m);
return 0;
}