diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/README.md b/solution/2900-2999/2928.Distribute Candies Among Children I/README.md index 5e5ff3bfb0086..7721c98560d4e 100644 --- a/solution/2900-2999/2928.Distribute Candies Among Children I/README.md +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/README.md @@ -41,6 +41,16 @@ +**方法一:组合数学 + 容斥原理** + +根据题目描述,我们需要将 $n$ 个糖果分给 $3$ 个小孩,每个小孩分到的糖果数在 $[0, limit]$ 之间。 + +这实际上等价于把 $n$ 个球放入 $3$ 个盒子中。由于盒子可以为空,我们可以再增加 $3$ 个虚拟球,然后再利用隔板法,即一共有 $n + 3$ 个球,我们在其中 $n + 3 - 1$ 个位置插入 $2$ 个隔板,从而将实际的 $n$ 个球分成 $3$ 组,并且允许盒子为空,因此初始方案数为 $C_{n + 2}^2$。 + +我们需要在这些方案中,排除掉存在盒子分到的小球数超过 $limit$ 的方案。考虑其中有一个盒子分到的小球数超过 $limit$,那么剩下的球(包括虚拟球)最多有 $n + 3 - (limit + 1) = n - limit + 2$ 个,位置数为 $n - limit + 1$,因此方案数为 $C_{n - limit + 1}^2$。由于存在 $3$ 个盒子,因此这样的方案数为 $3 \times C_{n - limit + 1}^2$。这样子算,我们会多排除掉同时存在两个盒子分到的小球数超过 $limit$ 的方案,因此我们需要再加上这样的方案数,即 $3 \times C_{n - 2 \times limit}^2$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 + ### **Python3** @@ -48,7 +58,16 @@ ```python - +class Solution: + def distributeCandies(self, n: int, limit: int) -> int: + if n > 3 * limit: + return 0 + ans = comb(n + 2, 2) + if n > limit: + ans -= 3 * comb(n - limit + 1, 2) + if n - 2 >= 2 * limit: + ans += 3 * comb(n - 2 * limit, 2) + return ans ``` ### **Java** @@ -56,19 +75,89 @@ ```java - +class Solution { + public int distributeCandies(int n, int limit) { + if (n > 3 * limit) { + return 0; + } + long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return (int) ans; + } + + private long comb2(int n) { + return 1L * n * (n - 1) / 2; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int distributeCandies(int n, int limit) { + auto comb2 = [](int n) { + return 1LL * n * (n - 1) / 2; + }; + if (n > 3 * limit) { + return 0; + } + long long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } +}; ``` ### **Go** ```go +func distributeCandies(n int, limit int) int { + comb2 := func(n int) int { + return n * (n - 1) / 2 + } + if n > 3*limit { + return 0 + } + ans := comb2(n + 2) + if n > limit { + ans -= 3 * comb2(n-limit+1) + } + if n-2 >= 2*limit { + ans += 3 * comb2(n-2*limit) + } + return ans +} +``` +### **TypeScript** + +```ts +function distributeCandies(n: number, limit: number): number { + const comb2 = (n: number) => (n * (n - 1)) / 2; + if (n > 3 * limit) { + return 0; + } + let ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/README_EN.md b/solution/2900-2999/2928.Distribute Candies Among Children I/README_EN.md index 8e564d72fcf1c..c17c8a685f696 100644 --- a/solution/2900-2999/2928.Distribute Candies Among Children I/README_EN.md +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/README_EN.md @@ -35,30 +35,119 @@ ## Solutions +**Solution 1: Combinatorial Mathematics + Principle of Inclusion-Exclusion** + +According to the problem description, we need to distribute $n$ candies to $3$ children, with each child receiving between $[0, limit]$ candies. + +This is equivalent to placing $n$ balls into $3$ boxes. Since the boxes can be empty, we can add $3$ virtual balls, and then use the method of inserting partitions, i.e., there are a total of $n + 3$ balls, and we insert $2$ partitions among the $n + 3 - 1$ positions, thus dividing the actual $n$ balls into $3$ groups, and allowing the boxes to be empty. Therefore, the initial number of schemes is $C_{n + 2}^2$. + +We need to exclude the schemes where the number of balls in a box exceeds $limit$. Consider that there is a box where the number of balls exceeds $limit$, then the remaining balls (including virtual balls) have at most $n + 3 - (limit + 1) = n - limit + 2$, and the number of positions is $n - limit + 1$, so the number of schemes is $C_{n - limit + 1}^2$. Since there are $3$ boxes, the number of such schemes is $3 \times C_{n - limit + 1}^2$. In this way, we will exclude too many schemes where the number of balls in two boxes exceeds $limit$ at the same time, so we need to add the number of such schemes, i.e., $3 \times C_{n - 2 \times limit}^2$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def distributeCandies(self, n: int, limit: int) -> int: + if n > 3 * limit: + return 0 + ans = comb(n + 2, 2) + if n > limit: + ans -= 3 * comb(n - limit + 1, 2) + if n - 2 >= 2 * limit: + ans += 3 * comb(n - 2 * limit, 2) + return ans ``` ### **Java** ```java - +class Solution { + public int distributeCandies(int n, int limit) { + if (n > 3 * limit) { + return 0; + } + long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return (int) ans; + } + + private long comb2(int n) { + return 1L * n * (n - 1) / 2; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int distributeCandies(int n, int limit) { + auto comb2 = [](int n) { + return 1LL * n * (n - 1) / 2; + }; + if (n > 3 * limit) { + return 0; + } + long long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } +}; ``` ### **Go** ```go +func distributeCandies(n int, limit int) int { + comb2 := func(n int) int { + return n * (n - 1) / 2 + } + if n > 3*limit { + return 0 + } + ans := comb2(n + 2) + if n > limit { + ans -= 3 * comb2(n-limit+1) + } + if n-2 >= 2*limit { + ans += 3 * comb2(n-2*limit) + } + return ans +} +``` +### **TypeScript** + +```ts +function distributeCandies(n: number, limit: number): number { + const comb2 = (n: number) => (n * (n - 1)) / 2; + if (n > 3 * limit) { + return 0; + } + let ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.cpp b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.cpp new file mode 100644 index 0000000000000..d73e4ecc8dff0 --- /dev/null +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int distributeCandies(int n, int limit) { + auto comb2 = [](int n) { + return 1LL * n * (n - 1) / 2; + }; + if (n > 3 * limit) { + return 0; + } + long long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.go b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.go new file mode 100644 index 0000000000000..f3fbaaa67c026 --- /dev/null +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.go @@ -0,0 +1,16 @@ +func distributeCandies(n int, limit int) int { + comb2 := func(n int) int { + return n * (n - 1) / 2 + } + if n > 3*limit { + return 0 + } + ans := comb2(n + 2) + if n > limit { + ans -= 3 * comb2(n-limit+1) + } + if n-2 >= 2*limit { + ans += 3 * comb2(n-2*limit) + } + return ans +} \ No newline at end of file diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.java b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.java new file mode 100644 index 0000000000000..82637dd5dee84 --- /dev/null +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int distributeCandies(int n, int limit) { + if (n > 3 * limit) { + return 0; + } + long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return (int) ans; + } + + private long comb2(int n) { + return 1L * n * (n - 1) / 2; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.py b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.py new file mode 100644 index 0000000000000..f08cfa1c4f041 --- /dev/null +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def distributeCandies(self, n: int, limit: int) -> int: + if n > 3 * limit: + return 0 + ans = comb(n + 2, 2) + if n > limit: + ans -= 3 * comb(n - limit + 1, 2) + if n - 2 >= 2 * limit: + ans += 3 * comb(n - 2 * limit, 2) + return ans diff --git a/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.ts b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.ts new file mode 100644 index 0000000000000..39744f07459a0 --- /dev/null +++ b/solution/2900-2999/2928.Distribute Candies Among Children I/Solution.ts @@ -0,0 +1,14 @@ +function distributeCandies(n: number, limit: number): number { + const comb2 = (n: number) => (n * (n - 1)) / 2; + if (n > 3 * limit) { + return 0; + } + let ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; +} diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/README.md b/solution/2900-2999/2929.Distribute Candies Among Children II/README.md index 890445e82df5e..3423f948ca031 100644 --- a/solution/2900-2999/2929.Distribute Candies Among Children II/README.md +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/README.md @@ -41,6 +41,16 @@ +**方法一:组合数学 + 容斥原理** + +根据题目描述,我们需要将 $n$ 个糖果分给 $3$ 个小孩,每个小孩分到的糖果数在 $[0, limit]$ 之间。 + +这实际上等价于把 $n$ 个球放入 $3$ 个盒子中。由于盒子可以为空,我们可以再增加 $3$ 个虚拟球,然后再利用隔板法,即一共有 $n + 3$ 个球,我们在其中 $n + 3 - 1$ 个位置插入 $2$ 个隔板,从而将实际的 $n$ 个球分成 $3$ 组,并且允许盒子为空,因此初始方案数为 $C_{n + 2}^2$。 + +我们需要在这些方案中,排除掉存在盒子分到的小球数超过 $limit$ 的方案。考虑其中有一个盒子分到的小球数超过 $limit$,那么剩下的球(包括虚拟球)最多有 $n + 3 - (limit + 1) = n - limit + 2$ 个,位置数为 $n - limit + 1$,因此方案数为 $C_{n - limit + 1}^2$。由于存在 $3$ 个盒子,因此这样的方案数为 $3 \times C_{n - limit + 1}^2$。这样子算,我们会多排除掉同时存在两个盒子分到的小球数超过 $limit$ 的方案,因此我们需要再加上这样的方案数,即 $3 \times C_{n - 2 \times limit}^2$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 + ### **Python3** @@ -48,7 +58,16 @@ ```python - +class Solution: + def distributeCandies(self, n: int, limit: int) -> int: + if n > 3 * limit: + return 0 + ans = comb(n + 2, 2) + if n > limit: + ans -= 3 * comb(n - limit + 1, 2) + if n - 2 >= 2 * limit: + ans += 3 * comb(n - 2 * limit, 2) + return ans ``` ### **Java** @@ -56,19 +75,89 @@ ```java - +class Solution { + public long distributeCandies(int n, int limit) { + if (n > 3 * limit) { + return 0; + } + long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } + + private long comb2(int n) { + return 1L * n * (n - 1) / 2; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + long long distributeCandies(int n, int limit) { + auto comb2 = [](int n) { + return 1LL * n * (n - 1) / 2; + }; + if (n > 3 * limit) { + return 0; + } + long long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } +}; ``` ### **Go** ```go +func distributeCandies(n int, limit int) int64 { + comb2 := func(n int) int { + return n * (n - 1) / 2 + } + if n > 3*limit { + return 0 + } + ans := comb2(n+2) + if n > limit { + ans -= 3 * comb2(n-limit+1) + } + if n-2 >= 2*limit { + ans += 3 * comb2(n-2*limit) + } + return int64(ans) +} +``` +### **TypeScript** + +```ts +function distributeCandies(n: number, limit: number): number { + const comb2 = (n: number) => (n * (n - 1)) / 2; + if (n > 3 * limit) { + return 0; + } + let ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/README_EN.md b/solution/2900-2999/2929.Distribute Candies Among Children II/README_EN.md index dde2b652379a8..2aea8b3a40a1a 100644 --- a/solution/2900-2999/2929.Distribute Candies Among Children II/README_EN.md +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/README_EN.md @@ -35,30 +35,119 @@ ## Solutions +**Solution 1: Combinatorial Mathematics + Principle of Inclusion-Exclusion** + +According to the problem description, we need to distribute $n$ candies to $3$ children, with each child receiving between $[0, limit]$ candies. + +This is equivalent to placing $n$ balls into $3$ boxes. Since the boxes can be empty, we can add $3$ virtual balls, and then use the method of inserting partitions, i.e., there are a total of $n + 3$ balls, and we insert $2$ partitions among the $n + 3 - 1$ positions, thus dividing the actual $n$ balls into $3$ groups, and allowing the boxes to be empty. Therefore, the initial number of schemes is $C_{n + 2}^2$. + +We need to exclude the schemes where the number of balls in a box exceeds $limit$. Consider that there is a box where the number of balls exceeds $limit$, then the remaining balls (including virtual balls) have at most $n + 3 - (limit + 1) = n - limit + 2$, and the number of positions is $n - limit + 1$, so the number of schemes is $C_{n - limit + 1}^2$. Since there are $3$ boxes, the number of such schemes is $3 \times C_{n - limit + 1}^2$. In this way, we will exclude too many schemes where the number of balls in two boxes exceeds $limit$ at the same time, so we need to add the number of such schemes, i.e., $3 \times C_{n - 2 \times limit}^2$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def distributeCandies(self, n: int, limit: int) -> int: + if n > 3 * limit: + return 0 + ans = comb(n + 2, 2) + if n > limit: + ans -= 3 * comb(n - limit + 1, 2) + if n - 2 >= 2 * limit: + ans += 3 * comb(n - 2 * limit, 2) + return ans ``` ### **Java** ```java - +class Solution { + public long distributeCandies(int n, int limit) { + if (n > 3 * limit) { + return 0; + } + long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } + + private long comb2(int n) { + return 1L * n * (n - 1) / 2; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + long long distributeCandies(int n, int limit) { + auto comb2 = [](int n) { + return 1LL * n * (n - 1) / 2; + }; + if (n > 3 * limit) { + return 0; + } + long long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } +}; ``` ### **Go** ```go +func distributeCandies(n int, limit int) int64 { + comb2 := func(n int) int { + return n * (n - 1) / 2 + } + if n > 3*limit { + return 0 + } + ans := comb2(n+2) + if n > limit { + ans -= 3 * comb2(n-limit+1) + } + if n-2 >= 2*limit { + ans += 3 * comb2(n-2*limit) + } + return int64(ans) +} +``` +### **TypeScript** + +```ts +function distributeCandies(n: number, limit: number): number { + const comb2 = (n: number) => (n * (n - 1)) / 2; + if (n > 3 * limit) { + return 0; + } + let ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.cpp b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.cpp new file mode 100644 index 0000000000000..008d72255bb8e --- /dev/null +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + long long distributeCandies(int n, int limit) { + auto comb2 = [](int n) { + return 1LL * n * (n - 1) / 2; + }; + if (n > 3 * limit) { + return 0; + } + long long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.go b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.go new file mode 100644 index 0000000000000..7705fb2057848 --- /dev/null +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.go @@ -0,0 +1,16 @@ +func distributeCandies(n int, limit int) int64 { + comb2 := func(n int) int { + return n * (n - 1) / 2 + } + if n > 3*limit { + return 0 + } + ans := comb2(n+2) + if n > limit { + ans -= 3 * comb2(n-limit+1) + } + if n-2 >= 2*limit { + ans += 3 * comb2(n-2*limit) + } + return int64(ans) +} \ No newline at end of file diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.java b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.java new file mode 100644 index 0000000000000..68684d6c5f5b5 --- /dev/null +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public long distributeCandies(int n, int limit) { + if (n > 3 * limit) { + return 0; + } + long ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; + } + + private long comb2(int n) { + return 1L * n * (n - 1) / 2; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.py b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.py new file mode 100644 index 0000000000000..f08cfa1c4f041 --- /dev/null +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def distributeCandies(self, n: int, limit: int) -> int: + if n > 3 * limit: + return 0 + ans = comb(n + 2, 2) + if n > limit: + ans -= 3 * comb(n - limit + 1, 2) + if n - 2 >= 2 * limit: + ans += 3 * comb(n - 2 * limit, 2) + return ans diff --git a/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.ts b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.ts new file mode 100644 index 0000000000000..39744f07459a0 --- /dev/null +++ b/solution/2900-2999/2929.Distribute Candies Among Children II/Solution.ts @@ -0,0 +1,14 @@ +function distributeCandies(n: number, limit: number): number { + const comb2 = (n: number) => (n * (n - 1)) / 2; + if (n > 3 * limit) { + return 0; + } + let ans = comb2(n + 2); + if (n > limit) { + ans -= 3 * comb2(n - limit + 1); + } + if (n - 2 >= 2 * limit) { + ans += 3 * comb2(n - 2 * limit); + } + return ans; +}