hdu 5491 The Next(暴力枚举)
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 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.
The first line of input contains a number T indicating the number of test cases (T≤).
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed that ≤D< and D is a WYH number.
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from . Y is the next WYH number.
Case #:
Case #:
Case #:
不想写,找了个别人的.
思路:
考虑通过比较当前的1的数目来进行最小的数的修正。
先将D加1,然后计算出D的1的数目tot,这时候比较tot和s1,s2的大小,这时候有两种情况:
1、tot<s1,这时我需要增加1的数目,因为要最小,所以我从右开始找到一个0(位置假设为i),将D加上2^i。
2、tot>s2,这时我需要减少1的数目,所以我从右开始找到一个1,将D加上2^i。
如此循环上述过程,直到符合tot符合s1,s2的条件。
上面操作的原因是:我加上2^i,就是将那一位由1变为0或者由0变为1,而我是从右边开始寻找的,可以保证每次所做的改变都是最小的。同时我的D是一直增加的,所以当条件满足时,就是那个最小的。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
int dirx[]={,,-,};
int diry[]={-,,,};
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100
#define inf 1e12
ll d,s1,s2;
ll num[N];
ll k;
ll count_(ll dd){
memset(num,,sizeof(num));
ll cnt=;
k=;
while(dd){
if(dd&) cnt++;
num[k++]=(dd&);
dd>>=;
}
num[k++]=; return cnt;
}
int main()
{
ll ac=;
ll t;
scanf("%I64d",&t);
while(t--){
scanf("%I64d%I64d%I64d",&d,&s1,&s2); ll dd=d+; while(){
ll tmp=count_(dd); if(tmp<s1){
for(ll i=;i<k;i++){
if(num[i]==){
dd+=(<<i);
break;
}
}
}else if(tmp>s2){
for(ll i=;i<k;i++){
if(num[i]==){
dd+=(<<i);
break;
}
}
}else{
break;
}
}
printf("Case #%I64d: ",++ac);
printf("%I64d\n",dd); }
return ;
}
hdu 5491 The Next(暴力枚举)的更多相关文章
- HDU 5778 abs (暴力枚举)
abs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem De ...
- HDU 1015.Safecracker【暴力枚举】【8月17】
Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is lo ...
- HDU - 5128The E-pang Palace+暴力枚举,计算几何
第一次写计算几何,ac,感动. 不过感觉自己的代码还可以美化一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意: 在一个坐标系中,有n个 ...
- HDU 5339 Untitled (暴力枚举)
题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...
- hdu 1238 Substrings(kmp+暴力枚举)
Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...
- HDU 6351暴力枚举 6354计算几何
Beautiful Now Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
- hdu 1172 猜数字(暴力枚举)
题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...
- BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定
题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. #include ...
- hdu 4445 Crazy Tank (暴力枚举)
Crazy Tank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- BackgroundWorker用法
BackgroundWorker主要用来提供后台运算服务(防止用户前台无响应等待),并提供服务进度的类: 代码如下: BackgroundWorker bgw = new BackgroundWork ...
- Uninstall or Disable Java on a Mac
You can run Java apps in two ways. The first is to run Java applets inside your Web browser with a p ...
- XCode中在提示窗体中对已弃用的API接口画上红线
当我们在XCode中写程序时会不断的出现相关API提示窗体,那敲起来是一个爽啊. 有时候会看到一些API已经弃用了被画上红色的横线.说明该接口已经被弃用,仍保留,但不建议使用,对弃用API实现画横线事 ...
- C#调用WebService实例和开发
一.基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是 ...
- JS获取地址参数
今天碰到获取地址参数的问题,所以总结了一下. 第一种情况:获取地址栏参数 function getUrlParam(name){ var reg = new RegExp("(^|& ...
- JavaScript-------寄生组合式继承
组合继承在前面有说过,也是JavaScript中最常用的一个继承模式:不过,它也有自己的不足.组合继承最大的问题就是无论什么情况,都会调用两次构造函数: 那我们来回顾下组合式继承基本模式: funct ...
- Winform开发之窗体传值
Winform的窗体之间的数据传递是开发的必备技术,下面介绍几种典型的传值方法 1.构造函数传值,但这种方法是单向的(推荐) 上代码,先传值 private void button2_Click(ob ...
- .NET的 DataTable中某列求和
public DataTable ReportDetail { get; set; }//定义datatable属性 this.txtTotalPiece.Text = ReportDetail.Co ...
- OpenCV——肤色检测
一.RGB color space 检测代码如下: void SkinRGB(IplImage* src,IplImage* dst) { //RGB颜色空间 //均匀照明:R>95,G> ...
- (转) Name visibility
Scopes Named entities, such as variables, functions, and compound types need to be declared before b ...