POJ 3070 Fibonacci

Source: Internet
Author: User

Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9769 Accepted: 6959

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 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 A/2x2 matrices is given by

.

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

.



The idea of solving problems: simple fast matrix power

Quick Matrix Power Templates:

Matrix Add (Matrix A,matrix b) {matrix ans;for (int i=0;i<2;i++) {for (int j=0;j<2;j++) {ans.mat[i][j]=a.mat[i][j]+ B.mat[i][j];if (ans.mat[i][j]>=m) {ans.mat[i][j]%=m;}}} return ans;} Matrix Mul (Matrix A,matrix b) {matrix ans;for (int i=0;i<2;i++) {for (int j=0;j<2;j++) {ans.mat[i][j]=0;for (int k=0; k<2;k++) {ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j];if (ans.mat[i][j]>=m) {ans.mat[i][j]%=m;}}}} return ans;} Matrix Init () {matrix ans;for (int i=0;i<2;i++) {for (int j=0;j<2;j++) {if (i==j) ans.mat[i][j]=1;elseans.mat[i][j] = 0;}} return ans;} Matrix exp (Matrix A,int k) {matrix ans=init (); while (k) {if (k&1) Ans=mul (ans,a); A=mul (a,a); k>>=1;} return ans;}


Reference code:

#include <iostream>using namespace Std;int m=10000;struct matrix{long long mat[2][2];}; Matrix Add (Matrix A,matrix b) {matrix ans;for (int i=0;i<2;i++) {for (int j=0;j<2;j++) {ans.mat[i][j]=a.mat[i][j]+ B.mat[i][j];if (ans.mat[i][j]>=m) {ans.mat[i][j]%=m;}}} return ans;} Matrix Mul (Matrix A,matrix b) {matrix ans;for (int i=0;i<2;i++) {for (int j=0;j<2;j++) {ans.mat[i][j]=0;for (int k=0; k<2;k++) {ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j];if (ans.mat[i][j]>=m) {ans.mat[i][j]%=m;}}}} return ans;} Matrix Init () {matrix ans;for (int i=0;i<2;i++) {for (int j=0;j<2;j++) {if (i==j) ans.mat[i][j]=1;elseans.mat[i][j] = 0;}} return ans;} Matrix exp (Matrix A,int k) {matrix ans=init (); while (k) {if (k&1) Ans=mul (ans,a); A=mul (a,a); k>>=1;} return ans;} int main () {Matrix a;a.mat[0][0]=a.mat[0][1]=a.mat[1][0]=1;a.mat[1][1]=0;int n;while (cin>>n&&n!=-1) { Matrix Ans=exp (a,n); Cout<<ans.mat[0][1]%m<<endl;} return 0;}





POJ 3070 Fibonacci

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.