"小女孩和最大的XOR"

时间:2015-07-02 10:39:25

标签: c++ bit-manipulation xor

如何解决代码强制上的this问题。

Editorial中的第二种方法(非DP)似乎更容易,但我无法理解它是如何工作的。

有人可以详细解释非dp方法吗?

我也发现了这个我无法理解的代码实现

Application.SendKeys ""^S", True

有人帮忙。

1 个答案:

答案 0 :(得分:1)

如果您在http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Other-Builtins.html

中了解__builtin_clzll
  

- 内置函数:int __builtin_clzll (unsigned long long x)

     

从最高有效位开始,返回x中前导0位的数量。如果x为0,则结果未定义。

来自https://cs.stackexchange.com/a/29510

  

可以从[l, r]确定区间l ⊕ r中任意两个整数的最大可能XOR,假设l, r为整数。此值等于pow(2, p) − 1,其中p是最小值,pow(2, p)大于l ⊕ r

现在,与代码相关,

val = 64 - __builtin_clzll(l ^ r);    // p

(1LL << val) - 1;    // pow(2, p) - 1