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

#include <bitset>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int mm;
    size_t nn;
    scanf("%d %ld", &mm, &nn);
    const auto m = mm;
    const auto n = nn;
    if (m == 0) {
      break;
    }
    constexpr auto l = 7368791 + 1;
    bitset<l> b;
    vector<int> s;
    const auto li = sqrt(l);
    for (auto i = 0; i < m; ++i) {
      b[i] = true;
    }
    auto head = m;
    while (true) {
      size_t nexthead = 0;
      for (size_t i = head; i < b.size(); i += head) {
        b[i] = true;
      }
      for (size_t j = head; true; ++j) {
        if (!b[j]) {
          nexthead = j;
          break;
        }
      }
      s.push_back(head);
      head = nexthead;
      if (s.size() == n) {
        break;
      }
      // if (li < head) {
      //    for (size_t i = head; i < b.size(); ++i) {
      //        if (!b[i]) {
      //            s.push_back(i);
      //        }
      //        if (s.size() > n) {
      //            break;
      //        }
      //    }
      //    break;
      //}
    }
    printf("%d\\n", head);
  }
}

Error: TLE

Fixed submission:

#include <bitset>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int mm;
    size_t nn;
    scanf("%d %ld", &mm, &nn);
    const auto m = mm;
    const auto n = nn;
    if (m == 0) {
      break;
    }
    constexpr auto l = 7368791 + 1;
    using T = bitset<l>;
    bitset<l> b;
    vector<int> s;
    const auto li = sqrt(l);
    for (auto i = 0; i < m; ++i) {
      b[i] = true;
    }
    auto head = m;
    while (true) {
      auto p = [](const T &t, bool b, size_t m = 30) {
        for (size_t i = 1; i < m; ++i) {
          if (t[i] == b) {
            printf("%ld ", i);
          } else {
            printf("  ");
          }
        }
        putchar('\\n');
      };
      auto getHead = [&b]() {
        int head = 0;
        for (size_t i = 0; i < b.size(); ++i) {
          if (!b[i]) {
            head = i;
            break;
          }
        }
        return head;
      };

      bool found = false;
      size_t nexthead = 0;
      for (size_t i = head; i < b.size(); ++i) {
        if (i % head == 0 && !b[i]) {
          b[i] = true;
        } else {
          if (!found && !b[i]) {
            nexthead = i;
            found = true;
            // printf("head = %d, nexthead = %ld\\n", head,
            // nexthead);
          }
        }
      }
      s.push_back(head);
      // p(b, false);
      // p(s, true, 100);
      head = nexthead;
      // printf("current ");
      // for (size_t i = 0; i < 20; ++i) {
      //    if (s[i]) {
      //        printf("%ld ", i);
      //    }
      //}
      // putchar('\\n');
      // printf("head = %d\\n", head);
      if (s.size() > n) {
        break;
      }
      if (li < head) {
        for (size_t i = head; i < b.size(); ++i) {
          if (!b[i]) {
            s.push_back(i);
          }
          if (s.size() > n) {
            break;
          }
        }
        break;
      }
    }
    // printf("output\\n");
    printf("%d\\n", s[n]);
    // size_t count = 0;

    // for (size_t i = 0; i < s.size(); ++i) {
    //    if (s[i]) {
    //        // printf("%ld ", i);
    //        ++count;
    //    }
    //    if (count == n + 1) {
    //        printf("%ld\\n", i);
    //        break;
    //    }
    //}
    // putchar('\\n');
  }
}