您的位置:首页 > 其它

51nod 1242 斐波那契数列的第N项

2015-06-21 16:42 411 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242



#include <cstdio>
typedef long long ll;
const int mod = 1e9+9;
struct Mat
{
ll matrix[2][2];
};

Mat mul(Mat a,Mat b)
{
Mat c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
c.matrix[i][j]=0;
for(int k=0;k<2;k++)
{
c.matrix[i][j]+=(a.matrix[i][k]*b.matrix[k][j])%mod;
}
c.matrix[i][j]%=mod;
}
return c;
}
Mat mat;
Mat solve(ll m)
{
Mat mt=mat;
m--;
while(m)
{
if(m&1)
{
mt=mul(mat,mt);
m--;
}
mat=mul(mat,mat);
m/=2;
}
return mt;
}
int main()
{
//freopen("a.txt","r",stdin);
ll n;
scanf("%lld",&n);
mat.matrix[0][0]=mat.matrix[0][1]=mat.matrix[1][0]=1;
mat.matrix[1][1]=0;
Mat c=solve(n-1);
printf("%lld\n",c.matrix[0][0]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: