Here is a difficult mathematics problem; your job is to solve it.
Given two integer N and K, we have
A(N, K) = {a | a = (a1 a2 a3 … aN), ai is integer and 0 <= ai <= K-1, i = 1,...,N}.
We define a function d = Image(a) from A(N, K) to A(N-1, K) as below:
For any a = (a1 a2 a3 ... aN), which is belonged to A(N, K), we have d = (d1 d2 ... dN-1) = Image(a) with di = min(ai, ai+1), i.e., di is the smaller one between ai and ai+1.
You should calculate the expression below
The input consists of only one line, which contains two integer N and K (N = 2, 3, 4. And if N=2, we has 1 <= K <= 5000; if N=3, we has 1 <= K <= 1000; if N=4, we has 1 <= K <= 500).
The output contains a single integer, which is the value of f(N, K). It's confirmed that the result is less than 2^63.
2 3
14