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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int N;

int pcnt(int x) { return __builtin_popcount(x); }

int f(int a) {
  if (a == 0)
    return 0;
  return f(a % pcnt(a)) + 1;
}

int main() {
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  rep(i, N) X[i] = s[i] - '0';
  int opc = 0;
  rep(i, N) opc += X[i];

  vector<int> ans(N);
  // b=0は0→1の反転をする場合
  rep(b, 2) {
    int pc = opc;
    if (b == 0)
      pc++;
    else
      pc--;

    // x(mod pc)を求める
    int y = 0;
    for (int i = 0; i < N; i++) {
      y = (2 * y) % pc;
      y += X[i];
    }

    // ビット反転させていく
    int bit = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (b == X[i]) {
        int z = y;
        if (b == 0) {
          z = (z + bit) % pc;
        } else {
          z = (z - bit + pc) % pc;
        }
        ans[i] = f(z) + 1;
      }
      bit *= 2;
      bit %= pc;
    }
  }

  rep(i, N) cout << ans[i] << endl;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int N;

int pcnt(int x) { return __builtin_popcount(x); }

int f(int a) {
  if (a == 0)
    return 0;
  return f(a % pcnt(a)) + 1;
}

int main() {
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  rep(i, N) X[i] = s[i] - '0';
  int opc = 0;
  rep(i, N) opc += X[i];

  vector<int> ans(N);
  // b=0は0→1の反転をする場合
  rep(b, 2) {
    int pc = opc;
    if (b == 0)
      pc++;
    else
      pc--;
    if (pc <= 0)
      continue;

    // x(mod pc)を求める
    int y = 0;
    for (int i = 0; i < N; i++) {
      y = (2 * y) % pc;
      y += X[i];
    }

    // ビット反転させていく
    int bit = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (b == X[i]) {
        int z = y;
        if (b == 0) {
          z = (z + bit) % pc;
        } else {
          z = (z - bit + pc) % pc;
        }
        ans[i] = f(z) + 1;
      }
      bit *= 2;
      bit %= pc;
    }
  }

  rep(i, N) cout << ans[i] << endl;
}
Problem p02609 (C++)

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

using namespace std;

int pw[MAX][3];
int sum[3];
int n;
string x;

void calc(int cnt, int idx) {
  if (cnt <= 0)
    return;
  pw[0][idx] = 1;
  for (int i = 1; i < MAX; i++) {
    pw[i][idx] = 2 * pw[i - 1][idx];
    pw[i][idx] %= cnt;
  }
  for (int i = 0; i < x.length(); i++) {
    if (x[i] == '1') {
      sum[idx] += pw[n - i - 1][idx];
      sum[idx] %= cnt;
    }
  }
}

int f(int num) {
  // std::popc
  if (num == 0)
    return 0;
  int p = __builtin_popcount(num);
  return 1 + f(num % p);
}

int main() {

  int cnt = 0;

  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cnt++;
  }
  calc(cnt - 1, 0);
  calc(cnt, 1);
  calc(cnt + 1, 2);
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int rem = sum[2];
      rem += pw[(n - i - 1)][2];
      rem %= (cnt + 1);
      // cout << rem << endl;
      cout << (1 + f(rem)) << endl;
    } else {
      if (cnt == 1)
        cout << 0 << endl;
      else {
        int rem = sum[0];
        rem -= pw[(n - i - 1)][0];
        rem %= (cnt - 1);
        if (rem < 0)
          rem += (cnt - 1);
        cout << (1 + f(rem)) << endl;
      }
    }
  }
  return 0;
}

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

using namespace std;

int pw[MAX][3];
int sum[3];
int n;
string x;

void calc(int cnt, int idx) {
  if (cnt <= 0)
    return;
  pw[0][idx] = 1;
  for (int i = 1; i < MAX; i++) {
    pw[i][idx] = 2 * pw[i - 1][idx];
    pw[i][idx] %= cnt;
  }
  for (int i = 0; i < x.length(); i++) {
    if (x[i] == '1') {
      sum[idx] += pw[n - i - 1][idx];
      sum[idx] %= cnt;
    }
  }
}

int f(int num) {
  // std::popc
  if (num == 0)
    return 0;
  int p = __builtin_popcount(num);
  return 1 + f(num % p);
}

