Problem p02992 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::complex;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::ios_base;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;
typedef complex<double> cd;

namespace MySpace {
struct Small_Segment_Tree {
  vector<int> a;
  int l, r;

private:
  int __summ(int L, int R, int l, int r, int v) {
    if (L <= l && r <= R)
      return a[v];
    if (R <= l || r <= L)
      return 0;
    int m = (l + r) / 2;
    return __summ(L, R, l, m, 2 * v + 1) + __summ(L, R, m, r, 2 * v + 2);
  }

  void __set(int P, int l, int r, int v, int V) {
    if (l + 1 == r) {
      a[v] = V;
      return;
    }
    int m = (l + r) / 2;
    if (P < m) {
      __set(P, l, m, 2 * v + 1, V);
      a[v] = a[2 * v + 1] + a[2 * v + 2];
    } else {
      __set(P, m, r, 2 * v + 2, V);
      a[v] = a[2 * v + 1] + a[2 * v + 2];
    }
  }

public:
  int Summ(int L, int R) {
    if (R < L)
      return 0;
    return __summ(L, R + 1, l, r, 0);
  }

  void Set(int P, int V) { __set(P, l, r, 0, V); }

  Small_Segment_Tree(int _n) {
    a.resize(4 * _n);
    l = 0, r = _n;
  }
};
long long inq(long long x, long long q, long long MOD) {
  if (q == 0)
    return 1;
  long long l = inq(x, q / 2, MOD);
  if (q % 2)
    return l * l % MOD * x % MOD;
  return l * l % MOD;
}
}; // namespace MySpace

using namespace MySpace;

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const ll MOD = 1e9 + 7;
ll n, k;
ll ROOT;
ll dp[2][101][150000];
ll sigma[2][101][150000];

ll tagget_for_0 = 0;
ll tagget_for_1 = 0;

ll Compute[150000];
ll LL[150000];
ll RR[150000];

ll compute(ll x) {
  if (Compute[x] != -1)
    return Compute[x];
  long long L = 0, R = 0;
  ll l = 0, r = 1e9 + 77;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (n / mid > x)
      l = mid;
    else
      r = mid;
  }
  L = r;

  l = 0, r = 1e9 + 77;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (n / mid >= x)
      l = mid;
    else
      r = mid;
  }

  R = l;
  // cout << x << " : " << L << " " << R << endl;
  LL[x] = L;
  RR[x] = R;
  return Compute[x] = R - L + 1;
}

ll oks(int vetka, int x) {
  if (vetka == 0) {
    return x <= tagget_for_0;
  } else {
    return x <= tagget_for_1;
  }
  return 0;
}

