标签:des style blog os 数据 io 2014 for
Description
Input
Output
Sample Input
1 100 0 0
Sample Output
80
思路:打表 ,数据量不大
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000000; int n, m; int f[maxn+5]; int check(int num) { while (num) { if (num % 10 == 4) return 0; if (num % 100 == 62) return 0; num /= 10; } return 1; } int cal(int num) { int ans = 0; for (int i = 1; i <= num; i++) if (check(i)) f[i] = f[i-1] + 1; else f[i] = f[i-1]; return ans; } int main() { memset(f, 0, sizeof(f)); cal(maxn); while (scanf("%d%d", &n, &m) != EOF && n+m) { printf("%d\n", f[m]-f[n-1]); } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 1000; /* * dp[i][0],表示长度为i,不存在不吉利数字 * dp[i][1],表示长度为i,不存在不吉利数字,且最高位为2 * dp[i][2],表示长度为i,存在不吉利数字 */ int n, m; int dp[maxn][3]; void init() { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; dp[0][1] = dp[0][2] = 0; for (int i = 1; i < maxn; i++) { dp[i][0] = dp[i-1][0]*9 - dp[i-1][1]; dp[i][1] = dp[i-1][0]; dp[i][2] = dp[i-1][2]*10 + dp[i-1][1] + dp[i-1][0]; } } int cal(int num) { int len = 0; int tmp = num; int bit[maxn]; while (num) { bit[++len] = num%10; num /= 10; } bit[len+1] = 0; int ans = 0; int flag = 0; for (int i = len; i >= 1; i--) { ans += dp[i-1][2]*bit[i]; if (flag) ans += dp[i-1][0]*bit[i]; if (!flag && bit[i] > 4) ans += dp[i-1][0]; if (!flag && bit[i+1] == 6 && bit[i] > 2) ans += dp[i][1]; if (!flag && bit[i] > 6) ans += dp[i-1][1]; if (bit[i] == 4 || (bit[i+1] == 6 && bit[i] == 2)) flag = 1; } if (flag) ans++; return tmp - ans; } int main() { init(); while (scanf("%d%d", &n, &m) != EOF && n+m) { printf("%d\n", cal(m)-cal(n-1)); } return 0; }
HDU - 2089 不要62 (暴力或数位DP),布布扣,bubuko.com
标签:des style blog os 数据 io 2014 for
原文地址:http://blog.csdn.net/u011345136/article/details/38140317