prithivMLmods/Coder-Stat huggingface dataset:
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll calcLucas(ll i_min_2, ll i_min_1, int stopIdx, int idx) {
  ll sum = i_min_1 + i_min_2;
  if (stopIdx + 1 == idx) {
    return sum;
  }
  return calcLucas(i_min_1, sum, stopIdx, idx + 1);
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll calcLucas(ll i_min_2, ll i_min_1, int stopIdx, int idx) {
  ll sum = i_min_1 + i_min_2;
  if (stopIdx + 1 == idx) {
    return sum;
  }
  return calcLucas(i_min_1, sum, stopIdx, idx + 1);
}

int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  } else if (n == 2) {
    cout << 3 << endl;
    return 0;
  }
  cout << calcLucas(2, 1, n, 3) << endl;
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  a[0] = 2, a[1] = 1;
  for (int i = 2; i < 100; i++)
    a[i] = a[i - 1] + a[i - 2];

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(100);
  a[0] = 2, a[1] = 1;
  for (int i = 2; i < 100; i++)
    a[i] = a[i - 1] + a[i - 2];

  cout << a[n] << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
#define rep(i, n) for (int i = 2; i < (int)(n); i++)

  int N;
  cin >> N;
  vector<int64_t> L(N);
  L[0] = 2;
  L[1] = 1;
  rep(i, N + 1) { L[i] = L[i - 1] + L[i - 2]; }
  cout << L[N];
}

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

