poj 3252 Round Numbers 数位dp
题意:
Round Numbers:把一个数分解成二进制,0的个数>=1的个数的数
分析:
显然数位dp可解,这题用排列组合也能做。不过还是数位dp简单点。
排列组合可以参考Kuangbin巨巨的题解:http://www.cnblogs.com/kuangbin/archive/2012/08/22/2651730.html
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=50;
int f[N][N][N];
int bit[50];
int dfs(int pos,int num0,int num1,bool lim,bool first)
{
if(pos==0)return num0>=num1;
if(!lim&&f[pos][num0][num1]!=-1)return f[pos][num0][num1];
int ans=0;
int m=lim?bit[pos]:1;
for(int i=0;i<=m;i++){
if(i==0){
if(!first)ans+=dfs(pos-1,0,0,0,0);
else ans+=dfs(pos-1,num0+1,num1,lim&&i==m,first);
}
if(i==1){
if(!first)ans+=dfs(pos-1,0,1,lim,1);
else ans+=dfs(pos-1,num0,num1+1,lim,1);
}
}
if(!lim)f[pos][num0][num1]=ans;
return ans;
}
int solve(int x)
{
int cnt=0;
while(x){
bit[++cnt]=x&1;
x>>=1;
}
return dfs(cnt,0,0,1,0);
}
int main()
{
//freopen("f.txt","r",stdin);
memset(f,-1,sizeof(f));
int s,t;
scanf("%d%d",&s,&t);
printf("%d\n",solve(t)-solve(s-1));
return 0;
}