UVALive 6918And Or(位运算)
来源:互联网 发布:呜呼与嗟乎的意思 编辑:程序博客网 时间:2024/05/21 17:58
题意:
求1≤L≤R≤1018区间的所有的数的AND和OR值
分析:
OR看1,AND看0
lowbit makes lowest 1 to 0 for AND
lowbit makes lowest 0 to 1(except trailing zeros) for OR
所以对于OR的话后缀零要特判一下
代码:
//// Created by TaoSama on 2015-12-08// Copyright (c) 2015 TaoSama. All rights reserved.////#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;typedef long long LL;int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); int kase = 0; while(t--) { LL l, r; scanf("%lld%lld", &l, &r);// LL a = l, b = l;// for(LL i = l; i <= r; ++i) a |= i, b &= i; LL ansAnd = l, ansOr = l; //trailing zeros for OR for(int i = 0; i < 64; ++i) { if(l >> i & 1) break; LL tmp = l + (1LL << i); if(tmp <= r) ansOr |= tmp; } //lowbit makes lowest 1 to 0 for AND //lowbit makes lowest 0 to 1(except trailing zeros) for OR while(l <= r) { ansOr |= l; ansAnd &= l; l += l & -l; } printf("Case %d: %lld %lld\n", ++kase, ansOr, ansAnd);// printf("Case %d: %lld %lld\n\n", kase, a, b); } return 0;}
0 0
- UVALive 6918And Or(位运算)
- UVALive 6918 And Or(位运算)
- python之位运算,and , or
- delphi and or ....位运算详解
- UVALive 6918 And Or 数学二进制+枚举
- UVALive - 6809 Spokes Wheel(位运算)
- delphi 按位运算 not and or xor shl shr
- 按位运算,窗体程序,And,Or,Xor
- UVALive 7279 Sheldon Numbers(位运算、暴力、想法)
- UVALive 7279 Sheldon Numbers(位运算、暴力)
- UVALive 3351 Easy and Not Easy Sudoku Puzzles 位运算~判断简单数独
- SQL AND & OR (运算符)
- Perl and or 运算
- AND & OR 运算符
- UVALive 6284 Hyperdrome 位运算+has
- UVALive 7279 Sheldon Numbers 【位运算】【暴力】
- 位运算 之按位与(AND)& 操作
- Rightmost Digit(快速幂+数学知识OR位运算)
- 04 @package访问控制符
- POJ 2455 解题报告
- UVALive 6917Decoding Baby Boos(签到)
- 05 @property和@synthesize
- lightoj1370 - Bi-shoe and Phi-shoe(欧拉函数)
- UVALive 6918And Or(位运算)
- Difference between <b> and <strong>
- 06 KVC与KVO
- 07 类的初始化和重写description方法
- 08 继承
- 09 多态
- 10 包装类(NSValue和NSNumber)
- 11 类别(category)和扩展(extension)
- 12 协议