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


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int, int>
#define m_p(x, y) make_pair(x, y)
#define vp vector<pair<int, int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v, n, x) v.assign(n, x)
#define incd(v, n) v.resize(n)
#define iniz(n) memset(n, 0, sizeof(n))
#define inin(n) memset(n, -1, sizeof(n))
#define inimi(n) memset(n, 0xc0, sizeof(n))
#define inima(n) memset(n, 0x3f, sizeof(n))
#define all(v) (v).begin(), (v).end()

using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.f >> t.se;
  return is;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\\n';
  return os;
}

const int N = 200001;
double pi = acos(-1.0);
int md = 1e9 + 7;
const int INF = 1e15;
int dx1[] = {0, 0, -1, 1};
int dy1[] = {1, -1, 0, 0};

template <class T> T abst(T a) { return a < 0 ? -a : a; }

template <class T> T max2(T a, T b) { return a > b ? a : b; }
template <class T> T min2(T a, T b) { return a < b ? a : b; }
map<pair<int, pii>, int> mp;
int Pow(int n, int x) {
  int ans = 1;
  while (x > 0) {
    if (x & 1) {
      ans = (ans * n) % md;
    }
    n = (n * n) % md;
    x = x >> 1;
  }
  return ans;
}
string s;
int num1, num2;
int arr1[200001];
int ones;
int fun1(int x) {
  if (arr1[x] != -1)
    return arr1[x];
  return arr1[x] = 1 + fun1(x % setbits(x));
}
int fun(int x, int y, int z) {
  if (z == ones + 1) {
    md = ones + 1;
    if (md == 0)
      return 0;
    if (y) {
      return (num1 + Pow(2, x)) % md;
    } else {
      return (num1 - Pow(2, x) + md) % md;
    }
  } else {
    md = ones - 1;
    if (md == 0)
      return 0;
    if (y) {
      return (num2 + Pow(2, x)) % md;
    } else
      return (num2 - Pow(2, x) + md) % md;
  }
}
void solve() {
  int n;
  cin >> n;
  cin >> s;
  inin(arr1);
  arr1[0] = 0;
  // int ones=0;
  for (char c : s)
    ones += (c == '1');
  // precompute
  // ones+1,ones-1
  for (int i = 1; i <= 200000; i++)
    arr1[i] = fun1(i);
  for (int i = 0; i < size(s); i++) {
    int po = n - i - 1;
    md = ones + 1;
    num1 = (num1 + (s[i] - '0') * Pow(2, po)) % md;
    md = ones - 1;
    if (md != 0)
      num2 = (num2 + (s[i] - '0') * Pow(2, po)) % md;
  }
  for (int i = 0; i < size(s); i++) {
    int power = n - i - 1;
    int add_or_subtract = 0;
    if (s[i] == '0')
      add_or_subtract = 1;
    int one = ones;
    if (add_or_subtract)
      one++;
    else
      one--;
    if (one == 0) {
      cout << 0 << "\\n";
      continue;
    }
    int x = fun(power, add_or_subtract, one);
    cout << 1 + arr1[x] << "\\n";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int, int>
#define m_p(x, y) make_pair(x, y)
#define vp vector<pair<int, int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v, n, x) v.assign(n, x)
#define incd(v, n) v.resize(n)
#define iniz(n) memset(n, 0, sizeof(n))
#define inin(n) memset(n, -1, sizeof(n))
#define inimi(n) memset(n, 0xc0, sizeof(n))
#define inima(n) memset(n, 0x3f, sizeof(n))
#define all(v) (v).begin(), (v).end()

using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.f >> t.se;
  return is;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\\n';
  return os;
}

const int N = 200001;
double pi = acos(-1.0);
int md = 1e9 + 7;
const int INF = 1e15;
int dx1[] = {0, 0, -1, 1};
int dy1[] = {1, -1, 0, 0};

template <class T> T abst(T a) { return a < 0 ? -a : a; }

template <class T> T max2(T a, T b) { return a > b ? a : b; }
template <class T> T min2(T a, T b) { return a < b ? a : b; }
map<pair<int, pii>, int> mp;
int Pow(int n, int x) {
  int ans = 1;
  while (x > 0) {
    if (x & 1) {
      ans = (ans * n) % md;
    }
    n = (n * n) % md;
    x = x >> 1;
  }
  return ans;
}
string s;

int cal(int x, int y, int z) {
  md = z;
  int ans = 0;
  for (int i = 0; i < size(s); i++) {
    int po = size(s) - 1 - i;
    ans = (ans + (s[i] - '0') * Pow(2, po)) % z;
  }
  if (y) {
    ans = (ans + Pow(2, x)) % z;
  } else {
    ans = (ans - Pow(2, x) + z) % z;
  }
  return ans;
}
int fun(int x, int y, int z) {
  if (mp.count({x, {y, z}}))
    return mp[{x, {y, z}}];
  int y1 = cal(x, y, z);
  int ans = 1;
  while (y1) {
    y1 = y1 % setbits(y1);
    ans++;
  }
  return mp[{x, {y, z}}] = ans;
}
void solve() {
  int n;
  cin >> n;
  cin >> s;
  int ones = 0;
  for (char c : s)
    ones += (c == '1');
  for (int i = 0; i < size(s); i++) {
    int power = n - i - 1;
    int add_or_subtract = 0;
    if (s[i] == '0')
      add_or_subtract = 1;
    int one = ones;
    if (add_or_subtract)
      one++;
    else
      one--;
    cout << fun(power, add_or_subtract, one) << "\\n";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}