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; }
原文地址:http://blog.csdn.net/wty__/article/details/38025327