UVA 12898 And Or 数学暴力

And Or

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83008#problem/B

Description

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. Each of the following T lines has two space separated integers A and B, 1 ≤ A ≤ B ≤ 1018
.

Output

For each input, print the output in the format, ‘Case C: X Y ’ (quote for clarity). here C is the case number starting from 1, X is the result of bitwise (inclusive) OR of numbers from A to B inclusive and 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 performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 1 if the 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 representations and 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

HINT

题意

从a一直or到b,从a一直&到b,问你最后的值是多少

题解:

两个思路都差不多,如果两个数转化成二进制之后,位数不一样的话,一个输出0,一个输出2^k

如果位数一样的话,那就分析一下就好了,从高位往低位,如果一样就不管,如果遇到不一样的,就直接break

然后把后面都置0或者置1

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 2000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

string get(ll a)
{
    string s;
    while(a)
    {
        if(a%2==1)
            s+='1';
        else
            s+='0';
        a/=2;
    }
    return s;
}
int main()
{
    int n=read();
    for(int cas=1;cas<=n;cas++)
    {
        ll a,b;
        cin>>a>>b;
        string s1=get(a),s2=get(b);
        ll ans1=0,ans2=0;
        if(s2.size()>s1.size())
        {
            ans2=0;
            ans1=1;
            for(int i=0;i<s2.size();i++)
                ans1*=2;
            ans1--;
        }
        else
        {
            string ss,ss1;
            for(int i=s1.size()-1;i>=0;i--)
            {
                if(s1[i]==s2[i])
                    ss+=s1[i],ss1+=s1[i];
                else
                    break;
            }
            while(ss.size()<s1.size())
                ss+='1',ss1+='0';
            ll kiss=1;
            ans1=ans2=0;
            for(int i=ss.size()-1;i>=0;i--)
            {
                if(ss[i]=='1')
                    ans1+=kiss;
                if(ss1[i]=='1')
                    ans2+=kiss;
                kiss*=2;
            }
        }
        
        ll x=a;
        ll y=a;
        for(int i=a+1;i<=b;i++)
            x|=i;
        for(int i=a+1;i<=b;i++)
            y&=i;
        
        printf("Case %d: %lld %lld
",cas,ans1,ans2);
    }
}
原文地址:https://www.cnblogs.com/qscqesze/p/4652152.html