博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 12898 And Or 数学暴力
阅读量:4310 次
发布时间:2019-06-06

本文共 3198 字,大约阅读时间需要 10 分钟。

And Or

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83008#problem/B

Description

Given A and B, 1 ≤ A ≤ B ≤ 1018, find the result of A|(A + 1)|(A + 2)| . . . |B and A&(A + 1)&(A + 2)& . . . &B.

| operator represents bitwise OR (inclusive)
& operator represents bitwise AND

Input

The first line of the input contains an integer T (T ≤ 100000) denoting the number of test cases. Each of the following T lines has two space separated integers A and B, 1 ≤ A ≤ B ≤ 1018

.

Output

For each input, print the output in the format, ‘Case C: X Y ’ (quote for clarity). here C is the case number starting from 1, X is the result of bitwise (inclusive) OR of numbers from A to B inclusive and Y is the result of bitwise AND of numbers from A to B, inclusive.

For the exact input/output format please check the sample input/output section.
Note:
| operator represents bitwise OR. A bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 or the second bit is 1 or both bits are 1; otherwise, the result is 0. [Source: Wikipedia] & operator represents bitwise AND. A bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, by multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0). [Source: Wikipedia]

Sample Input

2

1 1
1 2

Sample Output

Case 1: 1 1

Case 2: 3 0

HINT

 

题意

从a一直or到b,从a一直&到b,问你最后的值是多少

题解:

两个思路都差不多,如果两个数转化成二进制之后,位数不一样的话,一个输出0,一个输出2^k

如果位数一样的话,那就分析一下就好了,从高位往低位,如果一样就不管,如果遇到不一样的,就直接break

然后把后面都置0或者置1

代码

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define test freopen("test.txt","r",stdin) #define maxn 2000001#define mod 10007#define eps 1e-9const int inf=0x3f3f3f3f;const ll infll = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//**************************************************************************************string get(ll a){ string s; while(a) { if(a%2==1) s+='1'; else s+='0'; a/=2; } return s;}int main(){ int n=read(); for(int cas=1;cas<=n;cas++) { ll a,b; cin>>a>>b; string s1=get(a),s2=get(b); ll ans1=0,ans2=0; if(s2.size()>s1.size()) { ans2=0; ans1=1; for(int i=0;i
=0;i--) { if(s1[i]==s2[i]) ss+=s1[i],ss1+=s1[i]; else break; } while(ss.size()
=0;i--) { if(ss[i]=='1') ans1+=kiss; if(ss1[i]=='1') ans2+=kiss; kiss*=2; } } ll x=a; ll y=a; for(int i=a+1;i<=b;i++) x|=i; for(int i=a+1;i<=b;i++) y&=i; printf("Case %d: %lld %lld\n",cas,ans1,ans2); }}

 

转载于:https://www.cnblogs.com/qscqesze/p/4652152.html

你可能感兴趣的文章
PHP empty、isset、innull的区别
查看>>
apache+nginx 实现动静分离
查看>>
通过Navicat远程连接MySQL配置
查看>>
phpstorm开发工具的设置用法
查看>>
Linux 系统挂载数据盘
查看>>
Git基础(三)--常见错误及解决方案
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>