HDU 5491 The Next(技巧性模拟)
来源:互联网 发布:centos忘记密码 编辑:程序博客网 时间:2024/05/14 22:16
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 S1≤L≤S2 .
With a givenD , 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.
With a given
Each test case consists of three integers
311 2 422 3 315 2 5
Case #1: 12Case #2: 25Case #3: 17
题解:
题意:
给你一个数x,和两个数字s1,s2,让你找出大于x的满足二进制中1的个数在s1和s2之间的数的最小值
思路:
转化成二进制以后,从右往左扫,如果1的个数小于了s1,将扫到的0变成1,如果个数大于s2,将扫到的1进位,循环到符合条件为止
代码:
#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>#include<algorithm>#define ll long longusing namespace std;ll a[40];int cmp(ll x,ll y){ return x>y;}int main(){ int test,i,j,n,m,q,s1,s2,len,num,tag; ll d,s,x,t; scanf("%d",&test); for(q=1;q<=test;q++) { scanf("%lld%d%d",&x,&s1,&s2); memset(a,0,sizeof(a)); len=0; num=0; x++; t=x; while(x>0) { a[len]=x%2; x/=2; len++; if(a[len-1]==1) num++; } s=0,d=1; while(num<s1||num>s2) { while(num<s1) { tag=0; for(i=0;i<len;i++) { if(a[i]==0) { tag=1; a[i]=1; num++; if(num>=s1) break; } } if(!tag) { a[len]=1; num++; len++; } } while(num>s2) { for(i=0;i<len;i++) { if(a[i]) { a[i]=0; num--; j=i+1; if(j==len) { a[j]=1; len++; num++; break; } while(j<len&&a[j]) { a[j]=0; num--; j++; } if(j==len) { a[j]=1; num++; len++; break; } else { a[j]=1; num++; } if(num<=s2) break; } } } } for(i=0;i<len;i++) { s=s+d*a[i]; d*=2; } printf("Case #%d: %lld\n",q,s); } return 0;}
阅读全文
0 0
- HDU 5491 The Next(技巧性模拟)
- HDU 4393 Throw nails(技巧性模拟)
- HDU 3717 Rescue(二分+技巧性模拟)【详解】
- HDU 5491 The Next
- hdu 5491 The Next
- HDU 5491 The Next
- HDU 5491 The Next
- HDU 5491The Next
- HDU 5491 The Next【】
- hdu 5491 The Next(位操作)
- HDU 5491 The Next(构造数)
- HDU 5491 The Next(构造)
- hdu 5491 The Next(数学模拟)
- 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)
- HDU 4288 Coder (技巧性暴力模拟+二分||线段树+离线操作)
- HDU 5491 The Next (二进制) 2015合肥网络赛
- hdu 5491 The Next(位运算+贪心)
- [思路题] hdu 5491 The Next
- 设计模式之外观模式
- 基于MATLAB GUI的串口通信
- camera标定
- 零基础学小程序008----小程序列表实现+本地json数据解析渲染到小程序列表
- android.content.ActivityNotFoundException: No Activity found to handle Intent {
- HDU 5491 The Next(技巧性模拟)
- 在这里写上noip后要填的坑吧
- 光棍节的快乐
- 【2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛】 J Our Journey of Dalian Ends 【拆点费用流】
- 拓展-response响应头设置总结
- python数据分析复盘——数据分析相关库之Pandas
- 2017乌鲁木齐网络赛 skiing(spfa/bellman最长路模板题)
- hdu6201(无向图最长路)
- 使用thymeleaf在页面直接获取当前时间并格式化输出