noi水题,直接推出来每一位上取什么值才能取1
直接搞就可以了
1 var a,b:array[0..40] of longint; 2 x,n,i,j,k,t,m,ans:longint; 3 fl:boolean; 4 s:string; 5 ch:char; 6 7 procedure doit(x:longint); 8 begin 9 fillchar(b,sizeof(b),0); 10 m:=0; 11 while x<>0 do 12 begin 13 inc(m); 14 b[m]:=x mod 2; 15 x:=x shr 1; 16 end; 17 end; 18 19 begin 20 readln(n,k); 21 t:=31; 22 for i:=1 to 31 do 23 a[i]:=1; 24 for i:=1 to n do 25 begin 26 s:=''; 27 read(ch); 28 while ch<>' ' do 29 begin 30 s:=s+ch; 31 read(ch); 32 end; 33 readln(x); 34 doit(x); 35 if s='OR' then //2表示这一位取什么值都能使ans这位上取1,-1表示去什么都不可以,1表示取1才可以,0表示取0才可以 36 begin 37 for j:=1 to 31 do 38 if b[j]=1 then a[j]:=2; 39 end 40 else if s='AND' then 41 begin 42 for j:=1 to 31 do 43 if b[j]=0 then a[j]:=-1; 44 end 45 else begin 46 for j:=1 to 31 do 47 if b[j]=1 then 48 begin 49 if a[j]=-1 then a[j]:=2 50 else if a[j]=2 then a[j]:=-1 51 else if a[j]=1 then a[j]:=0 52 else if a[j]=0 then a[j]:=1; 53 end; 54 end; 55 end; 56 doit(k); 57 ans:=0; 58 fl:=false; //fl判断高位是否已经严格小于了 59 for i:=31 downto 1 do 60 if fl then 61 begin 62 if a[i]<>-1 then 63 ans:=ans+1 shl (i-1); 64 end 65 else begin 66 if (b[i]=0) and ((a[i]=2) or (a[i]=0)) then ans:=ans+1 shl (i-1); 67 if (b[i]=1) then 68 begin 69 if (a[i]=2) or (a[i]=0) then 70 begin 71 fl:=true; 72 ans:=ans+1 shl (i-1); 73 end 74 else if a[i]=-1 then fl:=true 75 else ans:=ans+1 shl (i-1); 76 end; 77 end; 78 79 writeln(ans); 80 end. 81 82