1969 数组元素的最小非零乘积

题目 1969. 数组元素的最小非零乘积 - 力扣(LeetCode) (leetcode-cn.com)

P=3,P=4,手推一下就可以发现规律,结果是 (2^P-2)^{(2^{(P-1)} - 1)}(2^P-1)

由于数字很大,用 C++ 实现快速幂需要手动取模。Python 中的 pow(x, y[, z]) 函数 z 可以指定为模数。

python class Solution: def minNonZeroProduct(self, p: int) -> int: return int((pow(2, p)-1) * pow( pow(2, p) - 2, (pow(2, p-1) - 1), 10**9+7)%(10**9+7))


最后更新: 2022-11-15

评论