例题:http://acm.hdu.edu.cn/showproblem.php?pid=2089
数位DP将数拆开存在数组,将数组DP(注意数字不能有连续的62和4)
详细讲解参考https://blog.csdn.net/jk211766/article/details/81474632
hdu2089代码贴上:
#include<bits/stdc++.h>
using namespace std;
#define long long ll
int a[20];//存数字
int dp[20][2];
int dfs(int count/*位数*/,int pre/**/,int sta/*状态*/,bool limit/*判断是否有上限*/){
if(count == -1)
return 1;
if(!limit && dp[count][sta] != -1)
return dp[count][sta];//退出递归
int up = limit ? a[count] : 9;
int tmp = 0;
for(int i = 0;i <= up;i++){
if(pre == 6 && i == 2)/*排除62的情况*/
continue;
if(i == 4)/*排除4的情况*/
continue;
tmp += dfs(count-1,i,i == 6,limit && i == a[count]);
}
if(!limit)
dp[count][sta] = tmp;
return tmp;
}
int solve(int x){//将数拆开存在数组里
int count = 0;
while(x){
a[count++] = x % 10;
x /= 10;
}
return dfs(count-1,-1,0,true);
}
int main(){
int a,b;
while(~scanf("%d%d",&a,&b) && a + b){
memset(dp,-1,sizeof(dp));
printf("%d\n",solve(b) - solve(a-1));
}
return 0;
}