HDU 2015 School Race 1002 Dual Horsetail (thinking problem)

Source: Internet
Author: User
Tags binary to decimal

Dual Horsetail

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): Accepted submission (s): 189


Problem Description Nanaya ' s favorite Dual Horsetail Robot have run out of power!
As a best friend of Nanaya, CDFPYSW decide to does something for him.
CDFPYSW, the King of Alpaca, had some very powerful energy balls numbered by 1,2,3......N. He let Nanaya divide them into some sets to save the robot.
However, if there is three balls whose numbers is labeled X, Y and x&y in the same set, they would explode! Nanaya must divide these balls into some sets and avoid explosion.
Nanaya is a shy man, he don ' t want to divide these balls into too many sets. Please tell him the minimum number of sets.

Input The first line contains an integer T, indicating the number of cases.
The next T line, each line contains an integer n indicating the number of balls. (1 <= n <= 109)

Output for each test case in the input, print one line: ' Case #X: Y ', where X is the ' Test Case number ' (starting with 1) a nd Y represents the minimum number of sets.

Sample Input223

Sample outputcase #1:1Case #2:2algorithm and problem analysis: T group data, each group input an n, representing 1 to n data. Now we're going to put the n number in some set, and find the minimum number of sets. The conditions must be met: X, Y, and (x&y), and these three elements will not appear in a collection at the same time! first, you need to know the binary notation of some special numbers:decimal---------- binary1 00013 00117 011111110001 1111            ......The rule is: We start with the data from 1, when a new number appears, and the binary representation of the number is all 1 (ignoring the leading 0),This number must be placed in a newly added set, otherwise the number will not match the requirements of some of the preceding figures. the range of n (1 <= n <= 109), so it is only necessary to find out the number of all binary representations of all 1 digits. Note that you do not need to put 1 to n datanext to the binary conversion. We just need to make a loop. The binary representation of all 1 of these numbers is regular if you understand binary to decimalalgorithm will understand! for example: 1 = 2^0.3 = 2^0+2^1.7 = 2^2+2^1+2^0.Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <iostream> #include <string> #include <algorithm> #define PI acos ( -1.0) #define INF 1e9using namespace Std;int dd[100];int main () {    int t;    scanf ("%d", &t);    int n;    memset (dd, 0, sizeof (DD));    int ans;    int e=1;    Dd[e++]=1;    Ans=1;    int i=1;    while (ans < INF)    {        Ans=ans+pow (2, i);        i++;        Dd[e++]=ans;    }   printf ("-----%d\n", i);    int cnt=1;    while (t--)    {        scanf ("%d", &n);        int pos;        For (i=0, i<e; i++)        {            if (N>=dd[i] && n<dd[i+1])            {                pos=i; break;            }        }        printf ("Case #%d:%d\n", cnt++, POS);    }    return 0;}

HDU 2015 School Race 1002 Dual Horsetail (thinking problem)

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.