Skip to main content

Cluster Process Scheduling

easy
Software engineer

You are a scheduler for a computing cluster. You need to assign nProcesses distinct processes to nIntervals available time slots.

A valid schedule must follow one critical rule: no single process can be assigned to two consecutive time slots.

Your task is to calculate the total number of valid scheduling arrangements. Since the answer can be very large, return the result modulo $10⁹ + 7$.

Example 1

Input

nProcesses = 2, nIntervals = 3

Output

2

Explanation

Let the processes be A and B. The possible schedules for 3 slots are:
- (A, B, A)
- (B, A, B)
Schedules like (A, A, B) are invalid because process A appears in consecutive slots.

Example 2

Input

nProcesses = 3, nIntervals = 2

Output

6

Explanation

With 3 processes (A, B, C) and 2 slots, any pair where the first and second processes are different is valid: (A,B), (A,C), (B,A), (B,C), (C,A), (C,B).

Example 3

Input

nProcesses = 1, nIntervals = 2

Output

0

Explanation

With only 1 process, it must be used in the first slot. To fill the second slot, we would have to use the same process again, which violates the consecutive rule. Thus, 0 valid schedules.

Constraints

  • 1 <= nIntervals <= 10⁹
  • 1 <= nProcesses <= 10⁹
  • Result should be modulo $10⁹ + 7$.

OA

CountingDynamic ProgrammingMath
Language
Code editor loads in the browser.

Output

Input

2
3

Expected

2

Your output

Run to see your output.