Fast Fibonacci series, Matrix Method

Source: Internet
Author: User
Fibonacci
Time limit:1000 ms   Memory limit:65536 K
Total submissions:9156   Accepted:6494

Description

In the Fibonacci integer sequence,F0 = 0,F1 = 1, andFN=FN? 1 +FN? 2N≥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 integerN, Your goal is to compute the last 4 digitsFN.

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 digitsFN. If the last four digitsFNAre all zeros, print '0'; otherwise, omit any leading zeros (I. e., printFNMoD 10000 ).

Sample Input

099999999991000000000-1

Sample output

0346266875

Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given

.

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

.


This question is to use matrix knowledge to solve the Fibonacci series. The formula given in the question is very simple. Just set it up. Let's look at another method besides the method given by the question, of course, it is still a matrix.

First look at the following formula:


It is not hard to see that this formula refers:


It is written as a matrix.

Obtain the following sub-iterations:


Therefore, the general formula is as follows:


Continue to expand:


Settings:


Then:


Therefore:


Now it is obvious that the knowledge of the Rapid power of matrices is also needed. The Code is as follows:

#include <stdio.h>#include <string.h>#include <math.h>typedef __int64 int64;int64 c[2][2],ans[2][2],d[2][2],a[2][2];int main(){int64 i,j,n,k,t;while(scanf("%I64d",&n)!=EOF){if(n==-1)break;if(n==0){printf("0\n");continue;}if(n==1){printf("1\n");continue;}memset(ans,0,sizeof(ans));for(i=0;i<2;i++)ans[i][i]=1;a[0][0]=a[0][1]=a[1][0]=1;a[1][1]=0;n=n-2;while(n!=0){if(n%2==1){memset(d,0,sizeof(d));for(i=0;i<2;i++)for(j=0;j<2;j++)if(a[i][j])for(k=0;k<2;k++){d[i][k]+=ans[i][j]*a[j][k];d[i][k]=d[i][k]%10000;}for(i=0;i<2;i++)for(j=0;j<2;j++)ans[i][j]=d[i][j];}memset(c,0,sizeof(c));for(i=0;i<2;i++)for(j=0;j<2;j++){if(a[i][j]==0)continue;for(k=0;k<2;k++){c[i][k]+=a[i][j]*a[j][k]; c[i][k]=c[i][k]%10000;}}for(i=0;i<2;i++)for(j=0;j<2;j++)a[i][j]=c[i][j];n=n/2;}t=(ans[0][0]+ans[0][1])%10000;if(t==0)printf("0\n");else printf("%I64d\n",t);}return 0;}


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.