POJ 3070 Fibonacci "matrix fast power to find the nth Fibonacci number%1000"

Source: Internet
Author: User

Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11123 Accepted: 7913

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:

.


Test instructions: give you a formula to solve the nth Fibonacci number. Let you find the FN% 10000.




Constructs the unit matrix, then is the Matrix fast power.


AC Code:

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 100#define LL Long Long#define    MOD 10000using namespace Std;struct matrix{LL A[MAXN][MAXN]; int R, c;//number of rows};    Matrix Ori, res;//initial matrix and result matrix void init ()//initialization matrix {memset (RES.A, 0, sizeof (RES.A)); RES.R = 2;    RES.C = 2;    for (int i = 1; I <= 2; i++)//tectonic unit matrix res.a[i][i] = 1; ORI.R = 2;    ORI.C = 2;    ORI.A[1][1] = ori.a[1][2] = ori.a[2][1] = 1; ORI.A[2][2] = 0;}    Matrix multi (Matrix x, Matrix y) {matrix z;    memset (z.a, 0, sizeof (Z.A)); Z.R = X.R, z.c = y.c;//the number of new matrix rows equals the number of rows of the x matrix equals the number of columns of the y matrix for (int i = 1; I <= X.R; i++) The number of rows of the//x matrix {for (int k = 1; K & Lt;= x.c;  k++)//matrix x the number of columns equal to the number of rows of the matrix Y is x.c = y.r {if (x.a[i][k] = = 0) continue;//optimization for (int j = 1; j<= y.c;        J + +)//y matrix number of columns z.a[i][j] = (Z.a[i][j] + (x.a[i][k] * y.a[k][j])% mod)% MoD; }} return z;}       void Matrix_mod (int n) {while (n)//n Power {if (N & 1)     res = multi (ori, RES);        Ori = multi (ori, ORI);    n >>= 1; } printf ("%lld\n", Res.a[1][2]% MOD);}    int main () {int N; while (scanf ("%d", &n), n!=-1) {init ();//Initialize the unit matrix Matrix_mod (N);//Matrix Fast Power} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3070 Fibonacci "matrix fast power to find the nth Fibonacci number%1000"

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.