Space 512M time 2s
"Title description"
There are n positive integers greater than 1 a1,a2,..., an, we know that the recursion of the Fibonacci sequence is F (i) =f (i-1) +f (i-2), Now we modify this recursive to F (i) =f (i-1) +f (i-2) +r (i-1), where R (x) is a1,a2,..., an in x The number of approximate numbers. The value of the F (m) MoD 19940417 is now required . Note: initial value F (1) =1,f (2) =1
Input format:
The first line is two numbers n,m.
The next row n positive integers a1,a2,..., an.
Output format:
The output line is only one number, and F (m) mod 19940417 values.
Sample input:
3 7
2 2 3
Sample output:
33
Data range:
30% data n<=1000,m<=1000
In addition 20% data n=0,m<=109
100% 's data n<=100000, m<=109,2<=ai<=109
Exercises
For 100% of the data, we can first consider the fib[i]=fib[i-1]+fib[i-2] answer first with the Matrix quickly run out of power. Then enter the AI in turn to see the effect of each AI on fib[m] because fib (i) =fib (i-1) +fib (i-2) +r (i-1), so each AI, where it is K times (K*AI<=M), will have a +1 effect on the Fibonacci value. We consider that if we add a 1 to the retracement sequence and continue with the subsequent recursion, then the value of item J (j>i) is fib[j]+fib[j-i+1]. So actually we can deal with each AI separately, for AI, it will contribute to the final answer fib[m MoD ai]+fib[ai+ (M mod ai)]+ ... Ensure that the value within [] is less than or equal to M.
But if the answer is just one to add fib[k*ai+ (M%ai)], or it will time out, it must be used to optimize the matrix fast power, if we let B to represent Fib[m%ai] matrix, then f[k*ai+ (M%ai)] can be expressed as b*a^k*ai, and then the solution is SUM = (A + a^2 + a^3 + ... + a^b)%c problem (explanation).
1#include <cstdio>2#include <algorithm>3 using namespacestd;4typedefLong LongLL;5 Const intMod=19940417;6 structMat {7 inta,b,c,d;8 }zr,e,f,ans;9 intn,m;TenMat pre[ -],pw[ -]; OneMatoperator*(Mat X,mat Y) { A Mat Z; -Z.a= (LL) x.a*y.a+ (LL) x.b*y.c)%MoD; -z.b= (LL) x.a*y.b+ (LL) x.b*y.d)%MoD; theZ.c= (LL) x.c*y.a+ (LL) x.d*y.c)%MoD; -Z.d= (LL) x.c*y.b+ (LL) x.d*y.d)%MoD; - returnZ; - } +Matoperator+(Mat X,mat Y) { - Mat Z; +Z.a= (X.A+Y.A)%MoD; Az.b= (x.b+y.b)%MoD; atz.c= (X.C+Y.C)%MoD; -z.d= (X.D+Y.D)%MoD; - returnZ; - } -Mat FPM (Mat A,intb) { -Mat w=E; in while(b) { - if(b&1) w=w*A; toa=a*A; +b>>=1; - } the returnW; * } $Mat Vsum (intN) {Panax Notoginseng if(n==0)returnZR; - if(n==1)returnE; the intm=1, t=0; + while(m<=n) m<<=1,++T; Am>>=1,--T; the returnPre[t]+pw[t]*vsum (nm); + } - voidPrepare (Mat A) {//a matrix is the AI sub-square of the coefficient matrix $ for(intI=0; i<= -;++i) { $ if(i==0) pw[i]=A; - Elsepw[i]=pw[i-1]*pw[i-1]; - if(i==0) Pre[i]=e;//Unit Matrix the Elsepre[i]=pre[i-1]* (e+pw[i-1]); - }Wuyi } theMat Solve (intd) { - if(d>=m)returnZR; Wu intk= (M-1)/D; - Prepare (FPM (f,d)); About returnFPM (f,m-1-K*D) *Vsum (k); $ } - intMain () { - //freopen ("fib.in", "R", stdin); - //freopen ("Fib.out", "w", stdout); Ascanf"%d%d",&n,&m); + if(m<=2){ theprintf"1\n"); - return 0; $ } theE.a=e.d=1; theF.a=f.b=f.c=1; theANS=ANS+FPM (f,m-1);//calculate the pure fib sequence first the - for(intI=1; i<=n;++i) {// in intx; thescanf"%d",&x); theans=ans+solve (x); About } theprintf"%d\n", ANS.A); the}
Bzoj 3657 Fibonacci Sequence (fib.cpp/pas/c/in/out)