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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T, vector<T>, greater<T>>
#define Pqza(T) priority_queue<T>
#define ENJYU                                                                  \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 9;

// vector の中身を出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

//************************************************************************//

// 型を決めてね！！！！！！！！！！！
typedef ll MatType;

// 演算の単位元も決めてね！！！！！！！！！！！！！！
const MatType plus_base = 0;
const MatType mul_base = 1;

typedef vector<MatType> Vec;
typedef vector<Vec> Mat;

// A * B = C
Mat mul(const Mat &A, const Mat &B) {
  // 大前提
  assert((int)A[0].size() == (int)B.size());

  Mat C((int)A.size(), Vec((int)B[0].size(), plus_base));

  // メモリへのアクセス的にこうしたほうが早いのです
  rep((int)A.size(), i) rep((int)B.size(), k) rep((int)B[0].size(), j) {
    C[i][j] += A[i][k] * B[k][j] % MOD;
    C[i][j] %= MOD;
  }

  return C;
}

// v' = A * v
// オーバーロードしているんです！！！！！！！！！！！！
Vec mul(const Mat &A, const Vec &v) {
  // 大前提
  assert((int)A[0].size() == (int)v.size());

  Vec nv((int)A.size(), plus_base);

  rep((int)A.size(), i) rep((int)v.size(), j) {
    nv[i] += A[i][j] * v[j] % MOD;
    nv[i] %= MOD;
  }

  return nv;
}

// A ^ N
Mat mpow(Mat A, ll N) {
  // 大前提
  assert((int)A[0].size() == (int)A.size() && N >= 0);

  Mat Res((int)A.size(), Vec((int)A.size(), plus_base));

  // A ^ 0 = E
  rep((int)A.size(), i) { Res[i][i] = mul_base; }

  while (N) {
    if (N & 1) {
      Res = mul(Res, A);
    }
    A = mul(A, A);
    N >>= 1;
  }

  return Res;
}

//************************************************************************//

void solve(void) {
  int W, N;
  ll H;
  int cnt = 1;
  while (cin >> W >> H >> N) {
    if (!W)
      break;

    // obs (h, w)
    vector<P> obs(N);
    rep(N, i) cin >> obs[i].snd >> obs[i].fst;
    sort(all(obs));

    Vec V(W, 0);
    V[0] = 1;

    ll acc = 0;

    for (auto p : obs) {
      ll n = p.fst - 1 - acc;

      Mat M(W, Vec(W, 0));
      rep(W, i) M[i][i] = 1;
      rep(W - 1, i) M[i + 1][i] = 1;
      rep(W - 1, i) M[i][i + 1] = 1;

      M = mpow(M, n);
      V = mul(M, V);
      acc += n;
      V[p.snd - 1] = 0;
    }

    Mat M(W, Vec(W, 0));
    rep(W, i) M[i][i] = 1;
    rep(W - 1, i) M[i + 1][i] = 1;
    rep(W - 1, i) M[i][i + 1] = 1;

    V = mul(mpow(M, H - acc - 1), V);

    cout << "Case " << cnt++ << ": " << V[W - 1] << endl;
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T, vector<T>, greater<T>>
#define Pqza(T) priority_queue<T>
#define ENJYU                                                                  \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);

typedef long long ll;
typedef pair<int, int> intP;
const ll MOD = 1e9 + 7;

// vector の中身を出力
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

//************************************************************************//

// 型を決めてね！！！！！！！！！！！
typedef ll MatType;

// 演算の単位元も決めてね！！！！！！！！！！！！！！
const MatType plus_base = 0;
const MatType mul_base = 1;

typedef vector<MatType> Vec;
typedef vector<Vec> Mat;

// A * B = C
Mat mul(const Mat &A, const Mat &B) {
  // 大前提
  assert((int)A[0].size() == (int)B.size());

  Mat C((int)A.size(), Vec((int)B[0].size(), plus_base));

  // メモリへのアクセス的にこうしたほうが早いのです
  rep((int)A.size(), i) rep((int)B.size(), k) rep((int)B[0].size(), j) {
    C[i][j] += A[i][k] * B[k][j] % MOD;
    C[i][j] %= MOD;
  }

  return C;
}

// v' = A * v
// オーバーロードしているんです！！！！！！！！！！！！
Vec mul(const Mat &A, const Vec &v) {
  // 大前提
  assert((int)A[0].size() == (int)v.size());

  Vec nv((int)A.size(), plus_base);

  rep((int)A.size(), i) rep((int)v.size(), j) {
    nv[i] += A[i][j] * v[j] % MOD;
    nv[i] %= MOD;
  }

  return nv;
}

// A ^ N
Mat mpow(Mat A, ll N) {
  // 大前提
  assert((int)A[0].size() == (int)A.size() && N >= 0);

  Mat Res((int)A.size(), Vec((int)A.size(), plus_base));

  // A ^ 0 = E
  rep((int)A.size(), i) { Res[i][i] = mul_base; }

  while (N) {
    if (N & 1) {
      Res = mul(Res, A);
    }
    A = mul(A, A);
    N >>= 1;
  }

  return Res;
}

//************************************************************************//

void solve(void) {
  int W, N;
  ll H;
  int cnt = 1;
  while (cin >> W >> H >> N) {
    if (!W)
      break;

    vector<intP> obs(N);
    rep(N, i) cin >> obs[i].fst >> obs[i].snd;
    sort(all(obs));

    Vec V(W, 0);
    V[0] = 1;

    ll acc = 0;

    for (auto p : obs) {

      Mat M(W, Vec(W, 0));
      rep(W, i) M[i][i] = 1;
      rep(W - 1, i) M[i + 1][i] = 1;
      rep(W - 1, i) M[i][i + 1] = 1;

      ll n = p.snd - 1 - acc;
      M = mpow(M, n);
      V = mul(M, V);
      acc += n;
      V[p.fst - 1] = 0;
    }

    Mat M(W, Vec(W, 0));
    rep(W, i) M[i][i] = 1;
    rep(W - 1, i) M[i + 1][i] = 1;
    rep(W - 1, i) M[i][i + 1] = 1;

    V = mul(mpow(M, H - acc - 1), V);

    cout << "Case " << cnt++ << ": " << V[W - 1] << endl;
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}