UVA 12898

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4763

题意:给你一个区间L,R;求区间所有数的并,和或的答案;

思路:两个数的二进制长度不一样,显然是2^(len(r))-1,0;

   长度一样取前面相等的部分,或后面填1,且后面填0即可;

跟某场CCPC区间(L,R)取两个数,好像是一样的;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
int getlen(ll x)
{
    int sum=0;
    while(x)
    {
        sum++;
        x/=2;
    }
    return sum;
}
int a[100],b[100];
int main()
{
    ll l,r;
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld",&l,&r);
        int x=getlen(l);
        int y=getlen(r);
        printf("Case %d: ",cas++);
        if(x!=y)
        {
            ll p=1;
            printf("%lld %lld
",(p<<y)-1,0LL);
            continue;
        }
        int flag=0;
        ll ansh=0,ansb=0;
        while(l)
        {
            a[flag]=l%2;
            b[flag]=r%2;
            flag++;
            l>>=1;
            r>>=1;
        }
        for(int i=x-1;i>=0;i--)
            if(a[i]!=b[i])
            {
                for(int j=i;j>=0;j--)
                    a[j]=1,b[j]=0;
                break;
            }
        for(int i=x-1;i>=0;i--)
        ansh=ansh*2+a[i],ansb=ansb*2+b[i];
        printf("%lld %lld
",ansh,ansb);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5985973.html