int main() {

  int cnt = 0;

  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cnt++;
  }
  calc(cnt - 1, 0);
  calc(cnt, 1);
  calc(cnt + 1, 2);
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int rem = sum[2];
      rem += pw[(n - i - 1)][2];
      rem %= (cnt + 1);
      // cout << rem << endl;
      cout << (1 + f(rem)) << endl;
    } else {
      if (cnt == 1)
        cout << 0 << endl;
      else {
        int rem = sum[0];
        rem -= pw[(n - i - 1)][0];
        rem %= (cnt - 1);
        if (rem < 0)
          rem += (cnt - 1);
        cout << (1 + f(rem)) << endl;
      }
    }
  }
  return 0;
}
Problem p02609 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> dp(n + 1);
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 0; j < 20; j++)
      if (i & (1 << j))
        cnt++;
    dp[i] = dp[i % cnt] + 1;
  }
  int modulo = 0;
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    x[i] = c - '0';
    if (x[i])
      modulo++;
  }
  reverse(x.begin(), x.end());
  int modulo1 = modulo - 1;
  int modulo2 = modulo + 1;
  if (modulo1 < 0)
    modulo1 = 1;
  vector<int> p1(n);
  vector<int> p2(n);
  p1[0] = 1 % modulo1;
  p2[0] = 1 % modulo2;
  for (int i = 1; i < n; i++) {
    p1[i] = p1[i - 1] * 2 % modulo1;
    p2[i] = p2[i - 1] * 2 % modulo2;
  }
  int sum1 = 0;
  int sum2 = 0;
  for (int i = 0; i < n; i++) {
    sum1 = (sum1 + p1[i] * x[i]) % modulo1;
    sum2 = (sum2 + p2[i] * x[i]) % modulo2;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (x[i] == 0) {
      int tmp = sum2;
      tmp = (tmp + p2[i]) % modulo2;
      ans[i] = 1 + dp[tmp];
      continue;
    }
    if (modulo == 1) {
      ans[i] = 0;
      continue;
    }
    int tmp = sum1;
    tmp = (tmp - p1[i] + modulo1) % modulo1;
    ans[i] = 1 + dp[tmp];
  }
  reverse(ans.begin(), ans.end());
  for (auto &now : ans)
    cout << now << '\n';
  return 0;
}

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> dp(n + 1);
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 0; j < 20; j++)
      if (i & (1 << j))
        cnt++;
    dp[i] = dp[i % cnt] + 1;
  }
  int modulo = 0;
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    x[i] = c - '0';
    if (x[i])
      modulo++;
  }
  reverse(x.begin(), x.end());
  int modulo1 = modulo - 1;
  int modulo2 = modulo + 1;
  if (modulo1 <= 0)
    modulo1 = 1;
  vector<int> p1(n);
  vector<int> p2(n);
  p1[0] = 1 % modulo1;
  p2[0] = 1 % modulo2;
  for (int i = 1; i < n; i++) {
    p1[i] = p1[i - 1] * 2 % modulo1;
    p2[i] = p2[i - 1] * 2 % modulo2;
  }
  int sum1 = 0;
  int sum2 = 0;
  for (int i = 0; i < n; i++) {
    sum1 = (sum1 + p1[i] * x[i]) % modulo1;
    sum2 = (sum2 + p2[i] * x[i]) % modulo2;
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (x[i] == 0) {
      int tmp = sum2;
      tmp = (tmp + p2[i]) % modulo2;
      ans[i] = 1 + dp[tmp];
      continue;
    }
    if (modulo == 1) {
      ans[i] = 0;
      continue;
    }
    int tmp = sum1;
    tmp = (tmp - p1[i] + modulo1) % modulo1;
    ans[i] = 1 + dp[tmp];
  }
  reverse(ans.begin(), ans.end());
  for (auto &now : ans)
    cout << now << '\n';
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):

// D - Anything Goes to Zero

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

ll rem[2][200000];
ll rem_sum[2];

// aのn乗をmodで割った余りを計算
ll mod_pow(ll a, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1)
      result = result * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return result;
}

int popcount(int n) {
  int result = 0;
  while (n) {
    if (n % 2 == 1)
      result++;
    n /= 2;
  }
  return result;
}

ll f(int n, int k) {
  if (n == 0)
    return k;

  return f(n % popcount(n), k + 1);
}

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

  int pop_cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      pop_cnt++;
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      rem_sum[0] += mod_pow(2, N - i - 1, pop_cnt + 1);
      rem_sum[1] += mod_pow(2, N - i - 1, pop_cnt - 1);
    }
  }

  vector<ll> ans_list;
  ans_list.reserve(N);

  for (int i = 0; i < N; i++) {
    ll r;

    if (X[i] == '0') {
      r = (rem_sum[0] + mod_pow(2, N - i - 1, pop_cnt + 1)) % (pop_cnt + 1);
    } else {
      if (pop_cnt == 1) {
        ans_list.push_back(0);
        continue;
      }

      r = (rem_sum[1] - mod_pow(2, N - i - 1, pop_cnt - 1)) % (pop_cnt - 1);
    }

    ans_list.push_back(f(r, 1));
  }

  for (ll ans : ans_list) {
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:

// D - Anything Goes to Zero

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

ll rem[2][200000];
ll rem_sum[2];

// aのn乗をmodで割った余りを計算
ll mod_pow(ll a, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1)
      result = result * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return result;
}

int popcount(int n) {
  int result = 0;
  while (n) {
    if (n % 2 == 1)
      result++;
    n /= 2;
  }
  return result;
}

ll f(int n, int k) {
  if (n == 0)
    return k;

  return f(n % popcount(n), k + 1);
}

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

  int pop_cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      pop_cnt++;
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      rem_sum[0] += mod_pow(2, N - i - 1, pop_cnt + 1);
      if (pop_cnt != 1)
        rem_sum[1] += mod_pow(2, N - i - 1, pop_cnt - 1);
    }
  }

  vector<ll> ans_list;
  ans_list.reserve(N);

  for (int i = 0; i < N; i++) {
    ll r;

    if (X[i] == '0') {
      r = (rem_sum[0] + mod_pow(2, N - i - 1, pop_cnt + 1)) % (pop_cnt + 1);
    } else {
      if (pop_cnt == 1) {
        ans_list.push_back(0);
        continue;
      }

      r = (rem_sum[1] - mod_pow(2, N - i - 1, pop_cnt - 1)) % (pop_cnt - 1);
    }

    ans_list.push_back(f(r, 1));
  }

  for (ll ans : ans_list) {
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++)

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

#define ll long long
ll mod = pow(10, 9) + 7;

ll pw(ll n, ll mod) {
  ll ans = 1;

  ll x = 2, y = n;
  while (y != 0) {
    if (y % 2)
      ans = ((ans % mod) * (x % mod)) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    y /= 2;
  }
  return ans;
}

ll cunt(ll n) {
  ll cnt = 0;
  while (n != 0) {
    if (n % 2)
      cnt++;
    n /= 2;
  }
  return cnt;
}

