题目;http://acm.hdu.edu.cn/showproblem.php?pid=5491
题意就是,T组测试数据。然后L,S1,S2。L的二进制中有x个1,x满足 S1<=x<=S2
求满足S1<=x<=S2
直接暴力,但是有个地方要注意,当L在递增枚举的过程中,X小于S1的时候,将其二进制中S1-X个0位转化成1,不然会超时
#include<cstdio>
using namespace std;
typedef long long ll;
int main()
{
ll t,ans=,a[],n,s1,s2;
ll i,sum;
scanf("%I64d",&t);
while (t--)
{
scanf("%I64d %I64d %I64d",&n,&s1,&s2);
int flag=;
while ()
{
n++;
ll e=n;
i=,sum=;
while (e)
{
a[++i]=e%;
if (e%) sum++;
e/=;
}
if (sum<s1)
{
ll q=s1-sum,j,w=;
for (j=;j<=i;j++)
{
if (!a[j])
{
a[j]=;
w++;
}
if (w==q) break;
}
break;
}
if (sum<=s2&&sum>=s1)
{
flag=;
break;
}
}
printf("Case #%I64d: ",ans++);
if (flag==) printf("%I64d\n",n);
else
{
sum=;ll x=;
for (ll j=;j<=i;j++)
{
sum+=x*a[j];
x*=;
}
printf("%I64d\n",sum);
}
}
return ;
}