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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

int S, N, T;
string wkd, timea;
int P, M;
map<string, int> m;
int wkdNum;
int timeInfo;

const double EPS = 1e-8;
bool EQ(double a, double b) { return abs(a - b) < EPS; }

double simu2(int sweek, int stime) {
  double res = 0;
  int nowTime = stime;
  int nowWeek = sweek;
  int cnt = 0;
  // M stage実験する
  for (int i = 0; i < M; i++) {
    if (!(wkdNum == 7 || nowWeek == wkdNum)) {
    }
    // どちらもallなら無条件でOK
    else if (wkdNum == 7 && timeInfo == 0)
      cnt++;
    else {
      bool ok = false;
      const int from = nowTime;
      const int to = nowTime + S;
      // s秒後、次の曜日になる場合
      if (to >= 24 * 60) {
        if (from < 18 * 60) {
        }
        // weekはallでないとx
        else if (wkdNum != 7) {
        } else {
          if (timeInfo == 1) {
          } else {
            if (to - 24 * 60 < 6 * 60)
              ok = true;
            else {
            }
          }
        }
      }
      // そうでない場合
      else {
        if (timeInfo == 0)
          ok = true;
        else if (from >= 0 && to < 6 * 60 && timeInfo == 2)
          ok = true;
        else if (from >= 6 * 60 && to < 18 * 60 && timeInfo == 1)
          ok = true;
        else if (from >= 18 * 60 && to < 24 * 60 && timeInfo == 2)
          ok = true;
      }
      if (ok)
        cnt++;
    }
    if (nowTime + T >= 24 * 60)
      nowWeek = (nowWeek + 1) % 7;
    nowTime = (nowTime + T) % (24 * 60);
  }
  const double a = 1.0 / P;
  const double b = (1 - pow((1 - a), N));
  res = (1 - pow((1 - b), cnt));
  return res;
}

int main() {
  m["Sun"] = 0;
  m["Mon"] = 1;
  m["Tue"] = 2;
  m["Wed"] = 3;
  m["Thu"] = 4;
  m["Fri"] = 5;
  m["Sat"] = 6;
  while (cin >> S >> N >> T >> wkd >> timea >> P >> M && N) {
    if (wkd == "All")
      wkdNum = 7;
    else
      wkdNum = m[wkd];
    if (timea == "All")
      timeInfo = 0;
    else if (timea == "Day")
      timeInfo = 1;
    else
      timeInfo = 2;
    double res = 0;
    // 開始設定
    for (int i = 0; i < 7; i++)
      for (int j = 0; j < 24 * 60; j++)
        res = max(res, simu2(i, j)); // この設定でsimulate
    printf("%.10f\\n", res);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

int S, N, T;
string wkd, timea;
int P, M;
map<string, int> m;
int wkdNum;
int timeInfo;

bool isChange(int sweek, int stime) {
  bool change = false;
  if (wkdNum == 7 || sweek == wkdNum) {
    if (timeInfo == 0)
      change = true;
    else if (timeInfo == 1) {
      if (stime >= 6 * 60 && stime < 18 * 60)
        change = true;
    } else {
      if (stime >= 18 * 60 || stime < 6 * 60)
        change = true;
    }
  }
  return change;
}

double simu(int sweek, int stime) {
  double res = 0;
  int nowTime = stime;
  int nowWeek = sweek;
  int cnt = 0;
  for (int i = 0; i < M; i++) {
    bool change = isChange(nowWeek, nowTime);
    bool ok = false;
    for (int j = 1; j <= T; j++) {
      nowTime = nowTime + 1;
      // timeをすすめる
      if (nowTime >= 24 * 60) {
        nowTime = 0;
        nowWeek = (nowWeek + 1) % 7;
      }
      change &= isChange(nowWeek, nowTime);
      if (j == S) {
        if (change) {
          ok = true;
          break;
        }
      }
    }
    // okがtrueなら、計算
    if (ok)
      cnt++;
  }
  double a = 1.0 / P;
  double b = (1 - pow((1 - a), N));
  res = (1 - pow((1 - b), cnt));
  return res;
}

int main() {

  m["Sun"] = 0;
  m["Mon"] = 1;
  m["Tue"] = 2;
  m["Wed"] = 3;
  m["Thu"] = 4;
  m["Fri"] = 5;
  m["Sat"] = 6;
  while (cin >> S >> N >> T >> wkd >> timea >> P >> M && N) {
    if (wkd == "All")
      wkdNum = 7;
    else
      wkdNum = m[wkd];
    if (timea == "All")
      timeInfo = 0;
    else if (timea == "Day")
      timeInfo = 1;
    else
      timeInfo = 2;
    double res = 0;
    // 開始設定
    for (int i = 0; i < 7; i++) {
      for (int j = 0; j < 60 * 24; j++) {
        // この設定でsimulate
        res = max(res, simu(i, j));
      }
    }
    printf("%.10f\\n", res);
  }

  return 0;
}