hdu2089 idea Digital DP template problem. Search from high to low, use POS to record the current position, LST records the number of the previous position, BZ records whether the last position is 6,limit to record whether the previous position reached the upper bound (if reached, it will need to process the upper bound of the current position) code
#include <cstdio> #include <iostream> #include <cstring>using namespace std;typedef long Long ll;ll Read () {ll x=0,f=1;char C=getchar (); while (c< ' 0 ' | | C> ' 9 ') {if (c== '-') f=-1; C=getchar (); } while (c>= ' 0 ' &&c<= ' 9 ') {x=x*10+c-' 0 '; C=getchar (); } return x*f;} int f[20][2];int a[20];int dfs (int pos,int lst,int bz,int limit) {if (pos==0) return 1; if (f[pos][bz]!=-1) return F[POS][BZ]; int up=limit?a[pos]:9; int ans=0; for (int i=up;i>=0;--i) {if (i==4| | (lst==6&&i==2)) Continue Ans+=dfs (Pos-1,i,i==6,limit&&i==up); } if (!limit) F[pos][bz]=ans; return ans;} int solve (int x) {memset (a,0,sizeof (a)); memset (F,-1,sizeof (f)); while (x) {a[++a[0]]=x%10; x/=10; } return Dfs (a[0],0,0,1);} int main () {int l,r; while (1) {l=read (); R=read (); if (!l&&!r) break; printf ("%d\n", Solve (R)-solve (L-1)); } return 0;}
[hdu2089][]