Problem p01511 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MOD 1000000009

typedef long long int LLI;
typedef LLI Matrix[75][75];

int Case;
int w;
LLI h;
int n;

void Add(Matrix &A, Matrix &B, Matrix &C) {
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      C[i][j] = (A[i][j] + B[i][j]) % MOD;
    }
  }
}

void Mul(Matrix &A, Matrix &B, Matrix &C) {
  Matrix res;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      res[i][j] = 0;
      for (int k = 0; k < w; k++) {
        res[i][j] = (res[i][j] + A[i][k] * B[k][j] % MOD) % MOD;
      }
    }
  }

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      C[i][j] = res[i][j];
    }
  }
}

void Exp(Matrix &A, LLI k, Matrix &C) {
  Matrix mul;
  Matrix res;

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      mul[i][j] = A[i][j];
      if (i == j)
        C[i][j] = 1;
      else
        C[i][j] = 0;
    }
  }

  LLI g = 0;
  while (k >> g) {
    if (k >> g & 1) {
      Mul(C, mul, C);
    }
    Mul(mul, mul, mul);
    g++;
  }
}

int main() {
  while (1) {
    scanf("%d%lld%d", &w, &h, &n);
    if ((w | h | n) == 0)
      return 0;

    map<LLI, set<int>> objs;
    for (int i = 0; i < n; i++) {
      int x;
      LLI y;
      scanf("%d%lld", &x, &y);
      if (y == 1)
        continue;
      objs[y].insert(x);
    }
    objs[h].insert(0);

    Matrix Ans;
    Matrix Base;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < w; j++) {
        if (i == j)
          Ans[i][j] = 1;
        else
          Ans[i][j] = 0;
        if (i - 1 <= j && j <= i + 1)
          Base[i][j] = 1;
        else
          Base[i][j] = 0;
      }
    }

    LLI cury = 1;
    for (auto &itr : objs) {
      LLI t = itr.first;
      Matrix mul, one;

      if (t - 1 - cury > 0) {
        Exp(Base, t - 1 - cury, mul);
        Mul(Ans, mul, Ans);
      }

      for (int j = 0; j < w; j++) {
        for (int k = 0; k < w; k++) {
          if (itr.second.count(k + 1))
            one[j][k] = 0;
          else
            one[j][k] = Base[j][k];
        }
      }
      Mul(Ans, one, Ans);
      cury = t;
    }

    printf("Case %d: %lld\\n", ++Case, Ans[0][w - 1]);
  }
}

Error: TLE

Fixed submission:

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

#define MOD 1000000009

typedef long long int LLI;
typedef pair<LLI, int> Pair;
typedef LLI Matrix[75][75];

int Case;
int w;
int h;
int n;
Pair ps[114];

void Add(Matrix &A, Matrix &B, Matrix &C) {
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      C[i][j] = (A[i][j] + B[i][j]) % MOD;
    }
  }
}

void Mul(Matrix &A, Matrix &B, Matrix &C) {
  Matrix res;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      res[i][j] = 0;
      for (int k = 0; k < w; k++) {
        res[i][j] = (res[i][j] + A[i][k] * B[k][j] % MOD) % MOD;
      }
    }
  }

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      C[i][j] = res[i][j];
    }
  }
}

void Exp(Matrix &A, int k, Matrix &C) {
  Matrix mul;
  Matrix res;

  if (k == 0) {
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < w; j++) {
        if (i == j)
          C[i][j] = 1;
        else
          C[i][j] = 0;
      }
    }
    return;
  }

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < w; j++) {
      mul[i][j] = A[i][j];
      C[i][j] = 0;
    }
  }

  int g = 0;
  while (k >> g) {
    if (k >> g & 1) {
      Add(C, mul, C);
    }
    Mul(mul, mul, mul);
    g++;
  }
}

int main() {
  while (1) {
    scanf("%d%d%d", &w, &h, &n);
    if ((w | h | n) == 0)
      return 0;

    map<LLI, set<int>> objs;
    for (int i = 0; i < n; i++) {
      int x;
      LLI y;
      scanf("%d%lld", &x, &y);
      objs[y].insert(x);
    }
    objs[h].insert(0);

    Matrix Ans;
    Matrix Base;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < w; j++) {
        if (i == j)
          Ans[i][j] = 1;
        else
          Ans[i][j] = 0;
        if (i - 1 <= j && j <= i + 1)
          Base[i][j] = 1;
        else
          Base[i][j] = 0;
      }
    }

    LLI cury = 1;
    for (auto &itr : objs) {
      LLI t = itr.first;
      Matrix mul, one;
      Exp(Base, max(0LL, t - 1 - cury), mul);
      Mul(Ans, mul, Ans);
      for (int j = 0; j < w; j++) {
        for (int k = 0; k < w; k++) {
          if (itr.second.count(k + 1))
            one[j][k] = 0;
          else
            one[j][k] = Base[j][k];
        }
      }
      Mul(Ans, one, Ans);
      cury = t;
    }

    printf("Case %d: %lld\\n", ++Case, Ans[0][w - 1]);
  }
}