hdu5491 The Next(合肥网赛)

来源:互联网 发布:装备增幅数据 编辑:程序博客网 时间:2024/06/10 04:16

The Next

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21 Accepted Submission(s): 10


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 D,S1, 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 

题意:L为D的二进制数中1的个数,然后求s1<=L<=s2的下一个D。
分析:把D的二进制数用数组存起来,然后记录末尾0(ret)和1(cnt)的个数,然后一位一位的找,比较水,只能想到这么暴力的方法,详解见代码。

#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;const int MOD = 1000000007;#define ll long long#define CL(a) memset(a,0,sizeof(a))ll ans;int d,s1,s2;int a[33];int main (){    int T;    scanf ("%d",&T);    for (int ii=1; ii<=T; ii++)    {        scanf ("%d%d%d",&d,&s1,&s2);        ans=d;        printf ("Case #%d: ",ii);        int k=1,sum=0;        CL(a);        while (d)        {            a[k++]=d&1;            if (d&1) sum++;//总共有多少个1            d=d>>1;        }        bool flag=false;        int t,cnt=0,ret=0;        for (int i=1; i<=k; i++)        {            if (!flag&&a[i]==0) ret++;//末尾的0数            if (a[i]==1) {flag=true; cnt++;}//末尾的1数            if (flag&&a[i]==0) {t=i; break;}        }        //cout<<ret<<" "<<cnt<<" "<<t<<endl;        if (!ret)//末尾没0        {            if (sum-cnt>=s1-1) ans+=1;//如果总数减去末尾1的数量任在[s1-1, s2]内,直接加1             else                       //因为进位会多出一个1,所以是s1-1            {                int p=s1-(sum-cnt);//否则一位一位的补1                if (p==1) ans=d+1;                else                {                    ans=ans+1;                    p--;                    while (p--)                    {                        ans=ans+(1<<p);                    }                }            }        }        else//如果末尾有0        {            if (sum<s2)//总1数小于s2            {                ans=ans+1;            }            else//总1数等于s2            {                int g=ret;                while (g--)//把末尾的1变成1                    ans=ans+(1<<g);                ans+=1;                int p=sum-cnt+1;                if (p<s1)//如果1的个数还小于s1,那就又变成的末尾没1的情况了                {                    int h=s1-p;                    while (h--)                    {                        ans=ans+(1<<h);                    }                }            }        }        printf ("%I64d\n",ans);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 6年免检车辆逾期未年检怎么办 行驶证检验有效期过期2年怎么办 骑摩托车忘带驾驶证和行驶证怎么办 驾驶证逾期未审验怎么办有什么后果 车祸至人腿部骨折沒钱偿还的怎么办 我有c4驾驶证想办c3怎么办 驾驶证和行驶证被交警扣了怎么办 行驶证被交警弄丢了怎么办 没带行驶证让警察发现怎么办 首尔转机换票换乘怎么办过境签证 驾驶证约考帐号与登密码丢失怎么办 考驾照的时候预约密码忘了怎么办 考驾照的预约密码忘了怎么办 摩托车不能挂档不能摘挡了怎么办 身份证丢失了派出所不给挂失怎么办 快递员在中午还送货夏天怎么办啊 驾照一个记分周期扣满12分怎么办 佛山南海车管所怎么办替人消分流程 福州快处中心几流程要怎么办 被对方追尾了对方只有交强险怎么办 摩托车行驶证年检过期一年半怎么办 户口迁到杭州了身份证掉了怎么办 户口已迁至外面要补办结婚证怎么办 汽车年检时间过了1个月怎么办 户口在老家在北京工作怎么办招工 报考驾照时手机号留错了怎么办 合肥驾照换证体检没过怎么办 驾驶证记分周期到了违章未消怎么办 驾照被扣科目一过期没考怎么办 驾驶证暂扣过了换证日期怎么办? 考驾照科目二身份证丢了怎么办 驾照科目一考试身份证丢了怎么办 换驾驶证名下有车辆脱审怎么办 交警把驾驶证和行车证扣了怎么办 连续两天驾照都是扣12分怎么办? 交警开的电动车罚单丢了怎么办 驾驶证b证体检报告拖期怎么办 在中国把美国护照弄丢了怎么办 拿了驾照两年了不敢上高速怎么办 我要移民过香港大陆的驾驶证怎么办 交警开的扣行驶证的单不见了怎么办