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

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

int f(int N, int K) {
  if (N == 1)
    return 0;
  return (f(N - 1, K) + K) % N;
}

int main() {
  int n, k, m;
  while (cin >> n >> k >> m && n && k && m) {
    n--;
    int ans = (f(n, k) + m + 1) % (n + 1);
    if (!ans)
      ans = n + 1;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

int main() {
  int n, k, m;
  while (cin >> n >> k >> m && n && k && m) {
    int a[10001] = {};
    int now = m;
    for (int i = 0; i < n - 1; i++) {
      if (now == 0)
        now = 1;
      a[now] = 1;
      for (int j = 0; j < k; j++) {
        now = (now + 1) % (n + 1);
        ;
        if (now == 0)
          now = 1;
        while (a[now]) {
          now = (now + 1) % (n + 1);
          if (now == 0)
            now = 1;
        }
      }
    }
    cout << now << endl;
  }
}