Problem p01511 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll W, H, N;

typedef vector<ll> Vec;
typedef vector<Vec> Mat;
const ll MOD = 1000000009;

void add(ll &a, ll b) {
  a += b;
  a %= MOD;
}

void view(const Mat &a) {
  cout << "view" << endl;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++)
      cout << a[i][j] << " ";
    cout << endl;
  }
}

Mat mult(const Mat &a, const Mat &b) {
  Mat res = Mat(a.size(), Vec(b[0].size(), 0));
  for (int i = 0; i < (int)res.size(); i++) {
    for (int j = 0; j < (int)res[i].size(); j++) {
      for (int k = 0; k < (int)a[0].size(); k++) {
        add(res[i][j], (a[i][k] * b[k][j]) % MOD);
      }
    }
  }
  return res;
}

// a * b^k
Mat pow_m(const Mat &a, const Mat &b, ll k) {
  Mat res = a;
  Mat x = b;
  while (k) {
    if (k & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
    k >>= 1;
  }
  return res;
}

ll X[100], Y[100];
ll fie[400];

Mat get_mat(int y) {
  Mat ret = Mat(W, Vec(W, 0));
  for (int j = 0; j < W; j++)
    for (int i = max(0, j - 1); i < min(j + 2, (int)W); i++)
      ret[i][j] = 1;
  if (y == -1)
    return ret;
  for (int i = 0; i < N; i++) {
    if ((int)Y[i] == y) {
      for (int j = 0; j < W; j++)
        ret[j][X[i]] = 0;
    }
  }
  return ret;
}

Mat solve(int h) {
  Mat res = Mat(1, Vec(W, 0));
  res[0][0] = 1;
  ll now_y = 1;
  for (int i = 0; i < h; i++) {
    if (fie[i] <= 1)
      continue;
    ll t = fie[i] - now_y;
    Mat next = get_mat(i);
    res = pow_m(res, next, t);
    now_y = fie[i];
  }
  return res;
}

int zatu() {
  vector<ll> v;
  v.push_back(1);
  v.push_back(H);
  for (int i = 0; i < N; i++)
    for (int j = -1; j < 1; j++)
      v.push_back(Y[i] + j);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i < (int)v.size(); i++)
    fie[i] = v[i];
  for (int i = 0; i < N; i++)
    Y[i] = lower_bound(v.begin(), v.end(), Y[i]) - v.begin();
  return v.size();
}

int main() {
  while (cin >> W >> H >> N && (W || H || N)) {
    static int ttt = 1;
    cout << "Case " << ttt++ << ": ";
    bool f = false;
    for (int i = 0; i < N; i++) {
      cin >> X[i] >> Y[i];
      --X[i];
      if (X[i] == 0 && Y[i] == 1)
        f = true;
    }
    if (f) {
      cout << 0 << endl;
      break;
    }
    int h = zatu();
    Mat ans = solve(h);
    cout << ans[0][W - 1] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll W, H, N;

typedef vector<ll> Vec;
typedef vector<Vec> Mat;
const ll MOD = 1000000009;

void add(ll &a, ll b) {
  a += b;
  a %= MOD;
}

void view(const Mat &a) {
  cout << "view" << endl;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[i].size(); j++)
      cout << a[i][j] << " ";
    cout << endl;
  }
}

Mat mult(const Mat &a, const Mat &b) {
  Mat res = Mat(a.size(), Vec(b[0].size(), 0));
  for (int i = 0; i < (int)res.size(); i++) {
    for (int j = 0; j < (int)res[i].size(); j++) {
      for (int k = 0; k < (int)a[0].size(); k++) {
        add(res[i][j], (a[i][k] * b[k][j]) % MOD);
      }
    }
  }
  return res;
}

// a * b^k
Mat pow_m(const Mat &a, const Mat &b, ll k) {
  Mat res = a;
  Mat x = b;
  while (k) {
    if (k & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
    k >>= 1;
  }
  return res;
}

ll X[100], Y[100];
ll fie[400];

Mat get_mat(int y) {
  Mat ret = Mat(W, Vec(W, 0));
  for (int j = 0; j < W; j++)
    for (int i = max(0, j - 1); i < min(j + 2, (int)W); i++)
      ret[i][j] = 1;
  if (y == -1)
    return ret;
  for (int i = 0; i < N; i++) {
    if ((int)Y[i] == y) {
      for (int i = 0; i < W; i++)
        ret[i][X[i]] = 0;
    }
  }
  return ret;
}

Mat solve(int h) {
  Mat res = Mat(1, Vec(W, 0));
  res[0][0] = 1;
  ll now_y = 1;
  for (int i = 0; i < h; i++) {
    if (fie[i] <= 1)
      continue;
    ll t = fie[i] - now_y;
    Mat next = get_mat(i);
    res = pow_m(res, next, t);
    now_y = fie[i];
  }
  return res;
}

int zatu() {
  vector<ll> v;
  v.push_back(1);
  v.push_back(H);
  for (int i = 0; i < N; i++)
    for (int j = -1; j < 1; j++)
      v.push_back(Y[i] + j);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 0; i < (int)v.size(); i++)
    fie[i] = v[i];
  for (int i = 0; i < N; i++)
    Y[i] = lower_bound(v.begin(), v.end(), Y[i]) - v.begin();
  return v.size();
}

int main() {
  while (cin >> W >> H >> N && (W || H || N)) {
    for (int i = 0; i < N; i++) {
      cin >> X[i] >> Y[i];
      --X[i];
    }
    int h = zatu();
    Mat ans = solve(h);
    static int ttt = 1;
    cout << "Case " << ttt++ << ": ";
    cout << ans[0][W - 1] << endl;
  }
}