Statistics

Problem Statement for "Shoelaces"

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

  1. 3

    3

    Returns: 6

  2. 3

    2

    Returns: 5

    The example above.

  3. 5

    3

    Returns: 59

  4. 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.

  5. 15

    1

    Returns: 1

  6. 20

    7

    Returns: 33246459524

  7. 30

    28

    Returns: 30067266499540954

  8. 30

    30

    Returns: 30067266499541040

  9. 30

    29

    Returns: 30067266499541039

  10. 17

    4

    Returns: 276838263

  11. 19

    14

    Returns: 9075080565

  12. 27

    9

    Returns: 487032895440438

  13. 27

    30

    Returns: 495918532948104

  14. 27

    28

    Returns: 495918532948104

  15. 1

    1

    Returns: 1

  16. 30

    1

    Returns: 1

  17. 24

    17

    Returns: 8233425743544

  18. 13

    3

    Returns: 680575

  19. 25

    19

    Returns: 32247602583925

  20. 2

    2

    Returns: 2

  21. 26

    9

    Returns: 124340849989989

  22. 16

    1

    Returns: 1

  23. 16

    2

    Returns: 774226

  24. 16

    3

    Returns: 24069217

  25. 16

    4

    Returns: 76643283

  26. 16

    5

    Returns: 118495050

  27. 16

    6

    Returns: 140363782

  28. 16

    7

    Returns: 149782021

  29. 16

    8

    Returns: 153376344

  30. 16

    9

    Returns: 154609980

  31. 16

    10

    Returns: 154988116

  32. 16

    11

    Returns: 155089624

  33. 16

    12

    Returns: 155112708

  34. 16

    13

    Returns: 155116918

  35. 16

    14

    Returns: 155117476

  36. 16

    15

    Returns: 155117519

  37. 16

    16

    Returns: 155117520

  38. 16

    17

    Returns: 155117520


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: