Problem Statement
Concurrency can be simulated on a single processor using the technique of time-slicing, giving each process a chance to run for some small, fixed interval before switching to the next process. For example, two processes might take turns executing for 10 milliseconds each until both have finished. However, switching between processes is expensive, so it is sometimes desirable to allow a process to execute more than one time slice in a row. On the other hand, to preserve the illusion of parallel execution, it is desirable that no process have to wait too long before getting a chance to run. One way to balance these conflicting desires is to set a limit K on the number of time slices a process might wait between turns. More precisely, no process that still needs more time slices will ever wait more than K time slices before getting a chance to run.
Consider two processes, A and B, that both need to run for N time slices. Given N and K, your task is to determine how many ways there are to schedule A and B such that A gets the first time slice and B gets the last time slice. For example, if N=3 and K=3, there are six possible schedules:
AAABBB AABABB AABBAB ABAABB ABABAB ABBAABIf K is reduced to 2, then the AAABBB schedule is eliminated, leaving five possible schedules.
Your task is to write a method that takes two integers, N (the number of time slices needed by each process) and K (the maximum number of time slices an unfinished process is willing to wait),
and returns the number of possible schedules as a
Definition
- Class:
- TimeSlicing
- Method:
- howMany
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long howMany(int N, int K)
- (be sure your method is public)
Constraints
- N is between 1 and 30, inclusive.
- K is between 1 and 30, inclusive.
Examples
3
3
Returns: 6
3
2
Returns: 5
The example above.
5
3
Returns: 59
4
2
Returns: 12
The twelve possible schedules are AABAABBB ABAABABB ABBAABAB AABABABB ABAABBAB ABBABAAB AABABBAB ABABAABB AABBAABB ABABABAB AABBABAB ABABBAAB Notice in the first schedule (AABAABBB) that process B runs for three time slices in a row even though K=2. This is legal because process A has already finished.
15
1
Returns: 1
20
7
Returns: 33246459524
30
28
Returns: 30067266499540954
30
30
Returns: 30067266499541040
30
29
Returns: 30067266499541039
17
4
Returns: 276838263
19
14
Returns: 9075080565
27
9
Returns: 487032895440438
27
30
Returns: 495918532948104
27
28
Returns: 495918532948104
1
1
Returns: 1
30
1
Returns: 1
24
17
Returns: 8233425743544
13
3
Returns: 680575
25
19
Returns: 32247602583925
2
2
Returns: 2
26
9
Returns: 124340849989989
16
1
Returns: 1
16
2
Returns: 774226
16
3
Returns: 24069217
16
4
Returns: 76643283
16
5
Returns: 118495050
16
6
Returns: 140363782
16
7
Returns: 149782021
16
8
Returns: 153376344
16
9
Returns: 154609980
16
10
Returns: 154988116
16
11
Returns: 155089624
16
12
Returns: 155112708
16
13
Returns: 155116918
16
14
Returns: 155117476
16
15
Returns: 155117519
16
16
Returns: 155117520
16
17
Returns: 155117520