Trains the technique from
LeetCode 3901Good Subsequence QueriesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Register values are given as nums, along with a target p.
Two positions i < j form a matched pair when nums[i] * nums[j] divides evenly by p.
Each entry of queries is [ind, val] and rewrites nums[ind] to val, a change that stays for every later query. Count the matched pairs after each rewrite, and return the total of those counts modulo 10^9 + 7.
Example 1
Each rewrite is counted after it lands and the changes carry forward, so the three counts are added together.
Example 2
Every product divides evenly by one, so the single pair always matches and the one rewrite reports one.
Example 3
The values start out all odd, so no product is even. The first rewrite puts a 2 in place and still leaves no pair, since one even value is not enough on its own; the second puts a 4 in, and that pair matches.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_good_subseq(nums: list[int], p: int, queries: list[list[int]]) -> int:public int countGoodSubseq(int[] nums, int p, int[][] queries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.