poj 3070

来源:互联网 发布:好看的古装网络电视剧 编辑:程序博客网 时间:2024/05/16 02:49

Fibonacci
Time Limit: 1000msMemory Limit: 65536KB This problem will be judged on PKU. Original ID: 3070
64-bit integer IO format: %lld Java class name: Main
Prev Submit Status Statistics Next
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:

.

Source
Stanford Local 2006
Tags Toggle

好久没更新博客,疲于期末,最不甘心的是微积分,虽然复习的不怎么样但是我跟答案对过感觉怼个80还是不成问题的,但是结果只有60,险些挂科,但是如果之后不考研,就再也不会和这门学科有交集,最后的成绩对我来时是个人生遗憾。

该题是和快速幂一样考察离散化处理。

以下转载//方便记忆
矩阵的快速幂是用来高效地计算矩阵的高次方的。将朴素的o(n)的时间复杂度,降到log(n)。

这里先对原理(主要运用了矩阵乘法的结合律)做下简单形象的介绍:

一般一个矩阵的n次方,我们会通过连乘n-1次来得到它的n次幂。

但做下简单的改进就能减少连乘的次数,方法如下:

把n个矩阵进行两两分组,比如:A*A*A*A*A*A => (A*A)(A*A)(A*A)

这样变的好处是,你只需要计算一次A*A,然后将结果(A*A)连乘自己两次就能得到A^6,即(A*A)^3=A^6。算一下发现这次一共乘了3次,少于原来的5次。

其实大家还可以取A^3作为一个基本单位。原理都一样:利用矩阵乘法的结合律,来减少重复计算的次数。

以上都是取一个具体的数来作为最小单位的长度,这样做虽然能够改进效率,但缺陷也是很明显的,取个极限的例子(可能有点不恰当,但基本能说明问题),当n无穷大的时候,你现在所取的长度其实和1没什么区别。所以就需要我们找到一种与n增长速度”相适应“的”单位长度“,那这个长度到底怎么去取呢???这点是我们要思考的问题。

有了以上的知识,我们现在再来看看,到底怎么迅速地求得矩阵的N次幂。

既然要减少重复计算,那么就要充分利用现有的计算结果咯!~怎么充分利用计算结果呢???这里考虑二分的思想。。

大家首先要认识到这一点:任何一个整数N,都能用二进制来表示。。这点大家都应该知道,但其中的内涵真的很深很深(这点笔者感触很深,在文章的最后,我将谈谈我对的感想)!!

计算机处理的是离散的信息,都是以0,1来作为信号的处理的。可想而知二进制在计算机上起着举足轻重的地位。它能将模拟信号转化成数字信号,将原来连续的实际模型,用一个离散的算法模型来解决。 好了,扯得有点多了,不过相信这写对下面的讲解还是有用的。

回头看看矩阵的快速幂问题,我们是不是也能把它离散化呢?比如A^19 => (A^16)(A^2)(A^1),显然采取这样的方式计算时因子数将是log(n)级别的(原来的因子数是n),不仅这样,因子间也是存在某种联系的,比如A^4能通过(A^2)(A^2)得到,A^8又能通过(A^4)(A^4)得到,这点也充分利用了现有的结果作为有利条件。下面举个例子进行说明:

现在要求A^156,而156(10)=10011100(2)

也就有A^156=>(A^4)(A^8)(A^16)*(A^128) 考虑到因子间的联系,我们从二进制10011100中的最右端开始计算到最左端。细节就说到这,下面给核心代码:

个人见解,及分析

#include<stdio.h>typedef long long LL;const int mod=10000;struct matrix{    int map[2][2];}ans,temp;matrix s_matrix(matrix a,matrix b)//矩阵乘法没什么好说的{    matrix tt;    for(LL i=0;i<2;i++)        for(LL j=0;j<2;j++)        {            tt.map[i][j]=0;            for(LL k=0;k<2;k++)                tt.map[i][j]=(tt.map[i][j]+a.map[i][k]*b.map[k][j])%mod;        }    return tt;}LL fast_matrix(LL n){    ans.map[0][0]=ans.map[1][1]=1;    ans.map[0][1]=ans.map[1][0]==0;    temp.map[0][0]=temp.map[0][1]=temp.map[1][0]=1;    temp.map[1][1]=0;    while(n)    {        if(n&1)//将n化为2进制,只有最右边的一位才会进入这个循环  就是这一位转化1*2的n次方            ans=s_matrix(ans,temp);        temp=s_matrix(temp,temp);        n=n>>1;//二进制向左移动以为相当于除二    }    return ans.map[0][1];}int main() {    LL n;    while(~scanf("%lld",&n)&&n!=-1)    {        if(n==0)        puts("0");        else        {            printf("%lld\n",fast_matrix(n));        }    }    return 0;}