Statistics

Problem Statement for "BunnyConverter"

Problem Statement

Bunnies like programming and they sometimes make useless devices.

One group of bunnies made a device called Converter. Converter has two fixed integers, n and z, and works as follows:
  1. It receives a card on which an integer x is written. This card will not be returned.
  2. It selects an integer y between 1 and n, inclusive, such that x + y^2 + z^3 is a multiple of n. If there is more than one such y, Converter allows the user to choose exactly one of them. If there is no such y, Converter will break and will never be usable again.
  3. It returns a card on which the integer y is written.
Initially you have a card on which an integer start is written. Your goal is to get a card on which an integer goal is written. Return the minimum number of times you must use Converter in order to achieve the goal. If it is impossible to get such a card, return -1 instead.

Definition

Class:
BunnyConverter
Method:
getMinimum
Parameters:
int, int, int, int
Returns:
int
Method signature:
int getMinimum(int n, int z, int start, int goal)
(be sure your method is public)

Constraints

  • n will be between 1 and 500,000, inclusive.
  • z, start and goal will each be between 1 and n, inclusive.

Examples

  1. 5

    1

    5

    3

    Returns: 1

    By using Converter once for a card with 5, you can choose 2 or 3 as y. For example, y = 3 can be chosen because 5 + 3^2 + 1^3 = 15 is a multiple of 5.

  2. 5

    1

    5

    1

    Returns: 2

    By using Converter once for a card with 5, you can choose 2 or 3 as y. By using Converter once for a card with 3, you can choose 1 or 4 as y.

  3. 6

    2

    3

    4

    Returns: -1

    You will never get a card with 4.

  4. 7

    7

    7

    7

    Returns: 0

  5. 499979

    499979

    499976

    3

    Returns: 249988

  6. 1

    1

    1

    1

    Returns: 0

  7. 2

    1

    1

    2

    Returns: 1

  8. 2

    2

    1

    2

    Returns: -1

  9. 2

    2

    2

    2

    Returns: 0

  10. 3

    3

    1

    2

    Returns: -1

  11. 3

    3

    2

    1

    Returns: 1

  12. 4

    1

    4

    1

    Returns: -1

  13. 500000

    500000

    500000

    500000

    Returns: 0

  14. 500000

    19

    17372

    2

    Returns: 6252

  15. 499979

    499979

    79820

    3

    Returns: 249987

  16. 499979

    499979

    499970

    3

    Returns: 1

  17. 499979

    44929

    499976

    3

    Returns: 124994

  18. 499559

    499559

    499557

    2

    Returns: 249778

  19. 251263

    251263

    246942

    4321

    Returns: 6210

  20. 289889

    289889

    52715

    197000

    Returns: 7541

  21. 422537

    422537

    90657

    12338

    Returns: 16160

  22. 163601

    86663

    75699

    7467

    Returns: 561

  23. 317717

    317717

    202497

    30099

    Returns: 1444

  24. 280811

    280811

    139851

    210088

    Returns: 14038

  25. 291509

    249987

    48928

    29380

    Returns: 189

  26. 109367

    109367

    97140

    101740

    Returns: -1

  27. 307009

    20096

    6325

    250706

    Returns: 130

  28. 212029

    25385

    198218

    167015

    Returns: -1

  29. 231893

    231893

    165849

    44123

    Returns: 19324

  30. 285697

    256524

    63069

    77882

    Returns: -1

  31. 383261

    136783

    140193

    362113

    Returns: 352

  32. 161267

    161267

    30024

    27690

    Returns: 3122

  33. 331973

    331973

    202016

    298264

    Returns: -1

  34. 403667

    72555

    216153

    134524

    Returns: 23

  35. 292025

    60317

    272338

    261565

    Returns: 49

  36. 426649

    426649

    212839

    208445

    Returns: 785

  37. 323849

    323849

    182551

    246259

    Returns: 884

  38. 242167

    24153

    24386

    164985

    Returns: 63

  39. 466652

    230375

    68757

    209034

    Returns: 1109

  40. 374107

    374107

    31387

    120038

    Returns: 83

  41. 275137

    275137

    241283

    246477

    Returns: 136

  42. 175611

    175611

    40641

    36144

    Returns: 65

  43. 360971

    234798

    189624

    247187

    Returns: 281

  44. 168307

    168307

    151428

    110743

    Returns: 98

  45. 29099

    29099

    9404

    4835

    Returns: 409

  46. 436528

    436528

    255888

    123336

    Returns: 4545

  47. 26453

    24851

    15969

    11362

    Returns: 14

  48. 463311

    118732

    25639

    162492

    Returns: 17

  49. 11

    3

    3

    10

    Returns: 2

  50. 477702

    425218

    261376

    80424

    Returns: 119

  51. 5540

    3746

    1088

    4586

    Returns: 31

  52. 1483

    1198

    919

    903

    Returns: -1

  53. 13333

    3804

    8829

    8273

    Returns: 26

  54. 1760

    1149

    42

    28

    Returns: 4

  55. 4331

    2624

    154

    3392

    Returns: -1

  56. 499979

    478979

    499976

    3

    Returns: -1

  57. 500000

    500000

    521

    50000

    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: