给你一个长度为 n
的正整数数组 nums
和一个整数 k
。
一开始,你的分数为 1
。你可以进行以下操作至多 k
次,目标是使你的分数最大:
nums[l, ..., r]
。nums[l, ..., r]
里面选择一个 质数分数 最高的元素 x
。如果多个元素质数分数相同且最高,选择下标最小的一个。x
。nums[l, ..., r]
表示 nums
中起始下标为 l
,结束下标为 r
的子数组,两个端点都包含。
一个整数的 质数分数 等于 x
不同质因子的数目。比方说, 300
的质数分数为 3
,因为 300 = 2 * 2 * 3 * 5 * 5
。
请你返回进行至多 k
次操作后,可以得到的 最大分数 。
由于答案可能很大,请你将结果对 109 + 7
取余后返回。
示例 1:
输入:nums = [8,3,9,3,8], k = 2 输出:81 解释:进行以下操作可以得到分数 81 : - 选择子数组 nums[2, ..., 2] 。nums[2] 是子数组中唯一的元素。所以我们将分数乘以 nums[2] ,分数变为 1 * 9 = 9 。 - 选择子数组 nums[2, ..., 3] 。nums[2] 和 nums[3] 质数分数都为 1 ,但是 nums[2] 下标更小。所以我们将分数乘以 nums[2] ,分数变为 9 * 9 = 81 。 81 是可以得到的最高得分。
示例 2:
输入:nums = [19,12,14,6,10,18], k = 3 输出:4788 解释:进行以下操作可以得到分数 4788 : - 选择子数组 nums[0, ..., 0] 。nums[0] 是子数组中唯一的元素。所以我们将分数乘以 nums[0] ,分数变为 1 * 19 = 19 。 - 选择子数组 nums[5, ..., 5] 。nums[5] 是子数组中唯一的元素。所以我们将分数乘以 nums[5] ,分数变为 19 * 18 = 342 。 - 选择子数组 nums[2, ..., 3] 。nums[2] 和 nums[3] 质数分数都为 2,但是 nums[2] 下标更小。所以我们将分数乘以 nums[2] ,分数变为 342 * 14 = 4788 。 4788 是可以得到的最高的分。
提示:
1 <= nums.length == n <= 105
1 <= nums[i] <= 105
1 <= k <= min(n * (n + 1) / 2, 109)
nums[i]
's prime score s[i]
by factoring in O(sqrt(nums[i]))
time.nums[i]
, find the nearest index left[i]
on the left (if any) such that s[left[i]] >= s[i]
. if none is found, set left[i]
to -1
. Similarly, find the nearest index right[i]
on the right (if any) such that s[right[i]] > s[i]
. If none is found, set right[i]
to n
.right[i]
and left[i]
.i
, if left[i] + 1 <= l <= i <= r <= right[i] - 1
, then s[i]
is the maximum value in the range [l, r]
. For this particular i
, there are ranges[i] = (i - left[i]) * (right[i] - i)
ranges where index i
will be chosen.nums
by non-increasing prime score, each element will be chosen min(ranges[i], remainingK)
times, where reaminingK
denotes the number of remaining operations. Therefore, the score will be multiplied by s[i]^min(ranges[i],remainingK)
.A^B mod C
.