poj3252 数位dp

这题不是用10进制储存的,要转化成2进制再计算

dp[i][j][k]   i是位数,j是1的个数,k是0的个数

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-7;
const int N=50+10,maxn=1000000+10,inf=0x3f3f3f;

int dp[N][N][N],digit[N];
int dfs(int len,int one,int zero,bool fi,bool fp)
{
    if(!len)
    {
        if(fi)return 1;
        return zero>=one;
    }
    if(!fp&&!fi&&dp[len][one][zero]!=-1)return dp[len][one][zero];
    int ans=0,fpmax=fp ? digit[len] : 1;
    for(int i=0;i<=fpmax;i++)
    {
        if(fi)
        {
            if(i==0)ans+=dfs(len-1,0,0,fi,fp&&i==fpmax);
            else ans+=dfs(len-1,one+1,zero,fi&0,fp&&i==fpmax);
        }
        else
        {
            if(i==0)ans+=dfs(len-1,one,zero+1,fi&0,fp&&i==fpmax);
            else ans+=dfs(len-1,one+1,zero,fi&0,fp&&i==fpmax);
        }
    }
    if(!fp&&!fi)dp[len][one][zero]=ans;
    return ans;
}
ll solve(ll x)
{
    int len=0;
    while(x)
    {
        digit[++len]=x&1;
        x/=2;
    }
    return dfs(len,0,0,1,1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a,b;
    memset(dp,-1,sizeof dp);
    cin>>a>>b;
    cout<<solve(b)-solve(a-1)<<endl;
    return 0;
}
/********************

********************/
写法1
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-7;
const int N=50+10,maxn=1000000+10,inf=0x3f3f3f;

int dp[N][N][N],digit[N];
int dfs(int len,int one,int zero,bool fi,bool fp)
{
    if(!len)
    {
        return !fi&&zero>=one;
    }
    if(!fp&&!fi&&dp[len][one][zero]!=-1)return dp[len][one][zero];
    int ans=0,fpmax=fp ? digit[len] : 1;
    for(int i=0;i<=fpmax;i++)
    {
        if(fi)
        {
            if(i==0)ans+=dfs(len-1,0,0,fi,fp&&i==fpmax);
            else ans+=dfs(len-1,one+1,zero,fi&0,fp&&i==fpmax);
        }
        else
        {
            if(i==0)ans+=dfs(len-1,one,zero+1,fi&0,fp&&i==fpmax);
            else ans+=dfs(len-1,one+1,zero,fi&0,fp&&i==fpmax);
        }
    }
    if(!fp&&!fi)dp[len][one][zero]=ans;
    return ans;
}
ll solve(ll x)
{
    int len=0;
    while(x)
    {
        digit[++len]=x&1;
        x/=2;
    }
    return dfs(len,0,0,1,1);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a,b;
    memset(dp,-1,sizeof dp);
    cin>>a>>b;
    cout<<solve(b)-solve(a-1)<<endl;
    return 0;
}
/********************

********************/
写法2

 

posted @ 2017-08-24 10:51  walfy  阅读(153)  评论(0编辑  收藏  举报