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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;

const int mod = 100000007;
int dp[100001][6], dp2[100001][4];
int sum[100001], sum2[100001];
string in;

int main() {
  dp[0][0] = dp2[0][0] = 1;

  rep(i, 100000) rep(j, 6) {
    if (j < 5)
      (dp[i + 1][j + 1] += dp[i][j]) %= mod;
    (dp[i + 1][1] += dp[i][j]) %= mod;
  }
  rep(i, 100000) rep(j, 4) {
    if (j < 3)
      (dp2[i + 1][j + 1] += dp2[i][j]) %= mod;
    (dp2[i + 1][1] += dp2[i][j]) %= mod;
  }
  rep(i, 100001) {
    rep(j, 6)(sum[i] += dp[i][j]) %= mod;
    rep(j, 4)(sum2[i] += dp2[i][j]) %= mod;
  }
  rep(i, 100001) {
    if (i > 5)
      (sum[i] += sum[i - 6]) %= mod;
    if (i > 3)
      (sum2[i] += sum2[i - 4]) %= mod;
  }

  while (getline(cin, in), in != "#") {
    int cnt = 0;
    vi v, type;
    rep(i, in.size()) {
      if (i && in[i] != in[i - 1]) {
        v.pb(cnt);
        cnt = 1;
        type.pb(in[i - 1] == '0' || in[i - 1] == '8');
      } else
        cnt++;
    }
    type.pb(in[in.size() - 1] == '0' || in[in.size() - 1] == '8');
    v.pb(cnt);

    ll ans = 1;
    rep(i, v.size()) {
      if (!type[i])
        (ans *= sum[v[i] - 1]) %= mod;
      else
        (ans *= sum2[v[i] - 1]) %= mod;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;

const int mod = 100000007;
int dp[100001][6], dp2[100001][4];
int sum[100001], sum2[100001];
string in;

int main() {
  dp[0][0] = dp2[0][0] = 1;

  rep(i, 100000) rep(j, 6) {
    if (j < 5)
      (dp[i + 1][j + 1] += dp[i][j]) %= mod;
    (dp[i + 1][1] += dp[i][j]) %= mod;
  }
  rep(i, 100000) rep(j, 4) {
    if (j < 3)
      (dp2[i + 1][j + 1] += dp2[i][j]) %= mod;
    (dp2[i + 1][1] += dp2[i][j]) %= mod;
  }
  rep(i, 100001) {
    rep(j, 6)(sum[i] += dp[i][j]) %= mod;
    rep(j, 4)(sum2[i] += dp2[i][j]) %= mod;
  }
  rep(i, 100001) {
    if (i > 5)
      (sum[i] += sum[i - 6]) %= mod;
    if (i > 3)
      (sum2[i] += sum2[i - 4]) %= mod;
  }

  rep(i, 20) cerr << sum[i] << " ";
  cerr << endl;

  while (getline(cin, in), in != "#") {
    int cnt = 0;
    vi v, type;
    rep(i, in.size()) {
      if (i && in[i] != in[i - 1]) {
        v.pb(cnt);
        cnt = 1;
        type.pb(in[i - 1] == '0' || in[i - 1] == '8');
      } else
        cnt++;
    }
    type.pb(in[in.size() - 1] == '0' || in[in.size() - 1] == '8');
    v.pb(cnt);

    ll ans = 1;
    rep(i, v.size()) {
      if (!type[i])
        (ans *= sum[v[i] - 1]) %= mod;
      else
        (ans *= sum2[v[i] - 1]) %= mod;
    }
    cout << ans << endl;
  }
  return 0;
}