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 ...
随机推荐
- Delphi 关闭MDI子窗口
需要在子窗口的onClose事件中吧Action = caFree; 就可以了. procedure Tfrm_aa.FormClose(Sender: TObject; var Action: TC ...
- Linux 文件的几种类型
文件的几种类型: 1.普通文件 普通文件就是一般意义上的文件,它们作为数据存储在系统磁盘中,可以随机访问文件的内容.Linux系统中的文件是面向字节的,文 件的内容以字节为单位进行存储与访问 ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_all_from_list(self, locator)
def select_all_from_list(self, locator): """Selects all values from multi-select list ...
- python27+django数据库配置常见问题
mysql缺乏模块,需要安装,建议去http://sourceforge.net/projects/mysql-python/files/mysql-python/下源码编译,或者安装msi文件htt ...
- Javascript兼容和CSS兼容总结
javascript部分 1. document.form.item 问题问题:代码中存在 document.formName.item(“itemName”) 这样的语句,不能在FF下运行解决方法: ...
- HighChart 实现从后台取数据来实时更新柱状和折线组图
前段时间公司让弄图表,给我说有HighCharts这个js插件,于是上网上搜,由于本人是写后端的,对于JavaScript和jQuery不是很熟悉,虽然找到了模板,但是还是不明白,所以一点一点的改,但 ...
- 初学JavaScript(入门一)
javaScript是世界上最流行的脚本语言 在我们的手机.电脑设备上所浏览的所有网页,以及基于HTML5手机App的交互都是通过javaScript驱动的,所以javascript是前端工作的一 ...
- 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits
190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- 2014搜狗前端面经【B事业部】
本来就投了一份简历,后来又收到了个B事业部的面试电话,今天刚面完一面,总体感觉还是很基础的,其中一名面试官帅到不行啊!另一个也不差,真是幸胡...(sorry,跑题了...) 上来先做了份笔试题,超级 ...
- Linux/Unix mac 命令笔记
bg和fg Linux/Unix 区别于微软平台最大的优点就是真正的多用户,多任务.因此在任务管理上也有别具特色的管理思想.我们知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行 ...