nyoj281 整数中的1(二) 数位DP

来源:互联网 发布:mac怎么卸载webstorm 编辑:程序博客网 时间:2024/06/15 19:04

    和整数中的1一毛一样。就是输入时改了一下罢了。

AC代码:

#include<cstdio>const int maxn = 35;int w[maxn], h[maxn];void deal(){h[0] = 0;w[0] = 1;w[1] = 2;h[1] = 1;for(int i = 2; i < 31; ++i) {w[i] = w[i - 1] * 2;h[i] = h[i - 1] + w[i - 1] + h[i - 1];}}int solve(int n){if(n == -1) return 0;int cnt = 0;int m = n;while(m > 0){if(m & 1) cnt++;m >>= 1;}int ans = cnt;for(int i = 1; n > 0; ++i, n >>= 1){if((n & 1) == 0) continue;cnt--;ans += cnt * w[i - 1] + h[i - 1];}return ans;}int main(){deal();int a, b;while(scanf("%d%d", &a, &b) == 2 && (a || b)){printf("%d\n", solve(b) - solve(a - 1));}return 0;}

如有不当之处欢迎指出!

0 0
原创粉丝点击