码迷,mamicode.com
首页 > Windows程序 > 详细

windy数

时间:2017-05-13 13:36:13      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:==   ram   line   cti   article   width   play   stc   数位dp   

题目链接

  • 题意:
    windy数:不含前导零且相邻两个数字之差至少为2的正整数。输入x、y,求[x, y]内的windy数的个数
  • 分析:
    简单的数位DP,注意前导零的影响
int f[20][10], bits[20];

int dfs(int pos, int pre, bool lmt, bool first)
{
    if (pos == -1) return 1;
    if (!lmt && !first && ~f[pos][pre]) return f[pos][pre];
    int u = lmt ?

bits[pos] : 9, ret = 0; for (int i = 0; i <= u; i++) { if (first || (!first && abs(pre - i) >= 2)) ret += dfs(pos - 1, i, lmt && i == u, first && !i); } return lmt || first ? ret : f[pos][pre] = ret; } int calc(int n) { CLR(f, -1); int len = 0; while (n) { bits[len++] = n % 10; n /= 10; } return dfs(len - 1, 0, true, true); } int main() { //freopen("0.txt", "r", stdin); int a, b; while (~RII(a, b)) { cout << calc(b) - calc(a - 1) << endl; } return 0; }



windy数

标签:==   ram   line   cti   article   width   play   stc   数位dp   

原文地址:http://www.cnblogs.com/yfceshi/p/6848535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!