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

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < (b) ? (a = b, 1) : 0)
#define chmin(a, b) (a > (b) ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
const int INF = 1 << 29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int, int> pii;
typedef long long ll;

bool weekdayOK[7];
bool hourOK[24];

int cnt[100];

int main() {
  int s, n, t;
  string weekday, time;
  int p, m;
  while (cin >> s >> n >> t >> weekday >> time >> p >> m, s) {
    string wdstr[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    REP(i, 7) {
      if (weekday == "All" || weekday == wdstr[i])
        weekdayOK[i] = 1;
      else
        weekdayOK[i] = 0;
    }
    REP(i, 24) hourOK[i] = 0;
    if (time == "All") {
      REP(i, 24) hourOK[i] = 1;
    } else if (time == "Day") {
      for (int i = 6; i < 18; ++i)
        hourOK[i] = 1;
    } else {
      for (int i = 0; i < 6; ++i)
        hourOK[i] = 1;
      for (int i = 18; i < 24; ++i)
        hourOK[i] = 1;
    }
    double ans = 0;
    REP(stime, 7 * 24 * 60) {
      int acc = 0;
      double tmp = 1;
      REP(_, m) {
        if (stime == 0) {
          cnt[_] = 0;
          REP(i, s + 1) {
            int cwd = (stime + acc) / (24 * 60) % 7;
            int chour = (stime + acc) % (24 * 60) / 60;
            if (!weekdayOK[cwd] || !hourOK[chour]) {
              cnt[_]++;
            }
            acc++;
          }
        } else {
          int cwd_s = (stime + acc + 7 * 24 * 60 - 1) / (24 * 60) % 7;
          int chour_s = (stime + acc + 7 * 24 * 60 - 1) % (24 * 60) / 60;
          int cwd_t = (stime + acc + s) / (24 * 60) % 7;
          int chour_t = (stime + acc + s) % (24 * 60) / 60;
          if (!weekdayOK[cwd_s] || !hourOK[chour_s])
            cnt[_]--;
          if (!weekdayOK[cwd_t] || !hourOK[chour_t])
            cnt[_]++;
          acc += s + 1;
        }

        if (cnt[_] == 0) {
          REP(i, n)
          tmp *= (1.0 - 1.0 / p);
        }
        acc += (t - s) - 1;
      }
      chmax(ans, 1 - tmp);
    }
    printf("%.10f\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < (b) ? (a = b, 1) : 0)
#define chmin(a, b) (a > (b) ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
const int INF = 1 << 29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int, int> pii;
typedef long long ll;

bool weekdayOK[7];
bool hourOK[24];

int main() {
  int s, n, t;
  string weekday, time;
  int p, m;
  while (cin >> s >> n >> t >> weekday >> time >> p >> m, s) {
    string wdstr[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    REP(i, 7) {
      if (weekday == "All" || weekday == wdstr[i])
        weekdayOK[i] = 1;
      else
        weekdayOK[i] = 0;
    }
    REP(i, 24) hourOK[i] = 0;
    if (time == "All") {
      REP(i, 24) hourOK[i] = 1;
    } else if (time == "Day") {
      for (int i = 6; i < 18; ++i)
        hourOK[i] = 1;
    } else {
      for (int i = 0; i < 6; ++i)
        hourOK[i] = 1;
      for (int i = 18; i < 24; ++i)
        hourOK[i] = 1;
    }
    double ans = 0;
    REP(stime, 7 * 24 * 60) {
      int acc = 0;
      double tmp = 1;
      REP(_, m) {
        bool ok = 1;
        REP(i, s + 1) {
          int cwd = (stime + acc) / (24 * 60) % 7;
          int chour = (stime + acc) % (24 * 60) / 60;
          if (!weekdayOK[cwd] || !hourOK[chour]) {
            ok = 0;
          }
          acc++;
        }
        if (ok) {
          REP(i, n)
          tmp *= (1.0 - 1.0 / p);
        }
        acc += (t - s) - 1;
      }
      chmax(ans, 1 - tmp);
    }
    printf("%.10f\\n", ans);
  }
}