Maximizing XOR

Maximizing XOR

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
/*
 * Complete the function below.
 */
int maxXor(int l, int r) {
    int max = l^l;
    for(int i = l;i <= r;i++)
        for(int j = i;j <= r;j++)
        {
            if((i^j) > max)
                max = i^j;
        }

    return max;
}

int main() {
    int res;
    int _l;
    cin >> _l;
    
    int _r;
    cin >> _r;
    
    res = maxXor(_l, _r);
    cout << res;
    
    return 0;
}
时间: 2024-07-28 16:29:23

Maximizing XOR的相关文章

【HackerRank】Maximizing XOR

给定两个整数:L 和 R ∀ L ≤ A ≤ B ≤ R, 找出 A xor B 的最大值. 输入格式 第一行包含 L 第一行包含 R 数据范围 1 ≤ L ≤ R ≤ 103 输出格式 输出最大的异或和 题解: 1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.regex.*; 6 7 public class Solution { 8

HackerRank &quot;Maximizing XOR&quot;

A natural thought is brutal-force. But as you may have already thought of, there must be a smarter one. And yes there is. Think like this: XOR gives you all different bits, if you could imagine the binary representation of L^R, it can be represented

bzoj 2337: [HNOI2011]XOR和路径

Description Input Output Sample Input Sample Output HINT Source Day2 终于把这个史前遗留的坑给填了... 首先异或的话由位无关性,可以按位处理... 那么对于每一位,设f[i]表示从i出发第一次到达n且xor和为1的概率,out[i]为i的出边,那么转移就比较容易了... if(w(i,j)&xxx) f[i]+=(1-f[j)/out[i];// 这条边该位为1,需要xor上0,xor和才为1 else f[i]+=f[j]/

【BZOJ】2337: [HNOI2011]XOR和路径

[算法]期望+高斯消元 [题解]因为异或不能和期望同时运算,所以必须转为加乘 考虑拆位,那么对于边权为1取反,边权为0不变. E(x)表示从x出发到n的路径xor期望. 对于点x,有E(x)=Σ(1-E(y))(边权1)||E(y)(边权0)/t[x]  t[x]为x的度. 那么有n个方程,整体乘上t[x]确保精度,右项E(x)移到左边--方程可以各种变形. 每次计算完后*(1<<k)就是贡献. 逆推的原因在于n不能重复经过,而1能重复经过,所以如果计算"来源"不能计算n,

421. Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum resul

Codeforces 617 E. XOR and Favorite Number

题目链接:http://codeforces.com/problemset/problem/617/E 一看这种区间查询的题目,考虑一下莫队. 如何${O(1)}$的修改和查询呢? 令${f(i,j)}$表示区间${\left [ l,r \right ]}$内数字的异或和. 那么:${f(l,r)=f(1,r)~~xor~~f(1,l-1)=k}$ 记一下前缀异或和即可维护. 1 #include<iostream> 2 #include<cstdio> 3 #include&l

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

HDU5344——数学题——MZL&#39;s xor

MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all (Ai+Aj)(1≤i,j≤n)The xor of an array B is defined as B1 xor B2...xor Bn Input Multiple test cases, the first line contains an integer T(no more than 20

xor异或逻辑操作(辅助完成图形的叠加)

异或操作的作用: 异或 两个不相同,返回true, 两个相同返回false 0 xor 0  = 0 0 xor 1  = 1 1 xor 0  = 1 1 xor 1  = 0 特殊情况, 全0的2*2矩阵,  一个其它矩阵和它xor的话是其本身: 依据 0 xor 0 =  0, 1 xor 0 = 1.   这个其它矩阵值不变. 0   0 0   0 特殊情况, 全1的2*2矩阵,  一个其它矩阵和它xor的话是其相反的值: 依据 0 xor 1 = 1,  1 xor 1 = 0,