zoukankan      html  css  js  c++  java
  • hdu2098 不要62 ——数位DP入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089

    题目大意:

      含有4或者62的数字是不吉利数字,给一个区间,[m, n],求这个区间内的除了不吉利数字以外的数字的数目。

    思路:

      由于数据范围只有1~1000000,可以暴力,水题,但是为了练习一下数位DP,没有把它当水题做……

      看的是这个人的代码:http://blog.csdn.net/acm_cxlove/article/details/7819907#

      和hdu3555那道题目相似,但是多了一个条件,多了一个不含有4的条件,讨论一下。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cctype>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <algorithm>
    10 #define lson l, m, rt<<1
    11 #define rson m+1, r, rt<<1|1
    12 using namespace std;
    13 typedef long long int LL;
    14 const int MAXN =  0x3f3f3f3f;
    15 const int  MIN =  -0x3f3f3f3f;
    16 const double eps = 1e-9;
    17 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1},
    18   {1,1},{1,-1},{-1,-1}};
    19 LL dp[8][3]; int a[8], b[8]; bool flag;
    20 int solve(int k){
    21   int len = 0; LL temp = k;
    22   while (k){
    23     a[++len] = k % 10; k /= 10;
    24   }
    25   LL ans = 0; int last = 0; flag = false;
    26   a[len+1] = 0;
    27   for (int i = len; i >= 1; --i){
    28     ans += dp[i-1][2] * a[i];
    29     if (flag){ans += dp[i-1][0] * a[i];}
    30     //高位位填4,低位的不管
    31     if (!flag && a[i] > 4) {ans += dp[i-1][0];}
    32     // 这一位填6,比它第一位的填2
    33     if (!flag && a[i] > 6) {ans += dp[i-1][1];}
    34     // 这一位填4,但是比它高一位的要保证是6,因为上面没有考虑到上一位等于6的情况
    35     if (!flag && a[i] > 2 && a[i+1] == 6) {ans += dp[i][1];} // 这里很悲剧地写成了dp[i-1][1]我去……
    36     // 标记一下这个数字本身也是不符合条件的
    37     if (a[i] == 4 || (a[i]==2&&a[i+1]==6)) flag = true;
    38   }
    39   if (flag) ans++; //求的是从1~n的不符合条件的个数,如果端点本身不符合,要加1,就是加上判断数字本身的情况
    40   // 因为上面的每一种情况都没有考虑到这个数字本身是非幸运数字的情况,所以要加上
    41   return temp - ans;
    42 }
    43 int main(void){
    44 #ifndef ONLINE_JUDGE
    45   freopen("hdu2089.in", "r", stdin);
    46 #endif
    47   int n, m;
    48   memset(dp, 0, sizeof(dp));
    49   dp[0][0] = 1;
    50   for (int i = 1; i < 8; ++i){
    51     dp[i][0] = dp[i-1][0] * 9 - dp[i-1][1];
    52     dp[i][1] = dp[i-1][0];
    53     dp[i][2] = dp[i-1][2] * 10 + dp[i-1][1] + dp[i-1][0];
    54   }
    55   while (~scanf("%d%d", &m, &n)){
    56     if (n+m==0) break;
    57     LL ans1, ans2;
    58     ans1 = solve(m-1); // 这个区间范围要注意,要求 m ~ n 所以要把 1 ~ m - 1 和1 ~ n - 1 的求出来
    59     ans2 = solve(n);
    60     cout << (ans2 - ans1) << endl;
    61     //可以输出1到10的solve(i),这种基本的调试技巧要会,别有错误就去问别人……
    62   }
    63 
    64   return 0;
    65 }

    这种题目关键是要考虑清楚所有的情况,思维要严谨,思路要清晰。

  • 相关阅读:
    支付扣款 并发 异步
    Floyd-Warshall算法
    black arch
    ChromeDriver only supports characters in the BMP
    Getting console.log output with Selenium Python API bindings
    微信公众号文章批量采集系统的构建
    node npm Bower
    PyPy CPython C++ connects programs written in C and C++ with a variety of high-level programming languages
    timeout connect 10000 # default 10 second time out if a backend is not found
    timeout connect 10000 # default 10 second time out if a backend is not found
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/3021056.html
Copyright © 2011-2022 走看看