呃.... 一直在玩,感觉暑假快废过去了。。。
一篇很好的文章、http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html
NOJ 1481 Big Fibonacci 链接:http://ac.nbutoj.com/Problem/view.xhtml?id=1481
这是一个应用吧,原题中有提示要用矩阵快速幂的,这里有点坑、、、 算是mark 一下、
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct matrix
{
__int64 m[2][2];
}ans, base;
matrix mul(matrix a, matrix b)
{
matrix tmp;
for(int i = 0; i < 2; i ++)
{
for(int j = 0; j < 2; j ++)
{
tmp.m[i][j] = 0;
for(int k = 0; k < 2; k ++)
tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % 20130719 ;
}
}
return tmp;
}
__int64 fibo(__int64 n)
{
base.m[0][0] = base.m[0][1] = base.m[1][0] = 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(n)
{
if(n & 1)
{
ans = mul(ans, base);
}
base = mul(base, base);
n >>= 1;
}
return ans.m[0][1];
}
int main()
{
__int64 n;
while(~scanf("%I64d",&n))
{
printf("%I64d\n", fibo(n));
}
}