Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 200005;

int N;
string S;
int SCount = 0;

int V[MAX_N][3];

int myPow(int n, int k, int mod) {
  int tmp = n;
  int ret = 1;
  while (k) {
    if (k & 1) {
      ret *= tmp;
    }
    tmp *= tmp;
    k /= 2;
    ret %= mod;
    tmp %= mod;
  }
  return ret % mod;
}

void initSet() {
  FOR(k, -1, 2) {
    int K = SCount + k;
    if (K == 0) {
      REP(i, S.size()) { V[i][k + 1] = 0; }
      continue;
    }
    int ret = 0;
    for (const char &c : S) {
      ret *= 2;
      if (c == '1') {
        ret++;
      }
      ret %= K;
    }
    REP(i, S.size()) {
      if (S[i] == '0') {
        V[i][k + 1] = ret;
      } else {
        V[i][k + 1] = (ret + K - (myPow(2, (int)S.size() - 1 - i, K))) % K;
      }
    }
  }
}

string myMod(const string &str, int k, bool useArray, bool plusToggle,
             int index) {
  int ret = 0;
  if (useArray) {
    ret = V[index][plusToggle ? 2 : 0];
    if (plusToggle) {
      ret += myPow(2, N - index - 1, k);
      ret %= k;
    }
  } else {
    for (const char &c : str) {
      ret *= 2;
      if (c == '1') {
        ret++;
      }
      ret %= k;
    }
  }

  string retStr = "";
  while (ret) {
    if (ret % 2 == 1) {
      retStr += '1';
    } else {
      retStr += '0';
    }
    ret /= 2;
  }
  reverse(retStr.begin(), retStr.end());
  return retStr;
}

bool toggle(int n) {
  S[n] = (S[n] == '0' ? '1' : '0');
  return S[n] == '1';
}

signed main() {
  cin >> N;
  cin >> S;
  for (char s : S) {
    if (s == '1') {
      SCount++;
    }
  }

  if (SCount == 0) {
    REP(n, N) { cout << 1 << endl; }
    return 0;
  }

  initSet();

  REP(n, N) {
    bool plusToggle = false;
    if (toggle(n)) {
      SCount++;
      plusToggle = true;
    } else {
      SCount--;
    }

    int ans = 0;
    int k = SCount;
    string s;
    while (k) {
      ans++;
      if (ans == 1) {
        s = myMod(S, k, true, plusToggle, n);
      } else {
        s = myMod(s, k, false, plusToggle, n);
      }
      k = 0;
      for (char c : s) {
        if (c == '1') {
          k++;
        }
      }
    }
    cout << ans << endl;

    if (toggle(n)) {
      SCount++;
    } else {
      SCount--;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 200005;

int N;
string S;
int SCount = 0;

int V[MAX_N][3];

int myPow(int n, int k, int mod) {
  int tmp = n;
  int ret = 1;
  while (k) {
    if (k & 1) {
      ret *= tmp;
    }
    tmp *= tmp;
    k /= 2;
    ret %= mod;
    tmp %= mod;
  }
  return ret % mod;
}

void initSet() {
  FOR(k, -1, 2) {
    int K = SCount + k;
    int ret = 0;
    for (const char &c : S) {
      ret *= 2;
      if (c == '1') {
        ret++;
      }
      ret %= K;
    }
    REP(i, S.size()) {
      if (S[i] == '0') {
        V[i][k + 1] = ret;
      } else {
        V[i][k + 1] = (ret + K - (myPow(2, S.size() - 1 - i, K))) % K;
      }
    }
  }
}

string myMod(const string &str, int k, bool useArray, bool plusToggle,
             int index) {
  int ret = 0;
  // int ret2 = 0;
  if (useArray) {
    ret = V[index][plusToggle ? 2 : 0];
    if (plusToggle) {
      ret += myPow(2, N - index - 1, k);
      ret %= k;
    }
  } else {
    for (const char &c : str) {
      ret *= 2;
      if (c == '1') {
        ret++;
      }
      ret %= k;
    }
  }

  // for (const char& c : str) {
  //   ret2 *= 2;
  //   if (c == '1') {
  //     ret2++;
  //   }
  //   ret2 %= k;
  // }

  // cerr << S << ": " << ret << " " << ret2 << endl;

  string retStr = "";
  while (ret) {
    if (ret % 2 == 1) {
      retStr += '1';
    } else {
      retStr += '0';
    }
    ret /= 2;
  }
  reverse(retStr.begin(), retStr.end());
  return retStr;
}

bool toggle(int n) {
  S[n] = (S[n] == '0' ? '1' : '0');
  return S[n] == '1';
}

signed main() {
  cin >> N;
  cin >> S;
  for (char s : S) {
    if (s == '1') {
      SCount++;
    }
  }

  if (SCount == 0) {
    REP(n, N) { cout << 0 << endl; }
    return 0;
  }

  initSet();

  REP(n, N) {
    bool plusToggle = false;
    if (toggle(n)) {
      SCount++;
      plusToggle = true;
    } else {
      SCount--;
    }

    int ans = 0;
    int k = SCount;
    string s;
    while (k) {
      ans++;
      if (ans == 1) {
        s = myMod(S, k, true, plusToggle, n);
      } else {
        s = myMod(s, k, false, plusToggle, n);
      }
      k = 0;
      for (char c : s) {
        if (c == '1') {
          k++;
        }
      }
    }
    cout << ans << endl;

    if (toggle(n)) {
      SCount++;
    } else {
      SCount--;
    }
  }
  return 0;
}