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 ...
随机推荐
- Sciter使用心得
1. div双击事件 $(div).onMouse = function(evt) { switch(evt.type) { case Event.MOUSE_DCLI ...
- Android百度地图开发(二)地图覆盖物
上一篇文章写道如何在一个mapview中简单的显示地图:本次学习一下如何在地图上添加一些覆盖物. 1.设置显示交通地图: // 显示交通地图 mapView.setTraffic(true); 2.设 ...
- ansible网络模块安装httplib2
ansible网络模块安装httplib2 在进行使用ansible的网络模块的时候,需要安装httplib2模块 下载地址: https://pypi.python.org/pypi?%3Aacti ...
- Maven,预加载资源文件
预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...
- AudioPolicyManager::setDeviceConnectionState 流程(一)
当有线耳机插入/拔出或蓝牙耳机的插入/拔出等,这些事件都会引起Audio Route的重新配置.重新配置的过程实在AudioPolicyManager::setDeviceConnectionStat ...
- 【VC】VC工具栏图标合并工具(非tbcreator和visual toolbar)
VC开发难免会用到toolbar,在没有美工的时候,大部分时间我们只能自己上. 第一个方法:fireworks/photoshop平铺.现在的图片资源大多为背景透明的png图片,虽然fireworks ...
- [POJ] #1004# Financial Management : 浮点数运算
一. 题目 Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 173910 Acc ...
- Android通过tcpdump抓包
1. 手机要有root权限 2. 下载tcpdump http://www.strazzere.com/android/tcpdump 3. adb push c:\wherever_you_pu ...
- 各种less开发工具
less是前端开发CSS的神器,但如何让less代码语法高亮,智能提示,快速编译及格式化,这不是一般的IDE的less插件能做到.下面是我搜刮到的一些工具 Codekit - incident57又一 ...
- css之clear属性
clear属性用来设置元素左右两边是否可以存在浮动元素. 它的值包括:left,right,both,none.其中none代表左右两边可以出现浮动元素.