There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of…
- User
There are N balls placed in a row. AtCoDeer the deer is painting each of these in one of the K colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.
Find the number of the possible ways to paint the balls.
Constraints
- 1≦N≦1000
- 2≦K≦1000
- The correct answer is at most 2^{31}-1. Input
The input is given from Standard Input in the following format:
N K
Output
Print the number of the possible ways to paint the balls.
Examples
Input
2 2
Output
2
Input
1 10
Output
10
Assistant
We need to solve: number of ways to color N balls with K colors, adjacent balls different.
We know: first ball has K choices. Each subsequent ball has K-1 choices (any color except the previous). So total = K * (K-1)^(N-1). But careful: for N=1, then it's just K, which matches formula: K * (K-1)^0 = K. So answer is that.
But wait: constraints: answer at most 2^31-1, so fits in 32-bit signed int. But we must compute potentially large numbers: K up to 1000, N up to 1000, so (K-1)^(N-1) can be huge, but answer is guaranteed at most 2^31-1, so we can compute directly using integer arithmetic with possible overflow? However we need to output correct answer. Since maximum answer is 2^31-1 (which is about 2.147e9), we can safely compute using 64-bit integer (e.g., long long) to avoid overflow. In Python it's fine.