Fibonacci Series Library

Source: Internet
Author: User

Http://acm.tju.edu.cn/toj/showp3267.html 3267. Library Time limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 214 Accepted Runs: 96



Descriptionas We all know, there is very long stairs before our library in campus III of our school. It's so tired for us to go up stairs. ALPC60 have to go up and down stairs every day, what a boring walk!

One day is Alpc60 went up stairs, he thought that every time he can step over one or both stairs, if there is n Stairs, then how many ways he can reach the top?

The clever acmers, can-you-help ALPC60-Calculate how many ways he can go up n (1≤ n ≤1,000,000,000) Stairs.

Because the number of the answer is so large, you must output the number module by 9901.

Inputeach line of the input contains a number Nindicating the stairs number.

Input is ended with-1 which are not the stairs number.

Outputfor each case of the input output, the possible ways module by 9901.Sample input
125-1
Sample Output
128
Hint: The Bruce force method would simply leads to the time Limit exceeded error, try some efficient method to solve this proble M.



Source: Nudt Programming Contest

Test instructions: Up stairs, each time can be on 1 knots or 2 knots, there are several ways to go upstairs, is a typical Fibonacci sequence, a state can be from two situations, 1, the last two to here, 2, 1 knots here, is f[n] = f[n-1]+f[n-2]

But because the numbers will be large, the matrix is used, and the fast matrix powers

1 /*2 Fast Power matrix method3 DP Dynamic Programming4 A[n] = a[n-1]+ a[n-2]; This is a typical Fibonacci sequence, and the Fibonacci sequence is already big when it's 20, so in general, the Fast method5 The construction matrix has6 a[n-1] = q[n-1]; In order to construct a matrix7 Upper and lower two-type analysis has [a[n]] = [1,1]*[a[n-1]]8 [a[n-1]] = [1,0] [a[n-2]]9 where you want to customize the multiplication of matricesTen and then the recursion turns into the problem of finding the power of the matrix. One */ A     -#include <cstdio> -#include <cmath> the #defineN 9901 - using namespacestd; - structmtx{ - //int n;//the order of the Matrix +     inta[2][2]; -nt[operator* (ConstMTX o)Const { + MTX C; A //return C.N = n; atc.a[0][0]= c.a[0][1] = c.a[1][0] = c.a[1][1] =0;//do the pre-multiplication initialization -          for(inti =0; I <2; i++ ) -         { -              for(intj =0; J <2; J + +) -             { -                  for(intK =0; K <2; k++) in                 { -C.A[I][J] + = ((a[i][k]%n) * (o.a[k][j]%n))%N; toC.A[I][J]%=N; +                 } -             } the         } *         returnC; $     } Panax Notoginseng};//Defining matrix multiplication - /*if the power of a recursive write number is the int f (int a, int b) + { A int ret = 1; the if (b = = 1) return A; + int t = f (A,B/2) - t = t*t; $ if (b&1) return t*a; $ return t; - } -  the but the multiplication of matrices generally does not write recursively, because recursion is stored with stacks, which can explode memory - Wuyi power of non-recursive form write the  - int f (int a, int b) Wu { - int ret = 1; About While (b > 0) $     { - if (b&1) ret *= A; - a *= A;  - b >>= 1; A     } + return ret; the }  - int f (int a, int b) $ { the int ret; the For (ret = 1; b; b>>=1) the     { the if (b&1) ret*=a; - A = a * A; in     } the return ret; the } About  the int f (int a, int b) the { the int ret; + for (ret = 1; b; b>>=1, a = A * a%mod) - if (b&1) ret = ret*a%n;////+= than speed up .....  can also be written ret*=a; RET%=a; the return ret;Bayi } the */ the  -MTX F (intb) - { the MTX t; the mtx haha; thehaha.a[0][0] = haha.a[0][1] = haha.a[1][0] =1; thehaha.a[1][1] =0; -      thet.a[0][1] = t.a[1][0] =0; thet.a[1][1] = t.a[0][0] =1;  the MTX ret;94     //if (b==1) return t; the      for(ret = t; b; b>>=1) the     { the //printf ("%d\n", b);98         if(b&1) ret = RET *haha; Abouthaha = haha *haha; -     } 101     returnret;102}//Fast Power matrix103 104  the intMain ()106 {107     intCNT;108      while(~SCANF ("%d", &cnt) &&cnt!=-1)109     { the mtx ans;111ans = f (cnt-1); the     //printf ("%d%d\n%d%d\n", ans.a[0][0], ans.a[0][1], ans.a[1][0], ans.a[1][1]);113         intsum = (1*ans.a[0][0]+1*ans.a[0][1])%N; theprintf"%d\n", sum); the     }  the     return 0 ;117}

Fibonacci Series Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.