void calc(int t, int len, int x) {
  if (t == 0) {
    dp[t][len][x] = (sigma[0][len - 1][ROOT] + sigma[1][len - 1][x]) % MOD;
  } else {
    dp[t][len][x] = compute(x) * sigma[0][len - 1][x] % MOD;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  if (n <= 3) {
    cout << 100 / 0 << endl;
  }
  while (ROOT * ROOT <= n)
    ROOT++;
  for (int i = 1; i <= 49999; i++)
    Compute[i] = -1;
  for (int i = 1; i <= 49999; i++) {
    compute(i);
  }
  for (int i = 1; i <= 49999; i++) {
    if (LL[i] * LL[i] > n) {
      tagget_for_0 = RR[i + 1];
      tagget_for_1 = i;
    }
  }
  // cout << tagget_for_0 << " " << tagget_for_1 << endl;

  for (int i = 1; i <= 49999; i++) {
    if (oks(0, i))
      dp[0][1][i] = 1;
    if (oks(1, i))
      dp[1][1][i] = compute(i);
    if (dp[1][1][i] > MOD || dp[0][1][i] > MOD) {
      cout << 100 / 0;
      return 0;
    }
    sigma[0][1][i] = (sigma[0][1][i - 1] + dp[0][1][i]) % MOD;
    // sigma[1][1][i] = (sigma[1][1][i - 1] + dp[1][1][i]) % MOD;
  }
  for (int x = 49999; x >= 0; x--) {
    sigma[1][1][x] = (sigma[1][1][x + 1] + dp[1][1][x]) % MOD;
  }
  for (int i = 2; i <= k; i++) {
    for (int x = 1; x <= 49999; x++) {
      if (oks(0, x))
        calc(0, i, x);
      if (oks(1, x))
        calc(1, i, x);
      sigma[0][i][x] = (sigma[0][i][x - 1] + dp[0][i][x]) % MOD;
    }
    for (int x = 49999; x >= 0; x--) {
      sigma[1][i][x] = (sigma[1][i][x + 1] + dp[1][i][x]) % MOD;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= 49999; i++) {
    ans += (oks(0, i) * dp[0][k][i] + oks(1, i) * dp[1][k][i]) % MOD;
    ans %= MOD;
  }
  cout << ans % MOD;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::complex;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::ios_base;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;
typedef complex<double> cd;

namespace MySpace {
struct Small_Segment_Tree {
  vector<int> a;
  int l, r;

private:
  int __summ(int L, int R, int l, int r, int v) {
    if (L <= l && r <= R)
      return a[v];
    if (R <= l || r <= L)
      return 0;
    int m = (l + r) / 2;
    return __summ(L, R, l, m, 2 * v + 1) + __summ(L, R, m, r, 2 * v + 2);
  }

  void __set(int P, int l, int r, int v, int V) {
    if (l + 1 == r) {
      a[v] = V;
      return;
    }
    int m = (l + r) / 2;
    if (P < m) {
      __set(P, l, m, 2 * v + 1, V);
      a[v] = a[2 * v + 1] + a[2 * v + 2];
    } else {
      __set(P, m, r, 2 * v + 2, V);
      a[v] = a[2 * v + 1] + a[2 * v + 2];
    }
  }

public:
  int Summ(int L, int R) {
    if (R < L)
      return 0;
    return __summ(L, R + 1, l, r, 0);
  }

  void Set(int P, int V) { __set(P, l, r, 0, V); }

  Small_Segment_Tree(int _n) {
    a.resize(4 * _n);
    l = 0, r = _n;
  }
};
long long inq(long long x, long long q, long long MOD) {
  if (q == 0)
    return 1;
  long long l = inq(x, q / 2, MOD);
  if (q % 2)
    return l * l % MOD * x % MOD;
  return l * l % MOD;
}
}; // namespace MySpace

using namespace MySpace;

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const ll MOD = 1e9 + 7;
ll n, k;
ll ROOT;
ll dp[2][101][150000];
ll sigma[2][101][150000];

ll tagget_for_0 = 0;
ll tagget_for_1 = 0;

ll Compute[150000];
ll LL[150000];
ll RR[150000];

ll compute(ll x) {
  if (Compute[x] != -1)
    return Compute[x];
  long long L = 0, R = 0;
  ll l = 0, r = 1e9 + 77;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (n / mid > x)
      l = mid;
    else
      r = mid;
  }
  L = r;

  l = 0, r = 1e9 + 77;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (n / mid >= x)
      l = mid;
    else
      r = mid;
  }

  R = l;
  // cout << x << " : " << L << " " << R << endl;
  LL[x] = L;
  RR[x] = R;
  return Compute[x] = R - L + 1;
}

ll oks(int vetka, int x) {
  if (vetka == 0) {
    return x <= tagget_for_0;
  } else {
    return x <= tagget_for_1;
  }
  return 0;
}

void calc(int t, int len, int x) {
  if (t == 0) {
    dp[t][len][x] = (sigma[0][len - 1][ROOT] + sigma[1][len - 1][x]) % MOD;
  } else {
    dp[t][len][x] = compute(x) * sigma[0][len - 1][x] % MOD;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  if (n == 1) {
    cout << 1;
    return 0;
  }
  while (ROOT * ROOT <= n)
    ROOT++;
  for (int i = 1; i <= 49999; i++)
    Compute[i] = -1;
  for (int i = 1; i <= 49999; i++) {
    compute(i);
  }
  for (int i = 1; i <= 49999; i++) {
    if (LL[i] * LL[i] > n) {
      tagget_for_0 = RR[i + 1];
      tagget_for_1 = i;
    }
  }
  // cout << tagget_for_0 << " " << tagget_for_1 << endl;

  for (int i = 1; i <= 49999; i++) {
    if (oks(0, i))
      dp[0][1][i] = 1;
    if (oks(1, i))
      dp[1][1][i] = compute(i);
    if (dp[1][1][i] > MOD || dp[0][1][i] > MOD) {
      cout << 100 / 0;
      return 0;
    }
    sigma[0][1][i] = (sigma[0][1][i - 1] + dp[0][1][i]) % MOD;
    // sigma[1][1][i] = (sigma[1][1][i - 1] + dp[1][1][i]) % MOD;
  }
  for (int x = 49999; x >= 0; x--) {
    sigma[1][1][x] = (sigma[1][1][x + 1] + dp[1][1][x]) % MOD;
  }
  for (int i = 2; i <= k; i++) {
    for (int x = 1; x <= 49999; x++) {
      if (oks(0, x))
        calc(0, i, x);
      if (oks(1, x))
        calc(1, i, x);
      sigma[0][i][x] = (sigma[0][i][x - 1] + dp[0][i][x]) % MOD;
    }
    for (int x = 49999; x >= 0; x--) {
      sigma[1][i][x] = (sigma[1][i][x + 1] + dp[1][i][x]) % MOD;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= 49999; i++) {
    ans += (oks(0, i) * dp[0][k][i] + oks(1, i) * dp[1][k][i]) % MOD;
    ans %= MOD;
  }
  cout << ans % MOD;
}