Hdoj m Fibonacci sequence 4549 "Matrix fast Power + fast Power + Fermat theorem + Euler function"

Source: Internet
Author: User



M Fibonacci SequenceTime limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 2096 Accepted Submission (s): 596


Problem descriptionm 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 inputs contain 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 outputs an integer f[n], because f[n] may be large, you only need to output F[N] The value after 1000000007 modulo, each set of data output one row.
Sample Input
0 1 06) 10 2

Sample Output
060

Source2013 Gold Shanxi Mountain Lodge Creative Game Program Challenge--Preliminaries (2)
recommendliuyiding | We have carefully selected several similar problems for you:5390 5389 5388 5387 5386

Topic Analysis:

The problem is to multiply the Fibonacci number, and begin to think of the sum of Fibonacci, thought to be very bare matrix fast power ...

However, the problem is that it is the process of multiplying. But when you write a few numbers, you will find that these numbers are Cheng Feibo.

3 4 12 48 ..... ==> 3^1*4^0 3^0*4^1 3^1*4^1 3^1*4^2 .....

We can ask what is the nth exponent of the assignment. But because the number is more, so the results will certainly be relatively large, we have to think about the index to take surplus. We think of the Fermat theorem:

a ^ n = a ^ (n Euler (M)) * (mod m)

The problem is that the result is m=1000000007, so we should be Euler (M) for the number of the index to be taken. Since M is a prime number, Euler (m) = 1000000006.

It is easy to find the multiplication matrix, which can be quickly idempotent. But attention to the topic of N, the title n represents the N+1 item.

At the beginning, I looked for the matrix of multiplication that was the matrix of 3*3 .... Started Tle, thought it was the reason for the matrix, changed to 2*2 ... It's a joke. The matrix is not too large. More than one dimension is not slow where. Mainly should be long long type, write int on tle ... Long Long is over.

AC Code: Multiplication matrix of 3*3

#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using namespace Std;const int modn = 1000000007;const int MAXN = 3;typedef long long LL;    ll Res[maxn][maxn];void Matmul (ll x[maxn][maxn],ll Y[MAXN][MAXN]) {int t[maxn][maxn]={0}; for (int i=0;i<maxn;i++) {for (int k=0;k<maxn;k++) {if (X[i][k]) for (int j=0;j<maxn        ; j + +) t[i][j]= (t[i][j]+x[i][k]*y[k][j]% (Modn-1))% (Modn-1); }} for (int i=0;i<maxn;i++) for (int j=0;j<maxn;j++) x[i][j]=t[i][j];}        void Matrix (LL x[maxn][maxn],ll N) {for (int i=0;i<maxn;i++) {//res[i][i]=1;    for (int j=0;j<maxn;j++) res[i][j]= (I==J);        } while (n) {if (n&1) Matmul (res,x);        Matmul (X,X);    n>>=1;    }}ll Power (LL x,ll N) {x%=modn;    LL Res=1; WhilE (n) {if (n&1) Res=res*x%modn;        X=X*X%MODN;    n>>=1; } return res;    int main () {LL a,b,n;            while (scanf ("%lld%lld%lld", &a,&b,&n)!=eof) {if (n<3) {if (n==0) printf ("%lld\n", a);            else if (n==1) printf ("%lld\n", b);            else if (n==2) printf ("%lld\n", a*b);        Continue        } LL NUM[MAXN][MAXN];        LL F[3];        memset (num,0,sizeof (num));        Num[0][0]=num[0][1]=1;        Num[1][2]=num[1][0]=1;        Matrix (num,n-2);//(N+1)-3;        F[2]=1;f[1]=0;f[0]=1;        LL ans1=0;        for (int j=0;j<3;j++) ans1= (ans1+f[j]*res[j][0]% (Modn-1))% (Modn-1);        F[2]=0;f[1]=1;f[0]=1;        LL ans2=0;        for (int j=0;j<3;j++) ans2= (ans2+f[j]*res[j][0]% (Modn-1))% (Modn-1);        LL cnt=power (a,ans1) *power (b,ans2)%modn;    printf ("%lld\n", CNT); } return 0;}

Multiplication matrix of 2*2

#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using namespace Std;const int modn = 1000000007;const int MAXN = 2;typedef long long LL;    ll Res[maxn][maxn];void Matmul (ll x[maxn][maxn],ll Y[maxn][maxn]) {ll t[maxn][maxn]={0}; for (int i=0;i<maxn;i++) {for (int k=0;k<maxn;k++) {if (X[i][k]) for (int j=0;j<maxn        ; j + +) t[i][j]= (t[i][j]+x[i][k]*y[k][j]% (Modn-1))% (Modn-1); }} for (int i=0;i<maxn;i++) for (int j=0;j<maxn;j++) x[i][j]=t[i][j];}        void Matrix (LL x[maxn][maxn],ll N) {for (int i=0;i<maxn;i++) {//res[i][i]=1;    for (int j=0;j<maxn;j++) res[i][j]= (I==J);        } while (n) {if (n&1) Matmul (res,x);        Matmul (X,X);    n>>=1;    }}ll Power (LL x,ll N) {x%=modn;    LL Ans=1; While(n)        {if (n&1) Ans=ans*x%modn;        X=X*X%MODN;    n>>=1; } return ans;    int main () {LL a,b,n;            while (scanf ("%lld%lld%lld", &a,&b,&n)!=eof) {if (n<3) {if (n==0) printf ("%lld\n", a);            else if (n==1) printf ("%lld\n", b);            else if (n==2) printf ("%lld\n", a*b);        Continue        } LL NUM[MAXN][MAXN];        LL F[3];        Num[0][0]=num[1][0]=num[0][1]=1;        num[1][1]=0;        Matrix (num,n-1);//(N+1)-3;        f[1]=1;f[0]=0;        LL ans1=0;        for (int j=0;j<maxn;j++) ans1= (ans1+f[j]*res[j][0]% (Modn-1))% (Modn-1);        printf ("%d", ans1);        F[1]=0;f[0]=1;        LL ans2=0;        for (int j=0;j<maxn;j++) ans2= (ans2+f[j]*res[j][0]% (Modn-1))% (Modn-1);        printf ("%d \ n", ans2);        LL cnt=power (a,ans1) *power (b,ans2)%modn;    printf ("%lld\n", CNT); } return 0;}


M Fibonacci SequenceTime limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 2096 Accepted Submission (s): 596


Problem descriptionm 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 inputs contain 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 outputs an integer f[n], because f[n] may be large, you only need to output F[N] The value after 1000000007 modulo, each set of data output one row.
Sample Input
0 1 06) 10 2

Sample Output
060

Source2013 Gold Shanxi Mountain Lodge Creative Game Program Challenge--Preliminaries (2)
recommendliuyiding | We have carefully selected several similar problems for you:5390 5389 5388 5387 5386

Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Hdoj m Fibonacci sequence 4549 "Matrix fast Power + fast Power + Fermat theorem + Euler function"

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.