Portal
Description
Gives the number of two 11-digit \ (l,r\), which satisfies the following two conditions in the [l,r]\] .
- appears at least \ (3\) adjacent to the same number;
- Cannot appear both \ (4\) and \ (8\).
Solution
Digital DP.
First, convert the problem into\ (Solve (R)-solve (L) \)The form, so that only requires no more than\ (n\)The number of satisfied conditions.
Defined\ (dp[k][x][f_1][f_2][f_3][f_4]\), where\ (k\)Represents the number of bits,\ (x\)Represents the Mantissa,\ (f_1\)Represents the first\ (k\)Bit and section\ (k-1\)Bit is the same,\ (f_2\)Indicates if there have been three connected,\ (f_3\)Said\ (4,8\)The occurrence of (\ (00,01,10,11\)),\ (f_4\)Indicates whether the first\ (k\)Reach the upper limit.
Consider section\ (k+1\)Each of the BITS is evaluated\ (i\)。 If\ (i=x\), you\ (f_1=1\)If there is already a third company or the original\ (f_1=1\)And\ (i=x\), you\ (f_2=1\); if\ (i\)Equals\ (4\)Or\ (8\)Change\ (f_3\); If in the first\ (k\)Limit is reached and\ (i\)equals N of the first\ (k+1\)Bit, then\ (f_4=1\)。
Use the queue to transfer or cycle through each dimension to resolve.
Time complexity \ (O (11x10x2x2x4x2\cdot10) \).
Code
//"cqoi2016" mobile phone number#include <cstdio>#include <cstring>#include <queue>typedef Long LongLintstructstate{intK,X,F1,F2,F3,F4; Stateint_k,int_x,int_F1,int_F2,int_F3,int_F4) {k=_k,x=_x,f1=_f1,f2=_f2,f3=_f3,f4=_f4;}};Const intlen= One; Lint dp[ A][Ten][2][2][4][2];std::queue<state> Q;intIS48 (intx) {return(x==8) <<1| (x==4);} Lint solve (lint N) {memset (DP,0,sizeofDP);intv[ A]; for(Lint i=len,t=n;i>=1; I--, t/=Ten) v[i]=t%Ten; for(intI=1; i<=v[1];i++) {intF3=IS48 (i), f4= (i==v[1]); dp[1][i][0][0][f3][f4]=1; Q.push (State (1I0,0, F3,F4)); } while(! Q.empty ()) {State s=q.front (); Q.pop ();intK=S.K,X=S.X,F1=S.F1,F2=S.F2,F3=S.F3,F4=S.F4,VAL=DP[K][X][F1][F2][F3][F4];if(K==len)Continue;intT=f4?v[k+1]:9; for(intI=0; i<=t;i++) {int_f1= (i==x), _f2=f2| | f1&& (i==x), _f3=f3|is48 (i),_f4=f4&&i==t; Lint &r=dp[k+1][I][_F1][_F2][_F3][_F4];if(!R) Q.push (State (k+1, I,_F1,_F2,_F3,_F4)); R+=val; }} Lint R=0; for(intI=0; i<=9; i++) for(int_f1=0; _f1<=1; _f1++) for(int_f3=0; _f3<=2; _f3++) r+=dp[len][i][_f1][1][_f3][0]+dp[len][i][_f1][1][_f3][1];returnR;}intMain () {lint l,r; scanf"%lld%lld", &l,&r);if(l== (Lint)1e10) printf ("%lld\n", Solve (R)-solve (L)+1);Elseprintf"%lld\n", Solve (R)-solve (L-1));return 0;}
LibreOJ2044-"cqoi2016" Mobile phone number