alexjercan/bugnet huggingface dataset:
Problem p00167 (C++) — buggy submission, verdict: Runtime Error

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

int a[100];
int main() {
  int b;
  while (cin >> b, b) {
    for (int c = 0; c < b; c++)
      scanf("%d", &a[c]);
    int sum = 0;
    for (int d = 0; d < b - 1; d++) {
      for (int e = d + 1; e < b; e++) {
        if (a[d] > a[e])
          sum++;
      }
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

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

int BIT[500];
int sum(int a) {
  int b = 0;
  while (a > 0) {
    b += BIT[a];
    a -= a & -a;
  }
  return b;
}
void add(int a) {
  while (a <= 300) {
    BIT[a]++;
    a += a & -a;
  }
}
int main() {
  int a;
  while (cin >> a, a) {
    fill(BIT, BIT + 101, 0);
    int c = 0;
    for (int b = 0; b < a; b++) {
      int d;
      scanf("%d", &d);
      c += b - sum(d);
      add(d);
    }
    cout << c << endl;
  }
}
Problem p00167 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  for (int n; cin >> n && n;) {
    int res = 0;
    vint v(n);

    rep(i, n) cin >> v[i];

    for (int j = n - 1; 0 < j; j--)
      for (int i = 0; i < j; i++) {
        if (v[i] > v[i + 1]) {
          swap(v[i], v[i + 1]);
          res++;
        }
      }

    cout << res << endl;
  }
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  for (int n; cin >> n && n;) {
    int res = 0;
    vint v(n);

    rep(i, n) cin >> v[i];

    for (int j = n - 1; 0 < j; j--)
      for (int i = 0; i < j; i++) {
        if (v[i] > v[i + 1]) {
          swap(v[i], v[i - 1]);
          res++;
        }
      }

    cout << res << endl;
  }
}
Problem p00167 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int n[1000001] = {};
int main() {

  int N;

  int c = 0;
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    int c = 0;

    for (int i = 0; i < N; i++) {
      scanf("%d", &n[i]);
    }

    for (int i = 0; i < N; i++)
      for (int j = N - 1; j > i; j--) {
        if (n[j] < n[j - 1]) {
          int T = n[j];
          n[j] = n[j - 1];
          n[j - 1] = T;
          c++;
        }
      }
    printf("%d\\n", c);
    for (int i = 0; i < N; i++)
      n[i] = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int n[1000001] = {};
int main() {

  int N;

  int c = 0;
  while (1) {
    scanf("%d", &N);

    int c = 0;

    for (int i = 0; i < N; i++) {
      scanf("%d", &n[i]);
    }

    for (int i = 0; i < N; i++)
      for (int j = N - 1; j > i; j--) {
        if (n[j] < n[j - 1]) {
          int T = n[j];
          n[j] = n[j - 1];
          n[j - 1] = T;
          c++;
        }
      }
    printf("%d\\n", c);
    for (int i = 0; i < N; i++)
      n[i] = 0;
  }
  return 0;
}
Problem p00167 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string.h>
using namespace std;
// BIT
long bit[1000001], n;
long MAX;
long sum(int i) {
  long s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(long i, long x) {
  while (i <= MAX) {
    bit[i] += x;
    i += i & -i;
  }
}
long a[100];
bool input() {
  cin >> n;
  if (!n)
    return false;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  MAX = a[0];
  for (int i = 0; i < n; i++)
    MAX = max(MAX, a[i]);
  return true;
}
int main() {
  while (input()) {
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += i - sum(a[i]);
      add(a[i], 1);
    }
    cout << ans << '\\n';
    memset(bit, 0, sizeof(bit));
    memset(a, 0, sizeof(a));
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;
// BIT
long bit[101], n;
long sum(int i) {
  long s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(long i, long x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}
long a[100];
bool input() {
  cin >> n;
  if (!n)
    return false;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  return true;
}
int main() {
  while (input()) {
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += i - sum(a[i]);
      add(a[i], 1);
    }
    cout << ans << '\\n';
    memset(bit, 0, sizeof(bit));
  }
}
Problem p00167 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int N = 100;
int bit[N + 10];

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 <= N) {
    bit[i] += x;
    i += i & -i;
  }
}

vector<int> compress(vector<int> v) {
  map<int, int> mp;
  for (int i = 0; i < v.size(); i++)
    mp[v[i]] = 0;

  int id = 1;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    it->second = id++;
  }

  for (int i = 0; i < v.size(); i++)
    v[i] = mp[v[i]];

  return v;
}

int main() {
  int n;
  while (cin >> n && n) {
    memset(bit, 0, sizeof(bit));

    vector<int> v(n);
    for (int i = 0; i < n; i++)
      cin >> v[i];

    v = compress(v);

    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      ans += sum(v[i] - 1);
      add(v[i], 1);
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int N = 100;
int bit[N + 10];

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 <= N) {
    bit[i] += x;
    i += i & -i;
  }
}

vector<int> compress(vector<int> v) {
  map<int, int> mp;
  for (int i = 0; i < v.size(); i++)
    mp[i] = 0;

  int id = 1;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    it->second = id++;
  }

  for (int i = 0; i < v.size(); i++)
    v[i] = mp[v[i]];

  return v;
}

int main() {
  int n;
  while (cin >> n && n) {
    memset(bit, 0, sizeof(bit));

    vector<int> v(n);
    for (int i = 0; i < n; i++)
      cin >> v[i];

    v = compress(v);

    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      ans += sum(v[i] - 1);
      add(v[i], 1);
    }

    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> vec;
    vec.resize(n);
    for (int i = 0; i < n; i++)
      cin >> vec[i];

    int ans = 0;

    for (int i = vec.size() - 1; i > 0; i--)
      for (int j = 0; j < i; j++)
        if (vec[j + 1] < vec[j])
          ans++, swap(vec[j + 1], vec[j]);

    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#define all(n) n.begin(), n.end()
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define Iter(c) __typeof__((c).begin())
#define foreach(i, c) for (Iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define INF (1 << 27)
#define EPS 1e-10
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

int n, a[10000000];

int bit[10000001];

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 <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

void solve() {
  rep(i, 10000000) { bit[i] = 0; }
  ll ans = 0;
  rep(j, n) {
    ans += j - sum(a[j]);
    add(a[j], 1);
  }
  cout << ans << endl;
}

int main() {

  while (cin >> n && n) {
    rep(i, n) { cin >> a[i]; }
    solve();
  }
  return 0;
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int data[30];
  data[0] = 1;
  data[1] = 2;
  data[2] = 4;
  for (int i = 3; i < 30; i++) {
    data[i] = data[i - 3] + data[i - 2] + data[i - 1];
  }
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    cout << ((data[n - 1] + 10) / 10 - 1) / 365 + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int ans;

int rec(int n, int now) {
  static int ans;
  if (now == 0)
    ans = 0;
  for (int i = 1; i <= 3; i++) {
    int next = now + i;
    if (next >= n) {
      ans++;
      break;
    }
    rec(n, next);
  }
  if (now == 0)
    return ans;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    int ans;
    ans = rec(n, 0);
    cout << ((ans + 10) / 10 - 1) / 365 + 1 << endl;
  }
  return 0;
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int limit;
int dfs(int n) {
  if (n == limit)
    return 1;
  if (n > limit)
    return 0;
  int count = 0;
  for (int i = 1; i <= 3; i++) {
    count += dfs(n + i);
  }
  return count;
}

int calc(int n) {
  limit = n;
  int count = dfs(0);
  return count / 10;
}

int calc2(int n) {
  vector<int> dp(n + 4, 0);
  dp[1] = dp[2] = dp[3] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++)
      dp[i + j] += dp[i];
  }
  return dp[n] / 10;
}

int main(void) {
  int n;
  while (cin >> n && n) {
    int days = calc2(n);
    cout << days / 365 + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int limit;
int dfs(int n) {
  if (n == limit)
    return 1;
  if (n > limit)
    return 0;
  int count = 0;
  for (int i = 1; i <= 3; i++) {
    count += dfs(n + i);
  }
  return count;
}

int calc(int n) {
  limit = n;
  int count = dfs(0);
  return count / 10;
}

int main(void) {
  int n;
  while (cin >> n && n) {
    int days = calc(n);
    cout << days / 365 + 1 << endl;
  }
  return 0;
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>

using namespace std;

struct Memo {

  int count;
  bool flag;

  Memo() {
    count = 0;
    flag = false;
  }
};

int ProgressNumberOfYears(Memo memo[][4], int step_num, int step_kind) {

  int kind_count = 0;

  if (step_num == 0)
    return 1;
  else if (step_num < 0)
    return 0;
  else if (memo[step_num][step_kind].flag)
    return memo[step_num][step_kind].count;
  for (int i = 1; i < 4; ++i) {
    kind_count = kind_count + ProgressNumberOfYears(memo, step_num - i, i);
  }

  memo[step_num][step_kind].count = kind_count;
  memo[step_num][step_kind].flag = true;

  return kind_count;
}

int main() {

  int step_num;
  Memo memo[31][4];

  while (cin >> step_num && step_num != 0)
    cout << ProgressNumberOfYears(memo, step_num, 0) / 10 / 365 + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int ProgressNumberOfYears(int step_num, int step_kind) {

  if (step_num == 0)
    return 1;
  else if (step_num < 0)
    return 0;

  int count = 0;

  for (int i = 1; i < 4; ++i)
    count = count + ProgressNumberOfYears(step_num - i, i);

  return count;
}

int main() {

  int step_num;

  while (cin >> step_num && step_num != 0)
    cout << ProgressNumberOfYears(step_num, 0) / 10 / 365 + 1 << endl;

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

#include <iostream>
using namespace std;
int main() {
  while (1) {
    int dp[31] = {1, 1, 2, 4};
    int n;
    cin >> n;
    if (!n)
      return 0;
    for (int i = 4; i <= n; ++i) {
      dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
    }
    cout << ((dp[n] - 1) / 3650) + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  while (1) {
    int dp[31] = {1, 1, 2, 4};
    int n;
    cin >> n;
    for (int i = 4; i <= n; ++i) {
      dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
    }
    cout << ((dp[n] - 1) / 3650) + 1 << endl;
  }
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)

ll d[40];
void solve() {}
void show() {
  for (int i = 0; i < 40; i++) {
    cout << i << ":" << d[i] << endl;
  }
}
void init() {
  fill(d, d + 40, 0);
  d[1] = 1;
  d[2] = 2;
  d[3] = 4;
  for (int i = 4; i < 31; i++) {
    d[i] = d[i - 3] + d[i - 2] + d[i - 1];
  }
}
int main() {
  int n;
  init();
  while (cin >> n, n) {
    cout << (d[n] / 3650) + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

long long search(int n, int count) {
  int i, j;
  if (n > 2) {
    count = search(n - 3, count);
  }
  if (n > 1) {
    count = search(n - 2, count);
  }
  if (n > 0) {
    count = search(n - 1, count);
  }
  if (n == 0)
    return count + 1;
  //	return count;
}
int main() {
  int d;
  while (true) {
    cin >> d;
    if (!d)
      break;
    cout << (search(d, 0) / 10) / 365 + 1 << endl;
    ;
  }
  return 0;
}
Problem p00168 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int func(int x);

int main() {
  int x;
  while (true) {
    cin >> x;
    if (x == 0) {
      break;
    }
    cout << func(x) / 3650 + 1 << endl;
  }

  return 0;
}

int func(int x) {
  if (x == 1) {
    return 1;
  } else if (x == 2) {
    return 2;
  } else if (x == 3) {
    return 4;
  } else {
    return func(x - 1) + func(x - 2) + func(x - 3);
  }
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int func(int x);

int main() {
  int x;
  while (cin >> x) {
    cin >> x;
    cout << func(x) / 3650 + 1 << endl;
  }

  return 0;
}

int func(int x) {
  if (x == 1) {
    return 1;
  } else if (x == 2) {
    return 2;
  } else if (x == 3) {
    return 4;
  } else {
    return func(x - 1) + func(x - 2) + func(x - 3);
  }
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    int dp[30];
    dp[0] = 1;
    dp[1] = 2;
    dp[2] = 4;
    FOR(i, 3, n) { dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; }
    if (dp[n - 1] % 3650 == 0) {
      cout << dp[n - 1] / 3650 << endl;
    } else {
      cout << dp[n - 1] / 3650 + 1 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int main() {
  while (1) {
    int n;
    cin >> n;
    int dp[30];
    dp[0] = 1;
    dp[1] = 2;
    dp[2] = 4;
    FOR(i, 3, n) { dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; }
    if (dp[n] % 3650 == 0) {
      cout << dp[n - 1] / 3650 << endl;
    } else {
      cout << dp[n - 1] / 3650 + 1 << endl;
    }
  }
  return 0;
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<int> dan(35, 0);
  vector<int> dp(35, 0);
  int N;
  for (int i = 0; i < 35; i++) {
    int tmp;
    cin >> tmp;
    if (tmp == 0) {
      N = i;
      break;
    }
    dan[i] = tmp;
  }
  dp[0] = 1;
  for (int i = 1; i < 31; i++) {
    dp[i] += dp[i - 1];
    if (i > 1)
      dp[i] += dp[i - 2];
    if (i > 2)
      dp[i] += dp[i - 3];
  }
  for (int i = 0; i < N; i++) {
    int tmp = dp[dan[i]] / 3650;
    if (dp[dan[i]] % 3650 != 0)
      tmp++;
    cout << tmp << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<int> dan;
  vector<int> dp(35, 0);
  int N;
  for (int i = 0; i < 35; i++) {
    int tmp;
    cin >> tmp;
    if (tmp == 0)
      break;
    dan.push_back(tmp);
  }
  dp[0] = 1;
  for (int i = 1; i < 31; i++) {
    dp[i] += dp[i - 1];
    if (i > 1)
      dp[i] += dp[i - 2];
    if (i > 2)
      dp[i] += dp[i - 3];
  }
  for (int i : dan) {
    int tmp = dp[i] / 3650;
    if (dp[i] % 3650 != 0)
      tmp++;
    cout << tmp << endl;
  }

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

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

int func(int n, int m) {
  if (m == n)
    return 1;
  else {
    int cnt = 0;
    for (int i = 1; i <= 3 && m + i <= n; ++i) {
      cnt += func(n, m + i);
    }
    return cnt;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    cout << func(n, 0) / 10 / 365 + 1 << endl;
  }
}

Error: TLE

Fixed submission:

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

int func(int n, int m) {
  if (m == n)
    return 1;
  else if (m > n)
    return 0;
  else {
    int cnt = 0;
    for (int i = 1; i <= 3; ++i) {
      cnt += func(n, m + i);
    }
    return cnt;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    cout << func(n, 0) / 10 / 365 + 1 << endl;
  }
}
Problem p00168 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int n;
int memo[366];

int dfs(int num) {

  int sum = 0;

  if (memo[num] != -1) {
    return memo[num];
  }

  if (num == n) {
    return 1;
  }

  if (num < n)
    sum += dfs(num + 1);

  if (num < n)
    sum += dfs(num + 2);

  if (num < n)
    sum += dfs(num + 3);

  memo[num] = sum;

  return sum;
}

int main() {

  while (1) {
    cin >> n;

    for (int i = 0; i <= 31; i++) {
      memo[i] = -1;
    }

    if (n == 0) {
      break;
    }

    cout << dfs(0) / 10 / 365 + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int n;
int memo[31];

int dfs(int num) {

  int sum = 0;

  if (memo[num] != -1) {
    return memo[num];
  }

  if (num == n) {
    return 1;
  }

  if (num < n)
    sum += dfs(num + 1);

  if (num < n)
    sum += dfs(num + 2);

  if (num < n)
    sum += dfs(num + 3);

  memo[num] = sum;

  return sum;
}

int main() {

  while (1) {
    cin >> n;

    for (int i = 0; i <= 31; i++) {
      memo[i] = -1;
    }

    if (n == 0) {
      break;
    }

    cout << dfs(0) / 10 / 365 + 1 << endl;
  }
}
Problem p00168 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char *argv[]) {
  vector<int> dp(31, 0);
  dp[0] = 1;
  dp[1] = 1;
  dp[2] = 2;
  for (int i = 3; i <= 30; i++) {
    dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
  }
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = dp[n] / (365 * 10);
    if (dp[n] % (365 * 10) > 0)
      ans++;
    cout << ans << endl;
  }
  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char *argv[]) {
  vector<int> dp(30, 0);
  dp[0] = 1;
  dp[1] = 1;
  dp[2] = 2;
  for (int i = 3; i <= 30; i++) {
    dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
  }
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = dp[n] / (365 * 10);
    if (dp[n] % 365 > 0)
      ans++;
    cout << ans << endl;
  }
  return 0;
}