You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/3300-3399/3317.Find the Number of Possible Ways for an Event/README_EN.md
+114-4
Original file line number
Diff line number
Diff line change
@@ -87,32 +87,142 @@ tags:
87
87
88
88
<!-- solution:start -->
89
89
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))$.
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.
0 commit comments