void func(ll n) {
  ll cnt = 1;
  while (n != 0) {
    ll temp = cunt(n);
    cnt++;
    n = n % temp;
  }
  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll mn = 0, mx = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      mn++;
  }
  if (mn < n) {
    mx = mn + 1;
    mn--;
  } else {
    mn--;
    mx = mn;
  }

  ll chota = 0, bada = 0;

  reverse(s.begin(), s.end());
  ll temp1 = 1, temp2 = 1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      chota = ((chota % mn) + (temp1 % mn)) % mn;
      bada = ((bada % mx) + (temp2 % mx)) % mx;
    }
    temp1 = ((temp1 % mn) * 2) % mn;
    temp2 = ((temp2 % mx) * 2) % mx;
  }
  vector<ll> ans;
  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll temp = chota - pw(i, mn);
      temp += mn;
      temp = temp % mn;
      func(temp);
    } else {
      ll temp = bada + pw(i, mx);
      temp = temp % mx;
      func(temp);
    }
  }
}


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

#define ll long long
ll mod = pow(10, 9) + 7;

ll pw(ll n, ll mod) {
  ll ans = 1;

  ll x = 2, y = n;
  while (y != 0) {
    if (y % 2)
      ans = ((ans % mod) * (x % mod)) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    y /= 2;
  }
  return ans;
}

ll cunt(ll n) {
  ll cnt = 0;
  while (n != 0) {
    if (n % 2)
      cnt++;
    n /= 2;
  }
  return cnt;
}

void func(ll n) {
  ll cnt = 1;
  while (n != 0) {
    ll temp = cunt(n);
    cnt++;
    n = n % temp;
  }
  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll mn = 0, mx = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      mn++;
  }
  if (mn < n) {
    mx = mn + 1;
    mn--;
  } else {
    mn--;
    mx = mn;
  }

  if (mn == 0 || mx == 0) {
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        if (i == n - 1)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
    return 0;
  }

  ll chota = 0, bada = 0;

  reverse(s.begin(), s.end());
  ll temp1 = 1, temp2 = 1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      chota = ((chota % mn) + (temp1 % mn)) % mn;
      bada = ((bada % mx) + (temp2 % mx)) % mx;
    }
    temp1 = ((temp1 % mn) * 2) % mn;
    temp2 = ((temp2 % mx) * 2) % mx;
  }
  vector<ll> ans;
  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll temp = chota - pw(i, mn);
      temp += mn;
      temp = temp % mn;
      func(temp);
    } else {
      ll temp = bada + pw(i, mx);
      temp = temp % mx;
      func(temp);
    }
  }
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
using llong = long long;
#define inv(X) ((X) == '1' ? '0' : '1')

llong n;
string x;

llong bitcnt(llong a) {
  llong ret = 0;
  while (a > 0) {
    if (a & 1)
      ret++;
    a >>= 1;
  }
  return ret;
}

llong f(llong x) {
  llong cnt = 0;
  while (bitcnt(x) > 0) {
    x %= bitcnt(x);
    cnt++;
  }
  return cnt;
}

llong to_int(string &s, llong mod) {
  llong ret = 0;
  for (auto ch : s) {
    ret *= 2;
    if (ch == '1')
      ret |= 1;
    ret %= mod;
  }
  return ret;
}

llong pow_mod(llong n, llong m, llong mod) {
  llong ret = 1;
  while (m > 0) {
    if (m & 1) {
      ret *= n;
      ret %= mod;
    }
    n *= n;
    n %= mod;
    m >>= 1;
  }
  return ret;
}

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

  llong cnt = count(x.begin(), x.end(), '1');
  llong x1 = to_int(x, cnt + 1);
  llong x2 = to_int(x, cnt - 1);

  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      llong s = x1 + pow_mod(2, n - i - 1, cnt + 1);
      s %= cnt + 1;
      cout << f(s) + 1 << '\n';
    } else {
      if (cnt - 1 == 0) {
        cout << 0 << '\n';
        continue;
      }
      llong s = x2 - pow_mod(2, n - i - 1, cnt - 1);
      s %= cnt - 1;
      s += cnt - 1;
      s %= cnt - 1;
      cout << f(s) + 1 << '\n';
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
using llong = long long;
#define inv(X) ((X) == '1' ? '0' : '1')

llong n;
string x;

llong bitcnt(llong a) {
  llong ret = 0;
  while (a > 0) {
    if (a & 1)
      ret++;
    a >>= 1;
  }
  return ret;
}

llong f(llong x) {
  llong cnt = 0;
  while (bitcnt(x) > 0) {
    x %= bitcnt(x);
    cnt++;
  }
  return cnt;
}

llong to_int(string &s, llong mod) {
  llong ret = 0;
  for (auto ch : s) {
    ret *= 2;
    if (ch == '1')
      ret |= 1;
    ret %= mod;
  }
  return ret;
}

llong pow_mod(llong n, llong m, llong mod) {
  llong ret = 1;
  while (m > 0) {
    if (m & 1) {
      ret *= n;
      ret %= mod;
    }
    n *= n;
    n %= mod;
    m >>= 1;
  }
  return ret;
}

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

  llong cnt = count(x.begin(), x.end(), '1');
  llong x1 = to_int(x, cnt + 1);
  llong x2;
  if (cnt > 1)
    x2 = to_int(x, cnt - 1);

  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      llong s = x1 + pow_mod(2, n - i - 1, cnt + 1);
      s %= cnt + 1;
      cout << f(s) + 1 << '\n';
    } else {
      if (cnt - 1 == 0) {
        cout << 0 << '\n';
        continue;
      }
      llong s = x2 - pow_mod(2, n - i - 1, cnt - 1);
      s %= cnt - 1;
      s += cnt - 1;
      s %= cnt - 1;
      cout << f(s) + 1 << '\n';
    }
  }
}
Problem p02609 (C++)

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

