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

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

using namespace std;

struct Segm {
  int x, y, ind;
  bool operator<(const Segm &A) const { return y < A.y; }
} segm[Nmax], Ev[Nmax * 20];
int aib[Nmax], len, ans[Nmax];

inline void upd(int p, int val) {
  for (int i = p; i; i -= (i & (-i)))
    aib[i] += val;
}

inline int qry(int p) {
  int sol = 0;
  for (int i = p; i < Nmax; i += (i & (-i)))
    sol += aib[i];
  return sol;
}

int main() {
  int n, m, i, j, d;
  /*#ifndef ONLINE_JUDGE
      freopen ("date.in","r",stdin);
      freopen ("date.out","w",stdout);
  #endif*/

  cin >> n >> m;
  for (i = 1; i <= n; ++i)
    cin >> segm[i].x >> segm[i].y;
  sort(segm + 1, segm + n + 1);

  for (d = 1; d <= m; ++d) {
    for (i = d; i <= m; i += d) {
      Ev[++len].y = i - 1;
      Ev[len].x = i - d + 1;
      Ev[len].ind = d;
    }
    i -= d;
    if (i < m) {
      Ev[++len].y = m;
      Ev[len].x = i + 1;
      Ev[len].ind = d;
    }
  }
  sort(Ev + 1, Ev + len + 1);

  for (i = j = 1; i <= len; ++i) {
    for (; j <= n && segm[j].y <= Ev[i].y; upd(segm[j++].x, 1))
      ;
    ans[Ev[i].ind] += qry(Ev[i].x);
  }

  for (i = 1; i <= m; ++i)
    cout << n - ans[i] << "\n";
  return 0;
}


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

using namespace std;

struct Segm {
  int x, y, ind;
  bool operator<(const Segm &A) const { return y < A.y; }
} segm[Nmax], Ev[Nmax * 20];
int aib[Nmax], len, ans[Nmax];

inline void upd(int p, int val) {
  for (int i = p; i; i -= (i & (-i)))
    aib[i] += val;
}

inline int qry(int p) {
  int sol = 0;
  for (int i = p; i < Nmax; i += (i & (-i)))
    sol += aib[i];
  return sol;
}

