Uva 12898 - And Or

Sep 26, 2015

Problem: 12898 - And Or (download)
Author: Muhammed Hedayetul Islam
Alternate: Shiplu Hawlader, Md Mahbubul Hasan
Type: Adhoc, bitwise operation.
Solution: Just take pen and paper and write numbers one after one. You will understand that, for AND it is quite often that the leading digits are 1 and then remaining digits are 0. Almost same for OR. Leading digits are 0 and remaining are 1. How to understand where to stop? Well, just loop from msg to lsb of L and H. You will know how to decide. [Collected from: http://www.bubt-cse.edu.bd/ACM-ICPC%202014%20Document/Final_problem_set_Analysis.pdf, 26/09/2015]


Explain:
Step 1: Count bit length of a and b
step 2: if lenb>lena then OR = 2^lenb - 1 and And = 0*/
step 3: find/count not change bits between a and b, from MSB to LSB
        Or = b | R
         And = b | ~R  where R contain's 1 (total 1 = count)

Example:
[1]a = 7  b = 8

lena = 3 , Lenb = 4

 so Or = 2^lenb - 1 = 16 - 1 = 15

And = 0

[2] Let a = 12 , b =15

lena = 4, len b = 4
a = 1100 , b = 1111

start j = 3 and break when j = 1

R = 1LL<<(j+1) - 1 = (11)2
unchanged first 2 bits
Or = b | R = (1111) = 15
And = b & ~R = (1100) = 12


Code:
-----


No comments:

Post a Comment