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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

const static char weekdays[][4] = {"Sun", "Mon", "Tue", "Wed",
                                   "Thu", "Fri", "Sat"};
const static char times[][6] = {"Day", "Night"};

const char *compute_weekday(int minutes) {
  int tmp = (minutes / (60 * 24)) % 7;
  return weekdays[tmp];
}

const char *compute_time_band(int minutes) {
  int idx = 0;
  int tmp = minutes % (60 * 24);
  if (tmp >= 6 * 60 && tmp < 18 * 60) {
    idx = 0;
  } else {
    idx = 1;
  }
  return times[idx];
}

int main() {
  int wait_hatch;
  int num_of_egg;
  int stage_life;
  char mutation_weekday[4];
  char mutation_time_band[6];
  int inv_mutation_prob;
  int total_stages;
  double probs[101];
  while (~scanf("%d %d %d %s %s %d %d", &wait_hatch, &num_of_egg, &stage_life,
                mutation_weekday, mutation_time_band, &inv_mutation_prob,
                &total_stages)) {

    if (total_stages == 0)
      break;

    probs[0] = 1.0;
    for (int i = 0; i < num_of_egg; i++) {
      probs[i + 1] = probs[i] * (1.0 - 1.0 / inv_mutation_prob);
    }

    double res = 0.0;
    for (int start = 0; start < 60 * 24 * 7; start++) {
      int init_start = start;
      double prob = 1.0;

      for (int round = 0; round < total_stages; round++) {
        int time = init_start + wait_hatch;

        if (strcmp(mutation_weekday, "All") != 0 &&
            strcmp(compute_weekday(time), mutation_weekday) != 0) {
          init_start += stage_life;
          continue;
        }

        if (strcmp(mutation_time_band, "All") != 0 &&
            strcmp(compute_time_band(time), mutation_time_band) != 0) {
          init_start += stage_life;
          continue;
        }

        if (strcmp(mutation_weekday, "All") != 0 &&
            strcmp(compute_weekday(init_start), compute_weekday(time)) != 0) {
          init_start += stage_life;
          continue;
        }
        if (strcmp(mutation_time_band, "All") != 0 &&
            strcmp(compute_time_band(init_start), compute_time_band(time)) !=
                0) {
          init_start += stage_life;
          continue;
        }

        prob *= probs[num_of_egg];
        init_start += stage_life;
      }
      res = max(res, 1.0 - prob);
    }
    printf("%.10f\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

const static char weekdays[][4] = {"Sun", "Mon", "Tue", "Wed",
                                   "Thu", "Fri", "Sat"};
const static char times[][6] = {"Day", "Night"};

const char *compute_weekday(int minutes) {
  int tmp = (minutes / (60 * 24)) % 7;
  return weekdays[tmp];
}

const char *compute_time_band(int minutes) {
  int idx = 0;
  int tmp = minutes % (60 * 24);
  if (tmp >= 6 * 60 && tmp < 18 * 60) {
    idx = 0;
  } else {
    idx = 1;
  }
  return times[idx];
}

int main() {
  int wait_hatch;
  int num_of_egg;
  int stage_life;
  char mutation_weekday[4];
  char mutation_time[6];
  int inv_mutation_prob;
  int total_stages;
  double probs[101];
  while (~scanf("%d %d %d %s %s %d %d", &wait_hatch, &num_of_egg, &stage_life,
                mutation_weekday, mutation_time, &inv_mutation_prob,
                &total_stages)) {

    if (total_stages == 0)
      break;

    probs[0] = 1.0;
    for (int i = 0; i < num_of_egg; i++) {
      probs[i + 1] = probs[i] * (1.0 - 1.0 / inv_mutation_prob);
    }

    double res = 0.0;
    for (int start = 0; start < 60 * 24 * 7; start++) {
      int init_start = start;
      double prob = 1.0;

      for (int round = 0; round < total_stages; round++) {
        int time = init_start + wait_hatch;
        bool isok = true;
        while (time-- > init_start) {
          if (strcmp(mutation_weekday, "All") != 0 &&
              strcmp(compute_weekday(time), mutation_weekday) != 0) {
            isok = false;
            break;
          }
          if (strcmp(mutation_time, "All") != 0 &&
              strcmp(compute_time_band(time), mutation_time) != 0) {
            isok = false;
            break;
          }
        }
        if (isok) {
          prob *= probs[num_of_egg];
        }
        init_start += stage_life;
      }
      res = max(res, 1.0 - prob);
    }
    printf("%.10f\\n", res);
  }
}