#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 RepG(i, u) for (int i = head[u]; ~i; i = e[i].next)

typedef long long ll;

const int N = 2e5 + 5;

template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  x *= f;
}

int n;
int a[N], cnt;
int sum0, sum1;

int Qpow(int base, int ind, int p) {
  int res = 1;
  while (ind) {
    if (ind & 1)
      res = 1ll * res * base % p;
    base = 1ll * base * base % p;
    ind >>= 1;
  }
  return res;
}

int solve(int x) {
  if (!x)
    return 1;
  int t = x, count = 0;
  while (t) {
    if (t & 1)
      count++;
    t /= 2;
  }
  return solve(x % count) + 1;
}

int main() {
  read(n);
  Rep(i, 1, n) scanf("%1d", &a[i]), cnt += a[i];
  Rep(i, 1, n) {
    sum0 += a[i] * Qpow(2, n - i, cnt + 1), sum0 %= (cnt + 1);
    sum1 += a[i] * Qpow(2, n - i, cnt - 1), sum1 %= (cnt - 1);
  }
  Rep(i, 1, n) if (a[i]) printf(
      "%d\n", solve((sum1 - Qpow(2, n - i, cnt - 1) + (cnt - 1)) % (cnt - 1)));
  else printf("%d\n",
              solve((sum0 + Qpow(2, n - i, cnt + 1) + cnt + 1) % (cnt + 1)));
  return 0;
}

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

#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 RepG(i, u) for (int i = head[u]; ~i; i = e[i].next)

typedef long long ll;

const int N = 2e5 + 5;

template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  x *= f;
}

int n;
int a[N], cnt;
int sum0, sum1;

int Qpow(int base, int ind, int p) {
  int res = 1;
  while (ind) {
    if (ind & 1)
      res = 1ll * res * base % p;
    base = 1ll * base * base % p;
    ind >>= 1;
  }
  return res;
}

int solve(int x) {
  if (!x)
    return 1;
  int t = x, count = 0;
  while (t) {
    if (t & 1)
      count++;
    t /= 2;
  }
  return solve(x % count) + 1;
}

