prithivMLmods/Coder-Stat huggingface dataset:
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
constexpr int INF = (int)1e9;
int d[10000][3];
signed main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
  }
  int S, T;
  cin >> S >> T;
  S--;
  T--;
  REP(i, N) REP(j, 3) d[i][j] = INF;
  queue<pair<int, int>> Q;
  Q.emplace(S, 0);
  d[S][0] = 0;
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    int u = p.first;
    int m = p.second;
    for (int v : G[u]) {
      int m1 = (m + 1) % 3;
      if (d[v][m1] > d[u][m] + 1) {
        d[v][m1] = d[u][m] + 1;
        Q.emplace(v, m1);
      }
    }
  }
  if (d[T][0] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[T][0] / 3 << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
constexpr int INF = (int)1e9;
int d[100000][3];
signed main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
  }
  int S, T;
  cin >> S >> T;
  S--;
  T--;
  REP(i, N) REP(j, 3) d[i][j] = INF;
  queue<pair<int, int>> Q;
  Q.emplace(S, 0);
  d[S][0] = 0;
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    int u = p.first;
    int m = p.second;
    for (int v : G[u]) {
      int m1 = (m + 1) % 3;
      if (d[v][m1] > d[u][m] + 1) {
        d[v][m1] = d[u][m] + 1;
        Q.emplace(v, m1);
      }
    }
  }
  if (d[T][0] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[T][0] / 3 << endl;
  }
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(a, b, c) for (int a = b; a <= c; ++a)
#define Dor(a, b, c) for (int a = b; a >= c; --a)
using namespace std;
const int N = 100007;
int n, m, s, e, hd[N], nt[N], to[N], Q[N * 3], D[N * 3], F[N * 3], B[N][3];
int main() {
  scanf("%d%d", &n, &m);
  For(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    nt[i] = hd[x], hd[x] = i, to[i] = y;
  }
  scanf("%d%d", &s, &e);
  Q[1] = s, D[1] = 0;
  int h = 0, t = 1;
  while (h < t) {
    int u = Q[++h], d = D[h];
    for (int i = hd[u]; i; i = nt[i])
      if (!B[to[i]][(d + 1) % 3]) {
        Q[++t] = to[i], D[t] = (d + 1) % 3, F[t] = F[h] + 1;
        if (Q[t] == e && !D[t]) {
          printf("%d\n", F[t] / 3);
          return 0;
        }
      }
  }
  puts("-1");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(a, b, c) for (int a = b; a <= c; ++a)
#define Dor(a, b, c) for (int a = b; a >= c; --a)
using namespace std;
const int N = 100007;
int n, m, s, e, hd[N], nt[N], to[N], Q[N * 3], D[N * 3], F[N * 3], B[N][3];
int main() {
  scanf("%d%d", &n, &m);
  For(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    nt[i] = hd[x], hd[x] = i, to[i] = y;
  }
  scanf("%d%d", &s, &e);
  Q[1] = s, D[1] = 0;
  int h = 0, t = 1;
  while (h < t) {
    int u = Q[++h], d = D[h];
    for (int i = hd[u]; i; i = nt[i])
      if (!B[to[i]][(d + 1) % 3]) {
        Q[++t] = to[i], D[t] = (d + 1) % 3, F[t] = F[h] + 1,
        B[to[i]][(d + 1) % 3] = 1;
        if (Q[t] == e && !D[t]) {
          printf("%d\n", F[t] / 3);
          return 0;
        }
      }
  }
  puts("-1");
  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll dis[100001], cur;
vector<ll> v[N];
queue<ll> q;

void dfs(ll x, ll power) {

  if (power == 0) {
    if (dis[x] > cur + 1) {
      dis[x] = cur + 1;
      q.push(x);
    }
    return;
  }

  for (auto node : v[x])
    dfs(node, power - 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, s, t;
  cin >> n >> m;
  fill(dis, dis + n + 1, inf);

  for (ll i = 1; i <= m; i++) {
    ll a, b;
    cin >> a >> b;
    v[b].push_back(a);
  }

  cin >> s >> t;
  q.push(t);
  dis[t] = 0;

  while (!q.empty()) {
    ll x = q.front();
    q.pop();
    cur = dis[x];

    dfs(x, 3);
  }

  if (dis[s] == inf)
    cout << "-1\n";
  else
    cout << dis[s] << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 1000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
ll dis[100001], cur;
vector<ll> v[N];
queue<ll> q;

void dfs(ll x, ll power) {

  if (power == 0) {
    if (dis[x] > cur + 1) {
      dis[x] = cur + 1;
      q.push(x);
    }
    return;
  }

  for (auto node : v[x])
    dfs(node, power - 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, s, t;
  cin >> n >> m;
  fill(dis, dis + n + 1, inf);

  for (ll i = 1; i <= m; i++) {
    ll a, b;
    cin >> a >> b;
    v[b].push_back(a);
  }

  cin >> s >> t;
  q.push(t);
  dis[t] = 0;

  while (!q.empty()) {
    ll x = q.front();
    q.pop();
    cur = dis[x];

    dfs(x, 3);
    if (dis[s] != inf)
      break;
  }

  if (dis[s] == inf)
    cout << "-1\n";
  else
    cout << dis[s] << "\n";

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int n, m, s, t;
vector<int> adjl[100002];
int dist[100002][3];
queue<pair<int, int>> bfs;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dist, -1, sizeof dist);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adjl[u].push_back(v);
  }
  cin >> s >> t;
  bfs.push({s, 0});
  while (!bfs.empty()) {
    int now = bfs.front().first;
    int distnow = bfs.front().second;
    bfs.pop();
    if (dist[now][distnow % 3] != -1 && dist[now][distnow % 3] < distnow)
      continue;
    dist[now][distnow % 3] = distnow;
    for (int next : adjl[now])
      bfs.push({next, distnow + 1});
  }
  cout << (dist[t][0] == -1 ? -1 : dist[t][0] / 3) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m, s, t;
vector<int> adjl[100002];
int dist[100002][3];
queue<pair<int, int>> bfs;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dist, -1, sizeof dist);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adjl[u].push_back(v);
  }
  cin >> s >> t;
  bfs.push({s, 0});
  while (!bfs.empty()) {
    int now = bfs.front().first;
    int distnow = bfs.front().second;
    bfs.pop();
    if (dist[now][distnow % 3] != -1)
      continue;
    dist[now][distnow % 3] = distnow;
    for (int next : adjl[now])
      bfs.push({next, distnow + 1});
  }
  cout << (dist[t][0] == -1 ? -1 : dist[t][0] / 3) << "\n";
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n);
  REP(i, n) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  s--, t--;

  vector<vector<ll>> dist(n, vector<ll>(3, -1));
  dist[s][0] = 0;
  queue<P> que;
  que.emplace(s, 0);
  while (!que.empty()) {
    P cur = que.front();
    que.pop();
    int v = cur.first;
    int step = cur.second;
    for (auto nv : G[v]) {
      int np = (step + 1) % 3;
      if (dist[nv][np] == -1) {
        dist[nv][np] = dist[v][step] + 1;
        que.emplace(nv, np);
      }
    }
  }

  cout << (dist[t][0] == -1 ? -1 : dist[t][0] / 3) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> G(n);
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
  }
  int s, t;
  cin >> s >> t;
  s--, t--;

  vector<vector<ll>> dist(n, vector<ll>(3, -1));
  dist[s][0] = 0;
  queue<P> que;
  que.emplace(s, 0);
  while (!que.empty()) {
    P cur = que.front();
    que.pop();
    int v = cur.first;
    int step = cur.second;
    for (auto nv : G[v]) {
      int np = (step + 1) % 3;
      if (dist[nv][np] == -1) {
        dist[nv][np] = dist[v][step] + 1;
        que.emplace(nv, np);
      }
    }
  }

  cout << (dist[t][0] == -1 ? -1 : dist[t][0] / 3) << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using P = pair<int, int>;
