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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

typedef list<int> li;
void add(li::iterator &it, li &lst) {
  it++;
  if (it == lst.end()) {
    it = lst.begin();
  }
}
void reduce(li::iterator &it, li &lst) {
  if (it == lst.begin()) {
    it = lst.end();
  }
  it--;
}
int main() {
  int n, k, m;
  while (cin >> n >> k >> m, n | k | m) {
    li l(n);
    int a = 1;
    for (li::iterator it = l.begin(); it != l.end(); it++) {
      (*it) = a;
      a++;
    }
    li::iterator it = l.begin();
    REP(j, m - 1) { add(it, l); }
    it = l.erase(it);
    reduce(it, l);
    int sz = l.size();
    REP(i, n - 2) {
      REP(j, k % sz) { add(it, l); }
      it = l.erase(it);
      reduce(it, l);
      sz--;
    }
    cout << (*it) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

typedef list<int> li;
void add(li::iterator &it, li &lst) {
  it++;
  if (it == lst.end()) {
    it = lst.begin();
  }
}
void reduce(li::iterator &it, li &lst) {
  if (it == lst.begin()) {
    it = lst.end();
  }
  it--;
}
int main() {
  int n, k, m;
  while (cin >> n >> k >> m, n | k | m) {
    li l(n);
    int a = 1;
    for (li::iterator it = l.begin(); it != l.end(); it++) {
      (*it) = a;
      a++;
    }
    li::iterator it = l.begin();
    REP(j, m - 1) { add(it, l); }
    it = l.erase(it);
    reduce(it, l);
    REP(i, n - 2) {
      REP(j, k % l.size()) { add(it, l); }
      it = l.erase(it);
      reduce(it, l);
    }
    cout << (*it) << endl;
  }
}