diff --git a/src/3300-3399/3336.cpp b/src/3300-3399/3336.cpp new file mode 100644 index 0000000..7b619ec --- /dev/null +++ b/src/3300-3399/3336.cpp @@ -0,0 +1,128 @@ +/* +Tags +level-hard +array, math, dp, number-theory + +Problem Description +3336. Find the Number of Subsequences With Equal GCD + +You are given an integer array nums. +Your task is to find the number of pairs of non-empty subsequences (seq1, seq2) of nums that satisfy the following conditions: +The subsequences seq1 and seq2 are disjoint, meaning no index of nums is common between them. +The GCD of the elements of seq1 is equal to the GCD of the elements of seq2. +Return the total number of such pairs. +Since the answer may be very large, return it modulo 109 + 7. + +Example 1: +Input: nums = [1,2,3,4] +Output: 10 +Explanation: +The subsequence pairs which have the GCD of their elements equal to 1 are: +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) +([1, 2, 3, 4], [1, 2, 3, 4]) + +Example 2: +Input: nums = [10,20,30] +Output: 2 +Explanation: +The subsequence pairs which have the GCD of their elements equal to 10 are: +([10, 20, 30], [10, 20, 30]) +([10, 20, 30], [10, 20, 30]) + +Example 3: +Input: nums = [1,1,1,1] +Output: 50 + +Constraints: +1 <= nums.length <= 200 +1 <= nums[i] <= 200 +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../core/core.h" +using namespace std; + +class Solution +{ +private: + static constexpr int mod = 1e9 + 7; +public: + int subsequencePairCount(vector& nums) + { + int m = *max_element(nums.begin(), nums.end()); + + vector> dp(m + 1, vector(m + 1)); + dp[0][0] = 1; + + for (int num : nums) + { + vector> ndp(m + 1, vector(m + 1)); + for (int j = 0; j <= m; j++) + { + int divisor1 = gcd(j, num); + for (int k = 0; k <= m; k++) + { + int val = dp[j][k]; + if (val == 0) + { + continue; + } + + int divisor2 = gcd(k, num); + ndp[j][k] = (ndp[j][k] + val) % mod; + ndp[divisor1][k] = (ndp[divisor1][k] + val) % mod; + ndp[j][divisor2] = (ndp[j][divisor2] + val) % mod; + } + } + dp.swap(ndp); + } + + int result = 0; + for (int j = 1; j <= m; j++) + { + result = (result + dp[j][j]) % mod; + } + + return result; + } +}; + +int main() +{ + int n; + cin >> n; + vector nums(n, 0); + Solution sol; + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + int result = sol.subsequencePairCount(nums); + cout << result << "\n"; + return 0; +} \ No newline at end of file diff --git a/tests/3300-3399/3336.txt b/tests/3300-3399/3336.txt new file mode 100644 index 0000000..91be47e --- /dev/null +++ b/tests/3300-3399/3336.txt @@ -0,0 +1,20 @@ +Test Case 1: +Input: +4 +1 2 3 4 +Output: +10 + +Test Case 2: +Input: +3 +10 20 30 +Output: +2 + +Test Case 2: +Input: +4 +1 1 1 1 +Output: +50 \ No newline at end of file