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

#include <bitset>
#include <iostream>
#include <map>
#include <string>
using namespace std;
std::map<int, long long> mp1;
std::map<int, long long> mp2;
unsigned long ans1 = 0;
unsigned long ans2 = 0;
std::map<int, long long> mp3;

long long pow(long long n, long long p, long long m) {
  if (m == 0)
    return 0;
  if (p == 0)
    return (1 % m);
  long long ret = 1;
  for (; p > 0; p >>= 1, n = n * n % m) {
    if (p % 2 == 1) {
      ret = ret * n % m;
    }
  }
  return ret;
}

unsigned long f(std::bitset<200000> &bs, int hibit, long long index,
                long long N) {
  long long ret = -1;
  long long count = 0;

  // 初回だけmpを使って計算
  // int hibit = count;
  if (bs[index] == 0) {
    ret = ans1 + mp1[index];
    hibit++;
  } else {
    ret = ans2 - mp2[index];
    hibit--;
    if (hibit <= 0)
      return count;
    if (ret < 0)
      ret += hibit;
  }
  if (ret >= hibit) {
    // std::cout << ">> " << ret << std::endl;
    ret = ret % hibit;
  }
  count++;
  // std::cout << ">>>> " << ret << std::endl;

  std::map<int, long long>::iterator itr = mp3.find(ret);
  if (itr != mp3.end()) {
    return itr->second;
  }
  int tmp = ret;

  while (ret != 0) {
    // std::cout << ">>>> " << ret << std::endl;
    std::bitset<200000> tmpbs(ret);
    ret = ret % tmpbs.count();
    count++;
  }
  mp3.emplace(tmp, count);

  // nが0になるまでの操作回数を返す
  return count;
}

int main() {
  long long N;
  std::cin >> N;
  std::string X;
  std::cin >> X;

  // 前準備
  std::bitset<200000> bs(X);
  // N = 200000;
  // std::bitset<200000> bs(std::string(N, '0'));
  // bs[0].flip();
  const int count = bs.count();

  // 制約から1<=Nなので、count=0のケースは考慮不要
  for (long long i = N - 1; i >= 0; i--) {
    mp1[i] = pow(2, i, count + 1);
    mp2[i] = pow(2, i, count - 1);
    if (bs[i] == 1) {
      ans1 += mp1[i];
      ans2 += mp2[i];
    }
  }
  // ans1 = ans1 % (count+1);
  // ans2 = ans2 % (count-1);

  // ここから本処理
  for (long long i = N - 1; i >= 0; i--) {
    unsigned long result = f(bs, count, i, N);
    std::cout << result << std::endl;
  }
}


Error: 0

Fixed submission:

#include <bitset>
#include <iostream>
#include <map>
#include <string>
using namespace std;
std::map<int, long long> mp1;
std::map<int, long long> mp2;
unsigned long ans1 = 0;
unsigned long ans2 = 0;
std::map<int, long long> mp3;

long long pow(long long n, long long p, long long m) {
  if (m == 0)
    return 0;
  if (p == 0)
    return (1 % m);
  long long ret = 1;
  for (; p > 0; p >>= 1, n = n * n % m) {
    if (p % 2 == 1) {
      ret = ret * n % m;
    }
  }
  return ret;
}

unsigned long f(std::bitset<200000> &bs, int hibit, long long index,
                long long N) {
  long long ret = -1;
  long long count = 0;

  // 初回だけmpを使って計算
  // int hibit = count;
  if (bs[index] == 0) {
    ret = ans1 + mp1[index];
    hibit++;
  } else {
    ret = ans2 - mp2[index];
    hibit--;
    if (hibit <= 0)
      return count;
    if (ret < 0)
      ret += hibit;
  }
  if (ret >= hibit) {
    // std::cout << ">> " << ret << std::endl;
    ret = ret % hibit;
  }
  count++;
  // std::cout << ">>>> " << ret << std::endl;

  /*
  std::map<int, long long>::iterator itr = mp3.find(ret);
  if(itr != mp3.end()){
      return itr->second;
  }
  int tmp = ret;
  */

  while (ret != 0) {
    // std::cout << ">>>> " << ret << std::endl;
    std::bitset<200000> tmpbs(ret);
    ret = ret % tmpbs.count();
    count++;
  }
  // mp3.emplace(tmp, count);

  // nが0になるまでの操作回数を返す
  return count;
}

int main() {
  long long N;
  std::cin >> N;
  std::string X;
  std::cin >> X;

  // 前準備
  std::bitset<200000> bs(X);
  // N = 200000;
  // std::bitset<200000> bs(std::string(N, '1'));
  // bs[0].flip();
  const int count = bs.count();

  // 制約から1<=Nなので、count=0のケースは考慮不要
  for (long long i = N - 1; i >= 0; i--) {
    mp1[i] = pow(2, i, count + 1);
    mp2[i] = pow(2, i, count - 1);
    if (bs[i] == 1) {
      ans1 += mp1[i];
      ans2 += mp2[i];
    }
  }
  ans1 = ans1 % (count + 1);
  ans2 = ans2 % (count - 1);

  // ここから本処理
  for (long long i = N - 1; i >= 0; i--) {
    unsigned long result = f(bs, count, i, N);
    std::cout << result << std::endl;
  }
}