const int mx = 100005;
int n, m, u, v, s, t, dis[mx * 3];
queue<int> qu;
vector<int> node[mx];
int main() {
  scanf("%d%d", &n, &m);
  rep(i, m) {
    scanf("%d%d", &u, &v);
    u--;
    v--;
    node[u].push_back(v + mx);
    node[u + mx].push_back(v + mx * 2);
    node[u + mx * 2].push_back(v);
  }
  scanf("%d%d", &s, &t);
  s--;
  t--;
  qu.push(s);
  dis[s] = 1;
  while (!qu.empty()) {
    int pos = qu.front();
    qu.pop();
    for (int it : node[pos]) {
      if (dis[it] == 0) {
        qu.push(it);
        dis[it] = dis[pos] + 1;
      }
    }
  }
  printf("%d\n", dis[t] ? dis[t] / 3 : -1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using P = pair<int, int>;
const int mx = 100005;
int n, m, u, v, s, t, dis[mx * 3];
queue<int> qu;
vector<int> node[mx * 3];
int main() {
  scanf("%d%d", &n, &m);
  rep(i, m) {
    scanf("%d%d", &u, &v);
    u--;
    v--;
    node[u].push_back(v + mx);
    node[u + mx].push_back(v + mx * 2);
    node[u + mx * 2].push_back(v);
  }
  scanf("%d%d", &s, &t);
  s--;
  t--;
  qu.push(s);
  dis[s] = 1;
  while (!qu.empty()) {
    int pos = qu.front();
    qu.pop();
    for (int it : node[pos]) {
      if (dis[it] == 0) {
        qu.push(it);
        dis[it] = dis[pos] + 1;
      }
    }
  }
  printf("%d\n", dis[t] ? dis[t] / 3 : -1);
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>

#define MAX_N 100000
#define INF (1 << 29)

using namespace std;

typedef pair<int, int> P;

int N, M;
vector<int> G[3 * MAX_N + 1];

int d[MAX_N + 1];

int dijkstra(int s, int t, int N) {
  priority_queue<P> que;
  for (int v = 1; v <= N; v++) {
    d[v] = (v == s) ? 0 : INF;
    que.push(P(-d[v], v));
  }

  while (!que.empty()) {
    int u = que.top().second;
    que.pop();

    for (auto v : G[u]) {
      int alt = d[u] + 1;
      if (d[v] > alt) {
        d[v] = alt;
        que.push(P(-d[v], v));
      }
    }
  }

  return d[t] >= INF ? -1 : d[t];
}

int main(int argc, char **argv) {

  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;

    int us[] = {u, u + N, u + 2 * N};
    int vs[] = {v, v + N, v + 2 * N};

    // u0->v1; u1->v2; u2->v0;
    for (int i = 0; i < 3; i++)
      G[us[i]].push_back(vs[(i + 1) % 3]);
  }

  int S, T;
  cin >> S >> T;

  int ans = dijkstra(S, T, 3 * N);
  cout << (ans < 0 ? ans : ans / 3) << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>

#define MAX_N 100000
#define INF (1 << 29)

using namespace std;

typedef pair<int, int> P;

int N, M;
vector<int> G[3 * MAX_N + 1];

int d[3 * MAX_N + 1];

int dijkstra(int s, int t, int N) {
  priority_queue<P> que;
  for (int v = 1; v <= N; v++) {
    d[v] = (v == s) ? 0 : INF;
    que.push(P(-d[v], v));
  }

  while (!que.empty()) {
    int u = que.top().second;
    que.pop();

    for (auto v : G[u]) {
      int alt = d[u] + 1;
      if (d[v] > alt) {
        d[v] = alt;
        que.push(P(-d[v], v));
      }
    }
  }

  return d[t] >= INF ? -1 : d[t];
}

int main(int argc, char **argv) {

  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;

    int us[] = {u, u + N, u + 2 * N};
    int vs[] = {v, v + N, v + 2 * N};

    // u0->v1; u1->v2; u2->v0;
    for (int i = 0; i < 3; i++)
      G[us[i]].push_back(vs[(i + 1) % 3]);
  }

  int S, T;
  cin >> S >> T;

  int ans = dijkstra(S, T, 3 * N);
  cout << (ans < 0 ? ans : ans / 3) << endl;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, N - 1, 0)
#define RREPS(i, n) RFOR(i, N, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5;

signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> Graph[3 * N];
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    Graph[3 * a].pb(3 * b + 1);
    Graph[3 * a + 1].pb(3 * b + 2);
    Graph[3 * a + 2].pb(3 * b);
  }
  vector<int> depth(3 * N, INF);
  int S, T;
  cin >> S >> T;
  S--;
  T--;
  queue<int> q;
  q.push(3 * S);
  depth[3 * S] = 0;
  while (q.size()) {
    int now = q.front();
    q.pop();
    for (auto v : Graph[now]) {
      if (depth[v] < depth[now] + 1)
        continue;
      depth[v] = depth[now] + 1;
      q.push(v);
    }
  }
  cout << (depth[3 * T] == INF ? -1 : depth[3 * T] / 3) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, N - 1, 0)
