Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define show(a, n)                                                             \
  rep(i, n) { cout << a[i] << ' '; }                                           \
  cout << endl;

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 70000;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

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

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

  ll base[N];
  ll last;
  for (int i = 1; i <= sqrt(n) + 2; i++) {
    base[i] = i;
    if (n / i <= i) {
      last = i;
      break;
    }
  }
  int start, ref;
  if (n / last == last) {
    start = last + 1;
  } else if (n / last < last) {
    start = last;
  }
  int i_max;
  for (int i = start, ref = last - 1; ref >= 1; i++, ref--) {
    base[i] = n / ref;
    i_max = i;
  }
  // if n=10, base={1,2,3,5,10}
  for (int i = i_max; i >= 2; i--) {
    base[i] = base[i] - base[i - 1];
  }
  // if n=10, base={1,1,1,2,5}

  vector<vector<mint>> dp(k + 1, vector<mint>(i_max + 1));
  //	mint dp[101][N];
  for (int i = 1; i <= i_max; i++) {
    dp[1][i] = base[i];
  }
  for (int i = 2; i <= k; i++) {
    dp[i][i_max + 1] = 0;
    for (int j = i_max; j >= 1; j--) {
      dp[i][j] = dp[i][j + 1] + dp[i - 1][i_max - j + 1];
    }
    for (int j = 1; j <= i_max; j++) {
      dp[i][j] *= base[j];
    }
  }
  //	cout << i_max << endl;

  mint ans = 0;
  for (int i = 1; i <= i_max; i++) {
    ans += dp[k][i];
  }
  cout << ans.x << endl;
}


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

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define show(a, n)                                                             \
  rep(i, n) { cout << a[i] << ' '; }                                           \
  cout << endl;

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 70000;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

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

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

  ll base[N];
  ll last;
  for (int i = 1; i <= sqrt(n) + 2; i++) {
    base[i] = i;
    if (n / i <= i) {
      last = i;
      break;
    }
  }
  int start, ref;
  if (n / last == last) {
    start = last + 1;
  } else if (n / last < last) {
    start = last;
  }
  int i_max;
  for (int i = start, ref = last - 1; ref >= 1; i++, ref--) {
    base[i] = n / ref;
    i_max = i;
  }
  // if n=10, base={1,2,3,5,10}
  for (int i = i_max; i >= 2; i--) {
    base[i] = base[i] - base[i - 1];
  }
  // if n=10, base={1,1,1,2,5}

  vector<vector<mint>> dp(k + 1, vector<mint>(i_max + 2));
  //	mint dp[101][N];
  for (int i = 1; i <= i_max; i++) {
    dp[1][i] = base[i];
  }
  for (int i = 2; i <= k; i++) {
    dp[i][i_max + 1] = 0;
    for (int j = i_max; j >= 1; j--) {
      dp[i][j] = dp[i][j + 1] + dp[i - 1][i_max - j + 1];
    }
    for (int j = 1; j <= i_max; j++) {
      dp[i][j] *= base[j];
    }
  }
  //	cout << i_max << endl;

  mint ans = 0;
  for (int i = 1; i <= i_max; i++) {
    ans += dp[k][i];
  }
  cout << ans.x << endl;
}