```
public class Solution {
public int numWays(int n, int k) {
if (n == 0 || k == 0) return 0;
int a = 0, b = k;
for (int i = 2; i <= n; i++) {
int tmp = a;
a = b;
b = (k - 1) * (tmp + b);
}
return a + b;
}
}
```