hdu-4549 (Matrix fast Power + Euler's theorem)

Source: Internet
Author: User
Tags printf

Problem Description:

M Fibonacci Sequence F[n] is an integer sequence, which is defined as follows:

F[0] = a
F[1] = b
F[n] = f[n-1] * F[n-2] (n > 1)

Now give a, B, N, can you find the value of F[n]?

Input

Input contains multiple sets of test data;
One row per group of data, containing 3 integers a, b, n (0 <= A, b, n <= 10^9)

Output

For each set of test data output an integer f[n], because f[n] may be large, you only need to output F[N] to 1000000007 modulo value, each set of data output one row.

Sample Input

0 1 0
6 10 2
Sample Output

0
60
Topic Test Instructions: Topic give us a n let's find out a new Fibonacci sequence of f (n)% 1e9+7.

Title Analysis: Although this topic is different from the original Fibonacci sequence, but the essence is the same, for a, b their index conforms to the Fibonacci sequence of rules f (n) =f (n-1) +f (n-2), just need to note that their F (0) and F (1) value is not the same, We use matrices to quickly run A and b exponentially, and the rest is to calculate a^ (x1)%c and b^ (x2)%c.

This place I am a lot of fat, find this place is the difficulty of this problem.

For A^b%c, we

The first thing I thought was:

For the a^x% mod formula, we have the following expansion:

A^x% mod=a^ (X%phi[mod]+phi[mod])%mod (when and only when X>=phi[mod] (in my previous blog, I used the formula)

But this place is not good, because the MoD is very large and has no effect.

Here we will use Euler's theorem to solve: in number theory, Euler's theorem, (also called Fermat-Euler theorem) is a property about congruence. Euler's theorem shows that if n,a is a positive integer and N,a coprime, then:

For A^b%c, we make x*phi[c]+y=b equal to a^ (x*phi[c]+y)%c
a^ (x*phi[c]+y)%c=a^ (x*phi[c])%c * a^ (y)%c = (a^ (phi[c])%c) ^x *a^ (y)%c found no, the preceding equation is that the Euler theorem expression equals 1
Then the primitive is equal to a^ (y)%c because C is the prime number then phi[c]=c-1, then y=b% (Phi[c]) =b% (C-1); namely: A^b%c =a^ (B% (C-1))%c code is as follows:

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #define LL Long Long

using namespace Std;
const int mod=1e9+7;

int a,b,n;
    struct matrix//build matrix {ll f[3][3];
        Matrix operator * (const matrix &a) Const {Matrix res;
                for (int i=1;i<=2;i++) {for (int j=1;j<=2;j++) {res.f[i][j]=0;
                for (int k=1;k<=2;k++) res.f[i][j]= (res.f[i][j]+ (*this). F[i][k]*a.f[k][j]);
            res.f[i][j]=res.f[i][j]% (mod-1);
    }} return res;

}}ppow;
    void init ()//constant matrix {ppow.f[1][1]=ppow.f[1][2]=1;
Ppow.f[2][1]=1;
    } Matrix Fast_pow1 (Matrix Base,int k)//fast power matrix {matrix ans=base;
        while (k) {if (k&1) ans=ans*base;
        Base=base*base;
    k>>=1;
} return ans;
    } ll Fast_pow2 (ll base,ll k)//fast power number {LL ans=1;
    Base=base%mod;
          while (k) {if (k&1)  Ans=ans*base%mod;
        Base=base*base%mod;
    k>>=1;
} return ans;
    } int main () {init ();
        while (scanf ("%d%d%d", &a,&b,&n)!=eof) {if (n==0) {printf ("%d\n", a%mod); continue;}
        if (n==1) {printf ("%d\n", b%mod); continue;}
        n-=2;
        ll Sum=1;
        struct matrix cur;
        Cur=ppow;
        Cur=fast_pow1 (Cur,n);
        Sum=fast_pow2 (a,cur.f[1][2])%mod;//a and B are not the same as the index of the storage note sum=sum*fast_pow2 (b,cur.f[1][1])%mod;
    printf ("%lld\n", sum);
} 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.