• + 0 comments
    long countArray(int n, int k, int x) {
        // index 0 <= i <= n-1
        vector<long> a(n); // number of array with length 1 <= i+1 <= n end with x
        vector<long> b(n); // number of array with length 1 <= i+1 <= n not end with x
        if (x == 1) {
            a[0] = 1;
            b[0] = 0;
        } else {
            a[0] = 0;
            b[0] = 1;
        }
        
        for (int i=1; i<n; i++) {
            b[i] = a[i-1] * (k-1) + b[i-1] * (k-2);
            b[i] %= 1000000007;
            a[i] = b[i-1];
        }
        
        return a[n-1] % 1000000007;
    }