Statistics

Problem Statement for "CountExpressions"

Problem Statement

You are helping your brother with his homework assignment. His teacher gave him two distinct numbers x and y, and asked him to use those numbers to form as many different expressions as possible. Each expression must satisfy all of the following rules:

  1. The only allowed operators are '+', '-' and '*'.
  2. x and y must each appear exactly twice. No other numbers are allowed.
  3. The result of the expression must be equal to val.


In other words, each expression can be written in the form "a op1 b op2 c op3 d", where each of op1, op2 and op3 is '+', '-' or '*', and among the numbers a, b, c and d, exactly two are equal to x and the other two are equal to y. Please note that the unary minus is not allowed (see example 0). Expressions are calculated from left to right, and there is no operator precedence. For example, to calculate the result of "2 + 2 * 3 + 3", you would first calculate 2 + 2, then multiply the result by 3, and then add 3 to get 15.


Return the total number of different expressions that can be formed. Two expressions are considered different if their string notations (as described in the previous paragraph) are different. For example, the expressions "2 + 3 - 2 - 3", "2 - 2 + 3 - 3" and "2 - 3 - 2 + 3" are all different.

Definition

Class:
CountExpressions
Method:
calcExpressions
Parameters:
int, int, int
Returns:
int
Method signature:
int calcExpressions(int x, int y, int val)
(be sure your method is public)

Constraints

  • x and y will each be between -100 and 100, inclusive.
  • x and y will be different.
  • val will be between -100000000 and 100000000, inclusive.

Examples

  1. 7

    8

    16

    Returns: 9

    The possible expressions are: 8 + 8 + 7 - 7 8 + 7 + 8 - 7 7 + 8 + 8 - 7 8 + 8 - 7 + 7 8 + 7 - 7 + 8 7 + 8 - 7 + 8 8 - 7 + 8 + 7 8 - 7 + 7 + 8 7 - 7 + 8 + 8 Please note that the unary minus is not allowed, so "-7 + 7 + 8 + 8" is not a valid expression.

  2. 100

    1

    10000

    Returns: 12

  3. -100

    100

    2

    Returns: 0

  4. 3

    5

    7

    Returns: 5

    The possible expressions are: 3 * 5 - 3 - 5 5 * 3 - 3 - 5 3 * 5 - 5 - 3 5 * 3 - 5 - 3 5 - 3 * 5 - 3

  5. 34

    67

    95

    Returns: 0

  6. 12

    24

    36

    Returns: 0

  7. 25

    50

    1000

    Returns: 0

  8. 99

    100

    98010000

    Returns: 6

  9. 48

    -65

    -153985

    Returns: 1

  10. -53

    -64

    -179712

    Returns: 3

  11. -61

    -77

    -286594

    Returns: 3

  12. -13

    -52

    0

    Returns: 14

  13. -87

    43

    43

    Returns: 1

  14. -96

    -95

    18335

    Returns: 1

  15. -35

    -38

    1444

    Returns: 6

  16. -87

    -15

    -113310

    Returns: 1

  17. 73

    -95

    -497230

    Returns: 1

  18. -83

    -31

    -212598

    Returns: 1

  19. -80

    -30

    5760000

    Returns: 6

  20. -99

    42

    -1764

    Returns: 2

    -99 - (-99) - 42 * 42 -99 - 42 - (-99) * 42

  21. -23

    5

    2640

    Returns: 3

  22. -84

    -92

    -649060

    Returns: 3

  23. -99

    -22

    -216106

    Returns: 1

  24. -72

    -59

    7765

    Returns: 0

  25. -82

    58

    -129

    Returns: 0

  26. -75

    -92

    -9202

    Returns: 0

  27. 100

    -100

    100000000

    Returns: 6

  28. 100

    -100

    -100000000

    Returns: 0

    There are no valid expressions.

  29. 2

    3

    6

    Returns: 13

  30. 1

    2

    6

    Returns: 15

  31. -1

    1

    0

    Returns: 36

  32. 100

    -100

    100

    Returns: 6

  33. 100

    -100

    -100

    Returns: 6

  34. 1

    2

    5

    Returns: 17

  35. -100

    -99

    9901

    Returns: 4

  36. -100

    -50

    100

    Returns: 7

  37. -100

    -1

    -1

    Returns: 8

  38. -100

    -99

    -200

    Returns: 9

  39. -100

    -51

    -200

    Returns: 10

  40. -100

    -49

    -200

    Returns: 11

  41. -4

    -2

    -4

    Returns: 16

  42. -100

    -1

    0

    Returns: 19

  43. -100

    100

    -10000

    Returns: 18

  44. -4

    4

    -16

    Returns: 21

  45. -4

    -2

    0

    Returns: 20

  46. -2

    -1

    0

    Returns: 22

  47. -100

    100

    0

    Returns: 24

  48. -2

    2

    4

    Returns: 27

  49. -2

    0

    4

    Returns: 26

  50. -2

    2

    0

    Returns: 30

  51. -2

    2

    -4

    Returns: 33

  52. 0

    2

    4

    Returns: 38

  53. 1

    0

    0

    Returns: 76

  54. 100

    0

    1000

    Returns: 0

  55. 2

    1

    6

    Returns: 15

  56. 0

    1

    1

    Returns: 36

  57. 0

    99

    99

    Returns: 17

  58. 8

    7

    16

    Returns: 9


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: