poj3070
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12215 | Accepted: 8670 |
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
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0 9 999999999 1000000000 -1
Sample Output
0 34 626 6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
n个初设小矩阵相乘, 则新得到的矩阵含有Fn ,Fn+1
可 n 个小矩阵 太多了 ----->
通过这个方法 升级,可以提升计算效率 O(log n 以二
为底)
这个题是一很好的例题,快速求第10000000位斐波那契数列值,这里的n为10^9 ,那么对于O(n)时间也不行了。
同过给出矩阵的巧妙乘法,可以把算法 提升到2^n
我的方法时打表(捂脸)
先打出 打够30个就可以了 F[30] >1000000000 ,F[i]对应的是 矩阵的 i 次相乘 ,i次相乘代表了 2^i 个
1 1
1 0 这样的矩阵相乘,但不是一个一个乘,可以想象成2^i个这样的矩阵,通过分治或者说是 区间的二分,整体整体相乘 log 10000000 以二为底 最多就30次相乘
打表:
for(i = 1 ; i<=30 ;i++)
{
a[1]= (fabc[i-1][1]*fabc[i-1][1] + fabc[i-1][2]*fabc[i-1][3])%10000;
a[2]= (fabc[i-1][1]*fabc[i-1][2] + fabc[i-1][2]*fabc[i-1][4])%10000;
a[3]= (fabc[i-1][3]*fabc[i-1][1] + fabc[i-1][4]*fabc[i-1][3])%10000;
a[4]= (fabc[i-1][3]*fabc[i-1][2] + fabc[i-1][4]*fabc[i-1][4])%10000;
fabc[i][1] = a[1]; //一定注意上面的运算 要有个变量代替保存运算结果 ,fabc 不能直接参与结果的赋值,否者上面的结果一定乱了,想想看,WA的是不是这里
fabc[i][2] = a[2];
fabc[i][3] = a[3];
fabc[i][4] = a[4];
}
这样就可以利用打好的表 进行任何数的运算了,注意 数的奇偶。
#include<stdio.h>
int fabc[30][5];
int s[5];
int a[5];
int solve( int l , int h )
{
int i,k=0,c;
while(1)
{
for( i = 0;1<<i <= h;i++);
i--;
a[1] = (s[1]*fabc[i][1] + s[2]*fabc[i][3])%10000;
a[2] = (s[1]*fabc[i][2] + s[2]*fabc[i][4])%10000;
a[3] = (s[3]*fabc[i][1] + s[4]*fabc[i][3])%10000;
a[4]= (s[3]*fabc[i][2] + s[4]*fabc[i][4])%10000;
s[1] =a[1];s[2] = a[2];
s[3] =a[3];s[4] = a[4];
c = 1<<i;
h = h- c; //举个例子: h开始传过来的是10 ,表上的都是2的i次相乘,则循环第一次 去fabc[3][] 代表已经有(2^3)8个初始小矩阵相乘,还有两个没乘,h = 2进入下次循环,取表fabc[1][] (2^1), h = 0 退出 为偶数,最后输出偶数的输出形式即可
if(h == 1)return 1;
else if(h == 0) return 0;
}
}
int main()
{
int n,i;
fabc[0][4] = 0;
fabc[0][1] = 1;
fabc[0][2] = 1;
fabc[0][3] = 1;
for(i = 1 ; i<=30 ;i++) //打表
{
a[1]= (fabc[i-1][1]*fabc[i-1][1] + fabc[i-1][2]*fabc[i-1][3])%10000;
a[2]= (fabc[i-1][1]*fabc[i-1][2] + fabc[i-1][2]*fabc[i-1][4])%10000;
a[3]= (fabc[i-1][3]*fabc[i-1][1] + fabc[i-1][4]*fabc[i-1][3])%10000;
a[4]= (fabc[i-1][3]*fabc[i-1][2] + fabc[i-1][4]*fabc[i-1][4])%10000;
fabc[i][1] = a[1];
fabc[i][2] = a[2];
fabc[i][3] = a[3];
fabc[i][4] = a[4];
}
while(scanf("%d",&n))
{
if(n ==-1)break;
s[1]= s[4] = 1;
s[2]= s[3] = 0;
if(solve(0,n))printf("%d\n",s[1]); //基数
else printf("%d\n",s[2]); //偶数
}
return 0;
}
PS: 1 . 1<< i 是 2的i次方
2 . 虽然它的本质就是 就像二叉树一样管理着区间,可以用递归 分支计算,就像二叉树那样。但是在这个题这种数据完全一致的情况下,会进行不必要的重复,不如直接打表快!(两种都试了一试,从测试的结果看)