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 1000 0Sample Output
80
————————————————————————————
垃圾水题,如果范围开大点就只能数位dp了。
于是自己写了一个数位dp。
大致思路和BZOJ1026: [SCOI2009]windy数一模一样。
也没啥好多说的了。
#include<cstdio> #include<cstring> using namespace std; const int N=11; int a[N],f[N][10][2]; int dp(int x){ int len=0; while(x)a[++len]=x%10,x/=10; if(len==0)a[++len]=0; memset(f,0,sizeof(f)); for(int i=0;i<=9;i++){ if(i==4)continue; if(i<=a[1])f[1][i][0]=1; else f[1][i][1]=1; } for(int i=2;i<=len;i++){ for(int j=0;j<=9;j++){ for(int k=0;k<=9;k++){ if(j==4||k==4)continue; if(j==6&&k==2)continue; if(j<a[i]) f[i][j][0]+=f[i-1][k][0]+f[i-1][k][1]; else if(j==a[i]) f[i][j][0]+=f[i-1][k][0],f[i][j][1]+=f[i-1][k][1]; else f[i][j][1]+=f[i-1][k][0]+f[i-1][k][1]; } } } int ans=0; for(int i=1;i<=9;i++){ if(i<a[len])ans+=f[len][i][0]+f[len][i][1]; if(i==a[len])ans+=f[len][i][0]; } for(int i=len-1;i;i--){ for(int j=1;j<=9;j++){ ans+=f[i][j][0]+f[i][j][1]; } } return ans; } int main(){ int x,y; while(scanf("%d%d",&x,&y)!=EOF&&x+y){ printf("%d ",dp(y)-dp(x-1)); } return 0; }