uva 12898 And Or

来源:互联网 发布:文明的网络素养 编辑:程序博客网 时间:2024/05/03 18:58

题目:  

Given A and B, 1 ≤ A ≤ B ≤ 1018, find the result of A|(A + 1)|(A + 2)| . . . |B and A&(A + 1)&(A +2)& . . . &B.| operator represents bitwise OR (inclusive)& operator represents bitwise AND

Input:

The first line of the input contains an integer T (T ≤ 100000) denoting the number of test cases. Eachof the following T lines has two space separated integers A and B, 1 ≤ A ≤ B ≤ 10^18.

Output:

For each input, print the output in the format, ‘Case C: X Y ’ (quote for clarity). here C is the casenumber starting from 1, X is the result of bitwise (inclusive) OR of numbers from A to B inclusiveand Y is the result of bitwise AND of numbers from A to B, inclusive.For the exact input/output format please check the sample input/output section.Note:| operator represents bitwise OR. A bitwise OR takes two bit patterns of equal length and performsthe logical inclusive OR operation on each pair of corresponding bits. The result in each position is 1 ifthe first bit is 1 or the second bit is 1 or both bits are 1; otherwise, the result is 0. [Source: Wikipedia]& operator represents bitwise AND. A bitwise AND takes two equal-length binary representationsand performs the logical AND operation on each pair of the corresponding bits, by multiplying them.Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1(1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0). [Source: Wikipedia]

Sample Input


2

1 1

1 2

Sample Output

Case 1: 1 1

Case 2: 3 0

  题目大意:从a一直or到b,从a一直&到b,问你最后的值是多少。

思路:第一次天真的循环&和|直接超时23333. 把a b转换为二进制数,如果是不同位数的因为进位所以会导致&的结果为0,|的结果为2^k-1,k是b的二进制数的位数。同一个二进制位的情况下,从最高位往下比较,碰到某一位不同后,后面的&全部置0,|全部置1(也是进位的原因)。

 AC代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<cmath>using namespace std;string f(int a){    string s;    while(a)    {        if(a%2==1) s+='1';        if(a%2==0) s+='0';        a/=2;    }    return s;}int main(){    int t;    cin>>t;    for(int i=1;i<=t;i++)    {        long long int a,b,res1=0,res2=0;        cin>>a>>b;        string s1=f(a);        string s2=f(b);        string ss1;        string ss2;        if(s2.size()>s1.size())        {            res2=0;            res1=1;            for(int i=0;i<s2.size();i++)                res1*=2;            res1--;        }        else        {            long long int kk=1;            for(int j=s1.size()-1;j>=0;j--)            {                if(s1[j]==s2[j])                    ss1+=s1[j],ss2+=s1[j];                else                    break;            }            while(ss1.size()<s1.size())                ss1+='1',ss2+='0';            for(int j=ss1.size()-1;j>=0;j--)            {                if(ss1[j]=='1')                    res1+=kk;                if(ss2[j]=='1')                   res2+=kk;                kk*=2;            }        }         cout<<"Case "<<i<<": "<<res1<<" "<<res2<<endl;    }    return 0;}


0 0
原创粉丝点击