HDU 5491 The Next(表示我的方法比较暴力,但需要考虑的东西比较少)——2015 ACM/ICPC Asia Regional Hefei Online

来源:互联网 发布:数控转塔冲床编程视频 编辑:程序博客网 时间:2024/05/16 05:19

The Next

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
Let L denote the number of 1s in integer D’s binary representation. Given two integers S1 and S2, we call D a WYH number if S1LS2.
With a given D, we would like to find the next WYH number Y, which is JUST larger than D. In other words, Y is the smallest WYH number among the numbers larger than D. Please write a program to solve this problem.
 

Input
The first line of input contains a number T indicating the number of test cases (T300000).
Each test case consists of three integers DS1, and S2, as described above. It is guaranteed that 0D<231 and D is a WYH number.
 

Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the next WYH number.
 

Sample Input
311 2 422 3 315 2 5
 

Sample Output
Case #1: 12Case #2: 25Case #3: 17
 

Source
2015 ACM/ICPC Asia Regional Hefei Online
 
/*********************************************************************/

题意:给你一个数D,它的二进制表示中'1'的个数为L,满足S1≤L≤S2,问比D大的同样满足'1'的个数在S1~S2之间的最小的数是多少

解题思路:毕竟比较菜鸟,这个题目虽然水,但刚开始并没有想到什么好的方法,也是不断举举例子,才发现了一种做法

对于任意给定的一个D,比如11,它的二进制表示为(1011)2,我们暂且在最高位前添一个0(因为对于1111这样的二进制数,要比它大,就得在最高位前添一位),此时,D为(01011)2,我们需要做的事情很简单:

因为要比原来的数大,那至少需要把一个'0'改成'1',所以我们只需要遍历所有的'0',将该'0'后面的数清空后,从低位开始向高位添'1',直到'1'的个数满足在S1~S2之内,最后取最小值即可

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 40;const __int64 inf = 10000000000000;const int mod = 2009;int s[N],a[N];int main(){    int t,i,j,d,s1,s2,k=1,p,c,cc,tt;    __int64 ans,Min;    scanf("%d",&t);    while(t--)    {        c=p=0;Min=inf;        scanf("%d%d%d",&d,&s1,&s2);        while(d)        {            if(d%2)                c++;            s[p++]=d%2;            d/=2;        }        s[p++]=0;        for(tt=i=0;i<p;i++)        {            if(!s[i])            {                for(cc=j=0;j<i&&c-tt+1+cc<s1;j++)                    a[j]=1,cc++;                for(;j<i;j++)                    a[j]=0;                //printf("*%d*\n",cc+1+c-tt);                if(cc+1+c-tt<=s2)                    a[i]=1;                else                {                    a[i]=0;                    continue;                }                /*for(j=p-1;j>=0;j--)                    printf("%d ",s[j]);                puts("##");                for(j=i;j>=0;j--)                    printf("%d ",a[j]);                puts("@@");*/                for(ans=0,j=p-1;j>i;j--)                    ans=ans*2+s[j];                for(;j>=0;j--)                    ans=ans*2+a[j];                //printf("%I64d***\n",ans);                Min=min(ans,Min);            }            else                tt++;        }        printf("Case #%d: %I64d\n",k++,Min);    }    return 0;}
菜鸟成长记
0 0