Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有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$题...
我们先预处理出一个$f$数组:$f[i][j]$表示长度为$i$的数首位为$j$的方案总数。很容易想到转移方程。
预处理完之后,我们按照数位$DP$的一般套路,求出$[1,R]$中的答案$-$$[1,L-1]$中的答案得出$[L,R]$间的答案。
我们处理的时候还是按位处理。
值得注意的是,按位分离后若当前已有$4$和$62$就不必要继续做了,因为之后的都是不满足条件的。
同样注意的是,因为我们的$count$函数计算的本来就是$[1,r)$,所以右端点数值应该统一$+1$。
1 //It is made by Awson on 2017.9.21 2 #include <set> 3 #include <map> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <string> 9 #include <cstdio> 10 #include <vector> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #define Min(a, b) ((a) < (b) ? (a) : (b)) 16 #define Max(a, b) ((a) > (b) ? (a) : (b)) 17 #define LL long long 18 using namespace std; 19 const int N = 1000000; 20 21 int n, m; 22 int f[10][10]; 23 24 int count(int x) { 25 int a[10] = {0}, cnt = 0; 26 while (x) { 27 a[++cnt] = x%10; 28 x /= 10; 29 } 30 int sum = 0; 31 for (int i = cnt; i; i--) { 32 for (int j = 0; j < a[i]; j++) 33 if (!(j == 2 && a[i+1] == 6)) sum += f[i][j]; 34 if (a[i] == 4 || (a[i] == 2 && a[i+1] == 6)) break; 35 } 36 return sum; 37 } 38 void work() { 39 printf("%d ", count(m+1)-count(n)); 40 } 41 void pre() { 42 for (int i = 0; i <= 9; i++) f[1][i] = 1; 43 f[1][4] = 0; 44 for (int i = 2; i <= 7; i++) 45 for (int j = 0; j <= 9; j++) if (j != 4) { 46 for (int k = 0; k <= 9; k++) if (j != 6 || k != 2) { 47 f[i][j] += f[i-1][k]; 48 } 49 } 50 } 51 int main() { 52 pre(); 53 while ((~scanf("%d%d", &n, &m)) && (n || m)) 54 work(); 55 return 0; 56 }