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)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define RI(i, n) FOR(i, 1, (n))
#define REP(i, n) FOR(i, 0, (n)-1)
#define mini(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int)w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;

int dp[maxN];
string s;

int solve(int i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == 0)
    return 0;
  int x = __builtin_popcount(i);
  dp[i] = solve(i % x) + 1;
  return dp[i];
}

ll pow2(ll a, ll b, ll MOD) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll d = pow2(a, b / 2, MOD);
  d = d * d % MOD;
  if (b % 2)
    return d * a % MOD;
  return d;
}

// sprawdz MODULO!
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  cin >> s;
  dp[0] = 0;
  RI(i, 2e5) dp[i] = -1;
  RI(i, 2e5) { dp[i] = solve(i); }
  ll popcnt = 0;
  FORD(i, n - 1, 0) { popcnt += (s[i] == '1'); }
  ll ans = 0;
  ll MOD = (popcnt - 1) * (popcnt + 1);
  if (MOD == 0)
    MOD = 2;
  ll pot = 1;
  FORD(i, n - 1, 0) {
    if (s[i] == '1')
      ans = (ans + pot) % MOD;
    pot = (pot * 2) % MOD;
  }
  pot = 1;
  vector<int> vec;
  FORD(i, n - 1, 0) {
    ll new_cnt = popcnt;
    if (s[i] == '0') {
      new_cnt++;
      ll new_ans = (ans + pot) % MOD;
      vec.pb(1 + dp[new_ans % new_cnt]);
    } else {
      new_cnt--;
      if (new_cnt == 0)
        vec.pb(0);
      else {
        ll new_ans = (ans - pot + MOD) % MOD;
        vec.pb(1 + dp[new_ans % new_cnt]);
      }
    }
    pot = (pot * 2) % MOD;
  }
  reverse(vec.begin(), vec.end());
  for (auto x : vec) {
    cout << x << "\\n";
  }
  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)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define RI(i, n) FOR(i, 1, (n))
#define REP(i, n) FOR(i, 0, (n)-1)
#define mini(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int)w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;

int dp[maxN];
string s;

int solve(int i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == 0)
    return 0;
  int x = __builtin_popcount(i);
  dp[i] = solve(i % x) + 1;
  return dp[i];
}

ll pow2(ll a, ll b, ll MOD) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll d = pow2(a, b / 2, MOD);
  d = d * d % MOD;
  if (b % 2)
    return d * a % MOD;
  return d;
}

// sprawdz MODULO!
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  cin >> s;
  dp[0] = 0;
  RI(i, 2e5) dp[i] = -1;
  RI(i, 2e5) { dp[i] = solve(i); }
  ll popcnt = 0;
  FORD(i, n - 1, 0) { popcnt += (s[i] == '1'); }
  ll ans = 0;
  ll MOD = (popcnt - 1) * (popcnt + 1);
  ll pot = 1;
  FORD(i, n - 1, 0) {
    if (s[i] == '1')
      ans = (ans + pot) % MOD;
    pot = (pot * 2) % MOD;
  }
  pot = 1;
  vector<int> vec;
  FORD(i, n - 1, 0) {
    ll new_cnt = popcnt;
    if (s[i] == '0') {
      new_cnt++;
      ll new_ans = (ans + pot) % MOD;
      vec.pb(1 + dp[new_ans % new_cnt]);
    } else {
      new_cnt--;
      if (new_cnt == 0)
        vec.pb(0);
      else {
        ll new_ans = (ans - pot + MOD) % MOD;
        vec.pb(1 + dp[new_ans % new_cnt]);
      }
    }

    pot = (pot * 2) % MOD;
  }
  reverse(vec.begin(), vec.end());
  for (auto x : vec) {
    cout << x << "\\n";
  }
  return 0;
}