#GL0000377. Dãy con có GCD và LCM xác định (GCD and LCM Subsequences)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con có GCD và LCM xác định (GCD and LCM Subsequences)
GCD and LCM Subsequences
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array of positive integers and two target integers , count the non-empty subsequences satisfying
$$\gcd(\text{selected elements})=G,\qquad \operatorname{lcm}(\text{selected elements})=L.$$A subsequence selects a subset of positions while keeping their original order. Different index subsets count as different subsequences even if values are repeated. The empty subsequence does not count.
The input does not guarantee ; print if the targets cannot be met. Report the count modulo .
Input
The first line contains . The second line contains the values . The third line contains .
Output
Print the number of valid non-empty subsequences modulo .
Subtasks
-
Subtask 1 (20 points): , .
-
Subtask 2 (30 points): , .
-
Subtask 3 (50 points): , .
Examples
Example 1
Input
5
50 4 50 4 100
2 100
Output
18
Explanation
Choose at least one and at least one . Without a , every selected value is divisible by . Without a , every selected value is divisible by . A and a already give GCD and LCM . There are three nonempty choices in each group of two positions, while is optional, giving .
Example 2
Input
5
8 2 2 1 2
1 8
Output
8
Explanation
To attain LCM , select the element ; to attain GCD , select the element . Each of the three positions containing may be independently selected or skipped, giving subsequences.
Example 3
Input
4
2 4 8 16
2 16
Output
4
Explanation
Both and must be included. The elements and can each be included or skipped, resulting in .