poj3252
题目
就是求指定范围内转换成二进制0的数目大于等于一的数目的数的个数。
思路
dp[pos][no][nz],pos表示第几位,no表示零的个数,nz表示1的个数,转移的时候我多加了一个first判断有没有前导零,分了情况讨论。
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
ll dp[
40][
40][
40];
int dis[
40];
ll dfs(
int len,
int first,
int no,
int nz,
int lim)
{
if(len<
0)
return first||nz>=no;
if(!first&&!lim&&dp[len][no][nz]!=-
1)
return dp[len][no][nz];
ll res=
0;
int u=lim?dis[len]:
1;
for(
int i=
0; i<=u; i++)
if(first)
{
if(i==
0)
res+=dfs(len-
1,
1,no,nz,lim&&i==u);
else
res+=dfs(len-
1,
0,no+
1,nz,lim&&i==u);
}
else
{
if(i==
0)
res+=dfs(len-
1,
0,no,nz+
1,lim&&i==u);
else
res+=dfs(len-
1,
0,no+
1,nz,lim&&i==u);
}
if(!lim&&!first)
return dp[len][no][nz]=res;
else return res;
}
ll solve(ll x)
{
int len=
0;
while(x)
{
dis[len++]=x%
2;
x/=
2;
}
return dfs(len-
1,
1,
0,
0,
1);
}
ll a,b;
int main()
{
scanf(
"%I64d %I64d",&a,&b);
memset(dp,-
1,
sizeof(dp));
printf(
"%I64d\n",solve(b)-solve(a-
1));
}
转载请注明原文地址: https://ju.6miu.com/read-1296539.html