#define RREPS(i, n) RFOR(i, N, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5;

signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> Graph[3 * N];
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    Graph[3 * a].pb(3 * b + 1);
    Graph[3 * a + 1].pb(3 * b + 2);
    Graph[3 * a + 2].pb(3 * b);
  }
  vector<int> depth(3 * N, INF);
  int S, T;
  cin >> S >> T;
  S--;
  T--;
  queue<int> q;
  q.push(3 * S);
  depth[3 * S] = 0;
  while (q.size()) {
    int now = q.front();
    q.pop();
    for (auto v : Graph[now]) {
      if (depth[v] != INF)
        continue;
      depth[v] = depth[now] + 1;
      q.push(v);
    }
  }
  cout << (depth[3 * T] == INF ? -1 : depth[3 * T] / 3) << endl;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
#define P pair<int, int>
using namespace std;

int main() {
  int a, b, c, d, e, f, i, j, n, m, s, t;
  int dist[100000] = {};
  rep(i, 100000) dist[i]--;

  cin >> n >> m;
  vector<vector<int>> v(3 * n);
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b + n);
    v[a + n].push_back(b + n * 2);
    v[a + 2 * n].push_back(b);
  }
  cin >> s >> t;
  s--;
  t--;
  queue<P> q;
  q.push(P(s, 0));
  dist[s] = 0;

  while (q.size()) {
    int now = q.front().first;
    int step = q.front().second;
    q.pop();

    rep(i, v[now].size()) {
      int next = v[now][i];
      if (dist[next] != -1)
        continue;
      q.push(P(next, step + 1));
      dist[next] = dist[now] + 1;
    }
  }

  e = dist[t];
  if (e != -1)
    e = e / 3;
  cout << e;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
#define P pair<int, int>
using namespace std;

int main() {
  int a, b, c, d, e, f, i, j, n, m, s, t;
  int dist[300000] = {};
  rep(i, 300000) dist[i]--;

  cin >> n >> m;
  vector<vector<int>> v(3 * n);
  rep(i, m) {
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b + n);
    v[a + n].push_back(b + n * 2);
    v[a + 2 * n].push_back(b);
  }
  cin >> s >> t;
  s--;
  t--;
  queue<P> q;
  q.push(P(s, 0));
  dist[s] = 0;

  while (q.size()) {
    int now = q.front().first;
    int step = q.front().second;
    q.pop();

    rep(i, v[now].size()) {
      int next = v[now][i];
      if (dist[next] != -1)
        continue;
      q.push(P(next, step + 1));
      dist[next] = dist[now] + 1;
    }
  }

  e = dist[t];
  if (e != -1)
    e = e / 3;
  cout << e;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  // 頂点数と辺数
  int N, M;
  cin >> N >> M;

  // グラフ入力受取 (ここでは無向グラフを想定)
  Graph G(N), g(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;
  // BFS のためのデータ構造
  vector<int> dist(N, -1), seen(N, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[s] = 0;
  que.push(s); // 0 を橙色頂点にする

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      for (int nnv : G[nv]) {
        for (int nnnv : G[nnv]) {
          if (dist[nnnv] != -1)
            continue;
          dist[nnnv] = dist[v] + 1;
          que.push(nnnv);
        }
      }
    }
  }

  // 結果出力 (各頂点の頂点 0 からの距離を見る)
  cout << dist[t] << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  // 頂点数と辺数
  int N, M;
  cin >> N >> M;

  // グラフ入力受取 (ここでは無向グラフを想定)
  Graph G(N), g(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
  }
  int s, t;
  cin >> s >> t;
  s--;
  t--;
  // BFS のためのデータ構造
  vector<int> dist(N, -1), seen(N, -1); // 全頂点を「未訪問」に初期化
  queue<int> que;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[s] = 0;
  que.push(s); // 0 を橙色頂点にする

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    int v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    // v から辿れる頂点をすべて調べる
    for (int nv : G[v]) {
      for (int nnv : G[nv]) {
        for (int nnnv : G[nnv]) {
          if (dist[nnnv] != -1)
            continue;
          dist[nnnv] = dist[v] + 1;
          if (nnnv == t) {
            cout << dist[nnnv] << endl;
            return 0;
          }
          que.push(nnnv);
        }
      }
    }
  }

  // 結果出力 (各頂点の頂点 0 からの距離を見る)
  cout << dist[t] << endl;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define debug(var) std::cout << #var << " : " << var;