int main() {
#define rep(i, n) for (int i = 2; i < (int)(n); i++)

  int N;
  cin >> N;
  vector<int64_t> L(N + 1);
  L[0] = 2;
  L[1] = 1;
  rep(i, N + 1) { L[i] = L[i - 1] + L[i - 2]; }
  cout << L[N];
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

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


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

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

int main() {
  int n;
  cin >> n;
  long long ret = 0;
  long long prev_2 = 2;
  long long prev_1 = 1;
  if (n == 0) {
    cout << 2 << endl;
    return 0;
  } else if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    ret = prev_1 + prev_2;
    prev_2 = prev_1;
    prev_1 = ret;
  }
  cout << ret << endl;
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
ll lucas(ll n) {
  if (n == 0)
    return 2;
  else if (n == 1)
    return 1;
  else
    return lucas(n - 1) + lucas(n - 2);
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
ll lucas(ll n) {
  if (n == 0)
    return 2;
  else if (n == 1)
    return 1;
  else
    return lucas(n - 1) + lucas(n - 2);
}

int main() {
  ll n;
  cin >> n;
  vector<ll> data(n + 1);
  data[0] = 2;
  data[1] = 1;
  for (ll i = 2; i <= n; i++) {
    data[i] = data[i - 1] + data[i - 2];
  }
  cout << data[n] << endl;
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
int lucas(int X) {
  if (X == 0)
    return 2;
  else if (X == 1)
    return 1;
  else
    return lucas(X - 1) + lucas(X - 2);
}
signed main() {
  int N;
  cin >> N;
  cout << lucas(N) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
int lucas(int X) {
  if (X == 0)
    return 2;
  else if (X == 1)
    return 1;
  else {
    int A = 2, B = 1, ans;
    rep(i, 1, X) {
      ans = A + B;
      A = B;
      B = ans;
    }
    return ans;
  }
}
signed main() {
  int N;
  cin >> N;
  cout << lucas(N) << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long int> L(N);
  L[0] = 2;
  L[1] = 1;
  for (int i = 2; i <= N; i++) {
    L[i] = L[i - 1] + L[i - 2];
  }

  cout << L[N] << endl;
}


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

int main() {
  int N;
  cin >> N;
  vector<long long> L(N + 1);
  L[0] = 2;
  L[1] = 1;
  for (int i = 2; i <= N; i++) {
    L[i] = L[i - 1] + L[i - 2];
  }

  cout << L[N] << endl;
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n) && n >= 1 && n <= 86) {
    if (n >= 2) {
      long long a = 2, b = 1, t;
      for (int i = 2; i <= n; i++) {
        t = a + b;
        a = b;
        b = t;
      }
      cout << t << endl;
    } else if (n == 1)
      printf("1\n");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n >= 2) {
    long long a = 2, b = 1, t;
    for (int i = 2; i <= n; i++) {
      t = a + b;
      a = b;
      b = t;
    }
    cout << t << endl;
  } else if (n == 1)
    printf("1\n");
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long N;
  cin >> N;

  vector<long long> ryuka;
  ryuka[0] = 2, ryuka[1] = 1;
  for (int i = 2; i <= N; i++)
    ryuka[i] = ryuka[i - 1] + ryuka[i - 2];

  cout << ryuka[N] << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long N;
  cin >> N;

  vector<long long> ryuka(N + 1);
  ryuka = {2, 1};

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

  cout << ryuka[N] << endl;

  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  cin.ignore();
  vector<long long> ryuka(N);
  ryuka = {2, 1};

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

  cout << ryuka[N] << endl;

  return 0;
}


Fixed submission:
#include <cstdio>
main() {
  long N, a = 2, b = 1, c;
  scanf("%d", &N);
  while (N--) {
    c = a + b;
    a = b;
    b = c;
  }
  printf("%ld\n", a);
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...)                                                       \
  ([&](decltype((x)) whole) {                                                  \
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \
  })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void) {
  int n;
  cin >> n;
  vector<ll> A(n);
  A[0] = 2;
  A[1] = 1;
  FOR(i, 2, n + 1) { A[i] = A[i - 1] + A[i - 2]; }
  cout << A[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...)                                                       \
  ([&](decltype((x)) whole) {                                                  \
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \
  })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void) {
  int n;
  cin >> n;
  vector<ll> A(n + 1);
  A[0] = 2;
  A[1] = 1;
  FOR(i, 2, n + 1) { A[i] = A[i - 1] + A[i - 2]; }
  cout << A[n] << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < n; i++)
typedef long long int ll;

int main() {

  int n;
  cin >> n;
  vector<ll> a(87);

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < n; i++)
typedef long long int ll;

int main() {

  int n;
  cin >> n;
  vector<ll> a(90);

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

  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#ifdef _DEBUG
#define DBG(n) n
#else
#define DBG(n)
#endif
#define INF 1e9
#define INFLL 1e18
#define EPS 1e-9
#define MOD 1000000007
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define REPR1(i, n) for (ll i = (n); i > 0; --i)
#define REPC(i, obj) for (auto i : obj)
#define ALL(obj) (obj).begin(), (obj).end()
#define SETP(n) cout << fixed << setprecision(n)
#define VV(T, h, w) move(vector<vector<T>>(h, move(vector<T>(w))))
#define VVI(T, h, w, i) move(vector<vector<T>>(h, move(vector<T>(w, i))))
using namespace std;
using ll = long long;
template <typename T = ll> inline T in() {
  T ret;
  cin >> ret;
  return ret;
}

ll lucas(ll n) { return n == 0 ? 2 : n == 1 ? 1 : lucas(n - 1) + lucas(n - 2); }
int main() { cout << lucas(in()) << endl; }

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#ifdef _DEBUG
#define DBG(n) n
#else
#define DBG(n)
#endif
#define INF 1e9
#define INFLL 1e18
#define EPS 1e-9
#define MOD 1000000007
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define REPR(i, n) for (ll i = (n)-1; i >= 0; --i)
#define REPR1(i, n) for (ll i = (n); i > 0; --i)
#define REPC(i, obj) for (auto i : obj)
#define ALL(obj) (obj).begin(), (obj).end()
#define SETP(n) cout << fixed << setprecision(n)
#define VV(T, h, w) move(vector<vector<T>>(h, move(vector<T>(w))))
#define VVI(T, h, w, i) move(vector<vector<T>>(h, move(vector<T>(w, i))))
using namespace std;
using ll = long long;
template <typename T = ll> inline T in() {
  T ret;
  cin >> ret;
  return ret;
}

ll lucas(ll n) {
  static vector<ll> memo(87, -1);
  if (memo[n] != -1) {
    return memo[n];
  } else if (n == 0) {
    return 2;
  } else if (n == 1) {
    return 1;
  } else {
    return (memo[n - 1] = lucas(n - 1)) + (memo[n - 2] = lucas(n - 2));
  }
}
int main() { cout << lucas(in()) << endl; }
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> a(N + 1);
  a[0] = 2;
  a[1] = 1;
  for (int i = 2; i <= N; i++)
    a[i] = a[i - 1] + a[i - 2];
  cout << a[N] << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  l[0] = 2;
  l[1] = 1;
  if (n == 1)
    cout << 1 << endl;
  else {
    for (int i = 2; i <= n; i++) {
      l[i] = l[i - 1] + l[i - 2];
    }
    cout << l[n] << endl;
  }
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<long long int> l(n + 1);
  l[0] = 2;
  l[1] = 1;
  if (n == 1)
    cout << 1 << endl;
  else {
    for (int i = 2; i <= n; i++) {
      l[i] = l[i - 1] + l[i - 2];
    }
    cout << l[n] << endl;
  }
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<uint64_t> luc(86, 0);

uint64_t luca(int n) {
  if (n == 0)
    return 2;
  else if (n == 1)
    return 1;
  else {
    if (luc.at(n) > 0)
      return luc.at(n);
    else
      return luc.at(n) = luca(n - 1) + luca(n - 2);
  }
}

int main() {
  int N;
  cin >> N;
  cout << luca(N) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<uint64_t> luc(87, 0);

uint64_t luca(int n) {
  if (n == 0)
    return 2;
  else if (n == 1)
    return 1;
  else {
    if (luc.at(n) > 0)
      return luc.at(n);
    else
      return luc.at(n) = luca(n - 1) + luca(n - 2);
  }
}

int main() {
  int N;
  cin >> N;
  cout << luca(N) << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

/*
ll Lucas(int N){
  if (N == 0) return 2;
  else if (N == 1) return 1;
  else return Lucas(N - 1) + Lucas(N - 2);
}
*/

int main(void) {
  int N;
  cin >> N;

  vector<ll> L(N);
  L.at(0) = 2;
  L.at(1) = 1;
  if (N == 1)
    cout << L.at(1) << endl;
  else {
    for (int i = 2; i <= N; i++) {
      L.at(i) = L.at(i - 2) + L.at(i - 1);
    }
    cout << L.at(N) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

/*
ll Lucas(int N){
  if (N == 0) return 2;
  else if (N == 1) return 1;
  else return Lucas(N - 1) + Lucas(N - 2);
}
*/

int main(void) {
  int N;
  cin >> N;

  vector<ll> L(87);
  L.at(0) = 2;
  L.at(1) = 1;
  if (N == 1)
    cout << L.at(1) << endl;
  else {
    for (int i = 2; i <= N; i++) {
      L.at(i) = L.at(i - 2) + L.at(i - 1);
    }
    cout << L.at(N) << endl;
  }
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N;
  cin >> N;

  if (N == 0) {
    cout << 2 << endl;
    return 0;
  }
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  vector<int64_t> Lyca(N + 1);
  Lyca.at(0) = 2;
  Lyca.at(1) = 1;
  if (2 <= N) {
    for (int i = 2; i <= N; N++) {
      Lyca.at(i) = Lyca.at(i - 1) + Lyca.at(i - 2);
    }
  }
  cout << Lyca.at(N) << endl;
}


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

int main() {
  int64_t N;
  cin >> N;

  if (N == 0) {
    cout << 2 << endl;
    return 0;
  }
  if (N == 1) {
    cout << 1 << endl;
    return 0;
  }
  vector<int64_t> Lyca(N + 1);
  Lyca.at(0) = 2;
  Lyca.at(1) = 1;
  for (int i = 2; i <= N; i++) {
    Lyca.at(i) = Lyca.at(i - 1) + Lyca.at(i - 2);
  }
  cout << Lyca.at(N) << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long f(long long i, vector<long long> &v) {
  if (i == 0) {
    v[i] = 2;
    return v[i];
  } else if (i == 1) {
    v[i] = 1;
    return v[i];
  } else if (v[i - 1] != 0 && v[i - 2] != 0) {
    v[i] = v[i - 1] + v[i - 2];
    return v[i];
  } else {
    v[i] = f(i - 1, v) + f(i - 2, v);
    return f(i - 1, v) + f(i - 2, v);
  }
}
int main() {
  long long a;
  cin >> a;
  vector<long long> v(a, 0);
  cout << f(a, v) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long f(long long i, vector<long long> &v) {
  if (i == 0) {
    v[i] = 2;
    return v[i];
  } else if (i == 1) {
    v[i] = 1;
    return v[i];
  } else if (v[i - 1] != 0 && v[i - 2] != 0) {
    v[i] = v[i - 1] + v[i - 2];
    return v[i];
  } else {
    v[i] = f(i - 1, v) + f(i - 2, v);
    return f(i - 1, v) + f(i - 2, v);
  }
}
int main() {
  long long a;
  cin >> a;
  vector<long long> v(a + 1, 0);
  cout << f(a, v) << endl;
}
Problem p03544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int n;
ll solve(ll a, ll b, int count) {
  if (count == n)
    return a + b;
  return solve(b, a + b, count + 1);
}
int main(void) {
  cin >> n;
  cout << solve(2, 1, 2) << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int n;
ll solve(ll a, ll b, int count) {
  if (count == n)
    return a + b;
  return solve(b, a + b, count + 1);
}
int main(void) {
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  cout << solve(2, 1, 2) << endl;
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  APG4b
//
//  Created by 安藤光希 on 2020/03/21.
//  Copyright © 2020 安藤光希. All rights reserved.
//

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  ll N;
  cin >> N;

  if (N == 0) {
    cout << 2 << endl;
  } else if (N == 1) {
    cout << 1 << endl;
  } else {
    vector<ll> S(N, 0);
    S[0] = 2;
    S[1] = 1;

    for (int i = 2; i < N + 1; i++) {
      S[i] += S[i - 1];
      S[i] += S[i - 2];
    }
    cout << S[N] << endl;
  }
}


Fixed submission:
//
//  main.cpp
//  APG4b
//
//  Created by 安藤光希 on 2020/03/21.
//  Copyright © 2020 安藤光希. All rights reserved.
//

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int main() {
  ll N;
  cin >> N;

  if (N == 0) {
    cout << 2 << endl;
  } else if (N == 1) {
    cout << 1 << endl;
  } else {
    vector<ll> S(N + 1, 0);
    S[0] = 2;
    S[1] = 1;

    for (int i = 2; i < N + 1; i++) {
      S[i] += S[i - 1];
      S[i] += S[i - 2];
    }
    cout << S[N] << endl;
  }
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t a = 2;
  int64_t b = 1;
  int N;
  cin >> N;
  if (N == 1) {
    cout << b << endl;
    exit(1);
  }
  int64_t t;
  for (int i = 2; i < N + 1; i++) {
    t = a + b;
    a = b;
    b = t;
  }
  cout << b << endl;
}


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

int main() {
  int64_t a = 2;
  int64_t b = 1;
  int N;
  cin >> N;
  if (N == 1) {
    cout << b << endl;
    exit(0);
  }
  int64_t t;
  for (int i = 2; i < N + 1; i++) {
    t = a + b;
    a = b;
    b = t;
  }
  cout << b << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

  if (N == 1)
    return 1;

  int64_t two = 2;
  int64_t one = 1;
  uint8_t i = 2;
  int64_t sum = 0;

  for (int i = 2; i <= N; i++) {
    sum = one + two;
    two = one;
    one = sum;
  }
  cout << sum;

  return 0;
}

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

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

  if (N == 1) {
    cout << 1;
    return 0;
  }

  int64_t two = 2;
  int64_t one = 1;
  uint8_t i = 2;
  int64_t sum = 0;

  for (int i = 2; i <= N; i++) {
    sum = one + two;
    two = one;
    one = sum;
  }
  cout << sum;

  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define ll long long int
#define MOD 1000000007

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<ll> ans(N);
  ans[0] = 2;
  ans[1] = 1;
  for (int i = 2; i <= N; i++) {
    ans[i] = ans[i - 1] + ans[i - 2];
  }
  cout << ans[N] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define ll long long int
#define MOD 1000000007

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<ll> ans(N + 1);
  ans[0] = 2;
  ans[1] = 1;
  for (int i = 2; i <= N; i++) {
    ans[i] = ans[i - 1] + ans[i - 2];
  }
  cout << ans[N] << endl;
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> data(N);

  data.at(0) = 2;
  data.at(1) = 1;

  for (int i = 2; i <= N; i++) {
    data.at(i) = data.at(i - 1) + data.at(i - 2);
  }

  cout << data.at(N) << endl;
}

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> data(N + 1);

  data.at(0) = 2;
  data.at(1) = 1;

  for (int i = 2; i <= N; i++) {
    data.at(i) = data.at(i - 1) + data.at(i - 2);
  }

  cout << data.at(N) << endl;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; i < (n); ++(i))
#define REP(i, m, n) for (int(i) = (m); (i) <= (n); ++(i))
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
typedef long long ll;
using namespace std;

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; i < (n); ++(i))
#define REP(i, m, n) for (int(i) = (m); (i) <= (n); ++(i))
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
typedef long long ll;
using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> l(87);
  l[0] = 2;
  l[1] = 1;
  for (ll i = 2; i <= n; ++i) {
    l[i] = l[i - 1] + l[i - 2];
  }
  cout << l[n] << endl;
  return 0;
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  vector<ll> Lucas(2);
  Lucas[0] = 2;
  Lucas[1] = 1;
  ll N;
  cin >> N;
  REP(i, N) { Lucas[i + 2] = Lucas[i + 1] + Lucas[i]; }
  cout << Lucas[N] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  vector<ll> Lucas(2);
  Lucas[0] = 2;
  Lucas[1] = 1;
  ll N;
  cin >> N;
  REP(i, N) { Lucas.pb(Lucas[i + 1] + Lucas[i]); }
  cout << Lucas[N] << "\n";
}
Problem p03544 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  A[0] = 2LL;
  A[1] = 1LL;
  for (int i = 2; i <= N; i++) {
    A[i] = A[i - 1] + A[i - 2];
  }
  cout << A[N] << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(90);
  A[0] = 2;
  A[1] = 1;
  for (int i = 2; i <= N; i++) {
    A[i] = A[i - 1] + A[i - 2];
  }
  cout << A[N] << endl;
  return 0;
}
Problem p03545 (Python)

Buggy submission (status: Runtime Error):
def solve():
    n = list(input())
    n = [ord(c) - ord("0") for c in n]
    sign = ["-", "+"]

    for i in range(2):
        for j in range(2):
            for k in range(2):
                t = n[0]
                t += n[1] * (i * 2 - 1)
                t += n[2] * (j * 2 - 1)
                t += n[3] * (k * 2 - 1)
                if t == 7:
                    print(f"{n[0]}{sign[i]}{n[1]}{sign[j]}{n[2]}{sign[k]}{n[3]}=7")
                    return


if __name__ == "__main__":
    solve()


Fixed submission:
def solve():
    n = list(input())
    n = [ord(c) - ord("0") for c in n]
    sign = ["-", "+"]

    for i in range(2):
        for j in range(2):
            for k in range(2):
                t = n[0]
                t += n[1] * (i * 2 - 1)
                t += n[2] * (j * 2 - 1)
                t += n[3] * (k * 2 - 1)
                if t == 7:
                    print(
                        "{}{}{}{}{}{}{}=7".format(
                            n[0], sign[i], n[1], sign[j], n[2], sign[k], n[3]
                        )
                    )
                    return


if __name__ == "__main__":
    solve()
Problem p03545 (Python)

Buggy submission (status: Runtime Error):
# https://atcoder.jp/contests/abc079/tasks/abc079_c
# C - Train Ticket

a, b, c, d = list(input().split()[0])
a, b, c, d = [int(x) for x in [a, b, c, d]]

# 2 ** 3通りなので全探索でいけそう
is_ok = False

for op1 in [True, False]:
    work_1 = a + b if op1 else a - b
    for op2 in [True, False]:
        work_2 = work_1 + c if op2 else work_1 - c
        for op3 in [True, False]:
            work_3 = work_2 + d if op3 else work_2 - c
            if work_3 == 7:
                is_ok = True
                op_list = [op1, op2, op3]
                break
        if is_ok:
            break
    if is_ok:
        break

op_list = ["+" if op else "-" for op in op_list]
ans = str(a) + op_list[0] + str(b) + op_list[1] + str(c) + op_list[2] + str(d) + "=7"
print(ans)


Fixed submission:
# https://atcoder.jp/contests/abc079/tasks/abc079_c
# C - Train Ticket

a, b, c, d = list(input().split()[0])
a, b, c, d = [int(x) for x in [a, b, c, d]]

# 2 ** 3通りなので全探索でいけそう
is_ok = False

for op1 in [True, False]:
    work_1 = a + b if op1 else a - b
    for op2 in [True, False]:
        work_2 = work_1 + c if op2 else work_1 - c
        for op3 in [True, False]:
            work_3 = work_2 + d if op3 else work_2 - d
            if work_3 == 7:
                is_ok = True
                op_list = [op1, op2, op3]
                break
        if is_ok:
            break
    if is_ok:
        break

op_list = ["+" if op else "-" for op in op_list]
ans = str(a) + op_list[0] + str(b) + op_list[1] + str(c) + op_list[2] + str(d) + "=7"
print(ans)
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define double long double
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
const int INF = 100000000;
typedef pair<int, int> P;

int a[3];

int main(void) {
  string str;
  cin >> str;
  a[0] = str[0] - '0';
  a[1] = str[1] - '0';
  a[2] = str[2] - '0';
  a[3] = str[3] - '0';
  string s = to_string(a[0]);
  int check = a[0];
  for (int bit = 0; bit < (1 << 3); ++bit) {
    for (int i = 0; i < 3; i++) {
      if (bit & (1 << i)) {
        check += a[i + 1];
        s += "+";
        s += to_string(a[i + 1]);
      } else {
        check -= a[i + 1];
        s += "-";
        s += to_string(a[i + 1]);
      }
    }
    if (check == 7) {
      s += "=7";
      cout << s << endl;
      break;
    } else {
      s = to_string(a[0]);
      check = a[0];
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define double long double
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
const int INF = 100000000;
typedef pair<int, int> P;

int a[4];

int main(void) {
  string str;
  cin >> str;
  a[0] = str[0] - '0';
  a[1] = str[1] - '0';
  a[2] = str[2] - '0';
  a[3] = str[3] - '0';
  string s = to_string(a[0]);
  int check = a[0];
  for (int bit = 0; bit < (1 << 3); ++bit) {
    for (int i = 0; i < 3; i++) {
      if (bit & (1 << i)) {
        check += a[i + 1];
        s += "+";
        s += to_string(a[i + 1]);
      } else {
        check -= a[i + 1];
        s += "-";
        s += to_string(a[i + 1]);
      }
    }
    if (check == 7) {
      s += "=7";
      cout << s << endl;
      break;
    } else {
      s = to_string(a[0]);
      check = a[0];
    }
  }
  return 0;
}
Problem p03545 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

void dfs(int i, string s, string f, int sum) {
  if (i == 4 && sum == 7) {
    cout << f;
    cout << "=7" << endl;
    exit(0);
  }

  string f2 = f + '+' + s[i];
  int sum2 = sum + (s[i] - '0');
  dfs(i + 1, s, f2, sum2);

  f2 = f + '-' + s[i];
  sum2 = sum - (s[i] - '0');
  dfs(i + 1, s, f2, sum2);

  return;
}

int main() {
  string s;
  cin >> s;
  string f;
  f += s[0];
  int sum = s[0] - '0';
  dfs(1, s, f, sum);
}


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

void dfs(int i, string s, string f, int sum) {
  if (i == 4) {
    if (sum == 7) {
      cout << f;
      cout << "=7" << endl;
      exit(0);
    }
    return;
  }

  string f2 = f + '+' + s[i];
  int sum2 = sum + (s[i] - '0');
  dfs(i + 1, s, f2, sum2);

  f2 = f + '-' + s[i];
  sum2 = sum - (s[i] - '0');
  dfs(i + 1, s, f2, sum2);

  return;
}

int main() {
  string s;
  cin >> s;
  string f;
  f += s[0];
  int sum = s[0] - '0';
  dfs(1, s, f, sum);
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<int> data(4);
  vector<char> hugou(3);
  int N;
  cin >> N;
  data[3] = N % 10;
  N /= 10;
  data[2] = N % 10;
  N /= 10;
  data[1] = N % 10;
  N /= 10;
  data[0] = N;
  for (int bit = 0; bit < (1 << 3); bit++) {
    int sum = data[0];
    for (int i = 0; i < 3; i++) {
      if (bit & (1 << i)) {
        sum += data[i];
        hugou[i - 1] = '+';
      } else {
        sum -= data[i];
        hugou[i - 1] = '-';
      }
    }
    if (sum == 7) {
      cout << data[0] << hugou[0] << data[1] << hugou[1] << data[2] << hugou[2]
           << data[3] << '=' << 7 << endl;
      break;
    }
  }
}

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

int main() {
  vector<int> data(4);
  vector<char> hugou(3);
  int N;
  cin >> N;
  data[3] = N % 10;
  N /= 10;
  data[2] = N % 10;
  N /= 10;
  data[1] = N % 10;
  N /= 10;
  data[0] = N;
  for (int bit = 0; bit < (1 << 3); bit++) {
    int sum = data[0];
    for (int i = 1; i < 4; i++) {
      if (bit & (1 << i - 1)) {
        sum += data[i];
        hugou[i - 1] = '+';
      } else {
        sum -= data[i];
        hugou[i - 1] = '-';
      }
    }
    if (sum == 7) {
      cout << data[0] << hugou[0] << data[1] << hugou[1] << data[2] << hugou[2]
           << data[3] << '=' << 7 << endl;
      break;
    }
  }
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int hugou[3];

int dfs(vector<int> vec_s, int i) {
  i += 1;
  // return 条件
  if (i > 3) {
    // cout << "i=3" << endl;
    int sum = 0;
    sum += vec_s[0];
    rep(j, 3) {
      if (hugou[j] == 0) {
        sum -= vec_s[j + 1];
      } else {
        sum += vec_s[j + 1];
      }
    }
    if (sum == 7) {
      // rep(i, 4) cout << vec_s[i];
      char plmi[3];
      rep(j, 3) {
        if (hugou[j] == 0) {
          plmi[j] = '-';
        } else {
          plmi[j] = '+';
        }
      }
      rep(j, 3) { cout << vec_s[j] << plmi[j]; }
      cout << vec_s[3] << "=7" << endl;
      exit(1);
    } else {
      return 0;
    }
  }
  // +をいれるとき
  hugou[i - 1] = 1;
  dfs(vec_s, i);

  // -をいれるとき
  hugou[i - 1] = 0;
  dfs(vec_s, i);

  return 0;
}

int main() {
  const int s_size = 4;
  string s;
  cin >> s;
  vector<int> vec_s(s_size, 0);
  rep(i, s_size) { vec_s[i] = s[i] - '0'; }
  // rep(i, 4) cout << vec_s[i];
  // cout << endl;
  dfs(vec_s, 0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int hugou[3];

int dfs(vector<int> vec_s, int i) {
  i += 1;
  // return 条件
  if (i > 3) {
    // cout << "i=3" << endl;
    int sum = 0;
    sum += vec_s[0];
    rep(j, 3) {
      if (hugou[j] == 0) {
        sum -= vec_s[j + 1];
      } else {
        sum += vec_s[j + 1];
      }
    }
    if (sum == 7) {
      // rep(i, 4) cout << vec_s[i];
      char plmi[3];
      rep(j, 3) {
        if (hugou[j] == 0) {
          plmi[j] = '-';
        } else {
          plmi[j] = '+';
        }
      }
      rep(j, 3) { cout << vec_s[j] << plmi[j]; }
      cout << vec_s[3] << "=7" << endl;
      exit(0);
    } else {
      return 0;
    }
  }
  // +をいれるとき
  hugou[i - 1] = 1;
  dfs(vec_s, i);

  // -をいれるとき
  hugou[i - 1] = 0;
  dfs(vec_s, i);

  return 0;
}

int main() {
  const int s_size = 4;
  string s;
  cin >> s;
  vector<int> vec_s(s_size, 0);
  rep(i, s_size) { vec_s[i] = s[i] - '0'; }
  // rep(i, 4) cout << vec_s[i];
  // cout << endl;
  dfs(vec_s, 0);
  return 0;
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  char ch, b[4];
  int a[4], s1 = 0, s2 = 0, i, j, k, m;
  for (i = 0; i < 4; i++) {
    cin >> ch;
    a[i] = ch - '0';
    s1 += a[i];
  }
  if (s1 == 7)
    cout << a[0] << '+' << a[1] << '+' << a[2] << '+' << a[3] << "=7" << endl;
  else {
    s2 = a[0] - a[1] - a[2] - a[3];
    if (s2 == 7)
      cout << a[0] << '-' << a[1] << '-' << a[2] << '-' << a[3] << "=7" << endl;
    else {
      for (k = 0; k < 2; k++) {
        for (i = 1; i < 4; i++) {
          for (j = 1; j < 4; j++) {
            if (j == i)
              k == 0 ? b[j] = '+' : b[j] = '-';
            else
              k == 0 ? b[j] = '-' : b[j] = '+';
          }
          m = k == 0 ? s2 + 2 * a[i] : s1 - 2 * a[i];
          if (m == 7) {
            cout << a[0] << b[1] << a[1] << b[2] << a[2] << b[3] << a[3] << "=7"
                 << endl;
            exit(1);
          }
        }
      }
    }
  }
}

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

int main() {
  char ch, b[4];
  int a[4], s1 = 0, s2 = 0, i, j, k, m;
  for (i = 0; i < 4; i++) {
    cin >> ch;
    a[i] = ch - '0';
    s1 += a[i];
  }
  if (s1 == 7)
    cout << a[0] << '+' << a[1] << '+' << a[2] << '+' << a[3] << "=7" << endl;
  else {
    s2 = a[0] - a[1] - a[2] - a[3];
    if (s2 == 7)
      cout << a[0] << '-' << a[1] << '-' << a[2] << '-' << a[3] << "=7" << endl;
    else {
      for (k = 0; k < 2; k++) {
        for (i = 1; i < 4; i++) {
          for (j = 1; j < 4; j++) {
            if (j == i)
              k == 0 ? b[j] = '+' : b[j] = '-';
            else
              k == 0 ? b[j] = '-' : b[j] = '+';
          }
          m = k == 0 ? s2 + 2 * a[i] : s1 - 2 * a[i];
          if (m == 7) {
            cout << a[0] << b[1] << a[1] << b[2] << a[2] << b[3] << a[3] << "=7"
                 << endl;
            exit(0);
          }
        }
      }
    }
  }
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  int d = n % 10;
  n /= 10;
  int c = n % 10;
  n /= 10;
  int b = n % 10;
  n /= 10;
  int a = n;
  for (int i = 0; i < (1 << 3); i++) {
    bitset<3> s(i);
    int sum = a;
    if (s.test(0))
      sum += b;
    else
      sum -= b;
    if (s.test(1))
      sum += c;
    else
      sum -= c;
    if (s.test(3))
      sum += d;
    else
      sum -= d;
    if (sum == 7) {
      cout << a;
      if (s.test(0))
        cout << '+';
      else
        cout << '-';
      cout << b;
      if (s.test(1))
        cout << '+';
      else
        cout << '-';
      cout << c;
      if (s.test(2))
        cout << '+';
      else
        cout << '-';
      cout << d << "=7" << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  int d = n % 10;
  n /= 10;
  int c = n % 10;
  n /= 10;
  int b = n % 10;
  n /= 10;
  int a = n;
  for (int i = 0; i < (1 << 3); i++) {
    bitset<3> s(i);
    int sum = a;
    if (s.test(0))
      sum += b;
    else
      sum -= b;
    if (s.test(1))
      sum += c;
    else
      sum -= c;
    if (s.test(2))
      sum += d;
    else
      sum -= d;
    if (sum == 7) {
      cout << a;
      if (s.test(0))
        cout << '+';
      else
        cout << '-';
      cout << b;
      if (s.test(1))
        cout << '+';
      else
        cout << '-';
      cout << c;
      if (s.test(2))
        cout << '+';
      else
        cout << '-';
      cout << d << "=7" << endl;
      return 0;
    }
  }
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
char c[4];

void search(int a[], int b[]) {
  int s = a[0];
  int i;
  if (c[0] != 0)
    return;

  for (i = 0; i < 3; i++) {
    if (b[i] == 0)
      s += a[i + 1];
    else
      s -= a[i + 1];
  }
  if (s == 7) {
    for (i = 0; i < 3; i++) {
      if (b[i] == 0)
        c[i] = '+';
      else
        c[i] = '-';
    }
  }

  for (i = 0; i < 3; i++) {
    b[i]++;
    search(a, b);
    b[i]--;
  }
}

int main(void) {
  int a[4], b[4];
  int i, n;

  scanf("%d", &n);

  for (i = 0; i < 4; i++) {
    a[3 - i] = n % 10;
    b[i] = 0;
    c[i] = 0;
    n /= 10;
  }

  search(a, b);

  for (i = 0; i < 3; i++)
    printf("%d%c", a[i], c[i]);
  printf("%d=7\n", a[3]);

  return 0;
}

Fixed submission:
#include <stdio.h>
char c[4];

void search(int a[], int b[]) {
  int s = a[0];
  int i;
  if (c[0] != 0)
    return;

  for (i = 0; i < 3; i++) {
    if (b[i] >= 2)
      return;
    if (b[i] == 0)
      s += a[i + 1];
    else
      s -= a[i + 1];
  }
  if (s == 7) {
    for (i = 0; i < 3; i++) {
      if (b[i] == 0)
        c[i] = '+';
      else
        c[i] = '-';
    }
  }

  for (i = 0; i < 3; i++) {
    b[i]++;
    search(a, b);
    b[i]--;
  }
}

int main(void) {
  int a[4], b[4];
  int i, n;

  scanf("%d", &n);

  for (i = 0; i < 4; i++) {
    a[3 - i] = n % 10;
    b[i] = 0;
    c[i] = 0;
    n /= 10;
  }

  search(a, b);

  for (i = 0; i < 3; i++)
    printf("%d%c", a[i], c[i]);
  printf("%d=7\n", a[3]);

  return 0;
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string func(vector<int> &n_abcd, int index, int count, string formula) {
  if (index >= 3) {
    // ベースケース
    if (count == 7) {
      return formula;
    } else {
      return formula.substr(0, formula.size() - 2);
    }
  } else {
    // 再帰ケース
    formula = func(n_abcd, index + 1, count + n_abcd.at(index + 1),
                   formula += "+" + to_string(n_abcd.at(index + 1)));
    if (formula.size() == 7) {
      return formula;
    }
    formula = func(n_abcd, index + 1, count - n_abcd.at(index + 1),
                   formula += "-" + to_string(n_abcd.at(index + 1)));
    if (formula.size() == 7) {
      return formula;
    }
  }
}

int main() {
  string abcd;
  cin >> abcd;

  int num = stoi(abcd);
  vector<int> n_abcd(4);

  for (int i = 0; i < 4; i++) {
    n_abcd.at(i) = num % 10;
    num /= 10;
  }
  reverse(n_abcd.begin(), n_abcd.end());

  string result = func(n_abcd, 0, n_abcd.at(0), to_string(n_abcd.at(0)));
  cout << result + "=7" << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string func(vector<int> &n_abcd, int index, int count, string formula) {
  if (index >= 3) {
    // ベースケース
    if (count == 7) {
      return formula;
    } else {
      return formula.substr(0, formula.size() - 2);
    }
  } else {
    // 再帰ケース
    formula = func(n_abcd, index + 1, count + n_abcd.at(index + 1),
                   formula += "+" + to_string(n_abcd.at(index + 1)));
    if (formula.size() == 7) {
      return formula;
    }
    formula = func(n_abcd, index + 1, count - n_abcd.at(index + 1),
                   formula += "-" + to_string(n_abcd.at(index + 1)));
    if (formula.size() == 7) {
      return formula;
    }
    return formula.substr(0, formula.size() - 2);
  }
}

int main() {
  string abcd;
  cin >> abcd;

  int num = stoi(abcd);
  vector<int> n_abcd(4);

  for (int i = 0; i < 4; i++) {
    n_abcd.at(i) = num % 10;
    num /= 10;
  }
  reverse(n_abcd.begin(), n_abcd.end());

  string result = func(n_abcd, 0, n_abcd.at(0), to_string(n_abcd.at(0)));
  cout << result + "=7" << endl;
}
Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;

int ctoi(char c) {
  string s = "0123456789";
  FOR(i, 0, 10) if (c == s[i]) return i;
  return 0;
}

int op[] = {-1, 1};

int main() {
  string S;
  cin >> S;
  int A = ctoi(S[0]);
  int B = ctoi(S[1]);
  int C = ctoi(S[2]);
  int D = ctoi(S[3]);
  FOR(i, 0, 2) FOR(j, 0, 2) FOR(k, 0, 2) {
    if (A + op[i] * B + op[j] * C + op[k] * D == 7) {
      cout << A;
      i ? cout << "+" : cout << "-";
      cout << B;
      j ? cout << "+" : cout << "-";
      cout << C;
      k ? cout << "+" : cout << "-";
      cout << D << "=7" << endl;
      exit(1);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
using namespace std;

int ctoi(char c) {
  string s = "0123456789";
  FOR(i, 0, 10) if (c == s[i]) return i;
  return 0;
}

int op[] = {-1, 1};

int main() {
  string S;
  cin >> S;
  int A = ctoi(S[0]);
  int B = ctoi(S[1]);
  int C = ctoi(S[2]);
  int D = ctoi(S[3]);
  FOR(i, 0, 2) FOR(j, 0, 2) FOR(k, 0, 2) {
    if (A + op[i] * B + op[j] * C + op[k] * D == 7) {
      cout << A;
      i ? cout << "+" : cout << "-";
      cout << B;
      j ? cout << "+" : cout << "-";
      cout << C;
      k ? cout << "+" : cout << "-";
      cout << D << "=7" << endl;
      i = 2;
      j = 2;
      k = 2;
    }
  }
}
Problem p03546 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[10][10];
int b[205][205];

int main() {
  int i, j, m, n, k, h, w;
  cin >> h >> w;
  for (i = 0; i < 10; i++)
    for (j = 0; j < 10; j++)
      cin >> a[i][j];
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      cin >> b[i][j];
  for (k = 0; k < 10; k++)
    for (i = 0; i < 10; i++)
      for (j = 0; j < 10; j++)
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  int ans = 0;
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      if (a[i][j] >= 0)
        ans += a[b[i][j]][1];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[10][10];
int b[205][205];

int main() {
  int i, j, m, n, k, h, w;
  cin >> h >> w;
  for (i = 0; i < 10; i++)
    for (j = 0; j < 10; j++)
      cin >> a[i][j];
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      cin >> b[i][j];
  for (k = 0; k < 10; k++)
    for (i = 0; i < 10; i++)
      for (j = 0; j < 10; j++)
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  int ans = 0;
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      if (b[i][j] >= 0)
        ans += a[b[i][j]][1];
  cout << ans << endl;
  return 0;
}
Problem p03546 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define INF 120000000

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

void warshall_floyd(vector<vector<int>> &d) {
  int n = d.size();
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> d(10, vector<int>(10));
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      cin >> d[i][j];
    }
  }

  warshall_floyd(d);

  vector<int> a(10, 0);
  for (int i = 0; i < h * w; i++) {
    int tmp;
    cin >> tmp;
    a[tmp]++;
  }
  int ans = 0;
  for (int i = 0; i <= 9; i++) {
    ans += a[i] * d[i][1];
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define INF 120000000

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

void warshall_floyd(vector<vector<int>> &d) {
  int n = d.size();
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> d(10, vector<int>(10));
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      cin >> d[i][j];
    }
  }

  warshall_floyd(d);

  vector<int> a(10, 0);
  for (int i = 0; i < h * w; i++) {
    int tmp;
    cin >> tmp;
    if (tmp != -1)
      a[tmp]++;
  }
  int ans = 0;
  for (int i = 0; i <= 9; i++) {
    ans += a[i] * d[i][1];
  }
  cout << ans << endl;

  return 0;
}
Problem p03546 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> c(10, vector<int>(10));
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> c[i][j];
    }
  }
  for (int k = 0; k < 10; k++) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
      }
    }
  }

  int ans = 0;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      int A;
      cin >> A;
      ans += c[A][1];
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> c(10, vector<int>(10));
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> c[i][j];
    }
  }
  for (int k = 0; k < 10; k++) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
      }
    }
  }

  int ans = 0;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      int A;
      cin >> A;
      if (A != -1) {
        ans += c[A][1];
      }
    }
  }
  cout << ans << endl;
  return 0;
}