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

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int calc[45];

int fib(int n) {
  if (calc[n] != 0)
    return calc[n];
  for (int i = 2; i <= n; i++)
    calc[i] = calc[i - 2] + calc[i - 1];
  return calc[n];
}

int main() {
  int n;
  cin >> n;
  REP(i, 45) calc[i] = 0;
  calc[0] = 1;
  calc[1] = 1;
  cout << fib(n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
int calc[45];

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

int main() {
  int n;
  cin >> n;
  REP(i, 45) calc[i] = 0;
  calc[0] = 1;
  calc[1] = 1;
  cout << fib(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int memo[50];

int fib(int n) {
  if (memo[n] != -1)
    return memo[n];

  if (n == 0 or n == 1) {
    memo[n] = 1;
    return 1;
  }
  int ans = fib(n - 1) + fib(n - 2);
  memo[n] = ans;
  return ans;
}

int main() {

  int n;
  cin >> n;
  memset(memo, -1, sizeof(memo));
  cout << fib(n) << endl;
}

Error: TLE

Fixed submission:

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

int fib(int n) {
  if (n == 0 or 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;

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;
}
*/

#include <iostream>
using namespace std;

int fib[45] = {0};

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

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


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 <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX 45
int F[MAX];

int fibonacci(int i) {
  if (i == 1 || i == 0)
    return F[i] = 1;
  if (F[i])
    return F[i];
  return F[i] = fibonacci(i - 1) + fibonacci(i - 2);
}

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

  cout << F[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  cout << fibonacci(n) << endl;

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

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long a = 1, b = 1, c;
  for (int i = 0; i < n - 1; i++) {
    c = b;
    b = a;
    a = b + c;
  }
  cout << a << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int N;

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

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

#include <iostream>
#include <stdio.h>
using namespace std;

int n, d[50];

long long int fib(long long int k) {
  if (!k || k == 1)
    return 1;
  if (!d[k]) {
    d[k] = fib(k - 1) + fib(k - 2);
    return d[k];
  } else
    return d[k];
}

int main() {
  cin >> n;
  d[0] = 1;
  d[1] = 1;
  cout << fib(n) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define PI 3.1415926535
#define INF 114514810
#define FOR(i, r, n) for (int i = (r); i < (n); i++)
#define REP(i, n) FOR(i, (0), n)

using namespace std;

long long int n;

long long int fib(int k) {
  if (!k || k == 1)
    return 1;
  return fib(k - 1) + fib(k - 2);
}

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

// DP(???????¨??????? Dynamic Programing)
// ????????±???????????¢???
// ?????£??????????????°???
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A

#include <iostream>
using namespace std;

#define MAX_N 44

struct memo {
  int num;
  bool flg;
} Memo[MAX_N + 1];

int n;
int fib(int);

int main() {
  cin >> n;
  for (int i = 0; i < MAX_N + 1; ++i)
    Memo[i].flg = false;
  cout << fib(n) << endl;
  return 0;
}

int fib(int m) {
  if (m == 0) {
    if (!Memo[0].flg) {
      Memo[0].num = 1;
      Memo[0].flg = true;
    }
    return 1;
  }
  if (m == 1) {
    if (!Memo[1].flg) {
      Memo[1].num = 2;
      Memo[1].flg = true;
    }
    return 1;
  }

  if (Memo[m].flg) {
    return Memo[m].num;
  } else {
    Memo[m].num = fib(m - 1) + fib(m - 2);
    Memo[m].flg = true;
    return fib(m - 1) + fib(m - 2);
  }
}

Error: TLE

Fixed submission:

// DP(???????¨??????? Dynamic Programing)
// ????????±???????????¢???
// ?????£??????????????°???
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_A

#include <iostream>
using namespace std;

#define MAX_N 44

struct memo {
  int num;
  bool flg;
} Memo[MAX_N + 1];

int n;
int fib(int);

int main() {
  cin >> n;
  for (int i = 0; i < MAX_N + 1; ++i)
    Memo[i].flg = false;
  cout << fib(n) << endl;
  return 0;
}

int fib(int m) {
  if (m == 0) {
    if (!Memo[0].flg) {
      Memo[0].num = 1;
      Memo[0].flg = true;
    }
    return 1;
  }
  if (m == 1) {
    if (!Memo[1].flg) {
      Memo[1].num = 2;
      Memo[1].flg = true;
    }
    return 1;
  }

  if (Memo[m].flg) {
    return Memo[m].num;
  } else {
    Memo[m].num = fib(m - 1) + fib(m - 2);
    Memo[m].flg = false;
    return fib(m - 1) + fib(m - 2);
  }
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n, a[45];

int main() {
  cin >> n;
  a[1] = a[0] = 1;
  for (int i = 2; i <= n; i++) {
    a[i] = a[i - 1] + a[i - 2];
  }
  cout << a[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

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

int main(void) {
  int n, v[50];
  cin >> n;
  if (n == 0 || n == 1) {
    cout << 1 << endl;
  } else {
    v[0] = v[1] = 1;
    for (int i = 2; i <= n; i++) {
      v[i] = v[i - 1] + v[i - 2];
    }
    cout << v[n] << endl;
  }
}

Error: 0

Fixed submission:

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

int main(void) {
  int n;
  cin >> n;
  if (n == 0 || n == 1) {
    cout << 1 << endl;
  } else {
    vector<int> v(n);
    v[0] = v[1] = 1;
    for (int i = 2; i <= n; i++) {
      v[i] = v[i - 1] + v[i - 2];
    }
    cout << v[n] << endl;
  }
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int fibmemo[45] = {0};
int fibMemo(int n);

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

int fibMemo(int n) {
  if (fibmemo[n] == 0) {
    fibmemo[n] = fib(n);
  }
  return fibmemo[n];
}

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


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(void) {
  int n;
  cin >> n;
  cout << fib(n) << endl;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int f[46] = {0};

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

  return f[n];
}

int main(void) {
  int x;
  cin >> x;
  cout << fib(x) << 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(void) {
  int x;
  cin >> x;
  cout << fib(x) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

// フィボナッチ数列
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

#define MAX_N 44
// 無効な値の定義
const int INV = -1;

// フィボナッチ数列を保持する配列
int fib[MAX_N + 1];

int Fibonacci(int n);

int main(void) {

  int n;

  cin >> n;

  for (int i = 0; i < MAX_N + 1; i++) {
    fib[i] = INV;
  }

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

  // フィボナッチ数列の表示
  printf("%d\\n", Fibonacci(n));

  return 0;
}

// フィボナッチ数列を算出する関数
int Fibonacci(int n) {

  if (n <= 1) {
    // n = 0, n = 1 の時
    return 1;
  }

  if (fib[n] != INV) {
    // 既に計算されている時は, 結果を返す
    return fib[n];
  }

  // フィボナッチ数列の算出の算出
  fib[n] = Fibonacci(n - 1) + Fibonacci(n - 2);

  return fib[n];
}


Error: TLE

Fixed submission:

// フィボナッチ数列
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

#define MAX_N 44
// 無効な値の定義
const int INV = -1;

// フィボナッチ数列を保持する配列
int fib[MAX_N + 1];

int Fibonacci(int n);

int main(void) {

  int n;

  cin >> n;

  for (int i = 0; i <= MAX_N; i++) {
    fib[i] = INV;
  }

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

  // フィボナッチ数列の表示
  printf("%d\\n", Fibonacci(n));

  return 0;
}

// フィボナッチ数列を算出する関数
int Fibonacci(int n) {
  if (n <= 1) {
    return 1;
  }

  if (fib[n] != INV) {
    // 既に計算されている時は, 結果を返す
    return fib[n];
  }

  return Fibonacci(n - 1) + Fibonacci(n - 2);
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int fib(int n) {
  int ans[45] = {1, 1}, i;
  for (i = 2; i <= n; i++) {
    ans[i] = ans[i - 1] + ans[i - 2];
  }
  return ans[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) {
  switch (n) {
  case 0:
    return 1;
  case 1:
    return 1;
  default:
    return fib(n - 1) + fib(n - 2);
  }
}

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

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

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
int memofib(int n) {
  static map<int, int> cache;
  map<int, int>::iterator it = cache.find(n);
  if (it != cache.cend())
    return it->second;
  int t;
  if (n == 1)
    return 1;
  else if (n == 2)
    return 2;
  else
    return memofib(n - 1) + memofib(n - 2);
  cache.insert(std::make_pair(n, t));
  return t;
}
int main(void) {
  int n;
  cin >> n;
  cout << memofib(n) << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int d;
int dd[50];
int fib(int a) {
  if (a == 0 || a == 1)
    return dd[a] = 1;
  if (dd[a] != -1)
    return dd[a];
  return dd[a] = fib(a - 1) + fib(a - 2);
}

int main() {
  int c;
  cin >> c;
  for (int i = 0; i < 50; ++i) {
    dd[i] = -1;
  }

  cout << fib(c) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
int d;
int dd[50];
int fib(int a) {
  if (a == 0 || a == 1)
    return dd[a] = 1;
  if (dd[a] != -1)
    return dd[a];
  return fib(a - 1) + fib(a - 2);
}

int main() {
  int c;
  cin >> c;
  for (int i = 0; i < 50; ++i) {
    dd[i] = -1;
  }

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

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

int f[45];

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;
  for (int i = 0; i <= 44; i++)
    f[i] = -1;
  cout << fib(n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int fib(int n) {
  int f[45] = {0};
  if (n == 0 || n == 1)
    return f[n] = 1;
  if (f[n] > 0)
    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 <iostream>
using namespace std;
int n, fibonacci, f[44] = {0};

int makefibnacci(int n) {
  f[0] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; i++) {
    f[i] = f[i - 2] + f[i - 1];
  }
  return f[n];
}

int main() {
  cin >> n;
  fibonacci = makefibnacci(n);
  cout << fibonacci << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

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

typedef long long ll;

ll table[45];
bool flg[45];

ll fib(int n) {

  if (n == 0)
    return 1;
  else if (n == 1)
    return 1;
  else {
    if (flg[n])
      return table[n];
    else {
      flg[n] = true;
      return table[n] = fib(n - 1) + fib(n - 2);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  table[0] = table[1] = 1;
  flg[0] = flg[1] = true;
  int n;
  cin >> n;
  cout << fib(n) << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

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

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
int fib(int);
using namespace std;
int main() {
  int n, ans, dp[1001];
  dp[0] = 1;
  dp[1] = 1;
  cin >> n;
  for (int i = 0; i <= n; i++) {
    dp[i + 2] = dp[i] + dp[i + 1];
  }
  // ans = fib(n - 1);
  ans = dp[n];
  cout << ans << endl;
}
int fib(int n) {
  if (n < 1)
    return 1;
  else {
    return fib(n - 1) + fib(n - 2);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
int fib(int);
using namespace std;
int main() {
  int n, ans;
  cin >> n;
  ans = fib(n - 1);
  cout << ans << endl;
}
int fib(int n) {
  if (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 memo[50];
int fib(int n) {
  if (n == 0 || n == 1)
    memo[n] = 1;

  if (memo[n] != 0) {
    return memo[n];
  } else {
    memo[n] = fib(n - 1) + fib(n - 2);
    return memo[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;

  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>
#include <vector>

using namespace std;

vector<int> F(45, -1);

int fib(int n) {
  if (!n || 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;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int fib(int n) {
  if (!n || 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 <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int dp[50];

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

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

  REP(i, 50) { dp[i] = -1; }

  cout << fib(X) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

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

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

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

#include <iostream>
using namespace std;

typedef long long hyper;

hyper fib(int n) {
  if (n < 2)
    return 1;
  if (n == 2)
    return 2;
  if (n == 3)
    return 3;
  return 3 * fib(n - 2) - fib(n - 4);
}

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

typedef long long hyper;

hyper fib(int n) {
  if (n < 2) {
    return 1;
  }
  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 <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int ans = fib(n);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int ans = fib(n);
  cout << ans << endl;
  return 0;
}
Problem p02233 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  long long int fib[n];
  fib[0] = 1;
  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;

long long int fib(int, long long int *);
int main() {
  int n;
  cin >> n;
  long long int fibarray[n + 1];
  for (int i = 0; i <= n; i++)
    fibarray[i] = -1;
  cout << fib(n, fibarray) << endl;

  return 0;
}

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

#include <iostream>
using namespace std;

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

  int n, N[50];
  N[0] = N[1] = 1;
  cin >> n;

  for (int i = 2; i <= n; i++)
    N[i] = N[i - 1] + N[i - 2];

  cout << N[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() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  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 fib(int n);
int memo[45];
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 (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 memo[45];
int main() {
  int n;

  // 入力
  cin >> n;

  // 出力
  cout << fib(n) << endl;

  return 0;
}

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