HDU 5667 Sequence Matrix fast Power + Fermat theorem

Source: Internet
Author: User

Sequence

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)


Problem Description    Holion 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.

Input    The 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≤1≤a,b,c≤9 , P is a prime number,and p≤9+7.

OutputOutput one number for each Case,which is F n mod p. c8>

Sample Input15 3 3 3 233

Sample Output190

Sourcebestcoder Round #80思路: is f (n) =f (n-1) *c+f (n-2) +b; A^f (n)%p=a^ (f (n)% (p-1))%p;//Pony Small Matrix Construction Blog: http://www.cnblogs.com/frog112111/archive/2013/05/19/3087648.html There's a pit point A. %p==0
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell __int64#defineMoD 1000000007#defineINF 999999999//#pragma COMMENT (linker, "/stack:102400000,102400000")ll A,b,c,n,p;struct  is{ll a[Ten][Ten];}; isJuzhenmul ( isA isB,ll Hang, ll lie) {    inti,t,j;  isans; memset (ANS.A,0,sizeof(ANS.A));  for(i=1; i<=hang;i++)     for(t=1; t<=lie;t++)     for(j=1; j<=lie;j++) {Ans.a[i][t]+ = (a.a[i][j]*b.a[j][t]); Ans.a[i][t]%= (P-1); }    returnans;} isQuickpow ( isAns isa,ll x) {     while(x) {if(x&1) Ans=juzhenmul (Ans,a,3,3); A=juzhenmul (A,a,3,3); X>>=1; }    returnans;} ll Quickpow1 (ll a,ll N) {ll mul=1; if(a%p==0)    return 0;  while(n) {if(n&1) mul*=a,mul%=p; N>>=1; A=a*A; A%=p; }    returnMul;}intMain () { isAnsBase, GG; intGGGG; scanf ("%d",&GGGG);  while(gggg--) {ll fn; scanf ("%i64d%i64d%i64d%i64d%i64d",&n,&a,&b,&c,&p); if(n>2) {memset (ANS.A,0,sizeof(ANS.A)); ans.a[1][1]=1; ans.a[2][2]=1; ans.a[3][3]=1; memset (BaseA0,sizeof(Base. a)); Base. a[1][1]=C; Base. a[1][2]=1; Base. a[2][1]=1; Base. a[3][1]=1; Base. a[3][3]=1; Ans=quickpow (ans,Base, N-2); gg.a[1][1]=b; gg.a[1][2]=0; gg.a[1][3]=b; Ans=juzhenmul (Gg,ans,1,3); FN=ans.a[1][1]; }        Else        {            if(n==1) FN=0; Elsefn=b; } printf ("%i64d\n", Quickpow1 (A,FN)); }    return 0;}
View Code

HDU 5667 Sequence Matrix fast Power + Fermat theorem

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.