#K38372. Valid Garlands
Valid Garlands
Valid Garlands
You are given an integer N representing the number of available colors and an integer M representing the number of beads. Your task is to compute the number of valid garlands that can be created under the following conditions:
- If there is only one bead (M = 1), any of the N colors can be used.
- If only one color is available (N = 1) and more than one bead is required (M > 1), a valid garland cannot be formed.
- For M > 2 and N > 1, the number of valid garlands is determined by a recurrence relation: \[ dp[1] = N, \quad dp[2] = N(N-1), \quad dp[m] = (N-1) \times dp[m-1] \quad \text{for } 3 \le m \le M. \]
For example, with 2 colors and 3 beads, there are 2 valid garlands, and with 3 colors and 2 beads, there are 6 valid garlands.
inputFormat
Input is given in a single line via standard input (stdin) with two space-separated integers N and M where N is the number of colors and M is the number of beads.
outputFormat
Output a single integer via standard output (stdout) representing the number of valid garlands that can be created with the given constraints.## sample
2 1
2