问 题 简 述:
求Fibonacci数列第n项 F[n] % 10000。其中: n ≤ 10086110 询问次数 Q ≤ 10
Hint:
Fibonacci数列定义:
F[0] = 0
F[1] = 1
F[n] = F[n − 1] + F[n − 2] ( n ≥ 2)
#include <cstdio>
int A[3], B[3], temp[3], n, q;
void mul (int* X, int* Y )
{
temp[0] = X[0] * Y[0] + X[1] * Y[1] % 10000;
temp[1] = X[0] * Y[1] + X[1] * Y[2] % 10000;
temp[2] = X[1] * Y[1] + X[2] * Y[2] % 10000;
for ( int i = 0; i < 3; ++i) X[i] = temp[i];
}
void pow()
{
for( ;n; n >>= 1, mul (A, A))
if(n&1==1) mul (B,A);
}
int main()
{
scanf("%d", &q);
while(q-- )
{
scanf ("%d", &n) ;
A[0]=A[1]=B[0]=B[2]=1, A[2]=B[1]=0;
pow();
printf ("%d\n", B[1]) ;
}
return 0;
}
/*
输入示例
2
0
9
输出示例
0
34
*/