|
35 | 35 |
|
36 | 36 | ## Solutions
|
37 | 37 |
|
| 38 | +**Solution 1: Combinatorial Mathematics + Principle of Inclusion-Exclusion** |
| 39 | + |
| 40 | +According to the problem description, we need to distribute $n$ candies to $3$ children, with each child receiving between $[0, limit]$ candies. |
| 41 | + |
| 42 | +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$. |
| 43 | + |
| 44 | +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$. |
| 45 | + |
| 46 | +The time complexity is $O(1)$, and the space complexity is $O(1)$. |
| 47 | + |
38 | 48 | <!-- tabs:start -->
|
39 | 49 |
|
40 | 50 | ### **Python3**
|
41 | 51 |
|
42 | 52 | ```python
|
43 |
| - |
| 53 | +class Solution: |
| 54 | + def distributeCandies(self, n: int, limit: int) -> int: |
| 55 | + if n > 3 * limit: |
| 56 | + return 0 |
| 57 | + ans = comb(n + 2, 2) |
| 58 | + if n > limit: |
| 59 | + ans -= 3 * comb(n - limit + 1, 2) |
| 60 | + if n - 2 >= 2 * limit: |
| 61 | + ans += 3 * comb(n - 2 * limit, 2) |
| 62 | + return ans |
44 | 63 | ```
|
45 | 64 |
|
46 | 65 | ### **Java**
|
47 | 66 |
|
48 | 67 | ```java
|
49 |
| - |
| 68 | +class Solution { |
| 69 | + public int distributeCandies(int n, int limit) { |
| 70 | + if (n > 3 * limit) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + long ans = comb2(n + 2); |
| 74 | + if (n > limit) { |
| 75 | + ans -= 3 * comb2(n - limit + 1); |
| 76 | + } |
| 77 | + if (n - 2 >= 2 * limit) { |
| 78 | + ans += 3 * comb2(n - 2 * limit); |
| 79 | + } |
| 80 | + return (int) ans; |
| 81 | + } |
| 82 | + |
| 83 | + private long comb2(int n) { |
| 84 | + return 1L * n * (n - 1) / 2; |
| 85 | + } |
| 86 | +} |
50 | 87 | ```
|
51 | 88 |
|
52 | 89 | ### **C++**
|
53 | 90 |
|
54 | 91 | ```cpp
|
55 |
| - |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + int distributeCandies(int n, int limit) { |
| 95 | + auto comb2 = [](int n) { |
| 96 | + return 1LL * n * (n - 1) / 2; |
| 97 | + }; |
| 98 | + if (n > 3 * limit) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + long long ans = comb2(n + 2); |
| 102 | + if (n > limit) { |
| 103 | + ans -= 3 * comb2(n - limit + 1); |
| 104 | + } |
| 105 | + if (n - 2 >= 2 * limit) { |
| 106 | + ans += 3 * comb2(n - 2 * limit); |
| 107 | + } |
| 108 | + return ans; |
| 109 | + } |
| 110 | +}; |
56 | 111 | ```
|
57 | 112 |
|
58 | 113 | ### **Go**
|
59 | 114 |
|
60 | 115 | ```go
|
| 116 | +func distributeCandies(n int, limit int) int { |
| 117 | + comb2 := func(n int) int { |
| 118 | + return n * (n - 1) / 2 |
| 119 | + } |
| 120 | + if n > 3*limit { |
| 121 | + return 0 |
| 122 | + } |
| 123 | + ans := comb2(n + 2) |
| 124 | + if n > limit { |
| 125 | + ans -= 3 * comb2(n-limit+1) |
| 126 | + } |
| 127 | + if n-2 >= 2*limit { |
| 128 | + ans += 3 * comb2(n-2*limit) |
| 129 | + } |
| 130 | + return ans |
| 131 | +} |
| 132 | +``` |
61 | 133 |
|
| 134 | +### **TypeScript** |
| 135 | + |
| 136 | +```ts |
| 137 | +function distributeCandies(n: number, limit: number): number { |
| 138 | + const comb2 = (n: number) => (n * (n - 1)) / 2; |
| 139 | + if (n > 3 * limit) { |
| 140 | + return 0; |
| 141 | + } |
| 142 | + let ans = comb2(n + 2); |
| 143 | + if (n > limit) { |
| 144 | + ans -= 3 * comb2(n - limit + 1); |
| 145 | + } |
| 146 | + if (n - 2 >= 2 * limit) { |
| 147 | + ans += 3 * comb2(n - 2 * limit); |
| 148 | + } |
| 149 | + return ans; |
| 150 | +} |
62 | 151 | ```
|
63 | 152 |
|
64 | 153 | ### **...**
|
|
0 commit comments