【问题标题】:Find X such that the product (A ^ X) * (B ^ X) is maximum找到 X 使得乘积 (A ^ X) * (B ^ X) 最大
【发布时间】:2022-11-20 03:15:13
【问题描述】:

找到 X 使得 (A ^ X) * (B ^ X) 最大

给定 A、B 和 N (X < 2^N)

例子:

A = 4
B = 6
N = 3
We can choose X = 3 and (A ^ X) = 7 and (B ^ X) = 5.
The product will be 35 which is the maximum.

【问题讨论】:

  • 乘积是对 2 的某个幂取模,还是“完整”乘积
  • 乘积取模 10^9+7
  • 好的,您自己的答案似乎没有考虑到这一点
  • 是的,现在编辑了。

标签: bit-manipulation xor


【解决方案1】:
int limit = (1<<n) - 1;
int MOD = 1_000_000_007;
int maxProd = 1;
for(int i = 1; i <= limit; i++){
  int x1 = (A^i);
  int x2 = (B^i);
  maxProd = max(maxProd, (x1*x2) % MOD);
}
return maxProd;

时间复杂度 = O(2^N)

如何以更小的时间复杂度找到 X?

【讨论】:

  • 这不是答案,这是您问题的延续。请改为编辑您的问题。
猜你喜欢
  • 2022-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
相关资源
最近更新 更多