int main() {
  read(n);
  Rep(i, 1, n) scanf("%1d", &a[i]), cnt += a[i];
  if (cnt == 1) {
    Rep(i, 1, n) sum0 += a[i] * Qpow(2, n - i, cnt + 1), sum0 %= (cnt + 1);
    Rep(i, 1, n) if (!a[i]) printf(
        "%d\n", solve((sum0 + Qpow(2, n - i, cnt + 1) + cnt + 1) % (cnt + 1)));
    else puts("0");
    return 0;
  }
  Rep(i, 1, n) {
    sum0 += a[i] * Qpow(2, n - i, cnt + 1), sum0 %= (cnt + 1);
    sum1 += a[i] * Qpow(2, n - i, cnt - 1), sum1 %= (cnt - 1);
  }
  Rep(i, 1, n) if (a[i]) printf(
      "%d\n", solve((sum1 - Qpow(2, n - i, cnt - 1) + (cnt - 1)) % (cnt - 1)));
  else printf("%d\n",
              solve((sum0 + Qpow(2, n - i, cnt + 1) + cnt + 1) % (cnt + 1)));
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define F(i, a, b) for (int i = a; i <= b; ++i)
const int maxn = 2e5 + 5;
int n, a[maxn], pw1[maxn], pw2[maxn], f[maxn], sum1 = 0, sum2 = 0, tot = 0;
char s[maxn];
int pct(int x) { return x == 0 ? x : pct(x >> 1) + (x & 1); }
void run(int p) {
  a[p] ^= 1;
  int x = 0, pc = 0;
  if (a[p])
    pc = tot + 1;
  else
    pc = tot - 1;
  if (!pc) {
    a[p] ^= 1;
    f[p] = 0;
    return;
  }
  if (pc == tot - 1)
    x = (sum1 + tot - 1 - pw1[p]) % (tot - 1);
  else
    x = (sum2 + pw2[p]) % (tot + 1);
  ++f[p];
  while (x) {
    pc = pct(x);
    x %= pc;
    ++f[p];
  }
  a[p] ^= 1;
}
int main() {
  // freopen("data.in","r",stdin);
  // freopen("data.out","w",stdout);
  scanf("%d", &n);
  scanf("%s", s + 1);
  F(i, 1, n)
  if (s[i] == '1')
    a[i] = 1, ++tot;
  LL all = 0;
  for (int i = n; i; --i)
    all += a[i] * (1LL << (n - i));
  int tmp = 1;
  if (tot) {
    F(i, 1, n) sum1 = (2 * sum1 + a[i]) % (tot - 1);
    for (int i = n; i; --i)
      pw1[i] = tmp, tmp = tmp * 2 % (tot - 1);
  }
  if (tot < n) {
    tmp = 1;
    F(i, 1, n)
    sum2 = (2 * sum2 + a[i]) % (tot + 1);
    for (int i = n; i; --i)
      pw2[i] = tmp, tmp = tmp * 2 % (tot + 1);
  }
  F(i, 1, n) run(i);
  F(i, 1, n) printf("%d\n", f[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define F(i, a, b) for (int i = a; i <= b; ++i)
const int maxn = 2e5 + 5;
int n, a[maxn], pw1[maxn], pw2[maxn], f[maxn], sum1 = 0, sum2 = 0, tot = 0;
char s[maxn];
int pct(int x) { return x == 0 ? x : pct(x >> 1) + (x & 1); }
void run(int p) {
  a[p] ^= 1;
  int x = 0, pc = 0;
  if (a[p])
    pc = tot + 1;
  else
    pc = tot - 1;
  if (!pc) {
    a[p] ^= 1;
    f[p] = 0;
    return;
  }
  if (pc == tot - 1)
    x = (sum1 + tot - 1 - pw1[p]) % (tot - 1);
  else
    x = (sum2 + pw2[p]) % (tot + 1);
  ++f[p];
  while (x) {
    pc = pct(x);
    x %= pc;
    ++f[p];
  }
  a[p] ^= 1;
}
int main() {
  // freopen("data.in","r",stdin);
  // freopen("data.out","w",stdout);
  scanf("%d", &n);
  scanf("%s", s + 1);
  F(i, 1, n)
  if (s[i] == '1')
    a[i] = 1, ++tot;
  LL all = 0;
  for (int i = n; i; --i)
    all += a[i] * (1LL << (n - i));
  int tmp = 1;
  if (tot > 1) {
    F(i, 1, n) sum1 = (2 * sum1 + a[i]) % (tot - 1);
    for (int i = n; i; --i)
      pw1[i] = tmp, tmp = tmp * 2 % (tot - 1);
  }
  if (tot < n) {
    tmp = 1;
    F(i, 1, n)
    sum2 = (2 * sum2 + a[i]) % (tot + 1);
    for (int i = n; i; --i)
      pw2[i] = tmp, tmp = tmp * 2 % (tot + 1);
  }
  F(i, 1, n) run(i);
  F(i, 1, n) printf("%d\n", f[i]);
  return 0;
}
Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const ll MOD = 1e9 + 7;
const int INF = 2e9;
const double PI = acos(-1);

const int MAX = 200010;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  vector<ll> b1(n), b2(n);
  b1[0] = 1, b2[0] = 1;
  ll s1 = 0, s2 = 0;
  if (cnt - 1 > 0) {
    rep(i, n - 1) b1[i + 1] = (2 * b1[i]) % (cnt - 1);
    rep(i, n) {
      if (s[i] == '1')
        s1 = (s1 + b1[i]) % (cnt - 1);
    }
  }
  rep(i, n - 1) b2[i + 1] = (2 * b2[i]) % (cnt + 1);
  rep(i, n) {
    if (s[i] == '1')
      s2 = (s2 + b2[i]) % (cnt + 1);
  }
  vector<int> p(60);
  repr(i, 1, 60) {
    int j = i, m = 0;
    while (j > 0) {
      if (j % 2 == 1)
        ++m;
      j /= 2;
    }
    p[i] = p[i % m] + 1;
  }
  invrep(i, n) {
    int ans = 0;
    if (s[i] == '1') {
      if (cnt > 1) {
        ll tmp = (s1 + (cnt - 1) - b1[i]) % (cnt - 1);
        ans = p[tmp] + 1;
      }
    } else {
      ll tmp = (s2 + b2[i]) % (cnt + 1);
      ans = p[tmp] + 1;
    }
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const ll MOD = 1e9 + 7;
const int INF = 2e9;
const double PI = acos(-1);

const int MAX = 200010;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  string s;
  cin >> n >> s;
  reverse(s.begin(), s.end());
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  vector<ll> b1(n), b2(n);
  b1[0] = 1, b2[0] = 1;
  ll s1 = 0, s2 = 0;
  if (cnt - 1 > 0) {
    rep(i, n - 1) b1[i + 1] = (2 * b1[i]) % (cnt - 1);
    rep(i, n) {
      if (s[i] == '1')
        s1 = (s1 + b1[i]) % (cnt - 1);
    }
  }
  rep(i, n - 1) b2[i + 1] = (2 * b2[i]) % (cnt + 1);
  rep(i, n) {
    if (s[i] == '1')
      s2 = (s2 + b2[i]) % (cnt + 1);
  }
  vector<int> p(200010);
  repr(i, 1, 200010) {
    int j = i, m = 0;
    while (j > 0) {
      if (j % 2 == 1)
        ++m;
      j /= 2;
    }
    p[i] = p[i % m] + 1;
  }
  invrep(i, n) {
    int ans = 0;
    if (s[i] == '1') {
      if (cnt > 1) {
        ll tmp = (s1 + (cnt - 1) - b1[i]) % (cnt - 1);
        ans = p[tmp] + 1;
      }
    } else {
      ll tmp = (s2 + b2[i]) % (cnt + 1);
      ans = p[tmp] + 1;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++)

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, pwm1[200020], pw[200020], pwp1[200020], num = 0, resm1 = 0, res = 0,
                                              resp1 = 0;
string s;

int get(LL x) {
  int ret = 0, y;
  while (x > 0) {
    y = __builtin_popcount(x);
    x %= y;
    ret++;
  }
  return ret;
}

int main() {
  cin >> n >> s;
  rep(i, n) num += s[i] - '0';
  pwm1[0] = pw[0] = pwp1[0] = 1;
  repn(i, 200010) {
    if (num - 1 > 0)
      pwm1[i] = pwm1[i - 1] * 2 % (num - 1);
    if (num > 0)
      pw[i] = pw[i - 1] * 2 % num;
    pwp1[i] = pwp1[i - 1] * 2 % (num + 1);
  }
  rep(i, n) {
    if (num - 1 > 0)
      resm1 = (resm1 + (s[i] == '1') * pwm1[n - i - 1]) % (num - 1);
    if (num > 0)
      res = (res + (s[i] == '1') * pw[n - i - 1]) % num;
    resp1 = (resp1 + (s[i] == '1') * pwp1[n - i - 1]) % (num + 1);
  }
  rep(i, n) {
    if (s[i] == '0') {
      LL tmp = (resp1 + pwp1[n - i - 1]) % (num + 1);
      printf("%d\n", get(tmp) + 1);
    } else {
      LL tmp = (resm1 - pwm1[n - i - 1] + num - 1 + num - 1) % (num - 1);
      printf("%d\n", get(tmp) + 1);
    }
  }
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, pwm1[200020], pw[200020], pwp1[200020], num = 0, resm1 = 0, res = 0,
                                              resp1 = 0;
string s;

int get(LL x) {
  int ret = 0, y;
  while (x > 0) {
    y = __builtin_popcount(x);
    x %= y;
    ret++;
  }
  return ret;
}

int main() {
  cin >> n >> s;
  rep(i, n) num += s[i] - '0';
  pwm1[0] = pw[0] = pwp1[0] = 1;
  repn(i, 200010) {
    if (num - 1 > 0)
      pwm1[i] = pwm1[i - 1] * 2 % (num - 1);
    if (num > 0)
      pw[i] = pw[i - 1] * 2 % num;
    pwp1[i] = pwp1[i - 1] * 2 % (num + 1);
  }
  rep(i, n) {
    if (num - 1 > 0)
      resm1 = (resm1 + (s[i] == '1') * pwm1[n - i - 1]) % (num - 1);
    if (num > 0)
      res = (res + (s[i] == '1') * pw[n - i - 1]) % num;
    resp1 = (resp1 + (s[i] == '1') * pwp1[n - i - 1]) % (num + 1);
  }
  rep(i, n) {
    if (s[i] == '0') {
      LL tmp = (resp1 + pwp1[n - i - 1]) % (num + 1);
      printf("%d\n", get(tmp) + 1);
    } else {
      if (num == 1) {
        puts("0");
        continue;
      }
      LL tmp = (resm1 - pwm1[n - i - 1] + num - 1 + num - 1) % (num - 1);
      printf("%d\n", get(tmp) + 1);
    }
  }
  return 0;
}
Problem p02610 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

li calc(vector<pair<int, li>> &vec) {
  int n = vec.size();
  rep(i, n) vec[i].first *= -1;
  sort(vec.begin(), vec.end());
  rep(i, n) vec[i].first *= -1;
  li s = 0;
  int i = 0, i0 = vec[0].first;
  multiset<li> st;
  while (i0) {
    while (i < n and vec[i].first == i0)
      st.insert(-vec[i++].second);
    if (st.size()) {
      s -= *st.begin();
      st.erase(st.begin());
    }
    i0--;
  }
  return s;
}

void solve(vector<int> &k, vector<li> &l, vector<li> r) {
  int n = k.size();
  vector<pair<int, li>> lft, rgt;
  li s = 0;
  rep(i, n) {
    if (r[i] < l[i]) {
      lft.push_back({k[i], l[i] - r[i]});
      s += r[i];
    } else if (l[i] < r[i]) {
      rgt.push_back({n - k[i], r[i] - l[i]});
      s += l[i];
    } else {
      s += l[i];
    }
  }
  print(s + calc(rgt) + calc(lft));
}

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<li> l(n), r(n);
    rep(i, n) { cin >> k[i] >> l[i] >> r[i]; }
    solve(k, l, r);
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

li calc(vector<pair<int, li>> &vec) {
  int n = vec.size();
  if (n == 0)
    return 0;
  rep(i, n) vec[i].first *= -1;
  sort(vec.begin(), vec.end());
  rep(i, n) vec[i].first *= -1;
  li s = 0;
  int i = 0, i0 = vec[0].first;
  multiset<li> st;
  while (i0) {
    while (i < n and vec[i].first == i0)
      st.insert(-vec[i++].second);
    if (st.size()) {
      s -= *st.begin();
      st.erase(st.begin());
    }
    i0--;
  }
  return s;
}

void solve(vector<int> &k, vector<li> &l, vector<li> r) {
  int n = k.size();
  vector<pair<int, li>> lft, rgt;
  li s = 0;
  rep(i, n) {
    if (r[i] < l[i]) {
      lft.push_back({k[i], l[i] - r[i]});
      s += r[i];
    } else if (l[i] < r[i]) {
      rgt.push_back({n - k[i], r[i] - l[i]});
      s += l[i];
    } else {
      s += l[i];
    }
  }
  print(s + calc(rgt) + calc(lft));
}

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<li> l(n), r(n);
    rep(i, n) { cin >> k[i] >> l[i] >> r[i]; }
    solve(k, l, r);
  }
}
Problem p02610 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  vector<pair<int, int>> l, r;
  rep(i, n) {
    int k, x, y;
    cin >> k >> x >> y;
    if (x >= y) {
      ans += y;
      l.emplace_back(x - y, k - 1);
    } else {
      ans += x;
      r.emplace_back(y - x, k - 1);
    }
  }
  set<int> L, R;
  rep(i, l.size()) L.insert(i);
  rep(i, r.size()) R.insert(i + l.size());
  sort(l.rbegin(), l.rend());
  sort(r.rbegin(), r.rend());
  for (auto e : l) {
    auto itr = L.upper_bound(e.second);
    if (itr != L.begin()) {
      --itr;
      ans += e.first;
      L.erase(itr);
    }
  }
  for (auto e : r) {
    auto itr = R.upper_bound(e.second);
    if (itr != R.end()) {
      ans += e.first;
      L.erase(itr);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  vector<pair<int, int>> l, r;
  rep(i, n) {
    int k, x, y;
    cin >> k >> x >> y;
    if (x >= y) {
      ans += y;
      l.emplace_back(x - y, k - 1);
    } else {
      ans += x;
      r.emplace_back(y - x, k - 1);
    }
  }
  set<int> L, R;
  rep(i, l.size()) L.insert(i);
  rep(i, r.size()) R.insert(i + l.size());
  sort(l.rbegin(), l.rend());
  sort(r.rbegin(), r.rend());
  for (auto e : l) {
    auto itr = L.upper_bound(e.second);
    if (itr != L.begin()) {
      --itr;
      ans += e.first;
      L.erase(itr);
    }
  }
  for (auto e : r) {
    auto itr = R.upper_bound(e.second);
    if (itr != R.end()) {
      ans += e.first;
      R.erase(itr);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}
Problem p02610 (C++)

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

const int maxn = 100010;
int T, n;
vector<int> G[maxn], H[maxn];

int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      G[i].clear(), H[i].clear();
    }
    long long ans = 0;
    for (int i = 1, K, L, R; i <= n; i++) {
      scanf("%d %d %d", &K, &L, &R);
      if (L > R)
        ans += R, G[K].push_back(L - R);
      else
        ans += L, H[K].push_back(R - L);
    }
    multiset<int> S;
    for (int i = 1; i <= n; i++) {
      for (int j : G[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    S.clear();
    for (int i = n; i; i--) {
      for (int j : H[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > n - i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

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

const int maxn = 200010;
int T, n;
vector<int> G[maxn], H[maxn];

int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      G[i].clear(), H[i].clear();
    }
    long long ans = 0;
    for (int i = 1, K, L, R; i <= n; i++) {
      scanf("%d %d %d", &K, &L, &R);
      if (L > R)
        ans += R, G[K].push_back(L - R);
      else
        ans += L, H[K].push_back(R - L);
    }
    multiset<int> S;
    for (int i = 1; i <= n; i++) {
      for (int j : G[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    S.clear();
    for (int i = n; i; i--) {
      for (int j : H[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > n - i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p02612 (Python)

Buggy submission (status: Runtime Error):
n = input()
n = int(n[1:])
if 1000 - n == 1000:
    print(0)
else:
    print(1000 - n)


Fixed submission:
n = int(input())
n = n % 1000
if 1000 - n == 1000:
    print(0)
else:
    print(1000 - n)
Problem p02612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

int main() {
  int N = {};            // 商品代金
  int change = {};       // お釣り
  int Min_value = 1;     // 下限値
  int Max_value = 10000; // 上限値
  std::cin >> N;

  if (Min_value > N || N > Max_value) {
    std::cout << "予算を10000円を超えてます！" << std::endl;
  }

  while (N >= 1000) {
    if (N == 1000) {
      change = 0;
      std::cout << change << std::endl;
      return 0;
    }

    N - 1000;
  }

  change = 1000 - N;
  std::cout << change << std::endl;
}

Fixed submission:
#include <iostream>

int main() {
  int N = {};            // 商品代金
  int change = {};       // お釣り
  int Min_value = 1;     // 下限値
  int Max_value = 10000; // 上限値
  std::cin >> N;

  if (Min_value > N || N > Max_value) {
    std::cout << "予算を10000円を超えてます！" << std::endl;
  }

  while (N >= 1000) {
    if (N == 1000) {
      change = 0;
      std::cout << change << std::endl;
      return 0;
    }

    N = N - 1000;
  }

  change = 1000 - N;
  std::cout << change << std::endl;
}
Problem p02612 (Python)

Buggy submission (status: Runtime Error):
# input
N = int(input())
noguchi = [1000 * n for n in range(1, 11)]

# check
print(min([ngc - N for ngc in noguchi if (ngc - N) > 0]))


Fixed submission:
# input
N = int(input())
noguchi = [1000 * n for n in range(1, 11)]

# check
print(min([ngc - N for ngc in noguchi if (ngc - N) >= 0]))
Problem p02612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  scanf("%lld", n);
  int ans = n % 1000;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ans;
  ans = (1000 - n % 1000) % 1000;
  cout << ans << endl;
  return 0;
}
Problem p02612 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int t = n % 1000;
  return 1000 - t;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int t = n % 1000;
  if (t == 0)
    cout << "0";
  else
    cout << 1000 - t;
}
Problem p02612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main() {
  int p, c;
  scanf("%d", &p);
  for (c = 0; c < p; c = +1000) {
    c;
  }
  printf("%d", c - p);
}

Fixed submission:
#include <stdio.h>
int main() {
  int a;
  scanf("%d", &a);
  printf("%d\n", (1000 - (a - a / 1000 * 1000)) % 1000);
  return 0;
}
Problem p02612 (C++)

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

int main() {
  int cost;
  cin >> cost;
  int money = 0;
  while (money < cost) {
    money + 1000;
  }
  cout << money - cost << endl;
}

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

int main() {
  int cost;
  cin >> cost;
  int money = 0;
  while (money < cost) {
    money += 1000;
  }
  cout << money - cost << endl;
}
Problem p02612 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    if (n % 1000 != 0) {
      n %= 1000;
      cout << 1000 - n;
    } else
      cout << 0;
  }
}


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

int main() {
  int n;
  cin >> n;
  if (n % 1000 != 0) {
    n %= 1000;
    cout << 1000 - n;
  } else
    cout << 0;
}
Problem p02612 (C++)

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

using namespace std;
#define PI 3.1415926535897932384626
#define ll long long
#define ull unsigned long long

#define all(x) x.begin(), x.end()
#define fo(i, n) for (i = 0; i < n; i++)
#define sortall(x) sort(all(x))

#define vi vector<int>
#define vs vector<string>
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, int> pii;
typedef vector<psi> vpsi;
typedef vector<psi> vpci;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
//=======================

const int MOD = 1'000'000'007;
const int N = 2e6 + 13;

//=======================
vi g[N];
int a[N];
int n, m, k;
//=======================

void solve() { cin >> n; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t >= 1000) {
    t -= 1000;
  }
  cout << (1000 % t == 0 ? 0 : 1000 % t) << endl;
}

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

using namespace std;
#define PI 3.1415926535897932384626
#define ll long long
#define ull unsigned long long

#define all(x) x.begin(), x.end()
#define fo(i, n) for (i = 0; i < n; i++)
#define sortall(x) sort(all(x))

#define vi vector<int>
#define vs vector<string>
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, int> pii;
typedef vector<psi> vpsi;
typedef vector<psi> vpci;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
//=======================

const int MOD = 1'000'000'007;
const int N = 2e6 + 13;

//=======================
vi g[N];
int a[N];
int n, m, k;
//=======================

void solve() { cin >> n; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t >= 1000) {
    t -= 1000;
  }
  if (t != 0) {
    t = abs(t - 1000);
  }
  cout << t << endl;
}
Problem p02612 (C++)

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

int main() {
  int cost;
  cin >> cost;
  if (cost < 1000)
    return 1000 - cost;
  else
    return cost % 1000;
  return 0;
}

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

int main() {
  int cost;
  cin >> cost;
  int change;
  if (cost % 1000) {
    change = 1000 - cost % 1000;
  } else {
    change = 0;
  }

  cout << change;

  return 0;
}
Problem p02612 (C++)

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

int main() {
  string S;
  cin >> S;
  cout << S.substr(S.length() - 3) << endl;
}

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

int main() {
  int N;
  cin >> N;
  cout << (1000 - N % 1000) % 1000 << endl;
}
Problem p02612 (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;

  int S = N / 1000;
  int T = S * 1000;
  cout << N % T << 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;

  int S = N % 1000;

  if (S == 0) {
    cout << 0 << endl;
  } else {
    cout << 1000 - S << endl;
  }
}
Problem p02612 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>

using namespace std;

int main() {

  int N;
  cin >> N;

  if (N % 1000) {
    cout << (1000 - N % 1000) << std::endl;
  } else {
    cout << N % 1000 << endl;
  }

  return 1;
}

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int N;
  cin >> N;

  if (N % 1000) {
    cout << (1000 - N % 1000) << std::endl;
  } else {
    cout << N % 1000 << endl;
  }

  return 0;
}
Problem p02612 (C++)

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

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

  for (int i; i = 1; i++) {
    if (i * 1000 >= n) {
      cout << i * 1000 - n << endl;
      break;
    }
  }
}


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

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

  if (n % 1000 != 0)
    cout << 1000 - n % 1000 << endl;
  else
    cout << 0 << endl;
}
Problem p02612 (C++)

Buggy submission (status: Runtime Error):
// ABC173.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
using namespace std;

int main() {
  int price, change, thousands;

  cin >> price;

  thousands = price / 1000;

  if (price % 1000 == 0) {
    cout << 0;
    return 0;
  } else {
    change = (thousands + 1) * 1000 - price;
    cout << change;
    return change;
  }
}


Fixed submission:
// ABC173.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
using namespace std;

int main() {
  int price, change, thousands;

  cin >> price;

  thousands = price / 1000;

  if (price % 1000 == 0) {
    cout << 0;
    return 0;
  } else {
    change = (thousands + 1) * 1000 - price;
    cout << change;
    return 0;
  }
}
Problem p02612 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  n = n % 1000;
  n = 1000 - n;
  if (n == 1000)
    return 0;
  return n;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  n = n % 1000;
  n = 1000 - n;
  if (n == 1000)
    cout << 0 << endl;
  else
    cout << n << endl;
}
Problem p02612 (Python)

Buggy submission (status: Runtime Error):
def main():
    N = input()
    return N % 1000


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    if N % 1000 == 0:
        print(0)
    else:
        print(1000 - (N % 1000))


if __name__ == "__main__":
    main()
Problem p02612 (C++)

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

#define int long long
#define ll long long
#define pb push_back

#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, a, b) for (int i = (a); i > (b); --i)

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;

const int mod = 1e9 + 7;
const ll inf = 2e18 + 5;

void solve() {
  int n, k;
  cin >> n;
  int m = n % m;
  m = n - m;
  if (m == n)
    cout << 0 << endl;
  else
    cout << m << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("fin.txt", "r", stdin);
  freopen("fout.txt", "w", stdout);
#endif
  int cases = 1;
  // cin>>cases;

  for (int iter = 0; iter < cases; iter++) {
    solve();
  }
  return 0;
}

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

#define int long long
#define ll long long
#define pb push_back

#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, a, b) for (int i = (a); i > (b); --i)

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;

const int mod = 1e9 + 7;
const ll inf = 2e18 + 5;

void solve() {
  int n, k;
  cin >> n;
  int m = n % 1000;
  m = 1000 - m;
  if (m == 1000)
    cout << 0 << endl;
  else
    cout << m << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("fin.txt", "r", stdin);
  freopen("fout.txt", "w", stdout);
#endif
  int cases = 1;
  // cin>>cases;

  for (int iter = 0; iter < cases; iter++) {
    solve();
  }
  return 0;
}
Problem p02612 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  cout << 1000 - n % 1000;
  return 1000 - n % 1000;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n % 1000 == 0)
    cout << 0;
  else
    cout << 1000 - n % 1000;
  return 0;
}
Problem p02612 (C++)

Buggy submission (status: Runtime Error):
/// You just can't beat the person who never gives up

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;

int n;
bool Never_give_up() {
  cin >> n;
  cout << (n + 999) / 1000 * 1000 - n << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    Never_give_up();
  return 0;
}

Fixed submission:
/// You just can't beat the person who never gives up

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;

int n;
bool Never_give_up() {
  cin >> n;
  cout << (n + 999) / 1000 * 1000 - n << endl;
  return 0;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // scanf("%d",&t);
  while (t--)
    Never_give_up();
  return 0;
}