Find the Number of Possible Ways for an Event - Problem

You are given three integers n, x, and y. An event is being held for n performers.

When a performer arrives, they are assigned to one of the x stages. All performers assigned to the same stage will perform together as a band, though some stages might remain empty.

After all performances are completed, the jury will award each band a score in the range [1, y].

Return the total number of possible ways the event can take place. Since the answer may be very large, return it modulo 10^9 + 7.

Note that two events are considered to have been held differently if either of the following conditions is satisfied:

  • Any performer is assigned a different stage.
  • Any band is awarded a different score.

Input & Output

Example 1 — Basic Case
$ Input: n = 1, x = 2, y = 3
Output: 6
💡 Note: 1 performer can go to stage 1 or 2 (2 ways). Each stage can get score 1, 2, or 3 (3 ways). Total: 2 × 3 = 6 ways.
Example 2 — Multiple Performers
$ Input: n = 2, x = 2, y = 2
Output: 10
💡 Note: Assignments: (1,1), (1,2), (2,1), (2,2). Only 1 stage used: 2 ways × C(2,1) × 2¹ = 8. Both stages used: 2 ways × C(2,2) × 2² = 8. But we subtract overlaps using inclusion-exclusion to get 10.
Example 3 — Single Stage
$ Input: n = 3, x = 1, y = 4
Output: 4
💡 Note: All 3 performers must go to the single stage (1 way). That stage can be scored 1, 2, 3, or 4 (4 ways). Total: 1 × 4 = 4 ways.

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ x ≤ 1000
  • 1 ≤ y ≤ 109

Visualization

Tap to expand
Find the Number of Possible Ways for an Event INPUT Event Setup P n=1 Performer Stage 1 Stage 2 x=2 Stages 1 2 3 y=3 Possible Scores n = 1, x = 2, y = 3 Input Parameters ALGORITHM STEPS 1 Stage Assignment Each performer picks 1 of x stages 2 Band Formation Performers on same stage = 1 band 3 Score Assignment Each band gets score from [1, y] 4 Calculate Total Multiply choices: stages x scores Direct Formula: Ways = x * y = 2 * 3 = 6 (For n=1: simple case) FINAL RESULT All 6 Possible Combinations: Stage1 + Score1 #1 Stage1 + Score2 #2 Stage1 + Score3 #3 Stage2 + Score1 #4 Stage2 + Score2 #5 Stage2 + Score3 #6 2 stages x 3 scores = 6 ways Output 6 OK - Verified! Total ways modulo 10^9+7 Key Insight: For n=1 performer: The formula simplifies to x * y (stages * scores). General case uses Stirling numbers: Sum over k bands of S(n,k) * P(x,k) * y^k where S is Stirling number of 2nd kind, P is permutation. Result taken modulo 10^9 + 7. TutorialsPoint - Find the Number of Possible Ways for an Event | Direct Mathematical Formula
Asked in
Google 15 Microsoft 12 Amazon 8
8.2K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen