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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define P pair<ll, ll>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

int main() {
  ll n;
  cin >> n;
  string ans = {};
  ll p = n, q;
  while (1) {
    q = p % 2; // あまり
    if (q < 0)
      q += 2;
    ans.push_back((char)'0' + q);
    p = (p - q) / (-2);
    if (p == 1) {
      ans.push_back((char)'1');
      break;
    }
  }
  reverse(ALL(ans));
  co(ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define P pair<ll, ll>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

int main() {
  ll n;
  cin >> n;
  if (n == 0) {
    co(0);
    return 0;
  }
  string ans = {};
  ll p = n, q;
  while (1) {
    q = p % 2; // あまり
    if (q < 0)
      q += 2;
    ans.push_back((char)'0' + q);
    p = (p - q) / (-2);
    if (p == 1) {
      ans.push_back((char)'1');
      break;
    }
  }
  reverse(ALL(ans));
  co(ans);

  return 0;
}
Problem p03286 (C++)

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

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

using namespace std;

typedef long long ll;

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

  int N;
  cin >> N;

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

  int b = 1;
  int s = 1;

  vector<int> v;
  while (N != 0) {
    if (N % (b * 2) != 0) {
      N -= s * b;
      v.push_back(1);
    } else {
      v.push_back(0);
    }

    s *= -1;
    b *= 2;
  }

  int l = (int)v.size();
  for (int i = l - 1; i >= 0; i--)
    cout << v[i];
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;

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

  int N;
  cin >> N;

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

  ll b = 1LL;
  int s = 1;

  vector<int> v;
  while (N != 0) {
    if (N % (b * 2) != 0) {
      N -= s * b;
      v.push_back(1);
    } else {
      v.push_back(0);
    }

    s *= -1;
    b *= 2;
  }

  int l = (int)v.size();
  for (int i = l - 1; i >= 0; i--)
    cout << v[i];
  cout << endl;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#include <istream>
#include <sstream>

using namespace std;

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

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

  stack<int> s;

  ll dig = 2;
  bool minus = false;
  while (N != 0) {
    int rem = abs(N % dig);
    s.push(rem / (dig / 2));
    if (minus)
      N += rem;
    else
      N -= rem;
    dig *= 2;
    minus = !minus;
  }

  if (s.size() == 0) {
    s.push(0);
  }

  while (s.size() != 0) {
    int p = s.top();
    s.pop();
    cout << p;
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#include <istream>
#include <sstream>

using namespace std;

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

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

  stack<int> s;

  ll dig = 2;
  bool minus = false;
  while (N != 0) {
    ll rem = abs(N % dig);
    s.push(rem / (dig / 2));
    if (minus)
      N += rem;
    else
      N -= rem;
    dig *= 2;
    minus = !minus;
  }

  if (s.size() == 0) {
    s.push(0);
  }

  while (s.size() != 0) {
    int p = s.top();
    s.pop();
    cout << p;
  }
  cout << endl;

  return 0;
}
Problem p03286 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << x << '\n'
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(i, v.size()) cout << ' ' << v[i];                                        \
  cout << '\n'
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

const ll MOD = 1000000007ll;
// const ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-11;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  if (n == 0) {
    cout << n << '\n';
    return 0;
  }
  int b = 1;
  string ans = "";
  while (n) {
    if (n % (b * (-2))) {
      n -= b;
      ans = '1' + ans;
    } else {
      ans = '0' + ans;
    }
    b *= -2;
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << x << '\n'
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(i, v.size()) cout << ' ' << v[i];                                        \
  cout << '\n'
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

const ll MOD = 1000000007ll;
// const ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-11;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  if (n == 0) {
    cout << n << '\n';
    return 0;
  }
  ll b = 1;
  string ans = "";
  while (n) {
    if (n % (b * (-2))) {
      n -= b;
      ans = '1' + ans;
    } else {
      ans = '0' + ans;
    }
    b *= -2;
  }
  cout << ans << '\n';
  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int abs(int s) {
  if (s < 0)
    s = -s;
  return s;
}
int main() {
  int N, r[100], i = -1;
  cin >> N;
  while (N != -1) {
    i++;
    r[i] = abs(N) % (2);
    if (N < 0 && abs(N) % 2 == 1) {
      N = N / (-2) + 1;
    } else {
      N = N / (-2);
    }
    if (N == 1) {
      i++;
      r[i] = 1;
      break;
    }
  }
  if (N == -1) {
    i++;
    r[i] = 1;
    i++;
    r[i] = 1;
  }
  for (int j = i; j >= 0; j--) {
    cout << r[j];
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int abs(int s) {
  if (s < 0)
    s = -s;
  return s;
}
int main() {
  int N, r[100], i = -1;
  cin >> N;
  if (N == 0) {
    cout << 0;
    return 0;
  }
  while (N != -1) {
    i++;
    r[i] = abs(N) % (2);
    if (N < 0 && abs(N) % 2 == 1) {
      N = N / (-2) + 1;
    } else {
      N = N / (-2);
    }
    if (N == 1) {
      i++;
      r[i] = 1;
      break;
    }
  }
  if (N == -1) {
    i++;
    r[i] = 1;
    i++;
    r[i] = 1;
  }
  for (int j = i; j >= 0; j--) {
    cout << r[j];
  }
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
void print(int x) {
  switch (x) {
  case 0:
    cout << "10";
    break;
  case 1:
    cout << "11";
    break;
  case 2:
    cout << "00";
    break;
  case 3:
    cout << "01";
    break;
  default:
    break;
  }
  return;
}

using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  if (n == 0 | n == 1) {
    cout << n << endl;
    return 0;
  } else if (n > 0) {
    n -= 2;
    int digit;
    digit = static_cast<int>(log(3 * n + 4) / log(4));
    n -= static_cast<int>((pow(4, digit) - 4) / 3);
    cout << "1";
    while (digit != 0) {
      print(n / static_cast<int>(pow(4, digit - 1)));
      n %= static_cast<int>(pow(4, digit - 1));
      digit--;
    }
  } else if (n < 0) {
    n *= -1;
    n -= 3;
    int digit, flag = 0;
    digit = static_cast<int>(log((3 * n + 8) / 2) / log(4));
    if (n % 2 == 0) {
      n++;
      flag++;
    }
    n -= static_cast<int>((2 * pow(4, digit) - 8) / 3);
    cout << "1";
    n /= 2;
    while (digit != 0) {
      print(n / static_cast<int>(pow(4, digit - 1)));
      n %= static_cast<int>(pow(4, digit - 1));
      digit--;
    }
    if (flag == 0) {
      cout << "0";
    } else {
      cout << "1";
    }
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
void print(int x) {
  switch (x) {
  case 0:
    cout << "10";
    break;
  case 1:
    cout << "11";
    break;
  case 2:
    cout << "00";
    break;
  case 3:
    cout << "01";
    break;
  default:
    break;
  }
  return;
}

using namespace std;
int main() {
  long long n;
  cin >> n;
  string s;
  if (n == 0 | n == 1) {
    cout << n << endl;
    return 0;
  } else if (n > 0) {
    n -= 2;
    int digit;
    digit = static_cast<int>(log(3 * n + 4) / log(4));
    n -= static_cast<int>((pow(4, digit) - 4) / 3);
    cout << "1";
    while (digit != 0) {
      print(n / static_cast<int>(pow(4, digit - 1)));
      n %= static_cast<int>(pow(4, digit - 1));
      digit--;
    }
  } else if (n < 0) {
    n *= -1;
    n -= 3;
    int digit, flag = 0;
    digit = static_cast<int>(log((3 * n + 8) / 2) / log(4));
    if (n % 2 == 0) {
      n++;
      flag++;
    }
    n -= static_cast<int>((2 * pow(4, digit) - 8) / 3);
    cout << "1";
    n /= 2;
    while (digit != 0) {
      print(n / static_cast<int>(pow(4, digit - 1)));
      n %= static_cast<int>(pow(4, digit - 1));
      digit--;
    }
    if (flag == 0) {
      cout << "0";
    } else {
      cout << "1";
    }
  }
  cout << endl;
  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF LLONG_MAX
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
  ll n;
  cin >> n;
  stack<ll> st;
  if (n == 0) {
    cout << 0 << endl;
  }
  while (n != 1) {
    if (n % 2 != 0) {
      st.push(1);
      n = (n - 1) / -2;
    } else {
      st.push(0);
      n /= -2;
    }
  }
  cout << 1;
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define INF LLONG_MAX
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
  ll n;
  cin >> n;
  stack<ll> st;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  while (n != 1) {
    if (n % 2 != 0) {
      st.push(1);
      n = (n - 1) / -2;
    } else {
      st.push(0);
      n /= -2;
    }
  }
  cout << 1;
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
  cout << endl;
  return 0;
}
Problem p03286 (C++)

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

using namespace std;

string n_ary_convert(long n, long ary) {
  if (n == 0)
    return 0;
  string ans = "";
  while (n != 0) {
    if (n < 0) {
      long temp = -n % ary;
      ans += to_string(temp);
      n = n / ary;
      if (temp == 0)
        continue;
      if (ary < 0)
        n++;
      else
        n--;
    } else {
      ans += to_string(n % ary);
      n /= ary;
    }
  }
  reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  int N;
  cin >> N;
  cout << n_ary_convert(N, -2) << endl;
  return 0;
}


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

using namespace std;

string n_ary_convert(long n, long ary) {
  if (n == 0)
    return "0";
  string ans = "";
  while (n != 0) {
    if (n < 0) {
      long temp = -n % ary;
      ans += to_string(temp);
      n = n / ary;
      if (temp == 0)
        continue;
      if (ary < 0)
        n++;
      else
        n--;
    } else {
      ans += to_string(n % ary);
      n /= ary;
    }
  }
  reverse(ans.begin(), ans.end());
  return ans;
}

int main() {
  int N;
  cin >> N;
  cout << n_ary_convert(N, -2) << endl;
  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  ll N;
  cin >> N;

  string S;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }

  REP(i, 32) {
    int m = 1 << i;
    if (abs(N) % (m * 2) == m) {
      S.push_back('1');
      if (i % 2 == 0) {
        N -= m;
      } else {
        N += m;
      }
    } else {
      S.push_back('0');
    }
    if (N == 0)
      break;
    // cout << i << " " << N << endl;
  }
  reverse(ALL(S));
  cout << S << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  ll N;
  cin >> N;

  string S;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }

  REP(i, 32) {
    ll m = 1LL << i;
    if (abs(N) % (m * 2) == m) {
      S.push_back('1');
      if (i % 2 == 0) {
        N -= m;
      } else {
        N += m;
      }
    } else {
      S.push_back('0');
    }
    if (N == 0)
      break;
    // cout << i << " " << N << endl;
  }
  reverse(ALL(S));
  cout << S << endl;

  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

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

  vector<ll> ans(0);

  // 右からM桁目を確定させる
  ll M = 0LL;
  while (N != 0LL || M == 65LL) {
    if (llabs(N) % (1 << (M + 1)) != 0LL) {
      ans.emplace_back(1);
      N -= pow(-2LL, M);
    } else
      ans.emplace_back(0);
    M++;
  }
  if (ans.empty()) {
    cout << 0 << endl;
    return 0;
  }
  ll j = ans.size();
  for (ll i = j - 1; i >= 0; i--) {
    cout << ans[i];
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

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

  vector<ll> ans(0);

  // 右からM桁目を確定させる
  ll M = 0;
  while (N != 0) {
    ll d = pow(-2LL, M + 1);
    if (N % d != 0) {
      ans.emplace_back(1);
      N -= pow(-2LL, M);
    } else
      ans.emplace_back(0);
    M++;
  }
  if (ans.empty()) {
    cout << 0 << endl;
    return 0;
  }
  ll j = ans.size();
  for (ll i = j - 1; i >= 0; i--) {
    cout << ans[i];
  }
  cout << endl;

  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

// inf
constexpr ll infl = 1LL << 60;
const int inf = 1 << 30;

int main() {
  int N;
  cin >> N;
  string ans = "";
  while (N != 0) {
    int d = abs(N) % 2;
    ans += to_string(d);
    if (N > 0)
      N /= -2;
    else
      N = (-N + d * 2) / 2;
  }
  if (ans.size())
    reverse(ans.begin(), ans.end());
  cout << (ans.size() ? ans : 0) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

// inf
constexpr ll infl = 1LL << 60;
const int inf = 1 << 30;

int main() {
  int N;
  cin >> N;
  string ans = "";
  while (N != 0) {
    int d = abs(N) % 2;
    ans += to_string(d);
    if (N > 0)
      N /= -2;
    else
      N = (-N + d * 2) / 2;
  }
  if (ans.size())
    reverse(ans.begin(), ans.end());
  cout << (ans.size() ? ans : "0") << endl;
  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef long long ll;
typedef long double lb;
typedef vector<int, vector<int>> vec;
typedef pair<int, int> P;
#define REP(i, j, n) for (int i = j; i < (n); i++)
#define RREP(i, n) for (int i = n; 0 < i; i--)
#define print(out) cout << out << "\n";
#define all(vec) vec.begin(), vec.end()
#define mod 1000000007
const int INF = 1e9;

int main() {
  ll N, i = 1;
  cin >> N;
  string ans = "";
  if (N == 0) {
    print(0) return 0;
  }
  while (N != 0) {
    // print(N<<" "<<i)
    if (N % (int)((powl(2LL, i))) != 0) {
      ans = '1' + ans;
      N = N - powl((-2LL), i - 1);
    } else {
      ans = '0' + ans;
    }
    i++;
  }

  print(ans)
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef long long ll;
typedef long double lb;
typedef vector<int, vector<int>> vec;
typedef pair<int, int> P;
#define REP(i, j, n) for (int i = j; i < (n); i++)
#define RREP(i, n) for (int i = n; 0 < i; i--)
#define print(out) cout << out << "\n";
#define all(vec) vec.begin(), vec.end()
#define mod 1000000007
const int INF = 1e9;

int main() {
  ll N, i = 1;
  cin >> N;
  string ans = "";
  if (N == 0) {
    print(0) return 0;
  }
  while (N != 0) {
    // print(N<<" "<<i)
    if (N % ((ll)((powl(2LL, i)))) != 0) {
      ans = '1' + ans;
      N = N - powl((-2LL), i - 1);
    } else {
      ans = '0' + ans;
    }
    i++;
  }

  print(ans)
}
Problem p03286 (C++)

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

int main() {
  ll n;
  cin >> n;
  if (n == 0 || n == 1) {
    cout << n << endl;
    return 0;
  }
  vector<int> ans;
  int x = 1;
  while (n != 0) {
    ll n0 = n;
    ll n1 = n + x;
    if (n0 % (-2 * x) == 0)
      ans.push_back(0);
    else {
      ans.push_back(1);
      n -= x;
    }
    x *= (-2);
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++)
    cout << ans.at(i);
}

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

int main() {
  ll n;
  cin >> n;
  if (n == 0 || n == 1) {
    cout << n << endl;
    return 0;
  }
  vector<int> ans;
  ll x = 1;
  while (n != 0) {
    ll n0 = n;
    ll n1 = n + x;
    if (n0 % (-2 * x) == 0)
      ans.push_back(0);
    else {
      ans.push_back(1);
      n -= x;
    }
    x *= (-2);
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++)
    cout << ans.at(i);
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef pair<ll, pair<int, int>> P;
typedef pair<ll, pair<ll, ll>> T;
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
int INFM = 100000000;
ll MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll pow_MOD(ll x, ll n) { // x^n
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  res %= MOD;
  return res;
}

int main() {
  ll N;
  cin >> N;
  if (N == 0)
    cout << 0;
  vector<int> ans(0);
  int cnt = 1;
  while (N != 0) {
    if (N % pow_MOD(2, cnt) == 0) {
      ans.push_back(0);
    } else {
      ans.push_back(1);
      if (cnt % 2) {
        N -= pow_MOD(2, cnt - 1);
      } else {
        N += pow_MOD(2, cnt - 1);
      }
    }
    cnt++;
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans.at(i);
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef pair<ll, pair<int, int>> P;
typedef pair<ll, pair<ll, ll>> T;
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
int INFM = 100000000;
ll MOD = 100000000700000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll pow_MOD(ll x, ll n) { // x^n
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  res %= MOD;
  return res;
}

int main() {
  ll N;
  cin >> N;
  if (N == 0)
    cout << 0;
  vector<int> ans(0);
  int cnt = 1;
  while (N != 0) {
    if (N % pow_MOD(2, cnt) == 0) {
      ans.push_back(0);
    } else {
      ans.push_back(1);
      if (cnt % 2) {
        N -= pow_MOD(2, cnt - 1);
      } else {
        N += pow_MOD(2, cnt - 1);
      }
    }
    cnt++;
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans.at(i);
  }
  cout << endl;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef long l;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
const double PI = 3.141592653589;
const int INF = 1000000007;
const ll LMAX = 1000000000000001;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  // cout<<n<<endl;
  if (n == 0)
    puts(0);
  else {
    vector<int> s(0);
    while (n != 1) {
      if (abs(n) % 2) {
        s.pb(1);
        n -= 1;
      } else {
        s.pb(0);
      }
      n /= -2;
    }
    s.pb(1);
    rep(i, s.size()) cout << s[s.size() - 1 - i];
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef long l;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
const double PI = 3.141592653589;
const int INF = 1000000007;
const ll LMAX = 1000000000000001;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  // cout<<n<<endl;
  if (n == 0)
    puts("0");
  else {
    vector<int> s(0);
    while (n != 1) {
      if (abs(n) % 2) {
        s.pb(1);
        n -= 1;
      } else {
        s.pb(0);
      }
      n /= -2;
    }
    s.pb(1);
    rep(i, s.size()) cout << s[s.size() - 1 - i];
    cout << endl;
  }

  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;

string dfs(LL N) {
  if (!N)
    return "";
  return (abs(N) % 2 ? dfs((N < 0 ? N / (-2) + 1 : N / (-2))) + "1"
                     : dfs(N / (-2)) + "0");
}

int main() {
  LL N;
  cin >> N;
  cout << (N ? dfs(N) : 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;

string dfs(LL N) {
  if (!N)
    return "";
  return (abs(N) % 2 ? dfs((N < 0 ? N / (-2) + 1 : N / (-2))) + "1"
                     : dfs(N / (-2)) + "0");
}

int main() {
  LL N;
  cin >> N;
  cout << (N ? dfs(N) : "0") << endl;
  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>

using namespace std;

int main() {

  int n, cnt = 0, di = 1;
  vector<int> ans;

  cin >> n;

  if (n == 0) {
    cout << "0" << endl;
    return 0;
  }

  while (n != 0) {
    // cout << n << " " << di << endl;
    // if (n == di) break;

    if (abs(n) % (di * 2) != 0) {
      ans.emplace_back(1);
      if (cnt % 2 == 0)
        n -= di;
      else
        n += di;
    } else
      ans.emplace_back(0);

    cnt++;
    di *= 2;
  }

  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i];
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>

using namespace std;

int main() {

  long long int n, cnt = 0, di = 1;
  vector<int> ans;

  cin >> n;

  if (n == 0) {
    cout << "0" << endl;
    return 0;
  }

  while (n != 0) {
    // cout << n << " " << di << endl;
    // if (n == di) break;

    if (abs(n) % (di * 2) != 0) {
      ans.emplace_back(1);
      if (cnt % 2 == 0)
        n -= di;
      else
        n += di;
    } else
      ans.emplace_back(0);

    cnt++;
    di *= 2;
  }

  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i];
  cout << endl;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  long int n;
  vector<int> s(1000);
  int len;
  cin >> n;

  vector<long int> a(31);
  a.at(0) = 1;
  for (int i = 1; i < 40; i++) {
    a.at(i) = (-2) * a.at(i - 1);
  }

  for (int i = 0; i < 40; i++) {
    if (n % abs(a.at(i + 1)) == 0)
      s.at(i) = 0;
    else {
      s.at(i) = 1;
      n -= a.at(i);
    }

    if (n == 0) {
      len = i + 1;
      break;
    }
  }

  for (int i = 1; i <= len; i++) {
    cout << s.at(len - i);
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  long int n;
  vector<int> s(1000);
  int len;
  cin >> n;

  vector<long int> a(40);
  a.at(0) = 1;
  for (int i = 1; i < 40; i++) {
    a.at(i) = (-2) * a.at(i - 1);
  }

  for (int i = 0; i < 40; i++) {
    if (n % abs(a.at(i + 1)) == 0)
      s.at(i) = 0;
    else {
      s.at(i) = 1;
      n -= a.at(i);
    }

    if (n == 0) {
      len = i + 1;
      break;
    }
  }

  for (int i = 1; i <= len; i++) {
    cout << s.at(len - i);
  }
  cout << endl;
}
Problem p03286 (C++)

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a), end(a)

int main(void) {
  ll N;
  cin >> N;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> base(31);
  REP(i, 31) {
    if (i == 0)
      base[i] = 1;
    else {
      base[i] = base[i - 1] * -2;
    }
  }

  string ans;
  int i = 0;
  while (N) {
    if (N % (2 * abs(base[i]))) {
      ans.pb('1');
      N -= base[i];
    } else {
      ans.pb('0');
    }
    i++;
  }
  reverse(ALL(ans));
  cout << ans << endl;

  return 0;
}

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a), end(a)

int main(void) {
  ll N;
  cin >> N;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }

  vector<ll> base(35);
  REP(i, 35) {
    if (i == 0)
      base[i] = 1;
    else {
      base[i] = base[i - 1] * -2;
    }
  }

  string ans;
  int i = 0;
  while (N) {
    if (N % (2 * abs(base[i]))) {
      ans.pb('1');
      N -= base[i];
    } else {
      ans.pb('0');
    }
    i++;
  }
  reverse(ALL(ans));
  cout << ans << endl;

  return 0;
}
Problem p03286 (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 main() {
  int n;
  cin >> n;

  string s = "";
  while (n) {
    if (n % -2) {
      --n;
      s = "1" + s;
    } else {
      s = "0" + s;
    }
    n /= -2;
  }
  cout << (s == "" ? 0 : s) << endl;
}


Fixed submission:
#include <bits/stdc++.h>

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

using namespace std;

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

  string s = "";
  while (n) {
    if (n % -2) {
      --n;
      s = "1" + s;
    } else {
      s = "0" + s;
    }
    n /= -2;
  }
  if (s == "")
    s = "0";
  cout << s << endl;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

string basem2(int n) {
  if (n == 0) {
    return 0;
  } else {
    string ans = "";
    while (n != 0) {
      int k = abs(n % 2);
      char c = k + '0';
      ans = c + ans;
      if (n < 0) {
        n -= 1;
      }
      n /= -2;
    }
    return ans;
  }
}

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

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

string basem2(int n) {
  if (n == 0) {
    return "0";
  } else {
    string ans = "";
    while (n != 0) {
      int k = abs(n % 2);
      char c = k + '0';
      ans = c + ans;
      if (n < 0) {
        n -= 1;
      }
      n /= -2;
    }
    return ans;
  }
}

int main() {
  int n;
  cin >> n;
  cout << basem2(n);
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  long long int n;
  cin >> n;
  vector<long long int> v;
  long long int t = 0;
  long long int start = 0;
  while (1) {
    if (t == n) {
      break;
    }
    if (start == 0) {
      long long int mod = n % 2;
      if (mod == 0) {
        v.push_back(0);
      } else {
        v.push_back(1);
        t += 1;
      }
    } else {
      long long int mod = n % (int)(pow(2, start + 1));
      if (mod == pow(-2, start) + t || mod == abs((int)pow(-2, start)) + t) {
        v.push_back(1);
        t += pow(-2, start);
      } else {
        v.push_back(0);
      }
    }
    start++;
  }
  reverse(v.begin(), v.end());
  bool check = false;
  if (n == 0) {
    cout << 0;
  } else {
    for (int i = 0; i < v.size(); i++) {
      if (v[i] == 1) {
        check = true;
      }
      if (check) {
        cout << v[i];
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  long long int n;
  cin >> n;
  vector<long long int> v;
  long long int t = 0;
  long long int start = 0;
  while (1) {
    if (t == n) {
      break;
    }
    if (start == 0) {
      long long int mod = n % 2;
      if (mod == 0) {
        v.push_back(0);
      } else {
        v.push_back(1);
        t += 1;
      }
    } else {
      long long int mod = n % (int)(pow(2, start + 1));
      if (mod == pow(-2, start) + t || mod == -1 * pow(-2, start) + t) {
        v.push_back(1);
        t += pow(-2, start);
      } else {
        v.push_back(0);
      }
    }
    start++;
  }
  reverse(v.begin(), v.end());
  bool check = false;
  if (n == 0) {
    cout << 0;
  } else {
    for (int i = 0; i < v.size(); i++) {
      if (v[i] == 1) {
        check = true;
      }
      if (check) {
        cout << v[i];
      }
    }
  }
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const constexpr int INF = 1e9;

long long N, M;

string S, T;

bool check(long long n, long long div) {
  bool ok = false;
  div = abs(div);
  n = abs(n);
  for (int i = 0; i < 32; ++i) {
    if (n % div == 0)
      ok = true;
    div *= 2;
  }
  return ok;
}
void solve() {
  vector<int> v;
  if (N == 0) {
    cout << 0 << endl;
    return;
  }
  if (N % 2 != 0) {
    v.push_back(1);
    N--;
  } else {
    v.push_back(0);
  }
  int base = -2;
  while (abs(N) > 0) {
    if (check(N + base, base * (-2))) { // trueなら
      v.push_back(1);
      N -= base;
    } else {
      v.push_back(0);
    }
    base *= (-2);
  }
  reverse(v.begin(), v.end());
  for (auto e : v)
    cout << e;
  cout << endl;
}
int main() {
  cin >> N;
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const constexpr int INF = 1e9;

long long N, M;

string S, T;

bool check(long long n, long long div) {
  bool ok = false;
  div = abs(div);
  n = abs(n);
  for (int i = 0; i < 32; ++i) {
    if (n % div == 0)
      ok = true;
    div *= 2;
  }
  return ok;
}
void solve() {
  vector<int> v;
  if (N == 0) {
    cout << 0 << endl;
    return;
  }
  if (N % 2 != 0) {
    v.push_back(1);
    N--;
  } else {
    v.push_back(0);
  }
  long long base = -2;
  while (abs(N) > 0) {
    if (check(N + base, base * (-2))) { // trueなら
      v.push_back(1);
      N -= base;
    } else {
      v.push_back(0);
    }
    base *= (-2);
  }
  reverse(v.begin(), v.end());
  for (auto e : v)
    cout << e;
  cout << endl;
}
int main() {
  cin >> N;
  solve();
  return 0;
}
Problem p03286 (C++)

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

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

const int INF = 1e9;
const int mod = 1e9 + 7;

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

  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }

  int z = 1;
  string ans;
  while (n != 0) {
    if (n % (2 * abs(z))) {
      ans.push_back('1');
      n -= z;
    } else {
      ans.push_back('0');
    }
    z *= -2;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}

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

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

const int INF = 1e9;
const int mod = 1e9 + 7;

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

  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }

  ll z = 1;
  string ans;
  while (n != 0) {
    if (n % (2 * abs(z))) {
      ans.push_back('1');
      n -= z;
    } else {
      ans.push_back('0');
    }
    z *= -2;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size)
#define pb push_back
#define optimize_cin()                                                         \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
using namespace std;
using lint = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int countDigit(int n) {
  int digit = 1;
  while ((n /= 10) != 0)
    digit++;
  return digit;
}

int main() {
  int a = -2; // 何進数かをここで設定できる
  int n;
  cin >> n;
  string ans;

  while (true) {
    if (n % a == 0) {
      ans.insert(0, "0");
      n /= a;
      continue;
    } else {
      ans.insert(0, "1");
      if (n == 1)
        break;
      n = (n - 1) / a;
    }
  }

  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size)
#define pb push_back
#define optimize_cin()                                                         \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
using namespace std;
using lint = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int countDigit(int n) {
  int digit = 1;
  while ((n /= 10) != 0)
    digit++;
  return digit;
}

int main() {
  int a = -2; // 何進数かをここで設定できる
  int n;
  cin >> n;
  string ans;

  while (true) {
    if (n % a == 0) {
      ans.insert(0, "0");
      if (n == 0)
        break;
      n /= a;
      continue;
    } else {
      ans.insert(0, "1");
      if (n == 1)
        break;
      n = (n - 1) / a;
    }
  }

  cout << ans;
  return 0;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1000000007
#define INF INT32_MAX / 2
#define PIE 3.14159265358979323

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

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

signed main() {
  int n;
  cin >> n;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  int i = 1;
  string s;
  while (n) {
    if (abs(n) % abs(i * (-2)) != 0) {
      n -= i;
      s.push_back('1');
    } else
      s.push_back('0');
    i *= -2;
  }
  if (s.back() == '0')
    s.pop_back();
  reverse(s.begin(), s.end());
  cout << s << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1000000007
#define INF INT32_MAX / 2
#define PIE 3.14159265358979323

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

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

signed main() {
  int n;
  cin >> n;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  int i = 1;
  string s;
  while (n) {
    if (abs(n) % abs(i * (-2)) != 0) {
      n -= i;
      s.push_back('1');
    } else
      s.push_back('0');
    i *= -2;
  }
  if (s.back() == '0')
    s.pop_back();
  reverse(s.begin(), s.end());
  cout << s << endl;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include "iostream"
#include "string"
using namespace std;

int main() {
  int n;
  cin >> n;
  string s = "";
  int a = 1;
  int PM = 1;
  if (n == 0) {
    cout << 0 << endl;
  } else {
    while (n != 0) {
      if (n % (2 * a) == 0) {
        s = "0" + s;
      } else {
        s = "1" + s;
        n = n - PM * a;
      }
      a = 2 * a;
      PM = (-1) * PM;
    }
    cout << s << endl;
  }
}


Fixed submission:
#include "iostream"
#include "string"
using namespace std;

int main() {
  int n;
  cin >> n;
  string s = "";
  long a = 1;
  int PM = 1;
  if (n == 0) {
    cout << 0 << endl;
  } else {
    while (n != 0) {
      if (n % (2 * a) == 0) {
        s = "0" + s;
      } else {
        s = "1" + s;
        n = n - PM * a;
      }
      a = 2 * a;
      PM = (-1) * PM;
    }
    cout << s << endl;
  }
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

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

  ll n;
  cin >> n;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }

  string ans = "";
  int base = 1;
  int i = 0;
  while (n != 0) {
    if (n % (2 * base) == 0) {
      ans = '0' + ans;
    } else {
      ans = '1' + ans;
      n -= base;
    }
    i += 1;
    base *= -2;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

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

  ll n;
  cin >> n;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }

  string ans = "";
  ll base = 1;
  ll i = 0;
  while (n != 0) {
    if (n % (2 * base) == 0) {
      ans = '0' + ans;
    } else {
      ans = '1' + ans;
      n -= base;
    }
    i += 1;
    base *= -2;
  }
  cout << ans << endl;
}
Problem p03286 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  long long int N, sw = -1;
  string a = "";
  cin >> N;
  if (N == 0) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 2; N != 0; i *= 2) {
    sw *= -1;

    if (N % i != 0) {
      a = "1" + a;
      N -= (i / 2) * sw;
    } else {
      a = "0" + a;
    }
  }

  cout << a << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
  long long int N, sw = -1;
  string a = "";
  cin >> N;
  if (N == 0) {
    cout << "0" << endl;
    return 0;
  }
  for (long long int i = 2; N != 0; i *= 2) {
    sw *= -1;

    if (N % i != 0) {
      a = "1" + a;
      N -= (i / 2) * sw;
    } else {
      a = "0" + a;
    }
  }

  cout << a << endl;
}