Statistics

Problem Statement for "RepeatedApplication"

Problem Statement

Given is a positive integer N.

Let S = { 0, 1, ..., N-1 }. All functions in this problem are functions defined on the set S with values from the set S.


Let f[t](x) denote the function f applied t times to the input x. In other words, f[1](x) is simply f(x), f[2](x) is f(f(x)), and so on.

Formally, f[0](x) = x and for all t, f[t+1](x) = f( f[t](x) ).


In addition to N, you are given another positive integer K. We are now interested in functions with the following property:

for all x in S: x + f[K](x) = N - 1


Count all such functions. Return their count modulo 10^9 + 7.

Definition

Class:
RepeatedApplication
Method:
count
Parameters:
int, int
Returns:
int
Method signature:
int count(int N, int K)
(be sure your method is public)

Notes

  • The total number of functions on S is finite, so the return value is always well-defined.

Constraints

  • N will be between 1 and 2,000, inclusive.
  • K will be between 1 and 2,000, inclusive.

Examples

  1. 42

    1

    Returns: 1

    There is clearly exactly one function f such that for all x from 0 to 41, inclusive, x + f(x) = 41.

  2. 2

    4

    Returns: 0

    For N = 2 we have S = {0, 1}. There are only four functions on this S: we have two options for the value of f(0) and two for the value of f(1). We can easily verify that none of the four functions have the desired property.

  3. 5

    2

    Returns: 2

    One of the two functions that have the desired property has the following values: f(0)=1, f(1)=4, f(2)=2, f(3)=0, and f(4)=3. We can easily verify that: 0 + f(f(0)) = 0 + f(1) = 0 + 4 = 4. 1 + f(f(1)) = 1 + f(4) = 1 + 3 = 4. 2 + f(f(2)) = 2 + f(2) = 2 + 2 = 4. 3 + f(f(3)) = 3 + f(0) = 3 + 1 = 4. 4 + f(f(4)) = 4 + f(3) = 4 + 0 = 4.

  4. 7

    4

    Returns: 0

  5. 8

    4

    Returns: 48

  6. 1

    1

    Returns: 1

  7. 1

    2

    Returns: 1

  8. 1

    3

    Returns: 1

  9. 1

    4

    Returns: 1

  10. 1

    5

    Returns: 1

  11. 1

    6

    Returns: 1

  12. 2

    1

    Returns: 1

  13. 2

    2

    Returns: 0

  14. 2

    3

    Returns: 1

  15. 2

    4

    Returns: 0

  16. 2

    5

    Returns: 1

  17. 2

    6

    Returns: 0

  18. 2

    63

    Returns: 1

  19. 3

    1

    Returns: 1

  20. 3

    2

    Returns: 0

  21. 3

    3

    Returns: 1

  22. 3

    4

    Returns: 0

  23. 3

    5

    Returns: 1

  24. 3

    6

    Returns: 0

  25. 3

    8

    Returns: 0

  26. 3

    1069

    Returns: 1

  27. 4

    1

    Returns: 1

  28. 4

    2

    Returns: 2

  29. 4

    3

    Returns: 1

  30. 4

    4

    Returns: 0

  31. 4

    5

    Returns: 1

  32. 4

    6

    Returns: 2

  33. 5

    1

    Returns: 1

  34. 5

    2

    Returns: 2

  35. 5

    3

    Returns: 1

  36. 5

    4

    Returns: 0

  37. 5

    5

    Returns: 1

  38. 5

    6

    Returns: 2

  39. 6

    1

    Returns: 1

  40. 6

    2

    Returns: 0

  41. 6

    3

    Returns: 9

  42. 6

    4

    Returns: 0

  43. 6

    5

    Returns: 1

  44. 6

    6

    Returns: 0

  45. 9

    363

    Returns: 33

  46. 12

    14

    Returns: 120

  47. 13

    11

    Returns: 1

  48. 14

    1012

    Returns: 0

  49. 15

    4

    Returns: 0

  50. 26

    2

    Returns: 0

  51. 31

    1

    Returns: 1

  52. 37

    6

    Returns: 935053234

  53. 59

    1071

    Returns: 829362404

  54. 80

    10

    Returns: 986227204

  55. 108

    62

    Returns: 607650842

  56. 127

    5

    Returns: 113322464

  57. 137

    26

    Returns: 637003322

  58. 166

    1130

    Returns: 0

  59. 188

    3

    Returns: 762514731

  60. 369

    112

    Returns: 0

  61. 453

    8

    Returns: 0

  62. 502

    1899

    Returns: 854848701

  63. 553

    503

    Returns: 1

  64. 616

    638

    Returns: 358276785

  65. 726

    1136

    Returns: 0

  66. 1013

    218

    Returns: 542825594

  67. 1365

    268

    Returns: 0

  68. 1471

    756

    Returns: 0

  69. 1900

    7

    Returns: 600064923

  70. 1900

    14

    Returns: 29545310

  71. 1909

    31

    Returns: 670689160

  72. 1910

    1976

    Returns: 0

  73. 1913

    7

    Returns: 864906344

  74. 1928

    1966

    Returns: 526159502

  75. 1942

    12

    Returns: 0

  76. 1945

    1903

    Returns: 257515141

  77. 1950

    1979

    Returns: 1

  78. 1952

    2

    Returns: 800294781

  79. 1952

    1837

    Returns: 241772777

  80. 1959

    57

    Returns: 589988788

  81. 1962

    1911

    Returns: 770343363

  82. 1977

    43

    Returns: 905566222

  83. 1979

    7

    Returns: 347088868

  84. 9

    17

    Returns: 1


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: