1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int main() {

    int n, m;
    cin >> n >> m;

    int maxArrays = pow(m, n);
    vector<int> data;
    for(int i = 0; i < n; i++) data.push_back(0);

    if(n <= 2) {
        cout << 0;
        return 0;
    }

    int count = 0;
    for(int i = 0; i < maxArrays; i++) {
        //print data
        

        //check data
        if(data[0] == data[n-1]) {
            count++;
        } else {
            bool result = true;
            
            int start = 0;
            while(start < n) {
                int x = data[start];
                for(int j = n-1; j >= start; j--) {
                    if(j == start) {
                        result = false;
                        start = n-1;
                    }
                    if(x == data[j]) {
                        start = j + 1;
                        break;
                    }
                }
            } 

            if(result) {
                count++;
            }
        }

        //data++
        data[n-1] = data[n-1] + 1;
        bool b = false;
        for(int j = n-1; j >= 0; j--) {
            if(b) {
                data[j] = data[j] + 1;
                b = false;
            }
            if(data[j] == m) {
                data[j] = 0;
                b = true;
            }
        }
    }

    cout << count;

    return 0;
}