Define a legal number that cannot contain 4 or consecutive 62
The number of valid numbers in the specified range [n, m]
Analysis: Digital DP
DP [I] [J] indicates the number of valid I-bits with the highest bit J.
Then, transfer according to the question rules.
After the end of DP, count the total number in the range, and finally output the table.
Code:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAX 100000000int n,k,m;int ans[1000010];int dp[10][10];int fun(int x){ int t=0; int res=0; int a[10]; while(x) { a[t++]=x%10; x/=10; } for(int i=t;i;i--) { for(int j=0;j<a[i-1];j++) { if((!(j==2&&a[i]==6))) res+=dp[i][j]; } if((a[i-1]==2&&a[i]==6)||a[i-1]==4) break; } return res;}void DP(){ for(int i=0;i<=9;i++) { if(i!=4) dp[1][i]=1; else dp[1][i]=0; } for(int i=2;i<=7;i++) { for(int j=0;j<=9;j++) { for(int k=0;k<=9;k++) { if(j!=4&&(j!=6||k!=2)) dp[i][j]+=dp[i-1][k]; } } }}void solve(){ for(int i=1;i<=1000001;i++) { ans[i]=fun(i); }}int main(){ DP(); solve(); while(scanf("%d%d",&n,&m)&&(n+m)) { printf("%d\n",ans[m+1]-ans[n]); } return 0;}
Hdu2089: No 62 (Basic digit DP)