举例1:Fibonacci
题意
$$f[1]=1,f[2]=1,f[3]=2,f[4]=3 \dots f[n]=f[n-1]+f[n-2]$$
那么输入$n$、$m$,求第n项Fibonacci的值$mod$ $m$,即$f[n]$ $mod$ $m$。
$$1\leq n \leq 2 \times 10^9$$
因为:$$f[i]=1 \times f[i-1]+1 \times f[i-2]$$$$f[i-1]=1\times f[i-1]+0 \times f[i-2]$$
所以,我们可以发现递推式可以转化为矩阵运算:
$$\left(\begin{array}{rcl}f[i]\\f[i-1]\end{array} \right) = \left(\begin{array}{rcl}1 \quad 1\\1\quad 0\end{array} \right) \times \left(\begin{array}{rcl}f[i-1]\\f[i-2]\end{array} \right)=\left(\begin{array}{rcl}1 \quad 1\\1\quad 0\end{array} \right)^2 \times \left(\begin{array}{rcl}f[i-2]\\f[i-3]\end{array} \right)$$
那么可得:
$$\left(\begin{array}{rcl}f[n]\\f[n-1]\end{array} \right)=\left(\begin{array}{rcl}1 \quad 1\\1\quad 0\end{array} \right)^{n-2} \times \left(\begin{array}{rcl}f[2]\\f[1]\end{array} \right)$$
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #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%10+'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; }
|
举例2:Fibonacci求和
题意
输入$n$、$m$,求出Fibonacci的前$n$项的和 $mod$ $m$的值,即:$f[n]\quad mod\quad m$
$$f[n]=f[n-1]+f[n-2]$$
$$f[n-1]=f[n-2]+f[n-3],f[n]=2\times f[n-2]+f[n-3]$$
$$f[n-2]=f[n-3]+f[n-4],f[n]=f[n-2]+2\times f[n-3]+f[n-4]$$
$$f[n-3]=f[n-4]+f[n-5],f[n]=f[n-2]+f[n-3]+2\times f[n-4]+f[n-5]$$
以此类推,可得:
$$f[n]=f[n-2]+f[n-3]+f[n-4]+f[n-5]+\dots f[2]+2 \times f[1]$$
那么:
$$f[n-2]+f[n-3]+f[n-4]+f[n-5]+\dots f[2]+f[1]=f[n]-f[1]$$
$$f[n-2]+f[n-3]+f[n-4]+f[n-5]+\dots f[2]+f[1]=f[n]-1$$
也就是:
$$f[n]+f[n-1]+f[n-2]+f[n-3]+\dots f[2]+f[1]=f[n+2]-1$$
所以只需要将上题代码改一改就好了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #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%10+'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; }
|