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


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - エイシング プログラミング コンテスト 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 DEBUG fprintf(stderr, "Passing [%s] line %d\\n", __FUNCTION__, __LINE__)
#define File(x)                                                                \\
  freopen(x ".in", "r", stdin);                                                \\
  freopen(x ".out", "w", stdout)

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

inline int gi() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

inline LL gl() {
  LL f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;

int n, m;
char s[N];
int cnt;
LL m1, m2, m3;
LL p1[N], p2[N], p3[N];

inline int get2(LL x) {
  LL j = x;
  int cnt = 0;
  while (j) {
    if (j & 1)
      ++cnt;
    j >>= 1;
  }
  return cnt;
}

inline int getans(LL x) {
  int ans = 0;
  while (x) {
    ++ans;
    x %= get2(x);
  }
  return ans;
}

int main() {
  // File("");
  n = gi();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i += 1)
    cnt += (s[i] - '0');
  p1[n] = p3[n] = 1;
  for (int i = n - 1; i >= 1; i -= 1) {
    if (cnt > 1)
      p1[i] = p1[i + 1] * 2 % (cnt - 1);
    p3[i] = p3[i + 1] * 2 % (cnt + 1);
  }
  if (cnt > 1)
    p1[n] = 1 % (cnt - 1);
  p3[n] = 1 % (cnt + 1);
  for (int i = 1; i <= n; i += 1)
    if (s[i] == '1') {
      if (cnt > 1)
        m1 = (m1 + p1[i]) % (cnt - 1);
      m3 = (m3 + p3[i]) % (cnt + 1);
    }
  for (int i = 1; i <= n; i += 1) {
    if (s[i] == '1') {
      if (cnt > 1) {
        int xx = (m1 - p1[i] + cnt - 1) % (cnt - 1);
        printf("%d\\n", getans(xx) + 1);
      } else
        puts("0");
    } else {
      // cout << (x + (1 << (n - i))) << ' ' << cnt + 1 << endl;
      // cout << m3 << ' ' << p3[i] << '?' << endl;
      int xx = (m3 + p3[i] + cnt + 1) % (cnt + 1);
      // cout << ' ' << m3 << endl;
      // cout << m3 << endl;
      printf("%d\\n", getans(xx) + 1);
    }
  }
  return 0;
}

Error: 0

Fixed submission:


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - エイシング プログラミング コンテスト 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 DEBUG fprintf(stderr, "Passing [%s] line %d\\n", __FUNCTION__, __LINE__)
#define File(x)                                                                \\
  freopen(x ".in", "r", stdin);                                                \\
  freopen(x ".out", "w", stdout)

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

inline int gi() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

inline LL gl() {
  LL f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int INF = 0x3f3f3f3f, N = 200003, M = N << 1, P = 5;

int n, m;
char s[N];
int cnt;
LL m1, m2, m3;
LL p1[N], p2[N], p3[N];

inline int get2(LL x) {
  LL j = x;
  int cnt = 0;
  while (j) {
    if (j & 1)
      ++cnt;
    j >>= 1;
  }
  return cnt;
}

inline int getans(LL x) {
  int ans = 0;
  while (x) {
    ++ans;
    x %= get2(x);
  }
  return ans;
}

int main() {
  // File("");
  n = gi();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i += 1)
    cnt += (s[i] - '0');
  p1[n] = p3[n] = 1;
  for (int i = n - 1; i >= 1; i -= 1) {
    if (cnt > 1)
      p1[i] = p1[i + 1] * 2 % (cnt - 1);
    p3[i] = p3[i + 1] * 2 % (cnt + 1);
  }
  p1[n] = 1 % (cnt - 1);
  p3[n] = 1 % (cnt + 1);
  for (int i = 1; i <= n; i += 1)
    if (s[i] == '1') {
      if (cnt > 1)
        m1 = (m1 + p1[i]) % (cnt - 1);
      m3 = (m3 + p3[i]) % (cnt + 1);
    }
  for (int i = 1; i <= n; i += 1) {
    if (s[i] == '1') {
      if (cnt > 1) {
        int xx = (m1 - p1[i] + cnt - 1) % (cnt - 1);
        printf("%d\\n", getans(xx) + 1);
      } else
        puts("0");
    } else {
      // cout << (x + (1 << (n - i))) << ' ' << cnt + 1 << endl;
      // cout << m3 << ' ' << p3[i] << '?' << endl;
      int xx = (m3 + p3[i] + cnt + 1) % (cnt + 1);
      // cout << ' ' << m3 << endl;
      // cout << m3 << endl;
      printf("%d\\n", getans(xx) + 1);
    }
  }
  return 0;
}