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

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

int fib(int n) {
  int num[10000] = {1, 1};
  for (int i = 2; i <= n; i++) {
    num[i] = num[i - 1] + num[i - 2];
  }
  return (num[n]);
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
}

Error: TLE

Fixed submission:

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

int fib(int n) {
  if (n <= 1)
    return (1);
  else
    return (fib(n - 1) + fib(n - 2));
}

int main() {
  int n;
  cin >> n;

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

#define PI 3.14159265359
#define INF 2000000000
#define MAX 500000
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

ll F[45] = {0};

ll fibo(int n) {
  if (F[n])
    return F[n];
  if (n == 0 || n == 1)
    return F[n] = 1;
  return F[n] = fibo(n - 1) + fibo(n - 2);
}

int main() {
  int n;
  cin >> n;
  cout << fibo(n) << endl;
}

Error: TLE

Fixed submission:

#define PI 3.14159265359
#define INF 2000000000
#define MAX 500000
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

ll F[45] = {0};

ll fibo(int n) {
  if (n == 0 || n == 1)
    return F[n] = 1;
  if (F[n])
    return F[n];
  else
    return fibo(n - 1) + fibo(n - 2);
}

int main() {
  int n;
  cin >> n;
  cout << fibo(n) << endl;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int A[50];
int fib(int n) {
  if (n == 1 || n == 0)
    A[n] = 1;

  else if (A[n])
    ;
  else
    A[n] = fib(n - 1) + fib(n - 2);
  return A[n];
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int A[50];
int fib(int n) {
  if (n == 1 || n == 0)
    A[n] = 1;
  else
    A[n] = fib(n - 1) + fib(n - 2);
  return A[n];
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, dp[45] = {1, 1};
  cin >> n;
  for (int i = 2; i <= n; i++)
    dp[i] = dp[i - 1] + dp[i - 2];
  cout << dp[n] << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int fib(int n) {
  switch (n) {
  case 0:
    return 1;
  case 1:
    return 1;
  default:
    return fib(n - 1) + fib(n - 2);
  }
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "iostream"

using namespace std;

int F[46];

int fib(int n) {

  if (F[n] != 0)
    return F[n];

  if (n == 0 || n == 1)
    F[n] = 1;

  else
    F[n] = fib(n - 1) + fib(n - 2);

  return F[n];
}

int main() {

  int n;

  cin >> n;

  cout << fib(n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "iostream"

using namespace std;

int fib(int n) {

  if (n == 0 || n == 1)
    return 1;

  else
    return fib(n - 1) + fib(n - 2);
}

int main() {

  int n;

  cin >> n;

  cout << fib(n) << endl;

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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i <= n; i++) // 変数,初期値,終値
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int main(void) {
  int n, i;
  int ans = 0;
  int dp[44] = {1, 1};
  cin >> n;
  i = n;
  REP(i, 2, n) { dp[i] = dp[i - 1] + dp[i - 2]; }
  cout << dp[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i <= n; i++) // 変数,初期値,終値
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int dp;
int memo;
int fib(int n);

int main(void) {
  int n;
  int ans = 0;
  cin >> n;
  ans = fib(n);

  cout << ans << endl;

  return 0;
}
int fib(int n) {
  if (n == 0 || n == 1)
    return 1;
  else
    return fib(n - 1) + fib(n - 2);
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int fib(int n, int a, int b);

int main() {
  int n;
  cin >> n;
  cout << fib(n - 1, 1, 1) << endl;
  return (0);
}

int fib(int n, int a, int b) {
  int f;
  f = a;
  if (n >= 1) {
    f = fib(n - 1, a + b, a);
  }
  return (f);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int fib(int n, int a, int b);

int main() {
  int n;
  cin >> n;
  cout << fib(n - 2, 1, 1) << endl;
  return (0);
}

int fib(int n, int a, int b) {
  int f;
  if (n != 0) {
    f = fib(n - 1, a + b, a);
  } else
    f = a + b;
  return (f);
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int fib(int n);
int memo[45];
int main() {
  int n;

  //??\\???
  cin >> n;

  //??????
  cout << fib(n) << endl;

  return 0;
}

int fib(int n) {
  /* ??????1 */
  if (n == 0 || n == 1)
    return 1;
  else if (memo[n] != 0) {
    return memo[n];
  } else {
    return memo[n] = fib(n - 1) + fib(n - 2);
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int fib(int n);

int main() {
  int n;

  //??\\???
  cin >> n;

  //??????
  cout << fib(n) << endl;

  return 0;
}

int fib(int n) {
  /* ??????1 */
  if (n == 0 || n == 1)
    return 1;
  return fib(n - 1) + fib(n - 2);
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define N 45
int main() {
  int F[N];
  int n, i;
  scanf("%d", &n);

  for (i = 0; i <= n; i++) {
    if (i == 0 || i == 1)
      F[i] = 1;
    else
      F[i] = F[i - 2] + F[i - 1];
  }
  printf("%d\\n", F[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define N 45

int F[N];

int fibonacci(int n) {
  if (n == 0 || n == 1) {
    F[n] = 1;
    return F[n];
  } else if (n < N) {
    F[n] = fibonacci(n - 2) + fibonacci(n - 1);
    return F[n];
  }
  return F[n];
}

int main() {
  int n;
  scanf("%d", &n);
  printf("%d\\n", fibonacci(n));
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
int f[50];

void fi(int a) { f[a] = f[a - 2] + f[a - 1]; }

int main(void) {
  int n;
  scanf("%d", &n);
  rep(i, 50) f[i] = 0;
  f[0] = 1, f[1] = 1;
  for (int i = 2; i < 50; ++i)
    fi(i);
  printf("%d\\n", f[n]);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

int fi(int a) { return (a == 0 || a == 1) ? 1 : fi(a - 1) + fi(a - 2); }

int main(void) {
  int n;
  scanf("%d", &n);
  printf("%d\\n", fi(n));
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

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

using namespace std;

// dp memory
const int MAX_N = 44;
int fib[MAX_N + 1];

int main() {
  int n;
  cin >> n;

  // fib dynamic programming
  fib[0] = 1;
  fib[1] = 1;
  REP(i, 2, n + 1) { fib[i] = fib[i - 1] + fib[i - 2]; }

  cout << fib[n] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

int fib(int n) {
  if (n <= 1)
    return 1;

  return fib(n - 1) + fib(n - 2);
}

int main() {
  int n;
  cin >> n;

  cout << fib(n) << endl;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 44

int main() {
  int n;
  int F[50];
  F[0] = F[1] = 1;

  cin >> n;

  for (int i = 2; i < n + 1; i++) {
    F[i] = F[i - 1] + F[i - 2];
  }

  cout << F[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 44

int fib(int i) {
  if (i <= 1)
    return 1;
  else
    return fib(i - 1) + fib(i - 2);
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;

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

#include <iostream>
using namespace std;

int f[45] = {};

int fib(int n);

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}

int fib(int n) {
  if (n == 0 || n == 1)
    return 1;
  else if (f[n] != 0)
    return f[n];
  else
    return f[n] = fib(n - 1) + fib(n - 2);
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

int fib(int n);

int main() {
  int n;
  scanf("%d", &n);
  printf("%d\\n", fib(n));
  return 0;
}

int fib(int n) {
  if (n == 0 || n == 1)
    return 1;
  else
    return fib(n - 1) + fib(n - 2);
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdlib.h>

using namespace std;

#define MAX_N 44

class Fib {
private:
  int size;
  int *A;

public:
  Fib(int size) {
    this->size = size;
    makeFib();
  }

  int getFib(int n) { return A[n]; }

  void makeFib() {
    int i;
    this->A = (int *)malloc(sizeof(int) * (this->size + 1));
    this->A[0] = 1;
    this->A[1] = 1;
    for (i = 2; i <= this->size; i++) {
      this->A[i] = this->A[i - 2] + this->A[i - 1];
    }
  }

  ~Fib() { free(A); }
};

int main() {

  int n;
  Fib *fib = new Fib(MAX_N);

  cin >> n;

  cout << fib->getFib(n) << endl;

  delete fib;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;

int F[44] = {0};

int Fib(int n) {

  if (n == 0 || n == 1) {
    return (F[n] = 1);
  }

  if (F[n] == 1) {
    return F[n];
  }

  return (F[n] = Fib(n - 1) + Fib(n - 2));
}

int main() {

  int n;

  cin >> n;

  cout << Fib(n) << endl;

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int n, a[50];
int solve(int i) {
  if (i == 1 || i == 0)
    return 1;
  return solve(i - 1) + solve(i - 2);
}
int main() {
  int n;
  cin >> n;
  int ans[50];
  ans[0] = 1, ans[1] = 1;
  for (int i = 2; i <= n; i++) {
    ans[i] = ans[i - 1] + ans[i - 2];
  }
  cout << ans[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int n, a[50];
int solve(int i) {
  if (i == 1 || i == 0)
    return 1;
  return solve(i - 1) + solve(i - 2);
}
int main() {
  int n;
  cin >> n;
  cout << solve(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

// 再帰
int fibonacci(int n) {
  if (n == 0)
    return 1; // たぶん本当はフィボナッチ数列はf(0)=0
              // だと思うのだが、問題文がそうなっていたので１を返す。
  else if (n == 1)
    return 1;
  else
    return fibonacci(n - 2) + fibonacci(n - 1);
}

// DP
int fibonacci_dp(int n) {
  int i;
  int fib[45];

  fib[0] = 1;
  fib[1] = 1;

  for (i = 2; i <= n; i++) {
    fib[i] = fib[i - 2] + fib[i - 1];
  }
  return fib[n];
}

int main() {
  int n;

  cin >> n;
  // cout << fibonacci(n) << endl;
  cout << fibonacci_dp(n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int fibonacci(int n) {
  if (n == 0)
    return 1; // たぶん本当はフィボナッチ数列はf(0)=0
              // だと思うのだが、問題文がそうなっていたので１を返す。
  else if (n == 1)
    return 1;
  else
    return fibonacci(n - 2) + fibonacci(n - 1);
}

int main() {
  int n;

  cin >> n;
  cout << fibonacci(n) << endl;

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

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

vector<long long int> fibonatti;
long long int dp[10000];
int calc(int n) {
  if (n <= 1)
    return 1;
  if (dp[n] != -1)
    return dp[n];
  fibonatti[n] = calc(n - 1) + calc(n - 2);
  return dp[n] = fibonatti[n];
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  fibonatti.resize(n + 1);
  fibonatti[0] = fibonatti[1] = 1;
  for (int i = 0; i <= n; i++) {
    dp[i] = -1;
  }
  dp[0] = dp[1] = 1;
  std::cout << calc(n) << std::endl;
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

vector<long long int> fibonatti;
long long int dp[10000];
int calc(int n) {
  if (n <= 1)
    return 1;
  if (dp[n] != -1)
    return dp[n];
  fibonatti[n] = calc(n - 1) + calc(n - 2);
  return dp[n] = fibonatti[n];
}

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  fibonatti.resize(n);
  fibonatti[0] = fibonatti[1] = 1;
  for (int i = 0; i <= n; i++) {
    dp[i] = -1;
  }
  dp[0] = dp[1] = 1;
  std::cout << calc(n) << std::endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define NIL -2000000001
#define MAX 2000

int A[MAX];
int n = 0;

int fib(int num) {
  if (A[num] != NIL) {
    return A[num];
  } else {
    A[num - 2] = fib(num - 2);
    A[num - 1] = fib(num - 1);
    return A[num - 2] + A[num - 1];
  }
}

int main() {
  int num;
  for (int i = 0; i < MAX; i++) {
    A[i] = NIL;
  }
  A[0] = 1;
  A[1] = 1;
  scanf("%d", &num);
  printf("%d\\n", fib(num));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define NIL -2000000001
#define MAX 2000

int A[MAX];
int n = 0;

int fib(int num) {
  if (A[num] != NIL) {
    return A[num];
  } else {
    return fib(num - 1) + fib(num - 2);
  }
}

int main() {
  int num;
  for (int i = 0; i < MAX; i++) {
    A[i] = NIL;
  }
  A[0] = 1;
  A[1] = 1;
  scanf("%d", &num);
  printf("%d\\n", fib(num));
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

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

unordered_map<int, int> h;

int fib(int n);

int fib(int n) {
  if (h.count(n) != 0) {
    return h[n];
  }

  int ret;
  if (n == 0) {
    ret = 1;
  } else if (n == 1) {
    ret = 1;
  } else {
    ret = fib(n - 1) + fib(n - 2);
  }
  h[n] = ret;
  return ret;
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int fib(int n) {
  if (n == 0)
    return 1;
  else if (n == 1)
    return 1;
  else {
    return fib(n - 1) + fib(n - 2);
  }
}

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int f[50];

int fibo(int n) {
  if (n <= 1)
    return 1;
  else {
    if (f[n]) {
      return f[n];
    } else {
      return f[n] = fibo(n - 1) + fibo(n - 2);
    }
  }
}

int main() {
  int n;
  cin >> n;

  cout << fibo(n) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int f[50];

int fibo(int n) {
  if (n <= 1)
    return 1;
  return f[n] = fibo(n - 1) + fibo(n - 2);
}

int main() {
  int n;
  cin >> n;

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

#include <iostream>
using namespace std;

// ?????£??????????????°???????±???????
int fibonacci(int n);
int fib[45];

int main() {
  int num;
  cin >> num;
  int res = 0;

  res = fibonacci(num);
  cout << res << endl;

  // getchar();
  // getchar();

  return 0;
}

int fibonacci(int n) {
  if (n == 0 || n == 1) {
    return 1;
  }
  // return fibonacci(n - 1) + fibonacci(n - 2);
  if (fib[n]) {
    return fib[n];
  }
  return fib[n] = fibonacci(n - 1) + fibonacci(n - 2);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

// ?????£??????????????°???????±???????
int fibonacci(int n);

int main() {
  int num;
  cin >> num;
  int res = 0;

  res = fibonacci(num);
  cout << res << endl;

  // getchar();
  // getchar();

  return 0;
}

int fibonacci(int n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return 1;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, fib[45];
  cin >> n;
  fib[0] = fib[1] = 1;
  for (int i = 2; i <= n; i++)
    fib[i] = fib[i - 1] + fib[i - 2];
  cout << fib[n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int fib(int p) {
  if (p == 0 || p == 1)
    return 1;
  else
    return fib(p - 1) + fib(p - 2);
}
int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;

ll F[45];
ll fibonacci(ll n) {
  if (n < 2)
    return F[n] = 1;
  if (F[n])
    return F[n];
  return F[n] = fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
  ll n;
  cin >> n;
  cout << fibonacci(n) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;

ll fibonacci(ll n) {
  if (n < 2)
    return 1;
  return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
  ll n;
  cin >> n;
  cout << fibonacci(n) << endl;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

#define MAX 45
#define NIL -1
int Memo[MAX];

int fibonacci(int n) {
  if (n == 0 || n == 1)
    return Memo[n] = 1;
  if (Memo[n] != NIL)
    return Memo[n];
  return Memo[n] = fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  int n;
  scanf("%d", &n);
  fill_n(Memo, MAX, NIL);
  printf("%d\\n", fibonacci(n));
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

int fibonacci(int n) {
  if (n == 0 || n == 1)
    return 1;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  int n;
  scanf("%d", &n);
  printf("%d\\n", fibonacci(n));
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

#define MAX 50

int db[MAX];

int fibonacci(int n) {
  if (n == 0 || n == 1)
    return 1;
  if (db[n] != -1)
    return db[n];
  return db[n] = fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  int n;
  for (int i = 0; i < MAX; ++i)
    db[i] = -1;

  scanf("%d", &n);
  int result = fibonacci(n);
  printf("%d\\n", result);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define MAX 50

int db[MAX];

int fibonacci(int n) {
  if (n == 0 || n == 1)
    return 1;
  if (db[n] != -1)
    return db[n];
  return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  int n;
  for (int i = 0; i < MAX; ++i)
    db[i] = -1;

  scanf("%d", &n);
  int result = fibonacci(n);
  printf("%d\\n", result);
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A
#include <iostream>
using namespace std;

int fibonacci(int n) {
  if (n == 0 || n == 1) {
    return 1;
  } else {
    return fibonacci(n - 1) + fibonacci(n - 2);
  }
}

int fib(int n) {
  int a0 = 0, a1 = 1;
  int a2 = 0;
  for (int i = 0; i < n; i++) {
    a2 = a0 + a1;
    a0 = a1;
    a1 = a2;
  }
  return a2;
}

int main() {
  int n;
  cin >> n;
  // cout << fibonacci(n) << endl;
  cout << fib(n) << endl;
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A
#include <iostream>
using namespace std;

int fibonacci(int n) {
  if (n == 0 || n == 1) {
    return 1;
  } else {
    return fibonacci(n - 1) + fibonacci(n - 2);
  }
}

int main() {
  int n;
  cin >> n;
  cout << fibonacci(n) << endl;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int fib(int n);

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
}

int fib(int n) {
  int num1 = 1, num2 = 1;
  int temp = 0;
  if (n == 0 || n == 1)
    return 1;

  for (int i = 1; i < n; i++) {
    temp = num2;
    num2 = num1 + num2;
    num1 = temp;
  }
  return num2;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int fib(int n);

int main() {
  int n;
  cin >> n;
  cout << fib(n) << endl;
}

int fib(int n) {
  if (n == 0 || n == 1)
    return 1;
  else {
    return fib(n - 1) + fib(n - 2);
  }
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {

  int n;
  cin >> n;

  int Fib[50] = {0};
  Fib[0] = Fib[1] = 1;
  for (int i = 2; i <= n; ++i)
    Fib[i] = Fib[i - 1] + Fib[i - 2];

  cout << Fib[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int fib(int i) {
  if (i == 0)
    return 1;
  if (i == 1)
    return 1;
  return fib(i - 1) + fib(i - 2);
}

int main() {

  int n;
  cin >> n;

  cout << fib(n) << endl;

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

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

constexpr int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int i = 1; i <= n; i++)
    m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++)
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
    }
  }

  cout << m[1][n] << endl;
}


Error: 0

Fixed submission:

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

constexpr int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i - 1] >> p[i];
  }

  for (int i = 1; i <= n; i++)
    m[i][i] = 0;
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n; i++) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for (int k = i; k <= j - 1; k++)
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
    }
  }

  cout << m[1][n] << endl;
}