E - Round Numbers

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <string>
 6 
 7 using namespace std;
 8 const int maxn = 2*1e9+5;
 9 int dp[100][100];
10 int a[100];
11 
12 //lead是否有前导0
13 int dfs(int pos,int sta, int lead, bool limit){
14     if(pos == -1)
15         return sta >= 32;
16     if(!limit && !lead && dp[pos][sta] != -1)
17         return dp[pos][sta];
18     int up = limit?a[pos]:1;
19     int ans = 0;
20     for(int i = 0;i <= up;i++){
21         if(lead && i == 0)
22             ans += dfs(pos-1, sta, lead, limit && a[pos] == i);    //有前导0的就不统计在内
23         else{
24             ans += dfs(pos-1, sta + (i == 0?1:-1),lead && i == 0,limit && a[pos] == i);
25         }
26     }
27     //无限制,切无前导0
28     if(!limit && !lead)
29         dp[pos][sta] = ans;
30     return ans;
31 }
32 
33 int solve(int x){
34     int pos = 0;
35     while(x){
36         a[pos++] = x&1;
37         x >>= 1;
38     }
39     //32当作0使用
40     return dfs(pos-1, 32, true, true);
41 }
42 
43 int main(){
44     memset(dp, -1, sizeof(dp));
45     int a, b;
46     while(~scanf("%d%d", &a, &b)){
47         int ans = solve(b) - solve(a-1);
48         printf("%d\n", ans);
49     }
50     return 0;
51 }

 

posted @ 2018-05-30 20:58  ouyang_wsgwz  阅读(126)  评论(0编辑  收藏  举报