码迷,mamicode.com
首页 > 其他好文 > 详细

Bomb

时间:2014-07-21 22:48:18      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:acm   dp   

题目链接

  • 题意:
    求1-x中出现连续的49的数有多少个(注意要用long long)
  • 分析:
    计算不包括连续的49或者计算包括连续的49。两种方法都差不多,简单的数位DP
LL f[100][2], bits[100];

LL dfs(int pos, int s, bool lmt)
{
    if (pos == -1) return 1;
    if (!lmt && ~f[pos][s]) return f[pos][s];
    int u = lmt ? bits[pos] : 9;
    LL ret = 0;
    for (int i = 0; i <= u; i++)
    {
        if (s == 1 && i == 9) continue;
        int nxt = 0;
        if (i == 4) nxt = 1;
        ret += dfs(pos - 1, nxt, lmt && i == u);
    }
    return lmt ? ret : f[pos][s] = ret;
}

LL calc(LL n)
{
    CLR(f, -1);
    LL len = 0;
    while (n)
    {
        bits[len++] = n % 10;
        n /= 10;
    }
    return dfs(len - 1, 0, true);
}

int main()
{
    //freopen("0.txt", "r", stdin);
    int T;
    RI(T);
    FE(kase, 1, T)
    {
        LL n;
        cin >> n;
        cout << 1 + n - calc(n) << endl;
    }
    return 0;
}


Bomb

标签:acm   dp   

原文地址:http://blog.csdn.net/wty__/article/details/38023729

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