POJ 3070 Fibonacci (Matrix Quick Power template)

Source: Internet
Author: User

Description:

In the Fibonacci integer sequence, f0 = 0, f1 = 1, and fn = fn −1 + fn −2 F or n ≥2. For example, the first ten terms of the Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal was to compute the last 4 digits of Fn.

Input:

The input test file would contain multiple test cases. Each of the test case consists of a 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 is all zeros, print ' 0 '; Otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input:

099999999991000000000-1

Sample Output:

0346266875

Hint:

As a reminder, matrix multiplication is associative, and the product of the 2x2 matrices are given by.

Also, note that raising any 2×2 matrix to the 0th power gives the identity matrix: .

Test instructions: is to require the nth Fibonacci number to 10000 of the remainder, because n is large, simple simulation is certainly not, then led to the Fibonacci of another representation method (the problem has been given), according to the description, we only require the 2*2 matrix {{1,1},{1,0}}^ n can do it.

This leads to a new algorithm: Matrix fast Power (according to the Fast power adaptation, the fast power calculation is the number of the N-square, and this is the matrix of the n-th square).

#include <stdio.h>#include<string.h>#include<queue>#include<math.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intn=1e6+Ten;Const intinf=0x3f3f3f3f;Const intMod=1e4;typedefLong LongLL;structnode{LL m[2][2];} ANS, TMP, Cnt;node Multiply (Node A, Node B)///calculates the matrix after multiplying two matrices{    intI, j, K;  for(i =0; I <2; i++)    {         for(j =0; J <2; J + +) {Cnt.m[i][j]=0;  for(k =0; K <2; k++) Cnt.m[i][j]= (Cnt.m[i][j]+a.m[i][k]*b.m[k][j])%MOD; }    }    returnCNT;} ll Matrix_power (ll N) {ans.m[0][0] = ans.m[1][1] =1; ans.m[0][1] = ans.m[1][0] =0; tmp.m[0][0] = tmp.m[0][1] = tmp.m[1][0] =1; tmp.m[1][1] =0;  while(n)///and the fast power seeking method is consistent    {        if(n%2!=0) ans=Multiply (ans, TMP); TMP=Multiply (TMP, TMP); N/=2; }    returnans.m[0][1];///since the nth Fibonacci number exists in {fn+1,fn,fn,fn-1}, we return the element at the (0,1) position}intMain () {LL n, num;  while(SCANF ("%lld", &n), n! =-1) {num=matrix_power (n); printf ("%lld\n", num); }    return 0;}

POJ 3070 Fibonacci (Matrix Quick Power template)

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.