题目描述
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
Sample Input
1 100
0 0
Sample Output
80
题目分析
这是一道比较简单的数位dp的题目,没什么特别难的地方。
因为要去掉区间中所有带62和4的数,因此在枚举每一位的时候,如果当前位是4或者当前位是2且上一位是6(dfs时要用一个变量来记录上一位是不是6),那么不加到答案中即可。
剩下的就算数位dp的模板了。
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int N=1e5+5;
int p[10]; //记录所给数字的每一位
int f[10][2]; //记忆化搜索,第一维是位数,第二维记录上一位是否为6
int dfs(int len,bool six,bool st) //len表示位数,six表示上一位是否为6,st表示当前是否位最大位数
{
if(len==-1) return 1;
if(!st&&f[len][six]!=-1) return f[len][six]; //记忆化搜索
int ans=0;
int x=st?p[len]:9;
for(int i=0;i<=x;i++) //枚举这一位可能的所有数
{
if(i==4||i==2&&six) continue; //若出现4或者62则跳过
ans+=dfs(len-1,i==6,st&&i==x); //否则继续递归计算
}
if(!st) f[len][six]=ans; //如果当前不是最大位(具有一般性),则用f记录
return ans;
}
int sol(int x) //计算0-x中所以符合条件的数的个数
{
int len=0;
while(x) //用p[]记录所给数
{
p[len++]=x%10;
x/=10;
}
return dfs(len-1,false,true); //从最高位开始递归
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m),(n||m))
{
memset(f,-1,sizeof f);
printf("%d\n",sol(m)-sol(n-1));
}
return 0;
}