Given an integer array nums and an integer k, return the number of subarrays of nums where the least common multiple of the subarray's elements is equal to k.
A subarray is a contiguous non-empty sequence of elements within an array.
The least common multiple of an array is the smallest positive integer that is divisible by all the array elements.
Function Signature
def subarray_lcm(nums: list[int], k: int) -> int:
Rules
-
A subarray is
nums[i..j]
with
0 <= i <= j < len(nums)
. Subarrays with different start or end indices are counted separately, even when they hold the same values.
-
k
does not have to appear in
nums
. Several smaller elements can combine to an LCM of
k
.
Constraints
-
1 <= len(nums) <= 1000
-
1 <= nums[i], k <= 1000
Examples
Input: nums = [3,6,2,7,1], k = 6
Output: 4
The subarrays with LCM 6 are [3,6], [3,6,2], [6] and [6,2]. Any subarray that includes the 7 has an LCM that is a multiple of 7, and the remaining ones ([3], [2], [1]) have LCM 3, 2 and 1.
Input: nums = [3], k = 2
Output: 0
The only subarray is [3], whose LCM is 3, so no subarray has LCM 2.
Input: nums = [2,3,4,6], k = 12
Output: 5
The subarrays with LCM 12 are [3,4], [4,6], [2,3,4], [3,4,6] and [2,3,4,6]. The other five subarrays have LCM 2, 3, 4, 6 and 6 ([2,3]), so 12 is reached only by combining elements.