Skip to content

Commit cabdeee

Browse files
authored
feat: add solutions to lc problem: No.3317 (#3650)
No.3317.Find the Number of Possible Ways for an Event
1 parent 51a32cf commit cabdeee

File tree

7 files changed

+314
-8
lines changed

7 files changed

+314
-8
lines changed

solution/3300-3399/3317.Find the Number of Possible Ways for an Event/README.md

+114-4
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,142 @@ tags:
9090

9191
<!-- solution:start -->
9292

93-
### 方法一
93+
### 方法一:动态规划
94+
95+
我们定义 $f[i][j]$ 表示前 $i$ 个表演者安排到 $j$ 个节目的方案数。初始时 $f[0][0] = 1$,其余 $f[i][j] = 0$。
96+
97+
对于 $f[i][j]$,其中 $1 \leq i \leq n$, $1 \leq j \leq x$,我们考虑第 $i$ 个表演者:
98+
99+
- 如果被安排到了一个已经有表演者的节目,一共有 $j$ 种选择,即 $f[i - 1][j] \times j$;
100+
- 如果被安排到了一个没有表演者的节目,一共有 $x - (j - 1)$ 种选择,即 $f[i - 1][j - 1] \times (x - (j - 1))$。
101+
102+
所以状态转移方程为:
103+
104+
$$
105+
f[i][j] = f[i - 1][j] \times j + f[i - 1][j - 1] \times (x - (j - 1))
106+
$$
107+
108+
对于每个 $j$,一共有 $y^j$ 种选择,所以最终答案为:
109+
110+
$$
111+
\sum_{j = 1}^{x} f[n][j] \times y^j
112+
$$
113+
114+
注意,由于答案可能很大,我们需要对 $10^9 + 7$ 取模。
115+
116+
时间复杂度 $O(n \times x)$,空间复杂度 $O(n \times x)$。其中 $n$ 和 $x$ 分别为表演者的数量和节目的数量。
94117

95118
<!-- tabs:start -->
96119

97120
#### Python3
98121

99122
```python
100-
123+
class Solution:
124+
def numberOfWays(self, n: int, x: int, y: int) -> int:
125+
mod = 10**9 + 7
126+
f = [[0] * (x + 1) for _ in range(n + 1)]
127+
f[0][0] = 1
128+
for i in range(1, n + 1):
129+
for j in range(1, x + 1):
130+
f[i][j] = (f[i - 1][j] * j + f[i - 1][j - 1] * (x - (j - 1))) % mod
131+
ans, p = 0, 1
132+
for j in range(1, x + 1):
133+
p = p * y % mod
134+
ans = (ans + f[n][j] * p) % mod
135+
return ans
101136
```
102137

103138
#### Java
104139

105140
```java
106-
141+
class Solution {
142+
public int numberOfWays(int n, int x, int y) {
143+
final int mod = (int) 1e9 + 7;
144+
long[][] f = new long[n + 1][x + 1];
145+
f[0][0] = 1;
146+
for (int i = 1; i <= n; ++i) {
147+
for (int j = 1; j <= x; ++j) {
148+
f[i][j] = (f[i - 1][j] * j % mod + f[i - 1][j - 1] * (x - (j - 1) % mod)) % mod;
149+
}
150+
}
151+
long ans = 0, p = 1;
152+
for (int j = 1; j <= x; ++j) {
153+
p = p * y % mod;
154+
ans = (ans + f[n][j] * p) % mod;
155+
}
156+
return (int) ans;
157+
}
158+
}
107159
```
108160

109161
#### C++
110162

111163
```cpp
112-
164+
class Solution {
165+
public:
166+
int numberOfWays(int n, int x, int y) {
167+
const int mod = 1e9 + 7;
168+
long long f[n + 1][x + 1];
169+
memset(f, 0, sizeof(f));
170+
f[0][0] = 1;
171+
for (int i = 1; i <= n; ++i) {
172+
for (int j = 1; j <= x; ++j) {
173+
f[i][j] = (f[i - 1][j] * j % mod + f[i - 1][j - 1] * (x - (j - 1) % mod)) % mod;
174+
}
175+
}
176+
long long ans = 0, p = 1;
177+
for (int j = 1; j <= x; ++j) {
178+
p = p * y % mod;
179+
ans = (ans + f[n][j] * p) % mod;
180+
}
181+
return ans;
182+
}
183+
};
113184
```
114185
115186
#### Go
116187
117188
```go
189+
func numberOfWays(n int, x int, y int) int {
190+
const mod int = 1e9 + 7
191+
f := make([][]int, n+1)
192+
for i := range f {
193+
f[i] = make([]int, x+1)
194+
}
195+
f[0][0] = 1
196+
for i := 1; i <= n; i++ {
197+
for j := 1; j <= x; j++ {
198+
f[i][j] = (f[i-1][j]*j%mod + f[i-1][j-1]*(x-(j-1))%mod) % mod
199+
}
200+
}
201+
ans, p := 0, 1
202+
for j := 1; j <= x; j++ {
203+
p = p * y % mod
204+
ans = (ans + f[n][j]*p%mod) % mod
205+
}
206+
return ans
207+
}
208+
```
118209

210+
#### TypeScript
211+
212+
```ts
213+
function numberOfWays(n: number, x: number, y: number): number {
214+
const mod = BigInt(10 ** 9 + 7);
215+
const f: bigint[][] = Array.from({ length: n + 1 }, () => Array(x + 1).fill(0n));
216+
f[0][0] = 1n;
217+
for (let i = 1; i <= n; ++i) {
218+
for (let j = 1; j <= x; ++j) {
219+
f[i][j] = (f[i - 1][j] * BigInt(j) + f[i - 1][j - 1] * BigInt(x - (j - 1))) % mod;
220+
}
221+
}
222+
let [ans, p] = [0n, 1n];
223+
for (let j = 1; j <= x; ++j) {
224+
p = (p * BigInt(y)) % mod;
225+
ans = (ans + f[n][j] * p) % mod;
226+
}
227+
return Number(ans);
228+
}
119229
```
120230

121231
<!-- tabs:end -->

solution/3300-3399/3317.Find the Number of Possible Ways for an Event/README_EN.md

+114-4
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,142 @@ tags:
8787

8888
<!-- solution:start -->
8989

90-
### Solution 1
90+
### Solution 1: Dynamic Programming
91+
92+
We define $f[i][j]$ to represent the number of ways to arrange the first $i$ performers into $j$ programs. Initially, $f[0][0] = 1$, and the rest $f[i][j] = 0$.
93+
94+
For $f[i][j]$, where $1 \leq i \leq n$ and $1 \leq j \leq x$, we consider the $i$-th performer:
95+
96+
- If the performer is assigned to a program that already has performers, there are $j$ choices, i.e., $f[i - 1][j] \times j$;
97+
- If the performer is assigned to a program that has no performers, there are $x - (j - 1)$ choices, i.e., $f[i - 1][j - 1] \times (x - (j - 1))$.
98+
99+
So the state transition equation is:
100+
101+
$$
102+
f[i][j] = f[i - 1][j] \times j + f[i - 1][j - 1] \times (x - (j - 1))
103+
$$
104+
105+
For each $j$, there are $y^j$ choices, so the final answer is:
106+
107+
$$
108+
\sum_{j = 1}^{x} f[n][j] \times y^j
109+
$$
110+
111+
Note that since the answer can be very large, we need to take the modulo $10^9 + 7$.
112+
113+
The time complexity is $O(n \times x)$, and the space complexity is $O(n \times x)$. Here, $n$ and $x$ represent the number of performers and the number of programs, respectively.
91114

92115
<!-- tabs:start -->
93116

94117
#### Python3
95118

96119
```python
97-
120+
class Solution:
121+
def numberOfWays(self, n: int, x: int, y: int) -> int:
122+
mod = 10**9 + 7
123+
f = [[0] * (x + 1) for _ in range(n + 1)]
124+
f[0][0] = 1
125+
for i in range(1, n + 1):
126+
for j in range(1, x + 1):
127+
f[i][j] = (f[i - 1][j] * j + f[i - 1][j - 1] * (x - (j - 1))) % mod
128+
ans, p = 0, 1
129+
for j in range(1, x + 1):
130+
p = p * y % mod
131+
ans = (ans + f[n][j] * p) % mod
132+
return ans
98133
```
99134

100135
#### Java
101136

102137
```java
103-
138+
class Solution {
139+
public int numberOfWays(int n, int x, int y) {
140+
final int mod = (int) 1e9 + 7;
141+
long[][] f = new long[n + 1][x + 1];
142+
f[0][0] = 1;
143+
for (int i = 1; i <= n; ++i) {
144+
for (int j = 1; j <= x; ++j) {
145+
f[i][j] = (f[i - 1][j] * j % mod + f[i - 1][j - 1] * (x - (j - 1) % mod)) % mod;
146+
}
147+
}
148+
long ans = 0, p = 1;
149+
for (int j = 1; j <= x; ++j) {
150+
p = p * y % mod;
151+
ans = (ans + f[n][j] * p) % mod;
152+
}
153+
return (int) ans;
154+
}
155+
}
104156
```
105157

106158
#### C++
107159

108160
```cpp
109-
161+
class Solution {
162+
public:
163+
int numberOfWays(int n, int x, int y) {
164+
const int mod = 1e9 + 7;
165+
long long f[n + 1][x + 1];
166+
memset(f, 0, sizeof(f));
167+
f[0][0] = 1;
168+
for (int i = 1; i <= n; ++i) {
169+
for (int j = 1; j <= x; ++j) {
170+
f[i][j] = (f[i - 1][j] * j % mod + f[i - 1][j - 1] * (x - (j - 1) % mod)) % mod;
171+
}
172+
}
173+
long long ans = 0, p = 1;
174+
for (int j = 1; j <= x; ++j) {
175+
p = p * y % mod;
176+
ans = (ans + f[n][j] * p) % mod;
177+
}
178+
return ans;
179+
}
180+
};
110181
```
111182
112183
#### Go
113184
114185
```go
186+
func numberOfWays(n int, x int, y int) int {
187+
const mod int = 1e9 + 7
188+
f := make([][]int, n+1)
189+
for i := range f {
190+
f[i] = make([]int, x+1)
191+
}
192+
f[0][0] = 1
193+
for i := 1; i <= n; i++ {
194+
for j := 1; j <= x; j++ {
195+
f[i][j] = (f[i-1][j]*j%mod + f[i-1][j-1]*(x-(j-1))%mod) % mod
196+
}
197+
}
198+
ans, p := 0, 1
199+
for j := 1; j <= x; j++ {
200+
p = p * y % mod
201+
ans = (ans + f[n][j]*p%mod) % mod
202+
}
203+
return ans
204+
}
205+
```
115206

207+
#### TypeScript
208+
209+
```ts
210+
function numberOfWays(n: number, x: number, y: number): number {
211+
const mod = BigInt(10 ** 9 + 7);
212+
const f: bigint[][] = Array.from({ length: n + 1 }, () => Array(x + 1).fill(0n));
213+
f[0][0] = 1n;
214+
for (let i = 1; i <= n; ++i) {
215+
for (let j = 1; j <= x; ++j) {
216+
f[i][j] = (f[i - 1][j] * BigInt(j) + f[i - 1][j - 1] * BigInt(x - (j - 1))) % mod;
217+
}
218+
}
219+
let [ans, p] = [0n, 1n];
220+
for (let j = 1; j <= x; ++j) {
221+
p = (p * BigInt(y)) % mod;
222+
ans = (ans + f[n][j] * p) % mod;
223+
}
224+
return Number(ans);
225+
}
116226
```
117227

118228
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int numberOfWays(int n, int x, int y) {
4+
const int mod = 1e9 + 7;
5+
long long f[n + 1][x + 1];
6+
memset(f, 0, sizeof(f));
7+
f[0][0] = 1;
8+
for (int i = 1; i <= n; ++i) {
9+
for (int j = 1; j <= x; ++j) {
10+
f[i][j] = (f[i - 1][j] * j % mod + f[i - 1][j - 1] * (x - (j - 1) % mod)) % mod;
11+
}
12+
}
13+
long long ans = 0, p = 1;
14+
for (int j = 1; j <= x; ++j) {
15+
p = p * y % mod;
16+
ans = (ans + f[n][j] * p) % mod;
17+
}
18+
return ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func numberOfWays(n int, x int, y int) int {
2+
const mod int = 1e9 + 7
3+
f := make([][]int, n+1)
4+
for i := range f {
5+
f[i] = make([]int, x+1)
6+
}
7+
f[0][0] = 1
8+
for i := 1; i <= n; i++ {
9+
for j := 1; j <= x; j++ {
10+
f[i][j] = (f[i-1][j]*j%mod + f[i-1][j-1]*(x-(j-1))%mod) % mod
11+
}
12+
}
13+
ans, p := 0, 1
14+
for j := 1; j <= x; j++ {
15+
p = p * y % mod
16+
ans = (ans + f[n][j]*p%mod) % mod
17+
}
18+
return ans
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int numberOfWays(int n, int x, int y) {
3+
final int mod = (int) 1e9 + 7;
4+
long[][] f = new long[n + 1][x + 1];
5+
f[0][0] = 1;
6+
for (int i = 1; i <= n; ++i) {
7+
for (int j = 1; j <= x; ++j) {
8+
f[i][j] = (f[i - 1][j] * j % mod + f[i - 1][j - 1] * (x - (j - 1) % mod)) % mod;
9+
}
10+
}
11+
long ans = 0, p = 1;
12+
for (int j = 1; j <= x; ++j) {
13+
p = p * y % mod;
14+
ans = (ans + f[n][j] * p) % mod;
15+
}
16+
return (int) ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numberOfWays(self, n: int, x: int, y: int) -> int:
3+
mod = 10**9 + 7
4+
f = [[0] * (x + 1) for _ in range(n + 1)]
5+
f[0][0] = 1
6+
for i in range(1, n + 1):
7+
for j in range(1, x + 1):
8+
f[i][j] = (f[i - 1][j] * j + f[i - 1][j - 1] * (x - (j - 1))) % mod
9+
ans, p = 0, 1
10+
for j in range(1, x + 1):
11+
p = p * y % mod
12+
ans = (ans + f[n][j] * p) % mod
13+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function numberOfWays(n: number, x: number, y: number): number {
2+
const mod = BigInt(10 ** 9 + 7);
3+
const f: bigint[][] = Array.from({ length: n + 1 }, () => Array(x + 1).fill(0n));
4+
f[0][0] = 1n;
5+
for (let i = 1; i <= n; ++i) {
6+
for (let j = 1; j <= x; ++j) {
7+
f[i][j] = (f[i - 1][j] * BigInt(j) + f[i - 1][j - 1] * BigInt(x - (j - 1))) % mod;
8+
}
9+
}
10+
let [ans, p] = [0n, 1n];
11+
for (let j = 1; j <= x; ++j) {
12+
p = (p * BigInt(y)) % mod;
13+
ans = (ans + f[n][j] * p) % mod;
14+
}
15+
return Number(ans);
16+
}

0 commit comments

Comments
 (0)