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

#include <cmath>
#include <iostream>
#include <stdio.h>
using namespace std;
int n, s, t, p, m, inwd;
string inwds, inho;

string change(int ho, int mi) {
  int tmin = 60 * ho + mi;
  if (360 <= tmin && tmin < 360 * 3)
    return "Day";
  else
    return "Night";
}

bool check(int wd, int ho, int mi) {
  if (inwd != -1 && wd != inwd) {
    return false;
  }
  if (inho != "All" && inho != change(ho, mi)) {
    return false;
  }
  return true;
}

int countFuka(int wd, int ho, int mi) {
  int res = 0;

  for (int i = 0; i < m; i++) {
    int flg = true;

    if (!check(wd, ho, mi))
      flg = false;
    mi += s;
    if (mi >= 60) {
      ho += (mi / 60);
      mi %= 60;
    }
    if (ho >= 24) {
      wd += (ho / 24);
      ho %= 24;
    }
    if (wd >= 7)
      wd %= 7;
    if (!check(wd, ho, mi))
      flg = false;
    mi += (t - s);
    if (mi >= 60) {
      ho += (mi / 60);
      mi %= 60;
    }
    if (ho >= 24) {
      wd += (ho / 24);
      ho %= 24;
    }
    if (wd >= 7)
      wd %= 7;
    /*
    for(int j=0;j<t;j++){
      if(j<=s&&!check(wd,ho,mi))flg=false;
      mi++;
      if(mi==60){mi=0;ho++;}
      if(ho==24){ho=0;wd++;}
      if(wd==7){wd=0;}
    }
    */
    if (flg)
      res++;
  }

  return res;
}

int main() {
  while (cin >> s >> n >> t >> inwds >> inho >> p >> m) {
    if (inwds == "None")
      break;
    if (inwds == "All")
      inwd = -1;
    if (inwds == "Mon")
      inwd = 0;
    if (inwds == "Tue")
      inwd = 1;
    if (inwds == "Wed")
      inwd = 2;
    if (inwds == "Thu")
      inwd = 3;
    if (inwds == "Fri")
      inwd = 4;
    if (inwds == "Sat")
      inwd = 5;
    if (inwds == "Sun")
      inwd = 6;

    int maxm = 0;
    int WD = inwd, H, M = 0;
    if (WD == -1)
      WD = 0;

    if (inho == "All")
      H = 0;
    else if (inho == "Day")
      H = 6;
    else
      H = 18;

    for (int i = 0; i <= t; i++) {
      maxm = max(maxm, countFuka(WD, H, M));
      M++;
      if (M == 60) {
        M = 0;
        H++;
      }
      if (H == 24) {
        H = 0;
        WD++;
      }
      if (WD == 7)
        WD = 0;
    }

    if (inho == "Night") {
      WD = inwd;
      if (WD == -1)
        WD = 0;
      H = 0;
      M = 0;
      if (WD == -1)
        WD = 0;
      for (int i = 0; i <= t; i++) {
        maxm = max(maxm, countFuka(WD, H, M));
        M++;
        if (M == 60) {
          M = 0;
          H++;
        }
        if (H == 24) {
          H = 0;
          WD++;
        }
        if (WD == 7)
          WD = 0;
      }
    }

    // cout<<maxm<<endl;
    double rate = 1.0 - 1.0 / (double)p; // hukasinaikakuritu
    double totalnochange, localnochange;
    localnochange = pow(rate, n);
    totalnochange = pow(localnochange, maxm);
    printf("%.10f\\n", 1.0 - totalnochange);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <stdio.h>
using namespace std;
int n, s, t, p, m, inwd;
string inwds, inho;

string change(int ho, int mi) {
  int tmin = 60 * ho + mi;
  if (360 <= tmin && tmin < 360 * 3)
    return "Day";
  else
    return "Night";
}

bool check(int wd, int ho, int mi) {
  if (inwd != -1 && wd != inwd) {
    return false;
  }
  if (inho != "All" && inho != change(ho, mi)) {
    return false;
  }
  return true;
}

int countFuka(int wd, int ho, int mi) {
  int res = 0;

  for (int i = 0; i < m; i++) {
    int flg = true;
    for (int j = 0; j < t; j++) {
      if (j <= s && !check(wd, ho, mi))
        flg = false;
      mi++;
      if (mi == 60) {
        mi = 0;
        ho++;
      }
      if (ho == 24) {
        ho = 0;
        wd++;
      }
      if (wd == 7) {
        wd = 0;
      }
    }
    if (flg)
      res++;
  }

  return res;
}

int main() {
  while (cin >> s >> n >> t >> inwds >> inho >> p >> m) {
    if (inwds == "None")
      break;
    if (inwds == "All")
      inwd = -1;
    if (inwds == "Mon")
      inwd = 0;
    if (inwds == "Tue")
      inwd = 1;
    if (inwds == "Wed")
      inwd = 2;
    if (inwds == "Thu")
      inwd = 3;
    if (inwds == "Fri")
      inwd = 4;
    if (inwds == "Sat")
      inwd = 5;
    if (inwds == "Sun")
      inwd = 6;

    int maxm = 0;
    int WD = inwd, H, M = 0;
    if (WD == -1)
      WD = 0;

    if (inho == "All")
      H = 0;
    else if (inho == "Day")
      H = 6;
    else
      H = 18;

    for (int i = 0; i <= t; i++) {
      maxm = max(maxm, countFuka(WD, H, M));
      M++;
      if (M == 60) {
        M = 0;
        H++;
      }
      if (H == 24) {
        H = 0;
        WD++;
      }
      if (WD == 7)
        WD = 0;
    }

    if (inho == "Night") {
      WD = inwd;
      if (WD == -1)
        WD = 0;
      H = 0;
      M = 0;
      if (WD == -1)
        WD = 0;
      for (int i = 0; i <= t; i++) {
        maxm = max(maxm, countFuka(WD, H, M));
        M++;
        if (M == 60) {
          M = 0;
          H++;
        }
        if (H == 24) {
          H = 0;
          WD++;
        }
        if (WD == 7)
          WD = 0;
      }
    }

    // cout<<maxm<<endl;
    double rate = 1.0 - 1.0 / (double)p; // hukasinaikakuritu
    double totalnochange, localnochange;
    localnochange = pow(rate, n);
    totalnochange = pow(localnochange, maxm);
    printf("%.10f\\n", 1.0 - totalnochange);
  }
  return 0;
}