hdu-5667 Sequence (Matrix fast Power + Fermat theorem + fast Power)

Source: Internet
Author: User

Topic Links:

Sequence

Time limit:2000/1000 MS (java/others)

Memory limit:65536/65536 K (java/others)


problem Descriptionholion August would eat every thing he has found.
Now there is many foods,but he does not want to eat all of the them at Once,so he find a sequence.

FN=?????1,AB,ABFCN−1FN−2,N=1n=2otherWIse

He gives you 5 numbers N,a,b,c,p,and he'll eat fn Foods. But there is only p foods,so your should tell him fn mod p.

InputThe first line has a Number,t,means testcase.

Each testcase have 5 numbers,including n,a,b,c,p in a line.

1≤T≤10,1≤n≤10^,1≤a,b,c≤10^9 , P is a prime number,and p≤10^9+7.

Output Output one number for each Case,which is fn mod p.

Sample Input 15 3 3 3 233

Sample Output 190 Test Instructions:Ask F (n) The result of P modulo; Ideas:(ab) p[n]= AB * ((AB) p[n-1]) c * ((AB) p[n-2]); Recursive formulas can be written like this, merged into (AB) p[n]= (AB) (c*p[n-1]+p[n-2]+1); p[n]=c*p[n-1]+p[n-2]+ can be obtained 1; Such recursion can be obtained by matrix multiplication of the nth item; This is an application of matrix multiplication, to the matrix67 great God's blog address can learn, dot here to construct matrix multiplication: P[n] C 1 1 p[n-1]p[n-1]= 1 0 0 * p[n-2]1 0 0 1 1 Then there is a problem in the middle, that is, the problem of modeling;ab*p[n]%mod=ab*p[n]% (mod-1)%mod;This is obtained according to the Fermat theorem; a (p-1) ξ1%p; here's the address.ab*p[n]%mod=ab*p[n]/(mod-1) * (mod-1) +b*p[n]% (mod-1)%mod;make x=b*p[n]/(mod-1) ab*p[n]%mod=ax* (mod-1) +b*p[n]% (mod-1 )%mod=ab*p[n]% (mod-1)%mod;This is the result of taking the mold;and then the answer is fast power calculation;also have to pay attention to is the situation of a%p==0; AC Code:
/*5667 0MS 1584K 1835B g++ 2014300227*/#include<bits/stdc++.h>using namespacestd;Const intn=1e4+5; typedefLong Longll;Const intmod=1e9+7; ll N,a,b,c,p;structmatrix{ll a[3][3];}; Matrix A;voidIint (ll x)//matrix initialization; {a.a[0][0]=x; a.a[0][1]=1; a.a[0][2]=1; a.a[1][0]=1; a.a[1][1]=a.a[1][2]=0; a.a[2][0]=a.a[2][1]=0; a.a[2][2]=1;}    Matrix Mul (Matrix X,matrix y)//matrices multiplied {matrix ans;  for(intI=0;i<3; i++)    {         for(intj=0;j<3; j + +) {Ans.a[i][j]=0;  for(intk=0;k<3; k++) {Ans.a[i][j]+ = (X.a[i][k]*y.a[k][j])% (P-1); ANS.A[I][J]%= (P-1); }        }    }    returnans;} ll fast_fun (Matrix Temp,ll num)//matrix fast power; {Matrix S,Base;  for(intI=0;i<3; i++)//s initialized to the unit matrix { for(intj=0;j<3; j + +) {S.a[i][j]=0; Base. a[i][j]=Temp.a[i][j]; }} s.a[0][0]=s.a[1][1]=s.a[2][2]=1;  while(num) {if(num&1) {s=mul (s),Base); }        Base=mul (Base,Base); Num= (num>>1); }    return(s.a[0][0]+s.a[0][2])% (P-1);} ll Fastpow (ll fx,ll FY)//fast power to find results; {ll S=1,Base=FX;  while(FY) {if(fy&1) {s*=Base; S%=p; }        Base*=Base; Base%=p; FY= (fy>>1); }    returns;}intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&p);        Iint (c); if(n==1) printf ("1\n"); Else if(n==2) printf ("%lld\n", Fastpow (A, b)); Else        {            if(a%p==0) printf ("0\n"); Else{ll GG=fast_fun (a,n-2) *b% (P-1); printf ("%lld\n", Fastpow (A,GG)); }        }    }    return 0;}

hdu-5667 Sequence (Matrix fast Power + Fermat theorem + fast Power)

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.