This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 3, N = 1e6 + 10;
int a[N], ch[N], fa[N], dp[N] = {0};
int find (int x) {
    return fa[x] == x ? x : fa[x] = find (fa[x]);
}
void add (int &x, int y) {
    x += y;
    if (x >= M) x -= M;
    if (x < 0) x += M;
}
void merge (int i, int j) {
    i = find(i), j = find(j);
    if (i == j) return;
    fa[i] = j;
    if (ch[i] == 3 or ch[j] == 3) ch[j] = min(ch[i], ch[j]);
    else if (ch[i] != ch[j]) {
        cout << "0\n";
        exit(0);
    }
}
void f(int i, int val) {
    i = find(i);
    if (ch[i] == 3) ch[i] = val;
    else if (ch[i] != val) {
        cout << "0\n";
        exit(0);
    }
}
int main () {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) fa[i] = i, ch[i] = 3;
    for (int i = 1; i + k - 1 <= n; i++) cin >> a[i];
    for (int i = 2; i + k - 1 <= n; i++) {
        int dif = a[i] - a[i - 1];
        if (dif == 0) merge(i - 1, i + k - 1);
        else if (dif == 1) f(i + k - 1, 1), f(i - 1, 0);
        else f(i + k - 1, 0), f(i - 1, 1);
    }
    int left = a[1];
    map <int,int> mp;
    // for (int i = 1; i <= n; i++) cout << find(i) << ' '; cout << '\n';
    // for (int i = 1; i <= n; i++) cout << ch[find(i)] << ' '; cout << '\n';
    for (int i = 1; i <= k; i++) {
        if (ch[find(i)] == 3) mp[find(i)]++;
        else if (ch[find(i)] == 1) {
            left--;
        }
    }
    dp[0] = 1;
    for (auto [x, y] : mp) {
        for (int i = left; i >= y; i--) add(dp[i], dp[i - y]);
    }
    cout << dp[left] << '\n';
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |