对于斐波那契数列 an=an-1+an-2;
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int mod = 10000;
int f=2;
ll n;
struct node{
ll materix[5][5];
};
node mul(node a,node b)
{
node res;
memset(res.materix,0,sizeof(res.materix));
for(int i=1;i<=f;i++)
{
for(int j=1;j<=f;j++)
{
for(int k=1;k<=f;k++)
{
res.materix[i][j]=(res.materix[i][j]+a.materix[i][k]*b.materix[k][j]%mod)%mod;
}
}
}
return res;
}
node ksm(node a,ll b)
{
node ans;
memset(ans.materix,0,sizeof(ans.materix));
for(int i=1;i<=f;i++)
{
ans.materix[i][i]=1;
}
while(b)
{
if(b&1)
ans=mul(ans,a);
b>>=1;
a=mul(a,a);
}
return ans;
}
int main()
{
while(cin>>n&&n!=-1)
{
if(n==0||n==1)
cout<<n<<endl;
else
{
node a,b;
node ans;
a.materix[1][1]=1;a.materix[1][2]=0;
a.materix[2][1]=1;a.materix[2][2]=0;
b.materix[1][1]=1;b.materix[1][2]=1;
b.materix[2][1]=1;b.materix[2][2]=0;
ans=ksm(b,n-2);
ans=mul(ans,a);
ll init = ans.materix[1][1];
printf("%lld\n",init);
}
}
return 0;
}
例题:::::::::::::::
题目链接>https://vjudge.net/contest/327427#problem/N
标题Plant 矩阵快速幂
Dwarfs have planted a very interesting plant, which is a triangle directed “upwards”. This plant has an amusing feature. After one year a triangle plant directed “upwards” divides into four triangle plants: three of them will point “upwards” and one will point “downwards”. After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
Help the dwarfs find out how many triangle plants that point “upwards” will be in n years.
Input
The first line contains a single integer n (0 ≤ n ≤ 1018) — the number of full years when the plant grew.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer — the remainder of dividing the number of plants that will point “upwards” in n years by 1000000007 (109 + 7).
Examples
Input
1
Output
3
Input
2
Output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
an=3an-1+bn-1;
**bn=3bn-1+an-1;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
const ll mod=1e9+7;
const int f=2;
struct node{
ll materix[5][5];
};
node mul(node a,node b)
{
node res;
memset(res.materix,0,sizeof(res.materix));
for(int i=1;i<=f;i++)
{
for(int j=1;j<=f;j++)
{
for(int k=1;k<=f;k++)
{
res.materix[i][j]=(res.materix[i][j]+a.materix[i][k]*b.materix[k][j]%mod)%mod;
}
}
}
return res;
}
node ksm(node a,ll b)
{
node ans;
memset(ans.materix,0,sizeof(ans.materix));
for(int i=1;i<=f;i++)
ans.materix[i][i]=1;
while(b)
{
if(b&1)
ans = mul(ans,a);
b>>=1;
a=mul(a,a);
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n;
node a,b,ans;
memset(a.materix,0,sizeof(a.materix));
memset(b.materix,0,sizeof(b.materix));
a.materix[1][1]=3,a.materix[1][2]=1;
a.materix[2][1]=1,a.materix[2][2]=3;
b.materix[1][1]=3;
b.materix[2][1]=1;
if(n==0)
cout<<"1"<<endl;
else if(n==1)
cout<<"3"<<endl;
else
{
ans=mul(ksm(a,n-1),b);
cout<<ans.materix[1][1]<<endl;
}
return 0;
}