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

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
long long a[100];
long long s[100];
long long t[100];
long long x[100];
long long y[100];
int main() {
  int T = 0;
  int n;
  while (scanf("%d", &n), n) {
    n *= 2;
    int p = (int)(sqrt((double)n + 0.00000005));
    if (p * p == n) {
      printf("Case %d: %d %d\\n", ++T, p, 1);
      continue;
    }
    long double now = sqrt((long double)n);
    a[0] = (long long)now;
    s[0] = 0;
    t[0] = 1;
    x[0] = 1;
    y[0] = 0;
    x[1] = a[0];
    y[1] = 1;
    for (int i = 0; i < 50; i++) {
      s[i + 1] = a[i] * t[i] - s[i];
      t[i + 1] = ((long long)n - s[i + 1] * s[i + 1]) / t[i];
      a[i + 1] = (a[0] + s[i + 1]) / t[i + 1];
      x[i + 2] = a[i + 1] * x[i + 1] + x[i];
      y[i + 2] = a[i + 1] * y[i + 1] + y[i];
      //		printf("%lld %lld %lld %lld
      //%lld\\n",s[i+1],t[i+1],a[i+1],x[i+2],y[i+2]);
      if (t[i + 1] == 1 && x[i + 1] * x[i + 1] - y[i + 1] * y[i + 1] * n == 1) {
        printf("Case %d: %lld %lld\\n", ++T, x[i + 1], y[i + 1]);
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
int main() {
  int T = 0;
  int n;
  while (scanf("%d", &n), n) {
    n *= 2;
    int p = (int)(sqrt((double)n + 0.00000005));
    if (p * p == n) {
      printf("Case %d: %d %d\\n", ++T, p, 1);
      continue;
    }
    for (int i = 1;; i++) {
      long long val = (long long)i * i * n + 1;
      long long t = (long long)sqrt((double)val);
      if (t * t == val) {
        printf("Case %d: %lld %lld\\n", ++T, t, i);
        break;
      }
    }
  }
}
Problem p01239 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 14
using namespace std;
int n;
typedef pair<double, double> P;
typedef pair<P, P> PP;

double X[N], Y[N], X2[N], Y2[N];

double dis(double x, double y, double x2, double y2) {
  return sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
}

map<P, double> mem[1 << N];

double dfs(double x, double y, int bit) {
  if (bit == (1 << n) - 1)
    return 0;

  P sta = P(x, y);
  if (mem[bit].count(sta))
    return mem[bit][sta];

  double res = 1e9;
  for (int i = 0; i < n; i++) {
    if (bit & (1 << i))
      continue;
    int nbit = bit | 1 << i;
    double a = dis(x, y, X[i], Y[i]) + dfs(X2[i], Y2[i], nbit);
    double b = dis(x, y, X2[i], Y2[i]) + dfs(X[i], Y[i], nbit);
    res = min(res, min(a, b));
  }
  return mem[bit][sta] = res;
}

int main() {
  int Case = 0;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    double sum = 0;
    for (int i = 0; i < n; i++)
      cin >> X[i] >> Y[i] >> X2[i] >> Y2[i],
          sum += dis(X[i], Y[i], X2[i], Y2[i]);
    for (int i = 0; i < (1 << N); i++)
      mem[i].clear();

    double ans = 1e9;
    for (int i = 0; i < n; i++)
      ans = min(ans, min(dfs(X2[i], Y2[i], 1 << i), dfs(X[i], Y[i], 1 << i)));
    printf("Case %d: %.10f\\n", ++Case, ans + sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 14
using namespace std;
int n;
typedef pair<double, double> P;
typedef pair<P, P> PP;

double X[N], Y[N], X2[N], Y2[N];

double dis(double x, double y, double x2, double y2) {
  return sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
}

map<PP, double> mem[1 << N];

double dfs(double x, double y, double x2, double y2, int bit) {
  if (bit == (1 << n) - 1)
    return 0;

  PP sta = PP(P(x, y), P(x2, y2));
  if (mem[bit].count(sta))
    return mem[bit][sta];

  double res = 1e9;
  for (int i = 0; i < n; i++) {
    if (bit & (1 << i))
      continue;
    int nbit = bit | 1 << i;
    double a = dis(x, y, X[i], Y[i]) + dfs(X2[i], Y2[i], x2, y2, nbit);
    double b = dis(x, y, X2[i], Y2[i]) + dfs(X[i], Y[i], x2, y2, nbit);
    double c = dis(x2, y2, X[i], Y[i]) + dfs(x, y, X2[i], Y2[i], nbit);
    double d = dis(x2, y2, X2[i], Y2[i]) + dfs(x, y, X[i], Y[i], nbit);
    res = min(res, min(a, min(b, min(c, d))));
  }
  return mem[bit][sta] = res;
}

int main() {
  int Case = 0;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    double sum = 0;
    for (int i = 0; i < n; i++)
      cin >> X[i] >> Y[i] >> X2[i] >> Y2[i],
          sum += dis(X[i], Y[i], X2[i], Y2[i]);
    for (int i = 0; i < (1 << N); i++)
      mem[i].clear();

    double ans = 1e9;
    for (int i = 0; i < n; i++)
      ans = min(ans, dfs(X[i], Y[i], X2[i], Y2[i], 1 << i));
    printf("Case %d: %.10f\\n", ++Case, ans + sum);
  }
  return 0;
}
Problem p01240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int dp[1 << 20], prev_[1 << 20], a[20][20], H, W;
int main() {
  int T;
  cin >> T;
  for (int i = 1; i <= T; i++) {
    cin >> W >> H;
    for (int i = 0; i < (1 << W); i++)
      dp[i] = -1LL << 30;
    for (int i = 0; i < (1 << W); i++)
      prev_[i] = -1LL << 30;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> a[i][j];
    }
    prev_[0] = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int k = 0; k < (1 << W); k++) {
          if (prev_[k] <= (-1LL << 20))
            continue;
          int bit[20];
          for (int l = 0; l < W; l++)
            bit[l] = (k / (1 << l)) % 2;
          if (bit[j] != 1 && (j == 0 || bit[j - 1] != 1)) {
            dp[k + (1 << j)] = max(dp[k + (1 << j)], prev_[k] + a[i][j]);
          }
          dp[k - (1 << j) * bit[j]] = max(dp[k - (1 << j) * bit[j]], prev_[k]);
        }
        for (int k = 0; k < (1 << W); k++) {
          prev_[k] = dp[k];
          dp[k] = -1LL << 30;
        }
      }
    }
    int maxn = 0;
    for (int i = 0; i < (1 << W); i++)
      maxn = max(maxn, prev_[i]);
    cout << "Case " << i << ": " << maxn << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int dp[1 << 20], prev_[1 << 20], a[20][20], H, W;
int main() {
  int T;
  cin >> T;
  for (int i = 1; i <= T; i++) {
    cin >> W >> H;
    for (int i = 0; i < (1 << W); i++)
      dp[i] = -1LL << 30;
    for (int i = 0; i < (1 << W); i++)
      prev_[i] = -1LL << 30;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> a[i][j];
    }
    prev_[0] = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int k = 0; k < (1 << W); k++) {
          int bit[20];
          for (int l = 0; l < W; l++)
            bit[l] = (k / (1 << l)) % 2;
          if (bit[j] != 1) {
            dp[k + (1 << j)] = max(dp[k + (1 << j)], prev_[k] + a[i][j]);
          }
          dp[k - (1 << j) * bit[j]] = max(dp[k - (1 << j) * bit[j]], prev_[k]);
        }
        for (int k = 0; k < (1 << W); k++) {
          prev_[k] = dp[k];
          dp[k] = -1LL << 30;
        }
      }
    }
    int maxn = 0;
    for (int i = 0; i < (1 << W); i++)
      maxn = max(maxn, prev_[i]);
    cout << "Case " << i << ": " << maxn << endl;
  }
  return 0;
}
Problem p01240 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int Q, H, W, r, a[409], pre[1048588], dp[1048588];
int main() {
  cin >> Q;
  int cnt = 0;
  while (Q--) {
    cin >> W >> H;
    r = (1 << W) - 1;
    for (int i = 0; i < H * W; i++)
      cin >> a[i];
    fill(pre, pre + (1 << W), -999999999);
    pre[0] = 0;
    pre[1] = a[0];
    for (int i = 0; i < H * W - 1; i++) {
      fill(dp, dp + (1 << W), -999999999);
      for (int j = 0; j < 1 << W; j++) {
        if (pre[j] >= 0) {
          dp[(j * 2) & r] = max(dp[(j * 2) & r], pre[j]);
          bool flag = true;
          if (i % W != W - 1 && (j & 1))
            flag = false;
          if (j & (1 << (W - 1)))
            flag = false;
          if (flag) {
            dp[(j * 2 + 1) & r] = max(dp[(j * 2 + 1) & r], pre[j] + a[i + 1]);
          }
        }
      }
      for (int j = 0; j < 1 << W; j++)
        pre[j] = dp[j];
    }
    int ret = *max_element(pre, pre + (1 << W));
    cout << "Case " << ++cnt << ": " << ret << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int Q, H, W, r, a[409], used[409];
map<int, int> dp[409];
int solve(int pos, int bit) {
  if (pos == H * W)
    return 0;
  if (dp[pos][bit])
    return dp[pos][bit] - 1;
  int ret = solve(pos + 1, (bit << 1) & r);
  bool flag = true;
  if (pos % W > 0 && used[pos - 1] == 1)
    flag = false;
  if (pos >= W && used[pos - W] == 1)
    flag = false;
  if (flag) {
    used[pos] = 1;
    ret = max(ret, solve(pos + 1, ((bit << 1) + 1) & r) + a[pos]);
    used[pos] = 0;
  }
  dp[pos][bit] = ret + 1;
  return ret;
}
int main() {
  cin >> Q;
  while (Q--) {
    cin >> W >> H;
    r = (1 << W) - 1;
    for (int i = 0; i < H * W; i++)
      cin >> a[i], dp[i].clear();
    cout << solve(0, 0) << endl;
  }
  return 0;
}
Problem p01240 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int w, h, a[20][20];

int idx[1 << 20]; // 2^w -> Fibonacci ID

int dp[20][4][76866]; // partial memoization (got MLE!!)
int dfs(int i, int j, int S) {
  if (i == h)
    return 0;
  if (j == w)
    return dfs(i + 1, 0, S);

  int dummy = -1;
  int &res = (j % 5 == 0 ? dp[i][j / 5][idx[S]] : dummy);

  if (res != -1)
    return res;

  // not choose a[i][j]
  res = dfs(i, j + 1, S & ~(1 << j));
  // choose a[i][j]
  if ((S >> j & 1) == 0 && (j == 0 || (S >> j - 1 & 1) == 0)) {
    res = max(res, dfs(i, j + 1, S | 1 << j) + a[i][j]);
  }
  return res;
}

int solve() {
  scanf("%d%d", &w, &h);
  rep(i, h) rep(j, w) scanf("%d", a[i] + j);

  int n = 0;
  rep(S, 1 << w) {
    int adj = 0;
    rep(j, w - 1) if ((S >> j & 1) && (S >> j + 1 & 1)) adj++;
    if (adj <= 1)
      idx[S] = n++;
  }

  rep(i, h) rep(j, 4) rep(k, n) dp[i][j][k] = -1;

  return dfs(0, 0, 0);
}

int main() {
  int T;
  scanf("%d", &T);
  for (int cas = 1; cas <= T; cas++)
    printf("Case %d: %d\\n", cas, solve());
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int w, h, a[20][20];

int idx[1 << 20]; // 2^w -> Fibonacci ID

int dp[20][5][76866]; // partial memoization (got MLE!!)
int dfs(int i, int j, int S) {
  if (i == h)
    return 0;
  if (j == w)
    return dfs(i + 1, 0, S);

  int dummy = -1;
  int &res = (j % 4 == 0 ? dp[i][j / 4][idx[S]] : dummy);

  if (res != -1)
    return res;

  // not choose a[i][j]
  res = dfs(i, j + 1, S & ~(1 << j));
  // choose a[i][j]
  if ((S >> j & 1) == 0 && (j == 0 || (S >> j - 1 & 1) == 0)) {
    res = max(res, dfs(i, j + 1, S | 1 << j) + a[i][j]);
  }
  return res;
}

int solve() {
  scanf("%d%d", &w, &h);
  rep(i, h) rep(j, w) scanf("%d", a[i] + j);

  int n = 0;
  rep(S, 1 << w) {
    int adj = 0;
    rep(j, w - 1) if ((S >> j & 1) && (S >> j + 1 & 1)) adj++;
    if (adj <= 1)
      idx[S] = n++;
  }

  rep(i, h) rep(j, 5) rep(k, n) dp[i][j][k] = -1;

  return dfs(0, 0, 0);
}

int main() {
  int T;
  scanf("%d", &T);
  for (int cas = 1; cas <= T; cas++)
    printf("Case %d: %d\\n", cas, solve());
  return 0;
}
Problem p01244 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int K, prime[1000];

double ans;

vector<int> A;

void update(int num, int sum) { ans = max(ans, 1.0 * sum / num); }

void dfs(int x, int num, int upper, int sum) {

  if (x == (int)A.size())
    return;

  int add = 0;

  for (int i = 1; i <= upper; i++) {

    add += pow(A[x], i);

    int nnum = num * pow(A[x], i);

    if (nnum > K)
      break;

    update(nnum, sum * (1 + add));

    dfs(x + 1, nnum, i, sum * (1 + add));
  }
}

signed main() {

  for (int i = 0; i < 1000; i++)
    prime[i] = 1;

  prime[0] = prime[1] = 0;

  for (int i = 0; i < 100; i++) {

    if (prime[i] == 0)
      continue;

    for (int j = i * 2; j <= 1000; j += i)
      prime[j] = 0;
  }

  ll x = 1;

  for (int i = 0; i < 1000; i++) {

    if (prime[i] == 1) {
      x *= i;
      A.push_back(i);
    }

    if (x > 1e15)
      break;
  }

  while (1) {

    cin >> K;

    if (K == 0)
      break;

    if (K == 1) {
      cout << 1 << endl;
      continue;
    }

    ans = 0;

    dfs(0, 1, 50, 1);

    printf("%.6f\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

vector<ll> diviser(ll A) {

  vector<ll> res;

  for (ll i = 1; i * i <= A; i++)
    if (A % i == 0) {
      res.push_back(i);
      if (i * i != A)
        res.push_back(A / i);
    }

  sort(res.begin(), res.end());

  return res;
}

int K, prime[1000];

int X = 0;

double ans;

vector<int> A;

void check(int num) {

  vector<int> r = diviser(num);

  double sum = 0;

  for (int i = 0; i < r.size(); i++)
    sum += r[i];

  ans = max(ans, sum / num);
}

void dfs(int x, int num, int upper, int sum) {

  if (x == (int)A.size())
    return;

  if (sum >= X)
    check(num);

  for (int i = 1; i <= upper; i++) {

    int nnum = num * pow(A[x], i);

    if (nnum > K)
      break;

    dfs(x + 1, nnum, i, sum + i);
  }
}

signed main() {

  for (int i = 0; i < 1000; i++)
    prime[i] = 1;

  prime[0] = prime[1] = 0;

  for (int i = 0; i < 100; i++) {

    if (prime[i] == 0)
      continue;

    for (int j = i * 2; j <= 1000; j += i)
      prime[j] = 0;
  }

  ll x = 1;

  for (int i = 0; i < 1000; i++) {

    if (prime[i] == 1) {
      x *= i;
      A.push_back(i);
    }

    if (x > 1e15)
      break;
  }

  while (1) {

    cin >> K;

    int p = K;

    X = 0;

    while (p)
      p /= 10, X++;

    if (K == 0)
      break;

    if (K == 1) {
      cout << 1 << endl;
      continue;
    }

    ans = 0;

    dfs(0, 1, 10, 0);

    printf("%.6f\\n", ans);
  }

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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int k;
double ans;

template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
};

vector<Int> pr;

Int isprime(Int x) {
  if (x <= 1)
    return 0;
  for (Int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return 0;
  return 1;
}

void dfs(Int x, double y, Int p, Int c) {
  // if(x<=k) cout<<y<<" "<<x<<":"<<(y/x)<<endl;
  if (x <= k)
    chmax(ans, y / x);
  else
    return;
  Int t = 1;
  double s = 0;
  if (c == 0)
    return;
  for (Int i = 0; i <= c; i++) {
    if (x > k / t)
      break;
    s += t;
    dfs(x * t, y * s, p + 1, i);
    if (t > k / pr[p])
      break;
    t *= pr[p];
  }
}

struct Precision {
  Precision() { cout << fixed << setprecision(6); }
} precision_beet;

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

  for (Int i = 2; i < 1e5; i++)
    if (isprime(i))
      pr.emplace_back(i);

  while (cin >> k, k) {
    ans = 1;
    dfs(1, 1, 0, 100);
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int k;
double ans;

template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
};

vector<Int> pr;

Int isprime(Int x) {
  if (x <= 1)
    return 0;
  for (Int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return 0;
  return 1;
}

void dfs(Int x, double y, Int p, Int c) {
  if (x <= k)
    chmax(ans, y / x);
  Int t = 1;
  double s = 0;
  if (c == 0)
    return;
  for (Int i = 0; i <= c; i++) {
    s += t;
    dfs(x * t, y * s, p + 1, i);
    if (t > k / pr[p])
      break;
    t *= pr[p];
  }
}

struct Precision {
  Precision() { cout << fixed << setprecision(6); }
} precision_beet;

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

  for (Int i = 2; i < 1e5; i++)
    if (isprime(i))
      pr.emplace_back(i);

  while (cin >> k, k) {
    ans = 1;
    dfs(1, 1, 0, 100);
    cout << ans << endl;
  }
  return 0;
}
Problem p01244 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

ll primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
ll UB;
double dfs(int idx, ll n, ll sigma, int e) {
  // printf("dfs %d %lld %d\\n", idx, n, last);
  double res = 1.0 * sigma / n;
  if (idx == 16)
    return res;
  ll n_sigma = sigma;
  for (int i = 0; i < e; i++) {
    if (n * primes[idx] > UB)
      break;
    n *= primes[idx];
    n_sigma = n_sigma * primes[idx] + sigma;
    res = max(res, dfs(idx + 1, n, n_sigma, i + 1));
  }
  return res;
}
int main() {
  while (cin >> UB && UB)
    printf("%.16lf\\n", dfs(0, 1, 1, 100));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

ll sigma(ll n) {
  ll res = 0;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res += i;
      if (i * i != n)
        res += n / i;
    }
  }
  return res;
}
double f(ll n) { return 1.0 * sigma(n) / n; }
ll primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
ll UB;
double dfs(int idx, ll n, int last) {
  double res = f(n);
  if (idx == 16)
    return res;
  for (int i = 0; i < last; i++) {
    if (n * primes[idx] > UB)
      break;
    n *= primes[idx];
    res = max(res, dfs(idx + 1, n, i + 1));
  }
  return res;
}
int main() {
  while (cin >> UB && UB)
    printf("%.16lf\\n", dfs(0, 1, 100));
  return 0;
}
Problem p01244 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

using ld = long double;

vector<int> primes;
void hurui(const int amax) {
  static bool flag = false;
  if (flag)
    return;
  vector<int> sos;
  sos = vector<int>(amax + 1, true);
  sos[0] = false;
  sos[1] = false;
  for (int i = 2; i <= amax; ++i) {
    if (sos[i]) {
      for (int j = 2 * i; j <= amax; j += i)
        sos[j] = false;
    }
  }
  for (int i = 0; i <= amax; ++i) {
    if (sos[i]) {
      primes.push_back(i);
    }
  }
  flag = true;
}

long long int K;

ld solve(long long int now_num, int pr_id, int use_max, ld now_val) {
  if (use_max == 0) {
    return now_val;
  }
  if (primes.size() == pr_id) {
    return now_val;
  } else {
    ld ans = now_val;
    int now_use = 0;
    long long int pr_sum = 1;
    long long int div_sum = 1;
    ld next_val = now_val * pr_sum / div_sum;
    long long int next_num = now_num;
    while (next_num <= K) {
      ans = max(ans, solve(next_num, pr_id + 1, now_use, next_val));

      now_use++;
      div_sum *= primes[pr_id];
      pr_sum += div_sum;
      next_val = now_val * pr_sum / div_sum;
      next_num *= primes[pr_id];
    }

    return ans;
  }
}
int main() {
  hurui(1e3);
  int case_id = 0;
  while (true) {
    cin >> K;
    if (!K)
      break;
    ld ans = solve(1, 0, 1000, 1);
    cout << setprecision(10) << fixed << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

using ld = long double;

vector<int> primes;
void hurui(const int amax) {
  static bool flag = false;
  if (flag)
    return;
  vector<int> sos;
  sos = vector<int>(amax + 1, true);
  sos[0] = false;
  sos[1] = false;
  for (int i = 2; i <= amax; ++i) {
    if (sos[i]) {
      for (int j = 2 * i; j <= amax; j += i)
        sos[j] = false;
    }
  }
  for (int i = 0; i <= amax; ++i) {
    if (sos[i]) {
      primes.push_back(i);
    }
  }
  flag = true;
}

long long int K;

ld solve(long long int now_num, int pr_id, int use_max, ld now_val) {
  if (use_max == 0) {
    return now_val;
  }
  if (primes.size() == pr_id) {
    return now_val;
  } else {
    ld ans = now_val;
    int now_use = 0;
    long long int pr_sum = 1;
    long long int div_sum = 1;
    ld next_val = now_val * pr_sum / div_sum;
    long long int next_num = now_num;
    while (next_num <= K) {
      ans = max(ans, solve(next_num, pr_id + 1, now_use, next_val));

      now_use++;
      div_sum *= primes[pr_id];
      pr_sum += div_sum;
      next_val = now_val * pr_sum / div_sum;
      next_num *= primes[pr_id];
    }

    return ans;
  }
}
int main() {
  hurui(1e3);
  int case_id = 0;
  while (true) {
    cin >> K;
    ld ans = solve(1, 0, 1000, 1);
    cout << setprecision(10) << fixed << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX - 1;

int memo[1111];

int func(int pos, const int &n, const int &width, const vector<int> &a) {
  if (memo[pos] != -1)
    return memo[pos];
  if (pos >= n)
    return 0;
  int w = 0;
  int res = INF;
  int p;
  int pp = -1;
  for (p = pos; p < n; p++) {
    w += a[p];
    int tmp = func(p + 1, n, width, a) + abs(width - w);
    if (res > tmp) {
      pp = p;
      res = tmp;
    }
  }
  if (pp + 1 == n) {
    memo[pos] = max(0, w - width);
    return memo[pos];
  }
  memo[pos] = res;
  return res;
}

int main(void) {
  int n, w;
  int Case = 1;
  while (cin >> n >> w, n | w) {
    for (int i = 0; i < 1111; i++)
      memo[i] = -1;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << "Case " << Case++ << ": " << func(0, n, w, a) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX - 1;

int memo[1111];

int func(int pos, const int &n, const int &width, const vector<int> &a) {
  if (memo[pos] != -1)
    return memo[pos];
  if (pos >= n)
    return 0;
  int w = 0;
  int res = INF;
  int p;
  int pp = -1;
  for (p = pos; p < n; p++) {
    w += a[p];
    int tmp = func(p + 1, n, width, a) + abs(width - w);
    if (res > tmp) {
      pp = p;
      res = tmp;
    }
  }
  if (pp + 1 == n)
    return max(0, w - width);
  memo[pos] = res;
  return res;
}

int main(void) {
  int n, w;
  int Case = 1;
  while (cin >> n >> w, n | w) {
    for (int i = 0; i < 1111; i++)
      memo[i] = -1;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << "Case " << Case++ << ": " << func(0, n, w, a) << endl;
  }
  return 0;
}