Personally, the problem is meaningful for a deeper understanding of the bitwise operation.
According to the topic, we ask for an independent variable in a given range, so that the final result is the largest.
So because this problem is for the bit operation, so you can think of a bit operation to limit the situation, that is, "0" and "(2^bit)-1" of the two cases, then you can get each of these operations after each of the circumstances, and then according to the appropriate situation to reverse the optimal solution to meet the conditions, And then you can be a.
Throw a question & code directly
Description
21st century, many people have a strange disease: Get up difficulty syndrome, its clinical manifestations are: Get up hard, get up after the bad spirit. As a young sunshine good teenager, ATM has been insisting on the struggle against getting out of trouble syndrome. By studying the relevant literature, he found the cause of the disease: in the depths of the Pacific Ocean, there is a dragon named DRD, which holds the essence of sleep, can arbitrarily prolong everyone's sleep time. It is because of the activities of DRD, get up difficulty syndrome intensified, at an alarming rate in the world spread. To eradicate the disease, the ATM decided to go to the seabed and destroy the dragon. After all the hardships, the ATM finally came to the place where DRD, ready to start a very arduous battle. DRD has a very special skill, and his defensive warfare line is capable of using certain calculations to change the damage he receives. Specifically, DRD's defensive front is made up of N -Fan defensive doors. Each guard gate consists of an op op and a parameter T, where the operation must be one of the Or,xor,and, and the parameter must be a nonnegative integer. If the attack has not yet passed through the guard Gate, the attack damage will change to x op t. The final DRD is hurt by the opponent's initial attack x in turn through all the n -door defensive doors . Due to the limited ATM level, his initial attack can only be an integer between 0 and m (i.e., his initial attack can only be selected in 0,1,...,m , but the attack after the defensive gate is not limited by m ). In order to conserve energy, he hopes that by choosing the right initial attack to make his attack drd the most damage, please help him to calculate how much his attack can damage DRD.
Input
The 1th line contains 2 integers, followed by n,m, to indicate that the DRD has N-Fan guard gates, and the initial attack power of the ATM is an integer between 0 and M. The next n lines, in turn, represent each of the defensive doors. Each row consists of a string op and a non-negative integer t, separated by a space, and the OP is in front, and the OP represents the action of the defensive gate, and T represents the corresponding parameter. N<=10^5
Output
An integer line that represents the maximum amount of damage that DRD is exposed to by an ATM attack.
Sample Input3 10
and 5
OR 6
XOR 7Sample Output1
HINT "Sample Description 1" ATM can choose the initial attack power of 0,1,..., 10. Assuming the initial attack damage is 4, the final attack is calculated as follows 4 and 5 = 7 OR 6 = + + 1 is similar, we can calculate the initial attack to 1,3,5,7,9 when the final attack is 0, the initial attack is 0,2,4,6,8,10 when the final attack is 1, so the ATM A maximum of one attack causes the DRD to receive a damage value of 1. 0<=m<=10^90<=t<=10^9 must be an "operation explanation" in the Or,xor,and. In the subject, the player needs to first convert the number into binary before the calculation. If the two-digit binary length of the operation is different, the first 0 to the same length. The or is a bitwise OR operation, processing two binary numbers of the same length, two corresponding bits as long as one is 1, then the result value of the bit is 1, otherwise 0. XOR performs a logical XOR or operation on a bitwise XOR or operation, with each bit of the equivalent long binary mode or binary number. If the two corresponding bits are different (dissimilar), the result value of the bit is 1, otherwise the bit is 0. And for bitwise and operations, processing two binary numbers of the same length, two corresponding bits are 1, the result value of this bit is 1, otherwise 0. For example, we use the decimal number 5 and the decimal number 3 respectively for Or,xor and and operations, you can get the following results: 0101 (decimal 5) 0101 (decimal 5) 0101 (decimal 5) OR 0011 (decimal 3) XOR 0011 (decimal 3) and 0011 (decimal 3) = 0111 (decimal 7) = 0110 (decimal 6) = 0 001 (decimal 1) then the code
1 /**************************************************************2 problem:36683 User:pencilwang4 language:c++5 result:accepted6 time:272 Ms7 memory:2540 KB8 ****************************************************************/9 Ten#include <stdio.h> One#include <algorithm> A#include <math.h> - using namespacestd; - structshit{ the Charstr[Ten]; - intnum; -}e[110000]; - intN,m,ans,ass; + intlen,fucker1[ +],fucker2[ +]; - voidGett (intPintNumintshit) + { A if(Shit> (M>>P))return ; at if(p==0) - { - if(shit<=m) ans=Max (ans,num); - return; - } - if(fucker1[p]==1) inGett (P-1,num<<1|1,shit<<1); - Else if(fucker2[p]==0) toGett (P-1,num<<1,shit<<1); + Else - { theGett (P-1,num<<1|1,shit<<1|1); *Gett (P-1,num<<1,shit<<1); $ }Panax Notoginseng return ; - } the intFuck (intnum) + { A for(intI=1; i<=n;i++) the { + if(e[i].str[1]=='X') -num^=E[i].num; $ Else if(e[i].str[1]=='O') $num|=E[i].num; - Else if(e[i].str[1]=='A') -num&=E[i].num; the } - returnnum;Wuyi } the intMain () - { Wuscanf"%d%d",&n,&m); - for(intI=1; i<=n;i++) Aboutscanf"%s%d", e[i].str+1,&e[i].num); $ intt=0, tt=m; - while(TT) - { -t=t<<1|1; ATt/=2; + } the intSb1=fuck (0), sb2=fuck (t); - while(sb1| |SB2) $ { thefucker1[++ass]=sb1&1; thefucker2[ass]=sb2&1; thesb1>>=1, sb2>>=1; the } -Gett (The0,0); inprintf"%d", ans); the return 0; the}
3668
Brush question to "a question about bitwise arithmetic (BZOJ3668) (hard)