Usaco 2006Nov Round Numbers

题意:定义Round Number为二进制表示下0的个数大于等于1的个数的数。求[l,r]中有多少圆环数

我们把二进制位用一颗01二叉树表示,如下:

我们依据二进制位来遍历这颗线段树,如果当前高度对应的位为1,则遍历左右两颗子树,否则遍历左子树。dfs的过程中我们可以用记忆化来减少枚举状态,同时注意到那些最靠左的子树在遍历的过程中是可以重计数的,这样可以减少需要枚举的状态

尽管据说此题是裸的数位dp,可是我还是不会呀QAQ

#include<bits/stdc++.h>
using namespace std;
int f[35][35][35],s[35];
int dfs(int all,int c1,int c2,int f1,int f2){
    if(!all){
        if(c1>=c2)return 1;
        return 0;
    }
    if(!f1&&!f2&&f[all][c1][c2]!=-1)return f[all][c1][c2];
    int tot=0,last=f2?s[all]:1;
    for(int i=0;i<=last;i++){
        if(f1){
            if(!i)tot+=dfs(all-1,0,0,1,f2&&(i==s[all])); 
            else tot+=dfs(all-1,c1,c2+1,0,f2&&(i==s[all]));
        }
        else{
            if(!i)tot+=dfs(all-1,c1+1,c2,0,f2&&(i==s[all]));
            else tot+=dfs(all-1,c1,c2+1,0,f2&&(i==s[all]));
        }
    }
    if(!f1&&!f2)f[all][c1][c2]=tot;
    return tot;
}
int sum(int x){
    int tot=0;
    memset(s,0,sizeof(s));
    for(;x;x>>=1)s[++tot]=x&1;
    return dfs(tot,0,0,1,1);
}
int main(){
    memset(f,-1,sizeof(f));
    int l,r;
    scanf("%d%d",&l,&r);
    printf("%d
",sum(r)-sum(l-1));
    return 0;
}
原文地址:https://www.cnblogs.com/NINGLONG/p/7689123.html