using Graph = std::vector<std::vector<int>>;

int main() {

  int N, M;
  std::cin >> N >> M;
  Graph G(N);
  rep(i, N) {
    int u, v;
    std::cin >> u >> v;
    G[u - 1].push_back(v - 1);
  }
  int S, T;
  std::cin >> S >> T;
  S--;
  T--;

  std::vector<std::array<int, 3>> memo(N, {-1, -1, -1});
  memo[S][0] = 0;
  std::queue<std::pair<int, int>> q;
  q.push(std::make_pair(S, 0));

  while (!q.empty()) {

    auto cur = q.front();
    q.pop();

    for (auto &v : G[cur.first]) {

      int tmp = ((cur.second) + 1) % 3;
      if (memo[v][tmp] != -1)
        continue;
      memo[v][tmp] = memo[cur.first][cur.second] + 1;
      q.push(std::make_pair(v, tmp));
    }
  }

  int ans = memo[T][0];

  if (ans != -1)
    ans /= 3;

  std::cout << ans;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define debug(var) std::cout << #var << " : " << var;

using Graph = std::vector<std::vector<int>>;

int main() {

  int N, M;
  std::cin >> N >> M;
  Graph G(N);
  rep(i, M) {
    int u, v;
    std::cin >> u >> v;
    G[u - 1].push_back(v - 1);
  }
  int S, T;
  std::cin >> S >> T;
  S--;
  T--;

  std::vector<std::array<int, 3>> memo(N, {-1, -1, -1});
  memo[S][0] = 0;
  std::queue<std::pair<int, int>> q;
  q.push(std::make_pair(S, 0));

  while (!q.empty()) {

    auto cur = q.front();
    q.pop();

    for (auto &v : G[cur.first]) {

      int tmp = ((cur.second) + 1) % 3;
      if (memo[v][tmp] != -1)
        continue;
      memo[v][tmp] = memo[cur.first][cur.second] + 1;
      q.push(std::make_pair(v, tmp));
    }
  }

  int ans = memo[T][0];

  if (ans != -1)
    ans /= 3;

  std::cout << ans;

  return 0;
}
Problem p02991 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
int vis[100008];
ll dist[100008];
vector<int> e[100008];
int n;
void add(int u, int v) {
  e[u].pb(v + n);
  e[u + n].pb(v + 2 * n);
  e[u + 2 * n].pb(v);
}

int main() {
  int m, i, j;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
  }
  int s, t;
  cin >> s >> t;
  vis[s] = 1;
  queue<int> q;
  q.push(s);
  int f = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == t)
      break;
    for (auto v : e[u]) {
      if (!vis[v]) {
        q.push(v);
        vis[v] = 1;
        dist[v] = dist[u] + 1;
      }
    }
  }
  if (dist[t] != 0 && dist[t] % 3 == 0) {
    cout << dist[t] / 3;
  } else
    cout << -1;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
int vis[300008];
ll dist[300008];
vector<int> e[300008];
int n;
void add(int u, int v) {
  e[u].pb(v + n);
  e[u + n].pb(v + 2 * n);
  e[u + 2 * n].pb(v);
}

int main() {
  int m, i, j;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
  }
  int s, t;
  cin >> s >> t;
  vis[s] = 1;
  queue<int> q;
  q.push(s);
  int f = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == t)
      break;
    for (auto v : e[u]) {
      if (!vis[v]) {
        q.push(v);
        vis[v] = 1;
        dist[v] = dist[u] + 1;
      }
    }
  }
  if (dist[t] != 0 && dist[t] % 3 == 0) {
    cout << dist[t] / 3;
  } else
    cout << -1;
  return 0;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, n) for (int i = a, _n = n; i <= _n; ++i)
#define drep(i, a, n) for (int i = a, _n = n; i >= _n; --i)
#define debug(x) cout << #x << " = " << x << endl

const int N = 65000;
const int Mod = 1e9 + 7;

int n, k;
int L[N], R[N], Cnt[N];

long long dp[105][N];
int main() {
  scanf("%d%d", &n, &k);
  int y = 1, cnt = 0;
  while (y <= k) {
    int t = k / y;
    int c = k / t;
    L[++cnt] = y, R[cnt] = c;
    y = c + 1;
  }
  rep(i, 1, cnt) dp[1][i] = Cnt[i] = R[i] - L[i] + 1;
  rep(i, 2, n) {
    long long Sum = 0;
    rep(j, 1, cnt) {
      (Sum += dp[i - 1][j]) %= Mod;
      dp[i][cnt - j + 1] = Sum * Cnt[cnt - j + 1] % Mod;
    }
  }
  long long Ans = 0;
  rep(i, 1, cnt)(Ans += dp[n][i]) %= Mod;
  cout << Ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, n) for (int i = a, _n = n; i <= _n; ++i)
#define drep(i, a, n) for (int i = a, _n = n; i >= _n; --i)
#define debug(x) cout << #x << " = " << x << endl

const int N = 65000;
const int Mod = 1e9 + 7;

int n, k;
int L[N], R[N], Cnt[N];

