UVA 12898 And Or 数学暴力
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 <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
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-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** string get(ll a)
{
string s;
while(a)
{
if(a%==)
s+='';
else
s+='';
a/=;
}
return s;
}
int main()
{
int n=read();
for(int cas=;cas<=n;cas++)
{
ll a,b;
cin>>a>>b;
string s1=get(a),s2=get(b);
ll ans1=,ans2=;
if(s2.size()>s1.size())
{
ans2=;
ans1=;
for(int i=;i<s2.size();i++)
ans1*=;
ans1--;
}
else
{
string ss,ss1;
for(int i=s1.size()-;i>=;i--)
{
if(s1[i]==s2[i])
ss+=s1[i],ss1+=s1[i];
else
break;
}
while(ss.size()<s1.size())
ss+='',ss1+='';
ll kiss=;
ans1=ans2=;
for(int i=ss.size()-;i>=;i--)
{
if(ss[i]=='')
ans1+=kiss;
if(ss1[i]=='')
ans2+=kiss;
kiss*=;
}
} ll x=a;
ll y=a;
for(int i=a+;i<=b;i++)
x|=i;
for(int i=a+;i<=b;i++)
y&=i; printf("Case %d: %lld %lld\n",cas,ans1,ans2);
}
}
UVA 12898 And Or 数学暴力的更多相关文章
- UVA 12898 - And Or 数学
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
- UVA.10986 Fractions Again (经典暴力)
UVA.10986 Fractions Again (经典暴力) 题意分析 同样只枚举1个,根据条件算出另外一个. 代码总览 #include <iostream> #include &l ...
- UVA 270 Lining Up 共线点 暴力
题意:给出几个点的位置,问一条直线最多能连过几个点. 只要枚举每两个点组成的直线,然后找直线上的点数,更新最大值即可. 我这样做过于暴力,2.7s让人心惊肉跳...应该还能继续剪枝的,同一直线找过之后 ...
- uva 10825 - Anagram and Multiplication(暴力)
题目链接:uva 10825 - Anagram and Multiplication 题目大意:给出m和n,要求找一个m位的n进制数,要求说该数乘以2~m中的随意一个数的结果是原先数各个位上数值的一 ...
- UVA 10976 分数拆分【暴力】
题目链接:https://vjudge.net/contest/210334#problem/C 题目大意: It is easy to see that for every fraction in ...
- UVa 11210 Chinese Mahjong (暴力,递归寻找)
题意:这个题意.有点麻烦,就是说给定13张牌,让你求能“听”的牌.(具体的见原题) 原题链接: https://uva.onlinejudge.org/index.php?option=com_onl ...
- UVa 1639 - Candy(数学期望 + 精度处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 11059 最大乘积 java 暴力破解
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
随机推荐
- [转]linux之less命令
转自:http://www.cnblogs.com/peida/archive/2012/11/02/2750588.html less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux ...
- 用正则表达式在注册页面(js/aspx.cs)的验证
1.验证邮箱(用户名) JS页面中: 首先定义变量和正则 var usermail = $("#usermail" ).val(); var username= /^([a-zA- ...
- hive 面试题 转载
转自:http://blog.csdn.net/ningguixin/article/details/12852051 有一张很大的表:TRLOG该表大概有2T左右TRLOG:CREATE TABLE ...
- MapReduce 中job.setJarByClass()方法的疑惑
在调试mr实例的时候,遇到如下的情况,如图所示 说明:就是我的mr程序类名称和我设置的setJarByclass()中设置的不一样,但是程序竟然没有报错!!!!当时把我吓尿了 疑惑:如果这样设置的话, ...
- C语言实现strlen
strlen: #ifndef STRLEN_H #define STRLEN_H #include <stdio.h> // 参考微软的写法 int cat_strlen(const c ...
- win+r 快速启动应用程序
如何使用WIN+R快捷键快速启动应用程序呢?其实很简单 首先随便在一个盘上建一个新文件夹,随便取什么名字,最好是英文格式,小编以D盘为例,如图 2. 打开文件夹,单击右键,选择“新建”,单击“快捷方式 ...
- Ruby on Rails vs. PHP vs. Python
开发者在开发web应用时,往往会对平台的选择感到困惑,而web专家通常会建议:要考虑几个因素,这些因素包括周转时间.质量.跨浏览器兼容性.与其他框架的整合.数据安全性.易于访问性等. 在考虑了这些因素 ...
- Apache Spark shell的实例操作
1.scala> val inFile = sc.textFile("./spam.data") 作用是将spam.data当作文本文件加载到Spark中,将spam.dat ...
- 使用DNSPod来处理网站的均衡负载(转)
add by zhj:配置倒是蛮简单的,其实就是把域名与多个IP进行关联,在数据库中实现这个应该也是蛮简单的. 原文:http://kb.cnblogs.com/page/75571/ 首先介绍下DN ...
- 详解Android定位
相信很多的朋友都有在APP中实现定位的需求,今天我就再次超炒冷饭,为大家献上国内开发者常用到的三种定位方式.它们分别为GPS,百度和高德,惯例先简单介绍下定位的背景知识. 什么是GPS定位.基站定位和 ...