diff --git a/Hash/3448.Count-Substrings-Divisible-By-Last-Digit/3448.Count-Substrings-Divisible-By-Last-Digit.cpp b/Hash/3448.Count-Substrings-Divisible-By-Last-Digit/3448.Count-Substrings-Divisible-By-Last-Digit.cpp new file mode 100644 index 000000000..fd25aa2ae --- /dev/null +++ b/Hash/3448.Count-Substrings-Divisible-By-Last-Digit/3448.Count-Substrings-Divisible-By-Last-Digit.cpp @@ -0,0 +1,43 @@ +using LL = long long; +class Solution { + int n; +public: + long long countSubstrings(string s) + { + n = s.size(); + vectornums; + for (auto ch: s) + nums.push_back(ch-'0'); + nums.insert(nums.begin(), 0); + + LL ret = 0; + for (int k=1; k<=9; k++) + ret += helper(nums, k); + return ret; + } + + LL helper(vector&nums, int k) + { + vectorcount(k, 0); + vectorcount2(k,0); + LL ret = 0; + + int r = 0; + count[0] = 1; + for (int i=1; i<=n; i++) + { + for (int d=0; d assignElements(vector& groups, vector& elements) + { + int n = *max_element(groups.begin(), groups.end()); + vectorarr(n+1, -1); + + for (int j=0; jn) continue; + + if (arr[x0]!=-1) continue; + + int x = x0; + while (x<=n) + { + if (arr[x]==-1) + arr[x] = j; + x+=x0; + } + } + + vectorrets; + for (int g: groups) + rets.push_back(arr[g]); + return rets; + } +}; diff --git a/Others/3447.Assign-Elements-to-Groups-with-Constraints/Readme.md b/Others/3447.Assign-Elements-to-Groups-with-Constraints/Readme.md new file mode 100644 index 000000000..3e6d9e611 --- /dev/null +++ b/Others/3447.Assign-Elements-to-Groups-with-Constraints/Readme.md @@ -0,0 +1,7 @@ +### 3447.Assign-Elements-to-Groups-with-Constraints + +突破点在于groups里的元素的数值不超过1e5.在这个范围是,如果枚举所有1的倍数,然后枚举所有2的倍数,然后枚举所有3的倍数,直至枚举n的倍数,那么总共的时间复杂度是`n+n/2+n/3+...n/n = n*(1+1/2+1/3+...1/n)`.这个级数虽然不收敛,但是它是趋近于nlog(n)的。所以本题可以用暴力枚举。 + +所以本题的算法很简单。我们开一个长度为1e5的数组assign,来记录每个自然数最早能被哪个element所assign。我们依次考察element里的每个元素,比如说elements[j]=x,然后枚举x的所有倍数(直至1e5),比如说kx,那样就有`assign[kx] = j`,当然根据题意,我们对于每个assign我们只更新一次。 + +最后根据groups的数值,从assgin里把答案拷贝过去即可。 diff --git a/Readme.md b/Readme.md index 08acb0ac8..d99cd8366 100644 --- a/Readme.md +++ b/Readme.md @@ -211,6 +211,7 @@ [2875.Minimum-Size-Subarray-in-Infinite-Array](https://github.com/wisdompeak/LeetCode/tree/master/Hash/2875.Minimum-Size-Subarray-in-Infinite-Array) (H-) [2949.Count-Beautiful-Substrings-II](https://github.com/wisdompeak/LeetCode/tree/master/Hash/2949.Count-Beautiful-Substrings-II) (H-) [2950.Number-of-Divisible-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Hash/2950.Number-of-Divisible-Substrings) (H-) +[3448.Count-Substrings-Divisible-By-Last-Digit](https://github.com/wisdompeak/LeetCode/tree/master/Hash/3448.Count-Substrings-Divisible-By-Last-Digit) (H-) #### [Sorted Container](https://github.com/wisdompeak/LeetCode/tree/master/Sorted_Container) [220.Contains-Duplicate-III](https://github.com/wisdompeak/LeetCode/tree/master/Sorted_Container/220.Contains-Duplicate-III) (M) @@ -1631,6 +1632,7 @@ [2768.Number-of-Black-Blocks](https://github.com/wisdompeak/LeetCode/tree/master/Others/2768.Number-of-Black-Blocks) (M+) [2857.Count-Pairs-of-Points-With-Distance-k](https://github.com/wisdompeak/LeetCode/tree/master/Others/2857.Count-Pairs-of-Points-With-Distance-k) (M+) [3404.Count-Special-Subsequences](https://github.com/wisdompeak/LeetCode/tree/master/Others/3404.Count-Special-Subsequences) (H) +[3447.Assign-Elements-to-Groups-with-Constraints](https://github.com/wisdompeak/LeetCode/tree/master/Others/3447.Assign-Elements-to-Groups-with-Constraints) (M+) * ``Presum`` [1878.Get-Biggest-Three-Rhombus-Sums-in-a-Grid](https://github.com/wisdompeak/LeetCode/tree/master/Others/1878.Get-Biggest-Three-Rhombus-Sums-in-a-Grid) (M+) [1906.Minimum-Absolute-Difference-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Others/1906.Minimum-Absolute-Difference-Queries) (M+)