Round Numbers

题目链接

  • 题意:
    Round Numbers:假设n换成二进制后,0的个数大于等于1的个数
    求x-y范围内Round Numbers有几个
  • 分析:
    记录一下num(0) - num(1)的值,注意存在负值,所以dp数组记录时候须要注意
LL f[100][100], bits[100];
const int ADD = 50;
LL dfs(int pos, int diff, bool lmt, bool first)
{
    if (pos == -1) return diff >= 0;
    if (!lmt && !first && ~f[pos][diff + ADD]) return f[pos][diff + ADD];
    LL u = lmt ? bits[pos] : 1, ret = 0;
    for (int i = 0; i <= u; i++)
    {
        int nxt = diff;
        if (i) nxt--;
        else if (!first && !i) nxt++;
        ret += dfs(pos - 1, nxt, lmt && i == u, first && !i);
    }
    return lmt || first ? ret : f[pos][diff + ADD] = ret;
}

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

int main()
{
    //freopen("0.txt", "r", stdin);
    LL a, b;
    while (cin >> a >> b)
        cout << calc(b) - calc(a - 1) << endl;
    return 0;
}


你可能感兴趣的:(number)