题目来源:
http://acm.hdu.edu.cn/showproblem.php?pid=2089
题目描述:
Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为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
Author
qianneng
Source
Recommend
lcy
解题思路:
题目大概就是求一个区间内有多少个数字满足包含有4和62的,数位dp入门题,用dp【pos】【pd】,dp【i】【0/1】,i表示第几位,0表示前面不是6,1表示前面是6,然后用数位dp模板就行。
代码:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e5+10;
int n,m,dp[10][2],sw[10];
int dfs(int pos,int pd,bool limit)
{
if(pos==-1)return 1;
if(!limit&&dp[pos][pd]!=-1)return dp[pos][pd];
int len=limit?sw[pos]:9;
int ans=0;
for(int i=0;i<=len;i++)
{
if(i==4||(pd&&i==2))continue;
ans+=dfs(pos-1,i==6,limit&&i==len);
}
if(!limit)dp[pos][pd]=ans;///为什么呢
return ans;
}
int jj(int x)
{
int len=0;
while(x)
{
sw[len++]=x%10;
x/=10;
}
return dfs(len-1,0,1);
}
int main()
{
memset(dp,-1,sizeof(dp));
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m)break;
printf("%d\n",jj(m)-jj(n-1));
}
return 0;
}