您的位置:首页 > 其它

集训第六周 矩阵快速幂 K题

2015-08-22 15:32 232 查看
Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

#include"iostream"
#include"cstdio"
using namespace std;

typedef struct
{
int m[2][2];
}node;

node work(node a,node b)
{
node c;
c.m[0][0]=(a.m[0][0]*b.m[0][0]+a.m[0][1]*b.m[1][0])%10000;
c.m[0][1]=(a.m[0][0]*b.m[0][1]+a.m[0][1]*b.m[1][1])%10000;
c.m[1][0]=(a.m[1][0]*b.m[0][0]+a.m[1][1]*b.m[1][0])%10000;
c.m[1][1]=(a.m[1][0]*b.m[0][1]+a.m[1][1]*b.m[1][1])%10000;
return c;
}

void caculate(int c)
{
node ans,base;
base.m[0][0]=base.m[1][0]=base.m[0][1]=1;
base.m[1][1]=0;
ans.m[0][0]=ans.m[1][1]=1;
ans.m[0][1]=ans.m[1][0]=0;
while(c)
{
if(c&1) ans=work(ans,base);
base=work(base,base);
c>>=1;
}
cout<<ans.m[1][0]<<endl;
}

int main()
{
int n;
while(cin>>n&&n>=0)
{
caculate(n);
}
}


View Code


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: