HDU 2604 Queuing (DP recursion)

Source: Internet
Author: User

Last night, the second matrix of fast power, I also want to set up a matrix directly (forgive elder brother template problem to do more), and then see clearly after test instructions feel a bit like before the digital DP water problem, then use the method of digital DP to analyze, pushed a good one will finally launch its recursive relationship (or rookie, The DP is still very unskilled):

DP[I][0/1] represents an i-digit number that starts with 0/1 without 101 and 111 (denoted by a f,0 for M, looks at the convenience point), and then the state transition equation is:

DP[I][0]=DP[I-1][0]+DP[I-1][1]; After 0, there's no problem with the number.

DP[I][1]=DP[I-2][0]+DP[I-3][0];

In this case, the second, the beginning of 1 words to consider more, first of all i-2 bit must not be 1, otherwise i-1 0/1 will constitute 101 or 111, so the first bit and the i-2 bit is determined (that is, 1x0xxx ... )。 Next look at the i-1 bit, if take 0, then I-2 bit after the number is no problem, so at this time there are dp[i-2][0] kind of situation, if take 1, then the i-3 bit can not be 1, otherwise the first i-1,i-2,i-3 will constitute 101, so the i-3 can only take 0, There is a dp[i-3][0] situation, and this is the second recursive formula.

With the state transition equation, it is easy to write code, pay attention to the processing of the boundary (because it involves dp[i-3], so the first 3 DP[I][0/1] are hand-calculated):

1#include <cstdio>2#include <cstring>3typedefLong LongLL;4 intdp[1000006][2], mod;5 //This is a linear recursive dp,4000+ms, but it's good to have fun ~6 7InlinevoidInitintN) {8dp[1][0]= dp[1][1]=1%MoD;9dp[2][0]= dp[2][1]=2%MoD;Tendp[3][0]=4%mod; dp[3][1]=2%MoD; One      for(intI=4; i<=n; ++i) { Adp[i][0]= (dp[i-1][0]+dp[i-1][1])%MoD; -dp[i][1]= (dp[i-2][0]+dp[i-3][0])%MoD; -     } the } -  - intMain () { -     intL; +      while(~SCANF ("%d%d",&l,&MoD)) { - init (L); +printf"%d\n", (dp[l][0]+dp[l][1])%MoD); A     } at     return 0; -}

The first commit after the timeout, in fact, because the scanf function is missing a file end of the flag, but I thought it was the problem of the algorithm, but also because the first known that the problem is related to the matrix fast power, so had to build a matrix to do. Put the just recursive relationship under the following:

DP[I][0]=DP[I-1][0]+DP[I-3][0]+DP[I-4][0]; In conjunction with the 2nd equation, we can draw

DP[I][1]=DP[I-2][0]+DP[I-3][0];

So there are dp[i][0]=1*dp[i-1][0]+0*dp[i-2][0]+1*dp[i-3][0]+1*dp[i-4][0];

The four-order, coefficients are 1,0,1,1, so there is [dp[i],dp[i-1],dp[i-2],dp[i-3]]t = A * [dp[i-1],dp[i-2],dp[i-3],dp[i-4]]t, where matrix A =

1 0 1 1
1 0 0 0
0 1 0 0
0 0 1 0

The code is as follows:

1#include <cstdio>2#include <cstring>3 intMoD;4 //after the introduction of linear recursive relations to construct a matrix to do, in some detail problems debugging for a long time,5 //Finally, because the scanf function did not write well to return time-out t.t, fortunately finally found the6 //finally to 500+ms over, I do not understand those dozens of MS on the Hangzhou Electric is how the people do! 7 8 structmatrix{9     intc[6][6],n;TenMatrixintn=0): N (n) {memset (c,0,sizeof(c)); } One     voididentity () { A          for(intI=1; i<=n; ++i) c[i][i]=1; -     } -Matrixoperator*(ConstMatrix &m2)Const { the Matrix Mul (n); -          for(intI=1; i<=n; ++i) -              for(intj=1; j<=n; ++j) -                  for(intk=1; k<=n; ++k) +mul.c[i][j]= (mul.c[i][j]+c[i][k]*m2.c[k][j]%mod)%MoD; -         returnMul; +     } AA4); at  -Matrix Quick_mod (Matrix M,intb) { - Matrix Res (M.N); - res.identity (); -      while(b) { -         if(b&1) res= res*m; inM= m*m; -b>>=1; to     } +     returnRes; - } the  *  $InlineintDp_i0 (inti) {Panax Notoginseng     intdp[5]= {1,1,2,4,6}; -     if(i<0)return 0; the     if(i<=4)returnDp[i]; +Matrix tmp= Quick_mod (a,i-4); A     return(tmp.c[1][1]*dp[4]%mod +tmp.c[1][2]*dp[3]%mod +tmp.c[1][3]*dp[2]%mod +tmp.c[1][4]*dp[1]%MOD)%MoD; the } +  - intMain () $ { $a.c[1][1]= a.c[1][3]= a.c[1][4]=1; -a.c[2][1]= a.c[3][2]= a.c[4][3]=1; -     intL; the      while(~SCANF ("%d%d",&l,&MoD)) { -         intans[4]= {0,2,4,6};Wuyi         if(l<=3) {printf ("%d\n", Ans[l]%mod);Continue ; } the          -         intdp_l0=dp_i0 (L); Wu         intDp_l1= (Dp_i0 (l2) +dp_i0 (l3))%MoD; -printf"%d\n", (DP_L0+DP_L1)%MoD); About     } $     return 0; -}

Or because some of the details of the debugging for a long time, after the first commit or timeout, inadvertently flip the code of other people suddenly see that scanf function only suddenly found their scanf function No document end of the logo, change it, the last 500+ms, sure enough logn algorithm is fast AH ~ Then the above direct recursion code is the same problem, after the submission also AC, but it is 4000+ms card, it seems that the background data should be a lot of, so 10^6 O (n) approach is still very close to the edge of the time frame ... But I still do not understand what those dozens of Ms people on Hangzhou Electric are doing!

HDU 2604 Queuing (DP recursion)

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.