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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int main(void) {
  while (1) {
    int n;
    int k;
    int m;
    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0) {
      break;
    }

    Vec<int> list(n, 1);

    int remain = n;
    int active;
    int now = m % n;
    int check;

    list[now] = 0;
    remain--;

    while (remain > 1) {
      active = k % remain;
      if (active == 0) {
        active = k;
      }
      // cout << "ACTIVE = " << active << endl;
      // cout << "HERE" << endl;
      // cout << "REM = " << remain << endl;
      while (1) {
        if (now == n) {
          now = 0;
        }
        check = now;
        // cout << "CHECK = " << check << endl;
        if (list[check] == 1) {
          active--;
          if (active == 0) {
            list[check] = 0;
            now = check;
            // cout << "CHECK_END = " << check << endl;
            break;
          }
        }
        now++;
      }
      remain--;
    }

    for (int i = 0; i < n; i++) {
      if (list[0] == 1) {
        cout << n << endl;
        break;
      }
      if (list[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define INF (int)1e09

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set; // insert erase find count
using std::setprecision;
using std::stack;
using std::string;
using std::vector;

template <typename T> using Vec = vector<T>; // c++11
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> PIII;

int main(void) {
  while (1) {
    int n;
    int k;
    int m;
    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0) {
      break;
    }

    Vec<int> list(n, 1);

    int remain = n;
    int active;
    int now = m % n;
    int check;

    list[now] = 0;
    remain--;

    while (remain > 1) {
      active = k % remain;
      if (active == 0) {
        active = k;
      }
      // cout << "ACTIVE = " << active << endl;
      // cout << "HERE" << endl;
      // cout << "REM = " << remain << endl;
      while (1) {
        check = now % n;
        // cout << "CHECK = " << check << endl;
        if (list[check] == 1) {
          active--;
          if (active == 0) {
            list[check] = 0;
            now = check;
            // cout << "CHECK_END = " << check << endl;
            break;
          }
        }
        now++;
      }
      remain--;
    }

    for (int i = 0; i < n; i++) {
      if (list[0] == 1) {
        cout << n << endl;
        break;
      }
      if (list[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}