Problem p03317 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
#define COUT(v) cout << (v) << endl
#define CIN(n)                                                                 \
  int(n);                                                                      \
  cin >> (n)
#define SCIN(n)                                                                \
  string(n);                                                                   \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)

#define FOREACH(x, a) for (auto &(x) : (a))

#define ALL(obj) (obj).begin(), (obj).end()

#define P pair<int, int>
#define I vector<int>
#define S set<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)), a.erase(unique(ALL(a)), a.end())

typedef string::const_iterator State;
class PalseError {};
class Edge {
public:
  int from, to, value;
  Edge(int a, int b, int c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(int a, int b) {
    from = a;
    to = b;
  }
};

int main() {
  CIN(N);
  CIN(K);
  I vec;
  int iti;
  REP(a, N) {
    CIN(b);
    if (b == 1)
      iti = a;
    vec.pb(b);
  }
  LL ans = 0;
  K--;
  ans += (iti % (K) == 0 ? iti / (K) : iti / (K) + 1);
  int w = iti % K;
  iti++;
  ans += ((N - iti) % (K) == 0 ? (N - iti) / (K) : (N - iti) / (K) + 1);
  w += (N - iti) % K;
  COUT(ans - w / (K - 1));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
#define COUT(v) cout << (v) << endl
#define CIN(n)                                                                 \
  int(n);                                                                      \
  cin >> (n)
#define SCIN(n)                                                                \
  string(n);                                                                   \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)

#define FOREACH(x, a) for (auto &(x) : (a))

#define ALL(obj) (obj).begin(), (obj).end()

#define P pair<int, int>
#define I vector<int>
#define S set<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)), a.erase(unique(ALL(a)), a.end())

typedef string::const_iterator State;
class PalseError {};
class Edge {
public:
  int from, to, value;
  Edge(int a, int b, int c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(int a, int b) {
    from = a;
    to = b;
  }
};

int main() {
  CIN(N);
  CIN(K);
  I vec;
  int iti;
  REP(a, N) {
    CIN(b);
    if (b == 1)
      iti = a;
    vec.pb(b);
  }
  N--;
  int ans = 0;
  ans = N / (K - 1);
  if (N % (K - 1) != 0)
    ans++;
  COUT(ans);
  return 0;
}