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

#ifndef _WIN32
#include <iostream>
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed;
    cout << setprecision(10);
  }
} star;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
typedef pair<LL, LL> P;
typedef vector<LL> V;
const int INF = 1e7;
int s, n, t, m;
double p;
string week, nobu;
int ok[114514];
int sum[114514];
const int WEEK = 10080;
const int DAY = 1440;
string ww[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void init() {
  REP(i, 114514) {
    ok[i] = 0;
    sum[i] = 0;
  }
  REP(i, 7) if (week == ww[i] || week == "All") {
    if (nobu == "All" || nobu == "Night") {
      int bg = i * DAY + 0;
      int ed = i * DAY + 360;
      FOR(j, bg, ed) ok[j] = 1;
    }
    if (nobu == "All" || nobu == "Night") {
      int bg = i * DAY + 1080;
      int ed = i * DAY + 1440;
      FOR(j, bg, ed) ok[j] = 1;
    }
    if (nobu == "All" || nobu == "Day") {
      int bg = i * DAY + 360;
      int ed = i * DAY + 1080;
      FOR(j, bg, ed) ok[j] = 1;
    }
  }
  REP(i, WEEK) ok[i + WEEK] = ok[i];
  REP(i, 2 * WEEK) sum[i + 1] = sum[i] + ok[i];
}

int main() {
  while (cin >> s >> n >> t >> week >> nobu >> p >> m, week != "None") {
    int res = 0;
    init();
    REP(i, WEEK) {
      int now = i;
      int ans = 0;
      REP(j, m) {
        if (sum[now + s + 1] - sum[now] == s + 1)
          ans++;
        now += t;
        now %= WEEK;
      }
      if (ans > res)
        res = ans;
    }
    p = 1.0 - 1.0 / p;
    double pp = 1;
    REP(i, n) pp *= p;
    double ppp = 1;
    REP(i, res) ppp *= pp;
    cout << 1 - ppp << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#ifndef _WIN32
#include <iostream>
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed;
    cout << setprecision(10);
  }
} star;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
typedef pair<LL, LL> P;
typedef vector<LL> V;
const int INF = 1e7;
int s, n, t, m;
double p;
string week, nobu;
int ok[114514];
const int WEEK = 10080;
const int DAY = 1440;
string ww[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void init() {
  REP(i, 114514) { ok[i] = 0; }
  REP(i, 7) if (week == ww[i] || week == "All") {
    if (nobu == "All" || nobu == "Night") {
      int bg = i * DAY + 0;
      int ed = i * DAY + 360;
      FOR(j, bg, ed) ok[j] = 1;
    }
    if (nobu == "All" || nobu == "Night") {
      int bg = i * DAY + 1080;
      int ed = i * DAY + 1440;
      FOR(j, bg, ed) ok[j] = 1;
    }
    if (nobu == "All" || nobu == "Day") {
      int bg = i * DAY + 360;
      int ed = i * DAY + 1080;
      FOR(j, bg, ed) ok[j] = 1;
    }
  }
}

int main() {
  while (cin >> s >> n >> t >> week >> nobu >> p >> m, week != "None") {
    int res = 0;
    init();
    REP(i, WEEK) {
      int now = i;
      int ans = 0;
      REP(j, m) {
        int ch = 1;
        REP(k, s + 1) { ch &= ok[(now + k) % WEEK]; }
        ans += ch;
        now += t;
        now %= WEEK;
      }
      if (ans > res)
        res = ans;
    }
    p = 1.0 - 1.0 / p;
    double pp = 1;
    REP(i, n) pp *= p;
    double ppp = 1;
    REP(i, res) ppp *= pp;
    cout << 1 - ppp << endl;
  }
  return 0;
}