HDU 5491 The Next structure (ACM/ICPC Asia regional Hefei Online)

Source: Internet
Author: User

"Link" click here~~

"Test Instructions":

Give you a D ( 0≤D<2 ) To ensure that the number of 1 in the binary of D is between S1 and S2

Then let you ask for a minimum number so that the binary number of this number is greater than or equal to S1, less than or equal to S2, and greater than D

"Thinking" although the game is a water problem, but began to see the time and did not think of good ideas, the last is to learn the younger brother forcibly over, today suddenly thought can use lowbit to beg

The first thing to see is the range of D is very large, then how to construct it?

Here is an example: for example, 11, binary representation (1011), take 11+1=12, binary representation (1100) s1=2, then we have to do is to consider the scope of the construction number, if the current number of S1 is less than the value of the left interval, we have to do is from the last 0 where the occurrence of 1, That is, from right to left, we found that 12 to take a bitwise inverse to get (0011), take a lowbit to get from right to left the first 1 position is 1, then we put 12+1=13 (1101), equivalent to 12 decimal plus one, also equals 12 of the binary added a 1, This is the structure is less than the left interval, then if greater than the left interval, but also meet the conditions, that is, take the minimum value greater than D, it is not difficult to find, just add lowbit (d) can

Code:

/* * PROBLEM:HDU no.5491* Running time:499ms * complier:g++ * Author:javaherongwei * Create time:8:47 2015/9/28 star    Period One */#include <bits/stdc++.h>using namespace Std;typedef long long ll;inline LL read () {int c=0,f=1;    Char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {c=c*10+ch-' 0 '; Ch=getchar ();} return c*f;}    ll Bitwei (ll x) {ll s=0;        while (x) {if (x%2) s++;    x/=2; } return s;}    ll Bitwei2 (ll x) {x = (x&0x55555555) + ((x>>1) & 0x55555555);    x = (x&0x33333333) + ((x>>2) & 0x33333333);    x = (x&0x0f0f0f0f) + ((x>>4) & 0x0f0f0f0f);    x = (X&AMP;0X00FF00FF) + ((x>>8) & 0x00ff00ff);    x = (X&AMP;0X0000FFFF) + ((x>>16) & 0x0000ffff); return x;} ll Lowbit (ll x) {return x& (-X);}    int main () {int t,tot=1;t=read ();        while (t--) {LL d,s1,s2;        D=read (); S1=read (); S2=read ();d + +; LL Len=bitWei2 (d); while (!) (            LEN&GT;=S1&AMP;&AMP;LEN&LT;=S2) {if (LEN&LT;S1) d+=lowbit (~d);            else D+=lowbit (d);        Len=bitwei2 (d);        } printf ("Case #%d:", tot++);    printf ("%lld\n", D); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5491 The Next structure (ACM/ICPC Asia regional Hefei Online)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.