You have p distinct processes and t time slots in a row. A schedule puts one process in each slot (a process may appear many times). It is valid when no process is in two neighbouring slots.
Write count_schedules(p: int, t: int) -> int: the number of valid schedules, modulo 1_000_000_007.
1 <= p <= 10**9,0 <= t <= 10**18. Witht == 0the answer is1(the empty schedule).treaches10**18, so a loop over the slots never finishes.- ⭐ Bonus: a speed test with 2,000 calls at
tnear10**18earns a star in O(log t) per call.
count_schedules(3, 3) # 12 (e.g. 1-2-1 and 3-1-2 are valid, 1-1-2 is not)
count_schedules(2, 4) # 2 (1-2-1-2 and 2-1-2-1)
count_schedules(1, 2) # 0
count_schedules(5, 0) # 1
Discussion
- Why is every row of the slot-by-process table identical, and what does that tell you?
- How does square-and-multiply modular exponentiation work, and where must you reduce modulo
10**9 + 7to avoid overflow in a fixed-width language?
Show hint
How many choices does slot i have once slot i - 1 is fixed? It doesn't depend on which process was chosen.