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>
#define int long long
#define ios                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define mii map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = (int)x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
using namespace std;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
int power(int x, int y, int MOD) {
  int res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;
  if (y == 0)
    return 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
int fun(int n) {
  if (n == 0)
    return 0;
  int ans = 1;
  ans += fun(n % setbits(n));
  return ans;
}
void solve() {
  int n;
  string x;
  cin >> n >> x;
  int z = count(all(x), '1');
  int z1 = 0, z2 = 0;
  if (z == 0) {
    rep(i, 0, n) { cout << 1 << endl; }
    return;
  }
  if (z == 1) {
    if (x[n - 1] == '1')
      z2 = 1;
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        if (i == n - 1)
          cout << 2 << endl;
        else
          cout << z2 + 1 << endl;
      }
    }
    return;
  }
  for (int i = 0; i < n; i++) {
    if (x[n - 1 - i] == '1') {
      z1 = (z1 + power(2, i, z - 1)) % (z - 1);
      z2 = (z2 + power(2, i, z + 1)) % (z + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    int ans = 0;
    if (x[i] == '1') {
      int kk =
          (((z1 - power(2, n - 1 - i, z - 1)) % (z - 1) + z - 1) % (z - 1));
      cout << fun(kk) + 1 << endl;
    } else {
      int kk = ((z2 + power(2, n - 1 - i, z + 1)) % (z + 1) + z + 1) % (z + 1);
      cout << fun(kk) + 1 << endl;
    }
  }
}

int32_t main() {
  ios;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

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>
#define int long long
#define ios                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define mii map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = (int)x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
using namespace std;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
int power(int x, unsigned int y, int MOD) {
  int res = 1;
  x = x % MOD;
  if (x == 0)
    return 0;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % MOD;
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return res;
}
int fun(int n) {
  if (n == 0)
    return 0;
  int ans = 1;
  ans += fun(n % setbits(n));
  return ans;
}
void solve() {
  int n;
  string x;
  cin >> n >> x;
  int z = count(all(x), '1');
  int z1 = 0, z2 = 0;
  for (int i = 0; i < n; i++) {
    if (x[n - 1 - i] == '1') {
      z1 = (z1 + power(2, i, z - 1)) % (z - 1);
      z2 = (z2 + power(2, i, z + 1)) % (z + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int ans = 0;
    if (x[i] == '1') {
      cout << fun(((z1 - power(2, n - 1 - i, z - 1)) % (z - 1) + z - 1) %
                  (z - 1)) +
                  1
           << endl;
    } else {
      cout << fun(((z2 + power(2, n - 1 - i, z + 1)) % (z + 1) + z + 1) %
                  (z + 1)) +
                  1
           << endl;
    }
  }
}

int32_t main() {
  ios;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}