Array Description.cpp | CSES-Solutions | Dynamic Programming

Array Description.cpp | CSES-Solutions | Dynamic Programming,
CSES-Solutions | Dynamic Programming 
Problem 1 Array Description
You know that an array has  integers between  and  , and the absolute difference between two adjacent values is at most .
Given a description of the array where some values may be unknown, your 
task is to count the number of arrays that match the description.
Input

The first input line has two integers  and : the array size and the upper bound for each value.
The next line has integers : the contents of the array. Value denotes an unknown value.
Output

Print one integer: the number of arrays modulo .
Solution CODE:

#include 

using namespace std;
typedef long long ll;
const int maxN = 1e5;
const int maxM = 100;
const ll MOD = 1e9+7;

int N, M, x[maxN+1];
ll ans, dp[maxN+1][maxM+1];

int main(){
    scanf("%d %d", &N, &M);
    for(int i = 1; i <= N; i++)
        scanf("%d", &x[i]);

    if(x[1])
        dp[1][x[1]] = 1;
    else
        for(int i = 1; i <= M; i++)
            dp[1][i] = 1;

    for(int i = 2; i <= N; i++){
        for(int j = 1; j <= M; j++){
            dp[i][j] = dp[i-1][j];
            if(j != 1)  dp[i][j] += dp[i-1][j-1];
            if(j != M)  dp[i][j] += dp[i-1][j+1];
            dp[i][j] %= MOD;
        }

        if(x[i])
            for(int j = 0; j <= M; j++)
                if(j != x[i])
                    dp[i][j] = 0;
    }

    for(int i= 1; i <= M; i++)
        ans = (ans + dp[N][i]) % MOD;
    printf("%lld\n", ans);
}
Men' fashion , Dating Tips , Relationship Advice

Post a Comment