alexjercan/bugnet huggingface dataset:
Problem p01704 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;
int main(void) {
  for (;;) {
    int n, pw;
    cin >> n;
    if (!n)
      return 0;
    cin >> pw;
    set<long double> wss;
    wss.insert(0.0);
    map<double, double> a;
    double tmpanswer = 0.0;
    a[0.0] = pw;
    int rnd;
    REP(i, n) {
      int vw, pf, vf, th;
      scanf("%d%d%d%d", &vw, &pf, &vf, &th);
      if (th > 0) {
        a[0.0] += -1.0 * pf * vw / vf;
        tmpanswer += 1.0 * th * pf / vf;
      }
      if (th == 0 && vw < 0) {
        a[0.0] += -1.0 * pf * vw / vf;
      }
      if (vw && th * vw > 0) {
        long double w = 1.0 * th / vw;
        wss.insert(w);
        a[w] += 1.0 * pf * abs(vw) / vf;
      }
    }
    vector<long double> ws(wss.begin(), wss.end());
    double answer = tmpanswer;
    REP(i, ws.size() - 1) {
      tmpanswer += a[ws[i]] * (ws[i + 1] - ws[i]);
      a[ws[i + 1]] += a[ws[i]];
      answer = min(answer, tmpanswer);
    }
    cout << fixed << setprecision(10) << answer << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

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

using namespace std;
int main(void) {
  for (;;) {
    int n, pw;
    cin >> n;
    if (!n)
      return 0;
    cin >> pw;
    vector<tuple<int, int, int, int>> f(n);
    vector<double> ws;
    ws.push_back(0.0);
    REP(i, n) {
      int vw, pf, vf, th;
      scanf("%d%d%d%d", &vw, &pf, &vf, &th);
      if (vw && 1.0 * th / vw > 0)
        ws.push_back(1.0 * th / vw);
      f[i] = make_tuple(vw, pf, vf, th);
    }
    double answer = 1e+9;
    for (auto w : ws) {
      double tmpanswer = w * pw;
      REP(i, n) {
        int vw, pf, vf, th;
        tie(vw, pf, vf, th) = f[i];
        tmpanswer += max(0.0, (th - vw * w) / vf * pf);
      }
      answer = min(answer, tmpanswer);
    }
    cout << answer << endl;
  }
}
Problem p01705 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>

using namespace std;

int main() {
  for (int N; cin >> N, N;) {
    int X[51234], R[51234];
    for (int i = 0; i < N; i++) {
      cin >> X[i] >> R[i];
    }
    double l = 0, h = 1e9;
    for (int i = 0; i < 66; i++) {
      double m = (l + h) / 2;
      map<double, int> mp;
      for (int j = 0; j < N; j++) {
        double inr = 1. * R[j] * R[j] - m * m;
        if (inr >= 0) {
          mp[-sqrt(inr) + X[j]]++;
          mp[sqrt(inr) + X[j]]--;
        }
      }
      int c = 0;
      double start;
      bool f = false;
      for (auto e : mp) {
        if (c == 0) {
          start = e.first;
        }
        c += e.second;
        if (c == 0) {
          f |= e.first - start >= m * 2;
        }
      }
      (f ? l : h) = m;
    }
    cout << fixed << l * 2 << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>

using namespace std;

int main() {
  for (int N; cin >> N, N;) {
    int X[51234], R[51234];
    for (int i = 0; i < N; i++) {
      cin >> X[i] >> R[i];
    }
    double l = 0, h = 1e9;
    for (int i = 0; i < 88; i++) {
      double m = (l + h) / 2;
      map<double, int> mp;
      for (int j = 0; j < N; j++) {
        double inr = 1. * R[j] * R[j] - m * m;
        if (inr >= 0) {
          mp[-sqrt(inr) + X[j]]++;
          mp[sqrt(inr) + X[j]]--;
        }
      }
      int c = 0;
      double start;
      bool f = false;
      for (auto e : mp) {
        if (c == 0) {
          start = e.first;
        }
        c += e.second;
        if (c == 0) {
          f |= e.first - start >= m * 2;
        }
      }
      (f ? l : h) = m;
    }
    cout << fixed << l * 2 << endl;
  }
}
Problem p01705 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<double, double> P;
const double INF = (1 << 29);
int main() {
  int n;
  while (cin >> n && n) {
    P a[n];
    for (int i = 0; i < n; i++)
      scanf("%lf%lf", &a[i].F, &a[i].S);
    double l = 0, r = 333333;
    for (int t = 0; t < 50; t++) {
      double m = (l + r) / 2;
      vector<P> v;
      for (int i = 0; i < n; i++) {
        if (a[i].S <= m)
          continue;
        double x = sqrt(a[i].S * a[i].S - m * m);
        v.push_back(P(a[i].F - x, -1));
        v.push_back(P(a[i].F + x, 1));
      }
      sort(v.begin(), v.end());
      int d[v.size() + 1];
      memset(d, 0, sizeof(d));
      for (int i = 0; i < v.size(); i++)
        d[i + 1] = d[i] - v[i].S;
      double x = -INF, y = 0;
      for (int i = 0; i < v.size() + 1; i++) {
        if (x == -INF && d[i] > 0)
          x = v[i - 1].F;
        if (d[i] == 0) {
          if (x != -INF)
            y = max(y, v[i - 1].F - x);
          x = -INF;
        }
      }
      if (y >= m * 2)
        l = m;
      else
        r = m;
    }
    printf("%.10f\\n", l * 2);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<double, double> P;

int main() {
  int n;
  while (cin >> n && n) {
    P a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i].F >> a[i].S;
    double l = 0, r = 111111;
    for (int t = 0; t < 100; t++) {
      double m = (l + r) / 2;
      vector<P> v;
      for (int i = 0; i < n; i++) {
        double x = sqrt(a[i].S * a[i].S - m * m);
        v.push_back(P(a[i].F - x, 1));
        v.push_back(P(a[i].F + x, -1));
      }
      sort(v.begin(), v.end());
      int d[v.size() + 1];
      memset(d, 0, sizeof(d));
      for (int i = 0; i < v.size(); i++)
        d[i + 1] = d[i] + v[i].S;
      double x = -1, y = 0;
      for (int i = 0; i < v.size() + 1; i++) {
        if (x == -1 && d[i] > 0)
          x = v[i - 1].F;
        if (d[i] == 0) {
          if (x != -1)
            y = max(y, v[i - 1].F - x);
          x = -1;
        }
      }
      if (y >= m * 2)
        l = m;
      else
        r = m;
    }
    printf("%.10f\\n", l * 2);
  }
  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define llint long long int
#define mod 1000000007

using namespace std;

llint N, D, X;
llint dp[2005][2005];
llint sum[2005][2005];
llint fact[2005];

llint pow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2)
    return ((a % mod) * (pow(a, n - 1) % mod)) % mod;
  llint t = pow(a, n / 2) % mod;
  return ((t % mod) * (t % mod)) % mod;
}

void make_fact() {
  llint seki = 1;
  for (llint i = 1; i <= 2005; i++) {
    seki *= i;
    seki %= mod;
    fact[i] = pow(seki, mod - 2) % mod;
  }
}

llint comb(llint n, llint r) {
  llint seki = 1;
  for (llint i = 0; i < r; i++) {
    seki *= (n - i) % mod;
    seki %= mod;
  }
  seki *= fact[r];
  return seki % mod;
}

int main(void) {
  make_fact();

  while (1) {
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0)
      break;

    for (llint n = 1; n <= N; n++)
      sum[n][0] = 0;

    for (llint m = 1; m <= N; m++) {
      dp[1][m] = 1;
      if (m >= X)
        dp[1][m] = 0;
      sum[1][m] = (sum[1][m - 1] + dp[1][m]) % mod;
    }

    for (llint n = 2; n <= N; n++) {
      for (llint m = 1; m <= N; m++) {
        if (m < n)
          dp[n][m] = 0;
        else
          dp[n][m] = sum[n - 1][m - 1] - sum[n - 1][max((llint)0, m - X)] + mod;
        dp[n][m] %= mod;
        sum[n][m] = (sum[n][m - 1] + dp[n][m]) % mod;
      }
    }

    llint ans = 0;
    for (llint i = 1; i <= min(D, N); i++) {
      ans += ((comb(D, i) % mod) * (dp[i][N] % mod)) % mod;
      ans %= mod;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define llint long long
#define mod 1000000007

using namespace std;

llint N, D, X;
llint dp[2005][2005];
llint sum[2005][2005];

llint pow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2)
    return ((a % mod) * pow(a, n - 1)) % mod;
  llint t = pow(a, n / 2) % mod;
  return (t * t) % mod;
}

llint comb(llint n, llint r) {
  llint seki = 1;
  for (llint i = 0; i < r; i++) {
    seki *= (n - i);
    seki %= mod;
  }
  for (llint i = 1; i <= r; i++) {
    seki *= pow(i, mod - 2);
    seki %= mod;
  }
  return seki;
}

int main(void) {
  while (1) {
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0)
      break;

    for (int n = 1; n <= N; n++)
      sum[n][0] = 0;

    for (llint m = 1; m <= N; m++) {
      dp[1][m] = 1;
      if (m >= X)
        dp[1][m] = 0;
      sum[1][m] = (sum[1][m - 1] + dp[1][m]) % mod;
    }

    for (llint n = 2; n <= N; n++) {
      for (llint m = 1; m <= N; m++) {
        if (m < n)
          dp[n][m] = 0;
        else
          dp[n][m] = sum[n - 1][m - 1] - sum[n - 1][max((llint)0, m - X)] + mod;
        dp[n][m] %= mod;
        sum[n][m] = (sum[n][m - 1] + dp[n][m]) % mod;
      }
    }

    llint ans = 0;
    for (llint i = 1; i <= min(D, N); i++) {
      ans += comb(D, i) * dp[i][N];
      ans %= mod;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

const long long MOD = 1000000007;

long long D, N, X;

long long mtr[2002], tmp[2002], vec[2002];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N >> D >> X && (N || D || X)) {
    fill(mtr, mtr + X, 1LL);
    fill(mtr + X, mtr + 2001, 0LL);
    fill(vec + 1, vec + N + 1, 0LL);
    vec[0] = 1LL;
    for (; D; D /= 2) {
      if (D & 1) {
        for (int i = 0; i <= N; i++) {
          tmp[i] = 0LL;
          for (int j = 0; j <= i; j++) {
            tmp[i] += mtr[i - j] * vec[j];
            tmp[i] %= MOD;
          }
        }
        for (int i = 0; i <= N; i++) {
          vec[i] = tmp[i];
        }
      }
      for (int i = 0; i <= N; i++) {
        tmp[i] = 0LL;
        for (int j = 0; j <= i; j++) {
          tmp[i] += mtr[i - j] * mtr[j];
          tmp[i] %= MOD;
        }
      }
      for (int i = 0; i <= N; i++) {
        mtr[i] = tmp[i];
      }
    }
    cout << vec[N] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

const long long MOD = 1000000007;

long long D, N, X;

long long mtr[2002], tmp[2002], vec[2002];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N >> D >> X && (N || D || X)) {
    fill(mtr, mtr + X, 1);
    fill(mtr + X, mtr + N + 1, 0);
    fill(vec + 1, vec + N + 1, 0);
    vec[0] = 1;
    for (; D; D /= 2) {
      if (D & 1) {
        for (int i = 0; i <= N; i++) {
          tmp[i] = 0;
          for (int j = 0; j <= i; j++) {
            tmp[i] += mtr[i - j] * vec[j];
            tmp[i] %= MOD;
          }
        }
        for (int i = 0; i <= N; i++) {
          vec[i] = tmp[i];
        }
      }
      for (int i = 0; i <= N; i++) {
        tmp[i] = 0;
        for (int j = 0; j <= i; j++) {
          tmp[i] += mtr[i - j] * mtr[j];
          tmp[i] %= MOD;
        }
      }
      for (int i = 0; i <= N; i++) {
        mtr[i] = tmp[i];
      }
    }
    cout << vec[N] << endl;
  }
  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>

#pragma warning(disable : 4996)

#define mod 1000000007

using namespace std;

long long N, D, X, inv[2001];
long long dp[2001][2001];

int ncr(long long n, long long r) {
  if (n < r) {
    return 0;
  }

  long long ret = 1;

  for (int i = 0; i < r; i++) {
    ret = n-- % mod * ret % mod;
  }

  for (int i = 1; i <= r; i++) {
    ret = ret * inv[i] % mod;
  }

  return ret;
}

int main() {
  inv[1] = 1;

  for (int i = 2; i <= 2000; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;

  while (true) {
    scanf("%lld", &N);
    scanf("%lld", &D);
    scanf("%lld", &X);

    if (N == 0 && D == 0 && X == 0) {
      break;
    }

    memset(dp, 0, sizeof(dp));

    dp[0][0] = 1;

    for (int i = 1; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (j < X) {
          dp[i][j] = (dp[i][j] + dp[i][j - 1] + dp[i - 1][j - 1]) % mod;
        } else {
          dp[i][j] = (dp[i][j] + dp[i][j - 1] + dp[i - 1][j - 1] -
                      dp[i - 1][j - X] + mod) %
                     mod;
        }
      }
    }

    long long ret = 0;

    for (int i = 1; i <= N; i++) {
      ret = (ret + ncr(D, i) * dp[i][N]) % mod;
    }

    printf("%lld\\n", ret);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define MOD 1000000007

int main() {
  long long N, D, X;

  while (true) {
    cin >> N >> D >> X;

    if (N == 0 && D == 0 && X == 0) {
      break;
    }

    vector<vector<int>> dp(D + 1, vector<int>(N + 1, 0));

    dp[0][0] = 1;

    for (long long i = 1LL; i <= D; i++) {
      for (long long j = 0LL; j <= N; j++) {
        for (long long k = 0LL; k < X; k++) {
          if (j - k >= 0LL) {
            dp[i][j] += dp[i - 1LL][j - k];

            dp[i][j] %= MOD;
          }
        }
      }
    }

    cout << dp[D][N] << endl;
  }

  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
const ll MOD = 1e9 + 7;

ll powm(ll x, ll k) {
  ll res = 1;
  while (k) {
    if (k & 1)
      res = (res * x) % MOD;
    x = (x * x) % MOD;
    k >>= 1;
  }
  return res;
}
ll inv(ll x) { return powm(x, MOD - 2); }
ll comb(ll n, ll k) {
  ll res = 1;
  for (int i = 0; i < k; i++) {
    res *= (n - i) % MOD;
    res %= MOD;
  }
  for (int i = 0; i < k; i++) {
    res *= inv(i + 1);
    res %= MOD;
  }
  return res;
}
int main() {
  ll n, d, x;
  while (cin >> n >> d >> x, n) {
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
      vector<ll> sum(n + 2, 0);
      partial_sum(dp[i].begin(), dp[i].end(), sum.begin() + 1,
                  [](int x, int y) { return (x + y) % MOD; });
      for (int j = 0; j <= n; j++) {
        dp[i + 1][j] = (sum[j + 1] + MOD - sum[max(j - x + 2, 0LL)]) % MOD;
      }
    }
    ll res = 0;
    for (int i = 1; i <= min(n, d); i++) {
      res += (comb(d, i) * dp[i][n - i]) % MOD;
      res %= MOD;
    }
    cout << (res + MOD) % MOD << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

const long long int MOD = 1e9 + 7;

long long int extgcd(long long int a, long long int b, long long int &x,
                     long long int &y) {
  long long int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

long long int mod_inverse(long long int a, long long int m) {
  long long int x, y;
  extgcd(a, m, x, y);     // ax+my=1
  return (m + x % m) % m; // return positive integer
}

long long int comb(long long int n, long long int r) {
  long long int res = 1;
  for (int i = 0; i < r; i++) {
    res *= (n - r);
    res %= MOD;
  }
  for (int i = 1; i <= r; i++) {
    res *= mod_inverse(i, MOD);
    res %= MOD;
  }
  return res;
}

int main() {
  int n, x;
  long long int d;
  while (cin >> n >> d >> x, n) {
    vector<vector<int>> dp(n + 1, vector<int>(2001, 0));
    dp[0][0] = 1;
    for (int i = 1; i <= 2000; i++) {
      for (int j = 1; j <= n; j++) {
        // renew dp[j][i]
        //	for(int k=1;k<=x && j-k>=0;k++){
        //   dp[j][i]+=dp[j-k][i-1];
        // }
        dp[j][i] = dp[j - 1][i] - (j - x - 1 >= 0 ? dp[j - x - 1][i - 1] : 0) +
                   dp[j - 1][i - 1];
      }
    }
    long long int res = 0;
    for (int day = 1; day <= 2000; day++) {
      long long int dCday;
      if (day > d) {
        dCday = 0;
      } else {
        dCday = comb(d, day);
      }
      res += dCday * dp[n][day];
    }
    cout << res << endl;
  }
  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void solve(long long d, int x, vector<long long> &dp) {
  if (d == 1) {
    for (int i = 0; i < dp.size() && i < x; i++)
      dp[i] = 1;
    return;
  }
  vector<long long> A(dp.size());
  if (d & 1) {
    solve(d - 1, x, A);
    for (int i = 0; i < x; i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j];
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  } else {
    solve(d >> 1, x, A);
    for (int i = 0; i < dp.size(); i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j] * A[i] % mod;
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  }
}

int main() {
  int x, n;
  long long d;
  while (~scanf("%d%lld%d", &n, &d, &x)) {
    if (!n && !d && !x)
      break;
    vector<long long> dp(n + 1);
    solve(d, x, dp);
    printf("%lld\\n", dp[n]);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void solve(int d, int x, vector<long long> &dp) {
  if (d == 1) {
    for (int i = 0; i < dp.size() && i < x; i++)
      dp[i] = 1;
    return;
  }
  vector<long long> A(dp.size());
  if (d & 1) {
    solve(d - 1, x, A);
    for (int i = 0; i < x; i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j];
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  } else {
    solve(d >> 1, x, A);
    for (int i = 0; i < dp.size(); i++) {
      for (int j = 0; j + i < dp.size(); j++) {
        dp[i + j] += A[j] * A[i] % mod;
        if (dp[i + j] >= mod)
          dp[i + j] -= mod;
      }
    }
  }
}

int main() {
  int x, n;
  long long d;
  while (~scanf("%d%lld%d", &n, &d, &x)) {
    if (!n && !d && !x)
      break;
    vector<long long> dp(n + 1);
    solve(d, x, dp);
    printf("%lld\\n", dp[n]);
  }
  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mod ((ll)1e9 + 7)
using namespace std;
typedef long long ll;
ll n, d, x, dp[2001][2001], K[2001];

ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2)
      res = res * x % mod;
    x = x * x % mod;
    n /= 2;
  }
  return res;
}

ll nCr(ll n, ll r) {
  ll N = 1;
  for (int i = 0; i < r; i++, n--)
    N = (N * (n % mod)) % mod;
  ll R = mod_pow(K[r], mod - 2);

  return N * R % mod;
}

int main() {
  K[0] = 1;
  for (int i = 1; i <= 2000; i++)
    K[i] = (K[i - 1] * i) % mod;

  while (1) {
    cin >> n >> d >> x;
    if (!n && !d && !x)
      break;

    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < x; i++)
      dp[0][i] = 1;

    for (int i = 1; i <= min(d, n); i++) {
      ll sum = 0;
      for (int j = i + 1; j <= n; j++) {
        if (j - x >= i)
          sum = (sum - dp[i - 1][j - x] + mod) % mod;
        sum = (sum + dp[i - 1][j - 1]) % mod;
        dp[i][j] = sum;
      }
    }

    ll ans = 0;
    for (int i = 0; i < min(d, n); i++)
      if (dp[i][n])
        ans = (ans + dp[i][n] * nCr(d, i + 1) % mod) % mod;
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mod ((int)1e9 + 7)
using namespace std;
typedef long long ll;
ll n, d, x, dp[2001][2001], K[2001];

ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2)
      res = res * x % mod;
    x = x * x % mod;
    n /= 2;
  }
  return res;
}

ll nCr(ll n, ll r) {
  ll N = K[n];
  ll R = mod_pow((K[r] * K[n - r]) % mod, mod - 2);
  return N * R % mod;
}

int main() {
  K[0] = 1;
  for (int i = 1; i <= 2000; i++)
    K[i] = (K[i - 1] * i) % mod;

  while (1) {
    cin >> n >> d >> x;
    if (!n && !d && !x)
      break;

    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < x; i++)
      dp[0][i] = 1;

    for (int i = 1; i <= min(d, n); i++) {
      ll sum = 0;
      for (int j = i + 1; j <= n; j++) {
        if (j - x >= i)
          sum = (sum - dp[i - 1][j - x] + mod) % mod;
        sum = (sum + dp[i - 1][j - 1]) % mod;
        dp[i][j] = sum;
      }
    }

    ll ans = 0;
    for (int i = 0; i < min(d, n); i++)
      if (dp[i][n])
        ans = (ans + dp[i][n] * nCr(d, d - i - 1) % mod) % mod;

    cout << ans << endl;
  }
  return 0;
}
Problem p01707 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
const int MOD = 1000000007;
typedef long long ll;
ll N, D, X;
typedef vector<int> Vec;
typedef vector<Vec> Mat;
void mat_mul(Vec &A, const Vec &B) {
  const int n = A.size();
  Vec col(n);
  REP(j, n) {
    ll s = 0;
    for (int k = 0; k <= j; ++k) {
      ll a = A[j - k];
      ll b = B[k];
      s += a * b;
      s %= MOD;
    }
    col[j] = s;
  }
  A = move(col);
}
Mat mat_pow(Mat A, ll n) {
  Vec col(A.size(), 0);
  col[0] = 1;
  Vec a(A.size());
  REP(i, A.size()) a[i] = A[i][0];
  for (; n > 0; n >>= 1) {
    if (n & 1)
      mat_mul(col, a);
    mat_mul(a, a);
  }
  Mat res(A.size(), Vec(A.size(), 0));
  REP(i, A.size()) REP(j, A.size()) {
    if (i - j >= 0)
      res[i][j] = col[i - j];
  }
  return res;
}
void mat_print(Mat A) {
  const int n = A.size();
  REP(i, n) {
    REP(j, n) { cerr << ' ' << A[i][j]; }
    cerr << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0)
      break;
    Mat A(N + 1, Vec(N + 1, 0));
    REP(i, N + 1) {
      for (int j = max<int>(i - X + 1, 0); j <= i; ++j) {
        A[i][j] = 1;
      }
    }
    Mat B = mat_pow(A, D);
    cout << B[N][0] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
const int MOD = 1000000007;
typedef long long ll;
ll N, D, X;
typedef vector<int> Vec;
typedef vector<Vec> Mat;
void mat_mul(Vec &A, const Vec &B) {
  const int n = A.size();
  Vec col(n);
  REP(j, n) {
    ll s = 0;
    REP(k, n) {
      ll a = (j - k >= 0) ? A[j - k] : 0;
      ll b = (k - 0 >= 0) ? B[k - 0] : 0;
      s += a * b;
      s %= MOD;
    }
    col[j] = s;
  }
  A = move(col);
}
Mat mat_pow(Mat A, ll n) {
  Vec col(A.size(), 0);
  col[0] = 1;
  Vec a(A.size());
  REP(i, A.size()) a[i] = A[i][0];
  for (; n > 0; n >>= 1) {
    if (n & 1)
      mat_mul(col, a);
    mat_mul(a, a);
  }
  Mat res(A.size(), Vec(A.size(), 0));
  REP(i, A.size()) REP(j, A.size()) {
    if (i - j >= 0)
      res[i][j] = col[i - j];
  }
  return res;
}
void mat_print(Mat A) {
  const int n = A.size();
  REP(i, n) {
    REP(j, n) { cerr << ' ' << A[i][j]; }
    cerr << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0)
      break;
    Mat A(N + 1, Vec(N + 1, 0));
    REP(i, N + 1) {
      for (int j = max<int>(i - X + 1, 0); j <= i; ++j) {
        A[i][j] = 1;
      }
    }
    Mat B = mat_pow(A, D);
    cout << B[N][0] << endl;
  }
}