题目;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,不然会超时
1 #include<cstdio> 2 using namespace std; 3 typedef long long ll; 4 int main() 5 { 6 ll t,ans=1,a[31],n,s1,s2; 7 ll i,sum; 8 scanf("%I64d",&t); 9 while (t--) 10 { 11 scanf("%I64d %I64d %I64d",&n,&s1,&s2); 12 int flag=0; 13 while (1) 14 { 15 n++; 16 ll e=n; 17 i=0,sum=0; 18 while (e) 19 { 20 a[++i]=e%2; 21 if (e%2) sum++; 22 e/=2; 23 } 24 if (sum<s1) 25 { 26 ll q=s1-sum,j,w=0; 27 for (j=1;j<=i;j++) 28 { 29 if (!a[j]) 30 { 31 a[j]=1; 32 w++; 33 } 34 if (w==q) break; 35 } 36 break; 37 } 38 if (sum<=s2&&sum>=s1) 39 { 40 flag=1; 41 break; 42 } 43 } 44 printf("Case #%I64d: ",ans++); 45 if (flag==1) printf("%I64d\n",n); 46 else 47 { 48 sum=0;ll x=1; 49 for (ll j=1;j<=i;j++) 50 { 51 sum+=x*a[j]; 52 x*=2; 53 } 54 printf("%I64d\n",sum); 55 } 56 } 57 return 0; 58 }