Problem:
Score : 200 points

Problem Statement

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.

Sample Input 1

2 2

Sample Output 1

2

We will denote the colors by 0 and 1. There are two possible ways: we can either paint the left ball in color 0 and the right ball in color 1, or paint the left in color 1 and the right in color 0.

Sample Input 2

1 10

Sample Output 2

10

Since there is only one ball, we can use any of the ten colors to paint it. Thus, the answer is ten.

Solution in C (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>

int p[1001], N, K, count=0;  // N : ball , K : paint

void f(int s, int c){
    int i;

    p[c] = s;

    if(c == N) {
        //for(i=1; i<=N; i++) printf("%d ", p[i]);
        //printf("\n");
        count++;
        return;
    }

    for(i=1; i<=K; i++){
        if(s != i) f(i, c+1);
    }
}

int main(){
    int p[1001]={0}, i, j;

    scanf("%d %d", &N, &K);

    for(i=1; i<=K; i++){
        f(i, 1);
    }

    printf("%d", count);

    return 0;
}