long long dp[105][N];
int main() {
  scanf("%d%d", &n, &k);
  swap(n, k);
  int y = 1, cnt = 0;
  while (y <= k) {
    int t = k / y;
    int c = k / t;
    L[++cnt] = y, R[cnt] = c;
    y = c + 1;
  }
  rep(i, 1, cnt) dp[1][i] = Cnt[i] = R[i] - L[i] + 1;
  rep(i, 2, n) {
    long long Sum = 0;
    rep(j, 1, cnt) {
      (Sum += dp[i - 1][j]) %= Mod;
      dp[i][cnt - j + 1] = Sum * Cnt[cnt - j + 1] % Mod;
    }
  }
  long long Ans = 0;
  rep(i, 1, cnt)(Ans += dp[n][i]) %= Mod;
  cout << Ans << endl;
  return 0;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long N, K;
const int MOD = 1000000007;
long long dp[101][50000] = {};
long long S[50000] = {};
long long num[50000];

int solve() {
  int M = 0;
  for (int i = 1; i <= N;) {
    int j = N / i;
    if (i <= j) {
      num[M++] = 1;
      ++i;
    } else {
      num[M++] = N / j - i + 1;
      i = N / j + 1;
    }
  }

  dp[0][0] = 1;
  for (int k = 0; k < K; ++k) {
    memset(S, 0, sizeof(S));
    for (int j = 0; j < M; ++j)
      S[j + 1] = (S[j] + dp[k][j]) % MOD;
    for (int j = 0; j < M; ++j)
      dp[k + 1][j] = (S[M - j] * num[j]) % MOD;
  }

  int res = 0;
  for (int j = 0; j <= M; ++j)
    res = (res + dp[K][j]) % MOD;
  return res;
}

int main() {
  cin >> N >> K;
  cout << solve() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long N, K;
const int MOD = 1000000007;
long long dp[101][110000] = {};
long long S[110000] = {};
long long num[110000];

int solve() {
  int M = 0;
  for (int i = 1; i <= N;) {
    int j = N / i;
    if (i <= j) {
      num[M++] = 1;
      ++i;
    } else {
      num[M++] = N / j - i + 1;
      i = N / j + 1;
    }
  }

  dp[0][0] = 1;
  for (int k = 0; k < K; ++k) {
    memset(S, 0, sizeof(S));
    for (int j = 0; j < M; ++j)
      S[j + 1] = (S[j] + dp[k][j]) % MOD;
    for (int j = 0; j < M; ++j)
      dp[k + 1][j] = (S[M - j] * num[j]) % MOD;
  }

  int res = 0;
  for (int j = 0; j <= M; ++j)
    res = (res + dp[K][j]) % MOD;
  return res;
}

int main() {
  cin >> N >> K;
  cout << solve() << endl;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
const long long MOD = 1000000000 + 7;
long long dp[40000][110];
int main() {

  int n, k;
  scanf("%d %d", &n, &k);

  int last = n;
  int tmp = 0, ss = 0;
  for (int i = 1; i * i <= n; i++) {
    dp[i][0] = last - n / (i + 1), last = n / (i + 1);
    tmp = i;
    ss += dp[i][0];
  }
  while (n - ss > 0) {
    dp[++tmp][0] = 1;
    ss++;
  }

  // for(int i = 1 ; i <= tmp ; i++) {
  //     printf("%d\n", dp[i][0]);
  // }

  for (int j = 1; j < k; j++) {
    long long sum = 0;
    for (int i = 1; i <= tmp; i++) {
      sum += dp[tmp - i + 1][j - 1];
      sum %= MOD;
      dp[i][j] = sum * dp[i][0] % MOD;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= tmp; i++) {
    ans += dp[i][k - 1] % MOD;
  }
  printf("%lld\n", ans % MOD);

  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
const long long MOD = 1000000000 + 7;
long long dp[100000][110];
int main() {

  int n, k;
  scanf("%d %d", &n, &k);

  int last = n;
  int tmp = 0, ss = 0;
  for (int i = 1; i * i <= n; i++) {
    dp[i][0] = last - n / (i + 1), last = n / (i + 1);
    tmp = i;
    ss += dp[i][0];
  }
  while (n - ss > 0) {
    dp[++tmp][0] = 1;
    ss++;
  }

  // for(int i = 1 ; i <= tmp ; i++) {
  //     printf("%d\n", dp[i][0]);
  // }

  for (int j = 1; j < k; j++) {
    long long sum = 0;
    for (int i = 1; i <= tmp; i++) {
      sum += dp[tmp - i + 1][j - 1];
      sum %= MOD;
      dp[i][j] = sum * dp[i][0] % MOD;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= tmp; i++) {
    ans += dp[i][k - 1] % MOD;
  }
  printf("%lld\n", ans % MOD);

  return 0;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const int mod = 1000000007;
const int inf = 1ll << 61;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int dp[100][100000];
int M2[100005], M3[100005];

signed main() {
  int n, k;
  cin >> n >> k;
  map<int, int> M, M1; // M1...nub->id
  for (int i = 1; i * i <= n; i++) {
    int x = n / i;
    M[i] = n / i;
    M[x] = n / x;
  }
  vector<int> v;
  for (auto itr = M.begin(); itr != M.end(); itr++) {
    v.push_back(itr->first);
  }
  int ld = 0;
  rep(i, v.size()) {
    dp[0][i] = v[i];
    M1[v[i]] = i;
    M2[i] = v[i];
    M3[i] = v[i] - ld;
    ld = v[i];
  }

  rep(i, k) {
    rep(j, v.size()) {
      dp[i + 1][j] += dp[i][M1[n / M2[j]]] * M3[j];
      dp[i + 1][j] %= mod;
    }
    rep(j, v.size()) {
      dp[i + 1][j + 1] += dp[i + 1][j];
      dp[i + 1][j + 1] %= mod;
    }
  }
  cout << dp[k - 1][v.size() - 1] << endl;
}


Fixed submission:
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const int mod = 1000000007;
const int inf = 1ll << 61;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int dp[102][100000];
int M2[100005], M3[100005];

signed main() {
  int n, k;
  cin >> n >> k;
  map<int, int> M, M1; // M1...nub->id
  for (int i = 1; i * i <= n; i++) {
    int x = n / i;
    M[i] = n / i;
    M[x] = n / x;
  }
  vector<int> v;
  for (auto itr = M.begin(); itr != M.end(); itr++) {
    v.push_back(itr->first);
  }
  int ld = 0;
  rep(i, v.size()) {
    dp[0][i] = v[i];
    M1[v[i]] = i;
    M2[i] = v[i];
    M3[i] = v[i] - ld;
    ld = v[i];
  }

  rep(i, k) {
    rep(j, v.size()) {
      dp[i + 1][j] += dp[i][M1[n / M2[j]]] * M3[j];
      dp[i + 1][j] %= mod;
    }
    rep(j, v.size()) {
      dp[i + 1][j + 1] += dp[i + 1][j];
      dp[i + 1][j + 1] %= mod;
    }
  }
  cout << dp[k - 1][v.size() - 1] << endl;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const long double EPS = 1e-10;

int main() {
  int n, k;
  cin >> n >> k;

  if (k == 1) {
    cout << 1 << endl;
    return 0;
  }

  int rootn = sqrt(n), m = n / (rootn + 1), s = rootn + m + 1;
  ll dp[k][s], cnt[s];
  memset(dp, 0, sizeof(dp));
  cnt[0] = 0;

  FOR(i, 1, rootn + 1) cnt[i] = 1;
  cnt[rootn + 1] = n / m - rootn;
  for (int i = rootn + 2, j = m - 1; i < s; i++, j--) {
    cnt[i] = n / j - n / (j + 1);
  }

  FOR(j, 1, s) { dp[0][j] = (cnt[j] + dp[0][j - 1]) % mod; }

  FOR(i, 1, k) {
    for (int j = 1, idx = s - 1; j < s; j++, idx--) {
      dp[i][j] = (dp[i - 1][idx] * cnt[j]) % mod;
      dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
    }
  }

  cout << dp[k - 1][s - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const long double EPS = 1e-10;

int main() {
  int n, k;
  cin >> n >> k;

  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }

  int rootn = sqrt(n), m = n / (rootn + 1), s = rootn + m + 1;
  ll dp[k][s], cnt[s];
  memset(dp, 0, sizeof(dp));
  cnt[0] = 0;

  FOR(i, 1, rootn + 1) cnt[i] = 1;
  cnt[rootn + 1] = n / m - rootn;
  for (int i = rootn + 2, j = m - 1; i < s; i++, j--) {
    cnt[i] = n / j - n / (j + 1);
  }

  FOR(j, 1, s) { dp[0][j] = (cnt[j] + dp[0][j - 1]) % mod; }

  FOR(i, 1, k) {
    for (int j = 1, idx = s - 1; j < s; j++, idx--) {
      dp[i][j] = (dp[i - 1][idx] * cnt[j]) % mod;
      dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;
    }
  }

  cout << dp[k - 1][s - 1] << endl;

  return 0;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

ll N, K;
ll dp[105][10000];
map<ll, ll> momove[100000];

int main() {
  cin >> N >> K;

  set<ll> numset;
  for (ll i = 1; i * i <= N; i++) {
    numset.insert(i);
    numset.insert(N / i);
  }

  vector<ll> nums;
  for (ll num : numset) {
    nums.push_back(num);
  }
  sort(nums.begin(), nums.end());

  rep(i, nums.size()) {
    if (i == 0) {
      dp[0][i] = 1;
    } else {
      dp[0][i] = (nums[i] - nums[i - 1]) % DIV;
    }
  }

  for (ll i = 1; i < K; i++) {
    rep(j, nums.size()) {
      dp[i - 1][j + 1] += dp[i - 1][j];
      dp[i - 1][j + 1] %= DIV;
    }
    rep(j, nums.size()) {
      // numsのj番目は numsのいくつ以下からもらえる？
      ll prev =
          lower_bound(nums.begin(), nums.end(), N / nums[j]) - nums.begin();
      ll add;
      if (j == 0) {
        add = 1;
      } else {
        add = nums[j] - nums[j - 1];
        add %= DIV;
      }
      dp[i][j] += dp[i - 1][prev] * add;
      dp[i][j] %= DIV;
    }
  }

  ll ans = 0;
  rep(i, nums.size()) {
    ans += dp[K - 1][i];
    ans %= DIV;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

ll N, K;
ll dp[105][500000];

int main() {
  cin >> N >> K;

  set<ll> numset;
  for (ll i = 1; i * i <= N; i++) {
    numset.insert(i);
    numset.insert(N / i);
  }

  vector<ll> nums;
  for (ll num : numset) {
    nums.push_back(num);
  }
  sort(nums.begin(), nums.end());

  rep(i, nums.size()) {
    if (i == 0) {
      dp[0][i] = 1;
    } else {
      dp[0][i] = (nums[i] - nums[i - 1]) % DIV;
    }
  }

  for (ll i = 1; i < K; i++) {
    rep(j, nums.size()) {
      dp[i - 1][j + 1] += dp[i - 1][j];
      dp[i - 1][j + 1] %= DIV;
    }
    rep(j, nums.size()) {
      // numsのj番目は numsのいくつ以下からもらえる？
      ll prev =
          lower_bound(nums.begin(), nums.end(), N / nums[j]) - nums.begin();
      ll add;
      if (j == 0) {
        add = 1;
      } else {
        add = nums[j] - nums[j - 1];
        add %= DIV;
      }
      dp[i][j] += dp[i - 1][prev] * add;
      dp[i][j] %= DIV;
    }
  }

  ll ans = 0;
  rep(i, nums.size()) {
    ans += dp[K - 1][i];
    ans %= DIV;
  }
  cout << ans << endl;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)

typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;

  VVLL dp(10000, VLL(40000, 0));

  int h = 1;

  VLL v;

  for (; h * h <= n; h++) {
    v.push_back(h);
    v.push_back(n / h);
  }
  v.push_back(0);

  sort(ALL(v));

  dp[0][0] = 1;
  REP(i, k + 1) {
    REP(j, v.size()) {
      dp[i][j + 1] += dp[i][j];
      dp[i][j + 1] %= mod;
    }
    REP(j, v.size() - 1) {
      dp[i + 1][j] = dp[i][v.size() - 2 - j] * (v[j + 1] - v[j]) % mod;
    }
  }

  ll ans = 0;

  cout << dp[k][v.size()] << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)

typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;

  VVLL dp(105, VLL(400000, 0));

  int h = 1;

  VLL v;

  for (; h * h <= n; h++) {
    v.push_back(h);
    v.push_back(n / h);
  }
  v.push_back(0);

  sort(ALL(v));

  dp[0][0] = 1;
  REP(i, k + 1) {
    REP(j, v.size()) {
      dp[i][j + 1] += dp[i][j];
      dp[i][j + 1] %= mod;
    }
    REP(j, v.size() - 1) {
      dp[i + 1][j] = dp[i][v.size() - 2 - j] * (v[j + 1] - v[j]) % mod;
    }
  }

  ll ans = 0;

  cout << dp[k][v.size()] << endl;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#define DIM 40000
#define mod 1000000007
using namespace std;
int n, k, i, j, r, nr, st, dr, mid;
int v[DIM], u[DIM], s[105][DIM];
int main() {
  cin >> n >> k;
  r = sqrt(n * 1.0);
  for (i = 1; i <= r + r; i++) {
    if (n / i <= r) {
      break;
    }
    v[++nr] = i;
  }
  for (i = r; i >= 1; i--) {
    v[++nr] = n / i;
  }
  for (i = 1; i <= nr; i++) {
    st = 1;
    dr = nr;
    while (st <= dr) {
      mid = (st + dr) / 2;
      if (n / v[i] >= v[mid]) {
        st = mid + 1;
      } else {
        dr = mid - 1;
      }
    }
    u[i] = dr;
    s[1][i] = s[1][i - 1] + v[i] - v[i - 1];
  }
  for (i = 2; i <= k; i++) {
    for (j = 1; j <= nr; j++) {
      s[i][j] = (s[i][j - 1] + s[i - 1][u[j]] * 1LL * (v[j] - v[j - 1])) % mod;
    }
  }
  cout << s[k][nr];
}


Fixed submission:
#include <cmath>
#include <iostream>
#define DIM 70000
#define mod 1000000007
using namespace std;
int n, k, i, j, r, nr, st, dr, mid;
int v[DIM], u[DIM], s[105][DIM];
int main() {
  cin >> n >> k;
  r = sqrt(n * 1.0);
  for (i = 1; i <= r + r; i++) {
    if (n / i <= r) {
      break;
    }
    v[++nr] = i;
  }
  for (i = r; i >= 1; i--) {
    v[++nr] = n / i;
  }
  for (i = 1; i <= nr; i++) {
    st = 1;
    dr = nr;
    while (st <= dr) {
      mid = (st + dr) / 2;
      if (n / v[i] >= v[mid]) {
        st = mid + 1;
      } else {
        dr = mid - 1;
      }
    }
    u[i] = dr;
    s[1][i] = s[1][i - 1] + v[i] - v[i - 1];
  }
  for (i = 2; i <= k; i++) {
    for (j = 1; j <= nr; j++) {
      s[i][j] = (s[i][j - 1] + s[i - 1][u[j]] * 1LL * (v[j] - v[j - 1])) % mod;
    }
  }
  cout << s[k][nr];
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SP << " " <<

LL mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, K;
  cin >> N >> K;

  LL M = 0;
  REP(i, N) {
    if (i * i > N) {
      M = i - 1;
      break;
    }
  }

  vector<LL> nums(200000, 1);
  REP(i, M) { nums[2 * M - 1 - i] = N / (i + 1) - N / (i + 2); }
  nums[M] = N / M - M;

  vector<LL> dp(200000, 0);
  REP(i, 2 * M) { dp[i] = nums[i]; }

  REP(i, K - 1) {
    vector<LL> newdp(200000, 0);
    LL sum = 0;
    REP(j, 2 * M) {
      sum += dp[j];
      sum %= mod;
      newdp[2 * M - 1 - j] = (nums[2 * M - 1 - j] * sum) % mod;
    }
    REP(j, 2 * M) { dp[j] = newdp[j]; }
  }

  LL ans = 0;
  REP(i, 2 * M) {
    ans += dp[i];
    ans %= mod;
  }

  cout << ans << endl;
}

// 1 -> 1~9
// 2 -> 1~4
// 3 -> 1~3
// ? -> 1~3
// 4~4 -> 1~2
// 5~9 -> 1

// 1 -> 1~11
// 2 -> 1~5
// 3 -> 1~3
// 4~5 -> 1~2
// 6~11 -> 1

// 1 -> 1~12
// 2 -> 1~6
// 3 -> 1~4
// 4 -> 1~3
// 5~6 -> 1~2
// 7~12 -> 1


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SP << " " <<

LL mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, K;
  cin >> N >> K;

  LL M = 0;
  REP(i, 200000) {
    if (i * i > N) {
      M = i - 1;
      break;
    }
  }

  vector<LL> nums(200000, 1);
  REP(i, M) { nums[2 * M - 1 - i] = N / (i + 1) - N / (i + 2); }
  nums[M] = N / M - M;

  vector<LL> dp(200000, 0);
  REP(i, 2 * M) { dp[i] = nums[i]; }

  REP(i, K - 1) {
    vector<LL> newdp(200000, 0);
    LL sum = 0;
    REP(j, 2 * M) {
      sum += dp[j];
      sum %= mod;
      newdp[2 * M - 1 - j] = (nums[2 * M - 1 - j] * sum) % mod;
    }
    REP(j, 2 * M) { dp[j] = newdp[j]; }
  }

  LL ans = 0;
  REP(i, 2 * M) {
    ans += dp[i];
    ans %= mod;
  }

  cout << ans << endl;
}

// 1 -> 1~9
// 2 -> 1~4
// 3 -> 1~3
// ? -> 1~3
// 4~4 -> 1~2
// 5~9 -> 1

// 1 -> 1~11
// 2 -> 1~5
// 3 -> 1~3
// 4~5 -> 1~2
// 6~11 -> 1

// 1 -> 1~12
// 2 -> 1~6
// 3 -> 1~4
// 4 -> 1~3
// 5~6 -> 1~2
// 7~12 -> 1
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define print(A, x, n)                                                         \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \
  cout << endl;
#define pprint(A, y, m, n)                                                     \
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int size = 1e5;
const int inf = 1e9;
int main() {
  int N, K;
  cin >> N >> K;
  int m = sqrt(N) + 1;
  long dp[K + 1][m + 1][2];
  rep(i, 1, m + 1) {
    dp[0][i][0] = 1;
    dp[0][i][1] = 1;
  }
  rep(i, 1, K + 1) {
    long res = 0;
    for (int j = 1; j <= m; j++) {
      (res += dp[i - 1][j][0]) %= mod;
      if (j == N / (N / j))
        dp[i][j][1] = res;
    }
    if (m <= N / (m - 1))
      dp[i][m][0] = dp[i][m - 1][1];
    else
      dp[i][m][0] = dp[i][m - 2][1];
    for (int j = m - 1; j > 0; j--) {
      if (j == N / (N / j)) {
        dp[i][j][0] = ((N / j - N / (j + 1)) * dp[i - 1][j][1]) % mod;
        (dp[i][j][0] += dp[i][j + 1][0]) %= mod;
      } else
        dp[i][j][0] = dp[i][j + 1][0];
    }
  }
  cout << dp[K][1][0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = x; i < y; i++)
#define print(A, x, n)                                                         \
  rep(i, 0, n) { cout << (i ? " " : "") << A[i] x; }                           \
  cout << endl;
#define pprint(A, y, m, n)                                                     \
  rep(j, 0, m) { print(A[j], y, n); }
const long mod = 1e9 + 7;
const int size = 1e5;
const int inf = 1e9;
int main() {
  int N, K;
  cin >> N >> K;
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  int m = sqrt(N) + 1;
  long dp[K + 1][m + 1][2];
  rep(i, 1, m + 1) {
    dp[0][i][0] = 1;
    dp[0][i][1] = 1;
  }
  rep(i, 1, K + 1) {
    long res = 0;
    for (int j = 1; j <= m; j++) {
      (res += dp[i - 1][j][0]) %= mod;
      if (j == N / (N / j))
        dp[i][j][1] = res;
    }
    if (m <= N / (m - 1))
      dp[i][m][0] = dp[i][m - 1][1];
    else
      dp[i][m][0] = dp[i][m - 2][1];
    for (int j = m - 1; j > 0; j--) {
      if (j == N / (N / j)) {
        dp[i][j][0] = ((N / j - N / (j + 1)) * dp[i - 1][j][1]) % mod;
        (dp[i][j][0] += dp[i][j + 1][0]) %= mod;
      } else
        dp[i][j][0] = dp[i][j + 1][0];
    }
  }
  cout << dp[K][1][0] << endl;
}
Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
#define int long long
using namespace std;

int mod = 1000000007;
int n, k;
vector<int> gL;
vector<int> gR;
vector<int> dp[101];

signed main() {
  int i, j;

  cin >> n >> k;

  for (i = 1; i < n;) {
    int st = i, ed = n + 1, mid; // oooxxx, [st, ed)
    while (ed - st >= 2) {
      mid = (st + ed) / 2;
      if (n / i == n / mid)
        st = mid;
      else
        ed = mid;
    }

    // i ≦ m ≦ stなる整数mについて、[n / m]は同じ値を取る
    gL.push_back(i);
    gR.push_back(st);
    i = st + 1;
  }

  int sz = gL.size();

  rep(i, k + 1) {
    dp[i].resize(sz);
    rep(j, sz) dp[i][j] = 0;
  }

  dp[0][0] = 1;
  rep(i, k) {
    int ssum = 0;
    rep(j, sz) {
      ssum += dp[i][j];
      ssum %= mod;
      int rj = sz - 1 - j;
      dp[i + 1][rj] += ssum * (gR[rj] - gL[rj] + 1) % mod;
      dp[i + 1][rj] %= mod;
    }
  }

  int ans = 0;
  rep(j, sz) {
    ans += dp[k][j];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
#define int long long
using namespace std;

int mod = 1000000007;
int n, k;
vector<int> gL;
vector<int> gR;
vector<int> dp[101];

signed main() {
  int i, j;

  cin >> n >> k;

  for (i = 1; i <= n;) {
    int st = i, ed = n + 1, mid; // oooxxx, [st, ed)
    while (ed - st >= 2) {
      mid = (st + ed) / 2;
      if (n / i == n / mid)
        st = mid;
      else
        ed = mid;
    }

    // i ≦ m ≦ stなる整数mについて、[n / m]は同じ値を取る
    gL.push_back(i);
    gR.push_back(st);
    i = st + 1;
  }

  int sz = gL.size();

  rep(i, k + 1) {
    dp[i].resize(sz);
    rep(j, sz) dp[i][j] = 0;
  }

  dp[0][0] = 1;
  rep(i, k) {
    int ssum = 0;
    rep(j, sz) {
      ssum += dp[i][j];
      ssum %= mod;
      int rj = sz - 1 - j;
      dp[i + 1][rj] += ssum * (gR[rj] - gL[rj] + 1) % mod;
      dp[i + 1][rj] %= mod;
    }
  }

  int ans = 0;
  rep(j, sz) {
    ans += dp[k][j];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}