And Or 或运算。注意超过了32位时,1要写成 1LL。

                                And Or

给出 a,b,   求  a|(a|a)|(a+2)|……|b   ,和  &

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF=0x4fffffff;
17 const int EXP=1e-5;
18 const int MS=1000005;
19 
20 int main()
21 {
22       int T;
23       scanf("%d",&T);
24       LL a,b,ans1,ans2;
25       int kase=1;
26       while(T--)
27       {
28             scanf("%lld%lld",&a,&b);
29             ans1=a;
30             for(LL i=0;(1LL<<i)<=b;i++)
31             {
32                   if(((a>>i)&1LL)==0)        //  注意这里一定要() ==0,不然出错。
33                   {
34                         if((((a>>i)+1LL)<<i)<=b)     //第(i+1)位为0,我们找这一位是1的,大于a的小于等于b的
35                               ans1|=(1LL<<i);             //  最小的数,  是哪一位呢。物理法,将a不断的加一,
36                   }                                                 //第i+1位为1时,前面一个状态必定是(i)位到第一位都为1.
37             }                                                     //再加1,就变成了第(i+1)位为1,第i到第一位为0
38 
39             ans2=a;
40             for(LL i=0;(1LL<<i)<=b;i++)
41             {
42                   if((a>>i)&1LL)
43                   {
44                         if((((a>>i)+1LL)<<i)<=b)                 //同样物理分析
45                             ans2-=(1LL<<i);
46                             // ans2&=~(1LL<<i);
47                   }
48             }
49             printf("Case %d: %lld %lld\n",kase++,ans1,ans2);
50       }
51       return 0;
52 }

 

posted @ 2015-04-04 20:15  daydaycode  阅读(217)  评论(0编辑  收藏  举报