【瞎搞】 Codeforces Round 276 DIV 2 C.Bits
2014-11-08 21:43
330 查看
求L-R 区间内的X的二进制中1 最多的个数
当前 L 为 popcount(L) 中最小的
每次从低到高 在 L为0 的位置上添上1 保证了值的最小
当前 L 为 popcount(L) 中最小的
每次从低到高 在 L为0 的位置上添上1 保证了值的最小
#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <cmath> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <map> #define cler(arr, val) memset(arr, val, sizeof(arr)) typedef long long LL; const int MAXN = 100000+6; const int MAXM = 140000; const int INF = 0x3f3f3f3f; const int mod = 1000000007; int num(LL x) { int sum=0; while(x) { LL c=x%2; x/=2; if(c) sum++; } return sum; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif LL n,l,r; cin>>n; while(n--) { cin>>l>>r; for(int i=60;i>=0;i--) { LL c=l; int cnt=i-num(l); for(int j=0;cnt&&c<=r;j++) { if((c&((LL)1<<j))==0) c|=((LL)1<<j),cnt--; } if(cnt==0&&c<=r) { cout<<c<<endl; break; } } } return 0; }
相关文章推荐
- 【瞎搞】 Codeforces Round 276 DIV 2 C.Bits
- Codeforces Round #276 (Div. 2) C. Bits
- 【Codeforces Round 276 (Div 2)C】【讨论】Bits [l,r]范围内第1个数最多数中最小的那个
- Codeforces Round #276 (Div. 2)
- Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数
- 【Codeforces Round 276 (Div 2)D】【数论 正难则反 枚举倍数 二分 O(nlogn)】Maximum Value n个数大数mod小数找最大余数
- Codeforces Round #276 (Div. 2) C
- Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心
- Codeforces Round #276 (Div. 1) D. Kindergarten
- 【Codeforces Round 276 (Div 2)A】【暴力】Factory 数增余数直到余数为0
- 【Codeforces Round 276 (Div 2)B】【水题】Valuable Resources 最小正方形包含所有点
- Codeforces Round #276 (Div. 1)D(贪心+dp)
- Codeforces Round #276 (Div. 2)
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts(瞎搞)
- Codeforces Round #276 (Div. 2)
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
- Codeforces Round #334 (Div. 2)C. Alternative Thinking(贪心)
- Educational Codeforces Round 35 (Rated for Div. 2)
- Codeforces Round #272 (Div. 1) C. Dreamoon and Strings