2015年ACM-ICPC亚洲区域赛合肥站网络预选赛H题——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 XHY number if S1≤L≤S2.
With a given D, we would like to find the next XHY number Y, which is JUST larger than D. In other words, is the smallest XHY 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≤100000).
Each test case consists of three integers D, S1, and S2, as described above. It is guaranteed that 0≤D<2^28 and D is a XHY number.

输出要求

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 XHY number.

测试数据示例

输入

3
11 2 4
22 3 3
15 2 5

输出

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

大水题,开始以为会TLE,结果暴力搞一下位运算就过了,主要是题目有难懂。

题目意思大概如下:

L代表整数D的二进制位为1的个数。譬如D=3,L=2。

假如s1<=L<=s2,则D就是XHY数。现在就是要你找出大于D的最小XHY数。

题目明白了之后就是很裸的按位与(&)操作。

上代码了.

C code

#include <stdio.h>

int slove(int d,int s1,int s2){
    int next;
    long end =  1 << 28;
    for(int i=d+1;i<=end;i++){
        int bitCount = 0;
        for(int j=0;j<31;j++){
            if( (i & (1<<j)) > 0) bitCount++;
        }
        if(bitCount>=s1 && bitCount <= s2){
           next=i;
           break;
        }
    }
    return next;
}
int main(){
    int n;
    int d;
    int s1;
    int s2;
    int c = 1;
    while(scanf("%d",&n)==1){
        for(int i=0;i<n;i++){
            scanf("%d",&d);
            scanf("%d",&s1);
            scanf("%d",&s2);
            int ret = slove(d,s1,s2);
            printf("Case #%d: %d\n",c++,ret);
        }
    }
}
时间: 2024-07-30 15:21:27

2015年ACM-ICPC亚洲区域赛合肥站网络预选赛H题——The Next (位运算)的相关文章

2014ACM/ICPC亚洲区域赛牡丹江站D和K题

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre

2015 ACM / ICPC 亚洲区域赛总结(长春站&amp;&amp;北京站)

队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍阵容:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之前只学了大部分图论内容,以及一些数据结构.动态规划等等,但是还不能熟练运用... ... 先从长春站说起吧... ... 长春站是我加入ACM以来参加的第一场ICPC,因此无比的激动!从杭州出发,乘了整整24小时的火车,终于到达了长春:第一次到大东北,沿途的风景与南方有很大差异,体会到了东北的寒

2014ACM/ICPC亚洲区域赛牡丹江站总结

我在集训队里面也就一般水平,这次学校史无前例的拿到了8个名额,由于大三的只有两个队伍,所以我们13及能分到名额,由于13及人数很多,组长就按照谁在oj上面a的题多就让谁去,我和tyh,sxk,doubleq幸运的在大二就有机会参加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛其实就是去张张见识,增加大赛经验(外加公费旅游2333),可是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子.第一天热身赛,double迅速切下水题,我一直再弄

2014ACM/ICPC亚洲区域赛牡丹江站汇总

球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doubleq运的在大二就有机会參加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛事实上就是去张张见识,添加大赛经验(外加公费旅游2333),但是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子. 第一天热身赛,double迅速切下水题.我一直再

2015亚洲区域赛长春赛区网络预选赛

第一次打网络赛,第一场,总体来说还可以吧,但是我们队三个人状态都并不太好,主要就是 WA 的比较多吧,开场看最后一题是我的习惯了,虽然貌似那题到打了一半可能才有队伍做出来了,我看了感觉像前几天训练赛的时候做的一道题.刻盘开场看 06 ,凯神开场 01.接着两分钟学长发现 07 水题我就跟着看了发题意,求区间最大值,静态.然后数据范围也很小,就直接开敲暴力,四分钟的时候过的,大概 20 名左右吧,那就是我们的最高名次了2333……接着我准备继续研究下 13 ,刻盘告诉我 06 是关于循环的子串的问

Digit sum (第 44 届 ACM/ICPC 亚洲区域赛(上海)网络赛)进制预处理水题

131072K A digit sum S_b(n)Sb?(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 + 3 = 8   S10?(233)=2+3+3=8, S_{2}(8)=1 + 0 + 0 = 1S2?(8)=1+0+0=1, S_{2}(7)=1 + 1 + 1 = 3S2?(7)=1+1+1=3. Given NN and bb, you need to calculate \sum_{n

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar University. He is clever and diligent. However, he is not good at math, especially in Mathematical Analysis. After a mid-term exam, Bob was anxious about his

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy. Entropy is t

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expres