poj3252的数位dp解法。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
//#include <unordered_set>
#define mkp make_pair
#define err cout<<"here"<<endl
using namespace std;
const double EPS=1e-12;
typedef long long lon;
typedef unsigned long long ull;
typedef map<ull,int>::iterator IT;
const lon SZ=37,SSZ=10,APB=128,m2=200003,mod=9901,one=97;
const lon INF=0x7FFFFFFF;
int n,dp[SZ][SZ][SZ],arr[SZ];
struct nd{
    int to,wt;
    nd(int a=0,int b=0):to(a),wt(b){}
};
char ch[SZ];

int dfs(int pos,int znum,int onum,int lim,int lead)
{
    if(!lim&&!lead&&dp[pos][znum][onum]!=-1)return dp[pos][znum][onum];
    if(pos==n+1)
    {
        return znum>=onum||lead;
    }
    int up=lim?arr[pos]:1,res=0;
    for(int i=0;i<=up;++i)
    {
        int nlead=lead&&i==0;
        res+=dfs(pos+1,nlead?0:znum+(i==0),nlead?0:onum+(i==1),lim&&i==arr[pos],nlead);
    }
    if(!lim&&!lead)dp[pos][znum][onum]=res;
    return res;
}

int calc(int x)
{
    if(x==0)return 1;
    else
    {
        int sz=0;
        memset(dp,-1,sizeof(dp));
        for(;x;x/=2)
        {
            arr[++sz]=x%2;
        }
        reverse(arr+1,arr+1+sz);
        n=sz;
        dfs(1,0,0,1,1);
    }
}

void init()
{
    int a,b;
    cin>>a>>b;
    int res=calc(b)-calc(a-1);
    cout<<res<<endl;
}

void work()
{
    
}

void release()
{
    
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //cout<<casenum<<endl;
    //for(lon time=1;time<=casenum;++time)
    //for(lon time=1;cin>>n;++time)
    {
        init();
        work();
        release();
    }
    return 0;
}

 

posted @ 2019-05-08 11:32  degvx  阅读(164)  评论(0)    收藏  举报