Poj 3070 Fibonacci [matrix concatenation]

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

Description

In the Fibonacci integer sequence,F0 = 0,F1 = 1, andFN=FN− 1 +FN−2N≥2. For example, the first ten terms
The maid 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:

.

Source

Stanford local 2006
3070 Accepted 132 k 0 ms C ++ 20172b 21:17:22

#include<cstdio>const int mod = 10000;struct Matrix{    __int64 a11, a12, a21, a22;}matrix;Matrix mull(Matrix a, Matrix b){    Matrix c;    c.a11 = a.a11*b.a11+a.a12*b.a21;    c.a12 = a.a11*b.a12+a.a12*b.a22;    c.a21 = a.a21*b.a11+a.a22*b.a21;    c.a22 = a.a21*b.a12+a.a22*b.a22;    c.a11 %= mod;    c.a12 %= mod;    c.a21 %= mod;    c.a22 %= mod;    return c;}Matrix find(Matrix m, __int64 n){    Matrix b;    b.a11 = 1; b.a12 = 0;    b.a21 = 0; b.a22 = 1;    while(n > 0)    {        if(n&1)        {            b = mull(b, m);        }        n = n>>1;        m = mull(m, m);    }    return b;}int main(){    __int64 n;    while(scanf("%I64d", &n) != EOF)    {        if(n == -1) break;        else if(n == 0)        {           printf("0\n");           continue;        }        __int64 a11, a12, a21, a22;        Matrix m, ans;        m.a11 = 1;        m.a12 = 1;        m.a21 = 1;        m.a22 = 0;        ans = find(m, n);        __int64 result = ans.a12%10000;        printf("%I64d\n", result);    }    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.