Codeforces484A (strings,bitmasks)
来源:互联网 发布:天数倒计时软件 编辑:程序博客网 时间:2024/06/07 03:27
- 第一种做法,字符串模拟
- 注意0和0情况
- 分l和r二进制位数相同和不同两种情况,这两种情况,r都有可能是答案
#include <cstdio>#include <cstring>void to_binary(long long num,int str[],int& mx){ int cnt=0; while(num!=0) { if(num&1) str[cnt] = 1; num=num>>1; cnt++; } mx = cnt;}void to_decimal(long long& num,int str[],int mx){ num=0; for(int i=mx-1;i>=0;i--) { num*=2; num+=str[i]; }}int compare_l_r(int strl[],int strr[],int mx){ for(int i=mx-1;i>=0;i--) { if(strl[i]!=strr[i]) return i; } return 0;}bool number_zero(int str[],int locl,int locr){ for(int i=locl;i<=locr;i++) if(str[i] == 0 ) return false; return true;}int main(){ int N; scanf("%d",&N); for(int i=0;i<N;i++) { long long l,r; scanf("%I64d%I64d",&l,&r); if(l==0&&r==0) { printf("0\n"); continue; } int lb[70]; int rb[70]; memset(lb,0,sizeof(lb)); memset(rb,0,sizeof(rb)); int lmax = 0; int rmax = 0; to_binary(l,lb,lmax); to_binary(r,rb,rmax); if(rmax>lmax) { int ansstr[70]; memset(ansstr,0,sizeof(ansstr)); for(int j=0;j<rmax;j++) ansstr[j] = 1; long long rtemp; to_decimal(rtemp,ansstr,rmax); if(rtemp == r) printf("%I64d\n",r); else { memset(ansstr,0,sizeof(ansstr)); for(int j=0;j<rmax-1;j++) ansstr[j] = 1; long long ans; to_decimal(ans,ansstr,rmax-1); printf("%I64d\n",ans); } }else { int loc = compare_l_r(lb,rb,rmax); if(number_zero(rb,0,loc)) printf("%I64d\n",r); else { int ansstr[70]; memset(ansstr,0,sizeof(ansstr)); for(int j=0;j<loc;j++) ansstr[j] = 1; for(int j=loc;j<rmax;j++) ansstr[j] = lb[j]; long long ans; to_decimal(ans,ansstr,rmax); printf("%I64d\n",ans); } } } return 0;}
- 第二种做法,循环二进制位数,推荐
#include <cstdio>int main(){ int N; scanf("%d",&N); for(int n=0;n<N;n++) { long long l,r; scanf("%I64d%I64d",&l,&r); for(long long i=1;(l|i)<=r;i=(i<<1)) l=(l|i); printf("%I64d\n",l); } return 0;}
1 0
- Codeforces484A (strings,bitmasks)
- CodeForces484A Bits(贪心)
- codeforces round# 296 (div1 D)(bitmasks)
- Surprising Strings(STL)
- Multiply Strings(medium)
- poj2406Power Strings(KMP)
- HDU6170Two strings(DP)
- Strings
- Strings
- strings
- Strings
- Strings
- Strings
- strings
- Strings
- CF#321-D - Kefa and Dishes-状压DP+bitmasks
- codeforces 868C.Qualification Rounds(bitmasks与状压dp)
- 七 Strings类(一)
- C++学习总结
- SpringBoot入门系列:第一篇 Hello World
- 怎样健康喝水 这样喝水有助我们的健康
- gson日期转换bean Data
- 一个表中一次性分类返回满足不同条件的数据
- Codeforces484A (strings,bitmasks)
- 利用RunLoop优化tableView
- 最小生成树n*n模版
- TCP与UDP区别
- Ping命令的原理和使用
- JAVA读取等操作Excel文件
- URLConnection方法
- cuda性能调优
- base-64 字符数组的无效长度