TopCoder problem "TimeSlicing" used in TCCC '03 Semifinals 3 (Division I Level Three)



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
     ABBAAB
If 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 long (see examples).

 

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

0)
    
3
2
Returns: 5
The example above.
1)
    
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.
2)
    
15
1
Returns: 1
3)
    
20
7
Returns: 33246459524
4)
    
30
28
Returns: 30067266499540954

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1256

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4493&pm=1256

Writer:

vorthys

Testers:

Logan , lbackstrom , brett1479 , schveiguy

Problem categories:

Advanced Math, Dynamic Programming