POJ 24,362 Binary enumeration + bitwise operation

Source: Internet
Author: User

Test instructions: Given the type of disease of n cattle, there are a total of M-type diseases, the number of cattle to find the most K-type disease;

Idea: Binary enumeration (diseased place is 1, otherwise 0, such as got 2 12 kinds of disease, the Code is 011 (decimal is 3)), first of all the number of 1 is equal to the binary number of K, and then with all the bull's code to compare, meet the +1, find out and the largest; is to convert 2 into trouble, using bit Well realized, but the bit operation is not very clear meaning, understand the supplementary;

Knowledge Points:

    1. 3 & 2 = 2, same as 1, different 0, 011 & 010 = 010; (How to use this feature is not clear);
    2. T |= (1 << (b-1)), which means T equals B converted to binary, then converted to decimal, or, the same is 0, the difference is 1, here is equivalent to the binary sum.
1#include <iostream>2#include <cstdlib>3#include <cstring>4#include <cstdio>5 using namespacestd;6 #defineMAXN 10057 intN, D, K;8 intCOW[MAXN];9 Ten intOkinta) One { A     intRET =0; -      while(a) -     { theRET + = (A &1); -A >>=1; -     } -     returnret = = k;///if a is converted to a binary with K 1, return K, otherwise return 0 + } -  + intMain () A { atscanf"%d%d%d", &n, &d, &k); -      for(inti =0; I < n; i++) -     { -         intA, B; -scanf"%d", &a); -          for(intj =0; J < A; J + +) in         { -scanf"%d", &b); toCow[i] |= (1<< (b-1)); +cout<<cow[i]<<Endl; -             ///Cow[i] Represents a 2-binary representation of a cow's disease and then converts to a decimal the             ///if 1 2, that is 010 (cow[i] = 2), the second position is 1 *             ///if 2 1 2 is 011 (cow[i] = 3), the 12th position is 1 $         }Panax Notoginseng     } -     ints = (1<< k)-1; the     intE = s << (d-k);///namely s* (2^ (d-k)) +     intAns =0; A      for(inti = s; I <= e; i++) the     { +         if(OK (i))///if the cow has K-type disease, enter the next level -         { $             inttemp =0; $              for(intj =0; J < N; J + +)///enumerate each cow, match the plus 1 -             { -                 ///don't understand if the                 if((I & cow[j]) = = Cow[j])///are converted to 2 binary, bitwise AND, the binary number is only smaller than I to meet -temp++;Wuyi             } theAns =max (ans, temp); -         } Wu     } -printf"%d\n", ans); About     return 0; $}
View Code

POJ 24,362 Binary enumeration + bitwise operation

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.