int main() {
  int n, m, i, j, d;
  /*#ifndef ONLINE_JUDGE
      freopen ("date.in","r",stdin);
      freopen ("date.out","w",stdout);
  #endif*/

  cin >> n >> m;
  for (i = 1; i <= n; ++i)
    cin >> segm[i].x >> segm[i].y;
  sort(segm + 1, segm + n + 1);

  for (d = 1; d <= m; ++d) {
    for (i = d; i <= m; i += d) {
      Ev[++len].y = i - 1;
      Ev[len].x = i - d + 1;
      Ev[len].ind = d;
    }
    i -= d;
    if (i < m) {
      Ev[++len].y = m;
      Ev[len].x = i + 1;
      Ev[len].ind = d;
    }
  }
  sort(Ev + 1, Ev + len + 1);

  for (i = j = 1; i <= len; ++i) {
    for (; j <= n && segm[j].y <= Ev[i].y; upd(segm[j++].x, 1))
      ;
    ans[Ev[i].ind] += qry(Ev[i].x);
  }

  for (i = 1; i <= m; ++i)
    cout << n - ans[i] << "\n";
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(N), R(N);
  for (int i = 0; i < N; ++i)
    cin >> L[i] >> R[i];

  vector<int> a(M + 3);
  for (int i = 0; i < N; ++i) {
    int n = 1;
    for (;;) {
      int x = (L[i] + n - 1) / n;
      int y = R[i] / n;
      if (y < 2)
        break;
      if (x > y) {
        ++n;
        continue;
      }

      int m = n;
      while (x > 2) {
        int m2 = R[i] / (x - 1);
        if (m2 <= m)
          break;
        m = m2;
        x = (L[i] + m - 1) / m;
      }
      x = max(2, x);
      // cout << "n = " << n << endl;
      // cout << "m = " << m << endl;
      // cout << "[" << x << ", " << y << "]" << endl;
      a[x]++;
      a[y + 1]--;

      if (x == 2)
        break;
      n = R[i] / x + 1;
    }
    // cout << endl;
  }

  cout << N << endl;
  int x = 0;
  for (int d = 2; d <= M; ++d) {
    x += a[d];
    cout << x << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> L(N), R(N);
  for (int i = 0; i < N; ++i)
    cin >> L[i] >> R[i];

  vector<int> a(M + 3);
  for (int i = 0; i < N; ++i) {
    int n = 1;
    for (;;) {
      int x = (L[i] + n - 1) / n;
      int y = R[i] / n;
      if (y < 2)
        break;
      if (x > y) {
        n = max(n + 1, L[i] / y);
        continue;
      }

      int m = n;
      while (x > 2) {
        int m2 = R[i] / (x - 1);
        if (m2 <= m)
          break;
        m = m2;
        x = (L[i] + m - 1) / m;
      }
      x = max(2, x);
      // cout << "n = " << n << endl;
      // cout << "m = " << m << endl;
      // cout << "[" << x << ", " << y << "]" << endl;
      a[x]++;
      a[y + 1]--;

      if (x == 2)
        break;
      n = R[i] / x + 1;
    }
    // cout << endl;
  }

  cout << N << endl;
  int x = 0;
  for (int d = 2; d <= M; ++d) {
    x += a[d];
    cout << x << endl;
  }
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 1000000007
#define mad(a, b) a = (a + b) % mod;
#define mul(a, b) a = a * b % mod;
#define N 300010
#define M 100010
namespace bit {
ll dat[N];
void init() {
  for (int i = 0; i < N; i++)
    dat[i] = 0;
}
void add(ll i) {
  for (; i < N; i += i & -i) {
    dat[i]++;
  }
}
ll sum(ll i) {
  ll res = 0;
  for (; i > 0; i -= i & -i) {
    res += dat[i];
  }
  return res;
}
} // namespace bit
vector<ll> v[2 * M];
ll n, m;
ll ans[M];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    ll l, r;
    cin >> l >> r;
    v[r].push_back(l);
  }
  bit::init();
  for (int i = 1; i <= 2 * m; i++) {
    vector<ll> di;
    for (ll j = 1; j * j <= i; j++) {
      if (i % j == 0) {
        di.push_back(j);
        if (j * j != i) {
          di.push_back(i / j);
        }
      }
    }
    for (auto d : di) {
      ans[d] += bit::sum(i - 1) - bit::sum(i - d);
      // cout<<"---"<<d<<" "<<ans[d]<<endl;
    }
    for (auto l : v[i]) {
      bit::add(l);
    }
    // for(int j=1;j<=m;j++)cout<<bit::sum(j)<<" "; cout<<endl;
  }
  for (int i = 1; i <= m; i++) {
    cout << n - ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 1000000007
#define mad(a, b) a = (a + b) % mod;
#define mul(a, b) a = a * b % mod;
#define N 300010
#define M 100010
namespace bit {
ll dat[N];
void init() {
  for (int i = 0; i < N; i++)
    dat[i] = 0;
}
void add(ll i) {
  for (; i < N; i += i & -i) {
    dat[i]++;
  }
}
ll sum(ll i) {
  ll res = 0;
  for (; i > 0; i -= i & -i) {
    res += dat[i];
  }
  return res;
}
} // namespace bit
vector<ll> v[2 * M];
ll n, m;
ll ans[2 * M];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    ll l, r;
    cin >> l >> r;
    v[r].push_back(l);
  }
  bit::init();
  for (int i = 1; i <= 2 * m; i++) {
    vector<ll> di;
    for (ll j = 1; j * j <= i; j++) {
      if (i % j == 0) {
        di.push_back(j);
        if (j * j != i) {
          di.push_back(i / j);
        }
      }
    }
    for (auto d : di) {
      ans[d] += bit::sum(i - 1) - bit::sum(i - d);
      // cout<<"---"<<d<<" "<<ans[d]<<endl;
    }
    for (auto l : v[i]) {
      bit::add(l);
    }
    // for(int j=1;j<=m;j++)cout<<bit::sum(j)<<" "; cout<<endl;
  }
  for (int i = 1; i <= m; i++) {
    cout << n - ans[i] << endl;
  }
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 300005
using namespace std;
struct P {
  int l, r;
} a[N];
struct trie {
  int w, l, r;
} t[2000005];
int n, m, cnt, root[N], pos[100005];
bool cmp(P a, P b) { return a.l < b.l; }
void insert(int pre, int now, int l, int r, int x) {
  t[now] = t[pre];
  t[now].w++;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (x <= mid)
    t[now].l = ++cnt, insert(t[pre].l, cnt, l, mid, x);
  else
    t[now].r = ++cnt, insert(t[pre].r, cnt, mid + 1, r, x);
}
int query(int k, int l, int r, int L, int R) {
  if (L <= l && R >= r)
    return t[k].w;
  int mid = l + r >> 1, ans = 0;
  if (L <= mid)
    ans += query(t[k].l, l, mid, L, R);
  if (R > mid)
    ans += query(t[k].r, mid + 1, r, L, R);
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    root[i] = ++cnt;
    insert(root[i - 1], cnt, 1, m, a[i].r);
  }
  for (int i = 1; i <= m; i++) {
    pos[i] = pos[i - 1];
    while (pos[i] < n && a[pos[i] + 1].l <= i)
      pos[i]++;
  }
  for (int i = 1; i <= m; i++) {
    int ans = 0;
    for (int p = i; p <= m; p += i) {
      ans += query(root[pos[p]], 1, m, p, p + i - 1);
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 300005
using namespace std;
struct P {
  int l, r;
} a[N];
struct trie {
  int w, l, r;
} t[N * 25];
int n, m, cnt, root[N], pos[100005];
bool cmp(P a, P b) { return a.l < b.l; }
void insert(int pre, int now, int l, int r, int x) {
  t[now] = t[pre];
  t[now].w++;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (x <= mid)
    t[now].l = ++cnt, insert(t[pre].l, cnt, l, mid, x);
  else
    t[now].r = ++cnt, insert(t[pre].r, cnt, mid + 1, r, x);
}
int query(int k, int l, int r, int L, int R) {
  if (L <= l && R >= r)
    return t[k].w;
  int mid = l + r >> 1, ans = 0;
  if (L <= mid)
    ans += query(t[k].l, l, mid, L, R);
  if (R > mid)
    ans += query(t[k].r, mid + 1, r, L, R);
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    root[i] = ++cnt;
    insert(root[i - 1], cnt, 1, m, a[i].r);
  }
  for (int i = 1; i <= m; i++) {
    pos[i] = pos[i - 1];
    while (pos[i] < n && a[pos[i] + 1].l <= i)
      pos[i]++;
  }
  for (int i = 1; i <= m; i++) {
    int ans = 0;
    for (int p = i; p <= m; p += i) {
      ans += query(root[pos[p]], 1, m, p, p + i - 1);
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int ans[500005], cf[500005], l[500005], r[500005];
const int bl = 2;
int main(int argc, char **argv) {
  int n, m, t;
  ios::sync_with_stdio(false);
  cin >> n >> m;
  t = m / bl;
  for (int i = 1; i <= n; i++)
    cin >> l[i] >> r[i];
  for (int i = 1; i <= n; i++) {
    int lst = t;
    for (int j = bl; j >= 1; j--) {
      int L = (l[i] - 1) / j + 1, R = r[i] / j;
      L = max(L, lst + 1);
      if (R < L)
        continue;
      ++cf[L], --cf[R + 1];
      lst = max(lst, R);
    }
  }
  for (int i = 1; i <= t; i++) {
    int ans = 0;
    for (int j = 1; j <= n; j++)
      ans += (l[j] - 1) / i != r[j] / i;
    cout << ans << "\n";
  }
  for (int i = t + 1; i <= m; i++) {
    cf[i] += cf[i - 1];
    cout << cf[i] << "\n";
  }
  return 0;
}
// dlstxdy

Fixed submission:
#include <iostream>
using namespace std;
int ans[500005], cf[500005], l[500005], r[500005];
const int bl = 530;
int main(int argc, char **argv) {
  int n, m, t;
  ios::sync_with_stdio(false);
  cin >> n >> m;
  t = m / bl;
  for (int i = 1; i <= n; i++)
    cin >> l[i] >> r[i];
  for (int i = 1; i <= n; i++) {
    int lst = t;
    for (int j = bl; j >= 1; j--) {
      int L = (l[i] - 1) / j + 1, R = r[i] / j;
      L = max(L, lst + 1);
      if (R < L)
        continue;
      ++cf[L], --cf[R + 1];
      lst = max(lst, R);
    }
  }
  for (int i = 1; i <= t; i++) {
    int ans = 0;
    for (int j = 1; j <= n; j++)
      ans += (l[j] - 1) / i != r[j] / i;
    cout << ans << "\n";
  }
  for (int i = t + 1; i <= m; i++) {
    cf[i] += cf[i - 1];
    cout << cf[i] << "\n";
  }
  return 0;
}
// dlstxdy
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _                                                                      \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template <typename T> class Bit {
public:
  Bit(int size) : bit(vector<T>(size + 1)), size(size) {}
  T sum(int i) {
    int s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, T x) {
    while (i <= size) {
      bit[i] += x;
      i += i & -i;
    }
  }

private:
  Bit();
  vector<T> bit;
  int size;
};

pair<int, pair<int, int>> v[100010];

int main() {
  _;
  int N, M;
  cin >> N >> M;
  int l, r;
  REP(i, N) {
    cin >> l >> r;
    v[i] = make_pair(r - l + 1, make_pair(l, r));
  }
  sort(v, v + N);
  Bit<int> b(M + 1);
  for (int i = 1, j = 0; i <= M; i++) {
    int ans = 0;
    while (j < N) {
      if (v[j].first >= i)
        break;
      b.add(v[j].second.first, 1);
      b.add(v[j].second.second + 1, -1);
      j++;
    }
    ans += N - j;
    for (int k = 0; k <= M; k += i)
      ans += b.sum(k);
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define _                                                                      \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template <typename T> class Bit {
public:
  Bit(int size) : bit(vector<T>(size + 1)), size(size) {}
  T sum(int i) {
    int s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(int i, T x) {
    while (i <= size) {
      bit[i] += x;
      i += i & -i;
    }
  }

private:
  Bit();
  vector<T> bit;
  int size;
};

pair<int, pair<int, int>> v[300010];

int main() {
  _;
  int N, M;
  cin >> N >> M;
  int l, r;
  REP(i, N) {
    cin >> l >> r;
    v[i] = make_pair(r - l + 1, make_pair(l, r));
  }
  sort(v, v + N);
  Bit<int> b(M + 1);
  for (int i = 1, j = 0; i <= M; i++) {
    int ans = 0;
    while (j < N) {
      if (v[j].first >= i)
        break;
      b.add(v[j].second.first, 1);
      b.add(v[j].second.second + 1, -1);
      j++;
    }
    ans += N - j;
    for (int k = 0; k <= M; k += i)
      ans += b.sum(k);
    cout << ans << endl;
  }
}
Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i, n) for (in i = 0; i < (n); ++i)
#define forv(i, v) forn(i, sz(v))
#define fors(i, s) for (auto i = (s).begin(); i != (s).end(); ++i)
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long in;
typedef vector<in> VI;
typedef vector<VI> VVI;
VI avl;
VI psm;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  in n, m;
  cin >> n >> m;
  avl.resize(m + 3);
  psm.resize(m + 3);
  in l, r;
  in tlm = 1;
  while ((tlm + 1) * (tlm + 1) < m)
    ++tlm;
  in nxt;
  in nr, nl;
  forn(zz, n) {
    cin >> l >> r;
    for (in i = 1; i <= tlm; ++i) {
      if (r / i != (l - 1) / i)
        ++avl[i];
    }
    nxt = tlm + 1;
    for (in hts = tlm + 4; hts >= 1; --hts) {
      nl = (l - 1) / hts + 1;
      nr = r / hts;
      nl = max(nl, nxt);
      if (nl > nr)
        continue;
      ++psm[nl];
      --psm[nr + 1];
      nxt = nr + 1;
    }
  }
  for (in i = 1; i <= m; ++i) {
    psm[i] += psm[i - 1];
    avl[i] += psm[i];
    cout << avl[i] << "\n";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i, n) for (in i = 0; i < (n); ++i)
#define forv(i, v) forn(i, sz(v))
#define fors(i, s) for (auto i = (s).begin(); i != (s).end(); ++i)
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef int in;
typedef vector<in> VI;
typedef vector<VI> VVI;
VI avl;
VI psm;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  in n, m;
  cin >> n >> m;
  avl.resize(m + 3);
  psm.resize(m + 3);
  in l, r;
  in tlm = 1;
  while ((tlm + 1) * (tlm + 1) < m)
    ++tlm;
  in nxt;
  in nr, nl;
  forn(zz, n) {
    cin >> l >> r;
    for (in i = 1; i <= tlm; ++i) {
      if (r / i != (l - 1) / i)
        ++avl[i];
    }
    nxt = tlm + 1;
    for (in hts = tlm + 4; hts >= 1; --hts) {
      nl = (l - 1) / hts + 1;
      nr = r / hts;
      nl = max(nl, nxt);
      if (nl > nr)
        continue;
      ++psm[nl];
      --psm[nr + 1];
      nxt = nr + 1;
    }
  }
  for (in i = 1; i <= m; ++i) {
    psm[i] += psm[i - 1];
    avl[i] += psm[i];
    cout << avl[i] << "\n";
  }
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
#define M(a, b) memset(a, b, sizeof(a))
#define pb push_back
const int maxn = 100000 + 10;
const LL mod = 1000000007;
struct Node {
  int l, r;
  bool operator<(const Node &a) const { return (r - l + 1) < (a.r - a.l + 1); }
} s[maxn];
int c[maxn];
int n, m;
void add(int l, int r) {

  while (l <= m) {
    ++c[l];
    l += l & (-l);
  }
  while (r <= m) {
    --c[r];
    r += r & (-r);
  }
  return;
}
int query(int i) {

  int res = 0;
  while (i) {
    res += c[i];
    i -= i & (-i);
  }
  return res;
}
int main() {

  M(c, 0);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &s[i].l, &s[i].r);
  }
  sort(s + 1, s + 1 + n);
  int cnt = 1;
  for (int i = 1; i <= m; ++i) {
    while (cnt <= n && s[cnt].r - s[cnt].l + 1 < i) {
      add(s[cnt].l, s[cnt].r + 1);
      ++cnt;
    }
    int ans = 0;
    for (int j = i; j <= m; j += i) {
      ans += query(j);
    }
    printf("%d\n", ans + n - (cnt - 1));
  }
  return 0;
}
/**
7 9
1 7
5 9
5 7
5 9
1 1
6 8
3 4
**/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
#define M(a, b) memset(a, b, sizeof(a))
#define pb push_back
const int maxn = 100000 + 10;
const LL mod = 1000000007;
struct Node {
  int l, r;
  bool operator<(const Node &a) const { return (r - l + 1) < (a.r - a.l + 1); }
} s[maxn + (maxn << 1)];
int c[maxn];
int n, m;
void add(int l, int r) {

  while (l <= m) {
    ++c[l];
    l += l & (-l);
  }
  while (r <= m) {
    --c[r];
    r += r & (-r);
  }
  return;
}
int query(int i) {

  int res = 0;
  while (i) {
    res += c[i];
    i -= i & (-i);
  }
  return res;
}
int main() {

  M(c, 0);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &s[i].l, &s[i].r);
  }
  sort(s + 1, s + 1 + n);
  int cnt = 1;
  for (int i = 1; i <= m; ++i) {
    while (cnt <= n && s[cnt].r - s[cnt].l + 1 < i) {
      add(s[cnt].l, s[cnt].r + 1);
      ++cnt;
    }
    int ans = 0;
    for (int j = i; j <= m; j += i) {
      ans += query(j);
    }
    printf("%d\n", ans + n - (cnt - 1));
  }
  return 0;
}
/**
7 9
1 7
5 9
5 7
5 9
1 1
6 8
3 4
**/
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>

#define MAX_N 100010
using namespace std;

int bit[MAX_N], m, l[MAX_N], r[MAX_N];
pair<int, int> p[3 * MAX_N];

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= m) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  int i, n, k, j = 0;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    p[i] = make_pair(r[i] - l[i] + 1, i);
  }
  sort(p, p + n);
  for (i = 0; i <= m; i++) {
    bit[i] = 0;
  }
  for (i = 1; i <= m; i++) {
    int ans = 0;
    while (p[j].first < i && j < n) {
      add(l[p[j].second], 1);
      add(r[p[j].second] + 1, -1);
      j++;
    }
    ans += n - j;
    for (k = 0; k <= m; k += i) {
      ans += sum(k);
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>

#define MAX_N 100010
using namespace std;

int bit[MAX_N], m, l[3 * MAX_N], r[3 * MAX_N];
pair<int, int> p[3 * MAX_N];

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= m) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  int i, n, k, j = 0;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    p[i] = make_pair(r[i] - l[i] + 1, i);
  }
  sort(p, p + n);
  for (i = 0; i <= m; i++) {
    bit[i] = 0;
  }
  for (i = 1; i <= m; i++) {
    int ans = 0;
    while (p[j].first < i && j < n) {
      add(l[p[j].second], 1);
      add(r[p[j].second] + 1, -1);
      j++;
    }
    ans += n - j;
    for (k = 0; k <= m; k += i) {
      ans += sum(k);
    }
    cout << ans << endl;
  }
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define N (int)1e5
#define F first
#define S second
typedef pair<int, int> P;
int n, m, ans;
int l[3 * N + 1], r[3 * N + 1];
P lr[3 * N + 1];
int bit[N + 1];
int sum(int i, int *b) {
  int s = 0;
  while (i > 0) {
    s += b[i];
    i = i & (i - 1);
  }
  return s;
}
void add(int i, int x, int *b) {
  while (i <= m) {
    b[i] += x;
    i += i & -i;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", lr[i].S, lr[i].F);
    lr[i].F = lr[i].F - lr[i].S + 1;
  }
  sort(lr, lr + n);
  int p1 = 0, p2 = 0;
  for (int i = 1; i <= m; i++) {
    ans = 0;
    p2 = lower_bound(lr, lr + n, make_pair(i, 0)) - lr;
    ans = n - p2;
    for (int j = p1; j < p2; j++) {
      add(lr[j].S, 1, bit);
      add(lr[j].F + lr[j].S, -1, bit);
    }
    p1 = p2;
    for (int j = 0; j <= m; j += i) {
      ans += sum(j, bit);
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define N (int)1e5
#define F first
#define S second
typedef pair<int, int> P;
int n, m, ans;
int l[3 * N + 1], r[3 * N + 1];
P lr[3 * N + 1];
int bit[N + 1];
int sum(int i, int *b) {
  int s = 0;
  while (i > 0) {
    s += b[i];
    i = i & (i - 1);
  }
  return s;
}
void add(int i, int x, int *b) {
  while (i <= m) {
    b[i] += x;
    i += i & -i;
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &lr[i].S, &lr[i].F);
    lr[i].F = lr[i].F - lr[i].S + 1;
  }
  sort(lr, lr + n);
  int p1 = 0, p2 = 0;
  for (int i = 1; i <= m; i++) {
    ans = 0;
    p2 = lower_bound(lr, lr + n, make_pair(i, 0)) - lr;
    ans = n - p2;
    for (int j = p1; j < p2; j++) {
      add(lr[j].S, 1, bit);
      add(lr[j].F + lr[j].S, -1, bit);
    }
    p1 = p2;
    for (int j = 0; j <= m; j += i) {
      ans += sum(j, bit);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#define ll long long
const int N = 4005, mo = 1e9 + 7;
int n, k, i, fact[N], inv[N], fv[N];
void init() {
  fact[1] = fact[0] = inv[1] = fv[0] = fv[1] = 1;
  for (i = 2; i <= n + k; i++) {
    fact[i] = (ll)fact[i - 1] * i % mo;
    inv[i] = (ll)inv[mo % i] * (mo - mo / i) % mo;
    fv[i] = (ll)fv[i - 1] * inv[i] % mo;
  }
}
int C(int n, int m) { return (ll)fact[n] * fv[m] % mo * fv[n - m] % mo; }
int cal(int n, int m) { return m >= 0 ? C(n + m, n) : 0; }
int main() {
  scanf("%d%d", &n, &k);
  init();
  if (n == 1 && k == 1) {
    puts("1");
    return 0;
  }
  int ans = (cal(n - 2, k - 1) - cal(n, k - 3) + mo) % mo;
  for (i = n - k - 1; i; i--)
    ans = 2ll * ans % mo;
  printf("%d", ans);
}

Fixed submission:
#include <cstdio>
#define ll long long
const int N = 4005, mo = 1e9 + 7;
int n, k, i, fact[N], inv[N], fv[N];
void init() {
  fact[1] = fact[0] = inv[1] = fv[0] = fv[1] = 1;
  for (i = 2; i <= n + k; i++) {
    fact[i] = (ll)fact[i - 1] * i % mo;
    inv[i] = (ll)inv[mo % i] * (mo - mo / i) % mo;
    fv[i] = (ll)fv[i - 1] * inv[i] % mo;
  }
}
int C(int n, int m) { return (ll)fact[n] * fv[m] % mo * fv[n - m] % mo; }
int cal(int n, int m) { return m >= 0 ? C(n + m, n) : 0; }
int main() {
  scanf("%d%d", &n, &k);
  init();
  if (n == 1 && k == 1) {
    puts("1");
    return 0;
  }
  int ans = (cal(n - 2, k - 1) - cal(n, k - 3) + mo) % mo;
  for (i = n - k - 1; i > 0; i--)
    ans = 2ll * ans % mo;
  printf("%d", ans);
}
Problem p03820 (C++)

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

const int MAXN = 2010;
const int mod = 1000000007;
typedef long long LL;
void reduce(int &x) { x += x >> 31 & mod; }
int mul(int a, int b) { return (LL)a * b % mod; }
int pow(int a, int b, int res = 1) {
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1)
      res = mul(res, a);
  return res;
}
int n, K;
int f[MAXN][MAXN], g[MAXN][MAXN], suc[MAXN];
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> n >> K;
  f[n + 1][0] = 1;
  for (int i = n; i; --i) {
    for (int j = n; ~j; --j) {
      reduce(suc[j] = f[i + 1][j] + g[i + 1][j] - mod);
      if (i != 1)
        reduce(suc[j] += suc[j + 1] - mod);
      reduce(g[i][j + 1] += g[i + 1][j] - mod);
      reduce(g[i][j + 1] += f[i + 1][j] - mod);
      reduce(f[i][j] += suc[j] - mod);
    }
  }
  std::cout << pow(2, n - K - 1, f[1][n - K]) << std::endl;
  return 0;
}


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

const int MAXN = 2010;
const int mod = 1000000007;
typedef long long LL;
void reduce(int &x) { x += x >> 31 & mod; }
int mul(int a, int b) { return (LL)a * b % mod; }
int pow(int a, int b, int res = 1) {
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1)
      res = mul(res, a);
  return res;
}
int n, K;
int f[MAXN][MAXN], g[MAXN][MAXN], suc[MAXN];
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> n >> K;
  f[n + 1][0] = 1;
  for (int i = n; i; --i) {
    for (int j = n; ~j; --j) {
      reduce(suc[j] = f[i + 1][j] + g[i + 1][j] - mod);
      if (i != 1)
        reduce(suc[j] += suc[j + 1] - mod);
      reduce(g[i][j + 1] += g[i + 1][j] - mod);
      reduce(g[i][j + 1] += f[i + 1][j] - mod);
      reduce(f[i][j] += suc[j] - mod);
    }
  }
  std::cout << pow(2, std::max(n - K - 1, 0), f[1][n - K]) << std::endl;
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, k, dp[2005];
inline long long ksm(long long x, long long y) {
  long long g = 1ll;
  while (y) {
    if (y & 1ll)
      g = g * x % mod;
    x = x * x % mod;
    y >>= 1ll;
  }
  return g;
}
signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  dp[0] = 1;
  for (int i = 1; i < n; i++, dp[0] = dp[1])
    for (int j = i; j >= 1; j--)
      dp[j] = (dp[j + 1] + dp[j - 1]) % mod;
  cout << 1ll * dp[n - k] * ksm(2ll, n - k - 1) % mod << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, k, dp[2005];
inline long long ksm(long long x, long long y) {
  if (y < 0)
    return 1;
  long long g = 1ll;
  while (y) {
    if (y & 1ll)
      g = g * x % mod;
    x = x * x % mod;
    y >>= 1ll;
  }
  return g;
}
signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  dp[0] = 1;
  for (int i = 1; i < n; i++, dp[0] = dp[1])
    for (int j = i; j >= 1; j--)
      dp[j] = (dp[j + 1] + dp[j - 1]) % mod;
  cout << 1ll * dp[n - k] * ksm(2ll, n - k - 1) % mod << endl;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int fastpow(int x, int a) {
  int ret = 1;
  while (a) {
    if (a & 1)
      ret = ret * 1LL * x % mod;
    x = x * 1LL * x % mod;
    a >>= 1;
  }
  return ret;
}
int dp[2][2005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int now = 0;
  dp[now][1] = 1;
  for (int i = 2; i <= n; i++) {
    memset(dp[now ^ 1], 0, sizeof(dp[0]));
    for (int j = 1; j <= i; j++)
      dp[now ^ 1][j] = (dp[now][j] + dp[now ^ 1][j - 1]) % mod;
    now ^= 1;
  }
  cout << dp[now][k] * 1LL * fastpow(2, n - k - 1) % mod << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int fastpow(int x, int a) {
  if (a <= 0)
    return 1;
  int ret = 1;
  while (a) {
    if (a & 1)
      ret = ret * 1LL * x % mod;
    x = x * 1LL * x % mod;
    a >>= 1;
  }
  return ret;
}
int dp[2][2005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int now = 0;
  dp[now][1] = 1;
  for (int i = 2; i <= n; i++) {
    memset(dp[now ^ 1], 0, sizeof(dp[0]));
    for (int j = 1; j <= i; j++)
      dp[now ^ 1][j] = (dp[now][j] + dp[now ^ 1][j - 1]) % mod;
    now ^= 1;
  }
  cout << dp[now][k] * 1LL * fastpow(2, n - k - 1) % mod << endl;
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
const int N = 2005;
const int mod = 1e9 + 7;
using namespace std;

int n, m, f[N];

template <typename T> inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * w;
}

int fpow(int x, int y) {
  int res = 1;
  for (; y; y >>= 1, x = 1ll * x * x % mod)
    if (y & 1)
      res = 1ll * res * x % mod;
  return res;
}

int main() {
#ifndef ONLIND_JUDGE
  freopen("cpp.in", "r", stdin);
#endif
  n = read<int>(), m = read<int>();
  f[n + 1] = 1;
  for (int i = 1; i <= m; i++)
    for (int j = n; j; j--) {
      if (n - j + 1 < i)
        f[j] = 0;
      else
        f[j] = (f[j] + f[j + 1]) % mod;
    }
  if (n != m)
    f[1] = 1ll * f[1] * fpow(2, n - m - 1) % mod;
  printf("%d\n", f[1]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
const int N = 2005;
const int mod = 1e9 + 7;
using namespace std;

int n, m, f[N];

template <typename T> inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * w;
}

int fpow(int x, int y) {
  int res = 1;
  for (; y; y >>= 1, x = 1ll * x * x % mod)
    if (y & 1)
      res = 1ll * res * x % mod;
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("cpp.in", "r", stdin);
#endif
  n = read<int>(), m = read<int>();
  f[n + 1] = 1;
  for (int i = 1; i <= m; i++)
    for (int j = n; j; j--) {
      if (n - j + 1 < i)
        f[j] = 0;
      else
        f[j] = (f[j] + f[j + 1]) % mod;
    }
  if (n != m)
    f[1] = 1ll * f[1] * fpow(2, n - m - 1) % mod;
  printf("%d\n", f[1]);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define f(a, n) for (int a = 0; a < n; a++)
#define F(a, n) for (int a = 1; a <= n; a++)
#define ll long long
using namespace std;
const ll MOD = 1000000007;
unordered_map<ll, ll> gmp;
ll pairit(ll a, ll b) { return (a << 32) + b; }
ll g(ll a, ll b) {
  if (a == 0)
    return 0;
  if (a == 1)
    return 1;
  ll tmp = pairit(a, b);
  if (gmp.count(tmp))
    return gmp[tmp];
  if (b == 0)
    gmp[tmp] = (g(a - 1, 0) + g(a - 1, 1)) % MOD;
  else
    gmp[tmp] = (g(a, b - 1) + g(a - 1, b + 1)) % MOD;
  return gmp[tmp];
}
ll powa(ll a, ll b) {
  if (b < 0)
    return 1;
  else if (b % 2)
    return (powa((a * a) % MOD, b / 2) * a) % MOD;
  return powa((a * a) % MOD, b / 2);
}
ll N, K;
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> K;
  cout << ((g(K, N - K) - g(K - 1, N - K + 1) + MOD) * powa(2, N - K - 1)) % MOD
       << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define f(a, n) for (int a = 0; a < n; a++)
#define F(a, n) for (int a = 1; a <= n; a++)
#define ll long long
using namespace std;
const ll MOD = 1000000007;
unordered_map<ll, ll> gmp;
ll pairit(ll a, ll b) { return (a << 32) + b; }
ll g(ll a, ll b) {
  if (a == 0)
    return 0;
  if (a == 1)
    return 1;
  ll tmp = pairit(a, b);
  if (gmp.count(tmp))
    return gmp[tmp];
  if (b == 0)
    gmp[tmp] = (g(a - 1, 0) + g(a - 1, 1)) % MOD;
  else
    gmp[tmp] = (g(a, b - 1) + g(a - 1, b + 1)) % MOD;
  return gmp[tmp];
}
ll powa(ll a, ll b) {
  if (b <= 0)
    return 1;
  else if (b % 2)
    return (powa((a * a) % MOD, b / 2) * a) % MOD;
  return powa((a * a) % MOD, b / 2);
}
ll N, K;
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> K;
  cout << ((g(K, N - K) - g(K - 1, N - K + 1) + MOD) * powa(2, N - K - 1)) % MOD
       << endl;
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long LL;
const int maxn = 2005;
const int mod = 1e9 + 7;

int dp[maxn][maxn], s[maxn][maxn], n, K;
LL fpm(LL p, LL k) {
  LL res = 1ll;
  while (k) {
    if (k & 1)
      (res *= p) %= mod;
    (p *= p) %= mod;
    k >>= 1;
  }
  return res;
}
void inc(int &x, int y) {
  x += y;
  x >= mod ? x -= mod : 0;
}
int main() {
#ifdef Amberframe
  freopen("arc068f.in", "r", stdin);
  freopen("arc068f.out", "w", stdout);
#endif
  scanf("%d %d", &n, &K);
  if (n == 1) {
    printf("1");
    return 0;
  }
  dp[0][0] = s[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = n; j >= 0; j--) {
      if (i != n)
        dp[i][j] = dp[i][j + 1];
      // for (int k=max(0,i-j-1);k<i;k++)
      //	inc(dp[i][j],dp[k][j-i+k+1]);
      inc(dp[i][j], s[i - 1][j]);
      s[i][j] = (s[i - 1][j - 1] + dp[i][j]) % mod;
    }
  int res = (LL)dp[n][n - K] * fpm(2, n - K - 1) % mod;
  printf("%d", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long LL;
const int maxn = 2005;
const int mod = 1e9 + 7;

int dp[maxn][maxn], s[maxn][maxn], n, K;
LL fpm(LL p, LL k) {
  LL res = 1ll;
  while (k) {
    if (k & 1)
      (res *= p) %= mod;
    (p *= p) %= mod;
    k >>= 1;
  }
  return res;
}
void inc(int &x, int y) {
  x += y;
  x >= mod ? x -= mod : 0;
}
int main() {
#ifdef Amberframe
  freopen("arc068f.in", "r", stdin);
  freopen("arc068f.out", "w", stdout);
#endif
  scanf("%d %d", &n, &K);
  if (n == 1) {
    printf("1");
    return 0;
  }
  dp[0][0] = s[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = n; j >= 0; j--) {
      if (i != n)
        dp[i][j] = dp[i][j + 1];
      // for (int k=max(0,i-j-1);k<i;k++)
      //	inc(dp[i][j],dp[k][j-i+k+1]);
      inc(dp[i][j], s[i - 1][j]);
      s[i][j] = (s[i - 1][j - 1] + dp[i][j]) % mod;
    }
  int res = (LL)dp[n][n - K] * fpm(2, max(0, n - K - 1)) % mod;
  printf("%d", res);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mo = 1e9 + 7, MAXN = 1e5 + 10;
template <class T> inline void _read(T &x) {
  x = 0;
  char t = getchar();
  while (!isdigit(t) && t != '-')
    t = getchar();
  if (t == '-') {
    _read(x);
    x *= -1;
    return;
  }
  while (isdigit(t)) {
    x = x * 10 + t - '0';
    t = getchar();
  }
}
ll n, k, f[MAXN];
inline ll fpm(ll a, ll b = mo - 2) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return res;
}

int main() {
  _read(n), _read(k);
  f[0] = 1ll;
  for (int i = 1; i < n; ++i) {
    for (int j = i; j; --j)
      f[j] = (f[j + 1] + f[j - 1]) % mo;
    f[0] = f[1];
  }
  printf("%lld", f[n - k] * fpm(2, n - k - 1) % mo);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mo = 1e9 + 7, MAXN = 1e5 + 10;
template <class T> inline void _read(T &x) {
  x = 0;
  char t = getchar();
  while (!isdigit(t) && t != '-')
    t = getchar();
  if (t == '-') {
    _read(x);
    x *= -1;
    return;
  }
  while (isdigit(t)) {
    x = x * 10 + t - '0';
    t = getchar();
  }
}
ll n, k, f[MAXN];
inline ll fpm(ll a, ll b = mo - 2) {
  if (b < 0)
    return 1;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return res;
}

int main() {
  _read(n), _read(k);
  f[0] = 1ll;
  for (int i = 1; i < n; ++i) {
    for (int j = i; j; --j)
      f[j] = (f[j + 1] + f[j - 1]) % mo;
    f[0] = f[1];
  }
  printf("%lld", f[n - k] * fpm(2, n - k - 1) % mo);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int n, k;
int f[1005][1005], s[1005];
const int p = 1000000007;

int main() {
  scanf("%d%d", &n, &k), f[0][n + 1] = 1;
  for (int i = 1; i <= k; ++i) {
    s[n + 1] = f[i - 1][n + 1];
    for (int j = n; j >= 1; --j)
      s[j] = (s[j + 1] + f[i - 1][j]) % p;
    for (int j = 1; j <= n - i + 1; ++j)
      f[i][j] = s[j];
  }
  int Ans = (f[k][1] - f[k - 1][1] + p) % p;
  for (int i = n - k - 1; i >= 1; --i)
    Ans = (Ans << 1) % p;
  printf("%d\n", Ans);
  return 0;
}

Fixed submission:
#include <cstdio>

int n, k;
int f[2005][2005], s[2005];
const int p = 1000000007;

int main() {
  scanf("%d%d", &n, &k), f[0][n + 1] = 1;
  for (int i = 1; i <= k; ++i) {
    s[n + 1] = f[i - 1][n + 1];
    for (int j = n; j >= 1; --j)
      s[j] = (s[j + 1] + f[i - 1][j]) % p;
    for (int j = 1; j <= n - i + 1; ++j)
      f[i][j] = s[j];
  }
  int Ans = (f[k][1] - f[k - 1][1] + p) % p;
  for (int i = n - k - 1; i >= 1; --i)
    Ans = (Ans << 1) % p;
  printf("%d\n", Ans);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define re register
using namespace std;

inline int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    X = X * 10 + c - '0', c = getchar();
  return X * w;
}

const int N = 4000 + 10;
const int mod = 1e9 + 7;

inline int qpow(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      c = 1ll * c * a % mod;
  return c;
}
int fac[N], ifac[N];
inline void init(int n) {
  fac[0] = 1;
  for (re int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  ifac[n] = qpow(fac[n], mod - 2);
  for (re int i = n; i; --i)
    ifac[i - 1] = 1ll * ifac[i] * i % mod;
}
inline int C(int n, int m) {
  return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}

int main() {
  int n = read(), k = read();
  init(n + k);
  printf("%lld\n", 1ll * (C(n + k - 2, k - 1) - C(n + k - 2, k - 2) + mod) *
                       qpow(2, n - k - 1) % mod);
  return 0;
}


Fixed submission:
// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define re register
using namespace std;

inline int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    X = X * 10 + c - '0', c = getchar();
  return X * w;
}

const int N = 4000 + 10;
const int mod = 1e9 + 7;

inline int qpow(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      c = 1ll * c * a % mod;
  return c;
}
int fac[N], ifac[N];
inline void init(int n) {
  fac[0] = 1;
  for (re int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  ifac[n] = qpow(fac[n], mod - 2);
  for (re int i = n; i; --i)
    ifac[i - 1] = 1ll * ifac[i] * i % mod;
}
inline int C(int n, int m) {
  return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}

int main() {
  int n = read(), k = read();
  init(n + k);
  printf("%lld\n", 1ll * (C(n + k - 2, k - 1) - C(n + k - 2, k - 2) + mod) *
                       qpow(2, max(0, n - k - 1)) % mod);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int mod = 1000000007;

long long qpow(long long base, int tms) {
  long long tmp = 1;
  while (tms) {
    if (tms & 1)
      tmp = tmp * base % mod;
    base = base * base % mod;
    tms >>= 1;
  }
  return tmp;
}

int f[2][2050];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int cur = 0, lst = 1;
  f[0][0] = 1;
  for (int i = 1; i <= k - 1; ++i) {
    cur ^= 1, lst ^= 1;
    memset(f[cur], 0, sizeof(f[cur]));
    int sum = f[lst][0];
    for (int j = 1; j <= n; ++j)
      f[cur][j] = (sum + f[lst][j]) % mod, sum = (sum + f[lst][j]) % mod;
    for (int j = 0; j < n; ++j)
      f[cur][j] = f[cur][j + 1];
  }
  int ans = 0;
  for (int i = 0; i < n - k + 1; ++i)
    ans = (ans + f[cur][i]) % mod;
  printf("%d\n", ans * 1ll * qpow(2, n - k - 1) % mod);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int mod = 1000000007;

long long qpow(long long base, int tms) {
  if (tms < 0)
    return 1;
  long long tmp = 1;
  while (tms) {
    if (tms & 1)
      tmp = tmp * base % mod;
    base = base * base % mod;
    tms >>= 1;
  }
  return tmp;
}

int f[2][2050];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int cur = 0, lst = 1;
  f[0][0] = 1;
  for (int i = 1; i <= k - 1; ++i) {
    cur ^= 1, lst ^= 1;
    memset(f[cur], 0, sizeof(f[cur]));
    int sum = f[lst][0];
    for (int j = 1; j <= n; ++j)
      f[cur][j] = (sum + f[lst][j]) % mod, sum = (sum + f[lst][j]) % mod;
    for (int j = 0; j < n; ++j)
      f[cur][j] = f[cur][j + 1];
  }
  int ans = 0;
  for (int i = 0; i < n - k + 1; ++i)
    ans = (ans + f[cur][i]) % mod;
  printf("%d\n", ans * 1ll * qpow(2, n - k - 1) % mod);
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define rhl (1000000007)

using namespace std;

int f[2010][2010][2], n, k;

int main() {
  cin >> n >> k, f[n][0][0] = 1;
  for (RG int i = n; i; --i)
    for (RG int j = n; j >= 0; --j) {
      (f[i][j][1] += f[i][j + 1][1]) %= rhl;
      for (RG int p = 0; p < 2; ++p) {
        (f[i - 1][j + 1][0] += f[i][j][p]) %= rhl;
        (f[i - 1][j][1] += f[i][j][p]) %= rhl;
      }
    }
  for (RG int i = n - k - 1; i; --i)
    (f[0][n - k][1] <<= 1) %= rhl;
  cout << f[0][n - k][1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define rhl (1000000007)

using namespace std;

int f[2010][2010][2], n, k;

int main() {
  cin >> n >> k, f[n][0][0] = 1;
  for (RG int i = n; i; --i)
    for (RG int j = n; j >= 0; --j) {
      (f[i][j][1] += f[i][j + 1][1]) %= rhl;
      for (RG int p = 0; p < 2; ++p) {
        (f[i - 1][j + 1][0] += f[i][j][p]) %= rhl;
        (f[i - 1][j][1] += f[i][j][p]) %= rhl;
      }
    }
  for (RG int i = n - k - 1; i > 0; --i)
    (f[0][n - k][1] <<= 1) %= rhl;
  cout << f[0][n - k][1];
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
using namespace std;
const int P = 1e9 + 7, maxn = 2e3 + 10;
int n, k, dp[maxn], tot;
inline void upd(int &x, int y) {
  x += y;
  (x >= P) && (x -= P);
}
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % P)
    if (b & 1)
      ans = 1ll * ans * a % P;
  return ans;
}
int main() {
  scanf("%d%d", &n, &k);
  dp[n + 1] = 1;
  rep(i, 1, k - 1) per(j, n - i + 1, 2) upd(dp[j], dp[j + 1]);
  rep(i, 2, n - k + 2) upd(tot, dp[i]);
  printf("%lld\n", 1ll * tot * ksm(2, n - k - 1) % P);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
using namespace std;
const int P = 1e9 + 7, maxn = 2e3 + 10;
int n, k, dp[maxn], tot;
inline void upd(int &x, int y) {
  x += y;
  (x >= P) && (x -= P);
}
inline int ksm(int a, int b) {
  int ans = 1;
  if (b < 0)
    return 1;
  for (; b; b >>= 1, a = 1ll * a * a % P)
    if (b & 1)
      ans = 1ll * ans * a % P;
  return ans;
}
int main() {
  scanf("%d%d", &n, &k);
  dp[n + 1] = 1;
  rep(i, 1, k - 1) per(j, n - i + 1, 2) upd(dp[j], dp[j + 1]);
  rep(i, 2, n - k + 2) upd(tot, dp[i]);
  printf("%lld\n", 1ll * tot * ksm(2, n - k - 1) % P);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <vector>

#define Rep(i, n) for (int i = 1; i <= n; i++)
#define Rep0(i, n) for (int i = 0; i <= n; i++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
#define mp(a, b) make_pair(a, b)

using namespace std;

typedef long long LL;
const int N = 2020;
const int mod = 1e9 + 7;

LL f[N][N][2], s[N];

LL qpow(LL a, LL b) {
  LL ret = 1, base = a;
  while (b) {
    if (b & 1)
      ret = ret * base % mod;
    base = base * base % mod, b >>= 1;
  }
  return ret;
}

void upd(LL &a, LL b) { a = (a + b) % mod; }

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  f[n + 1][0][0] = 1;
  for (int i = n + 1; i >= 2; i--) {
    for (int j = n - i + 1; j >= 0; j--)
      s[j] = s[j + 1], upd(s[j], f[i][j][0]);

    for (int j = n - i + 1; j >= 0; j--) {

      upd(f[i - 1][j + 1][1], s[j] + f[i][j][1]);
      upd(f[i - 1][j][0], s[j] + f[i][j][1]);
    }
  }

  printf("%lld\n", f[1][n - k][0] * qpow(2, n - k - 1) % mod);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <vector>

#define Rep(i, n) for (int i = 1; i <= n; i++)
#define Rep0(i, n) for (int i = 0; i <= n; i++)
#define RepG(i, x) for (int i = head[x]; i; i = edge[i].next)
#define v edge[i].to
#define mp(a, b) make_pair(a, b)

using namespace std;

typedef long long LL;
const int N = 2020;
const int mod = 1e9 + 7;

LL f[N][N][2], s[N];

LL qpow(LL a, LL b) {
  LL ret = 1, base = a;
  while (b) {
    if (b & 1)
      ret = ret * base % mod;
    base = base * base % mod, b >>= 1;
  }
  return ret;
}

void upd(LL &a, LL b) { a = (a + b) % mod; }

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  f[n + 1][0][0] = 1;
  for (int i = n + 1; i >= 2; i--) {
    for (int j = n - i + 1; j >= 0; j--)
      s[j] = s[j + 1], upd(s[j], f[i][j][0]);

    for (int j = n - i + 1; j >= 0; j--) {

      upd(f[i - 1][j + 1][1], s[j] + f[i][j][1]);
      upd(f[i - 1][j][0], s[j] + f[i][j][1]);
    }
  }

  if (n == k)
    printf("%lld\n", f[1][n - k][0]);
  else
    printf("%lld\n", f[1][n - k][0] * qpow(2, n - k - 1) % mod);

  return 0;
}