hdu 5969 最大的位或 ccpc 2016 合肥站

传送门

分析:
icpccamp上是用数位dp做的,我个人感觉不需要吧。。
我是贪心做的。
因为 x|y>=max(x,y) ,所以我规定两个数 x,y,(x<=y) ,那么就让 y r好了。然后考虑 x ,从x的高位向低位贪心。尽量让数的每一位都能够取到1,这么考虑。如果 y 的当前位位1,那么x可以不取1,能不取就尽量不取,这样可以让 x 尽量小,在考虑后面位置的时候能够取到1。判断一下如果当前位取0的话,即使后面全部位为1的时候x是否大于等于 l 。同理,如果y的当前位是0,那么 x 要尽量取1,判断取1是否会超过上限r

代码:

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

#define   offcin        ios::sync_with_stdio(false)
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   slch          v<<1
#define   srch          v<<1|1
#define   sgetmid       int m = (l+r)>>1
#define   ll            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   pb            push_back
#define   fi            first
#define   se            second

const int    INF    = 0x3f3f3f3f;
const ll     INFF   = 1e18;
const double pi     = acos(-1.0);
const double inf    = 1e18;
const double eps    = 1e-9;
const ll     mod    = 1e9+7;
const int    maxmat = 10;
const ull    BASE   = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/

ll x;

void work(ll l, ll r) {
    for (int i = 62; i >= 0; i --) {
        // cout<<x<<" "<<i<<endl;
        int c = (r >> i) & 1;
        if (c) {
            ll tmp = x + (1ll << i) - 1;
            if (tmp < l) x |= (1ll << i);
        }
        else {
            ll tmp = x | (1ll << i);
            if (tmp <= r) x |= (1ll << i);
        }
    }
}

int main(int argc, char const *argv[]) {
    ll l, r;
    int T; cin>>T;
    while (T --) {
        scanf("%I64d%I64d", &l, &r);
        x = 0;
        work(l, r);
        printf("%I64d\n", x | r);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值