problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef EZZAT
freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
#else
#endif
}
const int N = 2e5 + 1;
int f[N], pow2[3][N], res[3];
int cntBit(int n) {
if (n == 0)
return 0;
return 1 + cntBit(n - (n & -n));
}
int main() {
run();
for (int i = 1; i < N; i++)
f[i] = 1 + f[i % cntBit(i)];
int n;
string s;
cin >> n >> s;
int MOD = 0;
for (int i = 0; i < n; i++)
MOD += (s[i] == '1');
for (int j = 0; j < 3; j++) {
int mod = MOD + j - 1;
if (mod <= 0)
continue;
pow2[j][0] = 1;
for (int i = 1; i < n; i++)
pow2[j][i] = (pow2[j][i - 1] << 1) % mod;
for (int i = 0; i < n; i++)
res[j] = (res[j] + (s[i] == '1') * pow2[j][n - 1 - i]) % mod;
}
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
int mod = MOD - 1;
int cur = (((res[0] - pow2[0][n - 1 - i]) % mod) + mod) % mod;
cout << 1 + f[cur] << endl;
} else {
int mod = MOD + 1;
int cur = (res[2] + pow2[2][n - 1 - i]) % mod;
cout << 1 + f[cur] << endl;
}
}
}
| #include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef EZZAT
freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
#else
#endif
}
const int N = 2e5 + 1;
int f[N], pow2[3][N], res[3];
int cntBit(int n) {
if (n == 0)
return 0;
return 1 + cntBit(n - (n & -n));
}
int main() {
run();
for (int i = 1; i < N; i++)
f[i] = 1 + f[i % cntBit(i)];
int n;
string s;
cin >> n >> s;
int MOD = 0;
for (int i = 0; i < n; i++)
MOD += (s[i] == '1');
for (int j = 0; j < 3; j++) {
int mod = MOD + j - 1;
if (mod <= 0)
continue;
pow2[j][0] = 1;
for (int i = 1; i < n; i++)
pow2[j][i] = (pow2[j][i - 1] << 1) % mod;
for (int i = 0; i < n; i++)
res[j] = (res[j] + (s[i] == '1') * pow2[j][n - 1 - i]) % mod;
}
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
int mod = MOD - 1;
if (mod == 0)
cout << 0 << endl;
else {
int cur = (((res[0] - pow2[0][n - 1 - i]) % mod) + mod) % mod;
cout << 1 + f[cur] << endl;
}
} else {
int mod = MOD + 1;
int cur = (res[2] + pow2[2][n - 1 - i]) % mod;
cout << 1 + f[cur] << endl;
}
}
}
| replace | 58 | 60 | 58 | 64 | 0 | |
p02609 | C++ | Runtime Error | // aising2020_d
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()
#define INF (ll)(1e9)
#define MOD (1000000007)
#define print2D(h, w, arr) \
REP(i, h) { \
REP(j, w) cout << arr[i][j] << " "; \
cout << endl; \
}
#define print_line(vec, n) \
{ \
for (int i = 0; i < (n - 1); i++) \
cout << (vec)[i] << " "; \
cout << (vec)[(n)-1] << endl; \
}
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
cout << first << " ";
print(rest...);
}
struct PreMain {
PreMain() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} premain;
int mod = 1e9 + 7;
struct mint {
ll x;
mint(ll a = 0) { x = a >= 0 ? a % mod : mod - (-a) % mod; }
mint(const mint &m) : x(m.x) {}
mint &operator+=(const mint &a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator+=(const int &a) {
if ((x += a) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const int &a) {
if ((x += mod - a) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= mod;
return *this;
}
mint &operator*=(const int &a) {
(x *= a) %= mod;
return *this;
}
mint operator+(const mint &a) const {
mint res(*this);
return res += a;
}
mint operator+(const int &a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint &a) const {
mint res(*this);
return res -= a;
}
mint operator-(const int &a) const {
mint res(*this);
return res -= a;
}
mint operator-() const {
mint res(*this);
return mint(0) - res;
}
mint operator*(const mint &a) const {
mint res(*this);
return res *= a;
}
mint operator*(const int &a) const {
mint res(*this);
return res *= a;
}
bool operator==(const mint &a) const { return x == a.x; }
bool operator==(const int &a) const { return x == a; }
bool operator!=(const mint &a) const { return x != a.x; }
bool operator!=(const int &a) const { return x != a; }
operator bool() const { return x > 0; }
mint pow(ll t) const {
mint r = 1, a = *this;
do {
if (t & 1)
r *= a;
a *= a;
} while (t >>= 1);
return r;
}
mint pow(mint t) const {
mint r = 1, a = *this;
do {
if (t.x & 1)
r *= a;
a *= a;
} while (t.x >>= 1);
return r;
}
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
mint &operator/=(const int &a) { return (*this) *= mint(a).inv(); }
mint operator/(const mint &a) const {
mint res(*this);
return res /= a;
}
mint operator/(const int &a) const {
mint res(*this);
return res /= a;
}
friend ostream &operator<<(ostream &os, const mint &m) {
cout << m.x;
return os;
}
friend istream &operator>>(istream &is, mint &m) { return is >> m.x; }
};
// 階乗
struct Factorial {
vector<mint> values;
explicit Factorial(int n) : values(n + 1, 0) {
values[0] = 1;
for (int i = 1; i <= n; ++i) {
values[i] = values[i - 1] * i;
}
}
mint operator()(int n) { return n >= 0 ? values[n] : mint(0); }
};
vector<ll> memo;
ll func(ll x) {
if (memo[x] != -1)
return memo[x];
ll n = __builtin_popcount(x);
return memo[x] = func(x % n) + 1;
}
int main() {
#ifdef LOCAL
ifstream in("../arg.txt");
cin.rdbuf(in.rdbuf());
#endif
int N;
cin >> N;
string X;
cin >> X;
memo.assign(N + 1, -1);
memo[0] = 0;
// REP(i, 4){
// print(i, func(i));
// }
int num = 0;
REP(i, N) { num += (X[i] == '1'); }
vector<ll> res(2, 0);
vector<vector<ll>> tmp(2, vector<ll>(N + 1, 1));
vector<int> mod{num + 1, num - 1};
REP(i, N) {
REP(k, 2) {
res[k] += (X[N - 1 - i] == '1') * tmp[k][i];
res[k] %= mod[k];
tmp[k][i + 1] = tmp[k][i] * 2;
tmp[k][i + 1] %= mod[k];
}
}
REP(i, N) {
ll x = 0;
ll ans;
if (X[i] == '1') {
if (num == 1) {
ans = 0;
} else {
x = res[1];
x -= tmp[1][N - 1 - i];
x = (x + mod[1]) % mod[1];
ans = func(x) + 1;
}
} else {
x = res[0];
x += tmp[0][N - 1 - i];
x = (x + mod[0]) % mod[0];
ans = func(x) + 1;
}
// print(i, x, ans);
print(ans);
}
return 0;
}
| // aising2020_d
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()
#define INF (ll)(1e9)
#define MOD (1000000007)
#define print2D(h, w, arr) \
REP(i, h) { \
REP(j, w) cout << arr[i][j] << " "; \
cout << endl; \
}
#define print_line(vec, n) \
{ \
for (int i = 0; i < (n - 1); i++) \
cout << (vec)[i] << " "; \
cout << (vec)[(n)-1] << endl; \
}
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
cout << first << " ";
print(rest...);
}
struct PreMain {
PreMain() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} premain;
int mod = 1e9 + 7;
struct mint {
ll x;
mint(ll a = 0) { x = a >= 0 ? a % mod : mod - (-a) % mod; }
mint(const mint &m) : x(m.x) {}
mint &operator+=(const mint &a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator+=(const int &a) {
if ((x += a) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const int &a) {
if ((x += mod - a) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= mod;
return *this;
}
mint &operator*=(const int &a) {
(x *= a) %= mod;
return *this;
}
mint operator+(const mint &a) const {
mint res(*this);
return res += a;
}
mint operator+(const int &a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint &a) const {
mint res(*this);
return res -= a;
}
mint operator-(const int &a) const {
mint res(*this);
return res -= a;
}
mint operator-() const {
mint res(*this);
return mint(0) - res;
}
mint operator*(const mint &a) const {
mint res(*this);
return res *= a;
}
mint operator*(const int &a) const {
mint res(*this);
return res *= a;
}
bool operator==(const mint &a) const { return x == a.x; }
bool operator==(const int &a) const { return x == a; }
bool operator!=(const mint &a) const { return x != a.x; }
bool operator!=(const int &a) const { return x != a; }
operator bool() const { return x > 0; }
mint pow(ll t) const {
mint r = 1, a = *this;
do {
if (t & 1)
r *= a;
a *= a;
} while (t >>= 1);
return r;
}
mint pow(mint t) const {
mint r = 1, a = *this;
do {
if (t.x & 1)
r *= a;
a *= a;
} while (t.x >>= 1);
return r;
}
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
mint &operator/=(const int &a) { return (*this) *= mint(a).inv(); }
mint operator/(const mint &a) const {
mint res(*this);
return res /= a;
}
mint operator/(const int &a) const {
mint res(*this);
return res /= a;
}
friend ostream &operator<<(ostream &os, const mint &m) {
cout << m.x;
return os;
}
friend istream &operator>>(istream &is, mint &m) { return is >> m.x; }
};
// 階乗
struct Factorial {
vector<mint> values;
explicit Factorial(int n) : values(n + 1, 0) {
values[0] = 1;
for (int i = 1; i <= n; ++i) {
values[i] = values[i - 1] * i;
}
}
mint operator()(int n) { return n >= 0 ? values[n] : mint(0); }
};
vector<ll> memo;
ll func(ll x) {
if (memo[x] != -1)
return memo[x];
ll n = __builtin_popcount(x);
return memo[x] = func(x % n) + 1;
}
int main() {
#ifdef LOCAL
ifstream in("../arg.txt");
cin.rdbuf(in.rdbuf());
#endif
int N;
cin >> N;
string X;
cin >> X;
memo.assign(N + 1, -1);
memo[0] = 0;
// REP(i, 4){
// print(i, func(i));
// }
int num = 0;
REP(i, N) { num += (X[i] == '1'); }
vector<ll> res(2, 0);
vector<vector<ll>> tmp(2, vector<ll>(N + 1, 1));
vector<int> mod{num + 1, num - 1};
REP(i, N) {
REP(k, 2) {
if (mod[k] == 0)
continue;
res[k] += (X[N - 1 - i] == '1') * tmp[k][i];
res[k] %= mod[k];
tmp[k][i + 1] = tmp[k][i] * 2;
tmp[k][i + 1] %= mod[k];
}
}
REP(i, N) {
ll x = 0;
ll ans;
if (X[i] == '1') {
if (num == 1) {
ans = 0;
} else {
x = res[1];
x -= tmp[1][N - 1 - i];
x = (x + mod[1]) % mod[1];
ans = func(x) + 1;
}
} else {
x = res[0];
x += tmp[0][N - 1 - i];
x = (x + mod[0]) % mod[0];
ans = func(x) + 1;
}
// print(i, x, ans);
print(ans);
}
return 0;
}
| replace | 194 | 195 | 194 | 196 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef unsigned long long int sll;
typedef long double ld;
#define A 1000000007
#define D 100000000000000ll
#define B 998244353ll
#define C 1000000000000000000ll
#define M 4000
#define FAST \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define oset \
tree<pair<ll, ll>, null_type, less_equal<pair<ll, ll>>, rb_tree_tag, \
tree_order_statistics_node_update>
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define br break
#define PI acos(-1)
#define subt \
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC \
<< "ms\n"
ll dp[200005];
ll pc(ll x) {
ll co = 0;
while (x > 0) {
if (x & 1)
co++;
x = x / 2;
}
return co;
}
int main() {
FAST;
for (int i = 1; i < 200005; i++)
dp[i] = dp[i % pc(i)] + 1;
ll n;
cin >> n;
string s;
cin >> s;
ll c = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1')
c++;
if (c == 0) {
for (int i = 0; i < n; i++)
cout << 1 << '\n';
return 0;
}
ll x = 0, y = 0, z = 0;
ll te1[n], te2[n], te3[n];
te1[n - 1] = 1;
te2[n - 1] = 1;
te3[n - 1] = 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
x = (x + te1[i]) % (c - 1);
y = (y + te2[i]) % c;
z = (z + te3[i]) % (c + 1);
}
if (i != 0) {
te1[i - 1] = (te1[i] * 2ll) % (c - 1);
te2[i - 1] = (te2[i] * 2ll) % (c);
te3[i - 1] = (te3[i] * 2ll) % (c + 1);
}
}
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
ll a = (x - te1[i] + c - 1) % (c - 1);
cout << dp[a] + 1 << '\n';
} else {
ll a = (z + te3[i]) % (c + 1);
cout << dp[a] + 1 << '\n';
}
}
subt;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef unsigned long long int sll;
typedef long double ld;
#define A 1000000007
#define D 100000000000000ll
#define B 998244353ll
#define C 1000000000000000000ll
#define M 4000
#define FAST \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define oset \
tree<pair<ll, ll>, null_type, less_equal<pair<ll, ll>>, rb_tree_tag, \
tree_order_statistics_node_update>
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define br break
#define PI acos(-1)
#define subt \
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC \
<< "ms\n"
ll dp[200005];
ll pc(ll x) {
ll co = 0;
while (x > 0) {
if (x & 1)
co++;
x = x / 2;
}
return co;
}
int main() {
FAST;
for (int i = 1; i < 200005; i++)
dp[i] = dp[i % pc(i)] + 1;
ll n;
cin >> n;
string s;
cin >> s;
ll c = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1')
c++;
if (c == 0) {
for (int i = 0; i < n; i++)
cout << 1 << '\n';
return 0;
}
if (c == 1) {
if (s[n - 1] == '1') {
for (int i = 0; i < n; i++) {
if (s[i] == '1')
cout << 0 << "\n";
else
cout << 2 << "\n";
}
} else {
for (int i = 0; i < n; i++) {
if (s[i] != '1') {
if (i != n - 1)
cout << 1 << '\n';
else
cout << 2 << '\n';
} else
cout << 0 << '\n';
}
}
return 0;
}
ll x = 0, y = 0, z = 0;
ll te1[n], te2[n], te3[n];
te1[n - 1] = 1;
te2[n - 1] = 1;
te3[n - 1] = 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
x = (x + te1[i]) % (c - 1);
y = (y + te2[i]) % c;
z = (z + te3[i]) % (c + 1);
}
if (i != 0) {
te1[i - 1] = (te1[i] * 2ll) % (c - 1);
te2[i - 1] = (te2[i] * 2ll) % (c);
te3[i - 1] = (te3[i] * 2ll) % (c + 1);
}
}
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
ll a = (x - te1[i] + c - 1) % (c - 1);
cout << dp[a] + 1 << '\n';
} else {
ll a = (z + te3[i]) % (c + 1);
cout << dp[a] + 1 << '\n';
}
}
subt;
return 0;
} | insert | 66 | 66 | 66 | 87 | 0 | Time : 56.766ms
|
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long M;
long long BigMod(long long b, long long m) {
long long a = 2;
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
long long f(long long n) {
long long step = 0;
while (__builtin_popcount(n)) {
step++;
int x = __builtin_popcount(n);
n %= x;
}
return step;
}
int main() {
int n;
scanf("%d", &n);
string x;
cin >> x;
long long num_of_one = 0;
for (int i = 0; i < x.size(); i++) {
num_of_one += x[i] == '1';
}
reverse(x.begin(), x.end());
long long mod_zero[300000];
long long mod_one[300000];
memset(mod_zero, 0, sizeof(mod_zero));
memset(mod_one, 0, sizeof(mod_one));
for (int i = 0; i < n; i++) {
if (x[i] == '1') {
mod_zero[i] = BigMod(i, num_of_one + 1);
mod_one[i] = BigMod(i, num_of_one - 1);
}
if (i > 0) {
mod_zero[i] += mod_zero[i - 1];
mod_zero[i] %= (num_of_one + 1);
mod_one[i] += mod_one[i - 1];
if (num_of_one > 1) {
mod_one[i] %= (num_of_one - 1);
}
}
}
long long m_1 = mod_zero[n - 1];
long long m_2 = mod_one[n - 1];
long long les = num_of_one - 1;
long long gre = num_of_one + 1;
long long ans[300000];
for (int i = 0; i < n; i++) {
if (x[i] == '1') {
if (les > 0) {
long long k = BigMod(i, les);
long long z = (m_2 - k) % les;
if (z < 0)
z += les;
ans[i] = 1 + f(z);
} else {
ans[i] = 0;
}
} else {
long long k = BigMod(i, gre);
long long z = (m_1 + k) % gre;
ans[i] = 1 + f(z);
}
}
for (int i = n - 1; i >= 0; i--) {
printf("%lld\n", ans[i]);
}
}
| #include <bits/stdc++.h>
using namespace std;
long long M;
long long BigMod(long long b, long long m) {
if (m == 0)
return 0;
long long a = 2;
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
long long f(long long n) {
long long step = 0;
while (__builtin_popcount(n)) {
step++;
int x = __builtin_popcount(n);
n %= x;
}
return step;
}
int main() {
int n;
scanf("%d", &n);
string x;
cin >> x;
long long num_of_one = 0;
for (int i = 0; i < x.size(); i++) {
num_of_one += x[i] == '1';
}
reverse(x.begin(), x.end());
long long mod_zero[300000];
long long mod_one[300000];
memset(mod_zero, 0, sizeof(mod_zero));
memset(mod_one, 0, sizeof(mod_one));
for (int i = 0; i < n; i++) {
if (x[i] == '1') {
mod_zero[i] = BigMod(i, num_of_one + 1);
mod_one[i] = BigMod(i, num_of_one - 1);
}
if (i > 0) {
mod_zero[i] += mod_zero[i - 1];
mod_zero[i] %= (num_of_one + 1);
mod_one[i] += mod_one[i - 1];
if (num_of_one > 1) {
mod_one[i] %= (num_of_one - 1);
}
}
}
long long m_1 = mod_zero[n - 1];
long long m_2 = mod_one[n - 1];
long long les = num_of_one - 1;
long long gre = num_of_one + 1;
long long ans[300000];
for (int i = 0; i < n; i++) {
if (x[i] == '1') {
if (les > 0) {
long long k = BigMod(i, les);
long long z = (m_2 - k) % les;
if (z < 0)
z += les;
ans[i] = 1 + f(z);
} else {
ans[i] = 0;
}
} else {
long long k = BigMod(i, gre);
long long z = (m_1 + k) % gre;
ans[i] = 1 + f(z);
}
}
for (int i = n - 1; i >= 0; i--) {
printf("%lld\n", ans[i]);
}
}
| insert | 6 | 6 | 6 | 8 | 0 | |
p02609 | C++ | Runtime Error | // In the name of God //
#include <bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
using namespace std;
const ll mod = 998244353;
int power(ll x, ll y, ll p) {
if (y == 0) {
return 1 % p;
}
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
void solve() {
int n;
cin >> n;
string t;
cin >> t;
vector<int> a(n);
int p = 0;
for (int i = 0; i < n; i++) {
a[i] = t[i] - '0';
if (a[i])
p++;
}
if (p == 0) {
for (int i = 0; i < n; i++) {
cout << 1 << "\n";
}
return;
}
// if(p == 1) {
// for (int i = 0; i < n; i++) {
// if(a[i]) {
// cout << 0 << "\n";
// } else {
// }
// }
// return;
// }
// cout << p << "\n";
// cout << power(2, 0, 2);
ll s1 = 0, s2 = 0;
vector<ll> m1(n), m2(n);
for (int i = 0; i < n; i++) {
if (p > 1)
m1[i] = power(2, n - 1 - i, p - 1);
m2[i] = power(2, n - 1 - i, p + 1);
// cout << m1[i] << " " << m2[i] << "\n";
if (a[i]) {
s1 = ((s1 % (p - 1)) + (m1[i] % (p - 1)) % (p - 1));
s2 = ((s2 % (p + 1)) + (m2[i] % (p + 1)) % (p + 1));
}
}
// cout << s1 << " " << s2 << "\n";
vector<int> ans(n, 1);
for (int i = 0; i < n; i++) {
if (a[i]) {
if (p == 1) {
ans[i] = 0;
continue;
}
int x = (s1 - m1[i] + p - 1) % (p - 1);
// cout << x << " ";
while (x) {
// cout << x << " ";
x %= __builtin_popcount(x);
ans[i]++;
}
} else {
int x = (s2 + m2[i] + p + 1) % (p + 1);
// cout << x << " ";
while (x) {
// cout << x << " ";
x %= __builtin_popcount(x);
ans[i]++;
}
}
// cout << "\n";
}
for (int i = 0; i < n; i++) {
cout << ans[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt = 1;
// cin >> tt;
for (int tc = 1; tc <= tt; ++tc) {
solve();
}
// cout << 15*modInverse(6, mod)%mod << " " << 39*modInverse(10, mod)%mod;
} | // In the name of God //
#include <bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
using namespace std;
const ll mod = 998244353;
int power(ll x, ll y, ll p) {
if (y == 0) {
return 1 % p;
}
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
void solve() {
int n;
cin >> n;
string t;
cin >> t;
vector<int> a(n);
int p = 0;
for (int i = 0; i < n; i++) {
a[i] = t[i] - '0';
if (a[i])
p++;
}
if (p == 0) {
for (int i = 0; i < n; i++) {
cout << 1 << "\n";
}
return;
}
// if(p == 1) {
// for (int i = 0; i < n; i++) {
// if(a[i]) {
// cout << 0 << "\n";
// } else {
// }
// }
// return;
// }
// cout << p << "\n";
// cout << power(2, 0, 2);
ll s1 = 0, s2 = 0;
vector<ll> m1(n), m2(n);
for (int i = 0; i < n; i++) {
if (p > 1)
m1[i] = power(2, n - 1 - i, p - 1);
m2[i] = power(2, n - 1 - i, p + 1);
// cout << m1[i] << " " << m2[i] << "\n";
if (a[i]) {
if (p > 1)
s1 = ((s1 % (p - 1)) + (m1[i] % (p - 1)) % (p - 1));
s2 = ((s2 % (p + 1)) + (m2[i] % (p + 1)) % (p + 1));
}
}
// cout << s1 << " " << s2 << "\n";
vector<int> ans(n, 1);
for (int i = 0; i < n; i++) {
if (a[i]) {
if (p == 1) {
ans[i] = 0;
continue;
}
int x = (s1 - m1[i] + p - 1) % (p - 1);
// cout << x << " ";
while (x) {
// cout << x << " ";
x %= __builtin_popcount(x);
ans[i]++;
}
} else {
int x = (s2 + m2[i] + p + 1) % (p + 1);
// cout << x << " ";
while (x) {
// cout << x << " ";
x %= __builtin_popcount(x);
ans[i]++;
}
}
// cout << "\n";
}
for (int i = 0; i < n; i++) {
cout << ans[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt = 1;
// cin >> tt;
for (int tc = 1; tc <= tt; ++tc) {
solve();
}
// cout << 15*modInverse(6, mod)%mod << " " << 39*modInverse(10, mod)%mod;
} | replace | 71 | 72 | 71 | 73 | 0 | |
p02609 | C++ | Runtime Error | #pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IINF 1000000000
#define INF 3223372036854775807
#define MOD 1000000007
// #define mod 1000000007
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) fo - r(ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
// #define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i, l, r) for (ll i = (l); i < (r); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define chmax(x, y) x = mmax(x, y)
#define chmin(x, y) x = mmin(x, y)
#define all(x) (x).begin(), (x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n, k) ((n >> k) & 1)
// #define leng length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> Pin;
typedef pair<ll, ll> Pll;
template <class T> using V = vector<T>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
ll pom(ll a, ll n, int m) {
ll x = 1;
for (a %= m; n; n /= 2)
n & 1 ? x = x * a % m : 0, a = a * a % m;
return x;
}
#define invp(a, p) pom(a, p - 2, p)
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
ll cmp1(pair<ll, ll> a, pair<ll, ll> b) {
if (a.fi != b.fi)
return a.fi < b.fi;
else
return a.se > b.se;
}
ll cmp(ll a, ll b) { return abs(a) < abs(b); }
//----------------------------------------------------------------------
ll pop_count(ll n) {
n = (n & 0x55555555) + (n >> 1 & 0x55555555);
n = (n & 0x33333333) + (n >> 2 & 0x33333333);
n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
n = (n & 0x0000ffff) + (n >> 16 & 0x0000ffff);
return n;
}
ll modpow(ll a, ll n, ll mod) { // a^n(MOD)を求める
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod; // 次の準備
n >>= 1;
}
return res;
}
ll solve(ll n) {
if (n == 0)
return 0;
else
return solve(n % pop_count(n)) + 1;
}
//----------------------------------------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
//-------------------------------
// ll begin_t=clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//------------------------------
ll n;
cin >> n;
string x;
cin >> x;
ll cnt = 0;
for (ll i = 0; i < n; i++) {
if (x[i] == '1')
cnt++;
}
ll p = cnt;
ll a1 = 0, a2 = 0;
for (ll i = 0; i < n; i++) {
if (x[i] == '1') {
a1 = (a1 + modpow(2, n - i - 1, p - 1)) % (p - 1);
a2 = (a2 + modpow(2, n - i - 1, p + 1)) % (p + 1);
}
}
for (ll i = 0; i < n; i++) {
if (x[i] == '1') {
if (cnt == 1) {
cout << 0 << Endl;
} else {
ll a11 = (a1 - modpow(2, n - i - 1, p - 1) + p - 1) % (p - 1);
cout << solve(a11) + 1 << endl;
}
} else {
ll a22 = (a2 + modpow(2, n - i - 1, p + 1)) % (p + 1);
cout << solve(a22) + 1 << endl;
}
}
//------------------------------
// fclose(stdin);
// fclose(stdout);
// ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
//-------------------------------
return 0;
}
//----------------------------------------------------------------------
| #pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IINF 1000000000
#define INF 3223372036854775807
#define MOD 1000000007
// #define mod 1000000007
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) fo - r(ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
// #define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i, l, r) for (ll i = (l); i < (r); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define chmax(x, y) x = mmax(x, y)
#define chmin(x, y) x = mmin(x, y)
#define all(x) (x).begin(), (x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n, k) ((n >> k) & 1)
// #define leng length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> Pin;
typedef pair<ll, ll> Pll;
template <class T> using V = vector<T>;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
ll pom(ll a, ll n, int m) {
ll x = 1;
for (a %= m; n; n /= 2)
n & 1 ? x = x * a % m : 0, a = a * a % m;
return x;
}
#define invp(a, p) pom(a, p - 2, p)
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
ll cmp1(pair<ll, ll> a, pair<ll, ll> b) {
if (a.fi != b.fi)
return a.fi < b.fi;
else
return a.se > b.se;
}
ll cmp(ll a, ll b) { return abs(a) < abs(b); }
//----------------------------------------------------------------------
ll pop_count(ll n) {
n = (n & 0x55555555) + (n >> 1 & 0x55555555);
n = (n & 0x33333333) + (n >> 2 & 0x33333333);
n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
n = (n & 0x0000ffff) + (n >> 16 & 0x0000ffff);
return n;
}
ll modpow(ll a, ll n, ll mod) { // a^n(MOD)を求める
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod; // 次の準備
n >>= 1;
}
return res;
}
ll solve(ll n) {
if (n == 0)
return 0;
else
return solve(n % pop_count(n)) + 1;
}
//----------------------------------------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
//-------------------------------
// ll begin_t=clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//------------------------------
ll n;
cin >> n;
string x;
cin >> x;
ll cnt = 0;
for (ll i = 0; i < n; i++) {
if (x[i] == '1')
cnt++;
}
ll p = cnt;
ll a1 = 0, a2 = 0;
for (ll i = 0; i < n; i++) {
if (x[i] == '1') {
if (p == 1)
a1 = 0;
else
a1 = (a1 + modpow(2, n - i - 1, p - 1)) % (p - 1);
a2 = (a2 + modpow(2, n - i - 1, p + 1)) % (p + 1);
}
}
for (ll i = 0; i < n; i++) {
if (x[i] == '1') {
if (cnt == 1) {
cout << 0 << Endl;
} else {
ll a11 = (a1 - modpow(2, n - i - 1, p - 1) + p - 1) % (p - 1);
cout << solve(a11) + 1 << endl;
}
} else {
ll a22 = (a2 + modpow(2, n - i - 1, p + 1)) % (p + 1);
cout << solve(a22) + 1 << endl;
}
}
//------------------------------
// fclose(stdin);
// fclose(stdout);
// ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
//-------------------------------
return 0;
}
//----------------------------------------------------------------------
| replace | 130 | 131 | 130 | 134 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int64_t> memo;
int64_t f(int64_t n) {
if (n == 0) {
return 0;
}
if (memo[n] != -1) {
return memo[n];
}
return memo[n] = (f(n % __builtin_popcountll(n)) + 1);
}
// a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
int64_t result = 1;
while (b) {
if (b % 2 == 1) {
result *= a;
result %= mod;
}
b /= 2;
a *= a;
a %= mod;
}
return result;
}
int main() {
int64_t N;
cin >> N;
memo.resize(N + 2, -1);
string X;
cin >> X;
int64_t b = count(X.begin(), X.end(), '1');
// ビット数はb or (b + 1)になる
int64_t case_b_minus_1 = 0, case_b_plus_1 = 0;
for (int64_t i = 0; i < N; i++) {
if (X[i] == '1') {
(case_b_minus_1 += MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
(case_b_plus_1 += MODpow(2, N - 1 - i, b + 1)) %= (b + 1);
}
}
for (int64_t i = 0; i < N; i++) {
// iを反転
if (X[i] == '0') {
// 1になる
int64_t v = (case_b_plus_1 + MODpow(2, N - 1 - i, b + 1)) % (b + 1);
cout << f(v) + 1 << endl;
} else {
// 0になる
int64_t v = case_b_minus_1;
// 自分の影響を除く
(v += b - 1 - MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
cout << f(v) + 1 << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
vector<int64_t> memo;
int64_t f(int64_t n) {
if (n == 0) {
return 0;
}
if (memo[n] != -1) {
return memo[n];
}
return memo[n] = (f(n % __builtin_popcountll(n)) + 1);
}
// a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
int64_t result = 1;
while (b) {
if (b % 2 == 1) {
result *= a;
result %= mod;
}
b /= 2;
a *= a;
a %= mod;
}
return result;
}
int main() {
int64_t N;
cin >> N;
memo.resize(N + 2, -1);
string X;
cin >> X;
int64_t b = count(X.begin(), X.end(), '1');
// ビット数はb or (b + 1)になる
if (b == 1) {
// b - 1が0になって終わるので適宜計算
int64_t one_index = find(X.begin(), X.end(), '1') - X.begin();
for (int64_t i = 0; i < N; i++) {
if (i == one_index) {
// 0になるので0
cout << 0 << endl;
} else {
// こことone_indexだけが立つ
int64_t v =
(MODpow(2, N - 1 - i, 2) + MODpow(2, N - 1 - one_index, 2)) % 2;
cout << f(v) + 1 << endl;
}
}
return 0;
}
int64_t case_b_minus_1 = 0, case_b_plus_1 = 0;
for (int64_t i = 0; i < N; i++) {
if (X[i] == '1') {
(case_b_minus_1 += MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
(case_b_plus_1 += MODpow(2, N - 1 - i, b + 1)) %= (b + 1);
}
}
for (int64_t i = 0; i < N; i++) {
// iを反転
if (X[i] == '0') {
// 1になる
int64_t v = (case_b_plus_1 + MODpow(2, N - 1 - i, b + 1)) % (b + 1);
cout << f(v) + 1 << endl;
} else {
// 0になる
int64_t v = case_b_minus_1;
// 自分の影響を除く
(v += b - 1 - MODpow(2, N - 1 - i, b - 1)) %= (b - 1);
cout << f(v) + 1 << endl;
}
}
} | insert | 44 | 44 | 44 | 63 | 0 | |
p02609 | C++ | Runtime Error | /*
Author: Dinesh Verra
College: ABV-IIITM
Date: 10/07/2020
*/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define cu continue
#define br break
#define pb push_back
#define eb emplace_back
// #define mod 1000000007
#define inf LLONG_MAX
#define pll pair<ll, ll>
#define min_pq priority_queue<pll>, vector<pll>, greater<pll> >>
#define F first
#define S second
#define vll vector<ll>
#define vpll vector<pll>
#define dbg(n) cout << #n << ' ' << n << endl;
#define all(v) v.begin(), v.end()
#define nl cout << '\n'
template <typename A1> void prn(A1 &&arg) { cout << arg << '\n'; }
template <typename A1, typename... A> void prn(A1 &&arg, A &&...args) {
cout << arg << ' ';
prn(args...);
}
ll mpow(ll x, ll y, ll mod) {
if (mod == 0)
return 0;
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
x = (x * x) % mod;
y >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("/home/dinesh_verra/cpp/input.txt", "r", stdin);
// freopen("/home/dinesh_verra/cpp/output.txt","w",stdout);
#endif
ll n;
cin >> n;
string s;
cin >> s;
ll x = 0;
for (ll i = 0; i < n; i++)
if (s[i] == '1')
x++;
ll a[3][n + 5], idx = -1;
for (ll i = 0; i <= n; i++)
a[0][i] = 0, a[1][i] = 0, a[2][i] = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == '1') {
a[0][i] = mpow(2ll, (n - i - 1), x - 1);
a[1][i] = mpow(2ll, (n - i - 1), x);
a[2][i] = mpow(2ll, (n - i - 1), x + 1);
idx = i;
} else {
a[0][i] = 0;
a[1][i] = 0;
a[2][i] = 0;
}
}
for (ll i = 1; i < n; i++) {
a[0][i] = (a[0][i - 1] + a[0][i]) % (x - 1);
a[1][i] = (a[1][i - 1] + a[1][i]) % (x);
a[2][i] = (a[2][i - 1] + a[2][i]) % (x + 1);
}
ll ans[n + 5], tmp;
ll b[n + 5], y;
for (ll i = 0; i < n; i++) {
b[i] = 1;
if (x == 1 && idx == i) {
b[i] = 0;
}
if (s[i] == '1' && (x != 1)) {
tmp = a[0][n - 1];
tmp = (tmp - mpow(2ll, n - i - 1, x - 1) + (x - 1)) % (x - 1);
ans[i] = tmp;
} else {
tmp = a[2][n - 1];
tmp = (tmp + mpow(2ll, n - i - 1, x + 1)) % (x + 1);
ans[i] = tmp;
}
}
for (ll i = 0; i < n; i++) {
tmp = ans[i];
while (tmp > 0) {
y = __builtin_popcountll(tmp);
tmp %= y;
b[i]++;
}
prn(b[i]);
}
} | /*
Author: Dinesh Verra
College: ABV-IIITM
Date: 10/07/2020
*/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define cu continue
#define br break
#define pb push_back
#define eb emplace_back
// #define mod 1000000007
#define inf LLONG_MAX
#define pll pair<ll, ll>
#define min_pq priority_queue<pll>, vector<pll>, greater<pll> >>
#define F first
#define S second
#define vll vector<ll>
#define vpll vector<pll>
#define dbg(n) cout << #n << ' ' << n << endl;
#define all(v) v.begin(), v.end()
#define nl cout << '\n'
template <typename A1> void prn(A1 &&arg) { cout << arg << '\n'; }
template <typename A1, typename... A> void prn(A1 &&arg, A &&...args) {
cout << arg << ' ';
prn(args...);
}
ll mpow(ll x, ll y, ll mod) {
if (mod == 0)
return 0;
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
x = (x * x) % mod;
y >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("/home/dinesh_verra/cpp/input.txt", "r", stdin);
// freopen("/home/dinesh_verra/cpp/output.txt","w",stdout);
#endif
ll n;
cin >> n;
string s;
cin >> s;
ll x = 0;
for (ll i = 0; i < n; i++)
if (s[i] == '1')
x++;
ll a[3][n + 5], idx = -1;
for (ll i = 0; i <= n; i++)
a[0][i] = 0, a[1][i] = 0, a[2][i] = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == '1') {
a[0][i] = mpow(2ll, (n - i - 1), x - 1);
a[1][i] = mpow(2ll, (n - i - 1), x);
a[2][i] = mpow(2ll, (n - i - 1), x + 1);
idx = i;
} else {
a[0][i] = 0;
a[1][i] = 0;
a[2][i] = 0;
}
}
for (ll i = 1; i < n; i++) {
a[0][i] = (a[0][i - 1] + a[0][i]);
if (x != 1)
a[0][i] %= (x - 1);
a[1][i] = (a[1][i - 1] + a[1][i]);
if (x != 0)
a[1][i] %= x;
a[2][i] = (a[2][i - 1] + a[2][i]) % (x + 1);
}
ll ans[n + 5], tmp;
ll b[n + 5], y;
for (ll i = 0; i < n; i++) {
b[i] = 1;
if (x == 1 && idx == i) {
b[i] = 0;
}
if (s[i] == '1' && (x != 1)) {
tmp = a[0][n - 1];
tmp = (tmp - mpow(2ll, n - i - 1, x - 1) + (x - 1)) % (x - 1);
ans[i] = tmp;
} else {
tmp = a[2][n - 1];
tmp = (tmp + mpow(2ll, n - i - 1, x + 1)) % (x + 1);
ans[i] = tmp;
}
}
for (ll i = 0; i < n; i++) {
tmp = ans[i];
while (tmp > 0) {
y = __builtin_popcountll(tmp);
tmp %= y;
b[i]++;
}
prn(b[i]);
}
} | replace | 80 | 82 | 80 | 86 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int N;
string X;
int modPow(int a, int n, int mod) {
if (n == 0)
return 1 % mod;
if (n == 1)
return a % mod;
if (n % 2 == 1)
return (a * modPow(a, n - 1, mod)) % mod;
int t = modPow(a, n / 2, mod);
return (t * t) % mod;
}
int calc(int x, int depth) {
int m = 0;
int d = 0;
if (x == 0)
return depth;
for (int i = 60; i >= 0; --i) {
if ((x >> i) & 1)
d++;
}
if (d == 0)
return depth;
for (int i = 60; i >= 0; --i) {
if ((x >> i) & 1) {
m += modPow(2, i, d);
m %= d;
}
}
if (m == 0)
return depth + 1;
return calc(m, depth + 1);
}
int maesyori(string x, int mod) {
int ret = 0;
int m = 0;
for (int i = x.size() - 1; i >= 0; --i) {
if (x[i] == '1') {
m += modPow(2, x.size() - 1 - i, mod);
m %= mod;
}
}
if (m == 0)
return 0;
for (int i = 60; i >= 0; --i) {
if ((m >> i) & 1)
ret += modPow(2, i, mod);
}
return ret;
}
signed main() {
cin >> N;
int d = 0;
for (int i = 0; i < N; ++i) {
char x;
cin >> x;
X += x;
if (x == '1')
d++;
}
int pls = maesyori(X, d + 1);
int mns = maesyori(X, d - 1);
for (int i = 0; i < N; ++i) {
int x;
if (X[i] == '0') {
x = pls + modPow(2, X.size() - 1 - i, d + 1);
x %= (d + 1);
// cout<<x<<endl;
cout << calc(x, 0) + 1 << endl;
} else {
if (d - 1 == 0)
cout << 0 << endl;
else {
x = mns - modPow(2, X.size() - 1 - i, d - 1);
x = (x + d - 1) % (d - 1);
// cout<<x<<endl;
cout << calc(x, 0) + 1 << endl;
}
}
}
/*int x=mns-modPow(2,1,d-1);
x*/
// cout<<calc("111",0)<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int N;
string X;
int modPow(int a, int n, int mod) {
if (n == 0)
return 1 % mod;
if (n == 1)
return a % mod;
if (n % 2 == 1)
return (a * modPow(a, n - 1, mod)) % mod;
int t = modPow(a, n / 2, mod);
return (t * t) % mod;
}
int calc(int x, int depth) {
int m = 0;
int d = 0;
if (x == 0)
return depth;
for (int i = 60; i >= 0; --i) {
if ((x >> i) & 1)
d++;
}
if (d == 0)
return depth;
for (int i = 60; i >= 0; --i) {
if ((x >> i) & 1) {
m += modPow(2, i, d);
m %= d;
}
}
if (m == 0)
return depth + 1;
return calc(m, depth + 1);
}
int maesyori(string x, int mod) {
int ret = 0;
int m = 0;
for (int i = x.size() - 1; i >= 0; --i) {
if (x[i] == '1') {
m += modPow(2, x.size() - 1 - i, mod);
m %= mod;
}
}
if (m == 0)
return 0;
for (int i = 60; i >= 0; --i) {
if ((m >> i) & 1)
ret += modPow(2, i, mod);
}
return ret;
}
signed main() {
cin >> N;
int d = 0;
for (int i = 0; i < N; ++i) {
char x;
cin >> x;
X += x;
if (x == '1')
d++;
}
int pls = maesyori(X, d + 1);
int mns = maesyori(X, max(d - 1, 1ll));
for (int i = 0; i < N; ++i) {
int x;
if (X[i] == '0') {
x = pls + modPow(2, X.size() - 1 - i, d + 1);
x %= (d + 1);
// cout<<x<<endl;
cout << calc(x, 0) + 1 << endl;
} else {
if (d - 1 == 0)
cout << 0 << endl;
else {
x = mns - modPow(2, X.size() - 1 - i, d - 1);
x = (x + d - 1) % (d - 1);
// cout<<x<<endl;
cout << calc(x, 0) + 1 << endl;
}
}
}
/*int x=mns-modPow(2,1,d-1);
x*/
// cout<<calc("111",0)<<endl;
} | replace | 99 | 100 | 99 | 100 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define IO \
{ \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
}
#define ll long long
#define pi pair<ll, ll>
ll a[2000000], b[300000];
string s;
ll f(ll n, ll mod, ll b) {
ll ans = 1, aa;
if (!b)
return 1;
if (b % 2)
ans = n;
aa = f(n, mod, b / 2);
return ((aa * aa) % mod * ans) % mod;
}
ll g(ll n) {
ll cnt, ans = 0;
while (n) {
cnt = __builtin_popcount(n);
n %= cnt;
ans++;
}
return ans;
}
int main() {
IO;
ll i, j, k, l = 0, n, m, h, mod = 1e9 + 7, cnt = 0, mx = 0, t, ans = 0,
tc = 0, r, l1, l2 = 0;
cin >> n >> s;
for (i = 1; i <= n; i++) {
if (s[i - 1] == '1')
cnt++;
}
l1 = 0;
for (i = 1; i <= n; i++) {
if (s[i - 1] == '1')
l1 = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
}
if (cnt)
for (i = 1; i <= n; i++) {
if (s[i - 1] == '1')
l2 = (l2 + f(2, cnt - 1, n - i)) % (cnt - 1);
}
for (i = 1; i <= n; i++) {
if (s[i - 1] == '0') {
a[i] = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
b[i] = 1;
} else {
if (cnt != 1) {
a[i] = (l2 - f(2, cnt - 1, n - i)) % (cnt - 1);
b[i] = 1;
if (a[i] < 0)
a[i] += cnt - 1;
}
}
}
for (i = 1; i <= n; i++) {
b[i] += g(a[i]);
}
for (i = 1; i <= n; i++)
cout << b[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define IO \
{ \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
}
#define ll long long
#define pi pair<ll, ll>
ll a[2000000], b[300000];
string s;
ll f(ll n, ll mod, ll b) {
ll ans = 1, aa;
if (!b)
return 1;
if (b % 2)
ans = n;
aa = f(n, mod, b / 2);
return ((aa * aa) % mod * ans) % mod;
}
ll g(ll n) {
ll cnt, ans = 0;
while (n) {
cnt = __builtin_popcount(n);
n %= cnt;
ans++;
}
return ans;
}
int main() {
IO;
ll i, j, k, l = 0, n, m, h, mod = 1e9 + 7, cnt = 0, mx = 0, t, ans = 0,
tc = 0, r, l1, l2 = 0;
cin >> n >> s;
for (i = 1; i <= n; i++) {
if (s[i - 1] == '1')
cnt++;
}
l1 = 0;
for (i = 1; i <= n; i++) {
if (s[i - 1] == '1')
l1 = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
}
if (cnt > 1)
for (i = 1; i <= n; i++) {
if (s[i - 1] == '1')
l2 = (l2 + f(2, cnt - 1, n - i)) % (cnt - 1);
}
for (i = 1; i <= n; i++) {
if (s[i - 1] == '0') {
a[i] = (l1 + f(2, cnt + 1, n - i)) % (cnt + 1);
b[i] = 1;
} else {
if (cnt != 1) {
a[i] = (l2 - f(2, cnt - 1, n - i)) % (cnt - 1);
b[i] = 1;
if (a[i] < 0)
a[i] += cnt - 1;
}
}
}
for (i = 1; i <= n; i++) {
b[i] += g(a[i]);
}
for (i = 1; i <= n; i++)
cout << b[i] << endl;
return 0;
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = int;
using Graph = vector<vector<Edge>>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = 1e13;
const int MAX = 510000;
bool graph[nmax][nmax];
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
struct SegmentTree {
private:
ll n;
vector<ll> node;
public:
SegmentTree(vector<ll> v) {
ll sz = v.size();
n = 1;
while (n < sz) {
n *= 2;
}
node.resize(2 * n - 1, INF);
for (ll i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
for (ll i = n - 2; i >= 0; i--) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(ll x, ll val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
// findは半開区間で考える
ll find(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return node[k];
ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
void warshall_floyd(ll n) {
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
for (size_t k = 0; k < n; k++) {
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
}
}
}
}
class UnionFind {
public:
vector<ll> Parent;
UnionFind(ll N) { Parent = vector<ll>(N, -1); }
ll find(ll A) {
if (Parent[A] < 0)
return A;
return Parent[A] = find(Parent[A]);
}
ll size(ll A) { return -Parent[find(A)]; }
bool Union(ll A, ll B) {
A = find(A);
B = find(B);
if (A == B) {
return false;
}
if (size(A) < size(B))
swap(A, B);
Parent[A] += Parent[B];
Parent[B] = A;
return true;
}
};
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll mulMod(ll a, ll b, ll MOD) { return (((a % MOD) * (b % MOD)) % MOD); }
ll powMod(ll a, ll p, ll MOD) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
ll half = powMod(a, p / 2, MOD);
return mulMod(half, half, MOD);
} else {
return mulMod(powMod(a, p - 1, MOD), a, MOD);
}
}
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
vector<ll> tsort(Graph G) {
ll N = G.size();
vector<ll> in(N);
for (auto &&edges : G) {
for (auto &&edge : edges) {
in[edge]++;
}
}
queue<int> que;
for (int i = 0; i < N; i++) {
if (in[i] == 0) {
que.push(i);
}
}
int cnt = 0;
vector<ll> ans;
while (!que.empty()) {
int v = que.front();
que.pop();
ans.push_back(v);
for (auto &&next : G[v]) {
in[next]--;
if (in[next] == 0) {
que.push(next);
}
}
}
return ans;
}
void solve(long long N, std::string X) {
ll popcount = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '1')
popcount++;
}
ll popplus = popcount + 1;
ll popminus = popcount - 1;
ll originplus = 0, originminus = 0;
ll cnt_one = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '1') {
cnt_one++;
if (originplus <= N) {
originplus += powMod(2, N - i - 1, popplus);
originplus %= popplus;
}
if (originminus >= 0) {
originminus += powMod(2, N - i - 1, popminus);
originminus %= popminus;
}
}
}
for (int i = 0; i < N; i++) {
ll cnt = 1;
ll f = 0;
if (X[i] == '1') {
if (cnt_one == 1) {
cout << 0 << endl;
continue;
}
f = originminus - powMod(2, N - i - 1, popminus);
f %= popminus;
if (f < 0)
f += popminus;
while (f != 0) {
cnt++;
ll popc = bitset<64>(f).count();
f = f % popc;
if (f < 0)
f += popc;
}
cout << cnt << endl;
} else {
f = originplus + powMod(2, N - i - 1, popplus);
f %= popplus;
if (f < 0)
f += popplus;
while (f != 0) {
cnt++;
ll popc = bitset<64>(f).count();
f = f % popc;
if (f < 0)
f += popc;
}
cout << cnt << endl;
}
}
}
int main() {
long long N;
scanf("%lld", &N);
std::string X;
std::cin >> X;
solve(N, X);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = int;
using Graph = vector<vector<Edge>>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = 1e13;
const int MAX = 510000;
bool graph[nmax][nmax];
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
struct SegmentTree {
private:
ll n;
vector<ll> node;
public:
SegmentTree(vector<ll> v) {
ll sz = v.size();
n = 1;
while (n < sz) {
n *= 2;
}
node.resize(2 * n - 1, INF);
for (ll i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
for (ll i = n - 2; i >= 0; i--) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(ll x, ll val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
// findは半開区間で考える
ll find(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return node[k];
ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
void warshall_floyd(ll n) {
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
for (size_t k = 0; k < n; k++) {
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
}
}
}
}
class UnionFind {
public:
vector<ll> Parent;
UnionFind(ll N) { Parent = vector<ll>(N, -1); }
ll find(ll A) {
if (Parent[A] < 0)
return A;
return Parent[A] = find(Parent[A]);
}
ll size(ll A) { return -Parent[find(A)]; }
bool Union(ll A, ll B) {
A = find(A);
B = find(B);
if (A == B) {
return false;
}
if (size(A) < size(B))
swap(A, B);
Parent[A] += Parent[B];
Parent[B] = A;
return true;
}
};
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll mulMod(ll a, ll b, ll MOD) { return (((a % MOD) * (b % MOD)) % MOD); }
ll powMod(ll a, ll p, ll MOD) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
ll half = powMod(a, p / 2, MOD);
return mulMod(half, half, MOD);
} else {
return mulMod(powMod(a, p - 1, MOD), a, MOD);
}
}
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
vector<ll> tsort(Graph G) {
ll N = G.size();
vector<ll> in(N);
for (auto &&edges : G) {
for (auto &&edge : edges) {
in[edge]++;
}
}
queue<int> que;
for (int i = 0; i < N; i++) {
if (in[i] == 0) {
que.push(i);
}
}
int cnt = 0;
vector<ll> ans;
while (!que.empty()) {
int v = que.front();
que.pop();
ans.push_back(v);
for (auto &&next : G[v]) {
in[next]--;
if (in[next] == 0) {
que.push(next);
}
}
}
return ans;
}
void solve(long long N, std::string X) {
ll popcount = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '1')
popcount++;
}
ll popplus = popcount + 1;
ll popminus = popcount - 1;
ll originplus = 0, originminus = 0;
ll cnt_one = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '1') {
cnt_one++;
originplus += powMod(2, N - i - 1, popplus);
originplus %= popplus;
if (popminus != 0) {
originminus += powMod(2, N - i - 1, popminus);
originminus %= popminus;
}
}
}
for (int i = 0; i < N; i++) {
ll cnt = 1;
ll f = 0;
if (X[i] == '1') {
if (cnt_one == 1) {
cout << 0 << endl;
continue;
}
f = originminus - powMod(2, N - i - 1, popminus);
f %= popminus;
if (f < 0)
f += popminus;
while (f != 0) {
cnt++;
ll popc = bitset<64>(f).count();
f = f % popc;
if (f < 0)
f += popc;
}
cout << cnt << endl;
} else {
f = originplus + powMod(2, N - i - 1, popplus);
f %= popplus;
if (f < 0)
f += popplus;
while (f != 0) {
cnt++;
ll popc = bitset<64>(f).count();
f = f % popc;
if (f < 0)
f += popc;
}
cout << cnt << endl;
}
}
}
int main() {
long long N;
scanf("%lld", &N);
std::string X;
std::cin >> X;
solve(N, X);
return 0;
}
| replace | 195 | 200 | 195 | 199 | 0 | |
p02609 | C++ | Runtime Error | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define bit(n, k) (((ll)n >> (ll)k) & 1) /*nのk bit目*/
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;
// const ll MAX_V=114514LL;
// const ll MAX = 500010LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {0, 0, 1, -1};
vl dy = {1, -1, 0, 0};
// 素因数分解O(√n)
map<ll, ll> prime_factor(ll n) {
map<ll, ll> res;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
res[i]++;
n /= i;
}
}
if (n != 1)
res[n] = 1;
return res;
}
const ll MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
// finvが階乗の逆元
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(10);
/*--------------------------------*/
ll n;
cin >> n;
string s;
cin >> s;
ll one = 0;
rep(i, n) {
if (s[i] == '1')
one++;
}
vl pows_minus(n);
vl pows_plus(n);
pows_minus[0] = 1, pows_plus[0] = 1;
for (ll i = 1; i < n; i++) {
if (one > 1)
pows_minus[i] = pows_minus[i - 1] * 2 % (one - 1);
pows_plus[i] = pows_plus[i - 1] * 2 % (one + 1);
}
vl x(n);
ll x_plus = 0, x_minus = 0;
for (ll i = 0; i < n; i++) {
x_plus += ((s[n - 1 - i] - '0') * pows_plus[i]) % (one + 1);
x_plus %= one + 1;
if (one > 1)
x_minus += ((s[n - 1 - i] - '0') * pows_minus[i]) % (one - 1);
x_minus %= (one - 1);
}
rep(i, n) {
if (s[i] == '1') {
if (one == 1) {
cout << 0 << endl;
continue;
}
ll nx = (x_minus + (one - 1 - pows_minus[n - 1 - i])) % (one - 1);
ll ans = 1;
while (nx > 0) {
nx = (nx % __builtin_popcount(nx));
ans++;
}
cout << ans << endl;
}
if (s[i] == '0') {
// cout<<x_plus<<" "<<pows_minus[n-1-i]<<" "<<one+1<<endl;
ll nx = (x_plus + pows_plus[n - 1 - i]) % (one + 1);
ll ans = 1;
while (nx > 0) {
nx = (nx % __builtin_popcount(nx));
ans++;
}
cout << ans << endl;
}
}
// cout<<x_plus<<endl;
}
| // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define bit(n, k) (((ll)n >> (ll)k) & 1) /*nのk bit目*/
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;
// const ll MAX_V=114514LL;
// const ll MAX = 500010LL;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {0, 0, 1, -1};
vl dy = {1, -1, 0, 0};
// 素因数分解O(√n)
map<ll, ll> prime_factor(ll n) {
map<ll, ll> res;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
res[i]++;
n /= i;
}
}
if (n != 1)
res[n] = 1;
return res;
}
const ll MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
// finvが階乗の逆元
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(10);
/*--------------------------------*/
ll n;
cin >> n;
string s;
cin >> s;
ll one = 0;
rep(i, n) {
if (s[i] == '1')
one++;
}
vl pows_minus(n);
vl pows_plus(n);
pows_minus[0] = 1, pows_plus[0] = 1;
for (ll i = 1; i < n; i++) {
if (one > 1)
pows_minus[i] = pows_minus[i - 1] * 2 % (one - 1);
pows_plus[i] = pows_plus[i - 1] * 2 % (one + 1);
}
vl x(n);
ll x_plus = 0, x_minus = 0;
for (ll i = 0; i < n; i++) {
x_plus += ((s[n - 1 - i] - '0') * pows_plus[i]) % (one + 1);
x_plus %= one + 1;
if (one > 1)
x_minus += ((s[n - 1 - i] - '0') * pows_minus[i]) % (one - 1);
if (one > 1)
x_minus %= (one - 1);
}
rep(i, n) {
if (s[i] == '1') {
if (one == 1) {
cout << 0 << endl;
continue;
}
ll nx = (x_minus + (one - 1 - pows_minus[n - 1 - i])) % (one - 1);
ll ans = 1;
while (nx > 0) {
nx = (nx % __builtin_popcount(nx));
ans++;
}
cout << ans << endl;
}
if (s[i] == '0') {
// cout<<x_plus<<" "<<pows_minus[n-1-i]<<" "<<one+1<<endl;
ll nx = (x_plus + pows_plus[n - 1 - i]) % (one + 1);
ll ans = 1;
while (nx > 0) {
nx = (nx % __builtin_popcount(nx));
ans++;
}
cout << ans << endl;
}
}
// cout<<x_plus<<endl;
}
| replace | 109 | 110 | 109 | 111 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define dcout cout << fixed << setprecision(15);
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;
template <typename S, typename T> inline void chmax(S &a, const T &b) {
if (a < b)
a = b;
}
template <typename S, typename T> inline void chmin(S &a, const T &b) {
if (a > b)
a = b;
}
// lcm//
ll gcd(ll x, ll y) {
if (x == 0)
return y;
return gcd(y % x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
// a^n mod p//
ll modpow(ll a, ll n, ll p) {
if (n == 0)
return (ll)1;
if (n == 1)
return a % p;
if (n % 2 == 1)
return (a * modpow(a, n - 1, p)) % p;
ll t = modpow(a, n / 2, p);
return (t * t) % p;
}
// inversemod//
ll modinv(ll a, ll m) {
if (m == 0)
return (ll)1;
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];
//
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
//
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// LARGE n
ll com(ll n, ll m) {
if (n < m || n <= 0 || m < 0) {
return 0;
}
if (m == 0 || n == m) {
return 1;
}
ll k = 1;
for (ll i = 1; i <= m; i++) {
k *= (n - i + 1);
k %= MOD;
k *= modinv(i, MOD);
k %= MOD;
}
return k;
}
// radP
ll rad(ll u, ll p) {
ll cnt = 0;
while (u % p == 0) {
u /= p;
cnt++;
}
return cnt;
}
////////////////////////////////////////////////////////////////////
ll pow2[24];
ll kekka[1000];
ll res;
ll ok;
void count(ll p, ll kai) {
if (p == 0) {
res = kai;
return;
}
if (ok && p <= 999) {
res = kai + kekka[p];
return;
}
ll keta = 0;
for (ll i = 0; i <= 23; i++) {
ll val = pow2[i];
if (p & (ll)1 << i)
keta++;
}
count(p % keta, kai + 1);
return;
}
int main() {
ios::sync_with_stdio(false);
ok = 0;
res = 0;
pow2[0] = 1;
for (int i = 1; i <= 23; i++) {
pow2[i] = (ll)2 * pow2[i - 1];
}
ll n;
cin >> n;
string s;
cin >> s;
for (ll i = 1; i <= 999; i++) {
res = 0;
count(i, 0);
kekka[i] = res;
}
res = 0;
ok = 1;
ll cnt1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
cnt1++;
}
ll ans[n];
rep(i, n) { ans[i - 1] = 0; }
ll rem[3][n];
rem[0][n - 1] = 1;
rem[1][n - 1] = 1;
rem[2][n - 1] = 1;
for (int i = 0; i <= 2; i++) {
for (int j = n - 2; j >= 0; j--) {
ll p = cnt1 + i - 1;
rem[i][j] = (rem[i][j + 1] * 2) % p;
}
}
ll r0wa = 0;
ll r1wa = 0;
ll r2wa = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
r0wa += rem[0][i];
if (cnt1 >= 2) {
r0wa %= cnt1 - 1;
}
r1wa += rem[1][i];
r1wa %= cnt1;
r2wa += rem[2][i];
r2wa %= cnt1 + 1;
}
}
for (int i = 0; i < n; i++) {
res = 0;
ll val;
if (s[i] == '0') {
val = r2wa;
val += rem[2][i];
val %= cnt1 + 1;
count(val, 1);
}
if (s[i] == '1') {
val = r0wa;
val -= rem[0][i];
val += cnt1 * 2 - 2;
if (cnt1 == 1) {
ans[i] = 0;
continue;
}
val %= cnt1 - 1;
count(val, 1);
}
ans[i] = res;
}
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define dcout cout << fixed << setprecision(15);
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;
template <typename S, typename T> inline void chmax(S &a, const T &b) {
if (a < b)
a = b;
}
template <typename S, typename T> inline void chmin(S &a, const T &b) {
if (a > b)
a = b;
}
// lcm//
ll gcd(ll x, ll y) {
if (x == 0)
return y;
return gcd(y % x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
// a^n mod p//
ll modpow(ll a, ll n, ll p) {
if (n == 0)
return (ll)1;
if (n == 1)
return a % p;
if (n % 2 == 1)
return (a * modpow(a, n - 1, p)) % p;
ll t = modpow(a, n / 2, p);
return (t * t) % p;
}
// inversemod//
ll modinv(ll a, ll m) {
if (m == 0)
return (ll)1;
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];
//
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
//
ll COM(ll n, ll k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// LARGE n
ll com(ll n, ll m) {
if (n < m || n <= 0 || m < 0) {
return 0;
}
if (m == 0 || n == m) {
return 1;
}
ll k = 1;
for (ll i = 1; i <= m; i++) {
k *= (n - i + 1);
k %= MOD;
k *= modinv(i, MOD);
k %= MOD;
}
return k;
}
// radP
ll rad(ll u, ll p) {
ll cnt = 0;
while (u % p == 0) {
u /= p;
cnt++;
}
return cnt;
}
////////////////////////////////////////////////////////////////////
ll pow2[24];
ll kekka[1000];
ll res;
ll ok;
void count(ll p, ll kai) {
if (p == 0) {
res = kai;
return;
}
if (ok && p <= 999) {
res = kai + kekka[p];
return;
}
ll keta = 0;
for (ll i = 0; i <= 23; i++) {
ll val = pow2[i];
if (p & (ll)1 << i)
keta++;
}
count(p % keta, kai + 1);
return;
}
int main() {
ios::sync_with_stdio(false);
ok = 0;
res = 0;
pow2[0] = 1;
for (int i = 1; i <= 23; i++) {
pow2[i] = (ll)2 * pow2[i - 1];
}
ll n;
cin >> n;
string s;
cin >> s;
for (ll i = 1; i <= 999; i++) {
res = 0;
count(i, 0);
kekka[i] = res;
}
res = 0;
ok = 1;
ll cnt1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
cnt1++;
}
ll ans[n];
rep(i, n) { ans[i - 1] = 0; }
ll rem[3][n];
rem[0][n - 1] = 1;
rem[1][n - 1] = 1;
rem[2][n - 1] = 1;
for (int i = 0; i <= 2; i++) {
for (int j = n - 2; j >= 0; j--) {
ll p = cnt1 + i - 1;
if (p >= 1) {
rem[i][j] = (rem[i][j + 1] * 2) % p;
}
}
}
ll r0wa = 0;
ll r1wa = 0;
ll r2wa = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
r0wa += rem[0][i];
if (cnt1 >= 2) {
r0wa %= cnt1 - 1;
}
r1wa += rem[1][i];
r1wa %= cnt1;
r2wa += rem[2][i];
r2wa %= cnt1 + 1;
}
}
for (int i = 0; i < n; i++) {
res = 0;
ll val;
if (s[i] == '0') {
val = r2wa;
val += rem[2][i];
val %= cnt1 + 1;
count(val, 1);
}
if (s[i] == '1') {
val = r0wa;
val -= rem[0][i];
val += cnt1 * 2 - 2;
if (cnt1 == 1) {
ans[i] = 0;
continue;
}
val %= cnt1 - 1;
count(val, 1);
}
ans[i] = res;
}
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 180 | 181 | 180 | 183 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int pcount(int x) { return __builtin_popcount(x); }
int f(int x) {
int ans = 1;
int tmp;
if (x == 0) {
return ans;
}
while (x) {
tmp = pcount(x);
x %= tmp;
ans++;
}
return ans;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
int p1 = 0;
int p2, p3;
vector<int> amari2(n);
vector<int> amari3(n);
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
p1++;
}
}
p2 = p1 + 1;
p3 = p1 - 1;
for (int i = 0; i < n; i++) {
if (i == 0) {
amari2.at(i) = 1 % p2;
amari3.at(i) = 1 % p3;
} else {
amari2.at(i) = amari2.at(i - 1) * 2 % p2;
amari3.at(i) = amari3.at(i - 1) * 2 % p3;
}
}
int x02 = 0;
int x03 = 0;
if (p2 != 0 && p3 != 0) {
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
x02 += amari2.at(i);
x02 %= p2;
x03 += amari3.at(i);
x03 %= p3;
}
}
} else if (p2 == 0 && p3 != 0) {
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
x03 += amari3.at(i);
x03 %= p3;
}
}
x02 = 0;
} else if (p3 == 0 && p2 != 0) {
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
x02 += amari2.at(i);
x02 %= p2;
}
}
x03 = 0;
}
for (int i = n - 1; i >= 0; i--) {
int x = 0;
if (s.at(i) == '1') {
bool zero = true;
for (int j = 0; j < n; j++) {
if (j == i)
continue;
if (s.at(j) == '1') {
zero = false;
break;
}
}
if (zero) {
cout << 0 << endl;
continue;
}
x += x03;
x -= amari3.at(i);
if (x < 0) {
x += p3;
}
x %= p3;
if (x == 0) {
cout << 1 << endl;
} else {
int anss = f(x);
cout << anss << endl;
}
} else if (s.at(i) == '0') {
x += x02;
x += amari2.at(i);
x %= p2;
if (x == 0) {
cout << 1 << endl;
} else {
int anss = f(x);
cout << anss << endl;
}
}
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int pcount(int x) { return __builtin_popcount(x); }
int f(int x) {
int ans = 1;
int tmp;
if (x == 0) {
return ans;
}
while (x) {
tmp = pcount(x);
x %= tmp;
ans++;
}
return ans;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
int p1 = 0;
int p2, p3;
vector<int> amari2(n);
vector<int> amari3(n);
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
p1++;
}
}
p2 = p1 + 1;
p3 = p1 - 1;
if (p3 != 0) {
for (int i = 0; i < n; i++) {
if (i == 0) {
amari2.at(i) = 1 % p2;
amari3.at(i) = 1 % p3;
} else {
amari2.at(i) = amari2.at(i - 1) * 2 % p2;
amari3.at(i) = amari3.at(i - 1) * 2 % p3;
}
}
} else {
for (int i = 0; i < n; i++) {
if (i == 0) {
amari2.at(i) = 1 % p2;
amari3.at(i) = 0;
} else {
amari2.at(i) = amari2.at(i - 1) * 2 % p2;
amari3.at(i) = 0;
}
}
}
int x02 = 0;
int x03 = 0;
if (p2 != 0 && p3 != 0) {
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
x02 += amari2.at(i);
x02 %= p2;
x03 += amari3.at(i);
x03 %= p3;
}
}
} else if (p2 == 0 && p3 != 0) {
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
x03 += amari3.at(i);
x03 %= p3;
}
}
x02 = 0;
} else if (p3 == 0 && p2 != 0) {
for (int i = 0; i < n; i++) {
if (s.at(i) == '1') {
x02 += amari2.at(i);
x02 %= p2;
}
}
x03 = 0;
}
for (int i = n - 1; i >= 0; i--) {
int x = 0;
if (s.at(i) == '1') {
bool zero = true;
for (int j = 0; j < n; j++) {
if (j == i)
continue;
if (s.at(j) == '1') {
zero = false;
break;
}
}
if (zero) {
cout << 0 << endl;
continue;
}
x += x03;
x -= amari3.at(i);
if (x < 0) {
x += p3;
}
x %= p3;
if (x == 0) {
cout << 1 << endl;
} else {
int anss = f(x);
cout << anss << endl;
}
} else if (s.at(i) == '0') {
x += x02;
x += amari2.at(i);
x %= p2;
if (x == 0) {
cout << 1 << endl;
} else {
int anss = f(x);
cout << anss << endl;
}
}
}
} | replace | 37 | 44 | 37 | 56 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
ll inf = 1e9;
ll func(ll v) {
ll cnt = 0;
ll ans = 0;
while (v != 0) {
cnt = 0;
for (ll i = 0; i < 64; i++)
if ((v >> i) & 1)
cnt++;
v %= cnt;
ans++;
}
return ans;
}
int main() {
int n;
string s;
cin >> n >> s;
ll cnt = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == '1')
cnt++;
}
ll vp = 0, vu = 0;
vector<ll> bp(n, 1), bu(n, 1);
for (ll i = n - 1; i >= 1; i--) {
bp[i - 1] = (bp[i] * 2) % (cnt + 1);
bu[i - 1] = (bu[i] * 2) % (cnt - 1);
}
for (ll i = n - 1; i >= 0; i--) {
// cout<<i<<' '<<"OK"<<endl;
if (s[i] == '1') {
vp += bp[i];
vp %= cnt + 1;
if (cnt - 1 != 0) {
vu += bu[i];
vu %= cnt - 1;
}
}
}
for (ll i = 0; i < n; i++) {
if (s[i] == '1') {
ll c = cnt - 1;
if (c == 0) {
cout << c << endl;
continue;
}
ll v = (vu - bu[i] + c) % c;
cout << func(v) + 1 << endl;
} else {
ll c = cnt + 1;
ll v = (vp + bp[i]) % c;
cout << func(v) + 1 << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
ll inf = 1e9;
ll func(ll v) {
ll cnt = 0;
ll ans = 0;
while (v != 0) {
cnt = 0;
for (ll i = 0; i < 64; i++)
if ((v >> i) & 1)
cnt++;
v %= cnt;
ans++;
}
return ans;
}
int main() {
int n;
string s;
cin >> n >> s;
ll cnt = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == '1')
cnt++;
}
ll vp = 0, vu = 0;
vector<ll> bp(n, 1), bu(n, 1);
for (ll i = n - 1; i >= 1; i--) {
bp[i - 1] = (bp[i] * 2) % (cnt + 1);
if (cnt - 1 != 0)
bu[i - 1] = (bu[i] * 2) % (cnt - 1);
}
for (ll i = n - 1; i >= 0; i--) {
// cout<<i<<' '<<"OK"<<endl;
if (s[i] == '1') {
vp += bp[i];
vp %= cnt + 1;
if (cnt - 1 != 0) {
vu += bu[i];
vu %= cnt - 1;
}
}
}
for (ll i = 0; i < n; i++) {
if (s[i] == '1') {
ll c = cnt - 1;
if (c == 0) {
cout << c << endl;
continue;
}
ll v = (vu - bu[i] + c) % c;
cout << func(v) + 1 << endl;
} else {
ll c = cnt + 1;
ll v = (vp + bp[i]) % c;
cout << func(v) + 1 << endl;
}
}
return 0;
}
| replace | 34 | 35 | 34 | 36 | 0 | |
p02609 | C++ | Runtime Error | /* -*- coding: utf-8 -*-
*
* d.cc: D - Anything Goes to Zero
*/
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/* constant */
const int MAX_N = 200000;
/* typedef */
typedef long long ll;
/* global variables */
char s[MAX_N + 4];
int bnums[MAX_N + 1], fs[MAX_N + 1];
int bns[2], rs[2];
/* subroutines */
int powmod(int a, int n, int mod) { // a^n % MOD
int pm = 1;
while (n > 0) {
if (n & 1)
pm = (ll)pm * a % mod;
a = (ll)a * a % mod;
n >>= 1;
}
return pm;
}
/* main */
int main() {
for (int bits = 1, msb = 1; bits <= MAX_N; bits++) {
if ((msb << 1) <= bits)
msb <<= 1;
bnums[bits] = bnums[bits ^ msb] + 1;
fs[bits] = fs[bits % bnums[bits]] + 1;
}
int n;
scanf("%d%s", &n, s);
int bn = 0;
for (int i = 0; i < n; i++)
bn += s[i] - '0';
bns[0] = bn - 1, bns[1] = bn + 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
if (bns[j] > 0)
rs[j] = (rs[j] * 2 + s[i] - '0') % bns[j];
for (int i = 0; i < n; i++) {
int j = '1' - s[i];
int p = powmod(2, n - 1 - i, bns[j]);
if (j == 0)
p = (bns[j] - p) % bns[j];
int r = (bns[j] == 0) ? 0 : (rs[j] + p) % bns[j];
printf("%d\n", fs[r] + 1);
// printf("i=%d, j=%d, r=%d, f=%d\n", i, j, r, fs[r]);
}
return 0;
}
| /* -*- coding: utf-8 -*-
*
* d.cc: D - Anything Goes to Zero
*/
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/* constant */
const int MAX_N = 200000;
/* typedef */
typedef long long ll;
/* global variables */
char s[MAX_N + 4];
int bnums[MAX_N + 1], fs[MAX_N + 1];
int bns[2], rs[2];
/* subroutines */
int powmod(int a, int n, int mod) { // a^n % MOD
int pm = 1;
while (n > 0) {
if (n & 1)
pm = (ll)pm * a % mod;
a = (ll)a * a % mod;
n >>= 1;
}
return pm;
}
/* main */
int main() {
for (int bits = 1, msb = 1; bits <= MAX_N; bits++) {
if ((msb << 1) <= bits)
msb <<= 1;
bnums[bits] = bnums[bits ^ msb] + 1;
fs[bits] = fs[bits % bnums[bits]] + 1;
}
int n;
scanf("%d%s", &n, s);
int bn = 0;
for (int i = 0; i < n; i++)
bn += s[i] - '0';
bns[0] = bn - 1, bns[1] = bn + 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
if (bns[j] > 0)
rs[j] = (rs[j] * 2 + s[i] - '0') % bns[j];
for (int i = 0; i < n; i++) {
int j = '1' - s[i];
int f = 0;
if (bns[j] > 0) {
int p = powmod(2, n - 1 - i, bns[j]);
if (j == 0)
p = (bns[j] - p) % bns[j];
f = fs[(rs[j] + p) % bns[j]] + 1;
}
printf("%d\n", f);
// printf("i=%d, j=%d, r=%d, f=%d\n", i, j, r, fs[r]);
}
return 0;
}
| replace | 78 | 83 | 78 | 86 | 0 | |
p02609 | C++ | Runtime Error | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author Ido Kessler
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cmath>
#include <random>
#include <tuple>
using ll = long long int;
using ull = unsigned long long int;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using pi = std::pair<int, int>;
using vpi = std::vector<pi>;
using vvpi = std::vector<vpi>;
using pll = std::pair<ll, ll>;
using vpll = std::vector<pll>;
using vvpll = std::vector<vpll>;
#define all(x) (x).begin(), (x).end()
#define fori(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, e) for (int i = (int)(s); i <= (int)(e); ++i)
#define repr(i, s, e) for (int i = (int)(s); i >= (int)(e); --i)
#define pb push_back
template <typename T> void mk_unique(std::vector<T> &l) {
if (!std::is_sorted(all(l)))
std::sort(all(l));
auto it = std::unique(l.begin(), l.end());
auto d = std::distance(l.begin(), it);
l.resize(d);
}
template <typename T> T &smax(T &l, const T &r) {
if (l < r)
l = r;
return l;
}
template <typename T> T &smin(T &l, const T &r) {
if (r < l)
l = r;
return l;
}
constexpr int MOD = 1000000007; // 998244353;//1000000007;
namespace io {
class input_reader {
void throw_error() {
throw std::runtime_error("No more inputs while reading number");
}
template <typename T> inline void readIntegerValue(T &f) {
bool positive = true;
f = 0;
char c;
if (!in.get(c))
throw_error();
while (c < '0' || c > '9') {
if (c == '-')
positive = false;
if (!in.get(c))
throw_error();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + (c & 15);
if (!in.get(c))
break;
}
if (!positive)
f *= -1;
}
public:
std::istream ∈
explicit input_reader(std::istream &in) : in(in) {}
inline void read(int &f) { readIntegerValue(f); }
inline void read(short &f) { readIntegerValue(f); }
inline void read(long int &f) { readIntegerValue(f); }
inline void read(long long int &f) { readIntegerValue(f); }
inline void read(unsigned int &f) { readIntegerValue(f); }
inline void read(unsigned short &f) { readIntegerValue(f); }
inline void read(unsigned long int &f) { readIntegerValue(f); }
inline void read(unsigned long long int &f) { readIntegerValue(f); }
template <typename T> inline void read(std::vector<T> &f) {
for (auto &i : f)
read(i);
}
template <typename T, int N> inline void read(std::array<T, N> &res) {
for (auto &i : res)
read(i);
}
template <typename T1, typename T2> inline void read(std::pair<T1, T2> &p) {
read(p.first, p.second);
}
template <typename T> inline void read(T &f) { in >> f; }
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
read(t), read(args...);
}
template <typename T> inline void readArray(int n, T *res) {
while (n--)
read(*(res++));
}
template <typename T> inline input_reader &operator>>(T &t) {
return read(t), *this;
}
};
} // namespace io
namespace io {
class output_writer {
std::ostream &out;
template <typename T> inline void printIntegerHelper(T x) {
if (x >= 10)
printIntegerHelper(x / 10);
out.put(x % 10 + 48);
}
template <typename T> inline void printInteger(T x) {
if (x < 0)
out.put('-'), x = -x;
printIntegerHelper(x);
}
template <typename T> inline void printArray(T &ar) {
bool first = true;
for (auto &t : ar)
print((first) ? first = false, "" : " ", t);
}
public:
explicit output_writer(std::ostream &out) : out(out) {}
inline void flush() { out.flush(); }
inline void print() {}
inline void print(char x) { out.put(x); }
inline void print(short x) { printInteger(x); }
inline void print(int x) { printInteger(x); }
inline void print(long int x) { printInteger(x); }
inline void print(long long int x) { printInteger(x); }
inline void print(unsigned short x) { printInteger(x); }
inline void print(unsigned int x) { printInteger(x); }
inline void print(unsigned long int x) { printInteger(x); }
inline void print(unsigned long long int x) { printInteger(x); }
template <typename T1> inline void print(const T1 &t1) { out << t1; }
template <typename T1, typename T2>
inline void print(const std::pair<T1, T2> &t1) {
println(t1.first, ' ', t1.second);
}
template <typename T1> inline void print(const std::vector<T1> &t1) {
printArray(t1);
}
template <typename T1, int N> inline void print(const std::array<T1, N> &t1) {
printArray(t1);
}
template <typename T1, int N, int M>
inline void print(const std::array<std::array<T1, M>, N> &t1) {
for (auto &t : t1)
println(t);
}
template <typename T1>
inline void print(const std::vector<std::vector<T1>> &t1) {
bool first = true;
for (auto &t : t1)
print((first) ? first = false, "" : "\n", t);
}
template <typename T, typename... Args>
inline void print(const T &t, Args... args) {
print(t), print(args...);
}
template <typename... Args> inline void println(Args... args) {
print(args...), print('\n');
}
template <typename T> output_writer &operator<<(const T &t) {
return print(t), *this;
}
};
} // namespace io
inline ll pow_mod(ll a, ll b, const ll MOD) {
ll res = 1;
for (ll i = 1; b; b ^= b & i, i <<= 1, a = (a * a) % MOD)
if (b & i)
res = (res * a) % MOD;
return res;
}
inline ll mod_inv(const ll a, const ll MOD) { return pow_mod(a, MOD - 2, MOD); }
template <typename T> inline T gcd(T a, T b) {
T t;
while (a != 0)
t = a, a = b % a, b = t;
return b;
}
template <typename T> T lcm(const T &a, const T &b) {
assert(a != 0 && b != 0);
return a / gcd(a, b) * b;
}
namespace math {
template <class T> int gMOD(T v) {
int res = (int)(v % MOD);
if (res >= 0)
return res;
return res + MOD;
};
class mod_int {
using mi = mod_int;
int v;
public:
mod_int(int v) : v(gMOD(v)) {}
mod_int(unsigned int v) : v(gMOD(v)) {}
mod_int(long long int v) : v(gMOD(v)) {}
mod_int(unsigned long long int v) : v(gMOD(v)) {}
mod_int(long int v) : v(gMOD(v)) {}
mod_int(unsigned long int v) : v(gMOD(v)) {}
mod_int() = default;
mod_int(const mi &o) = default;
mod_int(mi &&o) = default;
inline mi &operator=(const mi &o) = default;
inline mi &operator=(mi &&o) = default;
inline mi &operator++() {
if (v == MOD - 1)
v = 0;
else
v++;
return *this;
}
inline mi &operator--() {
if (v == 0)
v = MOD - 1;
else
v--;
return *this;
}
inline mi operator++(int) {
mi t = *this;
++(*this);
return t;
}
inline mi operator--(int) {
mi t = *this;
--(*this);
return t;
}
inline bool friend operator==(const mi &lhs, const mi &rhs) {
return lhs.v == rhs.v;
}
inline bool friend operator!=(const mi &lhs, const mi &rhs) {
return lhs.v != rhs.v;
}
template <typename T>
inline bool friend operator==(const mi &lhs, const T &rhs) {
return lhs == mi(rhs);
}
template <typename T>
inline bool friend operator!=(const mi &lhs, const T &rhs) {
return lhs != mi(rhs);
}
inline mi friend operator-(const mi &lhs, const mi &rhs) {
mi res;
res.v = lhs.v - rhs.v;
if (res.v < 0)
res.v += MOD;
return res;
}
inline mi friend operator+(const mi &lhs, const mi &rhs) {
mi res;
res.v = lhs.v + rhs.v - MOD;
if (res.v < 0)
res.v += MOD;
return res;
}
inline mi friend operator*(const mi &lhs, const mi &rhs) {
return {((((long long int)lhs.v) * rhs.v))};
}
inline mi friend operator/(const mi &lhs, const mi &rhs) {
return lhs * rhs.inv();
}
template <typename T>
inline mi friend operator-(const mi &lhs, const T &rhs) {
return lhs - mi(rhs);
}
template <typename T>
inline mi friend operator+(const mi &lhs, const T &rhs) {
return lhs + mi(rhs);
}
template <typename T>
inline mi friend operator*(const mi &lhs, const T &rhs) {
return lhs * mi(rhs);
}
template <typename T>
inline mi friend operator/(const mi &lhs, const T &rhs) {
return lhs / mi(rhs);
}
template <typename T> inline mi &operator-=(const T &rhs) {
return *this = *this - rhs;
}
template <typename T> inline mi &operator+=(const T &rhs) {
return *this = *this + rhs;
}
template <typename T> inline mi &operator*=(const T &rhs) {
return *this = *this * rhs;
}
template <typename T> inline mi &operator/=(const T &rhs) {
return *this = *this / rhs;
}
inline mi inv() const { return pow(MOD - 2); }
inline mi pow(long long int pw) const {
mi res = 1, val = *this;
while (pw) {
if (pw & 1)
res *= val;
val *= val;
pw >>= 1;
}
return res;
}
inline operator int() const { return v; }
inline friend std::istream &operator>>(std::istream &in, mi &rhs) {
long long int v;
in >> v;
rhs = {v};
return in;
}
inline friend std::ostream &operator<<(std::ostream &out, mi &rhs) {
out << rhs.v;
return out;
}
};
} // namespace math
namespace math {
mod_int factorial(int n) {
static std::vector<mod_int> mem;
static mod_int last;
if (mem.empty())
last = {1}, mem.push_back(last);
while (n >= (int)mem.size())
mem.push_back(mem.back() * last++);
return mem[n];
}
mod_int n_choose_k(int n, int k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
} // namespace math
const double PI = 3.14159265358979323846264338327950288419716939937510;
std::mt19937 get_random_engine() {
return std::mt19937(std::random_device()());
}
template <typename T = int> T myrand(T high = std::numeric_limits<T>::max()) {
assert(high > 0);
static std::mt19937 random_engine = get_random_engine();
return std::uniform_int_distribution<T>(0, high - 1)(random_engine);
}
// #include "prioity_queue_with_changes/pq_with_changes.h"
// #include "points_query/point_query.hpp"
// #include "numeric/numeric.hpp"
// #include "hashes/hashes.hpp"
// #include "graphs/graphs.hpp"
// #include "geometry/geometry.hpp"
// #include "extension_operations/extensions.hpp"
// #include "ranges/ranges.hpp"
// #include "matching/unweighted_max_matching.hpp"
// #include "lists/lists.hpp"
using mi = math::mod_int;
using vmi = std::vector<mi>;
using vvmi = std::vector<std::vector<mi>>;
using namespace std;
class DAnythingGoesToZero {
class Solver {
public:
io::input_reader in;
io::output_writer out;
Solver(std::istream &in, std::ostream &out) : in(in), out(out) {}
int calc_f(int x) {
if (x == 0)
return 0;
int i = __builtin_popcount(x);
return 1 + calc_f(x % i);
}
void solve() {
// <code here>
int n;
in >> n;
string s;
in >> s;
vi ans(n);
int n0 = 0;
for (char c : s)
if (c == '1')
n0++;
{
int n1 = n0 + 1;
int rem_n1 = 0;
fori(i, n) if (s[n - i - 1] == '1') rem_n1 =
(rem_n1 + (int)pow_mod(2, i, n1)) % n1;
fori(i, n) if (s[n - i - 1] == '0') {
int cur = (rem_n1 + pow_mod(2, i, n1)) % n1;
ans[i] = 1 + calc_f(cur);
}
}
if (n0 != 0) {
int n1 = n0 - 1;
int rem_n1 = 0;
fori(i, n) if (s[n - i - 1] == '1') rem_n1 =
(rem_n1 + (int)pow_mod(2, i, n1)) % n1;
fori(i, n) if (s[n - i - 1] == '1') {
int cur = (rem_n1 - pow_mod(2, i, n1)) % n1;
cur += n1;
cur %= n1;
ans[i] = 1 + calc_f(cur);
}
}
reverse(all(ans));
// </code here>
out.println(ans);
}
};
public:
void solve(std::istream &in, std::ostream &out) { Solver(in, out).solve(); }
};
int main() {
std::ios_base::sync_with_stdio(false), cin.tie(0);
DAnythingGoesToZero solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
}
| /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author Ido Kessler
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cmath>
#include <random>
#include <tuple>
using ll = long long int;
using ull = unsigned long long int;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using pi = std::pair<int, int>;
using vpi = std::vector<pi>;
using vvpi = std::vector<vpi>;
using pll = std::pair<ll, ll>;
using vpll = std::vector<pll>;
using vvpll = std::vector<vpll>;
#define all(x) (x).begin(), (x).end()
#define fori(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, e) for (int i = (int)(s); i <= (int)(e); ++i)
#define repr(i, s, e) for (int i = (int)(s); i >= (int)(e); --i)
#define pb push_back
template <typename T> void mk_unique(std::vector<T> &l) {
if (!std::is_sorted(all(l)))
std::sort(all(l));
auto it = std::unique(l.begin(), l.end());
auto d = std::distance(l.begin(), it);
l.resize(d);
}
template <typename T> T &smax(T &l, const T &r) {
if (l < r)
l = r;
return l;
}
template <typename T> T &smin(T &l, const T &r) {
if (r < l)
l = r;
return l;
}
constexpr int MOD = 1000000007; // 998244353;//1000000007;
namespace io {
class input_reader {
void throw_error() {
throw std::runtime_error("No more inputs while reading number");
}
template <typename T> inline void readIntegerValue(T &f) {
bool positive = true;
f = 0;
char c;
if (!in.get(c))
throw_error();
while (c < '0' || c > '9') {
if (c == '-')
positive = false;
if (!in.get(c))
throw_error();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + (c & 15);
if (!in.get(c))
break;
}
if (!positive)
f *= -1;
}
public:
std::istream ∈
explicit input_reader(std::istream &in) : in(in) {}
inline void read(int &f) { readIntegerValue(f); }
inline void read(short &f) { readIntegerValue(f); }
inline void read(long int &f) { readIntegerValue(f); }
inline void read(long long int &f) { readIntegerValue(f); }
inline void read(unsigned int &f) { readIntegerValue(f); }
inline void read(unsigned short &f) { readIntegerValue(f); }
inline void read(unsigned long int &f) { readIntegerValue(f); }
inline void read(unsigned long long int &f) { readIntegerValue(f); }
template <typename T> inline void read(std::vector<T> &f) {
for (auto &i : f)
read(i);
}
template <typename T, int N> inline void read(std::array<T, N> &res) {
for (auto &i : res)
read(i);
}
template <typename T1, typename T2> inline void read(std::pair<T1, T2> &p) {
read(p.first, p.second);
}
template <typename T> inline void read(T &f) { in >> f; }
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
read(t), read(args...);
}
template <typename T> inline void readArray(int n, T *res) {
while (n--)
read(*(res++));
}
template <typename T> inline input_reader &operator>>(T &t) {
return read(t), *this;
}
};
} // namespace io
namespace io {
class output_writer {
std::ostream &out;
template <typename T> inline void printIntegerHelper(T x) {
if (x >= 10)
printIntegerHelper(x / 10);
out.put(x % 10 + 48);
}
template <typename T> inline void printInteger(T x) {
if (x < 0)
out.put('-'), x = -x;
printIntegerHelper(x);
}
template <typename T> inline void printArray(T &ar) {
bool first = true;
for (auto &t : ar)
print((first) ? first = false, "" : " ", t);
}
public:
explicit output_writer(std::ostream &out) : out(out) {}
inline void flush() { out.flush(); }
inline void print() {}
inline void print(char x) { out.put(x); }
inline void print(short x) { printInteger(x); }
inline void print(int x) { printInteger(x); }
inline void print(long int x) { printInteger(x); }
inline void print(long long int x) { printInteger(x); }
inline void print(unsigned short x) { printInteger(x); }
inline void print(unsigned int x) { printInteger(x); }
inline void print(unsigned long int x) { printInteger(x); }
inline void print(unsigned long long int x) { printInteger(x); }
template <typename T1> inline void print(const T1 &t1) { out << t1; }
template <typename T1, typename T2>
inline void print(const std::pair<T1, T2> &t1) {
println(t1.first, ' ', t1.second);
}
template <typename T1> inline void print(const std::vector<T1> &t1) {
printArray(t1);
}
template <typename T1, int N> inline void print(const std::array<T1, N> &t1) {
printArray(t1);
}
template <typename T1, int N, int M>
inline void print(const std::array<std::array<T1, M>, N> &t1) {
for (auto &t : t1)
println(t);
}
template <typename T1>
inline void print(const std::vector<std::vector<T1>> &t1) {
bool first = true;
for (auto &t : t1)
print((first) ? first = false, "" : "\n", t);
}
template <typename T, typename... Args>
inline void print(const T &t, Args... args) {
print(t), print(args...);
}
template <typename... Args> inline void println(Args... args) {
print(args...), print('\n');
}
template <typename T> output_writer &operator<<(const T &t) {
return print(t), *this;
}
};
} // namespace io
inline ll pow_mod(ll a, ll b, const ll MOD) {
ll res = 1;
for (ll i = 1; b; b ^= b & i, i <<= 1, a = (a * a) % MOD)
if (b & i)
res = (res * a) % MOD;
return res;
}
inline ll mod_inv(const ll a, const ll MOD) { return pow_mod(a, MOD - 2, MOD); }
template <typename T> inline T gcd(T a, T b) {
T t;
while (a != 0)
t = a, a = b % a, b = t;
return b;
}
template <typename T> T lcm(const T &a, const T &b) {
assert(a != 0 && b != 0);
return a / gcd(a, b) * b;
}
namespace math {
template <class T> int gMOD(T v) {
int res = (int)(v % MOD);
if (res >= 0)
return res;
return res + MOD;
};
class mod_int {
using mi = mod_int;
int v;
public:
mod_int(int v) : v(gMOD(v)) {}
mod_int(unsigned int v) : v(gMOD(v)) {}
mod_int(long long int v) : v(gMOD(v)) {}
mod_int(unsigned long long int v) : v(gMOD(v)) {}
mod_int(long int v) : v(gMOD(v)) {}
mod_int(unsigned long int v) : v(gMOD(v)) {}
mod_int() = default;
mod_int(const mi &o) = default;
mod_int(mi &&o) = default;
inline mi &operator=(const mi &o) = default;
inline mi &operator=(mi &&o) = default;
inline mi &operator++() {
if (v == MOD - 1)
v = 0;
else
v++;
return *this;
}
inline mi &operator--() {
if (v == 0)
v = MOD - 1;
else
v--;
return *this;
}
inline mi operator++(int) {
mi t = *this;
++(*this);
return t;
}
inline mi operator--(int) {
mi t = *this;
--(*this);
return t;
}
inline bool friend operator==(const mi &lhs, const mi &rhs) {
return lhs.v == rhs.v;
}
inline bool friend operator!=(const mi &lhs, const mi &rhs) {
return lhs.v != rhs.v;
}
template <typename T>
inline bool friend operator==(const mi &lhs, const T &rhs) {
return lhs == mi(rhs);
}
template <typename T>
inline bool friend operator!=(const mi &lhs, const T &rhs) {
return lhs != mi(rhs);
}
inline mi friend operator-(const mi &lhs, const mi &rhs) {
mi res;
res.v = lhs.v - rhs.v;
if (res.v < 0)
res.v += MOD;
return res;
}
inline mi friend operator+(const mi &lhs, const mi &rhs) {
mi res;
res.v = lhs.v + rhs.v - MOD;
if (res.v < 0)
res.v += MOD;
return res;
}
inline mi friend operator*(const mi &lhs, const mi &rhs) {
return {((((long long int)lhs.v) * rhs.v))};
}
inline mi friend operator/(const mi &lhs, const mi &rhs) {
return lhs * rhs.inv();
}
template <typename T>
inline mi friend operator-(const mi &lhs, const T &rhs) {
return lhs - mi(rhs);
}
template <typename T>
inline mi friend operator+(const mi &lhs, const T &rhs) {
return lhs + mi(rhs);
}
template <typename T>
inline mi friend operator*(const mi &lhs, const T &rhs) {
return lhs * mi(rhs);
}
template <typename T>
inline mi friend operator/(const mi &lhs, const T &rhs) {
return lhs / mi(rhs);
}
template <typename T> inline mi &operator-=(const T &rhs) {
return *this = *this - rhs;
}
template <typename T> inline mi &operator+=(const T &rhs) {
return *this = *this + rhs;
}
template <typename T> inline mi &operator*=(const T &rhs) {
return *this = *this * rhs;
}
template <typename T> inline mi &operator/=(const T &rhs) {
return *this = *this / rhs;
}
inline mi inv() const { return pow(MOD - 2); }
inline mi pow(long long int pw) const {
mi res = 1, val = *this;
while (pw) {
if (pw & 1)
res *= val;
val *= val;
pw >>= 1;
}
return res;
}
inline operator int() const { return v; }
inline friend std::istream &operator>>(std::istream &in, mi &rhs) {
long long int v;
in >> v;
rhs = {v};
return in;
}
inline friend std::ostream &operator<<(std::ostream &out, mi &rhs) {
out << rhs.v;
return out;
}
};
} // namespace math
namespace math {
mod_int factorial(int n) {
static std::vector<mod_int> mem;
static mod_int last;
if (mem.empty())
last = {1}, mem.push_back(last);
while (n >= (int)mem.size())
mem.push_back(mem.back() * last++);
return mem[n];
}
mod_int n_choose_k(int n, int k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
} // namespace math
const double PI = 3.14159265358979323846264338327950288419716939937510;
std::mt19937 get_random_engine() {
return std::mt19937(std::random_device()());
}
template <typename T = int> T myrand(T high = std::numeric_limits<T>::max()) {
assert(high > 0);
static std::mt19937 random_engine = get_random_engine();
return std::uniform_int_distribution<T>(0, high - 1)(random_engine);
}
// #include "prioity_queue_with_changes/pq_with_changes.h"
// #include "points_query/point_query.hpp"
// #include "numeric/numeric.hpp"
// #include "hashes/hashes.hpp"
// #include "graphs/graphs.hpp"
// #include "geometry/geometry.hpp"
// #include "extension_operations/extensions.hpp"
// #include "ranges/ranges.hpp"
// #include "matching/unweighted_max_matching.hpp"
// #include "lists/lists.hpp"
using mi = math::mod_int;
using vmi = std::vector<mi>;
using vvmi = std::vector<std::vector<mi>>;
using namespace std;
class DAnythingGoesToZero {
class Solver {
public:
io::input_reader in;
io::output_writer out;
Solver(std::istream &in, std::ostream &out) : in(in), out(out) {}
int calc_f(int x) {
if (x == 0)
return 0;
int i = __builtin_popcount(x);
return 1 + calc_f(x % i);
}
void solve() {
// <code here>
int n;
in >> n;
string s;
in >> s;
vi ans(n);
int n0 = 0;
for (char c : s)
if (c == '1')
n0++;
{
int n1 = n0 + 1;
int rem_n1 = 0;
fori(i, n) if (s[n - i - 1] == '1') rem_n1 =
(rem_n1 + (int)pow_mod(2, i, n1)) % n1;
fori(i, n) if (s[n - i - 1] == '0') {
int cur = (rem_n1 + pow_mod(2, i, n1)) % n1;
ans[i] = 1 + calc_f(cur);
}
}
if (n0 != 0) {
int n1 = n0 - 1;
if (n1 == 0) {
fori(i, n) if (s[n - i - 1] == '1') ans[i] = 0;
} else {
int rem_n1 = 0;
fori(i, n) if (s[n - i - 1] == '1') rem_n1 =
(rem_n1 + (int)pow_mod(2, i, n1)) % n1;
fori(i, n) if (s[n - i - 1] == '1') {
int cur = (rem_n1 - pow_mod(2, i, n1)) % n1;
cur += n1;
cur %= n1;
ans[i] = 1 + calc_f(cur);
}
}
}
reverse(all(ans));
// </code here>
out.println(ans);
}
};
public:
void solve(std::istream &in, std::ostream &out) { Solver(in, out).solve(); }
};
int main() {
std::ios_base::sync_with_stdio(false), cin.tie(0);
DAnythingGoesToZero solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
}
| replace | 419 | 427 | 419 | 431 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10, mod = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int f[N];
f[0] = 0;
for (int i = 1; i < N; i++) {
f[i] = f[i % __builtin_popcount(i)] + 1;
}
string x;
cin >> x;
int cnt = 0;
for (int i = 0; i < n; i++)
cnt += (x[i] == '1');
int pw1[n], pw2[n];
int val1 = 0, val2 = 0;
for (int i = n - 1, cur1 = 1, cur2 = 1; i >= 0;
i--, cur1 = (cur1 * 2) % (cnt - 1), cur2 = (cur2 * 2) % (cnt + 1)) {
pw1[n - 1 - i] = cur1;
pw2[n - 1 - i] = cur2;
if (x[i] == '1') {
val1 = (val1 + cur1) % (cnt - 1);
val2 = (val2 + cur2) % (cnt + 1);
}
}
for (int i = 0; i < n; i++) {
int add = -1, newVal = -1;
if (x[i] == '0') {
add = pw2[n - 1 - i];
newVal = val2;
newVal = (newVal + add) % (cnt + 1);
} else {
add = (cnt - 1) - pw1[n - 1 - i];
newVal = val1;
newVal = (newVal + add) % (cnt - 1);
}
cout << f[newVal] + 1 << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10, mod = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int f[N];
f[0] = 0;
for (int i = 1; i < N; i++) {
f[i] = f[i % __builtin_popcount(i)] + 1;
}
string x;
cin >> x;
int cnt = 0;
for (int i = 0; i < n; i++)
cnt += (x[i] == '1');
if (cnt == 0) {
for (int i = 0; i < n; i++)
cout << 1 << "\n";
return 0;
}
if (cnt == 1) {
for (int i = 0; i < n; i++) {
if (x[i] == '1')
cout << 0 << "\n";
else if (i == n - 1 || x[n - 1] == '1')
cout << 2 << "\n";
else
cout << 1 << "\n";
}
return 0;
}
int pw1[n], pw2[n];
int val1 = 0, val2 = 0;
for (int i = n - 1, cur1 = 1, cur2 = 1; i >= 0;
i--, cur1 = (cur1 * 2) % (cnt - 1), cur2 = (cur2 * 2) % (cnt + 1)) {
pw1[n - 1 - i] = cur1;
pw2[n - 1 - i] = cur2;
if (x[i] == '1') {
val1 = (val1 + cur1) % (cnt - 1);
val2 = (val2 + cur2) % (cnt + 1);
}
}
for (int i = 0; i < n; i++) {
int add = -1, newVal = -1;
if (x[i] == '0') {
add = pw2[n - 1 - i];
newVal = val2;
newVal = (newVal + add) % (cnt + 1);
} else {
add = (cnt - 1) - pw1[n - 1 - i];
newVal = val1;
newVal = (newVal + add) % (cnt - 1);
}
cout << f[newVal] + 1 << "\n";
}
return 0;
}
| insert | 24 | 24 | 24 | 40 | 0 | |
p02609 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
// Inserted snippets: io, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
if (v1 > v2) {
v1 = v2;
return true;
}
return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
if (v1 < v2) {
v1 = v2;
return true;
}
return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
cin >> head;
read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
cout << head << " ";
write(tail...);
}
template <typename... T> void die(T... tok) {
write(tok...);
exit(0);
}
// End snippets
ll bm(ll b, ll p, ll m) {
ll r = 1;
for (; p; p >>= 1, b = b * b % m)
if (p & 1)
r = r * b % m;
return r;
}
ll longmod(string s, ll m) {
ll r = 0;
for (char c : s) {
r = (r * 2 + c - '0') % m;
}
return r;
}
ll f(ll x) {
if (x == 0)
return 0;
return 1 + f(x % __builtin_popcountll(x));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n;
read(n);
string s;
read(s);
int bc = count(all(s), '1');
map<int, int> init;
for (int i = bc - 1; i <= bc + 1; i++)
init[i] = longmod(s, i);
for (int i = 0; i < n; i++) {
int c1 = bc - int(s[i] == '1') + int(s[i] == '0');
int rv = init[c1];
if (c1 == 0) {
write(0);
continue;
}
if (s[i] == '1') {
// subtract
rv -= bm(2, n - 1 - i, c1);
} else {
rv += bm(2, n - 1 - i, c1);
}
rv = (rv % c1 + c1) % c1;
cout << 1 + f(rv) << endl;
}
}
| #include "bits/stdc++.h"
using namespace std;
// Inserted snippets: io, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
if (v1 > v2) {
v1 = v2;
return true;
}
return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
if (v1 < v2) {
v1 = v2;
return true;
}
return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
cin >> head;
read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
cout << head << " ";
write(tail...);
}
template <typename... T> void die(T... tok) {
write(tok...);
exit(0);
}
// End snippets
ll bm(ll b, ll p, ll m) {
ll r = 1;
for (; p; p >>= 1, b = b * b % m)
if (p & 1)
r = r * b % m;
return r;
}
ll longmod(string s, ll m) {
ll r = 0;
for (char c : s) {
r = (r * 2 + c - '0') % m;
}
return r;
}
ll f(ll x) {
if (x == 0)
return 0;
return 1 + f(x % __builtin_popcountll(x));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n;
read(n);
string s;
read(s);
int bc = count(all(s), '1');
map<int, int> init;
for (int i = bc - 1; i <= bc + 1; i++)
if (i > 0)
init[i] = longmod(s, i);
for (int i = 0; i < n; i++) {
int c1 = bc - int(s[i] == '1') + int(s[i] == '0');
int rv = init[c1];
if (c1 == 0) {
write(0);
continue;
}
if (s[i] == '1') {
// subtract
rv -= bm(2, n - 1 - i, c1);
} else {
rv += bm(2, n - 1 - i, c1);
}
rv = (rv % c1 + c1) % c1;
cout << 1 + f(rv) << endl;
}
}
| replace | 74 | 75 | 74 | 76 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long modpow(long long n, long long k, long long MOD) {
long long ans = 1;
n %= MOD;
while (k > 0) {
if (k & 1)
ans = (ans * n) % MOD;
k >>= 1;
n = (n * n) % MOD;
}
return ans;
}
int rem(int n, string s, int pp) {
if (pp <= 0)
return -1;
reverse(s.begin(), s.end());
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
ans = (ans + modpow(2, i, pp)) % pp;
}
return ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
string s;
cin >> n >> s;
int pp = 0;
for (char c : s) {
pp += (c == '1');
}
int loss = rem(n, s, pp - 1);
int gain = rem(n, s, pp + 1);
for (int i = 0, j = n - 1; i < n; i++, j--) {
int ans = 1, cur = 0;
if (s[i] == '0') {
// gain a bit
cur = gain;
cur = (cur + modpow(2, j, pp + 1)) % (pp + 1);
} else {
// lose a bit
cur = loss;
cur = (cur - modpow(2, j, pp - 1)) % (pp - 1);
if (cur < 0)
cur += (pp - 1);
}
while (cur > 0) {
ans++;
cur %= __builtin_popcount(cur);
}
cout << ans << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
long long modpow(long long n, long long k, long long MOD) {
long long ans = 1;
n %= MOD;
while (k > 0) {
if (k & 1)
ans = (ans * n) % MOD;
k >>= 1;
n = (n * n) % MOD;
}
return ans;
}
int rem(int n, string s, int pp) {
if (pp <= 0)
return -1;
reverse(s.begin(), s.end());
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
ans = (ans + modpow(2, i, pp)) % pp;
}
return ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
string s;
cin >> n >> s;
int pp = 0;
for (char c : s) {
pp += (c == '1');
}
int loss = rem(n, s, pp - 1);
int gain = rem(n, s, pp + 1);
for (int i = 0, j = n - 1; i < n; i++, j--) {
int ans = 1, cur = 0;
if (s[i] == '0') {
// gain a bit
cur = gain;
cur = (cur + modpow(2, j, pp + 1)) % (pp + 1);
} else {
// lose a bit
if (pp == 1) {
ans = 0;
cur = 0;
} else {
cur = loss;
cur = (cur - modpow(2, j, pp - 1)) % (pp - 1);
if (cur < 0)
cur += (pp - 1);
}
}
while (cur > 0) {
ans++;
cur %= __builtin_popcount(cur);
}
cout << ans << '\n';
}
return 0;
}
| replace | 52 | 56 | 52 | 61 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
int N;
std::string X;
std::cin >> N >> X;
const int popcount = std::count(X.begin(), X.end(), '1');
std::map<int, int> x_map;
std::vector<std::map<int, int>> digit_map(N);
for (int mod = std::max(1, popcount - 1); mod <= popcount + 1; ++mod) {
int remainder = 0;
for (const char digit : X) {
remainder = (remainder * 2 + digit - '0') % mod;
}
x_map[mod] = remainder;
int digit = 1;
for (int i = 0; i < N; ++i) {
digit_map[N - i - 1][mod] = digit;
digit = digit * 2 % mod;
}
}
for (int i = 0; i < N; ++i) {
const int mod = popcount + (X[i] == '0' ? 1 : -1);
int f = 0;
if (popcount > 0) {
f = 1;
int n = x_map[mod];
if (X[i] == '0') {
n = (n + digit_map[i][mod]) % mod;
} else {
n = (n - digit_map[i][mod] + mod) % mod;
}
while (n > 0) {
++f;
n = n % __builtin_popcount(n);
}
}
std::cout << f << std::endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
int N;
std::string X;
std::cin >> N >> X;
const int popcount = std::count(X.begin(), X.end(), '1');
std::map<int, int> x_map;
std::vector<std::map<int, int>> digit_map(N);
for (int mod = std::max(1, popcount - 1); mod <= popcount + 1; ++mod) {
int remainder = 0;
for (const char digit : X) {
remainder = (remainder * 2 + digit - '0') % mod;
}
x_map[mod] = remainder;
int digit = 1;
for (int i = 0; i < N; ++i) {
digit_map[N - i - 1][mod] = digit;
digit = digit * 2 % mod;
}
}
for (int i = 0; i < N; ++i) {
const int mod = popcount + (X[i] == '0' ? 1 : -1);
int f = 0;
if (mod > 0) {
f = 1;
int n = x_map[mod];
if (X[i] == '0') {
n = (n + digit_map[i][mod]) % mod;
} else {
n = (n - digit_map[i][mod] + mod) % mod;
}
while (n > 0) {
++f;
n = n % __builtin_popcount(n);
}
}
std::cout << f << std::endl;
}
return 0;
}
| replace | 28 | 29 | 28 | 29 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
for (int i = 0; i < sz(v); i++) {
os << v[i];
if (i < sz(v) - 1)
os << " ";
}
return os;
}
int main() {
int n;
string s;
cin >> n >> s;
int cnt = 0;
rep(i, n) cnt += s[i] - '0';
map<int, int> x;
for (int d = cnt - 1; d <= cnt + 1; d++) {
rep(i, n) {
x[d] <<= 1;
x[d] += s[i] - '0';
x[d] %= d;
}
}
map<int, vector<int>> mp;
for (int d = cnt - 1; d <= cnt + 1; d++) {
mp[d].resize(n);
if (d == 0)
continue;
int k = 1;
rep(i, n) {
mp[d][i] = k;
k <<= 1;
k %= d;
}
}
rep(i, n) {
if (s[i] == '0') {
int xx = (x[cnt + 1] + mp[cnt + 1][n - 1 - i]) % (cnt + 1);
int ans = 1;
while (xx) {
xx %= __builtin_popcount(xx);
ans++;
}
cout << ans << endl;
} else {
if (cnt == 1) {
cout << 0 << endl;
continue;
}
int xx = (x[cnt - 1] - mp[cnt - 1][n - 1 - i] + cnt - 1) % (cnt - 1);
int ans = 1;
while (xx) {
xx %= __builtin_popcount(xx);
ans++;
}
cout << ans << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
for (int i = 0; i < sz(v); i++) {
os << v[i];
if (i < sz(v) - 1)
os << " ";
}
return os;
}
int main() {
int n;
string s;
cin >> n >> s;
int cnt = 0;
rep(i, n) cnt += s[i] - '0';
map<int, int> x;
for (int d = cnt - 1; d <= cnt + 1; d++) {
if (d == 0)
continue;
rep(i, n) {
x[d] <<= 1;
x[d] += s[i] - '0';
x[d] %= d;
}
}
map<int, vector<int>> mp;
for (int d = cnt - 1; d <= cnt + 1; d++) {
mp[d].resize(n);
if (d == 0)
continue;
int k = 1;
rep(i, n) {
mp[d][i] = k;
k <<= 1;
k %= d;
}
}
rep(i, n) {
if (s[i] == '0') {
int xx = (x[cnt + 1] + mp[cnt + 1][n - 1 - i]) % (cnt + 1);
int ans = 1;
while (xx) {
xx %= __builtin_popcount(xx);
ans++;
}
cout << ans << endl;
} else {
if (cnt == 1) {
cout << 0 << endl;
continue;
}
int xx = (x[cnt - 1] - mp[cnt - 1][n - 1 - i] + cnt - 1) % (cnt - 1);
int ans = 1;
while (xx) {
xx %= __builtin_popcount(xx);
ans++;
}
cout << ans << endl;
}
}
} | insert | 46 | 46 | 46 | 48 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
#define _USE_MATH_DEFINES
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;
#define EPS 1e-12
#define ull unsigned long long
#define ll long long
#define VI vector<ll>
#define PII pair<ll, ll>
#define VVI vector<vector<ll>>
#define VVVI vector<vector<vector<ll>>>
#define VVVVI vector<vector<vector<vector<ll>>>>
#define REP(i, n) for (ll i = 0, _n = (n); (i) < (ll)_n; ++i)
#define REPR(i, n) for (ll i = (ll)(n)-1; 0 <= (i); --i)
#define RANGE(i, a, b) for (ll i = (ll)a, _b = (ll)(b); (i) < _b; ++i)
#define RANGER(i, a, b) for (ll i = (ll)(b)-1, _a = (ll)(a); (_a) <= i; --i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP(a, b) make_pair(a, b)
#define POPCOUNT __builtin_popcount
#define POPCOUNTLL __builtin_popcountll
#define CLEAR(table, v) memset(table, v, sizeof(table));
#define UNIFORM_DOUBLE(a, b) \
(((b - a) * (double)rand() / RAND_MAX) + a) // [a, b)
#define UNIFORM_LL(a, b) (ll) UNIFORM_DOUBLE(a, b) // [a, b)
#define IN(v, lo, hi) ((lo) <= (v) && (v) < (hi))
#define DD(v) cout << #v << ": " << v << endl
#define FI first
#define SE second
template <typename T0, typename T1>
std::ostream &operator<<(std::ostream &os, const map<T0, T1> &v) {
for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) {
os << p->first << ": " << p->second << " ";
}
return os;
}
template <typename T0, typename T1>
std::ostream &operator<<(std::ostream &os, const pair<T0, T1> &v) {
os << v.first << ": " << v.second << " ";
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << " ";
}
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const set<T> &v) {
vector<T> tmp(v.begin(), v.end());
os << tmp;
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const deque<T> &v) {
vector<T> tmp(v.begin(), v.end());
os << tmp;
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<vector<T>> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << endl;
}
return os;
}
template <typename T> void maxUpdate(T &a, T b) { a = max(a, b); }
template <typename T> void minUpdate(T &a, T b) { a = min(a, b); }
string YN(bool v) { return v ? "Yes" : "No"; }
void uniq(VI &es) {
sort(ALL(es));
es.erase(unique(ALL(es)), es.end());
}
#define INF (1LL << 60)
ll f(ll n) {
if (n == 0)
return 0;
ll pc = POPCOUNTLL(n);
return 1 + f(n % pc);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
string X;
while (cin >> N >> X) {
ll refPC = 0;
REP(i, N) if (X[i] == '1') refPC++;
// DD(refPC);
// X % pc
VI refMod(2);
REP(i, 2) {
ll pc = i == 0 ? refPC - 1 : refPC + 1;
if (pc <= 0)
continue;
ll v = 1 % pc;
ll t = 0;
REP(j, N) {
if (X[N - 1 - j] == '1')
t = (t + v) % pc;
v = (v * 2) % pc;
}
refMod[i] = t;
}
VVI m(2, VI(N));
REP(i, 2) {
ll pc = i == 0 ? refPC - 1 : refPC + 1;
if (pc <= 0)
continue;
REP(j, N) {
if (j == 0) {
m[i][N - 1 - j] = 1 % pc;
} else {
m[i][N - 1 - j] = (m[i][N - 1 - (j - 1)] * 2) % pc;
}
}
}
// DD(refMod);
// DD(m);
REP(i, N) {
ll ans = 0;
if (X[i] == '1') {
ll pc = refPC - 1;
ans = 1 + f((refMod[0] - m[0][i] + pc) % pc);
} else {
ll pc = refPC + 1;
ans = 1 + f((refMod[1] + m[1][i] + pc) % pc);
}
cout << ans << endl;
}
}
return 0;
}
| #include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
#define _USE_MATH_DEFINES
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;
#define EPS 1e-12
#define ull unsigned long long
#define ll long long
#define VI vector<ll>
#define PII pair<ll, ll>
#define VVI vector<vector<ll>>
#define VVVI vector<vector<vector<ll>>>
#define VVVVI vector<vector<vector<vector<ll>>>>
#define REP(i, n) for (ll i = 0, _n = (n); (i) < (ll)_n; ++i)
#define REPR(i, n) for (ll i = (ll)(n)-1; 0 <= (i); --i)
#define RANGE(i, a, b) for (ll i = (ll)a, _b = (ll)(b); (i) < _b; ++i)
#define RANGER(i, a, b) for (ll i = (ll)(b)-1, _a = (ll)(a); (_a) <= i; --i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define ALLR(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP(a, b) make_pair(a, b)
#define POPCOUNT __builtin_popcount
#define POPCOUNTLL __builtin_popcountll
#define CLEAR(table, v) memset(table, v, sizeof(table));
#define UNIFORM_DOUBLE(a, b) \
(((b - a) * (double)rand() / RAND_MAX) + a) // [a, b)
#define UNIFORM_LL(a, b) (ll) UNIFORM_DOUBLE(a, b) // [a, b)
#define IN(v, lo, hi) ((lo) <= (v) && (v) < (hi))
#define DD(v) cout << #v << ": " << v << endl
#define FI first
#define SE second
template <typename T0, typename T1>
std::ostream &operator<<(std::ostream &os, const map<T0, T1> &v) {
for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) {
os << p->first << ": " << p->second << " ";
}
return os;
}
template <typename T0, typename T1>
std::ostream &operator<<(std::ostream &os, const pair<T0, T1> &v) {
os << v.first << ": " << v.second << " ";
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << " ";
}
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const set<T> &v) {
vector<T> tmp(v.begin(), v.end());
os << tmp;
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const deque<T> &v) {
vector<T> tmp(v.begin(), v.end());
os << tmp;
return os;
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<vector<T>> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << endl;
}
return os;
}
template <typename T> void maxUpdate(T &a, T b) { a = max(a, b); }
template <typename T> void minUpdate(T &a, T b) { a = min(a, b); }
string YN(bool v) { return v ? "Yes" : "No"; }
void uniq(VI &es) {
sort(ALL(es));
es.erase(unique(ALL(es)), es.end());
}
#define INF (1LL << 60)
ll f(ll n) {
if (n == 0)
return 0;
ll pc = POPCOUNTLL(n);
return 1 + f(n % pc);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
string X;
while (cin >> N >> X) {
ll refPC = 0;
REP(i, N) if (X[i] == '1') refPC++;
// DD(refPC);
// X % pc
VI refMod(2);
REP(i, 2) {
ll pc = i == 0 ? refPC - 1 : refPC + 1;
if (pc <= 0)
continue;
ll v = 1 % pc;
ll t = 0;
REP(j, N) {
if (X[N - 1 - j] == '1')
t = (t + v) % pc;
v = (v * 2) % pc;
}
refMod[i] = t;
}
VVI m(2, VI(N));
REP(i, 2) {
ll pc = i == 0 ? refPC - 1 : refPC + 1;
if (pc <= 0)
continue;
REP(j, N) {
if (j == 0) {
m[i][N - 1 - j] = 1 % pc;
} else {
m[i][N - 1 - j] = (m[i][N - 1 - (j - 1)] * 2) % pc;
}
}
}
// DD(refMod);
// DD(m);
REP(i, N) {
ll ans = 0;
if (X[i] == '1') {
ll pc = refPC - 1;
if (pc == 0)
ans = 0;
else
ans = 1 + f((refMod[0] - m[0][i] + pc) % pc);
} else {
ll pc = refPC + 1;
ans = 1 + f((refMod[1] + m[1][i] + pc) % pc);
}
cout << ans << endl;
}
}
return 0;
}
| replace | 149 | 150 | 149 | 153 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set
const ll MOD = 1e9 + 7;
void min_self(ll &a, ll b) { a = min(a, b); }
void max_self(ll &a, ll b) { a = max(a, b); }
ll add(ll x, ll y) { return (x + y) % MOD; }
ll sub(ll x, ll y) { return add(x, MOD - y); }
ll mul(ll x, ll y) { return (x * 1ll * y) % MOD; }
ll binpow(ll x, ll y) {
ll z = 1;
while (y > 0) {
if (y % 2 == 1)
z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
ll inv(ll x) { return binpow(x, MOD - 2); }
const int MXN = 1e6 + 10;
ll fact[MXN];
void fac_pre() {
fact[0] = 1;
for (ll i = 1; i < MXN; i++) {
fact[i] = mul(fact[i - 1], i);
}
}
ll C(ll n, ll k) { return mul(fact[n], inv(mul(fact[k], fact[n - k]))); }
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
///--//
int popcount(int n) {
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
/* KEEP IN MIND
* take care when sorting a 1 based indexed vector
* const int N is defined way above in the starting
*/
int n;
cin >> n;
string s;
cin >> s;
int sb = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
++sb;
}
vector<int> f(2e5 + 10, 0);
for (int i = 1; i < f.size(); i++) {
f[i] = f[i % (popcount(i))] + 1;
}
vector<int> mods{sb - 1, sb, sb + 1};
vector<vector<int>> modpow(n, vector<int>(3, 1));
for (int i = 1; i < n; i++) {
for (int j = 0; j < 3; j++) {
if (mods[j] <= 0)
continue;
modpow[i][j] = (modpow[i - 1][j] * 2) % mods[j];
}
}
vector<int> modsum(3, 0);
reverse(s.begin(), s.end());
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
if (mods[j] <= 0)
continue;
modsum[j] += (s[i] == '1') * modpow[i][j];
modsum[j] %= mods[j];
}
}
vector<int> ans(n);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
int x = modsum[0];
x -= modpow[i][0];
x += mods[0];
x %= mods[0];
ans[i] = f[x] + 1;
} else {
int x = modsum[2];
x += modpow[i][2];
x %= mods[2];
ans[i] = f[x] + 1;
}
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set
const ll MOD = 1e9 + 7;
void min_self(ll &a, ll b) { a = min(a, b); }
void max_self(ll &a, ll b) { a = max(a, b); }
ll add(ll x, ll y) { return (x + y) % MOD; }
ll sub(ll x, ll y) { return add(x, MOD - y); }
ll mul(ll x, ll y) { return (x * 1ll * y) % MOD; }
ll binpow(ll x, ll y) {
ll z = 1;
while (y > 0) {
if (y % 2 == 1)
z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
ll inv(ll x) { return binpow(x, MOD - 2); }
const int MXN = 1e6 + 10;
ll fact[MXN];
void fac_pre() {
fact[0] = 1;
for (ll i = 1; i < MXN; i++) {
fact[i] = mul(fact[i - 1], i);
}
}
ll C(ll n, ll k) { return mul(fact[n], inv(mul(fact[k], fact[n - k]))); }
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
///--//
int popcount(int n) {
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
/* KEEP IN MIND
* take care when sorting a 1 based indexed vector
* const int N is defined way above in the starting
*/
int n;
cin >> n;
string s;
cin >> s;
int sb = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
++sb;
}
vector<int> f(2e5 + 10, 0);
for (int i = 1; i < f.size(); i++) {
f[i] = f[i % (popcount(i))] + 1;
}
vector<int> mods{sb - 1, sb, sb + 1};
vector<vector<int>> modpow(n, vector<int>(3, 1));
for (int i = 1; i < n; i++) {
for (int j = 0; j < 3; j++) {
if (mods[j] <= 0)
continue;
modpow[i][j] = (modpow[i - 1][j] * 2) % mods[j];
}
}
vector<int> modsum(3, 0);
reverse(s.begin(), s.end());
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
if (mods[j] <= 0)
continue;
modsum[j] += (s[i] == '1') * modpow[i][j];
modsum[j] %= mods[j];
}
}
vector<int> ans(n);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
if (sb == 1) {
ans[i] = 0;
continue;
}
int x = modsum[0];
x -= modpow[i][0];
x += mods[0];
x %= mods[0];
ans[i] = f[x] + 1;
} else {
int x = modsum[2];
x += modpow[i][2];
x %= mods[2];
ans[i] = f[x] + 1;
}
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
} | insert | 108 | 108 | 108 | 112 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll N;
string X;
ll g[400001];
ll f[400001];
ll calc(ll m) {
if (m == 0)
return 0;
ll ret = 0;
for (auto &x : X) {
ret *= 2;
ret += x - '0';
ret %= m;
}
return ret;
}
int main() {
cin >> N;
cin >> X;
vector<ll> ans;
for (int i = 1; i <= 400000; i++) {
bitset<32> bt(i);
g[i] = i % bt.count();
}
for (int i = 1; i <= 400000; i++) {
if (g[i] == 0) {
f[i] = 1;
continue;
}
f[i] = f[g[i]] + 1;
}
bitset<200000> btX(X);
ll x_count = btX.count();
ll mod_p = x_count + 1;
ll mod_m = x_count - 1;
ll X_mod_p = calc(mod_p);
ll X_mod_m = calc(mod_m);
ll pp, mp;
pp = mp = 1;
for (int i = N - 1; i >= 0; i--) {
pp %= mod_p;
mp %= mod_m;
if (X[i] == '0') {
ll t = pp + X_mod_p;
t %= mod_p;
ans.push_back(f[t] + 1);
} else {
if (mod_m == 0) {
ans.push_back(0);
continue;
}
ll t = X_mod_m - mp;
t += mod_m;
t %= mod_m;
ans.push_back(f[t] + 1);
}
pp *= 2;
mp *= 2;
}
for (int i = N - 1; i >= 0; i--)
cout << ans[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll N;
string X;
ll g[400001];
ll f[400001];
ll calc(ll m) {
if (m == 0)
return 0;
ll ret = 0;
for (auto &x : X) {
ret *= 2;
ret += x - '0';
ret %= m;
}
return ret;
}
int main() {
cin >> N;
cin >> X;
vector<ll> ans;
for (int i = 1; i <= 400000; i++) {
bitset<32> bt(i);
g[i] = i % bt.count();
}
for (int i = 1; i <= 400000; i++) {
if (g[i] == 0) {
f[i] = 1;
continue;
}
f[i] = f[g[i]] + 1;
}
bitset<200000> btX(X);
ll x_count = btX.count();
ll mod_p = x_count + 1;
ll mod_m = x_count - 1;
ll X_mod_p = calc(mod_p);
ll X_mod_m = calc(mod_m);
ll pp, mp;
pp = mp = 1;
for (int i = N - 1; i >= 0; i--) {
pp %= mod_p;
if (mod_m != 0)
mp %= mod_m;
if (X[i] == '0') {
ll t = pp + X_mod_p;
t %= mod_p;
ans.push_back(f[t] + 1);
} else {
if (mod_m == 0) {
ans.push_back(0);
continue;
}
ll t = X_mod_m - mp;
t += mod_m;
t %= mod_m;
ans.push_back(f[t] + 1);
}
pp *= 2;
mp *= 2;
}
for (int i = N - 1; i >= 0; i--)
cout << ans[i] << endl;
return 0;
} | replace | 48 | 49 | 48 | 50 | 0 | |
p02609 | C++ | Runtime Error | #include <iostream>
#ifdef MBP
#include "stdc++.h"
#else
#include <bits/stdc++.h>
#endif
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define REP(i, n) for (long long int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (long long int i = 1, i##_len = (n); i <= i##_len; ++i)
#define REP_R(i, n) for (long long int i = (n - 1); i >= 0; --i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((long long int)(x).size())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// #define DEBUG
#ifdef DEBUG
#define OUT(x) std::cout << (x) << std::endl
#define OUT2(x, y) std::cout << (x) << "," << (y) << std::endl
#define OUT3(x, y, z) std::cout << (x) << "," << (y) << "," << (z) << std::endl
#define OUTSP(x) std::cout << (x) << " ";
#define OUTLF std::cout << std::endl
#define OUTPA(p) OUT2(p.first, p.second)
#else
#define OUT(x) NULL
#define OUT2(x, y) NULL
#define OUT3(x, y, z) NULL
#define OUTPA(p) NULL
#define OUTSP(p) NULL
#define OUTLF NULL
#endif
#define INF INT_MAX
#define INFLL LLONG_MAX
#define PB push_back
#define MOD 1000000007LL
#define LSB(x) __builtin_ctzll(x)
#define MSB(x) 64LL - __builtin_clzll(x) - 1LL
#define MSBINT(x) 32LL - __builtin_clz(x)
#define BITCNT(x) __builtin_popcountll(x)
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using vec = vector<ll>;
using mat = vector<vector<ll>>;
using pa = pair<ll, ll>;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
template <class T> inline void input(T &arr) {
ll n = arr.size();
REP(i, n)
cin >> arr[i];
}
template <class T> inline void input2(T &arr1, T &arr2) {
ll n = arr1.size();
REP(i, n)
cin >> arr1[i] >> arr2[i];
}
template <class T> inline void inputPA(T &arr) {
ll n = arr.size();
REP(i, n)
cin >> arr[i].first >> arr[i].second;
}
template <class T> inline void outArr(T &arr) {
for (auto x : arr) {
OUTSP(x);
}
OUTLF;
}
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};
const ll dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template <class T> bool chmax(T &, const T &);
template <class T> bool chmin(T &, const T &);
ll qp(ll a, ll b);
ll qp(ll a, ll b, ll mo);
ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void init_fact();
ll combination(ll, ll);
#define FACSIZE 200002LL
ll invfac[FACSIZE];
ll fac[FACSIZE];
/*
unordered_map<ll, ll> um;
unordered_map<ll, ll> umU;
unordered_map<ll, ll> umD;
*/
vec um;
vec umU;
vec umD;
ll amariU(ll x, ll d) {
if (x == 0) {
return umU[x] = 1 % d;
} else {
umU[x] = (umU[x - 1] * 2) % d;
}
return 0;
}
ll amariD(ll x, ll d) {
if (x == 0) {
return umD[x] = 1 % d;
} else {
umD[x] = (umD[x - 1] * 2) % d;
}
return 0;
}
ll f(ll x) {
if (x == 0)
return um[x] = 0;
else if (x == 1)
return um[x] = 1;
// if (um.find(x) != end(um))
if (um[x] != -1)
return um[x];
ll cnt = 0;
ll orgx = x;
while (x > 0) {
cnt += x % 2;
x /= 2;
}
return um[orgx] = f(orgx % cnt) + 1;
}
int main(void) {
um.resize(210000, -1);
umU.resize(210000);
umD.resize(210000);
ll n;
cin >> n;
string x;
cin >> x;
REP(i, 200110) { f(i); }
ll cnt = 0;
REP(i, x.size()) {
char c = x[i];
if (c == '1') {
cnt++;
}
}
REP(i, 200010) {
amariU(i, cnt + 1);
amariD(i, cnt - 1);
}
ll baseMU = 0;
ll baseMD = 0;
REP(i, n) {
auto c = x[n - 1 - i];
if (c == '1') {
baseMU += umU[i];
baseMU = baseMU % (cnt + 1);
baseMD += umD[i];
baseMD = baseMD % (cnt - 1);
}
}
REP(i, x.size()) {
char c = x[i];
if (c == '0') {
ll MU = baseMU + (cnt + 1 + umU[n - 1 - i]);
MU = MU % (cnt + 1);
cout << um[MU] + 1 << endl;
} else {
ll MD = baseMD + (cnt - 1 - umD[n - 1 - i]);
MD = MD % (cnt - 1);
cout << um[MD] + 1 << endl;
}
}
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
ll qp(ll a, ll b) {
ll ans = 1LL;
do {
if (b & 1LL)
ans = 1LL * mod_mul(ans, a) % MOD;
a = 1LL * mod_mul(a, a) % MOD;
} while (b >>= 1LL);
return ans;
}
ll qp(ll a, ll b, ll mo) {
ll ans = 1LL;
do {
if (b & 1LL)
ans = 1LL * (ans % mo) * (a % mo);
a = 1LL * (a % mo) * (a % mo);
} while (b >>= 1LL);
return ans;
}
ll mod_pow(ll x, ll r) {
if (r == 0)
return 1;
else if (r == 1)
return x % MOD;
else if (r % 2 == 0) {
ll t = mod_pow(x, r / 2) % MOD;
return t * t % MOD;
} else {
ll t = mod_pow(x, r / 2) % MOD;
ll k = t * t % MOD;
return k * x % MOD;
}
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_mul(ll a, ll b) { return (a * b) % MOD; }
ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, MOD - 2)); }
void make_fact(ll n) {
fac[0] = 1;
REP(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}
void make_invfact(ll n) {
invfac[n] = mod_pow(fac[n], MOD - 2);
REP_R(i, n) { invfac[i] = mod_mul(invfac[i + 1], i + 1); }
}
void init_fact() {
make_fact(FACSIZE - 1);
make_invfact(FACSIZE - 1);
}
ll combination(ll n, ll c) {
return mod_mul(fac[n], mod_mul(invfac[c], invfac[n - c]));
}
///////////////fact
/*
init_fact();
cout << fac[5] << endl;
cout << invfac[5] << endl;
cout << combination(8, 4) << endl;
cout << combination(31, 8) << endl;
//*/
///////////////////
| #include <iostream>
#ifdef MBP
#include "stdc++.h"
#else
#include <bits/stdc++.h>
#endif
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define REP(i, n) for (long long int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (long long int i = 1, i##_len = (n); i <= i##_len; ++i)
#define REP_R(i, n) for (long long int i = (n - 1); i >= 0; --i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((long long int)(x).size())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// #define DEBUG
#ifdef DEBUG
#define OUT(x) std::cout << (x) << std::endl
#define OUT2(x, y) std::cout << (x) << "," << (y) << std::endl
#define OUT3(x, y, z) std::cout << (x) << "," << (y) << "," << (z) << std::endl
#define OUTSP(x) std::cout << (x) << " ";
#define OUTLF std::cout << std::endl
#define OUTPA(p) OUT2(p.first, p.second)
#else
#define OUT(x) NULL
#define OUT2(x, y) NULL
#define OUT3(x, y, z) NULL
#define OUTPA(p) NULL
#define OUTSP(p) NULL
#define OUTLF NULL
#endif
#define INF INT_MAX
#define INFLL LLONG_MAX
#define PB push_back
#define MOD 1000000007LL
#define LSB(x) __builtin_ctzll(x)
#define MSB(x) 64LL - __builtin_clzll(x) - 1LL
#define MSBINT(x) 32LL - __builtin_clz(x)
#define BITCNT(x) __builtin_popcountll(x)
using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using vec = vector<ll>;
using mat = vector<vector<ll>>;
using pa = pair<ll, ll>;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
template <class T> inline void input(T &arr) {
ll n = arr.size();
REP(i, n)
cin >> arr[i];
}
template <class T> inline void input2(T &arr1, T &arr2) {
ll n = arr1.size();
REP(i, n)
cin >> arr1[i] >> arr2[i];
}
template <class T> inline void inputPA(T &arr) {
ll n = arr.size();
REP(i, n)
cin >> arr[i].first >> arr[i].second;
}
template <class T> inline void outArr(T &arr) {
for (auto x : arr) {
OUTSP(x);
}
OUTLF;
}
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};
const ll dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template <class T> bool chmax(T &, const T &);
template <class T> bool chmin(T &, const T &);
ll qp(ll a, ll b);
ll qp(ll a, ll b, ll mo);
ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void init_fact();
ll combination(ll, ll);
#define FACSIZE 200002LL
ll invfac[FACSIZE];
ll fac[FACSIZE];
/*
unordered_map<ll, ll> um;
unordered_map<ll, ll> umU;
unordered_map<ll, ll> umD;
*/
vec um;
vec umU;
vec umD;
ll amariU(ll x, ll d) {
if (x == 0) {
return umU[x] = 1 % d;
} else {
umU[x] = (umU[x - 1] * 2) % d;
}
return 0;
}
ll amariD(ll x, ll d) {
if (x == 0) {
return umD[x] = 1 % d;
} else {
umD[x] = (umD[x - 1] * 2) % d;
}
return 0;
}
ll f(ll x) {
if (x == 0)
return um[x] = 0;
else if (x == 1)
return um[x] = 1;
// if (um.find(x) != end(um))
if (um[x] != -1)
return um[x];
ll cnt = 0;
ll orgx = x;
while (x > 0) {
cnt += x % 2;
x /= 2;
}
return um[orgx] = f(orgx % cnt) + 1;
}
int main(void) {
um.resize(210000, -1);
umU.resize(210000);
umD.resize(210000);
ll n;
cin >> n;
string x;
cin >> x;
REP(i, 200110) { f(i); }
ll cnt = 0;
REP(i, x.size()) {
char c = x[i];
if (c == '1') {
cnt++;
}
}
if (cnt == 0) {
REP(i, n) { cout << 1 << endl; }
return 0;
} else if (cnt == 1) {
REP(i, 200010) { amariU(i, cnt + 1); }
ll baseMU = 0;
REP(i, n) {
auto c = x[n - 1 - i];
if (c == '1') {
baseMU += umU[i];
baseMU = baseMU % (cnt + 1);
}
}
REP(i, x.size()) {
char c = x[i];
if (c == '0') {
ll MU = baseMU + (cnt + 1 + umU[n - 1 - i]);
MU = MU % (cnt + 1);
cout << um[MU] + 1 << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
REP(i, 200010) {
amariU(i, cnt + 1);
amariD(i, cnt - 1);
}
ll baseMU = 0;
ll baseMD = 0;
REP(i, n) {
auto c = x[n - 1 - i];
if (c == '1') {
baseMU += umU[i];
baseMU = baseMU % (cnt + 1);
baseMD += umD[i];
baseMD = baseMD % (cnt - 1);
}
}
REP(i, x.size()) {
char c = x[i];
if (c == '0') {
ll MU = baseMU + (cnt + 1 + umU[n - 1 - i]);
MU = MU % (cnt + 1);
cout << um[MU] + 1 << endl;
} else {
ll MD = baseMD + (cnt - 1 - umD[n - 1 - i]);
MD = MD % (cnt - 1);
cout << um[MD] + 1 << endl;
}
}
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
ll qp(ll a, ll b) {
ll ans = 1LL;
do {
if (b & 1LL)
ans = 1LL * mod_mul(ans, a) % MOD;
a = 1LL * mod_mul(a, a) % MOD;
} while (b >>= 1LL);
return ans;
}
ll qp(ll a, ll b, ll mo) {
ll ans = 1LL;
do {
if (b & 1LL)
ans = 1LL * (ans % mo) * (a % mo);
a = 1LL * (a % mo) * (a % mo);
} while (b >>= 1LL);
return ans;
}
ll mod_pow(ll x, ll r) {
if (r == 0)
return 1;
else if (r == 1)
return x % MOD;
else if (r % 2 == 0) {
ll t = mod_pow(x, r / 2) % MOD;
return t * t % MOD;
} else {
ll t = mod_pow(x, r / 2) % MOD;
ll k = t * t % MOD;
return k * x % MOD;
}
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_mul(ll a, ll b) { return (a * b) % MOD; }
ll mod_div(ll a, ll b) { return mod_mul(a, mod_pow(b, MOD - 2)); }
void make_fact(ll n) {
fac[0] = 1;
REP(i, n) { fac[i + 1] = mod_mul(fac[i], i + 1); }
}
void make_invfact(ll n) {
invfac[n] = mod_pow(fac[n], MOD - 2);
REP_R(i, n) { invfac[i] = mod_mul(invfac[i + 1], i + 1); }
}
void init_fact() {
make_fact(FACSIZE - 1);
make_invfact(FACSIZE - 1);
}
ll combination(ll n, ll c) {
return mod_mul(fac[n], mod_mul(invfac[c], invfac[n - c]));
}
///////////////fact
/*
init_fact();
cout << fac[5] << endl;
cout << invfac[5] << endl;
cout << combination(8, 4) << endl;
cout << combination(31, 8) << endl;
//*/
///////////////////
| replace | 172 | 173 | 172 | 200 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fastInp \
cin.tie(0); \
cout.tie(0); \
ios_base::sync_with_stdio(0);
const ll SIZE = 1e5 * 2 + 10, INF = 1e9 + 10, MOD = 1e9 + 7;
ll binpow(ll a, ll n, ll md) {
if (n == 0)
return 1;
if (n % 2 == 1)
return (binpow(a, n - 1, md) * a) % md;
else {
ll b = binpow(a, n / 2, md);
return (b * b) % md;
}
}
ll bts(ll vl) {
ll ret = 0;
for (ll i = 60; i >= 0; i--) {
if ((vl >> i) & 1)
ret++;
}
return ret;
}
int main() {
fastInp;
ll n;
string s;
cin >> n;
cin >> s;
vector<pair<ll, ll>> vals;
vector<ll> sm(2);
ll cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1')
cnt++;
}
if (cnt == 0) {
for (int i = 0; i < n; i++) {
cout << "1\n";
}
return 0;
}
vals.resize(n);
for (int i = 0; i < s.size(); i++) {
ll vl1 = 0, vl2 = 0;
if (s[i] == '1') {
if (cnt + 1 >= 1)
vl1 = binpow(2, s.size() - i - 1, cnt + 1);
if (cnt - 1 >= 1)
vl2 = binpow(2, s.size() - i - 1, cnt - 1);
}
vals[i].first = vl1;
vals[i].second = vl2;
sm[0] += vl1;
sm[1] += vl2;
sm[0] %= cnt + 1;
sm[1] %= cnt - 1;
}
// cerr << sm[0] << " " << sm[1] << "\n";
for (int i = 0; i < s.size(); i++) {
// cerr << vals[i].first << " " << vals[i].second << "\n";
if (cnt == 1) {
if (s[i] == '0') {
ll vl = 0, c = 1;
vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) %
(cnt + 1);
while (vl > 0) {
vl = vl % bts(vl);
c++;
}
cout << c << "\n";
} else {
cout << "0\n";
}
continue;
}
ll vl = 0, c = 1;
if (s[i] == '0') {
vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) % (cnt + 1);
} else {
vl = (sm[1] - binpow(2, s.size() - i - 1, cnt - 1) + cnt - 1 + cnt - 1) %
(cnt - 1);
}
while (vl > 0) {
vl = vl % bts(vl);
c++;
}
cout << c << "\n";
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fastInp \
cin.tie(0); \
cout.tie(0); \
ios_base::sync_with_stdio(0);
const ll SIZE = 1e5 * 2 + 10, INF = 1e9 + 10, MOD = 1e9 + 7;
ll binpow(ll a, ll n, ll md) {
if (n == 0)
return 1;
if (n % 2 == 1)
return (binpow(a, n - 1, md) * a) % md;
else {
ll b = binpow(a, n / 2, md);
return (b * b) % md;
}
}
ll bts(ll vl) {
ll ret = 0;
for (ll i = 60; i >= 0; i--) {
if ((vl >> i) & 1)
ret++;
}
return ret;
}
int main() {
fastInp;
ll n;
string s;
cin >> n;
cin >> s;
vector<pair<ll, ll>> vals;
vector<ll> sm(2);
ll cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1')
cnt++;
}
if (cnt == 0) {
for (int i = 0; i < n; i++) {
cout << "1\n";
}
return 0;
}
vals.resize(n);
for (int i = 0; i < s.size(); i++) {
ll vl1 = 0, vl2 = 0;
if (s[i] == '1') {
if (cnt + 1 >= 1)
vl1 = binpow(2, s.size() - i - 1, cnt + 1);
if (cnt - 1 >= 1)
vl2 = binpow(2, s.size() - i - 1, cnt - 1);
}
vals[i].first = vl1;
vals[i].second = vl2;
sm[0] += vl1;
sm[1] += vl2;
if (cnt + 1 >= 1)
sm[0] %= cnt + 1;
if (cnt - 1 >= 1)
sm[1] %= cnt - 1;
}
// cerr << sm[0] << " " << sm[1] << "\n";
for (int i = 0; i < s.size(); i++) {
// cerr << vals[i].first << " " << vals[i].second << "\n";
if (cnt == 1) {
if (s[i] == '0') {
ll vl = 0, c = 1;
vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) %
(cnt + 1);
while (vl > 0) {
vl = vl % bts(vl);
c++;
}
cout << c << "\n";
} else {
cout << "0\n";
}
continue;
}
ll vl = 0, c = 1;
if (s[i] == '0') {
vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) % (cnt + 1);
} else {
vl = (sm[1] - binpow(2, s.size() - i - 1, cnt - 1) + cnt - 1 + cnt - 1) %
(cnt - 1);
}
while (vl > 0) {
vl = vl % bts(vl);
c++;
}
cout << c << "\n";
}
return 0;
} | replace | 77 | 79 | 77 | 81 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N], ans[N];
char s[N];
int main() {
int n, sum = 0, s1, s2, sum1 = 0, sum2 = 0, d1 = 1, d2 = 1;
scanf("%d%s", &n, s);
for (int i = 0; s[i]; i++)
sum += (s[i] ^= 0x30);
s1 = sum - 1, s2 = sum + 1;
f[0] = 1;
for (int i = 1; i <= s2; i++)
f[i] = 1 + f[i % __builtin_popcount(i)];
if (s1)
for (int i = n - 1; ~i; i--)
(sum1 += s[i] * d1) %= s1, (sum2 += s[i] * d2) %= s2, d1 = d1 * 2 % s1,
d2 = d2 * 2 % s2;
else
for (int i = n - 1; ~i; i--)
(sum2 += s[i] * d2) %= s2, d2 = d2 * 2 % s2;
d1 = d2 = 1;
for (int i = n - 1; ~i; i--) {
if (s[i])
ans[i] = s1 ? f[(sum1 - d1 + s1) % s1] : 0;
else
ans[i] = f[(sum2 + d2) % s2];
d2 = d2 * 2 % s2, d1 = d1 * 2 % s1;
}
for (int i = 0; i < n; i++)
printf("%d\n", ans[i]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N], ans[N];
char s[N];
int main() {
int n, sum = 0, s1, s2, sum1 = 0, sum2 = 0, d1 = 1, d2 = 1;
scanf("%d%s", &n, s);
for (int i = 0; s[i]; i++)
sum += (s[i] ^= 0x30);
s1 = sum - 1, s2 = sum + 1;
f[0] = 1;
for (int i = 1; i <= s2; i++)
f[i] = 1 + f[i % __builtin_popcount(i)];
if (s1)
for (int i = n - 1; ~i; i--)
(sum1 += s[i] * d1) %= s1, (sum2 += s[i] * d2) %= s2, d1 = d1 * 2 % s1,
d2 = d2 * 2 % s2;
else
for (int i = n - 1; ~i; i--)
(sum2 += s[i] * d2) %= s2, d2 = d2 * 2 % s2;
d1 = d2 = 1;
for (int i = n - 1; ~i; i--) {
if (s[i])
ans[i] = s1 ? f[(sum1 - d1 + s1) % s1] : 0;
else
ans[i] = f[(sum2 + d2) % s2];
d2 = d2 * 2 % s2;
if (s1)
d1 = d1 * 2 % s1;
}
for (int i = 0; i < n; i++)
printf("%d\n", ans[i]);
return 0;
} | replace | 27 | 28 | 27 | 30 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;
map<int, int> m;
string s;
int n;
int culc(int num) {
if (num == 0)
return 0;
if (num == 1)
return 1;
if (m.count(num) == 1)
return m[num];
int c = __builtin_popcount(num);
return m[num] = culc(num % c) + 1;
}
int main() {
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
cin >> n;
int count = 0;
cin >> s;
vector<vector<int>> cu(2, vector<int>(n + 2));
for (int i = 0; i < n; ++i) {
if (s[i] == '1')
count++;
}
cu[0][0] = 1;
cu[1][0] = 1;
for (int i = 0; i < n; ++i) {
cu[0][i + 1] = cu[0][i] * 2 % (count + 1);
if (count > 1)
cu[1][i + 1] = cu[1][i] * 2 % (count - 1);
}
vector<int> sum(2);
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
(sum[0] += cu[0][n - i - 1]) %= (count + 1);
(sum[1] += cu[1][n - i - 1]) %= (count - 1);
}
}
vector<int> ret(n);
for (int i = 0; i < n; ++i) {
int tmp = 0;
if (s[i] == '0')
tmp = (sum[0] + cu[0][n - i - 1]) % (count + 1);
else {
if (count == 1)
continue;
tmp =
((sum[1] - cu[1][n - i - 1]) % (count - 1) + count - 1) % (count - 1);
}
ret[i] = culc(tmp) + 1;
}
for (int i = 0; i < n; ++i)
cout << ret[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;
map<int, int> m;
string s;
int n;
int culc(int num) {
if (num == 0)
return 0;
if (num == 1)
return 1;
if (m.count(num) == 1)
return m[num];
int c = __builtin_popcount(num);
return m[num] = culc(num % c) + 1;
}
int main() {
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
cin >> n;
int count = 0;
cin >> s;
vector<vector<int>> cu(2, vector<int>(n + 2));
for (int i = 0; i < n; ++i) {
if (s[i] == '1')
count++;
}
cu[0][0] = 1;
cu[1][0] = 1;
for (int i = 0; i < n; ++i) {
cu[0][i + 1] = cu[0][i] * 2 % (count + 1);
if (count > 1)
cu[1][i + 1] = cu[1][i] * 2 % (count - 1);
}
vector<int> sum(2);
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
(sum[0] += cu[0][n - i - 1]) %= (count + 1);
if (count > 1)
(sum[1] += cu[1][n - i - 1]) %= (count - 1);
}
}
vector<int> ret(n);
for (int i = 0; i < n; ++i) {
int tmp = 0;
if (s[i] == '0')
tmp = (sum[0] + cu[0][n - i - 1]) % (count + 1);
else {
if (count == 1)
continue;
tmp =
((sum[1] - cu[1][n - i - 1]) % (count - 1) + count - 1) % (count - 1);
}
ret[i] = culc(tmp) + 1;
}
for (int i = 0; i < n; ++i)
cout << ret[i] << endl;
return 0;
} | replace | 44 | 45 | 44 | 46 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var) \
do { \
cout << #var << " : "; \
view(var); \
} while (0)
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
for (const auto &e : v) {
cout << e << " ";
}
cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int f(string s, int div) {
int n = s.size();
ll x = 0;
rep(i, n) {
if (s[i] == '1') {
x += modpow(2, n - 1 - i, div);
x %= div;
}
}
return x % div;
}
int f_(int x, int ct = 1) {
if (x == 0)
return ct;
int tmp = x % __builtin_popcount(x);
if (tmp == 0)
return ct + 1;
return f_(tmp, ct + 1);
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int div = 0;
rep(i, n) if (s[i] == '1') div++;
int x_plus = f(s, div + 1);
int x_minus = f(s, div - 1);
rep(i, n) {
int div_tmp = div;
string s_tmp = s;
if (s[i] == '0') {
s_tmp[i] = '1';
ll x = x_plus + modpow(2, n - 1 - i, div + 1);
x %= div + 1;
// debug(x);
cout << f_(x) << endl;
}
if (s[i] == '1') {
s_tmp[i] = '0';
if (div - 1 == 0)
cout << 0 << endl;
else {
ll x = x_minus - modpow(2, n - 1 - i, div - 1);
if (x < 0)
x += div - 1;
x %= div - 1;
// debug(x);
cout << f_(x) << endl;
}
}
// debug(s_tmp);
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var) \
do { \
cout << #var << " : "; \
view(var); \
} while (0)
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
for (const auto &e : v) {
cout << e << " ";
}
cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int f(string s, int div) {
int n = s.size();
ll x = 0;
rep(i, n) {
if (s[i] == '1') {
x += modpow(2, n - 1 - i, div);
x %= div;
}
}
return x % div;
}
int f_(int x, int ct = 1) {
if (x == 0)
return ct;
int tmp = x % __builtin_popcount(x);
if (tmp == 0)
return ct + 1;
return f_(tmp, ct + 1);
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int div = 0;
rep(i, n) if (s[i] == '1') div++;
int x_plus = f(s, div + 1);
int x_minus = -1;
if (div - 1 != 0)
x_minus = f(s, div - 1);
rep(i, n) {
int div_tmp = div;
string s_tmp = s;
if (s[i] == '0') {
s_tmp[i] = '1';
ll x = x_plus + modpow(2, n - 1 - i, div + 1);
x %= div + 1;
// debug(x);
cout << f_(x) << endl;
}
if (s[i] == '1') {
s_tmp[i] = '0';
if (div - 1 == 0)
cout << 0 << endl;
else {
ll x = x_minus - modpow(2, n - 1 - i, div - 1);
if (x < 0)
x += div - 1;
x %= div - 1;
// debug(x);
cout << f_(x) << endl;
}
}
// debug(s_tmp);
}
} | replace | 87 | 88 | 87 | 90 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long
const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで
using namespace std;
// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}
vector<int> x;
int ans[300];
int n2[300000];
int n3[300000];
signed main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int ne = 0;
int i2 = i;
while (i2 > 0) {
ne += i2 % 2;
i2 /= 2;
}
ans[i] = ans[i % ne] + 1;
}
int wa = 0;
REP(i, n) {
char x3;
cin >> x3;
int x1 = x3 - '0';
x.pb(x1);
wa += x1;
}
int ans1 = 0;
int ans2 = 0;
n2[n - 1] = 1;
REP(i, n) {
if (i != 0) {
n2[n - 1 - i] = n2[n - i] * 2 % (wa + 1);
}
ans1 += x[n - 1 - i] * n2[n - 1 - i] % (wa + 1);
ans1 %= (wa + 1);
}
if (wa == 1) {
} else {
n3[n - 1] = 1;
REP(i, n) {
if (i != 0) {
n3[n - 1 - i] = n3[n - i] * 2 % (wa - 1);
}
ans2 += x[n - 1 - i] * n3[n - 1 - i] % (wa - 1);
ans2 %= (wa - 1);
}
}
REP(i, n) {
if (x[i] == 1) {
if (wa == 1) {
cout << 0 << endl;
} else {
cout << ans[((wa - 1) + ans2 - n3[i] % (wa - 1)) % (wa - 1)] + 1
<< endl;
}
} else {
cout << ans[((ans1 + n2[i]) % (wa + 1)) % (wa + 1)] + 1 << endl;
}
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long
const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで
using namespace std;
// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}
vector<int> x;
int ans[300000];
int n2[300000];
int n3[300000];
signed main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int ne = 0;
int i2 = i;
while (i2 > 0) {
ne += i2 % 2;
i2 /= 2;
}
ans[i] = ans[i % ne] + 1;
}
int wa = 0;
REP(i, n) {
char x3;
cin >> x3;
int x1 = x3 - '0';
x.pb(x1);
wa += x1;
}
int ans1 = 0;
int ans2 = 0;
n2[n - 1] = 1;
REP(i, n) {
if (i != 0) {
n2[n - 1 - i] = n2[n - i] * 2 % (wa + 1);
}
ans1 += x[n - 1 - i] * n2[n - 1 - i] % (wa + 1);
ans1 %= (wa + 1);
}
if (wa == 1) {
} else {
n3[n - 1] = 1;
REP(i, n) {
if (i != 0) {
n3[n - 1 - i] = n3[n - i] * 2 % (wa - 1);
}
ans2 += x[n - 1 - i] * n3[n - 1 - i] % (wa - 1);
ans2 %= (wa - 1);
}
}
REP(i, n) {
if (x[i] == 1) {
if (wa == 1) {
cout << 0 << endl;
} else {
cout << ans[((wa - 1) + ans2 - n3[i] % (wa - 1)) % (wa - 1)] + 1
<< endl;
}
} else {
cout << ans[((ans1 + n2[i]) % (wa + 1)) % (wa + 1)] + 1 << endl;
}
}
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p02609 | C++ | Runtime Error | #pragma target("avx")
#pragma optimize("O3")
#include <bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T> using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
#define REP(i, b, e) for (ll i = b; i < e; i++)
#define ALL(vec) vec.begin(), vec.end()
#define PRINT(vec) \
printf("[ "); \
for (auto &i : vec) \
printf("%ld ", i); \
puts("]");
#define SCAN(vec) \
for (auto &i : vec) \
scanf("%ld", &i)
#define fi first
#define se second
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
ll modpow(ll a, ll p, ll m) {
ll ret = 1;
while (p) {
if (p % 2)
ret = ret * a % m;
a = a * a % m;
p >>= 1;
}
return ret;
}
ll rec(ll m, ll ans) {
if (m == 0)
return ans;
else
return rec(m % __builtin_popcount(m), ans + 1);
}
int main() {
int n;
scanf("%d", &n);
char s[n + 1];
scanf("%s", s);
int cnt = 0;
REP(i, 0, n) cnt += (s[i] == '1');
ll val_inc = 0, val_dec = 0, bi_inc = 1, bi_dec = 1;
REP(i, 0, n) {
if (s[n - i - 1] == '1') {
val_dec = (val_dec + bi_dec) % (cnt - 1);
val_inc = (val_inc + bi_inc) % (cnt + 1);
}
bi_dec = bi_dec * 2 % (cnt - 1);
bi_inc = bi_inc * 2 % (cnt + 1);
}
REP(i, 0, n) {
ll m, ans = 1;
if (s[i] == '1')
m = (val_dec - modpow(2, n - i - 1, cnt - 1) + (cnt - 1)) % (cnt - 1);
else
m = (val_inc + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
printf("%ld\n", rec(m, ans));
}
return 0;
}
| #pragma target("avx")
#pragma optimize("O3")
#include <bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll, ll>;
using PP = std::pair<ll, P>;
using V = std::vector<ll>;
template <typename T> using pq = std::priority_queue<T>;
template <typename T>
using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
#define REP(i, b, e) for (ll i = b; i < e; i++)
#define ALL(vec) vec.begin(), vec.end()
#define PRINT(vec) \
printf("[ "); \
for (auto &i : vec) \
printf("%ld ", i); \
puts("]");
#define SCAN(vec) \
for (auto &i : vec) \
scanf("%ld", &i)
#define fi first
#define se second
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
ll modpow(ll a, ll p, ll m) {
ll ret = 1;
while (p) {
if (p % 2)
ret = ret * a % m;
a = a * a % m;
p >>= 1;
}
return ret;
}
ll rec(ll m, ll ans) {
if (m == 0)
return ans;
else
return rec(m % __builtin_popcount(m), ans + 1);
}
int main() {
int n;
scanf("%d", &n);
char s[n + 1];
scanf("%s", s);
int cnt = 0;
REP(i, 0, n) cnt += (s[i] == '1');
if (cnt == 0) {
REP(i, 0, n) puts("1");
return 0;
} else if (cnt == 1) {
ll val_inc = 0, bi_inc = 1;
REP(i, 0, n) {
if (s[n - i - 1] == '1') {
val_inc = (val_inc + bi_inc) % (cnt + 1);
}
bi_inc = bi_inc * 2 % (cnt + 1);
}
REP(i, 0, n) {
if (s[i] == '1')
puts("0");
else {
ll m = (val_inc + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
printf("%ld\n", rec(m, 1));
}
}
return 0;
}
ll val_inc = 0, val_dec = 0, bi_inc = 1, bi_dec = 1;
REP(i, 0, n) {
if (s[n - i - 1] == '1') {
val_dec = (val_dec + bi_dec) % (cnt - 1);
val_inc = (val_inc + bi_inc) % (cnt + 1);
}
bi_dec = bi_dec * 2 % (cnt - 1);
bi_inc = bi_inc * 2 % (cnt + 1);
}
REP(i, 0, n) {
ll m, ans = 1;
if (s[i] == '1')
m = (val_dec - modpow(2, n - i - 1, cnt - 1) + (cnt - 1)) % (cnt - 1);
else
m = (val_inc + modpow(2, n - i - 1, cnt + 1)) % (cnt + 1);
printf("%ld\n", rec(m, ans));
}
return 0;
}
| insert | 53 | 53 | 53 | 77 | 0 | |
p02609 | C++ | Runtime Error | // Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define test() \
int t; \
cin >> t; \
while (t--)
#define all(v) v.begin(), v.end()
#define prinp(p) cout << p.first << " " << p.second << endl
#define prinv(V) \
for (auto v : V) \
cout << v << " "; \
cout << endl
#define take(V, f, n) \
for (int in = f; in < f + n; in++) \
cin >> V[in]
#define what(x) cerr << #x << " = " << x << endl
#define KStest() \
int t, t1; \
cin >> t; \
t1 = t; \
while (t--)
#define KScout cout << "Case #" << t1 - t << ": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAX = 1e6 + 5;
const ll INF = 1e18 + 5;
int MOD = 1e9 + 7;
/////////////////FastExp///////////////////
ll powN(ll a, ll p, int bit) {
if (p == 0)
return 1;
ll z = powN(a, p / 2, bit);
z = (z * z) % bit;
if (p % 2)
z = (z * a) % bit;
return z;
}
/////////////////FastExp///////////////////
///////////////////PnC/////////////////////
// vl Fac(MAX),Inv(MAX);
// void CalcFac()
// {
// Fac[0]=Inv[0]=1;
// for(int i=1;i<MAX;i++)
// {
// Fac[i]=(Fac[i-1]*i)%MOD;
// Inv[i]=powN(Fac[i],MOD-2);
// }
// }
// ll NCR(int n,int r)
// {
// if(r>n) return 0;
// return (((Fac[n]*Inv[r])%MOD)*Inv[n-r])%MOD;
// }
///////////////////PnC/////////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX, true);
vi MinDiv(MAX);
void Sieve() {
is_prime[0] = is_prime[1] = false;
int i, j;
for (i = 2; i * i <= MAX; i++) {
if (is_prime[i]) {
MinDiv[i] = i;
for (j = i * i; j <= MAX; j += i) {
is_prime[j] = false;
MinDiv[j] = i;
}
}
}
for (int i = 2; i <= MAX; i++)
if (is_prime[i])
MinDiv[i] = i;
}
//////////////////Sieve////////////////////
int main() {
int n;
cin >> n;
string X;
cin >> X;
int bit = 0;
for (auto x : X)
bit += (x == '1');
int bit2 = bit - 1;
// if(bit==0)
// {
// for(int i=0;i<n;i++)
// {
// cout<<1<<endl;
// }
// return 0;
// }
// else if(bit==1)
// {
// if(X[n-1]=='1')
// {
// for(int i=0;i<n;i++)
// {
// if(X[i]=='0')
// {
// cout<<2<<endl;
// }
// else cout<<0<<endl;
// }
// }
// else
// {
// for(int i=0;i<n;i++)
// {
// if(X[i]=='0')
// {
// cout<<1<<endl;
// }
// else cout<<0<<endl;
// }
// }
// return 0;
// }
bit++;
int curr = 1, curr2 = 1;
int NAO = 0, MNAO = 0;
for (int i = n - 1; i >= 0; i--) {
if (X[i] == '1') {
NAO += curr % bit;
NAO %= bit;
MNAO += curr2 % bit2;
MNAO %= bit2;
}
curr *= 2;
curr %= bit;
curr2 *= 2;
curr2 %= bit2;
}
// cout<<NAO<<endl;
// cout<<MNAO<<endl;
for (int i = 0; i < n; i++) {
int FN;
if (X[i] == '0') {
FN = NAO;
int ex = powN(2ll, (ll)n - i - 1, bit);
FN += ex;
FN %= bit;
} else {
FN = MNAO;
int ex = powN(2ll, (ll)n - i - 1, bit2);
FN -= ex;
FN %= bit2;
if (FN < 0)
FN += bit2;
}
int step = 1;
while (FN) {
int bitcnt = __builtin_popcount(FN);
FN %= bitcnt;
step++;
}
cout << step << endl;
}
} | // Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define test() \
int t; \
cin >> t; \
while (t--)
#define all(v) v.begin(), v.end()
#define prinp(p) cout << p.first << " " << p.second << endl
#define prinv(V) \
for (auto v : V) \
cout << v << " "; \
cout << endl
#define take(V, f, n) \
for (int in = f; in < f + n; in++) \
cin >> V[in]
#define what(x) cerr << #x << " = " << x << endl
#define KStest() \
int t, t1; \
cin >> t; \
t1 = t; \
while (t--)
#define KScout cout << "Case #" << t1 - t << ": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAX = 1e6 + 5;
const ll INF = 1e18 + 5;
int MOD = 1e9 + 7;
/////////////////FastExp///////////////////
ll powN(ll a, ll p, int bit) {
if (p == 0)
return 1;
ll z = powN(a, p / 2, bit);
z = (z * z) % bit;
if (p % 2)
z = (z * a) % bit;
return z;
}
/////////////////FastExp///////////////////
///////////////////PnC/////////////////////
// vl Fac(MAX),Inv(MAX);
// void CalcFac()
// {
// Fac[0]=Inv[0]=1;
// for(int i=1;i<MAX;i++)
// {
// Fac[i]=(Fac[i-1]*i)%MOD;
// Inv[i]=powN(Fac[i],MOD-2);
// }
// }
// ll NCR(int n,int r)
// {
// if(r>n) return 0;
// return (((Fac[n]*Inv[r])%MOD)*Inv[n-r])%MOD;
// }
///////////////////PnC/////////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX, true);
vi MinDiv(MAX);
void Sieve() {
is_prime[0] = is_prime[1] = false;
int i, j;
for (i = 2; i * i <= MAX; i++) {
if (is_prime[i]) {
MinDiv[i] = i;
for (j = i * i; j <= MAX; j += i) {
is_prime[j] = false;
MinDiv[j] = i;
}
}
}
for (int i = 2; i <= MAX; i++)
if (is_prime[i])
MinDiv[i] = i;
}
//////////////////Sieve////////////////////
int main() {
int n;
cin >> n;
string X;
cin >> X;
int bit = 0;
for (auto x : X)
bit += (x == '1');
int bit2 = bit - 1;
if (bit == 0) {
for (int i = 0; i < n; i++) {
cout << 1 << endl;
}
return 0;
} else if (bit == 1) {
if (X[n - 1] == '1') {
for (int i = 0; i < n; i++) {
if (X[i] == '0') {
cout << 2 << endl;
} else
cout << 0 << endl;
}
} else {
for (int i = 0; i < n; i++) {
if (i == n - 1) {
cout << 2 << endl;
} else if (X[i] == '0') {
cout << 1 << endl;
} else
cout << 0 << endl;
}
}
return 0;
}
bit++;
int curr = 1, curr2 = 1;
int NAO = 0, MNAO = 0;
for (int i = n - 1; i >= 0; i--) {
if (X[i] == '1') {
NAO += curr % bit;
NAO %= bit;
MNAO += curr2 % bit2;
MNAO %= bit2;
}
curr *= 2;
curr %= bit;
curr2 *= 2;
curr2 %= bit2;
}
// cout<<NAO<<endl;
// cout<<MNAO<<endl;
for (int i = 0; i < n; i++) {
int FN;
if (X[i] == '0') {
FN = NAO;
int ex = powN(2ll, (ll)n - i - 1, bit);
FN += ex;
FN %= bit;
} else {
FN = MNAO;
int ex = powN(2ll, (ll)n - i - 1, bit2);
FN -= ex;
FN %= bit2;
if (FN < 0)
FN += bit2;
}
int step = 1;
while (FN) {
int bitcnt = __builtin_popcount(FN);
FN %= bitcnt;
step++;
}
cout << step << endl;
}
} | replace | 103 | 137 | 103 | 128 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int deal(int x) {
int res = 0;
while (x) {
res++;
x = x % __builtin_popcount(x);
}
return res;
}
int n, p1 = -1, p2 = 1, x1, x2;
char s[200005];
int pw1[200005], pw2[200005];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s + 1;
for (int i = 1; i <= n; i++) {
p1 += s[i] == '1';
p2 += s[i] == '1';
}
pw1[0] = pw2[0] = 1;
for (int i = 1; i <= n; i++) {
if (p1)
x1 = (x1 << 1 | (s[i] == '1')) % p1;
x2 = (x2 << 1 | (s[i] == '1')) % p2;
if (p1)
pw1[i] = (pw1[i - 1] << 1) % p1;
pw2[i] = (pw2[i - 1] << 1) % p2;
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1') {
cout << deal((x1 - pw1[n - i] + p1) % p1) + 1 << endl;
} else {
cout << deal((x2 + pw2[n - i] + p2) % p2) + 1 << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int deal(int x) {
int res = 0;
while (x) {
res++;
x = x % __builtin_popcount(x);
}
return res;
}
int n, p1 = -1, p2 = 1, x1, x2;
char s[200005];
int pw1[200005], pw2[200005];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s + 1;
for (int i = 1; i <= n; i++) {
p1 += s[i] == '1';
p2 += s[i] == '1';
}
pw1[0] = pw2[0] = 1;
for (int i = 1; i <= n; i++) {
if (p1)
x1 = (x1 << 1 | (s[i] == '1')) % p1;
x2 = (x2 << 1 | (s[i] == '1')) % p2;
if (p1)
pw1[i] = (pw1[i - 1] << 1) % p1;
pw2[i] = (pw2[i - 1] << 1) % p2;
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1') {
if (p1)
cout << deal((x1 - pw1[n - i] + p1) % p1) + 1 << endl;
else
cout << 0 << endl;
} else {
cout << deal((x2 + pw2[n - i] + p2) % p2) + 1 << endl;
}
}
return 0;
} | replace | 38 | 39 | 38 | 42 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a <= b)
return 0;
a = b;
return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a >= b)
return 0;
a = b;
return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
ll beki[2][200020];
ll f(ll n) {
ll ret = 0;
ll tmp = n;
ll now = 0;
while (tmp) {
if (tmp % 2)
now++;
tmp /= 2;
}
if (now)
ret = 1 + f(n % now);
return ret;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1')
cnt++;
// mod -> cnt+1, cnt-1
ll l = 0, r = 0;
beki[0][0] = 1;
beki[1][0] = 1;
for (int i = 0; i < n; i++) {
l *= 2;
r *= 2;
beki[0][i + 1] = beki[0][i] * 2;
if (cnt != 1)
beki[1][i + 1] = beki[1][i] * 2;
if (s[i] == '1') {
l++;
r++;
}
l %= (cnt + 1);
beki[0][i + 1] %= (cnt + 1);
if (cnt != 1) {
r %= (cnt - 1);
beki[1][i + 1] %= (cnt - 1);
}
}
for (int i = 0; i < n; i++) {
ll now = 0;
if (s[i] == '1') {
// cnt-1
ll ans = r - beki[1][n - 1 - i] + cnt - 1;
if (cnt != 1)
now++;
ans %= (cnt - 1);
cout << now + f(ans) << "\n";
} else {
ll ans = l + beki[0][n - 1 - i];
now++;
ans %= (cnt + 1);
cout << now + f(ans) << "\n";
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
if (a <= b)
return 0;
a = b;
return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
if (a >= b)
return 0;
a = b;
return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
ll beki[2][200020];
ll f(ll n) {
ll ret = 0;
ll tmp = n;
ll now = 0;
while (tmp) {
if (tmp % 2)
now++;
tmp /= 2;
}
if (now)
ret = 1 + f(n % now);
return ret;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1')
cnt++;
// mod -> cnt+1, cnt-1
ll l = 0, r = 0;
beki[0][0] = 1;
beki[1][0] = 1;
for (int i = 0; i < n; i++) {
l *= 2;
r *= 2;
beki[0][i + 1] = beki[0][i] * 2;
if (cnt != 1)
beki[1][i + 1] = beki[1][i] * 2;
if (s[i] == '1') {
l++;
r++;
}
l %= (cnt + 1);
beki[0][i + 1] %= (cnt + 1);
if (cnt != 1) {
r %= (cnt - 1);
beki[1][i + 1] %= (cnt - 1);
}
}
for (int i = 0; i < n; i++) {
ll now = 0;
if (s[i] == '1') {
// cnt-1
ll ans = r - beki[1][n - 1 - i] + cnt - 1;
if (cnt != 1)
now++;
if (cnt != 1)
ans %= (cnt - 1);
cout << now + f(ans) << "\n";
} else {
ll ans = l + beki[0][n - 1 - i];
now++;
ans %= (cnt + 1);
cout << now + f(ans) << "\n";
}
}
} | replace | 75 | 76 | 75 | 77 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template <class T> void debug(T itr1, T itr2) {
auto now = itr1;
while (now < itr2) {
cout << *now << " ";
now++;
}
cout << endl;
}
#define repr(i, from, to) for (ll i = (ll)from; i < (ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)
const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;
vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
fac.resize(x, true);
rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
ll b = m, x = 1, y = 0;
while (b != 0) {
int d = a / b;
a -= b * d;
swap(a, b);
x -= y * d;
swap(x, y);
}
return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
if (a < b)
swap(a, b);
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll N;
string X;
ll calc(ll n) {
if (n <= 0)
return 0;
ll result = 0;
rep(i, N) result = (result * 2 + (X[i] - '0')) % n;
return result;
}
ll pcal(ll n) {
ll result = 0;
while (n) {
ll tn = n, pcnt = 0;
while (tn) {
pcnt += tn % 2;
tn /= 2;
}
n %= pcnt;
result++;
}
return result;
}
int main() {
cin >> N >> X;
ll count = 0;
rep(i, N) count += X[i] == '1';
ll to[2] = {calc(count - 1), calc(count + 1)};
// debug(to,to+2);
ll p2[2][N];
if (count - 1 > 0)
rep(i, N) p2[0][i] =
i == 0 ? 1 % (count - 1) : p2[0][i - 1] * 2 % (count - 1);
rep(i, N) p2[1][i] = i == 0 ? 1 : p2[1][i - 1] * 2 % (count + 1);
// debug(p2[0],p2[0]+N); debug(p2[1],p2[1]+N);
rep(i, N) {
if (X[0] == '1' && count == 1) {
cout << 0 << endl;
continue;
}
ll c1 = X[i] == '0'
? (to[1] + p2[1][(N - 1) - i]) % (count + 1)
: ((to[0] - p2[0][(N - 1) - i]) % (count - 1) + (count - 1)) %
(count - 1);
ll result = 1 + pcal(c1);
cout << result << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template <class T> void debug(T itr1, T itr2) {
auto now = itr1;
while (now < itr2) {
cout << *now << " ";
now++;
}
cout << endl;
}
#define repr(i, from, to) for (ll i = (ll)from; i < (ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)
const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;
vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
fac.resize(x, true);
rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
ll b = m, x = 1, y = 0;
while (b != 0) {
int d = a / b;
a -= b * d;
swap(a, b);
x -= y * d;
swap(x, y);
}
return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
if (a < b)
swap(a, b);
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll N;
string X;
ll calc(ll n) {
if (n <= 0)
return 0;
ll result = 0;
rep(i, N) result = (result * 2 + (X[i] - '0')) % n;
return result;
}
ll pcal(ll n) {
ll result = 0;
while (n) {
ll tn = n, pcnt = 0;
while (tn) {
pcnt += tn % 2;
tn /= 2;
}
n %= pcnt;
result++;
}
return result;
}
int main() {
cin >> N >> X;
ll count = 0;
rep(i, N) count += X[i] == '1';
ll to[2] = {calc(count - 1), calc(count + 1)};
// debug(to,to+2);
ll p2[2][N];
if (count - 1 > 0)
rep(i, N) p2[0][i] =
i == 0 ? 1 % (count - 1) : p2[0][i - 1] * 2 % (count - 1);
rep(i, N) p2[1][i] = i == 0 ? 1 : p2[1][i - 1] * 2 % (count + 1);
// debug(p2[0],p2[0]+N); debug(p2[1],p2[1]+N);
rep(i, N) {
if (X[i] == '1' && count == 1) {
cout << 0 << endl;
continue;
}
ll c1 = X[i] == '0'
? (to[1] + p2[1][(N - 1) - i]) % (count + 1)
: ((to[0] - p2[0][(N - 1) - i]) % (count - 1) + (count - 1)) %
(count - 1);
ll result = 1 + pcal(c1);
cout << result << endl;
}
return 0;
} | replace | 87 | 88 | 87 | 88 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <typename T> inline void readin(T &x) {
x = 0;
T fh = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
fh = -1;
for (; isdigit(ch); ch = getchar())
x = (x << 1) + (x << 3) + (ch ^ 48);
x *= fh;
}
template <typename T> inline void wt(T x) {
if (x > 9)
wt(x / 10);
putchar(x % 10 + 48);
}
template <typename T> inline void writeln(T x, char c) {
if (x < 0) {
putchar('-');
x = -x;
}
wt(x);
putchar(c);
}
int f[10000005];
inline void dfs(int x, int dep, int tot) {
f[x] = tot;
if (dep >= 18)
return;
dfs(x << 1, dep + 1, tot);
dfs(x << 1 | 1, dep + 1, tot + 1);
}
char s[1000005];
int n, m[3], cnt;
inline int qp(int x, int y, int mod) {
int res = 1;
while (y) {
if (y & 1)
res = (1ll * res * x) % mod;
x = (1ll * x * x) % mod;
y >>= 1;
}
return res;
}
inline int work(int x) {
int sum = 0;
if (s[x] == '1') {
sum = m[1];
sum -= qp(2, n - x, cnt - 1);
sum %= (cnt - 1);
if (sum < 0)
sum += cnt - 1;
} else {
sum = m[2];
sum += qp(2, n - x, cnt + 1);
sum %= (cnt + 1);
}
return sum;
}
int main() {
dfs(0, 0, 0);
readin(n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) {
if (s[i] == '1')
cnt++;
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1') {
if (cnt > 1)
m[1] += qp(2, n - i, cnt - 1);
m[2] += qp(2, n - i, cnt + 1);
}
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1' && cnt == 1) {
puts("0");
continue;
}
int k = work(i);
int tim = 1;
while (k) {
k %= f[k];
tim++;
}
writeln(tim, '\n');
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> inline void readin(T &x) {
x = 0;
T fh = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
fh = -1;
for (; isdigit(ch); ch = getchar())
x = (x << 1) + (x << 3) + (ch ^ 48);
x *= fh;
}
template <typename T> inline void wt(T x) {
if (x > 9)
wt(x / 10);
putchar(x % 10 + 48);
}
template <typename T> inline void writeln(T x, char c) {
if (x < 0) {
putchar('-');
x = -x;
}
wt(x);
putchar(c);
}
int f[10000005];
inline void dfs(int x, int dep, int tot) {
f[x] = tot;
if (dep >= 18)
return;
dfs(x << 1, dep + 1, tot);
dfs(x << 1 | 1, dep + 1, tot + 1);
}
char s[1000005];
int n, m[3], cnt;
inline int qp(int x, int y, int mod) {
int res = 1;
while (y) {
if (y & 1)
res = (1ll * res * x) % mod;
x = (1ll * x * x) % mod;
y >>= 1;
}
return res;
}
inline int work(int x) {
int sum = 0;
if (s[x] == '1') {
sum = m[1];
sum -= qp(2, n - x, cnt - 1);
sum %= (cnt - 1);
if (sum < 0)
sum += cnt - 1;
} else {
sum = m[2];
sum += qp(2, n - x, cnt + 1);
sum %= (cnt + 1);
}
return sum;
}
int main() {
dfs(0, 0, 0);
readin(n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) {
if (s[i] == '1')
cnt++;
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1') {
if (cnt > 1) {
m[1] = (m[1] + qp(2, n - i, cnt - 1)) % (cnt - 1);
}
m[2] = (m[2] + qp(2, n - i, cnt + 1)) % (cnt + 1);
}
}
for (int i = 1; i <= n; i++) {
if (s[i] == '1' && cnt == 1) {
puts("0");
continue;
}
int k = work(i);
int tim = 1;
while (k) {
k %= f[k];
tim++;
}
writeln(tim, '\n');
}
return 0;
}
| replace | 71 | 74 | 71 | 75 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
int Rec(int x) {
static int mem[200001];
static int used[200001];
if (used[x])
return mem[x];
used[x] = 1;
if (x == 0)
return 0;
int cnt = __builtin_popcount(x);
return mem[x] = Rec(x % cnt) + 1;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < n; ++i)
cnt += s[i] == '1';
int cnti = cnt + 1;
int cntd = cnt - 1;
int xi = 0, xd = 0, yi = 1, yd = 1;
int xx = 0, yy = 1;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '1') {
xx += yy;
xi = (xi + yi) % cnti;
if (cntd > 0)
xd = (xd + yd) % cntd;
}
yi = (yi * 2) % cnti;
if (cntd > 0)
yd = (yd * 2) % cntd;
yy = yy * 2;
}
yi = yd = 1;
vector<int> ans;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '1') {
if (cntd == 0) {
ans.push_back(0);
} else {
int x = (xd + cntd - yd) % cntd;
ans.push_back(Rec(x) + 1);
}
} else {
int x = (xi + yi) % cnti;
ans.push_back(Rec(x) + 1);
}
yi = (yi * 2) % cnti;
yd = (yd * 2) % cntd;
}
for (int i = n - 1; i >= 0; --i) {
cout << ans[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;
template <class T> bool ChangeMax(T &a, const T &b) {
if (a >= b)
return false;
a = b;
return true;
}
template <class T> bool ChangeMin(T &a, const T &b) {
if (a <= b)
return false;
a = b;
return true;
}
template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
fill((U *)a, (U *)(a + N), v);
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v)
is >> e;
return is;
}
int Rec(int x) {
static int mem[200001];
static int used[200001];
if (used[x])
return mem[x];
used[x] = 1;
if (x == 0)
return 0;
int cnt = __builtin_popcount(x);
return mem[x] = Rec(x % cnt) + 1;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < n; ++i)
cnt += s[i] == '1';
int cnti = cnt + 1;
int cntd = cnt - 1;
int xi = 0, xd = 0, yi = 1, yd = 1;
int xx = 0, yy = 1;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '1') {
xx += yy;
xi = (xi + yi) % cnti;
if (cntd > 0)
xd = (xd + yd) % cntd;
}
yi = (yi * 2) % cnti;
if (cntd > 0)
yd = (yd * 2) % cntd;
yy = yy * 2;
}
yi = yd = 1;
vector<int> ans;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '1') {
if (cntd == 0) {
ans.push_back(0);
} else {
int x = (xd + cntd - yd) % cntd;
ans.push_back(Rec(x) + 1);
}
} else {
int x = (xi + yi) % cnti;
ans.push_back(Rec(x) + 1);
}
yi = (yi * 2) % cnti;
if (cntd > 0)
yd = (yd * 2) % cntd;
}
for (int i = n - 1; i >= 0; --i) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 89 | 90 | 89 | 91 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;
// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>
void pr_init() {
#ifndef ONLINE_JUDGE
freopen("gin.txt", "r", stdin);
freopen("gout.txt", "w", stdout);
#endif
}
int raise(int a, int b, int m) {
int an = 1;
while (b > 0) {
if (b % 2 != 0)
an = ((an % m) * (a % m)) % m;
b /= 2;
a = ((a % m) * (a % m)) % m;
}
return an;
}
int dp[2];
int process(int a) {
int an = 0;
while (a > 0) {
int cn = 0;
for (int i = 0; i <= 32; i++) {
if ((a >> i) & 1)
cn++;
}
an++;
a %= cn;
}
return an;
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int cn = 0;
forf(i, 0, n) {
if (s[i] == '1')
cn++;
}
forf(j, 0, n) {
if (cn > 1)
dp[0] += (raise(2, n - j - 1, cn - 1) * (s[j] - '0')) % (cn - 1),
dp[0] %= (cn - 1);
if (cn < n)
dp[1] += (raise(2, n - j - 1, cn + 1) * (s[j] - '0')) % (cn + 1),
dp[1] %= (cn + 1);
}
forf(i, 0, n) {
if (s[i] == '0') {
int an = dp[1];
an = an + raise(2, n - i - 1, cn + 1);
an %= cn + 1;
cout << process(an) + 1 << "\n";
} else {
int an = dp[0];
an = an - raise(2, n - i - 1, cn - 1) + cn - 1;
an %= cn - 1;
cout << process(an) + 1 << "\n";
}
}
}
int32_t main() {
pr_init();
fastio;
auto start = high_resolution_clock::now();
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: "
// << duration.count() << " microseconds" << endl;
}
| #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;
// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>
void pr_init() {
#ifndef ONLINE_JUDGE
freopen("gin.txt", "r", stdin);
freopen("gout.txt", "w", stdout);
#endif
}
int raise(int a, int b, int m) {
int an = 1;
while (b > 0) {
if (b % 2 != 0)
an = ((an % m) * (a % m)) % m;
b /= 2;
a = ((a % m) * (a % m)) % m;
}
return an;
}
int dp[2];
int process(int a) {
int an = 0;
while (a > 0) {
int cn = 0;
for (int i = 0; i <= 32; i++) {
if ((a >> i) & 1)
cn++;
}
an++;
a %= cn;
}
return an;
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int cn = 0;
forf(i, 0, n) {
if (s[i] == '1')
cn++;
}
forf(j, 0, n) {
if (cn > 1)
dp[0] += (raise(2, n - j - 1, cn - 1) * (s[j] - '0')) % (cn - 1),
dp[0] %= (cn - 1);
if (cn < n)
dp[1] += (raise(2, n - j - 1, cn + 1) * (s[j] - '0')) % (cn + 1),
dp[1] %= (cn + 1);
}
forf(i, 0, n) {
if (s[i] == '0') {
int an = dp[1];
an = an + raise(2, n - i - 1, cn + 1);
an %= cn + 1;
cout << process(an) + 1 << "\n";
} else {
if (cn == 1) {
cout << "0\n";
continue;
}
int an = dp[0];
an = an - raise(2, n - i - 1, cn - 1) + cn - 1;
an %= cn - 1;
cout << process(an) + 1 << "\n";
}
}
}
int32_t main() {
pr_init();
fastio;
auto start = high_resolution_clock::now();
solve();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: "
// << duration.count() << " microseconds" << endl;
}
| insert | 113 | 113 | 113 | 117 | -11 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
typedef long long ll;
constexpr long long INFLL = 1e18;
using namespace std;
int f(int temp) {
if (temp == 0) {
return 0;
} else {
return f(temp % __builtin_popcount(temp)) + 1;
}
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> x(n);
int pc = 0;
for (int i = 0; i < n; i++) {
x.at(i) = s.at(i) - '0';
if (x.at(i)) {
pc++;
}
}
vector<int> ans(n);
for (int i = 0; i < 2; i++) {
int pc2 = pc;
if (i == 0) {
pc2++;
} else {
pc2--;
}
int sum = 0;
for (int j = 0; j < n; j++) {
sum = (sum * 2) % pc2;
sum += x.at(j);
}
int temp = 1;
int temp2;
for (int j = n - 1; j >= 0; j--) {
if (i == 0) {
if (x.at(j) == 0) {
temp2 = (sum + temp) % pc2;
ans.at(j) = f(temp2) + 1;
}
} else {
if (x.at(j) == 1) {
temp2 = (sum - temp + pc2) % pc2;
ans.at(j) = f(temp2) + 1;
}
}
temp = (temp * 2) % pc2;
}
}
for (int i = 0; i < n; i++) {
cout << ans.at(i) << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
typedef long long ll;
constexpr long long INFLL = 1e18;
using namespace std;
int f(int temp) {
if (temp == 0) {
return 0;
} else {
return f(temp % __builtin_popcount(temp)) + 1;
}
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> x(n);
int pc = 0;
for (int i = 0; i < n; i++) {
x.at(i) = s.at(i) - '0';
if (x.at(i)) {
pc++;
}
}
vector<int> ans(n);
for (int i = 0; i < 2; i++) {
int pc2 = pc;
if (i == 0) {
pc2++;
} else {
pc2--;
}
if (pc2 <= 0) {
continue;
}
int sum = 0;
for (int j = 0; j < n; j++) {
sum = (sum * 2) % pc2;
sum += x.at(j);
}
int temp = 1;
int temp2;
for (int j = n - 1; j >= 0; j--) {
if (i == 0) {
if (x.at(j) == 0) {
temp2 = (sum + temp) % pc2;
ans.at(j) = f(temp2) + 1;
}
} else {
if (x.at(j) == 1) {
temp2 = (sum - temp + pc2) % pc2;
ans.at(j) = f(temp2) + 1;
}
}
temp = (temp * 2) % pc2;
}
}
for (int i = 0; i < n; i++) {
cout << ans.at(i) << endl;
}
return 0;
} | insert | 48 | 48 | 48 | 51 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define sz(a) (int)a.size()
using namespace std;
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
rep(i, n) {
if (s[i] == '1')
cnt++;
}
map<int, int> mp;
mp[0] = 0;
rep(i, cnt + 10) {
int ai = i + 1;
int cnti = 0;
rep(iiii, 20) {
if (ai & (1 << iiii))
cnti++;
}
mp[ai] = mp[ai % cnti] + 1;
}
int cnt1 = cnt + 1;
int cnt2 = cnt - 1;
int amari1 = 0;
int amari2 = 0;
int amari = 1;
rep(i, n) {
int ai = amari * (s[n - 1 - i] - '0');
amari1 = (amari1 + ai) % cnt1;
amari = amari * 2 % cnt1;
}
amari = 1;
rep(i, n) {
int ai = amari * (s[n - 1 - i] - '0');
amari2 = (amari2 + ai) % cnt2;
amari = amari * 2 % cnt2;
}
rep(i, n) {
if (s[i] == '0') {
int amari1i = amari1;
amari1i += modpow(2, (n - 1 - i), cnt1);
amari1i %= cnt1;
cout << mp[amari1i] + 1 << endl;
} else {
if (cnt2 == 0) {
cout << 0 << endl;
continue;
}
int amari2i = amari2;
amari2i -= modpow(2, (n - 1 - i), cnt2);
amari2i = (amari2i + cnt2) % cnt2;
cout << mp[amari2i] + 1 << endl;
}
}
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define sz(a) (int)a.size()
using namespace std;
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
rep(i, n) {
if (s[i] == '1')
cnt++;
}
map<int, int> mp;
mp[0] = 0;
rep(i, cnt + 10) {
int ai = i + 1;
int cnti = 0;
rep(iiii, 20) {
if (ai & (1 << iiii))
cnti++;
}
mp[ai] = mp[ai % cnti] + 1;
}
int cnt1 = cnt + 1;
int cnt2 = cnt - 1;
int amari1 = 0;
int amari2 = 0;
int amari = 1;
rep(i, n) {
int ai = amari * (s[n - 1 - i] - '0');
amari1 = (amari1 + ai) % cnt1;
amari = amari * 2 % cnt1;
}
amari = 1;
if (cnt2 != 0) {
rep(i, n) {
int ai = amari * (s[n - 1 - i] - '0');
amari2 = (amari2 + ai) % cnt2;
amari = amari * 2 % cnt2;
}
}
rep(i, n) {
if (s[i] == '0') {
int amari1i = amari1;
amari1i += modpow(2, (n - 1 - i), cnt1);
amari1i %= cnt1;
cout << mp[amari1i] + 1 << endl;
} else {
if (cnt2 == 0) {
cout << 0 << endl;
continue;
}
int amari2i = amari2;
amari2i -= modpow(2, (n - 1 - i), cnt2);
amari2i = (amari2i + cnt2) % cnt2;
cout << mp[amari2i] + 1 << endl;
}
}
}
| replace | 61 | 65 | 61 | 67 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int calc_bit_count_for_string(const string &S, const int N) {
int ans = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1') {
ans++;
}
}
return ans;
}
int calc_bit_count_for_num(ll x) {
assert(x >= 0);
int ans = 0;
while (x != 0) {
if (x % 2 == 1) {
ans++;
}
x >>= 1;
}
return ans;
}
ll mod_pow(const ll base, const ll n, const int mod) {
assert(n >= 0);
assert(mod > 0);
if (n == 0) {
return 1LL % mod;
}
ll ans = mod_pow(base, n / 2, mod);
ans = (ans * ans) % mod;
if (n % 2 == 1) {
ans = (ans * base) % mod;
}
return ans;
}
ll calc_first(const int mod, const string &S, const int N) {
assert(mod > 0);
ll ans = 0;
for (int i = 0; i < N; i++) {
ans = (((ans * 2) % mod) + (S[i] - '0')) % mod;
}
return ans;
}
// 11:53
int main(void) {
int N;
cin >> N;
string S;
cin >> S;
vector<int> ans(N, 0);
// for zero
{
const int mod = calc_bit_count_for_string(S, N) + 1;
ll original = calc_first(mod, S, N);
for (int i = 0; i < N; i++) {
if (S[i] == '0') {
ll acc = original;
int cnt = 0;
acc = (acc + mod_pow(2, N - 1 - i, mod)) % mod;
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
while (acc > 0) {
acc = acc % calc_bit_count_for_num(acc);
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
}
ans[i] = cnt;
}
}
}
// for one
{
const int mod = calc_bit_count_for_string(S, N) - 1;
if (mod != 0) {
ll original = calc_first(mod, S, N);
for (int i = 0; i < N; i++) {
if (S[i] == '1') {
ll acc = original;
int cnt = 0;
acc = (acc + mod - mod_pow(2, N - 1 - i, mod)) % mod;
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
while (acc > 0) {
acc = acc % calc_bit_count_for_num(acc);
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
}
ans[i] = cnt;
}
}
}
}
for (int i = 0; i < N; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int calc_bit_count_for_string(const string &S, const int N) {
int ans = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1') {
ans++;
}
}
return ans;
}
int calc_bit_count_for_num(ll x) {
assert(x >= 0);
int ans = 0;
while (x != 0) {
if (x % 2 == 1) {
ans++;
}
x >>= 1;
}
return ans;
}
ll mod_pow(const ll base, const ll n, const int mod) {
assert(n >= 0);
assert(mod > 0);
if (n == 0) {
return 1LL % mod;
}
ll ans = mod_pow(base, n / 2, mod);
ans = (ans * ans) % mod;
if (n % 2 == 1) {
ans = (ans * base) % mod;
}
return ans;
}
ll calc_first(const int mod, const string &S, const int N) {
assert(mod > 0);
ll ans = 0;
for (int i = 0; i < N; i++) {
ans = (((ans * 2) % mod) + (S[i] - '0')) % mod;
}
return ans;
}
// 11:53
int main(void) {
int N;
cin >> N;
string S;
cin >> S;
vector<int> ans(N, 0);
// for zero
{
const int mod = calc_bit_count_for_string(S, N) + 1;
ll original = calc_first(mod, S, N);
for (int i = 0; i < N; i++) {
if (S[i] == '0') {
ll acc = original;
int cnt = 0;
acc = (acc + mod_pow(2, N - 1 - i, mod)) % mod;
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
while (acc > 0) {
acc = acc % calc_bit_count_for_num(acc);
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
}
ans[i] = cnt;
}
}
}
// for one
{
const int mod = calc_bit_count_for_string(S, N) - 1;
if (mod > 0) {
ll original = calc_first(mod, S, N);
for (int i = 0; i < N; i++) {
if (S[i] == '1') {
ll acc = original;
int cnt = 0;
acc = (acc + mod - mod_pow(2, N - 1 - i, mod)) % mod;
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
while (acc > 0) {
acc = acc % calc_bit_count_for_num(acc);
// printf("i: %d, mod: %d, acc: %lld\n", i, mod, acc);
cnt++;
}
ans[i] = cnt;
}
}
}
}
for (int i = 0; i < N; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| replace | 111 | 112 | 111 | 112 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
int main() {
int n;
cin >> n;
string s;
cin >> s;
int one = 0;
REP(i, n) {
if (s[i] == '1')
one++;
}
int p1 = 1, p2 = 1, sum1 = 0, sum2 = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
sum1 = (sum1 + p1) % (one + 1);
sum2 = (sum2 + p2) % (one - 1);
}
p1 = (p1 * 2) % (one + 1);
p2 = (p2 * 2) % (one - 1);
}
VI anss(n);
p1 = 1, p2 = 1;
for (int i = n - 1; i >= 0; i--) {
int ans = 1;
if (s[i] == '1') {
int sum = sum2;
sum += (one - 1 - p2);
sum %= one - 1;
while (sum != 0) {
int md = 0;
REP(i, 30) {
if (sum >> i & 1)
md++;
}
sum %= md;
ans++;
}
} else {
int sum = sum1;
sum += p1;
sum %= one + 1;
while (sum != 0) {
int md = 0;
REP(i, 30) {
if (sum >> i & 1)
md++;
}
sum %= md;
ans++;
}
}
anss[i] = ans;
p1 = (p1 * 2) % (one + 1);
p2 = (p2 * 2) % (one - 1);
}
REP(i, n)
cout << anss[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
int main() {
int n;
cin >> n;
string s;
cin >> s;
int one = 0;
REP(i, n) {
if (s[i] == '1')
one++;
}
if (one == 0) {
REP(i, n)
cout << 1 << endl;
return 0;
}
if (one == 1) {
if (s[n - 1] == '1') {
REP(i, n) {
if (s[i] == '1')
cout << 0 << endl;
else
cout << 2 << endl;
}
} else {
REP(i, n) {
if (i == n - 1)
cout << 2 << endl;
else if (s[i] == '1')
cout << 0 << endl;
else
cout << 1 << endl;
}
}
return 0;
}
int p1 = 1, p2 = 1, sum1 = 0, sum2 = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
sum1 = (sum1 + p1) % (one + 1);
sum2 = (sum2 + p2) % (one - 1);
}
p1 = (p1 * 2) % (one + 1);
p2 = (p2 * 2) % (one - 1);
}
VI anss(n);
p1 = 1, p2 = 1;
for (int i = n - 1; i >= 0; i--) {
int ans = 1;
if (s[i] == '1') {
int sum = sum2;
sum += (one - 1 - p2);
sum %= one - 1;
while (sum != 0) {
int md = 0;
REP(i, 30) {
if (sum >> i & 1)
md++;
}
sum %= md;
ans++;
}
} else {
int sum = sum1;
sum += p1;
sum %= one + 1;
while (sum != 0) {
int md = 0;
REP(i, 30) {
if (sum >> i & 1)
md++;
}
sum %= md;
ans++;
}
}
anss[i] = ans;
p1 = (p1 * 2) % (one + 1);
p2 = (p2 * 2) % (one - 1);
}
REP(i, n)
cout << anss[i] << endl;
return 0;
} | insert | 20 | 20 | 20 | 45 | 0 | |
p02609 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(pp, nn) for (int ii = pp; ii < nn; ++ii)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
string int_to_string(int x) {
stringstream ss;
ss << x;
string ni = ss.str();
return ni;
}
constexpr int mod = 1e9 + 7;
ll POW(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = ans * a;
}
a = a * a;
b /= 2;
}
return ans;
}
ll power(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return ans;
}
ll gcd(ll a, ll b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isprime(ll n) {
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
ll power_mod(ll a, ll b, ll mo) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans % mo * a % mo) % mo;
}
a = (a % mo * a % mo) % mo;
b /= 2;
}
return ans;
}
ll call(ll cnt, string s) {
ll p = 0;
ll x = 0;
for (int i = (int)s.size() - 1; i >= 0; --i) {
ll y = 0;
if (s[i] == '1') {
y = power_mod(2LL, p, cnt);
}
x = (y % cnt + x % cnt) % cnt;
p++;
}
return x;
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '1')
cnt++;
}
// cout << cnt << endl;
ll inc_val = call(cnt + 1, s);
ll dec_val = (cnt > 0) ? call(cnt - 1, s) : inc_val;
// cout << inc_val << " "<< dec_val << endl;
for (int i = 0; i < n; ++i) {
if (s[i] == '0')
cnt++, s[i] = '1';
else
cnt--, s[i] = '0';
if (cnt == 0) {
cout << 0 << endl;
if (s[i] == '1')
cnt--, s[i] = '0';
else
cnt++, s[i] = '1';
continue;
}
ll val = 0;
if (s[i] == '1') {
// inc_val
val = (power_mod(2LL, n - i - 1, cnt) + inc_val + cnt) % cnt;
} else {
val = (-power_mod(2LL, n - i - 1, cnt) + dec_val + cnt) % cnt;
}
ll step = 1;
// cout << val << " " << cnt << " ";
if (s[i] == '1')
cnt--, s[i] = '0';
else
cnt++, s[i] = '1';
while (val) {
ll p = 0;
ll x = val;
while (x) {
if (x & 1)
p++;
x /= 2;
}
val = val % p;
step++;
}
cout << step << endl;
}
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(pp, nn) for (int ii = pp; ii < nn; ++ii)
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
string int_to_string(int x) {
stringstream ss;
ss << x;
string ni = ss.str();
return ni;
}
constexpr int mod = 1e9 + 7;
ll POW(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = ans * a;
}
a = a * a;
b /= 2;
}
return ans;
}
ll power(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return ans;
}
ll gcd(ll a, ll b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
bool isprime(ll n) {
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
ll power_mod(ll a, ll b, ll mo) {
ll ans = 1;
while (b) {
if (b & 1) {
ans = (ans % mo * a % mo) % mo;
}
a = (a % mo * a % mo) % mo;
b /= 2;
}
return ans;
}
ll call(ll cnt, string s) {
ll p = 0;
ll x = 0;
for (int i = (int)s.size() - 1; i >= 0; --i) {
ll y = 0;
if (s[i] == '1') {
y = power_mod(2LL, p, cnt);
}
x = (y % cnt + x % cnt) % cnt;
p++;
}
return x;
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '1')
cnt++;
}
// cout << cnt << endl;
ll inc_val = call(cnt + 1, s);
ll dec_val = (cnt > 1) ? call(cnt - 1, s) : inc_val;
// cout << inc_val << " "<< dec_val << endl;
for (int i = 0; i < n; ++i) {
if (s[i] == '0')
cnt++, s[i] = '1';
else
cnt--, s[i] = '0';
if (cnt == 0) {
cout << 0 << endl;
if (s[i] == '1')
cnt--, s[i] = '0';
else
cnt++, s[i] = '1';
continue;
}
ll val = 0;
if (s[i] == '1') {
// inc_val
val = (power_mod(2LL, n - i - 1, cnt) + inc_val + cnt) % cnt;
} else {
val = (-power_mod(2LL, n - i - 1, cnt) + dec_val + cnt) % cnt;
}
ll step = 1;
// cout << val << " " << cnt << " ";
if (s[i] == '1')
cnt--, s[i] = '0';
else
cnt++, s[i] = '1';
while (val) {
ll p = 0;
ll x = val;
while (x) {
if (x & 1)
p++;
x /= 2;
}
val = val % p;
step++;
}
cout << step << endl;
}
}
| replace | 95 | 96 | 95 | 96 | 0 | |
p02609 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
int main() {
int n;
int sum = 0;
std::string S;
std::cin >> n >> S;
for (int i = 0; i < n; i++) {
if (S[i] == '1')
sum++;
}
// 例外処理
if (sum == 0) {
for (int i = 0; i < n; i++) {
std::cout << 1 << std::endl;
}
return 0;
}
// 2^iの剰余
std::vector<int> count_p1(n), count_m1(n);
count_p1[0] = 1 % (sum + 1);
count_m1[0] = 1 % (sum - 1);
for (int i = 1; i < n; i++) {
count_p1[i] = (count_p1[i - 1] * 2) % (sum + 1);
if (sum != 1)
count_m1[i] = (count_m1[i - 1] * 2) % (sum - 1);
}
// Xに対して事前の剰余計算
int r_p1 = 0, r_m1 = 0;
for (int i = 0; i < n; i++) {
if (S[i] == '1') {
r_p1 += count_p1[n - 1 - i];
if (sum != 1)
r_m1 += count_m1[n - 1 - i];
r_p1 %= sum + 1;
if (sum != 1)
r_m1 %= sum - 1;
}
}
// 後は愚直に
for (int i = 0; i < n; i++) {
int r = 0;
if (S[i] == '1') {
if (sum == 1) {
std::cout << 0 << std::endl;
continue;
}
// sum - 1の剰余で, 2^(n - 1- i)桁目が消える
r = r_m1;
r -= count_m1[n - 1 - i];
while (r < 0) {
r += sum - 1;
}
} else {
r = r_p1;
r += count_p1[n - 1 - i];
r %= sum + 1;
}
int cnt = 1;
while (r > 0) {
// popcountの計算
int x = r;
int popcount = 0;
while (x > 0) {
if (x % 2 == 1)
popcount++;
x /= 2;
}
r %= popcount;
cnt++;
}
std::cout << cnt << std::endl;
}
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
int main() {
int n;
int sum = 0;
std::string S;
std::cin >> n >> S;
for (int i = 0; i < n; i++) {
if (S[i] == '1')
sum++;
}
// 例外処理
if (sum == 0) {
for (int i = 0; i < n; i++) {
std::cout << 1 << std::endl;
}
return 0;
}
// 2^iの剰余
std::vector<int> count_p1(n), count_m1(n);
count_p1[0] = 1 % (sum + 1);
if (sum != 1)
count_m1[0] = 1 % (sum - 1);
for (int i = 1; i < n; i++) {
count_p1[i] = (count_p1[i - 1] * 2) % (sum + 1);
if (sum != 1)
count_m1[i] = (count_m1[i - 1] * 2) % (sum - 1);
}
// Xに対して事前の剰余計算
int r_p1 = 0, r_m1 = 0;
for (int i = 0; i < n; i++) {
if (S[i] == '1') {
r_p1 += count_p1[n - 1 - i];
if (sum != 1)
r_m1 += count_m1[n - 1 - i];
r_p1 %= sum + 1;
if (sum != 1)
r_m1 %= sum - 1;
}
}
// 後は愚直に
for (int i = 0; i < n; i++) {
int r = 0;
if (S[i] == '1') {
if (sum == 1) {
std::cout << 0 << std::endl;
continue;
}
// sum - 1の剰余で, 2^(n - 1- i)桁目が消える
r = r_m1;
r -= count_m1[n - 1 - i];
while (r < 0) {
r += sum - 1;
}
} else {
r = r_p1;
r += count_p1[n - 1 - i];
r %= sum + 1;
}
int cnt = 1;
while (r > 0) {
// popcountの計算
int x = r;
int popcount = 0;
while (x > 0) {
if (x % 2 == 1)
popcount++;
x /= 2;
}
r %= popcount;
cnt++;
}
std::cout << cnt << std::endl;
}
return 0;
}
| replace | 25 | 26 | 25 | 27 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
int dfs(int n) {
if (n == 0) {
return 0;
}
int now = n;
int co = 0;
while (now > 0) {
if (now % 2 == 1) {
co++;
}
now /= 2;
}
return dfs(n % co) + 1;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int tot = 0;
rep(i, n) {
if (s[i] == '1') {
tot++;
}
}
vector<int> com(n), cop(n);
int p = 1;
rep(i, n) {
if (tot > 1) {
com[i] = p % (tot - 1);
p = p * 2 % (tot - 1);
}
}
p = 1;
rep(i, n) {
cop[i] = p % (tot + 1);
p = p * 2 % (tot + 1);
}
int tm = 0;
int tp = 0;
rep(i, n) {
if (s[n - 1 - i] == '1') {
if (tot > 1) {
tm = (tm + com[i]) % (tot - 1);
}
tp = (tp + cop[i]) % (tot + 1);
}
}
rep(i, n) {
int nowm = tm;
int nowp = tp;
if (s[i] == '1') {
nowm = (nowm + tot - 1 - com[n - 1 - i]) % (tot - 1);
cout << dfs(nowm) + 1 << endl;
} else {
nowp = (nowp + cop[n - 1 - i]) % (tot + 1);
cout << dfs(nowp) + 1 << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
int dfs(int n) {
if (n == 0) {
return 0;
}
int now = n;
int co = 0;
while (now > 0) {
if (now % 2 == 1) {
co++;
}
now /= 2;
}
return dfs(n % co) + 1;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
int tot = 0;
rep(i, n) {
if (s[i] == '1') {
tot++;
}
}
vector<int> com(n), cop(n);
int p = 1;
rep(i, n) {
if (tot > 1) {
com[i] = p % (tot - 1);
p = p * 2 % (tot - 1);
}
}
p = 1;
rep(i, n) {
cop[i] = p % (tot + 1);
p = p * 2 % (tot + 1);
}
int tm = 0;
int tp = 0;
rep(i, n) {
if (s[n - 1 - i] == '1') {
if (tot > 1) {
tm = (tm + com[i]) % (tot - 1);
}
tp = (tp + cop[i]) % (tot + 1);
}
}
rep(i, n) {
int nowm = tm;
int nowp = tp;
if (s[i] == '1') {
if (tot <= 1) {
cout << 0 << endl;
} else {
nowm = (nowm + tot - 1 - com[n - 1 - i]) % (tot - 1);
cout << dfs(nowm) + 1 << endl;
}
} else {
nowp = (nowp + cop[n - 1 - i]) % (tot + 1);
cout << dfs(nowp) + 1 << endl;
}
}
}
| replace | 60 | 62 | 60 | 66 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod2 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define INF 2e9
int ans[200011];
string dtob(int num, int rlen) {
string r = "";
while (num > 0) {
r = to_string(num % 2) + r;
num /= 2;
}
int rem = rlen - r.length();
string t(rem, '0');
return t + r;
}
int func(string s) {
int ans = 0;
for (int i = 0; i < s.length(); i++)
if (s[i] == '1')
ans += 1;
return ans;
}
int main() {
ans[0] = 0;
for (int i = 1; i <= 200011; i++) {
int cnt = func(dtob(i, 20));
ans[i] = 1 + ans[i % cnt];
}
int n;
string x;
cin >> n >> x;
int cnt = func(x);
vector<int> v1(n), v2(n), v3(n, 0), v4(n, 0);
int p = 1;
for (int i = n - 1; i > -1; i--) {
if (x[i] == '1') {
v1[i] = p;
v2[i] = 0;
} else {
v1[i] = 0;
v2[i] = p;
}
p = (p * 1LL * 2) % (cnt + 1);
}
p = 1;
if (cnt - 1 > 0) {
for (int i = n - 1; i > -1; i--) {
if (x[i] == '1') {
v3[i] = p;
v4[i] = 0;
} else {
v3[i] = 0;
v4[i] = p;
}
p = (p * 1LL * 2) % (cnt - 1);
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++)
sum1 = (sum1 * 1LL + v1[i]) % (cnt + 1);
if (cnt > 1) {
for (int i = 0; i < n; i++)
sum2 = (sum2 * 1LL + v3[i]) % (cnt - 1);
}
// cout << sum1 << " " << sum2 << endl;
for (int i = 0; i < n; i++) {
if (x[i] == '0') {
int temp = (sum1 * 1LL - v1[i] + v2[i]) % (cnt + 1);
temp = (temp + cnt + 1) % (cnt + 1);
// cout << "temp=" << temp << endl;
cout << 1 + ans[temp] << endl;
} else {
if (cnt - 1 == 0)
cout << "0" << endl;
else {
int temp = (sum2 * 1LL - v3[i] + v4[i]) % (cnt - 1);
temp = (temp + cnt - 1) % (cnt - 1);
cout << 1 + ans[temp] << endl;
}
}
}
} | #include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod2 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define INF 2e9
int ans[200011];
string dtob(int num, int rlen) {
string r = "";
while (num > 0) {
r = to_string(num % 2) + r;
num /= 2;
}
int rem = rlen - r.length();
string t(rem, '0');
return t + r;
}
int func(string s) {
int ans = 0;
for (int i = 0; i < s.length(); i++)
if (s[i] == '1')
ans += 1;
return ans;
}
int main() {
ans[0] = 0;
for (int i = 1; i <= 200010; i++) {
int cnt = func(dtob(i, 20));
ans[i] = 1 + ans[i % cnt];
}
int n;
string x;
cin >> n >> x;
int cnt = func(x);
vector<int> v1(n), v2(n), v3(n, 0), v4(n, 0);
int p = 1;
for (int i = n - 1; i > -1; i--) {
if (x[i] == '1') {
v1[i] = p;
v2[i] = 0;
} else {
v1[i] = 0;
v2[i] = p;
}
p = (p * 1LL * 2) % (cnt + 1);
}
p = 1;
if (cnt - 1 > 0) {
for (int i = n - 1; i > -1; i--) {
if (x[i] == '1') {
v3[i] = p;
v4[i] = 0;
} else {
v3[i] = 0;
v4[i] = p;
}
p = (p * 1LL * 2) % (cnt - 1);
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++)
sum1 = (sum1 * 1LL + v1[i]) % (cnt + 1);
if (cnt > 1) {
for (int i = 0; i < n; i++)
sum2 = (sum2 * 1LL + v3[i]) % (cnt - 1);
}
// cout << sum1 << " " << sum2 << endl;
for (int i = 0; i < n; i++) {
if (x[i] == '0') {
int temp = (sum1 * 1LL - v1[i] + v2[i]) % (cnt + 1);
temp = (temp + cnt + 1) % (cnt + 1);
// cout << "temp=" << temp << endl;
cout << 1 + ans[temp] << endl;
} else {
if (cnt - 1 == 0)
cout << "0" << endl;
else {
int temp = (sum2 * 1LL - v3[i] + v4[i]) % (cnt - 1);
temp = (temp + cnt - 1) % (cnt - 1);
cout << 1 + ans[temp] << endl;
}
}
}
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02609 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
using ll = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
ll N;
string S;
ll pc;
ll x;
ll rankarr[201010];
ll f(ll n) {
ll ans = 0;
while (n > 0) {
bitset<64> bs(n);
n %= bs.count();
++ans;
}
return ans;
}
int main() {
//
cin >> N;
cin >> S;
for (auto c : S) {
if (c == '1') {
++pc;
}
}
ll pci = pc + 1;
ll pce = pc;
ll pcd = pc - 1;
ll mod = pci * pcd * pcd;
ll rank = 1;
for (int i = S.size() - 1; i >= 0; --i) {
if (S[i] == '1') {
x += rank;
x %= mod;
}
rankarr[i] = rank;
rank *= 2;
rank %= mod;
}
for (int i = 0; i < S.size(); ++i) {
ll xi;
if (S[i] == '0') {
xi = (x + rankarr[i]) % mod;
xi %= pci;
} else {
xi = (x + mod - rankarr[i]) % mod;
if (pcd == 0) {
cout << 0 << "\n";
continue;
} else
xi %= pcd;
}
cout << f(xi) + 1 << "\n";
}
}
| #include "bits/stdc++.h"
using namespace std;
using ll = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
ll N;
string S;
ll pc;
ll x;
ll rankarr[201010];
ll f(ll n) {
ll ans = 0;
while (n > 0) {
bitset<64> bs(n);
n %= bs.count();
++ans;
}
return ans;
}
int main() {
//
cin >> N;
cin >> S;
for (auto c : S) {
if (c == '1') {
++pc;
}
}
ll pci = pc + 1;
ll pce = pc;
ll pcd = pc - 1;
ll mod;
if (pcd == 0) {
mod = pci;
} else {
mod = pci * pcd;
}
ll rank = 1;
for (int i = S.size() - 1; i >= 0; --i) {
if (S[i] == '1') {
x += rank;
x %= mod;
}
rankarr[i] = rank;
rank *= 2;
rank %= mod;
}
for (int i = 0; i < S.size(); ++i) {
ll xi;
if (S[i] == '0') {
xi = (x + rankarr[i]) % mod;
xi %= pci;
} else {
xi = (x + mod - rankarr[i]) % mod;
if (pcd == 0) {
cout << 0 << "\n";
continue;
} else
xi %= pcd;
}
cout << f(xi) + 1 << "\n";
}
}
| replace | 62 | 63 | 62 | 68 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int, int> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\n'
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define forne(i, n) for (int i = 1; i <= n; i++)
#define rforne(i, n) for (int i = n; i >= 1; i--)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define rforse(i, s, e) for (int i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
const ll INFL = 1e18;
const int INFI = 1e9;
#ifdef KARAN
#include "../../trace.h"
#else
#define trace(args...)
#endif
ll div_floor(const ll &a, const ll &b) {
return a / b - (((a ^ b) < 0) and a % b);
}
ll div_ceil(const ll &a, const ll &b) {
return a / b + (((a ^ b) >= 0) and a % b);
}
ll modpow(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
ll bpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll modinv(ll a, ll m = MOD) { return modpow(a, m - 2, m); }
void graph(vvi &adj, int m) {
int x, y;
forn(i, m) {
cin >> x >> y;
adj[--x].pb(--y);
adj[y].pb(x);
}
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> process(n, 0);
forne(i, n - 1) {
bitset<20> b(i);
process[i] = process[i % (__builtin_popcount(i))] + 1;
}
ll cntr = 0;
forn(i, n) cntr += s[i] == '1';
trace(cntr);
ll cntr1 = cntr + 1;
ll cntr2 = cntr - 1;
ll rep1 = 0;
forn(i, n) if (s[i] == '1') {
rep1 += modpow(2, n - i - 1, cntr + 1);
rep1 %= cntr + 1;
}
ll rep2 = 0;
forn(i, n) if (s[i] == '1') {
rep2 += modpow(2, n - i - 1, cntr - 1);
rep2 %= cntr - 1;
}
trace(rep1, rep2);
forn(i, n) {
if (s[i] == '1') {
if (cntr2 == 0)
cout << 0 << endl;
else {
int crr = modpow(2, n - i - 1, cntr2);
cout << 1 + process[((rep2 - crr) % cntr2 + cntr2) % cntr2] << endl;
}
} else {
int crr = modpow(2, n - i - 1, cntr1);
cout << 1 + process[((rep1 + crr) % cntr1 + cntr1) % cntr1] << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef KARAN
freopen(".inp.txt", "r", stdin);
freopen(".out.txt", "w", stdout);
freopen(".err.txt", "w", stderr);
#endif
int t = 1;
// cin>>t;
while (t--)
solve();
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int, int> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\n'
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define forne(i, n) for (int i = 1; i <= n; i++)
#define rforne(i, n) for (int i = n; i >= 1; i--)
#define forse(i, s, e) for (int i = s; i < e; i++)
#define rforse(i, s, e) for (int i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
const ll INFL = 1e18;
const int INFI = 1e9;
#ifdef KARAN
#include "../../trace.h"
#else
#define trace(args...)
#endif
ll div_floor(const ll &a, const ll &b) {
return a / b - (((a ^ b) < 0) and a % b);
}
ll div_ceil(const ll &a, const ll &b) {
return a / b + (((a ^ b) >= 0) and a % b);
}
ll modpow(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
ll bpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll modinv(ll a, ll m = MOD) { return modpow(a, m - 2, m); }
void graph(vvi &adj, int m) {
int x, y;
forn(i, m) {
cin >> x >> y;
adj[--x].pb(--y);
adj[y].pb(x);
}
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> process(n, 0);
forne(i, n - 1) {
bitset<20> b(i);
process[i] = process[i % (__builtin_popcount(i))] + 1;
}
ll cntr = 0;
forn(i, n) cntr += s[i] == '1';
trace(cntr);
ll cntr1 = cntr + 1;
ll cntr2 = cntr - 1;
ll rep1 = 0;
forn(i, n) if (s[i] == '1') {
rep1 += modpow(2, n - i - 1, cntr + 1);
rep1 %= cntr + 1;
}
ll rep2 = 0;
forn(i, n) if (s[i] == '1' && cntr2 != 0) {
rep2 += modpow(2, n - i - 1, cntr - 1);
rep2 %= cntr - 1;
}
trace(rep1, rep2);
forn(i, n) {
if (s[i] == '1') {
if (cntr2 == 0)
cout << 0 << endl;
else {
int crr = modpow(2, n - i - 1, cntr2);
cout << 1 + process[((rep2 - crr) % cntr2 + cntr2) % cntr2] << endl;
}
} else {
int crr = modpow(2, n - i - 1, cntr1);
cout << 1 + process[((rep1 + crr) % cntr1 + cntr1) % cntr1] << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef KARAN
freopen(".inp.txt", "r", stdin);
freopen(".out.txt", "w", stdout);
freopen(".err.txt", "w", stderr);
#endif
int t = 1;
// cin>>t;
while (t--)
solve();
} | replace | 106 | 107 | 106 | 107 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)
ll pow(ll a, ll n) { // compute a^n with o(logn)
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
ll pow(ll a, ll n, ll mod) { // compute a^n (mod mod) with o(logn)
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
const int MAX = 199;
const int MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 順列計算
ll PER(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[n - k] % MOD) % MOD;
}
#define max 200000
int main() {
ll n;
string str;
cin >> n;
ll pow2_mod[max][2];
ll x0_mod[2] = {0};
ll p = 0;
// input
cin >> str;
rep0(i, n - 1) if (str[i] == '1') p++;
// pre-process
ll tmp0 = 1 % (p + 1);
ll tmp1 = 1 % (p - 1);
rep0(i, max) {
pow2_mod[i][0] = tmp0;
pow2_mod[i][1] = tmp1;
tmp0 *= 2;
tmp1 *= 2;
tmp0 %= p + 1;
tmp1 %= p - 1;
}
rep0(i, n - 1) {
if (str[i] == '1') {
x0_mod[0] += pow2_mod[n - i - 1][0];
x0_mod[0] %= p + 1;
x0_mod[1] += pow2_mod[n - i - 1][1];
x0_mod[1] %= p - 1;
}
}
rep1(i, n) {
ll cnt = 1;
ll tmp;
if (str[i - 1] == '1')
tmp = (x0_mod[1] - pow2_mod[n - i][1] + p - 1) % (p - 1);
else
tmp = (x0_mod[0] + pow2_mod[n - i][0]) % (p + 1);
while (tmp > 0) {
ll pop = 0;
rep0(i, 19) if (tmp & (1 << i)) pop++;
tmp %= pop;
cnt++;
}
cout << cnt << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)
ll pow(ll a, ll n) { // compute a^n with o(logn)
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
ll pow(ll a, ll n, ll mod) { // compute a^n (mod mod) with o(logn)
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
const int MAX = 199;
const int MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 順列計算
ll PER(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[n - k] % MOD) % MOD;
}
#define max 200000
int main() {
ll n;
string str;
cin >> n;
ll pow2_mod[max][2];
ll x0_mod[2] = {0};
ll p = 0;
// input
cin >> str;
rep0(i, n - 1) if (str[i] == '1') p++;
// 例外処理
if (p == 1) {
ll tmp0 = 1 % (p + 1);
rep0(i, max) {
pow2_mod[i][0] = tmp0;
tmp0 *= 2;
tmp0 %= p + 1;
}
rep0(i, n - 1) {
if (str[i] == '1') {
x0_mod[0] += pow2_mod[n - i - 1][0];
x0_mod[0] %= p + 1;
}
}
rep1(i, n) {
ll cnt = 1;
ll tmp;
if (str[i - 1] == '1') {
cout << 0 << endl;
continue;
} else {
tmp = (x0_mod[0] + pow2_mod[n - i][0]) % (p + 1);
while (tmp > 0) {
ll pop = 0;
rep0(i, 19) if (tmp & (1 << i)) pop++;
tmp %= pop;
cnt++;
}
cout << cnt << endl;
}
}
return 0;
}
// pre-process
ll tmp0 = 1 % (p + 1);
ll tmp1 = 1 % (p - 1);
rep0(i, max) {
pow2_mod[i][0] = tmp0;
pow2_mod[i][1] = tmp1;
tmp0 *= 2;
tmp1 *= 2;
tmp0 %= p + 1;
tmp1 %= p - 1;
}
rep0(i, n - 1) {
if (str[i] == '1') {
x0_mod[0] += pow2_mod[n - i - 1][0];
x0_mod[0] %= p + 1;
x0_mod[1] += pow2_mod[n - i - 1][1];
x0_mod[1] %= p - 1;
}
}
rep1(i, n) {
ll cnt = 1;
ll tmp;
if (str[i - 1] == '1')
tmp = (x0_mod[1] - pow2_mod[n - i][1] + p - 1) % (p - 1);
else
tmp = (x0_mod[0] + pow2_mod[n - i][0]) % (p + 1);
while (tmp > 0) {
ll pop = 0;
rep0(i, 19) if (tmp & (1 << i)) pop++;
tmp %= pop;
cnt++;
}
cout << cnt << endl;
}
return 0;
} | insert | 85 | 85 | 85 | 119 | -6 | *** stack smashing detected ***: terminated
|
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios \
; \
ios::sync_with_stdio(false); \
cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 998244353;
const int N = 1e6 + 5;
const int M = 1e3 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1)
res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int n, m, k;
char s[N];
int a[N];
ll qsm(ll x, ll y, ll MOD) {
ll res = 1;
while (y) {
if (y & 1)
res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
int main() {
ios;
cin >> n;
cin >> s + 1;
int res = 0, cnt = 0;
rep(i, 1, n) {
if (s[i] == '1')
++cnt;
a[n - i + 1] = s[i] - '0';
}
int res1 = res, res2 = res;
int cnt1 = cnt - 1, cnt2 = cnt + 1;
rep(i, 1, n) {
if (a[i]) {
res1 = (res1 + qsm(2, i - 1, cnt1)) % cnt1;
res2 = (res2 + qsm(2, i - 1, cnt2)) % cnt2;
}
}
per(i, n, 1) {
int tmp;
if (a[i]) {
if (cnt1 == 0) {
cout << "0\n";
continue;
}
tmp = (res1 - qsm(2, i - 1, cnt1) + cnt1) % cnt1;
} else {
tmp = (res2 + qsm(2, i - 1, cnt2) + cnt2) % cnt2;
}
int ans = 1;
while (tmp) {
cnt = __builtin_popcount(tmp);
tmp %= cnt;
++ans;
}
cout << ans << "\n";
}
return 0;
} | #include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios \
; \
ios::sync_with_stdio(false); \
cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 998244353;
const int N = 1e6 + 5;
const int M = 1e3 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1)
res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
int n, m, k;
char s[N];
int a[N];
ll qsm(ll x, ll y, ll MOD) {
ll res = 1;
while (y) {
if (y & 1)
res = res * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return res;
}
int main() {
ios;
cin >> n;
cin >> s + 1;
int res = 0, cnt = 0;
rep(i, 1, n) {
if (s[i] == '1')
++cnt;
a[n - i + 1] = s[i] - '0';
}
int res1 = res, res2 = res;
int cnt1 = cnt - 1, cnt2 = cnt + 1;
rep(i, 1, n) {
if (a[i]) {
if (cnt1 > 0)
res1 = (res1 + qsm(2, i - 1, cnt1)) % cnt1;
res2 = (res2 + qsm(2, i - 1, cnt2)) % cnt2;
}
}
per(i, n, 1) {
int tmp;
if (a[i]) {
if (cnt1 == 0) {
cout << "0\n";
continue;
}
tmp = (res1 - qsm(2, i - 1, cnt1) + cnt1) % cnt1;
} else {
tmp = (res2 + qsm(2, i - 1, cnt2) + cnt2) % cnt2;
}
int ans = 1;
while (tmp) {
cnt = __builtin_popcount(tmp);
tmp %= cnt;
++ans;
}
cout << ans << "\n";
}
return 0;
} | replace | 67 | 68 | 67 | 69 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using ll = long long;
using ld = long double;
#define int ll
#define double ld
#define VARNAME(v) #v
using P = pair<int, int>;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr ll INF = 1e16;
#pragma region tmp
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793;
const string endn = "\n";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &o) {
out << "(" << o.first << ", " << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const deque<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, queue<T> o) {
while (!o.empty())
out << o.front() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, priority_queue<T, vector<T>, U> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> ostream &operator<<(ostream &out, stack<T> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, initializer_list<T> b) {
T c = min(b);
if (a > c) {
a = c;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, initializer_list<T> b) {
T c = max(b);
if (a < c) {
a = c;
return 1;
}
return 0;
}
inline int mrep(int &a, int M = MOD) {
a %= M;
return a = (a < 0 ? a + M : a);
}
inline int add(int &a, int b, int M = MOD) {
return a = (mrep(a, M) + mrep(b, M)) % M;
}
inline int mul(int &a, int b, int M = MOD) {
return a = (mrep(a, M) * mrep(b, M)) % M;
}
inline int add(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) + accumulate(b.begin(), b.end(), (int)0,
[&M](int acc, int i) {
return (acc + mrep(i, M)) % M;
})) %
M;
}
inline int mul(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) * accumulate(b.begin(), b.end(), (int)1,
[&M](int acc, int i) {
return (acc * mrep(i, M)) % M;
})) %
M;
}
inline int modpow(int b, int e, int M = MOD) {
int ret = 1;
while (e > 0) {
if (e % 2)
mul(ret, b, M);
mul(b, b, M);
e /= 2;
};
return ret;
}
inline int modinv(int a, int M = MOD) {
int b = M, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mrep(u, M);
}
template <class T> inline void print(T a) { cout << a << endl; }
template <class T> inline void print(vector<T> a, int n) {
chmin(n, int(a.size()));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
template <class T, size_t SIZE> inline void print(T (&a)[SIZE], int n = SIZE) {
chmin(n, int(SIZE));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
#pragma #endregion tmp
signed main() {
int n;
string s;
cin >> n >> s;
int f[210000];
f[0] = 0;
repd(i, 1, 210000) {
int p = __builtin_popcount(i);
f[i] = f[i % p] + 1;
}
int c = 0;
rep(i, n) if (s[i] == '1') c++;
int a = 0, b = 0;
int k1 = 1, k2 = 1;
rep(i, n) {
if (s[n - i - 1] == '1') {
add(a, k1, c - 1);
add(b, k2, c + 1);
}
mul(k1, 2, c - 1);
mul(k2, 2, c + 1);
}
rep(i, n) {
if (s[i] == '1') {
cout << f[(a + c - 1 - modpow(2, n - i - 1, c - 1)) % (c - 1)] + 1
<< endl;
} else {
cout << f[(b + c + 1 + modpow(2, n - i - 1, c + 1)) % (c + 1)] + 1
<< endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using ll = long long;
using ld = long double;
#define int ll
#define double ld
#define VARNAME(v) #v
using P = pair<int, int>;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr ll INF = 1e16;
#pragma region tmp
constexpr double EPS = 1e-10;
constexpr double PI = 3.141592653589793;
const string endn = "\n";
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &o) {
out << "(" << o.first << ", " << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const deque<T> &o) {
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (itr + 1 != o.end() ? " " : "");
return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, const multiset<T> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? "," : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const multimap<T, U> &o) {
out << "{";
for (auto itr = o.begin(); itr != o.end(); itr++)
out << *itr << (next(itr, 1) != o.end() ? " " : "");
out << "}";
return out;
}
template <class T> ostream &operator<<(ostream &out, queue<T> o) {
while (!o.empty())
out << o.front() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, priority_queue<T, vector<T>, U> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> ostream &operator<<(ostream &out, stack<T> o) {
while (!o.empty())
out << o.top() << (o.size() > 0 ? " " : ""), o.pop();
return out;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, initializer_list<T> b) {
T c = min(b);
if (a > c) {
a = c;
return 1;
}
return 0;
}
template <class T> inline bool chmax(T &a, initializer_list<T> b) {
T c = max(b);
if (a < c) {
a = c;
return 1;
}
return 0;
}
inline int mrep(int &a, int M = MOD) {
a %= M;
return a = (a < 0 ? a + M : a);
}
inline int add(int &a, int b, int M = MOD) {
return a = (mrep(a, M) + mrep(b, M)) % M;
}
inline int mul(int &a, int b, int M = MOD) {
return a = (mrep(a, M) * mrep(b, M)) % M;
}
inline int add(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) + accumulate(b.begin(), b.end(), (int)0,
[&M](int acc, int i) {
return (acc + mrep(i, M)) % M;
})) %
M;
}
inline int mul(int &a, initializer_list<int> b, int M = MOD) {
return a = (mrep(a, M) * accumulate(b.begin(), b.end(), (int)1,
[&M](int acc, int i) {
return (acc * mrep(i, M)) % M;
})) %
M;
}
inline int modpow(int b, int e, int M = MOD) {
int ret = 1;
while (e > 0) {
if (e % 2)
mul(ret, b, M);
mul(b, b, M);
e /= 2;
};
return ret;
}
inline int modinv(int a, int M = MOD) {
int b = M, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return mrep(u, M);
}
template <class T> inline void print(T a) { cout << a << endl; }
template <class T> inline void print(vector<T> a, int n) {
chmin(n, int(a.size()));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
template <class T, size_t SIZE> inline void print(T (&a)[SIZE], int n = SIZE) {
chmin(n, int(SIZE));
rep(i, n) cout << a[i] << (i != n - 1 ? " " : "\n");
}
#pragma #endregion tmp
signed main() {
int n;
string s;
cin >> n >> s;
int f[210000];
f[0] = 0;
repd(i, 1, 210000) {
int p = __builtin_popcount(i);
f[i] = f[i % p] + 1;
}
int c = 0;
rep(i, n) if (s[i] == '1') c++;
if (c == 1) {
int b = 0;
int k2 = 1;
rep(i, n) {
if (s[n - i - 1] == '1') {
add(b, k2, c + 1);
}
mul(k2, 2, c + 1);
}
rep(i, n) {
if (s[i] == '1') {
cout << 0 << endl;
} else {
cout << f[(b + c + 1 + modpow(2, n - i - 1, c + 1)) % (c + 1)] + 1
<< endl;
}
}
return 0;
}
int a = 0, b = 0;
int k1 = 1, k2 = 1;
rep(i, n) {
if (s[n - i - 1] == '1') {
add(a, k1, c - 1);
add(b, k2, c + 1);
}
mul(k1, 2, c - 1);
mul(k2, 2, c + 1);
}
rep(i, n) {
if (s[i] == '1') {
cout << f[(a + c - 1 - modpow(2, n - i - 1, c - 1)) % (c - 1)] + 1
<< endl;
} else {
cout << f[(b + c + 1 + modpow(2, n - i - 1, c + 1)) % (c + 1)] + 1
<< endl;
}
}
}
| insert | 178 | 178 | 178 | 197 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int pop_count(int bits) {
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}
int main() {
int N;
cin >> N;
string str;
cin >> str;
reverse(str.begin(), str.end());
vector<int> S(N);
for (int i = 0; i < N; i++)
S[i] = (str[i] - '0');
int64_t cnt = accumulate(S.begin(), S.end(), 0);
int64_t V = (cnt == 0 ? 1 : (cnt + 1) * (cnt - 1));
int64_t R = 0, B = 1;
for (int i = 0; i < N; i++) {
if (S[i])
R = (R + B) % V;
B = B * 2 % V;
}
auto calc = [&](auto &&calc, int v) -> int {
if (v == 0) {
return 0;
} else {
return calc(calc, v % pop_count(v)) + 1;
}
};
B = 1;
vector<int> ans(N);
for (int i = 0; i < N; i++) {
int64_t v = cnt + (S[i] ? -1 : 1);
if (v == 0)
continue;
int64_t r = (R + B % v * (S[i] % v ? v - 1 : 1)) % v;
ans[N - 1 - i] = 1 + calc(calc, r);
B = B * 2 % V;
}
for (int a : ans)
cout << a << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int pop_count(int bits) {
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}
int main() {
int N;
cin >> N;
string str;
cin >> str;
reverse(str.begin(), str.end());
vector<int> S(N);
for (int i = 0; i < N; i++)
S[i] = (str[i] - '0');
int64_t cnt = accumulate(S.begin(), S.end(), 0);
int64_t V = 1;
for (auto a : {cnt + 1, cnt - 1})
if (a > 0)
V *= a;
int64_t R = 0, B = 1;
for (int i = 0; i < N; i++) {
if (S[i])
R = (R + B) % V;
B = B * 2 % V;
}
auto calc = [&](auto &&calc, int v) -> int {
if (v == 0) {
return 0;
} else {
return calc(calc, v % pop_count(v)) + 1;
}
};
B = 1;
vector<int> ans(N);
for (int i = 0; i < N; i++) {
int64_t v = cnt + (S[i] ? -1 : 1);
if (v == 0)
continue;
int64_t r = (R + B % v * (S[i] % v ? v - 1 : 1)) % v;
ans[N - 1 - i] = 1 + calc(calc, r);
B = B * 2 % V;
}
for (int a : ans)
cout << a << endl;
return 0;
}
| replace | 22 | 23 | 22 | 26 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ren(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d", &n)
#define gll(n) scanf("%lld", &n)
#define gstr(n) scanf("%s", n)
#define gl(n) cin >> n
#define oi(n) printf("%d", n)
#define oll(n) printf("%lld", n)
#define ostr(n) printf("%s", n)
#define ol(n) cout << n
#define os cout << " "
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it) \
for (__typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll>> mat;
const int N = 1e6 + 5;
int dp[N];
int p(int a, int b, int mod) {
int r = 1;
while (b) {
if (b % 2)
r = (r * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return r;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
dp[0] = 0;
rep(i, 1, N - 1) dp[i] = 1 + dp[i % __builtin_popcount(i)];
int n;
string s;
cin >> n >> s;
int cnt = 0;
rep(i, 0, n - 1) {
if (s[i] == '1')
cnt++;
}
int a1 = 0, a2 = 0;
rep(i, 0, n - 1) {
if (s[i] == '1') {
a1 = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
if (cnt > 1)
a2 = (a2 + p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
}
}
rep(i, 0, n - 1) {
if (s[i] == '1') {
if (cnt == 1) {
ol("0\n");
} else {
int kk = (a2 - p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
if (kk < 0)
kk += cnt - 1;
ol(1 + dp[kk]);
on;
}
} else {
int kk = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
ol(1 + dp[kk]);
on;
// ol(kk);on;
}
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ren(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d", &n)
#define gll(n) scanf("%lld", &n)
#define gstr(n) scanf("%s", n)
#define gl(n) cin >> n
#define oi(n) printf("%d", n)
#define oll(n) printf("%lld", n)
#define ostr(n) printf("%s", n)
#define ol(n) cout << n
#define os cout << " "
#define on cout << "\n"
#define o2(a, b) cout << a << " " << b
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it) \
for (__typeof(container.begin()) it = container.begin(); \
it != container.end(); it++)
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll>> mat;
const int N = 1e6 + 5;
int dp[N];
int p(ll a, ll b, ll mod) {
ll r = 1;
while (b) {
if (b % 2)
r = (r * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return r;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
dp[0] = 0;
rep(i, 1, N - 1) dp[i] = 1 + dp[i % __builtin_popcount(i)];
int n;
string s;
cin >> n >> s;
int cnt = 0;
rep(i, 0, n - 1) {
if (s[i] == '1')
cnt++;
}
int a1 = 0, a2 = 0;
rep(i, 0, n - 1) {
if (s[i] == '1') {
a1 = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
if (cnt > 1)
a2 = (a2 + p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
}
}
rep(i, 0, n - 1) {
if (s[i] == '1') {
if (cnt == 1) {
ol("0\n");
} else {
int kk = (a2 - p(2, n - 1 - i, cnt - 1)) % (cnt - 1);
if (kk < 0)
kk += cnt - 1;
ol(1 + dp[kk]);
on;
}
} else {
int kk = (a1 + p(2, n - 1 - i, cnt + 1)) % (cnt + 1);
ol(1 + dp[kk]);
on;
// ol(kk);on;
}
}
return 0;
}
| replace | 58 | 60 | 58 | 60 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
cerr << head;
dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
cerr << head << ", ";
dump(forward<Tail>(tail)...);
}
#define debug(...) \
do { \
cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
dump(__VA_ARGS__); \
} while (false)
#else
#define dump(...)
#define debug(...)
#endif
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
/////////////////////////////////////////////////////////////////////
const ll inf = 1LL << 60;
ll f(ll n) {
ll ret = 0;
while (n > 0) {
n %= __builtin_popcountll(n);
ret++;
}
return ret;
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
ll d = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
d++;
}
ll a = d - 1;
ll b = d;
ll c = d + 1;
vector<ll> x(n + 1, 1);
vector<ll> y(n + 1, 1);
vector<ll> z(n + 1, 1);
rep(i, n) {
if (a > 0)
x[i + 1] = x[i] * 2 % a;
if (b > 0)
y[i + 1] = y[i] * 2 % b;
if (c > 0)
z[i + 1] = z[i] * 2 % c;
}
ll xx = 0;
ll yy = 0;
ll zz = 0;
rep(i, n) {
if (s[i] == '1') {
(xx += x[i]) %= a;
(yy += y[i]) %= b;
(zz += z[i]) %= c;
}
}
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
if (a <= 0) {
cout << 0 << endl;
continue;
} else {
ll ans = f((xx + a - x[i]) % a);
cout << ans + 1 << endl;
}
} else {
ll ans = f((zz + z[i]) % c);
cout << ans + 1 << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
cerr << head;
dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
cerr << head << ", ";
dump(forward<Tail>(tail)...);
}
#define debug(...) \
do { \
cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
dump(__VA_ARGS__); \
} while (false)
#else
#define dump(...)
#define debug(...)
#endif
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
/////////////////////////////////////////////////////////////////////
const ll inf = 1LL << 60;
ll f(ll n) {
ll ret = 0;
while (n > 0) {
n %= __builtin_popcountll(n);
ret++;
}
return ret;
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
ll d = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
d++;
}
ll a = d - 1;
ll b = d;
ll c = d + 1;
vector<ll> x(n + 1, 1);
vector<ll> y(n + 1, 1);
vector<ll> z(n + 1, 1);
rep(i, n) {
if (a > 0)
x[i + 1] = x[i] * 2 % a;
if (b > 0)
y[i + 1] = y[i] * 2 % b;
if (c > 0)
z[i + 1] = z[i] * 2 % c;
}
ll xx = 0;
ll yy = 0;
ll zz = 0;
rep(i, n) {
if (s[i] == '1') {
if (a > 0)
(xx += x[i]) %= a;
if (b > 0)
(yy += y[i]) %= b;
if (c > 0)
(zz += z[i]) %= c;
}
}
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
if (a <= 0) {
cout << 0 << endl;
continue;
} else {
ll ans = f((xx + a - x[i]) % a);
cout << ans + 1 << endl;
}
} else {
ll ans = f((zz + z[i]) % c);
cout << ans + 1 << endl;
}
}
return 0;
}
| replace | 88 | 91 | 88 | 94 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int N;
string s;
int MODplus1[200005];
int MODminus1[200005];
int f(int x) {
if (x == 0)
return 0;
int cnt = 0;
rep(i, 20) {
if ((1 << i) & x)
cnt++;
}
return f(x % cnt) + 1;
}
int calc_mod(int x, int _mod[200005]) {
_mod[0] = 1;
rep(i, N) {
if (i == 0)
continue;
_mod[i] = 2 * _mod[i - 1] % x;
}
int sum = 0;
rep(i, N) {
if (s[i] == '1')
sum += _mod[N - 1 - i];
sum %= x;
}
return sum;
}
int main() {
INCANT;
cin >> N >> s;
int cnt = 0;
rep(i, N) if (s[i] == '1') cnt++;
int sum1 = calc_mod(cnt + 1, MODplus1);
int sum2 = calc_mod(cnt - 1, MODminus1);
rep(i, N) {
int sum;
if (s[i] == '1') { // 1→0
if (cnt - 1 == 0) {
cout << 0 << endl;
continue;
}
sum = sum2;
sum -= MODminus1[N - 1 - i];
sum += (cnt - 1);
sum %= (cnt - 1);
} else { // 0→1
sum = sum1;
sum += MODplus1[N - 1 - i];
sum %= (cnt + 1);
}
cout << f(sum) + 1 << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int N;
string s;
int MODplus1[200005];
int MODminus1[200005];
int f(int x) {
if (x == 0)
return 0;
int cnt = 0;
rep(i, 20) {
if ((1 << i) & x)
cnt++;
}
return f(x % cnt) + 1;
}
int calc_mod(int x, int _mod[200005]) {
if (x == 0)
return 0;
_mod[0] = 1;
rep(i, N) {
if (i == 0)
continue;
_mod[i] = 2 * _mod[i - 1] % x;
}
int sum = 0;
rep(i, N) {
if (s[i] == '1')
sum += _mod[N - 1 - i];
sum %= x;
}
return sum;
}
int main() {
INCANT;
cin >> N >> s;
int cnt = 0;
rep(i, N) if (s[i] == '1') cnt++;
int sum1 = calc_mod(cnt + 1, MODplus1);
int sum2 = calc_mod(cnt - 1, MODminus1);
rep(i, N) {
int sum;
if (s[i] == '1') { // 1→0
if (cnt - 1 == 0) {
cout << 0 << endl;
continue;
}
sum = sum2;
sum -= MODminus1[N - 1 - i];
sum += (cnt - 1);
sum %= (cnt - 1);
} else { // 0→1
sum = sum1;
sum += MODplus1[N - 1 - i];
sum %= (cnt + 1);
}
cout << f(sum) + 1 << endl;
}
return 0;
}
| insert | 35 | 35 | 35 | 37 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() \
std::ios::sync_with_stdio(false); \
std::cin.tie(0); \
std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
ll Count(ll n) {
ll cnt = 0;
while (n) {
n = n % __builtin_popcount(n);
cnt++;
}
return cnt;
}
int main() {
Init();
ll n, bitCnt = 0;
string s;
cin >> n >> s;
rep(i, n) if (s[i] == '1') bitCnt++;
ll a = bitCnt + 1;
ll b = bitCnt - 1;
vi p(n);
p[n - 1] = 1;
vi q(n);
q[n - 1] = 1;
for (ll i = n - 2; i >= 0; i--) {
if (a)
p[i] = p[i + 1] * 2 % a;
if (b)
q[i] = q[i + 1] * 2 % b;
}
ll sp = 0, sm = 0;
rep(i, n) if (s[i] == '1') sp += p[i], sm += q[i];
sp %= a;
sm %= b;
rep(i, n) {
if (s[i] == '1') {
// 反転する箇所が1の場合
if (b <= 0)
cout << 0 << endl;
else
cout << (1 + Count((sm - q[i] + b) % b)) << endl;
} else {
// 反転する箇所が0の場合
cout << (1 + Count((sp + p[i]) % a)) << endl;
}
}
} | #include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() \
std::ios::sync_with_stdio(false); \
std::cin.tie(0); \
std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
ll Count(ll n) {
ll cnt = 0;
while (n) {
n = n % __builtin_popcount(n);
cnt++;
}
return cnt;
}
int main() {
Init();
ll n, bitCnt = 0;
string s;
cin >> n >> s;
rep(i, n) if (s[i] == '1') bitCnt++;
ll a = bitCnt + 1;
ll b = bitCnt - 1;
vi p(n);
p[n - 1] = 1;
vi q(n);
q[n - 1] = 1;
for (ll i = n - 2; i >= 0; i--) {
if (a)
p[i] = p[i + 1] * 2 % a;
if (b)
q[i] = q[i + 1] * 2 % b;
}
ll sp = 0, sm = 0;
rep(i, n) if (s[i] == '1') sp += p[i], sm += q[i];
rep(i, n) {
if (s[i] == '1') {
// 反転する箇所が1の場合
if (b <= 0)
cout << 0 << endl;
else
cout << (1 + Count((sm - q[i] + b) % b)) << endl;
} else {
// 反転する箇所が0の場合
cout << (1 + Count((sp + p[i]) % a)) << endl;
}
}
} | delete | 65 | 67 | 65 | 65 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R)
ll lo = L - 1, hi = R - 1;
while (lo + 1 != hi) {
ll mi = (lo + hi) / 2;
if (f(mi) <= f(mi + 1))
hi = mi;
else
lo = mi;
}
return hi;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N;
string X;
cin >> N >> X;
auto f = [&](ll x) {
ll ct = 0;
while (x != 0) {
ll pt = __builtin_popcount(x);
x %= pt;
++ct;
}
return ct;
};
ll tmp = 0;
rep(i, N) tmp += X[i] == '1';
// tmp - 1 or mod tmp + 1の世界
// mod tmpの世界
vec T1{1 % (tmp - 1)};
vec T2{1 % (tmp + 1)};
rep(i, N - 1) {
if (tmp > 1)
T1.pb(T1.back() * 2 % (tmp - 1));
T2.pb(T2.back() * 2 % (tmp + 1));
}
reverse(ALL(T1));
reverse(ALL(T2));
ll sum1 = 0;
ll sum2 = 0;
rep(i, N) {
if (X[i] == '1') {
if (tmp > 1) {
sum1 += T1[i];
sum1 %= (tmp - 1);
}
sum2 += T2[i];
sum2 %= (tmp + 1);
}
}
rep(i, N) {
if (tmp == 1 and X[i] == '1') {
print(0);
continue;
}
if (X[i] == '1') {
ll now = sum1 - T1[i];
now = (now + (tmp - 1)) % (tmp - 1);
print(f(now) + 1);
} else {
ll now = sum2 + T2[i];
now = (now + (tmp + 1)) % (tmp + 1);
print(f(now) + 1);
}
}
return 0;
} | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const double PI = acos(-1.0);
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define drop(x) cout << (x) << '\n', exit(0)
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
template <class S, class T> inline bool chmax(S &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T> inline bool chmin(S &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); }
inline ll devc(ll x, ll y) { return (x + y - 1) / y; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t);
return os;
}
template <typename T1, typename T2, typename T3>
istream &operator>>(istream &is, tuple<T1, T2, T3> &t) {
is >> get<0>(t) >> get<1>(t) >> get<2>(t);
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
/*--------------------------------- Tools
* ------------------------------------------*/
template <typename T> vector<T> cumsum(const vector<T> &X) {
vector<T> res(X.size() + 1, 0);
for (int i = 0; i < X.size(); ++i)
res[i + 1] += res[i] + X[i];
return res;
}
template <typename S, typename T, typename F>
pair<T, T> bisearch(S left, T right, F f) {
while (abs(right - left) > 1) {
T mid = (right + left) / 2;
if (f(mid))
right = mid;
else
left = mid;
}
return {left, right};
}
template <typename S, typename T, typename F>
double trisearch(S left, T right, F f, int maxLoop = 90) {
double low = left, high = right;
while (maxLoop--) {
double mid_left = high / 3 + low * 2 / 3;
double mid_right = high * 2 / 3 + low / 3;
if (f(mid_left) >= f(mid_right))
low = mid_left;
else
high = mid_right;
}
return (low + high) * 0.5;
}
template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R)
ll lo = L - 1, hi = R - 1;
while (lo + 1 != hi) {
ll mi = (lo + hi) / 2;
if (f(mi) <= f(mi + 1))
hi = mi;
else
lo = mi;
}
return hi;
}
/*------------------------------- Main Code Here
* -----------------------------------------*/
int main() {
ll N;
string X;
cin >> N >> X;
auto f = [&](ll x) {
ll ct = 0;
while (x != 0) {
ll pt = __builtin_popcount(x);
x %= pt;
++ct;
}
return ct;
};
ll tmp = 0;
rep(i, N) tmp += X[i] == '1';
// tmp - 1 or mod tmp + 1の世界
// mod tmpの世界
vec T1{tmp == 1 ? 0 : 1};
vec T2{1 % (tmp + 1)};
rep(i, N - 1) {
if (tmp > 1)
T1.pb(T1.back() * 2 % (tmp - 1));
T2.pb(T2.back() * 2 % (tmp + 1));
}
reverse(ALL(T1));
reverse(ALL(T2));
ll sum1 = 0;
ll sum2 = 0;
rep(i, N) {
if (X[i] == '1') {
if (tmp > 1) {
sum1 += T1[i];
sum1 %= (tmp - 1);
}
sum2 += T2[i];
sum2 %= (tmp + 1);
}
}
rep(i, N) {
if (tmp == 1 and X[i] == '1') {
print(0);
continue;
}
if (X[i] == '1') {
ll now = sum1 - T1[i];
now = (now + (tmp - 1)) % (tmp - 1);
print(f(now) + 1);
} else {
ll now = sum2 + T2[i];
now = (now + (tmp + 1)) % (tmp + 1);
print(f(now) + 1);
}
}
return 0;
} | replace | 160 | 161 | 160 | 161 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
rep(j, size(v)) i >> v[j];
return i;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back
const int MOD = 1000000007;
const int INF = 1e9;
#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s;
cin >> n >> s;
int one = count(all(s), '1');
int mod1 = one - 1, mod2 = one + 1;
vector<ll> memo1(n), memo2(n);
memo1[n - 1] = memo2[n - 1] = 1;
for (int i = n - 2; i >= 0; i--)
memo1[i] = memo1[i + 1] * 2 % mod1;
for (int i = n - 2; i >= 0; i--)
memo2[i] = memo2[i + 1] * 2 % mod2;
int sum1 = 0, sum2 = 0;
if (mod1 > 0)
rep(i, n) {
if (s[i] == '1')
sum1 += memo1[i], sum1 %= mod1;
}
rep(i, n) {
if (s[i] == '1')
sum2 += memo2[i], sum2 %= mod2;
}
if (one <= 1) {
if (one == 0) {
rep(i, n) cout << 1 << '\n';
} else if (one == 1) {
rep(i, n) {
if (s[i] == '1')
cout << 0 << '\n';
else {
int now = sum2;
now = (now + memo2[i]) % mod2;
int ans = 1;
while (now > 0) {
int m = __builtin_popcount(now);
now = now % m;
ans++;
}
cout << ans << '\n';
}
}
}
} else {
rep(i, n) {
if (s[i] == '1') {
int now = sum1;
now = (now + mod1 - memo1[i]) % mod1;
int ans = 1;
while (now > 0) {
int m = __builtin_popcount(now);
now = now % m;
ans++;
}
cout << ans << '\n';
} else {
int now = sum2;
now = (now + memo2[i]) % mod2;
int ans = 1;
while (now > 0) {
int m = __builtin_popcount(now);
now = now % m;
ans++;
}
cout << ans << '\n';
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
rep(j, size(v)) i >> v[j];
return i;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back
const int MOD = 1000000007;
const int INF = 1e9;
#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s;
cin >> n >> s;
int one = count(all(s), '1');
int mod1 = one - 1, mod2 = one + 1;
vector<ll> memo1(n), memo2(n);
memo1[n - 1] = memo2[n - 1] = 1;
if (mod1 > 0)
for (int i = n - 2; i >= 0; i--)
memo1[i] = memo1[i + 1] * 2 % mod1;
for (int i = n - 2; i >= 0; i--)
memo2[i] = memo2[i + 1] * 2 % mod2;
int sum1 = 0, sum2 = 0;
if (mod1 > 0)
rep(i, n) {
if (s[i] == '1')
sum1 += memo1[i], sum1 %= mod1;
}
rep(i, n) {
if (s[i] == '1')
sum2 += memo2[i], sum2 %= mod2;
}
if (one <= 1) {
if (one == 0) {
rep(i, n) cout << 1 << '\n';
} else if (one == 1) {
rep(i, n) {
if (s[i] == '1')
cout << 0 << '\n';
else {
int now = sum2;
now = (now + memo2[i]) % mod2;
int ans = 1;
while (now > 0) {
int m = __builtin_popcount(now);
now = now % m;
ans++;
}
cout << ans << '\n';
}
}
}
} else {
rep(i, n) {
if (s[i] == '1') {
int now = sum1;
now = (now + mod1 - memo1[i]) % mod1;
int ans = 1;
while (now > 0) {
int m = __builtin_popcount(now);
now = now % m;
ans++;
}
cout << ans << '\n';
} else {
int now = sum2;
now = (now + memo2[i]) % mod2;
int ans = 1;
while (now > 0) {
int m = __builtin_popcount(now);
now = now % m;
ans++;
}
cout << ans << '\n';
}
}
}
}
| replace | 89 | 91 | 89 | 92 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// ゼロになる時が例外
int main() {
int n;
cin >> n;
vector<int> A;
string S;
cin >> S;
for (const auto &s : S)
A.push_back(s - '0');
int cnt = 0;
for (const auto &a : A) {
if (a == 1)
cnt++;
}
vector<int> D(n);
if (cnt == 1) {
for (int i = 0; i < n; i++) {
if (A[i] == 1)
cout << 0 << endl;
else if (i == n - 1)
cout << 2 << endl;
else
cout << 1 << endl;
}
return 0;
}
vector<vector<int>> B(3, vector<int>(n));
for (int j = -1; j < 2; j++) {
int m = 1;
for (int i = 0; i < n; i++) {
B[j + 1][n - i - 1] = m;
m *= 2;
m %= (cnt + j);
}
}
vector<int> C(3);
for (int j = 0; j <= 2; j++) {
for (int i = 0; i < n; i++) {
if (A[i] == 1) {
C[j] += B[j][i];
C[j] %= (cnt + j - 1);
}
}
}
for (int i = 0; i < n; i++) {
if (A[i] == 1)
D[i] = (C[0] - B[0][i] + cnt - 1) % (cnt - 1);
else
D[i] = (C[2] + B[2][i]) % (cnt + 1);
}
map<int, int> ans;
auto f = [&](int d, auto self) -> int {
if (ans.count(d))
return ans[d];
if (d == 0)
return 0;
return ans[d] = self(d % __builtin_popcount(d), self) + 1;
};
for (int i = 0; i < n; i++) {
f(D[i], f);
}
for (int i = 0; i < n; i++)
cout << ans[D[i]] + 1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
// ゼロになる時が例外
int main() {
int n;
cin >> n;
vector<int> A;
string S;
cin >> S;
for (const auto &s : S)
A.push_back(s - '0');
int cnt = 0;
for (const auto &a : A) {
if (a == 1)
cnt++;
}
vector<int> D(n);
if (cnt == 1) {
for (int i = 0; i < n; i++) {
if (A[i] == 1)
cout << 0 << endl;
else if (i == n - 1)
cout << 2 << endl;
else
cout << 1 << endl;
}
return 0;
}
if (cnt == 0) {
for (int i = 0; i < n; i++) {
cout << 1 << endl;
}
return 0;
}
vector<vector<int>> B(3, vector<int>(n));
for (int j = -1; j < 2; j++) {
int m = 1;
for (int i = 0; i < n; i++) {
B[j + 1][n - i - 1] = m;
m *= 2;
m %= (cnt + j);
}
}
vector<int> C(3);
for (int j = 0; j <= 2; j++) {
for (int i = 0; i < n; i++) {
if (A[i] == 1) {
C[j] += B[j][i];
C[j] %= (cnt + j - 1);
}
}
}
for (int i = 0; i < n; i++) {
if (A[i] == 1)
D[i] = (C[0] - B[0][i] + cnt - 1) % (cnt - 1);
else
D[i] = (C[2] + B[2][i]) % (cnt + 1);
}
map<int, int> ans;
auto f = [&](int d, auto self) -> int {
if (ans.count(d))
return ans[d];
if (d == 0)
return 0;
return ans[d] = self(d % __builtin_popcount(d), self) + 1;
};
for (int i = 0; i < n; i++) {
f(D[i], f);
}
for (int i = 0; i < n; i++)
cout << ans[D[i]] + 1 << endl;
} | insert | 29 | 29 | 29 | 35 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
std::cout << '(' << a.first << ", " << a.second << ')';
return out;
}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
std::cout << '[';
rep(i, a.size()) {
std::cout << a[i];
if (i != a.size() - 1)
std::cout << ", ";
}
std::cout << ']';
return out;
}
ll N;
std::string X;
void showBin(ll x) {
std::vector<ll> vs;
while (x > 0) {
vs.emplace_back(x & 1);
x >>= 1;
}
repd(i, vs.size()) std::cout << vs[i];
std::cout << std::endl;
}
void sim(ll x) {
while (x > 0) {
showBin(x);
x %= __builtin_popcountll(x);
}
return;
}
ll mod2(const std::string &x, ll mod) {
ll ret = 0;
rep(i, x.size()) {
ret *= 2;
ret %= mod;
ret += x[i] - '0';
ret %= mod;
}
return ret;
}
ll powMod[10];
ll two[200010][3];
int main() {
std::cin >> N >> X;
ll pop = 0;
rep(i, N) pop += X[i] == '1';
// std::cout << pop << std::endl;
for (ll s = -1; s <= 1; ++s) {
powMod[s + 1] = mod2(X, pop + s);
// std::cout << powMod[s+1] << std::endl;
}
two[0][0] = two[0][1] = two[0][2] = 1;
rep(i, N) for (ll s = -1; s <= 1; ++s) if (pop + s) {
two[i + 1][s + 1] = two[i][s + 1] * 2 % (pop + s);
}
rep(i, N) {
ll pop2 = pop;
ll add = 0;
if (X[i] == '1')
X[i] = '0', --pop2;
else
X[i] = '1', ++pop2;
ll ans = 0;
if (!pop2) {
ans = 0;
std::cout << ans << std::endl;
if (X[i] == '1')
X[i] = '0';
else
X[i] = '1';
continue;
}
ll m = (powMod[pop2 - pop + 1] +
(pop2 - pop) * two[N - i - 1][pop2 - pop + 1] + pop2) %
pop2;
ans = 1;
/*std::cout << "YEs" << std::endl;
std::cout << m << std::endl;
std::cout << pop2 << std::endl;
std::cout << two[21][1] << std::endl;*/
while (m > 0) {
m %= __builtin_popcount(m);
++ans;
}
std::cout << ans << std::endl;
if (X[i] == '1')
X[i] = '0';
else
X[i] = '1';
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
std::cout << '(' << a.first << ", " << a.second << ')';
return out;
}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
std::cout << '[';
rep(i, a.size()) {
std::cout << a[i];
if (i != a.size() - 1)
std::cout << ", ";
}
std::cout << ']';
return out;
}
ll N;
std::string X;
void showBin(ll x) {
std::vector<ll> vs;
while (x > 0) {
vs.emplace_back(x & 1);
x >>= 1;
}
repd(i, vs.size()) std::cout << vs[i];
std::cout << std::endl;
}
void sim(ll x) {
while (x > 0) {
showBin(x);
x %= __builtin_popcountll(x);
}
return;
}
ll mod2(const std::string &x, ll mod) {
ll ret = 0;
rep(i, x.size()) {
ret *= 2;
ret %= mod;
ret += x[i] - '0';
ret %= mod;
}
return ret;
}
ll powMod[10];
ll two[200010][3];
int main() {
std::cin >> N >> X;
ll pop = 0;
rep(i, N) pop += X[i] == '1';
// std::cout << pop << std::endl;
for (ll s = -1; s <= 1; ++s)
if (pop + s) {
powMod[s + 1] = mod2(X, pop + s);
// std::cout << powMod[s+1] << std::endl;
}
two[0][0] = two[0][1] = two[0][2] = 1;
rep(i, N) for (ll s = -1; s <= 1; ++s) if (pop + s) {
two[i + 1][s + 1] = two[i][s + 1] * 2 % (pop + s);
}
rep(i, N) {
ll pop2 = pop;
ll add = 0;
if (X[i] == '1')
X[i] = '0', --pop2;
else
X[i] = '1', ++pop2;
ll ans = 0;
if (!pop2) {
ans = 0;
std::cout << ans << std::endl;
if (X[i] == '1')
X[i] = '0';
else
X[i] = '1';
continue;
}
ll m = (powMod[pop2 - pop + 1] +
(pop2 - pop) * two[N - i - 1][pop2 - pop + 1] + pop2) %
pop2;
ans = 1;
/*std::cout << "YEs" << std::endl;
std::cout << m << std::endl;
std::cout << pop2 << std::endl;
std::cout << two[21][1] << std::endl;*/
while (m > 0) {
m %= __builtin_popcount(m);
++ans;
}
std::cout << ans << std::endl;
if (X[i] == '1')
X[i] = '0';
else
X[i] = '1';
}
return 0;
} | replace | 109 | 113 | 109 | 114 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz size()
#define ll long long
ll binpow(ll a, ll b, ll mod) {
a %= mod;
ll res = 1ll;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string s;
cin >> n;
cin >> s;
n = s.size();
int i;
int nuli = 0;
for (i = 0; i < n; i++)
if (s[i] == '1') {
nuli++;
}
int n1 = nuli - 1;
int n2 = nuli + 1;
ll presm1 = 0;
ll presm2 = 0;
for (i = 0; i < n; i++)
if (s[i] == '1') {
if (n1 > 0)
presm1 += binpow(2, n - i - 1, n1);
presm2 += binpow(2, n - i - 1, n2);
}
if (n1 > 0)
presm1 %= n1;
if (n2 > 0)
presm2 %= n2;
int powBuck[20001];
memset(powBuck, -1, sizeof(powBuck));
for (i = 0; i < n; i++) {
int curr = 0;
if (s[i] == '1') {
if (nuli == 1) {
cout << "0\n";
continue;
}
if (n1 > 0) {
curr = presm1;
ll kolku = binpow(2, n - i - 1, n1);
curr = (curr + n1 - kolku) % n1;
}
} else {
curr = presm2;
ll kolku = binpow(2, n - i - 1, n2);
curr = (curr + kolku) % n2;
}
int br = 1;
while (curr) {
if (powBuck[curr] == -1)
powBuck[curr] = __builtin_popcount(curr);
curr = curr % powBuck[curr];
br++;
}
cout << br << "\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz size()
#define ll long long
ll binpow(ll a, ll b, ll mod) {
a %= mod;
ll res = 1ll;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string s;
cin >> n;
cin >> s;
n = s.size();
int i;
int nuli = 0;
for (i = 0; i < n; i++)
if (s[i] == '1') {
nuli++;
}
int n1 = nuli - 1;
int n2 = nuli + 1;
ll presm1 = 0;
ll presm2 = 0;
for (i = 0; i < n; i++)
if (s[i] == '1') {
if (n1 > 0)
presm1 += binpow(2, n - i - 1, n1);
presm2 += binpow(2, n - i - 1, n2);
}
if (n1 > 0)
presm1 %= n1;
if (n2 > 0)
presm2 %= n2;
int powBuck[200001];
memset(powBuck, -1, sizeof(powBuck));
for (i = 0; i < n; i++) {
int curr = 0;
if (s[i] == '1') {
if (nuli == 1) {
cout << "0\n";
continue;
}
if (n1 > 0) {
curr = presm1;
ll kolku = binpow(2, n - i - 1, n1);
curr = (curr + n1 - kolku) % n1;
}
} else {
curr = presm2;
ll kolku = binpow(2, n - i - 1, n2);
curr = (curr + kolku) % n2;
}
int br = 1;
while (curr) {
if (powBuck[curr] == -1)
powBuck[curr] = __builtin_popcount(curr);
curr = curr % powBuck[curr];
br++;
}
cout << br << "\n";
}
return 0;
} | replace | 57 | 58 | 57 | 58 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MAX = 200010;
int main() {
int n;
string x;
cin >> n >> x;
int pc = 0;
rep(i, n) {
if ((x[i] - '0'))
pc++;
}
vector<int> base_0(n), base_1(n);
base_0[n - 1] = 1;
base_1[n - 1] = 1;
int val_0 = 0;
int val_1 = 0;
for (int i = n - 1; i >= 0; i--) {
val_0 += (x[i] - '0') * base_0[i];
val_1 += (x[i] - '0') * base_1[i];
val_0 %= (pc + 1);
if (pc != 1)
val_1 %= (pc - 1);
if (i != 0) {
base_0[i - 1] = base_0[i] * 2;
base_0[i - 1] %= (pc + 1);
base_1[i - 1] = base_1[i] * 2;
base_1[i - 1] %= (pc - 1);
}
}
// 埋め込み
vector<int> f(MAX, 0);
f[0] = 0;
rep(i, 200002) { f[i + 1] = 1 + f[(i + 1) % __builtin_popcount(i + 1)]; }
// 操作
rep(i, n) {
int num;
if ((x[i] - '0') && pc == 1) {
cout << 0 << endl;
continue;
} else if ((x[i] - '0'))
num = (val_1 - base_1[i] + (pc - 1)) % (pc - 1);
else
num = (val_0 + base_0[i]) % (pc + 1);
// printf("num:%d\n", num);
cout << f[num] + 1 << endl;
}
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int MAX = 200010;
int main() {
int n;
string x;
cin >> n >> x;
int pc = 0;
rep(i, n) {
if ((x[i] - '0'))
pc++;
}
vector<int> base_0(n), base_1(n);
base_0[n - 1] = 1;
base_1[n - 1] = 1;
int val_0 = 0;
int val_1 = 0;
for (int i = n - 1; i >= 0; i--) {
val_0 += (x[i] - '0') * base_0[i];
val_1 += (x[i] - '0') * base_1[i];
val_0 %= (pc + 1);
if (pc != 1)
val_1 %= (pc - 1);
if (i != 0) {
base_0[i - 1] = base_0[i] * 2;
base_0[i - 1] %= (pc + 1);
base_1[i - 1] = base_1[i] * 2;
if (pc != 1)
base_1[i - 1] %= (pc - 1);
}
}
// 埋め込み
vector<int> f(MAX, 0);
f[0] = 0;
rep(i, 200002) { f[i + 1] = 1 + f[(i + 1) % __builtin_popcount(i + 1)]; }
// 操作
rep(i, n) {
int num;
if ((x[i] - '0') && pc == 1) {
cout << 0 << endl;
continue;
} else if ((x[i] - '0'))
num = (val_1 - base_1[i] + (pc - 1)) % (pc - 1);
else
num = (val_0 + base_0[i]) % (pc + 1);
// printf("num:%d\n", num);
cout << f[num] + 1 << endl;
}
}
| replace | 30 | 31 | 30 | 32 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, s, n) for (int i = s; i < (int)(n); i++)
int pcnt(int x) { return __builtin_popcount(x); }
int f(int x) {
if (x == 0)
return 0;
else
return f(x % pcnt(x)) + 1;
}
int main() {
int n, pc = 0;
string s;
cin >> n >> s;
vector<int> x(n);
vector<int> ans(n);
rep(i, 0, n) {
x[i] = s[i] - '0';
if (s[i] == '1')
pc++;
}
rep(b, 0, 2) {
int npc = pc, r0 = 0;
if (b == 0)
npc++;
else
npc--;
rep(i, 0, n) {
r0 = (2 * r0) % npc;
r0 += x[i];
}
int k = 1;
for (int i = n - 1; i >= 0; i--) {
int r = r0;
if (x[i] == b) {
if (b == 0)
r = (r + k) % npc;
else
r = (r - k + npc) % npc;
ans[i] = f(r) + 1;
}
k = (2 * k) % npc;
}
}
rep(i, 0, n) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, s, n) for (int i = s; i < (int)(n); i++)
int pcnt(int x) { return __builtin_popcount(x); }
int f(int x) {
if (x == 0)
return 0;
else
return f(x % pcnt(x)) + 1;
}
int main() {
int n, pc = 0;
string s;
cin >> n >> s;
vector<int> x(n);
vector<int> ans(n);
rep(i, 0, n) {
x[i] = s[i] - '0';
if (s[i] == '1')
pc++;
}
rep(b, 0, 2) {
int npc = pc, r0 = 0;
if (b == 0)
npc++;
else
npc--;
if (npc <= 0)
continue;
rep(i, 0, n) {
r0 = (2 * r0) % npc;
r0 += x[i];
}
int k = 1;
for (int i = n - 1; i >= 0; i--) {
int r = r0;
if (x[i] == b) {
if (b == 0)
r = (r + k) % npc;
else
r = (r - k + npc) % npc;
ans[i] = f(r) + 1;
}
k = (2 * k) % npc;
}
}
rep(i, 0, n) cout << ans[i] << endl;
} | insert | 32 | 32 | 32 | 34 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T div_floor(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
int main() {
int n;
string s;
cin >> n >> s;
int dp[MAX];
dp[0] = 0;
For(i, 1, MAX) dp[i] = 1 + dp[i % __builtin_popcount(i)];
int p = count_if(s.begin(), s.end(), [](char c) { return c == '1'; }) + 1;
int q = p - 2;
int pow2_p[n], pow2_q[n], x = 0, y = 0;
pow2_p[0] = pow2_q[0] = 1;
For(i, 1, n) {
pow2_p[i] = pow2_p[i - 1] * 2 % p;
if (q > 0)
pow2_q[i] = pow2_q[i - 1] * 2 % q;
}
rep(i, n) {
x = (x * 2 + (s[i] - '0')) % p;
if (q > 0)
y = (y * 2 + (s[i] - '0')) % q;
}
rep(i, n) {
if (s[i] == '0') {
int tmp = (x + pow2_p[n - 1 - i]) % p;
printf("%d\n", 1 + dp[tmp]);
} else {
int tmp = (y + q - pow2_q[n - 1 - i]) % q;
printf("%d\n", 1 + dp[tmp]);
}
}
} | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T div_floor(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
int main() {
int n;
string s;
cin >> n >> s;
int dp[MAX];
dp[0] = 0;
For(i, 1, MAX) dp[i] = 1 + dp[i % __builtin_popcount(i)];
int p = count_if(s.begin(), s.end(), [](char c) { return c == '1'; }) + 1;
int q = p - 2;
int pow2_p[n], pow2_q[n], x = 0, y = 0;
pow2_p[0] = pow2_q[0] = 1;
For(i, 1, n) {
pow2_p[i] = pow2_p[i - 1] * 2 % p;
if (q > 0)
pow2_q[i] = pow2_q[i - 1] * 2 % q;
}
rep(i, n) {
x = (x * 2 + (s[i] - '0')) % p;
if (q > 0)
y = (y * 2 + (s[i] - '0')) % q;
}
rep(i, n) {
if (s[i] == '0') {
int tmp = (x + pow2_p[n - 1 - i]) % p;
printf("%d\n", 1 + dp[tmp]);
} else {
if (q == 0)
printf("%d\n", 0);
else {
int tmp = (y + q - pow2_q[n - 1 - i]) % q;
printf("%d\n", 1 + dp[tmp]);
}
}
}
} | replace | 70 | 72 | 70 | 76 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int MAX_N = 200000 * 2;
map<ll, vector<ll>> powers;
ll f(ll mod, int bit, bool to_one, const string &original_S) {
// printf("mod: %lld, bit: %d, to_one: %d, %s\n", mod, bit, to_one,
// original_S.c_str());
static map<ll, ll> memo;
if (powers.find(mod) == powers.end()) {
// power[i] is 2 ^ i % mod
vector<ll> power(MAX_N + 10);
power[0] = 1 % mod;
for (int i = 1; i <= MAX_N; i++) {
power[i] = (power[i - 1] * 2) % mod;
// printf("power[%d]: %lld\n", i, power[i]);
}
powers[mod] = power;
}
vector<ll> &power = powers[mod];
if (memo.find(mod) == memo.end()) {
ll v = 0;
string T = original_S;
reverse(T.begin(), T.end());
for (int i = 0; i < (int)T.size(); i++) {
if (T[i] == '1') {
v = (v + power[i]) % mod;
}
}
memo[mod] = v;
}
ll ans = memo[mod];
// printf("memo: %lld\n", ans);
if (to_one) {
ans = (ans + power[bit]) % mod;
} else {
// if ((x += MOD-a.x) >= MOD) x -= MOD;
if ((ans += mod - power[bit]) >= mod)
ans -= mod;
// ans = (ans - power[bit]) % mod;
// while (ans < 0) {
// ans += mod;
// }
}
// string T = S;
// reverse(T.begin(), T.end());
// for (int i = 0; i < (int) T.size(); i++) {
// if (T[i] == '1') {
// ans = (ans + (*power)[i]) % mod;
// }
// }
return ans;
}
int count_bit(const string &S) {
int ans = 0;
for (int i = 0; i < (int)S.size(); i++) {
if (S[i] == '1') {
ans++;
}
}
return ans;
}
int main(void) {
int N;
cin >> N;
string S;
cin >> S;
const int bit_count = count_bit(S);
// printf("bit_count: %d\n", bit_count);
for (int i = 0; i < N; i++) {
string T = S;
ll mod;
if (T[i] == '1') {
T[i] = '0';
mod = bit_count - 1;
} else {
T[i] = '1';
mod = bit_count + 1;
}
// printf("%s, %lld\n", T.c_str(), mod);
// printf("%d: %lld\n", i, f(mod, T));
ll x = f(mod, N - i - 1, T[i] == '1', S);
int cnt = 1;
// printf("x: %lld\n", x);
while (x > 0) {
int bit_count = __builtin_popcountll(x);
// printf("x: %lld, bit_count: %d\n", x, bit_count);
x = x % bit_count; // f(bit_count, to_string(x));
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
| #include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const int MAX_N = 200000 * 2;
map<ll, vector<ll>> powers;
ll f(ll mod, int bit, bool to_one, const string &original_S) {
// printf("mod: %lld, bit: %d, to_one: %d, %s\n", mod, bit, to_one,
// original_S.c_str());
static map<ll, ll> memo;
if (powers.find(mod) == powers.end()) {
// power[i] is 2 ^ i % mod
vector<ll> power(MAX_N + 10);
power[0] = 1 % mod;
for (int i = 1; i <= MAX_N; i++) {
power[i] = (power[i - 1] * 2) % mod;
// printf("power[%d]: %lld\n", i, power[i]);
}
powers[mod] = power;
}
vector<ll> &power = powers[mod];
if (memo.find(mod) == memo.end()) {
ll v = 0;
string T = original_S;
reverse(T.begin(), T.end());
for (int i = 0; i < (int)T.size(); i++) {
if (T[i] == '1') {
v = (v + power[i]) % mod;
}
}
memo[mod] = v;
}
ll ans = memo[mod];
// printf("memo: %lld\n", ans);
if (to_one) {
ans = (ans + power[bit]) % mod;
} else {
// if ((x += MOD-a.x) >= MOD) x -= MOD;
if ((ans += mod - power[bit]) >= mod)
ans -= mod;
// ans = (ans - power[bit]) % mod;
// while (ans < 0) {
// ans += mod;
// }
}
// string T = S;
// reverse(T.begin(), T.end());
// for (int i = 0; i < (int) T.size(); i++) {
// if (T[i] == '1') {
// ans = (ans + (*power)[i]) % mod;
// }
// }
return ans;
}
int count_bit(const string &S) {
int ans = 0;
for (int i = 0; i < (int)S.size(); i++) {
if (S[i] == '1') {
ans++;
}
}
return ans;
}
int main(void) {
int N;
cin >> N;
string S;
cin >> S;
const int bit_count = count_bit(S);
// printf("bit_count: %d\n", bit_count);
for (int i = 0; i < N; i++) {
string T = S;
ll mod;
if (T[i] == '1') {
T[i] = '0';
mod = bit_count - 1;
} else {
T[i] = '1';
mod = bit_count + 1;
}
// printf("%s, %lld\n", T.c_str(), mod);
// printf("%d: %lld\n", i, f(mod, T));
if (mod == 0) {
printf("%d\n", 0);
continue;
}
ll x = f(mod, N - i - 1, T[i] == '1', S);
int cnt = 1;
// printf("x: %lld\n", x);
while (x > 0) {
int bit_count = __builtin_popcountll(x);
// printf("x: %lld, bit_count: %d\n", x, bit_count);
x = x % bit_count; // f(bit_count, to_string(x));
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
| insert | 124 | 124 | 124 | 128 | 0 | |
p02609 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> ans(n);
for (int i = 1; i < n; i++) {
int b = 0, t = i;
while (t) {
b += (t & 1);
t >>= 1;
}
if (i % b == 0)
ans[i] = 1;
else
ans[i] = ans[i % b] + 1;
}
vector<int> a(n);
int s = 0;
for (int i = 0; i < n; i++) {
char c;
scanf(" %c", &c);
a[i] = c - '0';
s += a[i];
}
vector<vector<int>> two(2, vector<int>(n));
vector<int> x(2);
for (int i = 0; i < 2; i++) {
int m = i * 2 - 1;
two[i][0] = 1 % (s + m);
x[i] = a[0] % (s + m);
for (int j = 1; j < n; j++) {
two[i][j] = two[i][j - 1] * 2 % (s + m);
x[i] = (x[i] * 2 + a[j]) % (s + m);
}
}
for (int i = 0; i < n; i++) {
int u = s - 2 * a[i] + 1;
if (u == 0)
printf("0\n");
else {
int v =
(x[1 - a[i]] + (-2 * a[i] + 1) * two[1 - a[i]][n - i - 1] + u) % u;
printf("%d\n", ans[v] + 1);
}
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> ans(n);
for (int i = 1; i < n; i++) {
int b = 0, t = i;
while (t) {
b += (t & 1);
t >>= 1;
}
if (i % b == 0)
ans[i] = 1;
else
ans[i] = ans[i % b] + 1;
}
vector<int> a(n);
int s = 0;
for (int i = 0; i < n; i++) {
char c;
scanf(" %c", &c);
a[i] = c - '0';
s += a[i];
}
vector<vector<int>> two(2, vector<int>(n));
vector<int> x(2);
for (int i = 0; i < 2; i++) {
int m = i * 2 - 1;
if (s + m > 0) {
two[i][0] = 1 % (s + m);
x[i] = a[0] % (s + m);
for (int j = 1; j < n; j++) {
two[i][j] = two[i][j - 1] * 2 % (s + m);
x[i] = (x[i] * 2 + a[j]) % (s + m);
}
}
}
for (int i = 0; i < n; i++) {
int u = s - 2 * a[i] + 1;
if (u == 0)
printf("0\n");
else {
int v =
(x[1 - a[i]] + (-2 * a[i] + 1) * two[1 - a[i]][n - i - 1] + u) % u;
printf("%d\n", ans[v] + 1);
}
}
return 0;
} | replace | 35 | 41 | 35 | 42 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout << "Case #" << i + 1 << " :";
#define s(n) scanf("%d", &n)
#define rev(i, n) for (int i = n; i >= 0; --i)
#define REV(i, a, b) for (int i = a; i >= b; --i)
#define miN(a, b) (((a) < (b)) ? (a) : (b))
#define sc(n) scanf("%c", &n)
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define INF 1000000000
#define pii pair<long long int, long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
void pre() {}
int val(long long int a) {
int ret = 0;
while (a != 0) {
a = a % __builtin_popcount(a);
ret++;
}
return ret;
}
void solve() {
long long int n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
int a = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
a++;
}
}
long long int arr[n];
long long int brr[n];
for (int i = 0; i < n; i++) {
brr[i] = 0;
}
arr[0] = 1 % (a + 1);
if (a - 1 > 0)
brr[0] = 1 % (a - 1);
long long int up = arr[0] * (s[0] - '0'), down = brr[0] * (s[0] - '0');
for (int i = 1; i < n; i++) {
arr[i] = (arr[i - 1] * 2) % (a + 1);
if (a - 1 > 0) {
brr[i] = (brr[i - 1] * 2) % (a - 1);
}
up += arr[i] * (s[i] - '0');
down += brr[i] * (s[i] - '0');
}
long long int temp = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
if (a == '1') {
cout << 1 << "\n";
continue;
}
temp = down - brr[i];
temp = (temp + a - 1) % (a - 1);
cout << 1 + val(temp) << "\n";
} else {
temp = up + arr[i];
temp %= (a + 1);
cout << 1 + val(temp) << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
| #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout << "Case #" << i + 1 << " :";
#define s(n) scanf("%d", &n)
#define rev(i, n) for (int i = n; i >= 0; --i)
#define REV(i, a, b) for (int i = a; i >= b; --i)
#define miN(a, b) (((a) < (b)) ? (a) : (b))
#define sc(n) scanf("%c", &n)
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define INF 1000000000
#define pii pair<long long int, long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
void pre() {}
int val(long long int a) {
int ret = 0;
while (a != 0) {
a = a % __builtin_popcount(a);
ret++;
}
return ret;
}
void solve() {
long long int n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
int a = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
a++;
}
}
long long int arr[n];
long long int brr[n];
for (int i = 0; i < n; i++) {
brr[i] = 0;
}
arr[0] = 1 % (a + 1);
if (a - 1 > 0)
brr[0] = 1 % (a - 1);
long long int up = arr[0] * (s[0] - '0'), down = brr[0] * (s[0] - '0');
for (int i = 1; i < n; i++) {
arr[i] = (arr[i - 1] * 2) % (a + 1);
if (a - 1 > 0) {
brr[i] = (brr[i - 1] * 2) % (a - 1);
}
up += arr[i] * (s[i] - '0');
down += brr[i] * (s[i] - '0');
}
long long int temp = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
if (a == 1) {
cout << 0 << "\n";
continue;
}
temp = down - brr[i];
temp = (temp + a - 1) % (a - 1);
cout << 1 + val(temp) << "\n";
} else {
temp = up + arr[i];
temp %= (a + 1);
cout << 1 + val(temp) << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
| replace | 63 | 65 | 63 | 65 | 0 | |
p02609 | Python | Runtime Error | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
N = int(readline())
X = readline().rstrip().decode("utf-8")
nx = int(X, 2)
ans = [0] * N
oc = X.count("1")
if oc - 1 != 0:
mx = nx % (oc - 1)
f = True
else:
f = False
px = nx % (oc + 1)
for i in range(N):
if X[i] == "0":
a = (px + pow(2, N - i - 1, oc + 1)) % (oc + 1)
elif f:
a = (mx - pow(2, N - i - 1, oc - 1)) % (oc - 1)
else:
ans[i] = 0
count = 1
while a:
a = a % bin(a).count("1")
count += 1
ans[i] = count
print("\n".join(map(str, ans)))
if __name__ == "__main__":
main()
| import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
N = int(readline())
X = readline().rstrip().decode("utf-8")
nx = int(X, 2)
ans = [0] * N
oc = X.count("1")
if oc - 1 != 0:
mx = nx % (oc - 1)
f = True
else:
f = False
px = nx % (oc + 1)
for i in range(N):
if X[i] == "0":
a = (px + pow(2, N - i - 1, oc + 1)) % (oc + 1)
elif f:
a = (mx - pow(2, N - i - 1, oc - 1)) % (oc - 1)
else:
ans[i] = 0
continue
count = 1
while a:
a = a % bin(a).count("1")
count += 1
ans[i] = count
print("\n".join(map(str, ans)))
if __name__ == "__main__":
main()
| insert | 31 | 31 | 31 | 32 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()
ll modPow(ll a, ll n, ll p) {
if (n == 0)
return 1; // 0乗にも対応する場合
if (n == 1)
return a % p;
if (n % 2 == 1)
return (a * modPow(a, n - 1, p)) % p;
ll t = modPow(a, n / 2, p);
return (t * t) % p;
}
int main() {
ll N, x = 0, s = 0, t = 0;
string X;
cin >> N >> X;
vector<ll> f(N);
rep(i, N) {
if (i == 0)
f.at(i) = 0;
else {
bitset<20> s(i);
f.at(i) = f.at(i % s.count()) + 1;
}
}
rep(i, N) {
if (X.at(i) == '1')
x++;
}
rep(i, N) {
if (X.at(i) == '1') {
t += modPow(2, N - 1 - i, x + 1);
t %= x + 1;
s += modPow(2, N - 1 - i, x - 1);
s %= x - 1;
}
}
rep(i, N) {
if (x == 1 && X.at(i) == '1') {
cout << 0 << endl;
} else if (X.at(i) == '1') {
ll S = s;
S += x - 1;
S -= modPow(2, N - 1 - i, x - 1);
S %= x - 1;
cout << f.at(S) + 1 << endl;
} else {
ll T = t;
T += modPow(2, N - 1 - i, x + 1);
T %= x + 1;
cout << f.at(T) + 1 << endl;
}
}
}
| #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()
ll modPow(ll a, ll n, ll p) {
if (n == 0)
return 1; // 0乗にも対応する場合
if (n == 1)
return a % p;
if (n % 2 == 1)
return (a * modPow(a, n - 1, p)) % p;
ll t = modPow(a, n / 2, p);
return (t * t) % p;
}
int main() {
ll N, x = 0, s = 0, t = 0;
string X;
cin >> N >> X;
vector<ll> f(N);
rep(i, N) {
if (i == 0)
f.at(i) = 0;
else {
bitset<20> s(i);
f.at(i) = f.at(i % s.count()) + 1;
}
}
rep(i, N) {
if (X.at(i) == '1')
x++;
}
rep(i, N) {
if (X.at(i) == '1') {
t += modPow(2, N - 1 - i, x + 1);
t %= x + 1;
if (x > 1) {
s += modPow(2, N - 1 - i, x - 1);
s %= x - 1;
}
}
}
rep(i, N) {
if (x == 1 && X.at(i) == '1') {
cout << 0 << endl;
} else if (X.at(i) == '1') {
ll S = s;
S += x - 1;
S -= modPow(2, N - 1 - i, x - 1);
S %= x - 1;
cout << f.at(S) + 1 << endl;
} else {
ll T = t;
T += modPow(2, N - 1 - i, x + 1);
T %= x + 1;
cout << f.at(T) + 1 << endl;
}
}
} | replace | 41 | 43 | 41 | 45 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
int pw(int n, int k, int MOD) {
assert(k >= 0);
int res = 1;
while (k) {
if (k & 1)
(res *= n) %= MOD;
(n *= n) %= MOD;
k >>= 1;
}
return res;
}
int f(string s, int a) {
int res = 0, t = 1;
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i++) {
(res += t * (s[i] == '1')) %= a;
(t *= 2) %= a;
}
return res;
}
int p(int a) {
int res = 0;
while (a) {
if (a & 1)
res++;
a >>= 1;
}
return res;
}
int F(int a) {
int res = 0;
while (a) {
res++;
a %= p(a);
}
return res;
}
signed main() {
int n;
cin >> n;
string s;
cin >> s;
int sum = 0;
for (char p : s)
sum += p == '1';
int A = f(s, sum + 1);
int B = f(s, sum - 1);
for (int i = 0; i < n; i++) {
if (sum == 1 && s[i] == '1') {
cout << 0 << endl;
continue;
}
if (s[i] == '0') {
int res = A;
res += pw(2, n - i - 1, sum + 1);
res %= sum + 1;
cout << F(res) + 1 << endl;
} else {
int res = B;
res -= pw(2, n - i - 1, sum - 1);
res += sum - 1;
res %= sum - 1;
cout << F(res) + 1 << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
int pw(int n, int k, int MOD) {
assert(k >= 0);
int res = 1;
while (k) {
if (k & 1)
(res *= n) %= MOD;
(n *= n) %= MOD;
k >>= 1;
}
return res;
}
int f(string s, int a) {
if (a == 0)
return -1;
int res = 0, t = 1;
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); i++) {
(res += t * (s[i] == '1')) %= a;
(t *= 2) %= a;
}
return res;
}
int p(int a) {
int res = 0;
while (a) {
if (a & 1)
res++;
a >>= 1;
}
return res;
}
int F(int a) {
int res = 0;
while (a) {
res++;
a %= p(a);
}
return res;
}
signed main() {
int n;
cin >> n;
string s;
cin >> s;
int sum = 0;
for (char p : s)
sum += p == '1';
int A = f(s, sum + 1);
int B = f(s, sum - 1);
for (int i = 0; i < n; i++) {
if (sum == 1 && s[i] == '1') {
cout << 0 << endl;
continue;
}
if (s[i] == '0') {
int res = A;
res += pw(2, n - i - 1, sum + 1);
res %= sum + 1;
cout << F(res) + 1 << endl;
} else {
int res = B;
res -= pw(2, n - i - 1, sum - 1);
res += sum - 1;
res %= sum - 1;
cout << F(res) + 1 << endl;
}
}
}
| insert | 16 | 16 | 16 | 18 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-10;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
// ここから編集
ll modPow(ll x, ll n, ll mod = MOD) {
ll res = 1;
while (n) {
if (n & 1)
res = (res * x) % mod;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
int N;
string s;
cin >> N >> s;
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1')
cnt++;
}
if (cnt == 0) {
ll plus1 = cnt + 1;
ll s1 = 0;
for (int i = 0; i < s.size(); i++) {
// 0 -> 1
ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
}
} else {
ll plus1 = cnt + 1, minus1 = cnt - 1;
ll s1 = 0, s2 = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
s1 += modPow(2, s.size() - i - 1, plus1);
s1 %= plus1;
s2 += modPow(2, s.size() - i - 1, minus1);
s2 %= minus1;
}
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
// 1 -> 0
ll t = (s2 - modPow(2, s.size() - i - 1, minus1) + minus1) % minus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
} else {
// 0 -> 1
ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
}
}
}
return 0;
}
| #include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-10;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
// ここから編集
ll modPow(ll x, ll n, ll mod = MOD) {
ll res = 1;
while (n) {
if (n & 1)
res = (res * x) % mod;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
int N;
string s;
cin >> N >> s;
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1')
cnt++;
}
if (cnt == 0) {
ll plus1 = cnt + 1;
ll s1 = 0;
for (int i = 0; i < s.size(); i++) {
// 0 -> 1
ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
}
} else if (cnt == 1) {
ll plus1 = cnt + 1, minus1 = cnt - 1;
ll s1 = 0, s2 = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
s1 += modPow(2, s.size() - i - 1, plus1);
s1 %= plus1;
}
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
// 1 -> 0
if (minus1 == 0) {
cout << 0 << endl;
continue;
}
ll t = (s2 - modPow(2, s.size() - i - 1, minus1) + minus1) % minus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
} else {
// 0 -> 1
ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
}
}
} else {
ll plus1 = cnt + 1, minus1 = cnt - 1;
ll s1 = 0, s2 = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
s1 += modPow(2, s.size() - i - 1, plus1);
s1 %= plus1;
s2 += modPow(2, s.size() - i - 1, minus1);
s2 %= minus1;
}
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
// 1 -> 0
ll t = (s2 - modPow(2, s.size() - i - 1, minus1) + minus1) % minus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
} else {
// 0 -> 1
ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
int ans = 1;
while (t) {
t %= __builtin_popcount(t);
ans++;
}
cout << ans << endl;
}
}
}
return 0;
}
| insert | 116 | 116 | 116 | 152 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int v[200010], r[200010];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
string s;
cin >> s;
int pcnt = 0;
for (int i = 0; i < n; i++)
pcnt += s[i] == '1';
for (int i = 1; i <= n; i++) {
r[i] = 1 + r[i % __builtin_popcount(i)];
}
int p = pcnt + 1, val1 = 0;
int q = pcnt - 1, val2 = 0;
for (int i = 0; i < n; i++) {
val1 = val1 * 2 + (s[i] == '1');
val1 %= p;
if (q > 0) {
val2 = val2 * 2 + (s[i] == '1');
val2 %= q;
}
}
int mul1 = 1, mul2 = 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0') {
int now = (val1 + mul1) % p;
v[i] = now % p;
} else if (q > 0) {
int now = (val2 + q - mul2 % q) % q;
v[i] = now % q;
} else {
v[i] = -1;
}
mul1 = mul1 * 2 % p;
mul2 = mul2 * 2 % q;
}
for (int i = 0; i < n; i++) {
cout << (v[i] == -1 ? 0 : 1 + r[v[i]]) << endl;
}
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int v[200010], r[200010];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
string s;
cin >> s;
int pcnt = 0;
for (int i = 0; i < n; i++)
pcnt += s[i] == '1';
for (int i = 1; i <= n; i++) {
r[i] = 1 + r[i % __builtin_popcount(i)];
}
int p = pcnt + 1, val1 = 0;
int q = pcnt - 1, val2 = 0;
for (int i = 0; i < n; i++) {
val1 = val1 * 2 + (s[i] == '1');
val1 %= p;
if (q > 0) {
val2 = val2 * 2 + (s[i] == '1');
val2 %= q;
}
}
int mul1 = 1, mul2 = 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0') {
int now = (val1 + mul1) % p;
v[i] = now % p;
} else if (q > 0) {
int now = (val2 + q - mul2 % q) % q;
v[i] = now % q;
} else {
v[i] = -1;
}
mul1 = mul1 * 2 % p;
if (q > 0)
mul2 = mul2 * 2 % q;
}
for (int i = 0; i < n; i++) {
cout << (v[i] == -1 ? 0 : 1 + r[v[i]]) << endl;
}
}
| replace | 48 | 49 | 48 | 50 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// 定数
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
const long double PI = 3.1415926535897932;
const long long MAXLL = 9223372036854775807;
const long long INF = 2305843009213693951;
const long long dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const long long dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
// 型名省略
#define ll long long
#define ull unsigned long long
#define ld long double
#define uld unsigned long double
#define pll pair<long long, long long>
#define tll(n) array<long long, (int)(n)>
#define pcc pair<char, char>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<pair<long long, long long>>
#define vt(n) vector<array<long long, (int)(n)>>
#define umap unordered_map
#define uset unordered_set
#define Lqueue priority_queue<long long>
#define Squeue priority_queue<long long, vector<long long>, greater<long long>>
#define Fi first
#define Se second
#define mp make_pair
#define eb emplace_back
// マクロ
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rbf(a, x) for (auto &a : x)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define bitrep(i, s, n) for (long long i = (s); i < (1LL << (n)); i++)
#define Maxe(x) *max_element((x).begin(), (x).end())
#define Mine(x) *min_element((x).begin(), (x).end())
#define Size(x) ((long long)(x).size())
#define lin(s) getline(cin, (s))
#define Ssort(v) \
sort((v).begin(), (v).end(), \
[](auto &left, auto &right) { return left.second < right.second; })
// Yes,No
void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
// YES.NO
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }
// MAX,MIN
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
// 最大公約数,最小公倍数
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
// 累乗
template <typename t> constexpr t mypow(t a, long long b) {
if (b == 0)
return 1;
if (a == 0)
return 0;
t x = 1;
while (b > 0) {
if (b & 1LL)
x *= a;
a *= a;
b >>= 1LL;
}
return x;
}
#define pow mypow
// chmin,chmax
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// 組み合わせ O(r)
template <typename t> constexpr t nCr(t n, long long r) {
if (r == 0)
return 1;
if (n == 0)
return 0;
if (n < r)
return 0;
t x = 1;
for (long long i = 1; i <= r; i++) {
x *= n - i + 1;
x /= i;
}
return x;
}
ll solve(ll a) {
ll c = __builtin_popcount(a);
if (c == 0)
return 1;
return solve(a % c) + 1;
}
// main関数
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
string x;
cin >> n >> x;
ll cnt = 0;
rep(i, n) if (x[i] == '1') cnt++;
vl d1(n), d2(n);
d1[0] = d2[0] = 1;
rep2(i, 1, n) {
d1[i] = d1[i - 1] * 2 % (cnt + 1);
if (cnt - 1 != 0)
d2[i] = d2[i - 1] * 2 % (cnt - 1);
}
ll sum1 = 0;
ll sum2 = 0;
rep(i, n) {
if (x[n - 1 - i] == '1') {
sum1 += d1[i];
sum1 %= cnt + 1;
if (cnt - 1 != 0) {
sum2 += d2[i];
sum2 %= cnt - 1;
}
}
}
rep(i, n) {
ll res;
if (x[i] == '1') {
res = sum2;
res -= d2[n - 1 - i];
res += cnt - 1;
res %= cnt - 1;
} else {
res = sum1;
res += d1[n - 1 - i];
res %= cnt + 1;
}
cout << solve(res) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
// 定数
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
const long double PI = 3.1415926535897932;
const long long MAXLL = 9223372036854775807;
const long long INF = 2305843009213693951;
const long long dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const long long dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
// 型名省略
#define ll long long
#define ull unsigned long long
#define ld long double
#define uld unsigned long double
#define pll pair<long long, long long>
#define tll(n) array<long long, (int)(n)>
#define pcc pair<char, char>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<pair<long long, long long>>
#define vt(n) vector<array<long long, (int)(n)>>
#define umap unordered_map
#define uset unordered_set
#define Lqueue priority_queue<long long>
#define Squeue priority_queue<long long, vector<long long>, greater<long long>>
#define Fi first
#define Se second
#define mp make_pair
#define eb emplace_back
// マクロ
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rbf(a, x) for (auto &a : x)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++)
#define bitrep(i, s, n) for (long long i = (s); i < (1LL << (n)); i++)
#define Maxe(x) *max_element((x).begin(), (x).end())
#define Mine(x) *min_element((x).begin(), (x).end())
#define Size(x) ((long long)(x).size())
#define lin(s) getline(cin, (s))
#define Ssort(v) \
sort((v).begin(), (v).end(), \
[](auto &left, auto &right) { return left.second < right.second; })
// Yes,No
void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
// YES.NO
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }
// MAX,MIN
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
// 最大公約数,最小公倍数
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
// 累乗
template <typename t> constexpr t mypow(t a, long long b) {
if (b == 0)
return 1;
if (a == 0)
return 0;
t x = 1;
while (b > 0) {
if (b & 1LL)
x *= a;
a *= a;
b >>= 1LL;
}
return x;
}
#define pow mypow
// chmin,chmax
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// 組み合わせ O(r)
template <typename t> constexpr t nCr(t n, long long r) {
if (r == 0)
return 1;
if (n == 0)
return 0;
if (n < r)
return 0;
t x = 1;
for (long long i = 1; i <= r; i++) {
x *= n - i + 1;
x /= i;
}
return x;
}
ll solve(ll a) {
ll c = __builtin_popcount(a);
if (c == 0)
return 1;
return solve(a % c) + 1;
}
// main関数
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
string x;
cin >> n >> x;
ll cnt = 0;
rep(i, n) if (x[i] == '1') cnt++;
vl d1(n), d2(n);
d1[0] = d2[0] = 1;
rep2(i, 1, n) {
d1[i] = d1[i - 1] * 2 % (cnt + 1);
if (cnt - 1 != 0)
d2[i] = d2[i - 1] * 2 % (cnt - 1);
}
ll sum1 = 0;
ll sum2 = 0;
rep(i, n) {
if (x[n - 1 - i] == '1') {
sum1 += d1[i];
sum1 %= cnt + 1;
if (cnt - 1 != 0) {
sum2 += d2[i];
sum2 %= cnt - 1;
}
}
}
rep(i, n) {
ll res;
if (x[i] == '1') {
res = sum2;
res -= d2[n - 1 - i];
res += cnt - 1;
if (cnt - 1 != 0)
res %= cnt - 1;
else {
cout << 0 << endl;
continue;
}
} else {
res = sum1;
res += d1[n - 1 - i];
res %= cnt + 1;
}
cout << solve(res) << endl;
}
}
| replace | 155 | 156 | 155 | 161 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
long long MOD = 1000000000 + 7;
ll pow2mod(ll x, ll y) {
// 2^x を yで割ったあまり
ll ret = 0;
if (y == 0)
return 0;
if (y == 1)
return 0;
if (x == 0)
return 1;
if (x == 1)
return 2 % y;
if (x % 2 == 0) {
ret = pow2mod(x / 2, y);
return (ret * ret) % y;
}
if (x % 2 == 1) {
ret = pow2mod(x - 1, y) * 2;
return ret % y;
}
return 0;
}
map<ll, int> m;
int solve(int x, int num) {
if (x == 0)
return num;
if (x == 1)
return (num + 1);
ll p = __builtin_popcount(x);
if (p == 0) {
return num;
}
if (x % p == 0) {
return (num + 1);
} else {
return solve(x % p, num + 1);
}
}
int main() {
cout << setprecision(10);
int N;
cin >> N;
string X;
cin >> X;
ll y = 0;
for (char c : X) {
if (c == '1')
y++;
}
// X % (y+1)と、X%(y-1)を調べておく
int modyp1 = 0;
int modym1 = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '0')
continue;
modyp1 += pow2mod(N - i - 1, y + 1);
modyp1 %= (y + 1);
modym1 += pow2mod(N - i - 1, y - 1);
modym1 %= (y - 1);
}
cerr << modyp1 << " " << modym1 << endl;
for (int i = 0; i < N; i++) {
int j = N - i - 1;
if (X[i] == '0') {
int tmp = modyp1; // X%(y+1)
tmp += pow2mod(j, y + 1); // y+1でわったあまりが得られた
tmp %= (y + 1);
// cerr << "i: " << i << " tmp: " << tmp << " y+1: " << y+1 << endl;
cout << solve(tmp, 1) << endl;
} else {
int tmp = modym1; // X%(y-1)
tmp -= pow2mod(j, y - 1); // y-1でわったあまりが得られた
tmp += y - 1;
if (y - 1 == 0) {
cout << 0 << endl;
} else {
tmp %= (y - 1);
// cerr << "i: " << i << " tmp: " << tmp << " y-1: " << y-1 << endl;
cout << solve(tmp, 1) << endl;
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
long long MOD = 1000000000 + 7;
ll pow2mod(ll x, ll y) {
// 2^x を yで割ったあまり
ll ret = 0;
if (y == 0)
return 0;
if (y == 1)
return 0;
if (x == 0)
return 1;
if (x == 1)
return 2 % y;
if (x % 2 == 0) {
ret = pow2mod(x / 2, y);
return (ret * ret) % y;
}
if (x % 2 == 1) {
ret = pow2mod(x - 1, y) * 2;
return ret % y;
}
return 0;
}
map<ll, int> m;
int solve(int x, int num) {
if (x == 0)
return num;
if (x == 1)
return (num + 1);
ll p = __builtin_popcount(x);
if (p == 0) {
return num;
}
if (x % p == 0) {
return (num + 1);
} else {
return solve(x % p, num + 1);
}
}
int main() {
cout << setprecision(10);
int N;
cin >> N;
string X;
cin >> X;
ll y = 0;
for (char c : X) {
if (c == '1')
y++;
}
// X % (y+1)と、X%(y-1)を調べておく
int modyp1 = 0;
int modym1 = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '0')
continue;
modyp1 += pow2mod(N - i - 1, y + 1);
modyp1 %= (y + 1);
modym1 += pow2mod(N - i - 1, y - 1);
if (y - 1 == 0) {
modym1 = 0;
} else {
modym1 %= y - 1;
}
}
cerr << modyp1 << " " << modym1 << endl;
for (int i = 0; i < N; i++) {
int j = N - i - 1;
if (X[i] == '0') {
int tmp = modyp1; // X%(y+1)
tmp += pow2mod(j, y + 1); // y+1でわったあまりが得られた
tmp %= (y + 1);
// cerr << "i: " << i << " tmp: " << tmp << " y+1: " << y+1 << endl;
cout << solve(tmp, 1) << endl;
} else {
int tmp = modym1; // X%(y-1)
tmp -= pow2mod(j, y - 1); // y-1でわったあまりが得られた
tmp += y - 1;
if (y - 1 == 0) {
cout << 0 << endl;
} else {
tmp %= (y - 1);
// cerr << "i: " << i << " tmp: " << tmp << " y-1: " << y-1 << endl;
cout << solve(tmp, 1) << endl;
}
}
}
}
| replace | 77 | 78 | 77 | 82 | 0 | 0 0
|
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U> using pv = vector<pair<T, U>>;
template <typename T> using matrix = vector<vector<T>>;
template <typename T> using pque = priority_queue<T>;
template <typename T> using lpque = priority_queue<T, vector<T>, greater<T>>;
using ll = long long;
using intpair = pair<int, int>;
using llpair = pair<ll, ll>;
using ilpair = pair<int, ll>;
using lipair = pair<ll, int>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;
#define PI 3.141592653589793
#define INTINF ((1 << 30) - 1)
#define LLINF 1LL << 60
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME (1ll << 61) - 1
#define len length()
#define pushb push_back
#define fi first
#define se second
#define setpr fixed << setprecision(15)
#define all(name) name.begin(), name.end()
#define rall(name) name.rbegin(), name.rend()
#define gsort(vbeg, vend) sort(vbeg, vend, greater<>())
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline void init(T &v) {
for (auto &a : v)
cin >> a;
}
template <class T, class U> inline void init(vector<pair<T, U>> &v) {
for (auto &a : v)
cin >> a.first >> a.second;
}
template <class T, class N> inline void init(T &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a;
}
template <class T, class U, class N>
inline void init(vector<pair<T, U>> &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a.first >> a.second;
}
inline void out() { cout << endl; }
template <class T, class... U> inline void out(T a, U... alist) {
cout << a << " ";
out(forward<U>(alist)...);
}
template <class N> void resiz(N n) {
// empty
}
template <class N, class T, class... U> void resiz(N n, T &&hd, U &&...tl) {
hd.resize(n);
resiz(n, forward<U>(tl)...);
}
bool isout(int h, int w, int H, int W) {
return (h < 0 || h >= H || w < 0 || w >= W);
}
ll binpow(ll a, ll ex, ll p) {
ll result = 1;
while (ex > 0) {
if (ex & 1)
result = result * a % p;
ex >>= 1;
a = a * a % p;
}
return result;
}
int N;
int Xmn, Xpl, Xcnt;
intvec tms;
bitset<200000> bit;
void input() {
cin >> N >> bit;
tms.resize(N + 1, -1);
tms[0] = 0;
}
int precalc(int n) {
if (tms[n] >= 0)
return tms[n];
int nxt = n % bitset<32>(n).count();
return tms[n] = tms[nxt] + 1;
}
ll binmod(ll r) {
ll res = 0;
for (int i = N - 1; i >= 0; i--) {
res = (res * 2 + bit[i]) % r;
}
return res;
}
void solve() {
for (int i = 1; i <= N; i++)
precalc(i);
Xcnt = bit.count();
if (Xcnt == 0) {
for (int i = 0; i < N; i++)
cout << 1 << endl;
return;
}
Xmn = binmod(Xcnt - 1);
Xpl = binmod(Xcnt + 1);
for (int i = N - 1; i >= 0; i--) {
if (bit[i]) {
int cnt = Xcnt - 1;
int x = (cnt + Xmn - binpow(2, i, cnt)) % cnt;
cout << tms[x] + 1 << endl;
} else {
int cnt = Xcnt + 1;
int x = (Xpl + binpow(2, i, cnt)) % cnt;
cout << tms[x] + 1 << endl;
}
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
int t = 1;
while (t) {
input();
solve();
t--;
}
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U> using pv = vector<pair<T, U>>;
template <typename T> using matrix = vector<vector<T>>;
template <typename T> using pque = priority_queue<T>;
template <typename T> using lpque = priority_queue<T, vector<T>, greater<T>>;
using ll = long long;
using intpair = pair<int, int>;
using llpair = pair<ll, ll>;
using ilpair = pair<int, ll>;
using lipair = pair<ll, int>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;
#define PI 3.141592653589793
#define INTINF ((1 << 30) - 1)
#define LLINF 1LL << 60
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME (1ll << 61) - 1
#define len length()
#define pushb push_back
#define fi first
#define se second
#define setpr fixed << setprecision(15)
#define all(name) name.begin(), name.end()
#define rall(name) name.rbegin(), name.rend()
#define gsort(vbeg, vend) sort(vbeg, vend, greater<>())
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline void init(T &v) {
for (auto &a : v)
cin >> a;
}
template <class T, class U> inline void init(vector<pair<T, U>> &v) {
for (auto &a : v)
cin >> a.first >> a.second;
}
template <class T, class N> inline void init(T &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a;
}
template <class T, class U, class N>
inline void init(vector<pair<T, U>> &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a.first >> a.second;
}
inline void out() { cout << endl; }
template <class T, class... U> inline void out(T a, U... alist) {
cout << a << " ";
out(forward<U>(alist)...);
}
template <class N> void resiz(N n) {
// empty
}
template <class N, class T, class... U> void resiz(N n, T &&hd, U &&...tl) {
hd.resize(n);
resiz(n, forward<U>(tl)...);
}
bool isout(int h, int w, int H, int W) {
return (h < 0 || h >= H || w < 0 || w >= W);
}
ll binpow(ll a, ll ex, ll p) {
ll result = 1;
while (ex > 0) {
if (ex & 1)
result = result * a % p;
ex >>= 1;
a = a * a % p;
}
return result;
}
int N;
int Xmn, Xpl, Xcnt;
intvec tms;
bitset<200000> bit;
void input() {
cin >> N >> bit;
tms.resize(N + 1, -1);
tms[0] = 0;
}
int precalc(int n) {
if (tms[n] >= 0)
return tms[n];
int nxt = n % bitset<32>(n).count();
return tms[n] = tms[nxt] + 1;
}
ll binmod(ll r) {
ll res = 0;
for (int i = N - 1; i >= 0; i--) {
res = (res * 2 + bit[i]) % r;
}
return res;
}
void solve() {
for (int i = 1; i <= N; i++)
precalc(i);
Xcnt = bit.count();
if (Xcnt == 0) {
for (int i = 0; i < N; i++)
cout << 1 << endl;
return;
}
if (Xcnt == 1) {
if (bit[0]) {
for (int i = 0; i < N - 1; i++)
cout << 2 << endl;
cout << 0 << endl;
} else {
for (int i = N - 1; i > 0; i--) {
if (bit[i])
cout << 0 << endl;
else
cout << 1 << endl;
}
cout << 2 << endl;
}
return;
}
Xmn = binmod(Xcnt - 1);
Xpl = binmod(Xcnt + 1);
for (int i = N - 1; i >= 0; i--) {
if (bit[i]) {
int cnt = Xcnt - 1;
int x = (cnt + Xmn - binpow(2, i, cnt)) % cnt;
cout << tms[x] + 1 << endl;
} else {
int cnt = Xcnt + 1;
int x = (Xpl + binpow(2, i, cnt)) % cnt;
cout << tms[x] + 1 << endl;
}
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
int t = 1;
while (t) {
input();
solve();
t--;
}
}
| insert | 138 | 138 | 138 | 154 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
/* snippet: useful macros and functions */
#define ARC(i, a, b) for (Int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (Int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)
#define ALL(v) v.begin(), v.end()
template <class T> T rng(const T &a, const T &b) {
return a < b ? b - a : a - b;
}
template <class T>
void chmaxmin(T &a, const T &b, const T &(*f)(const T &, const T &)) {
a = f(a, b);
}
#define chmax(a, b) chmaxmin(a, b, std::max)
#define chmin(a, b) chmaxmin(a, b, std::min)
#if __has_include(<experimental/iterator>)
#include <experimental/iterator>
auto debug = std::experimental::make_ostream_joiner(std::cout, " ");
#endif
/* snippet: inf struct */
#if __cplusplus >= 201703
struct {
template <class T> constexpr operator T() {
return std::numeric_limits<T>::max();
}
constexpr auto operator-();
} inf;
struct {
template <class T> constexpr operator T() {
return std::numeric_limits<T>::lowest();
}
constexpr auto operator-();
} negative_inf;
constexpr auto decltype(inf)::operator-() { return negative_inf; }
constexpr auto decltype(negative_inf)::operator-() { return inf; }
#endif
/* snippet: modint library */
#if __cpp_concepts
template <std::signed_integral T, T MOD>
#else
template <class T, T MOD>
#endif
class Modint {
T val;
public:
Modint(T val = 0) : val(val % MOD + (val >= 0 ? 0 : MOD)) {}
operator T() { return val; }
Modint &operator+=(const Modint &a) {
val += a.val;
if (val >= MOD)
val -= MOD;
return *this;
}
Modint &operator-=(const Modint &a) {
val -= a.val;
if (val < 0)
val += MOD;
return *this;
}
Modint &operator*=(const Modint &a) {
val = val * a.val % MOD;
return *this;
}
void inverse() {
T x[2] = {MOD, val}, a[2] = {0, 1};
int i;
for (i = 0; x[!i]; i ^= 1) {
a[i] -= x[i] / x[!i] * a[!i];
x[i] = x[i] % x[!i];
}
if (!i)
a[i] += MOD;
val = a[i];
}
Modint &operator/=(Modint a) {
a.inverse();
return *this *= a;
}
friend Modint modpow(Modint a, int n) {
Modint ret(1);
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
friend Modint operator+(Modint a, const Modint &b) { return a += b; }
friend Modint operator-(Modint a, const Modint &b) { return a -= b; }
friend Modint operator*(Modint a, const Modint &b) { return a *= b; }
friend Modint operator/(Modint a, const Modint &b) { return a /= b; }
friend std::ostream &operator<<(std::ostream &os, const Modint &a) {
return os << a.val;
}
};
/* push n numbers and get i-th min */
/* to get min and 2nd min, declare K_th_min<T, 2> and use get<0>(), get<1>() */
#if __cplusplus >= 201703
template <class T, std::size_t K> class K_th_min {
friend class K_th_min<T, K + 1>;
T val;
K_th_min<T, K - 1> lower;
void push_(T &x) {
lower.push_(x);
if (val > x)
std::swap(val, x);
}
public:
K_th_min() : val(std::numeric_limits<T>::max()) {}
void push(T x) { push_(x); }
template <std::size_t I> T get() {
if constexpr (I == K - 1)
return val;
else
return lower.template get<I>();
}
};
template <class T> class K_th_min<T, 0> {
friend class K_th_min<T, 1>;
void push_(T &) {}
public:
K_th_min() {}
void push(T) {}
};
#endif
/* snippet: using declarations */
using Int = long long;
using std::cin;
using std::cout;
using std::endl;
using Mint = Modint<long long, 1000000007>;
int main() {
Int n;
cin >> n;
std::vector<Int> a(n);
a[0] = 0;
for (Int i = 1; i < n; ++i) {
a[i] = a[i % __builtin_popcount(i)] + 1;
}
std::string s;
cin >> s;
Int popcount = 0;
for (Int i = 0; i < n; ++i) {
popcount += s[i] == '1';
}
Int pos = 0, neg = 0;
for (Int i = 0; i < n; ++i) {
pos = (pos * 2 + (s[i] - '0')) % (popcount + 1);
neg = (neg * 2 + (s[i] - '0')) % (popcount - 1);
}
/*
cout << popcount << endl;
cout << pos << endl;
cout << neg << endl;
*/
std::vector<Int> ans(n);
Int add_pos = 1, add_neg = popcount - 2;
for (Int i = n - 1; i >= 0; --i) {
if (s[i] == '0') {
ans[i] = a[(pos + add_pos) % (popcount + 1)] + 1;
} else {
ans[i] = a[(neg + add_neg) % (popcount - 1)] + 1;
}
add_pos = add_pos * 2 % (popcount + 1);
add_neg = add_neg * 2 % (popcount - 1);
}
RC(i, n) { cout << ans[i] << endl; }
}
| #include <bits/stdc++.h>
/* snippet: useful macros and functions */
#define ARC(i, a, b) for (Int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (Int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)
#define ALL(v) v.begin(), v.end()
template <class T> T rng(const T &a, const T &b) {
return a < b ? b - a : a - b;
}
template <class T>
void chmaxmin(T &a, const T &b, const T &(*f)(const T &, const T &)) {
a = f(a, b);
}
#define chmax(a, b) chmaxmin(a, b, std::max)
#define chmin(a, b) chmaxmin(a, b, std::min)
#if __has_include(<experimental/iterator>)
#include <experimental/iterator>
auto debug = std::experimental::make_ostream_joiner(std::cout, " ");
#endif
/* snippet: inf struct */
#if __cplusplus >= 201703
struct {
template <class T> constexpr operator T() {
return std::numeric_limits<T>::max();
}
constexpr auto operator-();
} inf;
struct {
template <class T> constexpr operator T() {
return std::numeric_limits<T>::lowest();
}
constexpr auto operator-();
} negative_inf;
constexpr auto decltype(inf)::operator-() { return negative_inf; }
constexpr auto decltype(negative_inf)::operator-() { return inf; }
#endif
/* snippet: modint library */
#if __cpp_concepts
template <std::signed_integral T, T MOD>
#else
template <class T, T MOD>
#endif
class Modint {
T val;
public:
Modint(T val = 0) : val(val % MOD + (val >= 0 ? 0 : MOD)) {}
operator T() { return val; }
Modint &operator+=(const Modint &a) {
val += a.val;
if (val >= MOD)
val -= MOD;
return *this;
}
Modint &operator-=(const Modint &a) {
val -= a.val;
if (val < 0)
val += MOD;
return *this;
}
Modint &operator*=(const Modint &a) {
val = val * a.val % MOD;
return *this;
}
void inverse() {
T x[2] = {MOD, val}, a[2] = {0, 1};
int i;
for (i = 0; x[!i]; i ^= 1) {
a[i] -= x[i] / x[!i] * a[!i];
x[i] = x[i] % x[!i];
}
if (!i)
a[i] += MOD;
val = a[i];
}
Modint &operator/=(Modint a) {
a.inverse();
return *this *= a;
}
friend Modint modpow(Modint a, int n) {
Modint ret(1);
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
friend Modint operator+(Modint a, const Modint &b) { return a += b; }
friend Modint operator-(Modint a, const Modint &b) { return a -= b; }
friend Modint operator*(Modint a, const Modint &b) { return a *= b; }
friend Modint operator/(Modint a, const Modint &b) { return a /= b; }
friend std::ostream &operator<<(std::ostream &os, const Modint &a) {
return os << a.val;
}
};
/* push n numbers and get i-th min */
/* to get min and 2nd min, declare K_th_min<T, 2> and use get<0>(), get<1>() */
#if __cplusplus >= 201703
template <class T, std::size_t K> class K_th_min {
friend class K_th_min<T, K + 1>;
T val;
K_th_min<T, K - 1> lower;
void push_(T &x) {
lower.push_(x);
if (val > x)
std::swap(val, x);
}
public:
K_th_min() : val(std::numeric_limits<T>::max()) {}
void push(T x) { push_(x); }
template <std::size_t I> T get() {
if constexpr (I == K - 1)
return val;
else
return lower.template get<I>();
}
};
template <class T> class K_th_min<T, 0> {
friend class K_th_min<T, 1>;
void push_(T &) {}
public:
K_th_min() {}
void push(T) {}
};
#endif
/* snippet: using declarations */
using Int = long long;
using std::cin;
using std::cout;
using std::endl;
using Mint = Modint<long long, 1000000007>;
int main() {
Int n;
cin >> n;
std::vector<Int> a(n);
a[0] = 0;
for (Int i = 1; i < n; ++i) {
a[i] = a[i % __builtin_popcount(i)] + 1;
}
std::string s;
cin >> s;
Int popcount = 0;
for (Int i = 0; i < n; ++i) {
popcount += s[i] == '1';
}
if (popcount == 0) {
for (Int i = 0; i < n; ++i) {
cout << 1 << endl;
}
return 0;
} else if (popcount == 1) {
for (Int i = 0; i < n; ++i) {
if (s[i] == '1') {
cout << 0 << endl;
} else if (s[n - 1] == '0' && i != n - 1) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
}
return 0;
}
Int pos = 0, neg = 0;
for (Int i = 0; i < n; ++i) {
pos = (pos * 2 + (s[i] - '0')) % (popcount + 1);
neg = (neg * 2 + (s[i] - '0')) % (popcount - 1);
}
/*
cout << popcount << endl;
cout << pos << endl;
cout << neg << endl;
*/
std::vector<Int> ans(n);
Int add_pos = 1, add_neg = popcount - 2;
for (Int i = n - 1; i >= 0; --i) {
if (s[i] == '0') {
ans[i] = a[(pos + add_pos) % (popcount + 1)] + 1;
} else {
ans[i] = a[(neg + add_neg) % (popcount - 1)] + 1;
}
add_pos = add_pos * 2 % (popcount + 1);
add_neg = add_neg * 2 % (popcount - 1);
}
RC(i, n) { cout << ans[i] << endl; }
}
| insert | 157 | 157 | 157 | 176 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define testcase() \
int t; \
cin >> t; \
while (t--)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i <= n; i++)
#define vll vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
// ll M = 1e9 + 7;
double pi = acos(-1.0);
ll powerm(ll x, ll y, ll M) {
ll res = 1;
while (y) {
if (y & 1)
res = (res * x) % M;
y = y >> 1;
x = (x * x) % M;
}
return res % M;
}
ll power(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
ll gcd(ll a, ll b) {
if (b > a)
return gcd(b, a);
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
// ifstream cin("input.txt");
// ofstream cout("output.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
string s;
cin >> s;
ll cc = 0;
forn(i, n) if (s[i] == '1') cc++;
ll p1 = cc + 1, p2 = cc - 1;
if (p2 < 0)
p2 = p1;
ll curr1 = 0, curr2 = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0')
continue;
ll h = n - 1 - i;
curr1 += powerm((ll)2, h, p1);
curr1 %= p1;
curr2 += powerm((ll)2, h, p2);
curr2 %= p2;
// cout<<i<<':'<<curr1<<':'<<curr2<<endl;
}
ll pre[n + 10];
pre[0] = 0;
forn1(i, n) {
ll f = __builtin_popcountll(i);
pre[i] = pre[i % f] + 1;
}
// forn1(i,n) cout<<pre[i]<<' '; cout<<endl;
// cout<<curr1<<":"<<curr2<<endl;
forn(i, n) {
if (s[i] == '0') {
ll r = n - 1 - i;
ll d = curr1 + powerm((ll)2, r, p1);
d %= p1;
cout << 1 + pre[d] << '\n';
} else {
if (p2 == 0) {
cout << 0 << '\n';
continue;
}
ll r = n - 1 - i;
ll d = curr2 - powerm((ll)2, r, p2) + p2;
d %= p2;
cout << 1 + pre[d] << '\n';
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define testcase() \
int t; \
cin >> t; \
while (t--)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i <= n; i++)
#define vll vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
// ll M = 1e9 + 7;
double pi = acos(-1.0);
ll powerm(ll x, ll y, ll M) {
ll res = 1;
while (y) {
if (y & 1)
res = (res * x) % M;
y = y >> 1;
x = (x * x) % M;
}
return res % M;
}
ll power(ll x, ll y) {
ll res = 1;
while (y) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
ll gcd(ll a, ll b) {
if (b > a)
return gcd(b, a);
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
// ifstream cin("input.txt");
// ofstream cout("output.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
string s;
cin >> s;
ll cc = 0;
forn(i, n) if (s[i] == '1') cc++;
ll p1 = cc + 1, p2 = cc - 1;
if (p2 < 0)
p2 = p1;
ll curr1 = 0, curr2 = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0')
continue;
ll h = n - 1 - i;
curr1 += powerm((ll)2, h, p1);
curr1 %= p1;
if (p2 == 0)
continue;
curr2 += powerm((ll)2, h, p2);
curr2 %= p2;
// cout<<i<<':'<<curr1<<':'<<curr2<<endl;
}
ll pre[n + 10];
pre[0] = 0;
forn1(i, n) {
ll f = __builtin_popcountll(i);
pre[i] = pre[i % f] + 1;
}
// forn1(i,n) cout<<pre[i]<<' '; cout<<endl;
// cout<<curr1<<":"<<curr2<<endl;
forn(i, n) {
if (s[i] == '0') {
ll r = n - 1 - i;
ll d = curr1 + powerm((ll)2, r, p1);
d %= p1;
cout << 1 + pre[d] << '\n';
} else {
if (p2 == 0) {
cout << 0 << '\n';
continue;
}
ll r = n - 1 - i;
ll d = curr2 - powerm((ll)2, r, p2) + p2;
d %= p2;
cout << 1 + pre[d] << '\n';
}
}
return 0;
}
| insert | 73 | 73 | 73 | 75 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
ll A = 0, B = 0, N, x;
ll Q(ll &i, string &S, vector<ll> &F, vector<ll> &G) {
if (S.at(i) == '0')
return (A + F.at(i)) % (x + 1);
return ((B - G.at(i)) % (x - 1) + x - 1) % (x - 1);
}
ll f(ll m) {
if (m == 0)
return 0;
ll i = 0, n = m;
while (n > 0) {
if (n % 2 == 1)
i++;
n /= 2;
}
return 1 + f(m % i);
}
signed main() {
ll i, s = 1;
string S, T;
cin >> N >> S;
T = S;
sort(ALL(T));
x = upper_bound(ALL(T), '0') - T.begin();
x = N - x;
vector<ll> F(N);
REP(i, N) {
F.at(N - 1 - i) = s;
if (S.at(N - 1 - i) == '1')
A += s;
s *= 2;
s %= x + 1;
A %= x + 1;
}
s = 1;
vector<ll> G(N);
if (x <= 1) {
REP(i, N) G.at(i) = -1;
} else
REP(i, N) {
G.at(N - 1 - i) = s;
if (S.at(N - 1 - i) == '1')
B += s;
s *= 2;
s %= x - 1;
B %= x - 1;
}
REP(i, N) {
if (x == 1 && S.at(i) == '0')
cout << 0 << endl;
else {
ll RES = Q(i, S, F, G);
cout << 1 + f(RES) << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
ll A = 0, B = 0, N, x;
ll Q(ll &i, string &S, vector<ll> &F, vector<ll> &G) {
if (S.at(i) == '0')
return (A + F.at(i)) % (x + 1);
return ((B - G.at(i)) % (x - 1) + x - 1) % (x - 1);
}
ll f(ll m) {
if (m == 0)
return 0;
ll i = 0, n = m;
while (n > 0) {
if (n % 2 == 1)
i++;
n /= 2;
}
return 1 + f(m % i);
}
signed main() {
ll i, s = 1;
string S, T;
cin >> N >> S;
T = S;
sort(ALL(T));
x = upper_bound(ALL(T), '0') - T.begin();
x = N - x;
vector<ll> F(N);
REP(i, N) {
F.at(N - 1 - i) = s;
if (S.at(N - 1 - i) == '1')
A += s;
s *= 2;
s %= x + 1;
A %= x + 1;
}
s = 1;
vector<ll> G(N);
if (x <= 1) {
REP(i, N) G.at(i) = -1;
} else
REP(i, N) {
G.at(N - 1 - i) = s;
if (S.at(N - 1 - i) == '1')
B += s;
s *= 2;
s %= x - 1;
B %= x - 1;
}
REP(i, N) {
if (x == 1 && S.at(i) == '1')
cout << 0 << endl;
else {
ll RES = Q(i, S, F, G);
cout << 1 + f(RES) << endl;
}
}
return 0;
} | replace | 57 | 58 | 57 | 58 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &p : a) {
out << (first ? "" : ", ");
out << p.first << ":" << p.second;
first = 0;
}
out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
vector<int> ans;
int memo[220000];
int rec(int x) {
if (memo[x] != -1)
return memo[x];
if (x == 0)
return 0;
int ret = 0;
int popcnt = __builtin_popcountll(x);
int newpp = __builtin_popcountll(x % popcnt);
if (popcnt == 0)
return 1;
return memo[x] = rec(x % popcnt) + 1;
}
signed main() {
int n;
cin >> n;
string s;
cin >> s;
rep(i, n + 1) memo[i] = -1;
int popcnt = 0;
int valp = 0, valm = 0;
int ketap = 1;
int ketam = 1;
rep(i, n) popcnt += (s[i] - '0');
int modp = popcnt + 1;
int modm = popcnt - 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
valp += ketap;
valp %= modp;
valm += ketam;
valm %= modm;
}
ketap *= 2;
ketam *= 2;
ketap %= modp;
ketam %= modm;
}
ketap = 1;
ketam = 1;
// trace(valp,valm);
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0') {
// plus
int now = (valp + ketap) % modp;
ans.push_back(rec(now));
} else {
// minus
int now = (valm - ketam) % modm;
now = (now % modm + modm) % modm;
ans.push_back(rec(now));
}
ketap *= 2;
ketap %= modp;
ketam *= 2;
ketam %= modm;
}
for (int i = (int)ans.size() - 1; i >= 0; i--)
cout << ans[i] + 1 << endl;
return 0;
} | #include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &p : a) {
out << (first ? "" : ", ");
out << p.first << ":" << p.second;
first = 0;
}
out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
vector<int> ans;
int memo[220000];
int rec(int x) {
if (memo[x] != -1)
return memo[x];
if (x == 0)
return 0;
int ret = 0;
int popcnt = __builtin_popcountll(x);
int newpp = __builtin_popcountll(x % popcnt);
if (popcnt == 0)
return 1;
return memo[x] = rec(x % popcnt) + 1;
}
signed main() {
int n;
cin >> n;
string s;
cin >> s;
rep(i, n + 1) memo[i] = -1;
int popcnt = 0;
int valp = 0, valm = 0;
int ketap = 1;
int ketam = 1;
rep(i, n) popcnt += (s[i] - '0');
if (popcnt == 0) {
rep(i, n) cout << 1 << endl;
return 0;
}
if (popcnt == 1) {
rep(i, n) {
if (s[i] == '1')
cout << 0 << endl;
else {
// pp = 2;
if (s[n - 1] == '1' || i == n - 1) {
cout << 2 << endl;
} else {
cout << 1 << endl;
}
}
}
return 0;
}
int modp = popcnt + 1;
int modm = popcnt - 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
valp += ketap;
valp %= modp;
valm += ketam;
valm %= modm;
}
ketap *= 2;
ketam *= 2;
ketap %= modp;
ketam %= modm;
}
ketap = 1;
ketam = 1;
// trace(valp,valm);
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0') {
// plus
int now = (valp + ketap) % modp;
ans.push_back(rec(now));
} else {
// minus
int now = (valm - ketam) % modm;
now = (now % modm + modm) % modm;
ans.push_back(rec(now));
}
ketap *= 2;
ketap %= modp;
ketam *= 2;
ketam %= modm;
}
for (int i = (int)ans.size() - 1; i >= 0; i--)
cout << ans[i] + 1 << endl;
return 0;
} | insert | 102 | 102 | 102 | 121 | 0 | |
p02609 | C++ | Runtime Error | // I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >> Ikimono Gakari >> Garnidelia >>
// Kalafina >> Eir Aoi. .. dude? problems that involves any kind of persistent
// data structures are the best of the best, are not them?
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define ff first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
#define FER(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define IFR(i, a, b) for (ll i = ll(a); i >= ll(b); --i)
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define N 31000000
// #define M 19
// #define mod 11092019
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x) * (x)
#define INF 2000000000000
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template <typename t> void fff(const char *x, t &&val1) {
cout << x << " : " << val1 << "\n";
}
template <typename t1, typename... t2>
void fff(const char *x, t1 &&val1, t2 &&...val2) {
const char *xd = strchr(x + 1, ',');
cout.write(x, xd - x) << " : " << val1 << " | ";
fff(xd + 1, val2...);
}
inline ll mul(ll a, ll b, ll mod) { return (long long)a * b % mod; }
inline ll add(ll a, ll b, ll mod) { return a + b < mod ? a + b : a + b - mod; }
inline void Mul(ll &a, ll b, ll mod) { a = (long long)a * b % mod; }
inline ll bp(ll a, ll p, ll mod) {
ll ret;
for (ret = 1; p; p >>= 1, Mul(a, a, mod))
(p & 1) && (Mul(ret, a, mod), 1);
return ret;
}
static inline ll get_num() {
ll num = 0;
char c = getchar_unlocked();
while (!((c >= '0') & (c <= '9')))
c = getchar_unlocked();
while (c >= '0' && c <= '9') {
num = (num << 1) + (num << 3) + c - '0';
c = getchar_unlocked();
}
return num;
}
static inline void read(ll &x) { x = get_num(); }
ll cnt[1 << 19];
inline ll solve(ll x) {
if (x == 0)
return 0;
ll ret = __builtin_popcountll(x);
return 1 + solve(x % ret);
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
for (auto xd : s)
if (xd == '1')
cnt++;
ll a = cnt - 1, b = cnt + 1;
ll valor1 = 0, valor2 = 0;
if (a > 0) {
ll tot = 1;
IFR(i, n - 1, 0) {
valor1 += (tot * (s[i] - '0')) % a;
if (valor1 >= a)
valor1 -= a;
tot <<= 1;
tot %= a;
}
}
if (b > 0) {
ll tot = 1;
IFR(i, n - 1, 0) {
valor2 += (tot * (s[i] - '0')) % b;
if (valor2 >= b)
valor2 -= b;
tot <<= 1;
tot %= b;
}
}
FER(i, 0, n) {
if (s[i] == '1') {
// resto es a;
ll valor = (valor1 - bp(2, n - i - 1, a) + a) % a;
if (valor == 0)
cout << "1\n";
else
cout << solve(valor) + 1 << "\n";
} else {
// resto es b;
ll valor = (valor2 + bp(2, n - i - 1, b) + b) % b;
if (valor == 0)
cout << "1\n";
else
cout << solve(valor) + 1 << "\n";
}
}
return 0;
} | // I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >> Ikimono Gakari >> Garnidelia >>
// Kalafina >> Eir Aoi. .. dude? problems that involves any kind of persistent
// data structures are the best of the best, are not them?
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define ff first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
#define FER(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define IFR(i, a, b) for (ll i = ll(a); i >= ll(b); --i)
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define N 31000000
// #define M 19
// #define mod 11092019
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x) * (x)
#define INF 2000000000000
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template <typename t> void fff(const char *x, t &&val1) {
cout << x << " : " << val1 << "\n";
}
template <typename t1, typename... t2>
void fff(const char *x, t1 &&val1, t2 &&...val2) {
const char *xd = strchr(x + 1, ',');
cout.write(x, xd - x) << " : " << val1 << " | ";
fff(xd + 1, val2...);
}
inline ll mul(ll a, ll b, ll mod) { return (long long)a * b % mod; }
inline ll add(ll a, ll b, ll mod) { return a + b < mod ? a + b : a + b - mod; }
inline void Mul(ll &a, ll b, ll mod) { a = (long long)a * b % mod; }
inline ll bp(ll a, ll p, ll mod) {
ll ret;
for (ret = 1; p; p >>= 1, Mul(a, a, mod))
(p & 1) && (Mul(ret, a, mod), 1);
return ret;
}
static inline ll get_num() {
ll num = 0;
char c = getchar_unlocked();
while (!((c >= '0') & (c <= '9')))
c = getchar_unlocked();
while (c >= '0' && c <= '9') {
num = (num << 1) + (num << 3) + c - '0';
c = getchar_unlocked();
}
return num;
}
static inline void read(ll &x) { x = get_num(); }
ll cnt[1 << 19];
inline ll solve(ll x) {
if (x == 0)
return 0;
ll ret = __builtin_popcountll(x);
return 1 + solve(x % ret);
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
for (auto xd : s)
if (xd == '1')
cnt++;
ll a = cnt - 1, b = cnt + 1;
ll valor1 = 0, valor2 = 0;
if (a > 0) {
ll tot = 1;
IFR(i, n - 1, 0) {
valor1 += (tot * (s[i] - '0')) % a;
if (valor1 >= a)
valor1 -= a;
tot <<= 1;
tot %= a;
}
}
if (b > 0) {
ll tot = 1;
IFR(i, n - 1, 0) {
valor2 += (tot * (s[i] - '0')) % b;
if (valor2 >= b)
valor2 -= b;
tot <<= 1;
tot %= b;
}
}
FER(i, 0, n) {
if (s[i] == '1') {
// resto es a;
if (!a) {
cout << 0 << "\n";
continue;
}
ll valor;
if (a > 0)
valor = (valor1 - bp(2, n - i - 1, a) + a) % a;
if (valor == 0)
cout << "1\n";
else
cout << solve(valor) + 1 << "\n";
} else {
// resto es b;
ll valor = (valor2 + bp(2, n - i - 1, b) + b) % b;
if (valor == 0)
cout << "1\n";
else
cout << solve(valor) + 1 << "\n";
}
}
return 0;
} | replace | 127 | 128 | 127 | 134 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string X;
cin >> N >> X;
int PopCount(0);
for (int i = 0; i < N; ++i) {
if (X[i] == '1')
++PopCount;
}
vector<int> pMod(N, 1), nMod(N, 1);
for (int i = N - 2; i >= 0; --i) {
pMod[i] = (pMod[i + 1] << 1) % (PopCount + 1);
nMod[i] = (nMod[i + 1] << 1) % (PopCount - 1);
}
// Xp := X % (PopCount + 1)
// Xn := X % (PopCount - 1)
int Xp(0), Xn(0);
for (int i = 0; i < N; ++i) {
if (X[i] == '1') {
(Xp += pMod[i]) %= (PopCount + 1);
if (PopCount - 1 > 0)
(Xn += nMod[i]) %= (PopCount - 1);
}
}
for (int i = 0; i < N; ++i) {
int ans = 1;
if (X[i] == '1') {
if (PopCount - 1 <= 0) {
cout << 0 << endl;
continue;
}
int res = (Xn - nMod[i] + (PopCount - 1)) % (PopCount - 1);
// シミュレーション
while (res != 0) {
res = res % __builtin_popcount((unsigned int)res);
++ans;
}
} else {
int res = (Xp + pMod[i] + (PopCount + 1)) % (PopCount + 1);
while (res != 0) {
res = res % __builtin_popcount((unsigned int)res);
++ans;
}
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string X;
cin >> N >> X;
int PopCount(0);
for (int i = 0; i < N; ++i) {
if (X[i] == '1')
++PopCount;
}
vector<int> pMod(N, 1), nMod(N, 1);
for (int i = N - 2; i >= 0; --i) {
pMod[i] = (pMod[i + 1] << 1) % (PopCount + 1);
if (PopCount - 1 > 0)
nMod[i] = (nMod[i + 1] << 1) % (PopCount - 1);
}
// Xp := X % (PopCount + 1)
// Xn := X % (PopCount - 1)
int Xp(0), Xn(0);
for (int i = 0; i < N; ++i) {
if (X[i] == '1') {
(Xp += pMod[i]) %= (PopCount + 1);
if (PopCount - 1 > 0)
(Xn += nMod[i]) %= (PopCount - 1);
}
}
for (int i = 0; i < N; ++i) {
int ans = 1;
if (X[i] == '1') {
if (PopCount - 1 <= 0) {
cout << 0 << endl;
continue;
}
int res = (Xn - nMod[i] + (PopCount - 1)) % (PopCount - 1);
// シミュレーション
while (res != 0) {
res = res % __builtin_popcount((unsigned int)res);
++ans;
}
} else {
int res = (Xp + pMod[i] + (PopCount + 1)) % (PopCount + 1);
while (res != 0) {
res = res % __builtin_popcount((unsigned int)res);
++ans;
}
}
cout << ans << endl;
}
return 0;
} | replace | 17 | 18 | 17 | 19 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int mod = 1e9 + 7;
const int INF = 1001001001;
int f(int x) {
if (x == 0) {
return 0;
}
int p = __builtin_popcount(x);
return f(x % p) + 1;
}
int main() {
int N;
string S;
cin >> N >> S;
int pop = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1') {
pop++;
}
}
vector<int> ans(N);
for (int r = 0; r < 2; r++) {
int basep = pop;
if (r == 0) {
basep++;
} else {
basep--;
}
int base = 0;
int s = 1;
for (int i = N - 1; i >= 0; i--) {
if (S[i] == '1') {
(base += s) %= basep;
}
(s *= 2) %= basep;
}
s = 1;
int start = 0;
for (int i = N - 1; i >= 0; i--) {
if (S[i] - '0' == r) {
if (r == 0) {
(start = base + s) %= basep;
} else if (r == 1) {
(start = base - s + basep) %= basep;
}
ans[i] = f(start) + 1;
}
(s *= 2) %= basep;
}
}
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int mod = 1e9 + 7;
const int INF = 1001001001;
int f(int x) {
if (x == 0) {
return 0;
}
int p = __builtin_popcount(x);
return f(x % p) + 1;
}
int main() {
int N;
string S;
cin >> N >> S;
int pop = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1') {
pop++;
}
}
vector<int> ans(N);
for (int r = 0; r < 2; r++) {
int basep = pop;
if (r == 0) {
basep++;
} else {
basep--;
}
if (basep <= 0) {
continue;
}
int base = 0;
int s = 1;
for (int i = N - 1; i >= 0; i--) {
if (S[i] == '1') {
(base += s) %= basep;
}
(s *= 2) %= basep;
}
s = 1;
int start = 0;
for (int i = N - 1; i >= 0; i--) {
if (S[i] - '0' == r) {
if (r == 0) {
(start = base + s) %= basep;
} else if (r == 1) {
(start = base - s + basep) %= basep;
}
ans[i] = f(start) + 1;
}
(s *= 2) %= basep;
}
}
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
return 0;
} | insert | 51 | 51 | 51 | 55 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
long long mod_pow(long long x, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::setprecision(20) << std::fixed;
int N;
string X;
cin >> N >> X;
int cnt = 0;
for (int i = 0; i < N; i++) {
cnt += (X[i] == '1');
}
long long A = 0, B = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '1') {
if (cnt - 1 >= 0) {
A = (A + mod_pow(2, N - i - 1, cnt - 1)) % (cnt - 1);
}
B = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
}
}
for (int i = 0; i < N; i++) {
long long ans = 0, t = 0;
if (X[i] == '1') {
if (cnt != 1) {
t = ((A - mod_pow(2, N - i - 1, cnt - 1)) + (cnt - 1)) % (cnt - 1);
ans++;
}
} else {
t = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
ans++;
}
while (t != 0LL) {
t %= __builtin_popcountll(t);
ans++;
}
cout << ans << "\n";
}
return 0;
} | #include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
long long mod_pow(long long x, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::setprecision(20) << std::fixed;
int N;
string X;
cin >> N >> X;
int cnt = 0;
for (int i = 0; i < N; i++) {
cnt += (X[i] == '1');
}
long long A = 0, B = 0;
for (int i = 0; i < N; i++) {
if (X[i] == '1') {
if (cnt - 1 > 0) {
A = (A + mod_pow(2, N - i - 1, cnt - 1)) % (cnt - 1);
}
B = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
}
}
for (int i = 0; i < N; i++) {
long long ans = 0, t = 0;
if (X[i] == '1') {
if (cnt != 1) {
t = ((A - mod_pow(2, N - i - 1, cnt - 1)) + (cnt - 1)) % (cnt - 1);
ans++;
}
} else {
t = (B + mod_pow(2, N - i - 1, cnt + 1)) % (cnt + 1);
ans++;
}
while (t != 0LL) {
t %= __builtin_popcountll(t);
ans++;
}
cout << ans << "\n";
}
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02609 | C++ | Runtime Error | /*
Created by Rahul Goel.
*/
/*******************************************************************************/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/*******************************************************************************/
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;
/*******************************************************************************/
/*
.-.. . -. -.- .- / .. ... / .-.. --- ...- . -.-.--
*/
/* For PBDS */
/*Two new methods X.order_of_key() and X.find_by_order()*/
/*
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/
/*******************************************************************************/
ll mod_sum() { return 0LL; }
template <typename T, typename... Args> T mod_sum(T a, Args... args) {
return ((a + mod_sum(args...)) % mod + mod) % mod;
}
ll mod_prod() { return 1LL; }
template <typename T, typename... Args> T mod_prod(T a, Args... args) {
return (a * mod_prod(args...)) % mod;
}
/*******************************************************************************/
using pii = pair<ll, ll>;
using vi = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vpii = vector<pii>;
using vvpii = vector<vector<pii>>;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define uniq(c) (c).resize(distance((c).begin(), unique(all(c))))
#define min_pq(t) priority_queue<t, vector<t>, greater<t>>
#define max_pq(t) priority_queue<t>
#define pb push_back
#define ff first
#define ss second
/*******************************************************************************/
/*
Code begins after this.
*/
ll popcount(ll n) {
ll cnt = 0;
while (n)
cnt += (n & 1), n >>= 1;
return cnt;
}
ll f(ll n) {
if (n == 0)
return 0;
return 1 + f(n % popcount(n));
}
ll power(ll x, ll y, ll m) {
ll ret = 1;
while (y) {
if (y & 1)
ret = (ret * x) % m;
y >>= 1;
x = (x * x) % m;
}
return ret;
}
ll solve() {
ll n;
cin >> n;
string str;
cin >> str;
ll cnt1 = 0;
for (char &x : str)
cnt1 += (x == '1');
ll val0 = 0, val1 = 0, val2 = 0;
for (ll i = 0; i < n; i++) {
if (str[i] == '1') {
val0 = (val0 + power(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1);
val1 = (val1 + power(2, n - i - 1, cnt1 - 0)) % (cnt1 - 0);
val2 = (val2 + power(2, n - i - 1, cnt1 + 1)) % (cnt1 + 1);
}
}
vi ans;
for (ll i = 0; i < n; i++) {
if (str[i] == '1') {
str[i] = '0';
if (cnt1 - 1 == 0) {
ans.pb(0);
continue;
}
// cnt1 decreases by 1
ll val =
((val0 - power(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1) + cnt1 - 1) %
(cnt1 - 1);
ans.pb(1 + f(val));
str[i] = '1';
} else {
str[i] = '1';
if (cnt1 + 1 == 0) {
ans.pb(0);
continue;
}
// cnt1 increases by 1
ll val = (val2 + power(2, n - i - 1, cnt1 + 1) + cnt1 + 1) % (cnt1 + 1);
ans.pb(1 + f(val));
str[i] = '0';
}
}
for (ll x : ans)
cout << x << endl;
return 0;
}
signed main() {
fastio;
ll t = 1;
while (t--)
solve();
return 0;
}
/*******************************************************************************/
// Remeber - In a custom comparator cmp(a, b) a==b must always return to false.
| /*
Created by Rahul Goel.
*/
/*******************************************************************************/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
/*******************************************************************************/
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;
/*******************************************************************************/
/*
.-.. . -. -.- .- / .. ... / .-.. --- ...- . -.-.--
*/
/* For PBDS */
/*Two new methods X.order_of_key() and X.find_by_order()*/
/*
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
*/
/*******************************************************************************/
ll mod_sum() { return 0LL; }
template <typename T, typename... Args> T mod_sum(T a, Args... args) {
return ((a + mod_sum(args...)) % mod + mod) % mod;
}
ll mod_prod() { return 1LL; }
template <typename T, typename... Args> T mod_prod(T a, Args... args) {
return (a * mod_prod(args...)) % mod;
}
/*******************************************************************************/
using pii = pair<ll, ll>;
using vi = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vpii = vector<pii>;
using vvpii = vector<vector<pii>>;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define uniq(c) (c).resize(distance((c).begin(), unique(all(c))))
#define min_pq(t) priority_queue<t, vector<t>, greater<t>>
#define max_pq(t) priority_queue<t>
#define pb push_back
#define ff first
#define ss second
/*******************************************************************************/
/*
Code begins after this.
*/
ll popcount(ll n) {
ll cnt = 0;
while (n)
cnt += (n & 1), n >>= 1;
return cnt;
}
ll f(ll n) {
if (n == 0)
return 0;
return 1 + f(n % popcount(n));
}
ll power(ll x, ll y, ll m) {
ll ret = 1;
while (y) {
if (y & 1)
ret = (ret * x) % m;
y >>= 1;
x = (x * x) % m;
}
return ret;
}
ll solve() {
ll n;
cin >> n;
string str;
cin >> str;
ll cnt1 = 0;
for (char &x : str)
cnt1 += (x == '1');
ll val0 = 0, val1 = 0, val2 = 0;
for (ll i = 0; i < n; i++) {
if (str[i] == '1') {
if (cnt1 - 1 > 0)
val0 = (val0 + power(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1);
val1 = (val1 + power(2, n - i - 1, cnt1 - 0)) % (cnt1 - 0);
val2 = (val2 + power(2, n - i - 1, cnt1 + 1)) % (cnt1 + 1);
}
}
vi ans;
for (ll i = 0; i < n; i++) {
if (str[i] == '1') {
str[i] = '0';
if (cnt1 - 1 == 0) {
ans.pb(0);
continue;
}
// cnt1 decreases by 1
ll val =
((val0 - power(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1) + cnt1 - 1) %
(cnt1 - 1);
ans.pb(1 + f(val));
str[i] = '1';
} else {
str[i] = '1';
if (cnt1 + 1 == 0) {
ans.pb(0);
continue;
}
// cnt1 increases by 1
ll val = (val2 + power(2, n - i - 1, cnt1 + 1) + cnt1 + 1) % (cnt1 + 1);
ans.pb(1 + f(val));
str[i] = '0';
}
}
for (ll x : ans)
cout << x << endl;
return 0;
}
signed main() {
fastio;
ll t = 1;
while (t--)
solve();
return 0;
}
/*******************************************************************************/
// Remeber - In a custom comparator cmp(a, b) a==b must always return to false.
| replace | 118 | 119 | 118 | 120 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
/*#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
*/
using namespace std;
#define m 1000000001
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repb(i, a, b) for (int i = a; i >= b; --i)
#define repv(arr) for (auto it = arr[i].begin(); it != arr[i].end(); ++it)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define ssortA(arr) stable_sort(arr.begin(), arr.end())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define mp make_pair
#define LCM(a, b) boost::math::lcm(a, b)
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
int step(int a) {
if (a == 0) {
return 0;
} else {
int count = 0;
while (a != 0) {
a = a % __builtin_popcount(a);
count++;
}
return count;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<char> arr(n);
vi i1, i2;
int c = 0;
rep(i, 0, n) {
cin >> arr[i];
if (arr[i] == '1')
++c;
}
// cout<<c<<endl;
int x = c - 1;
if (x < 0) {
rep(i, 0, n) {
if (arr[i] == '0')
cout << 1 << endl;
else {
cout << 0 << endl;
}
}
return 0;
}
if (x == 0) {
rep(i, 0, n) {
if (arr[i] == '1')
cout << 0 << endl;
else {
if (arr[n - 1] == '1')
cout << 2 << endl;
else {
if (i == n - 1)
cout << 2 << endl;
else {
cout << 1 << endl;
}
}
}
}
}
int y = c + 1;
vi b(200001, 0);
rep(i, 1, 200001) {
int j = i;
int c1 = 0;
while (j != 0) {
if (j % 2 != 0)
++c1;
j /= 2;
}
b[i] = 1 + b[i % c1];
/*if(i<=100){
cout<<b[i]<<endl;
}*/
/*if(i>199900){
cout<<b[i]<<" ";
}*/
}
// cout<<endl;
// cout<<b[15]<<endl;
vi p1(n), p2(n);
p1[0] = 1 % x;
for (int i = 1; i < n; i++) {
p1[i] = p1[i - 1] * (2 % x);
p1[i] %= x;
// cout<<p1[i]<<" ";
}
// cout<<endl;
p2[0] = 1 % y;
for (int i = 1; i < n; i++) {
p2[i] = p2[i - 1] * (2 % y);
p2[i] %= y;
}
ll sum1 = 0, sum2 = 0;
rep(i, 0, n) {
if (arr[i] == '1') {
sum1 += p1[n - 1 - i];
sum1 = sum1 % x;
}
}
rep(i, 0, n) {
if (arr[i] == '1') {
sum2 += p2[n - 1 - i];
sum2 = sum2 % y;
}
}
rep(i, 0, n) {
if (arr[i] == '1') {
int g = sum1;
int e = p1[n - 1 - i] % x;
// cout<<e<<" "<<sum1<<endl;
g = (sum1 - e + x) % x;
int f = 1 + step(g);
cout << f << endl;
} else {
int g = sum2;
int e = p2[n - 1 - i] % y;
g = (sum2 + e + y) % y;
int f = 1 + step(g);
cout << f << endl;
}
}
// cout<<"hi"<<endl;
}
| #include <bits/stdc++.h>
/*#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
*/
using namespace std;
#define m 1000000001
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repb(i, a, b) for (int i = a; i >= b; --i)
#define repv(arr) for (auto it = arr[i].begin(); it != arr[i].end(); ++it)
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define ssortA(arr) stable_sort(arr.begin(), arr.end())
#define search(arr, c) binary_search(arr.begin(), arr.end(), c)
#define pb push_back
#define ll long long
#define mp make_pair
#define LCM(a, b) boost::math::lcm(a, b)
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
int step(int a) {
if (a == 0) {
return 0;
} else {
int count = 0;
while (a != 0) {
a = a % __builtin_popcount(a);
count++;
}
return count;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<char> arr(n);
vi i1, i2;
int c = 0;
rep(i, 0, n) {
cin >> arr[i];
if (arr[i] == '1')
++c;
}
// cout<<c<<endl;
int x = c - 1;
if (x < 0) {
rep(i, 0, n) {
if (arr[i] == '0')
cout << 1 << endl;
else {
cout << 0 << endl;
}
}
return 0;
}
if (x == 0) {
rep(i, 0, n) {
if (arr[i] == '1')
cout << 0 << endl;
else {
if (arr[n - 1] == '1')
cout << 2 << endl;
else {
if (i == n - 1)
cout << 2 << endl;
else {
cout << 1 << endl;
}
}
}
}
return 0;
}
int y = c + 1;
vi b(200001, 0);
rep(i, 1, 200001) {
int j = i;
int c1 = 0;
while (j != 0) {
if (j % 2 != 0)
++c1;
j /= 2;
}
b[i] = 1 + b[i % c1];
/*if(i<=100){
cout<<b[i]<<endl;
}*/
/*if(i>199900){
cout<<b[i]<<" ";
}*/
}
// cout<<endl;
// cout<<b[15]<<endl;
vi p1(n), p2(n);
p1[0] = 1 % x;
for (int i = 1; i < n; i++) {
p1[i] = p1[i - 1] * (2 % x);
p1[i] %= x;
// cout<<p1[i]<<" ";
}
// cout<<endl;
p2[0] = 1 % y;
for (int i = 1; i < n; i++) {
p2[i] = p2[i - 1] * (2 % y);
p2[i] %= y;
}
ll sum1 = 0, sum2 = 0;
rep(i, 0, n) {
if (arr[i] == '1') {
sum1 += p1[n - 1 - i];
sum1 = sum1 % x;
}
}
rep(i, 0, n) {
if (arr[i] == '1') {
sum2 += p2[n - 1 - i];
sum2 = sum2 % y;
}
}
rep(i, 0, n) {
if (arr[i] == '1') {
int g = sum1;
int e = p1[n - 1 - i] % x;
// cout<<e<<" "<<sum1<<endl;
g = (sum1 - e + x) % x;
int f = 1 + step(g);
cout << f << endl;
} else {
int g = sum2;
int e = p2[n - 1 - i] % y;
g = (sum2 + e + y) % y;
int f = 1 + step(g);
cout << f << endl;
}
}
// cout<<"hi"<<endl;
}
| insert | 76 | 76 | 76 | 77 | 0 | |
p02609 | C++ | Runtime Error | #define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template <class T, class S> inline bool chmax(T &a, const S &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
long long power(long long a, unsigned long long b, long long m) {
long long ret(1);
while (b) {
if (b & 1)
ret = (ret * a) % m;
a = (a * a) % m;
b /= 2;
if (ret < 0)
ret += m;
if (a < 0)
a += m;
}
return ret;
}
int main() {
int N;
std::cin >> N;
std::string X;
std::cin >> X;
int pop(0);
for (int i(0); i < N; ++i) {
if (X[i] == '1')
++pop;
}
int p1(0), d1(0);
for (int i(0); i < N; ++i) {
p1 = (p1 * 2 + (X[i] - '0')) % (pop + 1);
if (pop > 1) {
d1 = (d1 * 2 + (X[i] - '0')) % (pop - 1);
}
}
for (int i(0); i < N; ++i) {
long long n((X[i] == '0') ? p1 : d1), cnt(1);
if (X[i] == '0') {
n = (n + power(2, N - i - 1, pop + 1)) % (pop + 1);
} else {
n = (n - power(2, N - i - 1, pop - 1)) % (pop - 1);
if (n < 0)
n += pop - 1;
}
while (n) {
++cnt;
long long m(n);
int a(__builtin_popcount(n));
n %= a;
}
std::cout << cnt << "\n";
}
return 0;
}
| #define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template <class T, class S> inline bool chmax(T &a, const S &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
long long power(long long a, unsigned long long b, long long m) {
long long ret(1);
while (b) {
if (b & 1)
ret = (ret * a) % m;
a = (a * a) % m;
b /= 2;
if (ret < 0)
ret += m;
if (a < 0)
a += m;
}
return ret;
}
int main() {
int N;
std::cin >> N;
std::string X;
std::cin >> X;
int pop(0);
for (int i(0); i < N; ++i) {
if (X[i] == '1')
++pop;
}
int p1(0), d1(0);
for (int i(0); i < N; ++i) {
p1 = (p1 * 2 + (X[i] - '0')) % (pop + 1);
if (pop > 1) {
d1 = (d1 * 2 + (X[i] - '0')) % (pop - 1);
}
}
for (int i(0); i < N; ++i) {
long long n((X[i] == '0') ? p1 : d1), cnt(1);
if (X[i] == '0') {
n = (n + power(2, N - i - 1, pop + 1)) % (pop + 1);
} else {
if (pop == 1) {
cnt = 0;
n = 0;
} else {
n = (n - power(2, N - i - 1, pop - 1)) % (pop - 1);
if (n < 0)
n += pop - 1;
}
}
while (n) {
++cnt;
long long m(n);
int a(__builtin_popcount(n));
n %= a;
}
std::cout << cnt << "\n";
}
return 0;
}
| replace | 93 | 96 | 93 | 101 | 0 | |
p02609 | C++ | Runtime Error | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
class DAnythingGoesToZero {
public:
void solve(istream &cin, ostream &cout) {
SPEED;
int n;
cin >> n;
string s;
cin >> s;
vector<int> f(n + 1);
for (int i = 1; i <= n; i++) {
int p = __builtin_popcount(i);
f[i] = 1 + f[i % p];
}
int cnt = 0;
rep(i, n) {
if (s[i] == '1') {
cnt++;
}
}
vector<int> mod(3);
mod[0] = max(1, cnt - 1);
mod[1] = cnt;
mod[2] = cnt + 1;
vector<vector<int>> beki(n + 1, vector<int>(3));
rep(i, 3) { beki[0][i] = 1 % mod[i]; }
for (int i = 0; i < n; i++) {
rep(j, 3) { beki[i + 1][j] = beki[i][j] * 2 % mod[j]; }
}
vector<int> x(3);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
rep(j, 3) {
x[j] += beki[n - 1 - i][j];
x[j] %= mod[j];
}
}
}
for (int i = 0; i < n; i++) {
int j = 1;
int now = 0;
if (s[i] == '0') {
j = 2;
now = x[j] + beki[n - 1 - i][j];
now %= mod[j];
} else {
j = 0;
now = x[j] - beki[n - 1 - i][j] + mod[j];
now %= mod[j];
if (cnt == 1) {
cout << 0 << '\n';
continue;
}
}
cout << 1 + f[now] << '\n';
}
}
};
signed main() {
DAnythingGoesToZero solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
} | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
class DAnythingGoesToZero {
public:
void solve(istream &cin, ostream &cout) {
SPEED;
int n;
cin >> n;
string s;
cin >> s;
vector<int> f(n + 1);
for (int i = 1; i <= n; i++) {
int p = __builtin_popcount(i);
f[i] = 1 + f[i % p];
}
int cnt = 0;
rep(i, n) {
if (s[i] == '1') {
cnt++;
}
}
vector<int> mod(3);
mod[0] = max(1, cnt - 1);
mod[1] = max(1, cnt);
mod[2] = cnt + 1;
vector<vector<int>> beki(n + 1, vector<int>(3));
rep(i, 3) { beki[0][i] = 1 % mod[i]; }
for (int i = 0; i < n; i++) {
rep(j, 3) { beki[i + 1][j] = beki[i][j] * 2 % mod[j]; }
}
vector<int> x(3);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
rep(j, 3) {
x[j] += beki[n - 1 - i][j];
x[j] %= mod[j];
}
}
}
for (int i = 0; i < n; i++) {
int j = 1;
int now = 0;
if (s[i] == '0') {
j = 2;
now = x[j] + beki[n - 1 - i][j];
now %= mod[j];
} else {
j = 0;
now = x[j] - beki[n - 1 - i][j] + mod[j];
now %= mod[j];
if (cnt == 1) {
cout << 0 << '\n';
continue;
}
}
cout << 1 + f[now] << '\n';
}
}
};
signed main() {
DAnythingGoesToZero solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
} | replace | 71 | 72 | 71 | 72 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int v[2][200010], res[200010];
int f(int n) {
int res = 0;
while (n > 0) {
n %= __builtin_popcount(n);
++res;
}
return res;
}
int main() {
int n;
cin >> n;
string x;
cin >> x;
int d = count(begin(x), end(x), '1');
for (int i = 0; i < 1 + (d > 1); ++i) {
v[i][0] = 1;
for (int j = 0; j < n; ++j)
v[i][j + 1] = v[i][j] * 2 % (i ? d - 1 : d + 1);
}
int r0 = 0, r1 = 0;
for (auto &c : x)
r0 = (r0 * 2 + (c - '0')) % (d + 1);
for (auto &c : x)
r1 = (r1 * 2 + (c - '0')) % (d - 1);
reverse(begin(x), end(x));
for (int i = 0; i < n; ++i) {
char &c = x[i];
if (c == '0') {
int t = (r0 + v[0][i]) % (d + 1);
res[i] = f(t) + 1;
} else {
if (d > 1) {
int t = (r1 - v[1][i] + d - 1) % (d - 1);
res[i] = f(t) + 1;
}
}
}
reverse(res, res + n);
for (int i = 0; i < n; ++i)
cout << res[i] << '\n';
}
| #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int v[2][200010], res[200010];
int f(int n) {
int res = 0;
while (n > 0) {
n %= __builtin_popcount(n);
++res;
}
return res;
}
int main() {
int n;
cin >> n;
string x;
cin >> x;
int d = count(begin(x), end(x), '1');
for (int i = 0; i < 1 + (d > 1); ++i) {
v[i][0] = 1;
for (int j = 0; j < n; ++j)
v[i][j + 1] = v[i][j] * 2 % (i ? d - 1 : d + 1);
}
int r0 = 0, r1 = 0;
for (auto &c : x)
r0 = (r0 * 2 + (c - '0')) % (d + 1);
if (d > 1)
for (auto &c : x)
r1 = (r1 * 2 + (c - '0')) % (d - 1);
reverse(begin(x), end(x));
for (int i = 0; i < n; ++i) {
char &c = x[i];
if (c == '0') {
int t = (r0 + v[0][i]) % (d + 1);
res[i] = f(t) + 1;
} else {
if (d > 1) {
int t = (r1 - v[1][i] + d - 1) % (d - 1);
res[i] = f(t) + 1;
}
}
}
reverse(res, res + n);
for (int i = 0; i < n; ++i)
cout << res[i] << '\n';
}
| replace | 32 | 34 | 32 | 35 | 0 | |
p02609 | C++ | Runtime Error | #pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0); \
cout << fixed; \
cout << setprecision(10);
#define f first
#define s second
#define endl "\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a) \
for (auto it : a) \
cout << it << " "; \
cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;
const ll N = 3e5 + 5;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int INFi = 0x7f7f7f7f;
ll test = 1, n, ans[N], p2[N];
string x;
void calc(ll f, ll mod) {
p2[0] = 1;
for (int i = 1; i <= n; i++)
p2[i] = (2 * p2[i - 1]) % mod;
ll res = 0;
for (int i = 0; i < n; i++) {
if (x[i] == '1')
res = (res + p2[i]) % mod;
}
for (int i = 0; i < n; i++) {
if (x[i] == f + '0') {
ll res1, ct = 1;
if (f == 0)
res1 = (res + p2[i]) % mod;
else
res1 = (res - p2[i] + mod) % mod;
while (res1) {
res1 = res1 % (__builtin_popcount(res1));
ct++;
}
ans[i] = ct;
}
}
}
void solve() {
cin >> n >> x;
reverse(x.begin(), x.end());
ll ct = 0;
for (int i = 0; i < n; i++)
ct += x[i] - '0';
calc(0, ct + 1);
calc(1, ct - 1);
for (int i = n - 1; i >= 0; i--)
deb1(ans[i])
}
int main() {
fastio;
// cin>>test;
for (int i = 1; i <= test; i++) {
solve();
}
} | #pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0); \
cout << fixed; \
cout << setprecision(10);
#define f first
#define s second
#define endl "\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a) \
for (auto it : a) \
cout << it << " "; \
cout << endl;
#define deb1(a) cout << a << endl;
#define deb2(a, b) cout << a << " " << b << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;
const ll N = 3e5 + 5;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const int INFi = 0x7f7f7f7f;
ll test = 1, n, ans[N], p2[N];
string x;
void calc(ll f, ll mod) {
p2[0] = 1;
for (int i = 1; i <= n; i++)
p2[i] = (2 * p2[i - 1]) % mod;
ll res = 0;
for (int i = 0; i < n; i++) {
if (x[i] == '1')
res = (res + p2[i]) % mod;
}
for (int i = 0; i < n; i++) {
if (x[i] == f + '0') {
ll res1, ct = 1;
if (f == 0)
res1 = (res + p2[i]) % mod;
else
res1 = (res - p2[i] + mod) % mod;
while (res1) {
res1 = res1 % (__builtin_popcount(res1));
ct++;
}
ans[i] = ct;
}
}
}
void solve() {
cin >> n >> x;
reverse(x.begin(), x.end());
ll ct = 0;
for (int i = 0; i < n; i++)
ct += x[i] - '0';
if (ct + 1)
calc(0, ct + 1);
if (ct - 1)
calc(1, ct - 1);
for (int i = n - 1; i >= 0; i--)
deb1(ans[i])
}
int main() {
fastio;
// cin>>test;
for (int i = 1; i <= test; i++) {
solve();
}
} | replace | 73 | 75 | 73 | 77 | 0 | |
p02609 | C++ | Runtime Error | /*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using __gnu_cxx::power;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout; // replace by cerr in interactive.
template <class C> static auto o(C *x) -> decltype(os << *x, 0);
template <class C> static char o(...);
template <class C>
typename enable_if<sizeof o<C>(0) != 1, Dbg &>::type operator<<(const C &x) {
os << x;
return *this;
}
template <class C> Dbg &dump(C b, C e) {
for (
struct {
C i;
int j;
} i = {(*this << "\n[", b), 0};
i.i != e and i.j < 26; i.i++, i.j++)
*this << ", " + 2 * (i.i == b) << *i.i;
return *this << ']';
}
template <class C>
typename enable_if<sizeof o<C>(0) == 1, Dbg &>::type operator<<(const C &x) {
return dump(begin(x), end(x));
}
template <class C, size_t X>
typename enable_if<!is_same<C, const char>::value, Dbg &>::type
operator<<(C (&x)[X]) {
return dump(begin(x), end(x));
}
template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
return *this << '{' << x.first << ", " << x.second << '}';
}
Dbg &operator<<(ostream &(*x)(std::ostream &)) {
os << x;
return *this;
}
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
string name;
*iss >> name;
if (name.back() == ',')
name.pop_back();
dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...) \
dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...) \
if (a) \
bug(args);
#define ASSERT(a, o, b, args...) \
if (!((a)o(b))) \
bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 200005;
#define im(a, b) \
if (a >= b) \
a -= b; \
else if (a < 0) \
a += b;
int n, firstMod[4], p2Mod[4][mxn], pref[mxn], f[mxn];
char s[mxn];
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout.precision(11), cout.setf(ios::fixed);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
pref[i] = pref[i % __builtin_popcount(i)] + 1;
}
int cntSet = 0;
for (int i = 0; i < n; i++) {
cntSet += s[i] == '1';
}
for (int i = 0; i < 3; i++) {
p2Mod[i][0] = 1 % (cntSet + i - 1);
for (int j = 1; j <= n; j++) {
p2Mod[i][j] = p2Mod[i][j - 1] << 1;
im(p2Mod[i][j], cntSet + i - 1)
}
for (int j = 0; j < n; j++) {
firstMod[i] += (s[j] == '1' ? p2Mod[i][n - 1 - j] : 0);
im(firstMod[i], cntSet + i - 1)
}
}
for (int i = 0; i < n; i++) {
int o = s[i] == '1' ? 0 : 2;
if (cntSet - 1 + o == 0)
continue;
int o2 = firstMod[o] +
(s[i] == '1' ? -p2Mod[o][n - 1 - i] : p2Mod[o][n - 1 - i]);
im(o2, cntSet - 1 + o) f[i + 1] = pref[o2] + 1;
}
for (int i = 1; i <= n; i++) {
cout << f[i] << '\n';
}
} | /*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using __gnu_cxx::power;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout; // replace by cerr in interactive.
template <class C> static auto o(C *x) -> decltype(os << *x, 0);
template <class C> static char o(...);
template <class C>
typename enable_if<sizeof o<C>(0) != 1, Dbg &>::type operator<<(const C &x) {
os << x;
return *this;
}
template <class C> Dbg &dump(C b, C e) {
for (
struct {
C i;
int j;
} i = {(*this << "\n[", b), 0};
i.i != e and i.j < 26; i.i++, i.j++)
*this << ", " + 2 * (i.i == b) << *i.i;
return *this << ']';
}
template <class C>
typename enable_if<sizeof o<C>(0) == 1, Dbg &>::type operator<<(const C &x) {
return dump(begin(x), end(x));
}
template <class C, size_t X>
typename enable_if<!is_same<C, const char>::value, Dbg &>::type
operator<<(C (&x)[X]) {
return dump(begin(x), end(x));
}
template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
return *this << '{' << x.first << ", " << x.second << '}';
}
Dbg &operator<<(ostream &(*x)(std::ostream &)) {
os << x;
return *this;
}
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
string name;
*iss >> name;
if (name.back() == ',')
name.pop_back();
dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...) \
dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...) \
if (a) \
bug(args);
#define ASSERT(a, o, b, args...) \
if (!((a)o(b))) \
bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 200005;
#define im(a, b) \
if (a >= b) \
a -= b; \
else if (a < 0) \
a += b;
int n, firstMod[4], p2Mod[4][mxn], pref[mxn], f[mxn];
char s[mxn];
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout.precision(11), cout.setf(ios::fixed);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
pref[i] = pref[i % __builtin_popcount(i)] + 1;
}
int cntSet = 0;
for (int i = 0; i < n; i++) {
cntSet += s[i] == '1';
}
for (int i = 0; i < 3; i++) {
if (cntSet + i - 1 <= 0)
continue;
p2Mod[i][0] = 1 % (cntSet + i - 1);
for (int j = 1; j <= n; j++) {
p2Mod[i][j] = p2Mod[i][j - 1] << 1;
im(p2Mod[i][j], cntSet + i - 1)
}
for (int j = 0; j < n; j++) {
firstMod[i] += (s[j] == '1' ? p2Mod[i][n - 1 - j] : 0);
im(firstMod[i], cntSet + i - 1)
}
}
for (int i = 0; i < n; i++) {
int o = s[i] == '1' ? 0 : 2;
if (cntSet - 1 + o == 0)
continue;
int o2 = firstMod[o] +
(s[i] == '1' ? -p2Mod[o][n - 1 - i] : p2Mod[o][n - 1 - i]);
im(o2, cntSet - 1 + o) f[i + 1] = pref[o2] + 1;
}
for (int i = 1; i <= n; i++) {
cout << f[i] << '\n';
}
} | insert | 89 | 89 | 89 | 91 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)
ll INF = 1e9;
ll NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // 最小公倍数
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
//-----------------------------------------
ll num(ll x) {
ll res = 0;
while (x > 0) {
if (x % 2 == 1) {
res++;
}
x /= 2;
}
return res;
}
long long power_mod(long long x, long long y, long long m) {
long long res = 1;
if (y > 0) {
res = power_mod(x, y / 2, m);
if (y % 2 == 0) {
res = (res * res) % m;
} else {
res = (((res * res) % m) * x) % m;
}
}
return res;
}
//-----------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
string s;
cin >> n >> s;
reverse(ALL(s));
ll m = 0;
REP(i, n) {
if (s[i] == '1') {
m++;
}
}
vll mp(n, 0);
vll pp(n, 0);
REP(i, n) {
if (m != 1) {
mp[i] = power_mod(2, i, m - 1);
}
pp[i] = power_mod(2, i, m + 1);
}
ll a = 0;
ll b = 0;
REP(i, n) {
if (s[i] == '1') {
if (m != 1) {
a += mp[i];
a %= (m - 1);
}
b += pp[i];
b %= (m + 1);
}
}
vll vec;
ll hoge, ans;
REP(i, n) {
if (s[i] == '0') {
hoge = b;
hoge += pp[i];
hoge %= (m + 1);
ans = 1;
while (hoge > 0) {
ans++;
hoge %= num(hoge);
}
vec.PB(ans);
} else {
hoge = a;
hoge -= mp[i];
hoge += (m - 1);
hoge %= (m - 1);
ans = 1;
while (hoge > 0) {
ans++;
hoge %= num(hoge);
}
vec.PB(ans);
}
}
reverse(ALL(vec));
REP(i, n) { cout << vec[i] << endl; }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)
ll INF = 1e9;
ll NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } // 最小公倍数
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
//-----------------------------------------
ll num(ll x) {
ll res = 0;
while (x > 0) {
if (x % 2 == 1) {
res++;
}
x /= 2;
}
return res;
}
long long power_mod(long long x, long long y, long long m) {
long long res = 1;
if (y > 0) {
res = power_mod(x, y / 2, m);
if (y % 2 == 0) {
res = (res * res) % m;
} else {
res = (((res * res) % m) * x) % m;
}
}
return res;
}
//-----------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
string s;
cin >> n >> s;
reverse(ALL(s));
ll m = 0;
REP(i, n) {
if (s[i] == '1') {
m++;
}
}
vll mp(n, 0);
vll pp(n, 0);
REP(i, n) {
if (m != 1) {
mp[i] = power_mod(2, i, m - 1);
}
pp[i] = power_mod(2, i, m + 1);
}
ll a = 0;
ll b = 0;
REP(i, n) {
if (s[i] == '1') {
if (m != 1) {
a += mp[i];
a %= (m - 1);
}
b += pp[i];
b %= (m + 1);
}
}
vll vec;
ll hoge, ans;
REP(i, n) {
if (s[i] == '0') {
hoge = b;
hoge += pp[i];
hoge %= (m + 1);
ans = 1;
while (hoge > 0) {
ans++;
hoge %= num(hoge);
}
vec.PB(ans);
} else {
if (m == 1) {
vec.PB(0);
continue;
}
hoge = a;
hoge -= mp[i];
hoge += (m - 1);
hoge %= (m - 1);
ans = 1;
while (hoge > 0) {
ans++;
hoge %= num(hoge);
}
vec.PB(ans);
}
}
reverse(ALL(vec));
REP(i, n) { cout << vec[i] << endl; }
return 0;
}
| insert | 124 | 124 | 124 | 128 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER \
ios_base::sync_with_stdio(0); \
cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
for (const A &it : a)
out << it << " ";
return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
for (A &i : a)
in >> i;
return in;
}
// ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a, int b, const int &m = MOD) {
if (b == 0)
return 1;
int x = poww(a, b / 2, m);
x = x * x % m;
if (b & 1)
x = x * a % m;
return x;
}
int ceil(const int &a, const int &b) { return (a + b - 1) / b; }
////Read:
// Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),
int mp[400005];
signed main() {
FASTER;
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < 400005; i++) {
int tmp = i;
int ans = 0;
while (tmp) {
tmp = tmp % __builtin_popcountll(tmp);
ans++;
}
mp[i] = ans;
}
int cnt = 0;
for (char c : s)
cnt += (c == '1');
int Pp1 = 0, Pm1 = 0;
for (int i = n - 1, id = 0; i >= 0; i--, id++) {
if (s[i] == '1') {
Pp1 = (Pp1 + poww(2, id, cnt + 1)) % (cnt + 1);
if (cnt >= 2)
Pm1 = (Pm1 + (1LL << id)) % (cnt - 1);
}
}
vi cp1mod(n + n, 1);
for (int i = 1; i < 2 * n; i++) {
cp1mod[i] = cp1mod[i - 1] * 2 % (cnt + 1);
}
vi cm1mod(n + n, 1);
for (int i = 1; i < 2 * n && cnt > 1; i++) {
cm1mod[i] = cm1mod[i - 1] * 2 % (cnt - 1);
}
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
int rem = (Pp1 + cp1mod[n - i - 1]) % (cnt + 1);
int cntt = 1 + mp[rem];
cout << cntt << "\n";
} else {
if (cnt == 1) {
cout << 0 << "\n";
} else {
int rem = (Pm1 - cm1mod[n - i - 1] + cnt - 1) % (cnt - 1);
int cntt = 1 + mp[rem];
cout << cntt << "\n";
}
}
}
}
| #include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER \
ios_base::sync_with_stdio(0); \
cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
for (const A &it : a)
out << it << " ";
return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
for (A &i : a)
in >> i;
return in;
}
// ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a, int b, const int &m = MOD) {
if (b == 0)
return 1;
int x = poww(a, b / 2, m);
x = x * x % m;
if (b & 1)
x = x * a % m;
return x;
}
int ceil(const int &a, const int &b) { return (a + b - 1) / b; }
////Read:
// Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),
int mp[400005];
signed main() {
FASTER;
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < 400005; i++) {
int tmp = i;
int ans = 0;
while (tmp) {
tmp = tmp % __builtin_popcountll(tmp);
ans++;
}
mp[i] = ans;
}
int cnt = 0;
for (char c : s)
cnt += (c == '1');
int Pp1 = 0, Pm1 = 0;
for (int i = n - 1, id = 0; i >= 0; i--, id++) {
if (s[i] == '1') {
Pp1 = (Pp1 + poww(2, id, cnt + 1)) % (cnt + 1);
if (cnt >= 2)
Pm1 = (Pm1 + poww(2, id, cnt - 1)) % (cnt - 1);
}
}
vi cp1mod(n + n, 1);
for (int i = 1; i < 2 * n; i++) {
cp1mod[i] = cp1mod[i - 1] * 2 % (cnt + 1);
}
vi cm1mod(n + n, 1);
for (int i = 1; i < 2 * n && cnt > 1; i++) {
cm1mod[i] = cm1mod[i - 1] * 2 % (cnt - 1);
}
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
int rem = (Pp1 + cp1mod[n - i - 1]) % (cnt + 1);
int cntt = 1 + mp[rem];
cout << cntt << "\n";
} else {
if (cnt == 1) {
cout << 0 << "\n";
} else {
int rem = (Pm1 - cm1mod[n - i - 1] + cnt - 1) % (cnt - 1);
int cntt = 1 + mp[rem];
cout << cntt << "\n";
}
}
}
}
| replace | 80 | 81 | 80 | 81 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// 問題文および制約はちゃんと確認しよう!
// サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
// とりあえずサンプルを読んでから解法を考えよう?
int zyo[200100][2];
int dp[200100];
int out[200100];
int cal(int x) {
if (x == 0)
return 0;
if (dp[x] >= 0)
return dp[x];
return dp[x] = 1 + cal(x % __builtin_popcount(x));
}
int main() {
memset(dp, -1, sizeof(dp));
int n, m;
string s;
cin >> n;
cin >> s;
reverse(All(s));
rep(i, n) m += (s[i] - '0');
int mo[2] = {m + 1, max(1, m - 1)};
zyo[0][0] = zyo[0][1] = 1;
rep(i, n + 10) rep(j, 2) {
zyo[i + 1][j] = zyo[i][j] * 2;
zyo[i + 1][j] %= mo[j];
}
int sum[2] = {0, 0};
rep(i, n) rep(j, 2) {
sum[j] += zyo[i][j] * (s[i] - '0');
sum[j] %= mo[j];
}
rep(i, n) {
if (s[i] == '1') {
if (m == 1) {
out[i] = 0;
continue;
}
int su = sum[1] - zyo[i][1];
su %= mo[1];
su += mo[1];
su %= mo[1];
out[i] = 1 + cal(su);
} else {
int su = sum[0] + zyo[i][0];
su %= mo[0];
out[i] = 1 + cal(su);
}
}
rep(i, n) cout << out[n - 1 - i] << endl;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// 問題文および制約はちゃんと確認しよう!
// サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
// とりあえずサンプルを読んでから解法を考えよう?
int zyo[200100][2];
int dp[200100];
int out[200100];
int cal(int x) {
if (x == 0)
return 0;
if (dp[x] >= 0)
return dp[x];
return dp[x] = 1 + cal(x % __builtin_popcount(x));
}
int main() {
memset(dp, -1, sizeof(dp));
int n, m = 0;
string s;
cin >> n;
cin >> s;
reverse(All(s));
rep(i, n) m += (s[i] - '0');
int mo[2] = {m + 1, max(1, m - 1)};
zyo[0][0] = zyo[0][1] = 1;
rep(i, n + 10) rep(j, 2) {
zyo[i + 1][j] = zyo[i][j] * 2;
zyo[i + 1][j] %= mo[j];
}
int sum[2] = {0, 0};
rep(i, n) rep(j, 2) {
sum[j] += zyo[i][j] * (s[i] - '0');
sum[j] %= mo[j];
}
rep(i, n) {
if (s[i] == '1') {
if (m == 1) {
out[i] = 0;
continue;
}
int su = sum[1] - zyo[i][1];
su %= mo[1];
su += mo[1];
su %= mo[1];
out[i] = 1 + cal(su);
} else {
int su = sum[0] + zyo[i][0];
su %= mo[0];
out[i] = 1 + cal(su);
}
}
rep(i, n) cout << out[n - 1 - i] << endl;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
ll n;
string s;
ll b;
ll p, m;
ll ans[200005];
int main(void) {
cin >> n >> s;
if (s == "1") {
cout << 0 << endl;
return 0;
}
for (int i = 0; i < n; i++) {
if (s[i] == '1')
b++;
}
ll k = 1, l = 1;
for (int i = 0; i < n; i++) {
p = (p + (s[n - 1 - i] - '0') * k) % (b + 1);
if (b != 1) {
m = (m + (s[n - 1 - i] - '0') * l) % (b - 1);
l = (l * 2) % (b - 1);
}
k = (k * 2) % (b + 1);
}
// cout<<p<<" "<<m<<endl;
k = 1, l = 1;
ll c;
for (int i = n - 1; i >= 0; i--) {
c = 1;
if (s[i] == '0') {
ll t = (p + k) % (b + 1);
// cout<<i<<" "<<t<<endl;
while (t != 0) {
ll a = 0;
ll co = t;
while (co > 0) {
a += (co % 2);
co = co / 2;
}
t = t % a;
c++;
}
} else {
if (b == 1) {
ans[i] = 0;
continue;
}
ll T = (m - l) % (b - 1);
// cout<<i<<" "<<T<<endl;
if (T < 0) {
T += (b - 1);
}
while (T != 0) {
ll A = 0;
ll CO = T;
while (CO > 0) {
A += (CO % 2);
CO = CO / 2;
}
T = T % A;
c++;
}
}
ans[i] = c;
k = (k * 2) % (b + 1);
l = (l * 2) % (b - 1);
}
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
ll n;
string s;
ll b;
ll p, m;
ll ans[200005];
int main(void) {
cin >> n >> s;
if (s == "1") {
cout << 0 << endl;
return 0;
}
for (int i = 0; i < n; i++) {
if (s[i] == '1')
b++;
}
ll k = 1, l = 1;
for (int i = 0; i < n; i++) {
p = (p + (s[n - 1 - i] - '0') * k) % (b + 1);
if (b != 1) {
m = (m + (s[n - 1 - i] - '0') * l) % (b - 1);
l = (l * 2) % (b - 1);
}
k = (k * 2) % (b + 1);
}
// cout<<p<<" "<<m<<endl;
k = 1, l = 1;
ll c;
for (int i = n - 1; i >= 0; i--) {
c = 1;
if (s[i] == '0') {
ll t = (p + k) % (b + 1);
// cout<<i<<" "<<t<<endl;
while (t != 0) {
ll a = 0;
ll co = t;
while (co > 0) {
a += (co % 2);
co = co / 2;
}
t = t % a;
c++;
}
} else {
if (b == 1) {
ans[i] = 0;
continue;
}
ll T = (m - l) % (b - 1);
// cout<<i<<" "<<T<<endl;
if (T < 0) {
T += (b - 1);
}
while (T != 0) {
ll A = 0;
ll CO = T;
while (CO > 0) {
A += (CO % 2);
CO = CO / 2;
}
T = T % A;
c++;
}
}
ans[i] = c;
k = (k * 2) % (b + 1);
if (b != 1) {
l = (l * 2) % (b - 1);
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
}
| replace | 70 | 71 | 70 | 73 | 0 | |
p02609 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define lowbit(x) ((x) & -(x))
#define lson l, mid, id << 1
#define rson mid + 1, r, id << 1 | 1
#define ls id << 1
#define rs id << 1 | 1
#define MID(l, r) ((l) + (((r) - (l)) >> 1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using LL = long long;
// using LL = __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; // 1e6;
const int MOD = 1000000007; // 1e9 + 7
// const int MOD = 998244353;
// const int MOD = 911451407;
template <typename T = int> inline T read() {
char c = '\0';
bool f = false;
T x = 0;
while (c < '0' || '9' < c) {
if (c == '-')
f = true;
c = getchar();
}
while ('0' <= c && c <= '9') {
x = (x << 1) + (x << 3) + (c & 0xf);
c = getchar();
}
if (f)
return -x;
return x;
}
template <typename T> inline void read(T &x) { x = read<T>(); }
template <typename T, typename... Args> inline void read(T &x, Args &...args) {
read(x);
read(args...);
}
template <typename T> inline void getmin(T &x, T y) {
if (y < x)
x = y;
}
template <typename T> inline void getmax(T &x, T y) {
if (x < y)
x = y;
}
template <typename X, typename Y>
inline void depair(X &x, Y &y, const pair<X, Y> &pr) {
x = pr.fi;
y = pr.se;
}
inline int sig(double x) { return x < -eps ? -1 : eps < x; }
LL fp(LL a, LL n, LL mod = MOD) {
if (n < 0)
a = fp(a, mod - 2, mod), n = -n;
LL res = 1;
for (; n; n >>= 1, a = a * a % mod)
if (n & 1)
res = res * a % mod;
return res;
}
struct Mint {
int x;
Mint() : x(0) {}
Mint(int _x) : x(_x) {
if (x < 0 || x >= MOD)
x %= MOD;
if (x < 0)
x += MOD;
}
Mint(LL _x) {
if (_x < 0 || _x >= MOD)
_x %= MOD;
if (_x < 0)
_x += MOD;
x = _x;
}
Mint operator-() const { return Mint(MOD - x); }
Mint operator+(const Mint &rhs) const {
return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x);
}
Mint operator-(const Mint &rhs) const {
return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x);
}
Mint operator*(const Mint &rhs) const { return Mint((LL)x * rhs.x % MOD); }
Mint operator/(const Mint &rhs) const {
return Mint((LL)x * fp(rhs.x, -1) % MOD);
}
bool operator==(const Mint &rhs) const { return x == rhs.x; }
bool operator!=(const Mint &rhs) const { return x != rhs.x; }
Mint &operator+=(const Mint &rhs) {
x += rhs.x;
if (x >= MOD)
x -= MOD;
return *this;
}
Mint &operator-=(const Mint &rhs) {
x -= rhs.x;
if (x < 0)
x += MOD;
return *this;
}
Mint &operator*=(const Mint &rhs) {
x = (LL)x * rhs.x % MOD;
return *this;
}
Mint &operator/=(const Mint &rhs) {
x = (LL)x * fp(rhs.x, -1) % MOD;
return *this;
}
friend ostream &operator<<(ostream &out, Mint &rhs) {
return out << to_string(rhs.x);
}
Mint inv() { return Mint(fp(x, -1)); }
Mint pow(int k) { return Mint(fp(x, k)); }
Mint pow(const Mint &t) { return Mint(fp(x, t.x)); }
};
const int maxn = (int)2e5 + 20;
const int maxm = (int)2e6 + 20;
void work() {
int n;
string s;
cin >> n >> s;
int cnt = 0;
for (auto ch : s)
cnt += ch == '1';
vector<int> rem0(n, 0), rem1(n, 0);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
rem0[i] = fp(2, n - 1 - i, cnt + 1);
if (cnt >= 2)
rem1[i] = fp(2, n - 1 - i, cnt - 1);
}
}
int tot0 = 0, tot1 = 0;
for (int i = 0; i < n; i++) {
tot0 = (tot0 + rem0[i]) % (cnt + 1);
tot1 = (tot1 + rem1[i]) % (cnt - 1);
}
for (int i = 0; i < n; i++) {
auto f = [](int x) {
int res = 0;
while (x) {
int r = 0;
int y = x;
while (y) {
y -= lowbit(y);
r++;
}
x %= r;
res++;
}
return res;
};
int res = 0;
if (s[i] == '0') {
int x = (tot0 + fp(2, n - 1 - i, cnt + 1)) % (cnt + 1);
res++;
res += f(x);
} else {
if (cnt != 1) {
int x = tot1 - rem1[i];
x = (x % (cnt - 1) + (cnt - 1)) % (cnt - 1);
res++;
res += f(x);
}
}
cout << res << endl;
}
}
int main() {
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // yukihana0416
int tc = 1;
// read(tc);
for (int ca = 1; ca <= tc; ca++) {
// printf("Case #%d: ", ca);
work();
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define lowbit(x) ((x) & -(x))
#define lson l, mid, id << 1
#define rson mid + 1, r, id << 1 | 1
#define ls id << 1
#define rs id << 1 | 1
#define MID(l, r) ((l) + (((r) - (l)) >> 1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using LL = long long;
// using LL = __int64;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pLL = pair<LL, LL>;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; // 1e6;
const int MOD = 1000000007; // 1e9 + 7
// const int MOD = 998244353;
// const int MOD = 911451407;
template <typename T = int> inline T read() {
char c = '\0';
bool f = false;
T x = 0;
while (c < '0' || '9' < c) {
if (c == '-')
f = true;
c = getchar();
}
while ('0' <= c && c <= '9') {
x = (x << 1) + (x << 3) + (c & 0xf);
c = getchar();
}
if (f)
return -x;
return x;
}
template <typename T> inline void read(T &x) { x = read<T>(); }
template <typename T, typename... Args> inline void read(T &x, Args &...args) {
read(x);
read(args...);
}
template <typename T> inline void getmin(T &x, T y) {
if (y < x)
x = y;
}
template <typename T> inline void getmax(T &x, T y) {
if (x < y)
x = y;
}
template <typename X, typename Y>
inline void depair(X &x, Y &y, const pair<X, Y> &pr) {
x = pr.fi;
y = pr.se;
}
inline int sig(double x) { return x < -eps ? -1 : eps < x; }
LL fp(LL a, LL n, LL mod = MOD) {
if (n < 0)
a = fp(a, mod - 2, mod), n = -n;
LL res = 1;
for (; n; n >>= 1, a = a * a % mod)
if (n & 1)
res = res * a % mod;
return res;
}
struct Mint {
int x;
Mint() : x(0) {}
Mint(int _x) : x(_x) {
if (x < 0 || x >= MOD)
x %= MOD;
if (x < 0)
x += MOD;
}
Mint(LL _x) {
if (_x < 0 || _x >= MOD)
_x %= MOD;
if (_x < 0)
_x += MOD;
x = _x;
}
Mint operator-() const { return Mint(MOD - x); }
Mint operator+(const Mint &rhs) const {
return Mint(x + rhs.x >= MOD ? x + rhs.x - MOD : x + rhs.x);
}
Mint operator-(const Mint &rhs) const {
return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x);
}
Mint operator*(const Mint &rhs) const { return Mint((LL)x * rhs.x % MOD); }
Mint operator/(const Mint &rhs) const {
return Mint((LL)x * fp(rhs.x, -1) % MOD);
}
bool operator==(const Mint &rhs) const { return x == rhs.x; }
bool operator!=(const Mint &rhs) const { return x != rhs.x; }
Mint &operator+=(const Mint &rhs) {
x += rhs.x;
if (x >= MOD)
x -= MOD;
return *this;
}
Mint &operator-=(const Mint &rhs) {
x -= rhs.x;
if (x < 0)
x += MOD;
return *this;
}
Mint &operator*=(const Mint &rhs) {
x = (LL)x * rhs.x % MOD;
return *this;
}
Mint &operator/=(const Mint &rhs) {
x = (LL)x * fp(rhs.x, -1) % MOD;
return *this;
}
friend ostream &operator<<(ostream &out, Mint &rhs) {
return out << to_string(rhs.x);
}
Mint inv() { return Mint(fp(x, -1)); }
Mint pow(int k) { return Mint(fp(x, k)); }
Mint pow(const Mint &t) { return Mint(fp(x, t.x)); }
};
const int maxn = (int)2e5 + 20;
const int maxm = (int)2e6 + 20;
void work() {
int n;
string s;
cin >> n >> s;
int cnt = 0;
for (auto ch : s)
cnt += ch == '1';
vector<int> rem0(n, 0), rem1(n, 0);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
rem0[i] = fp(2, n - 1 - i, cnt + 1);
if (cnt >= 2)
rem1[i] = fp(2, n - 1 - i, cnt - 1);
}
}
int tot0 = 0, tot1 = 0;
for (int i = 0; i < n; i++) {
tot0 = (tot0 + rem0[i]) % (cnt + 1);
if (cnt >= 2)
tot1 = (tot1 + rem1[i]) % (cnt - 1);
}
for (int i = 0; i < n; i++) {
auto f = [](int x) {
int res = 0;
while (x) {
int r = 0;
int y = x;
while (y) {
y -= lowbit(y);
r++;
}
x %= r;
res++;
}
return res;
};
int res = 0;
if (s[i] == '0') {
int x = (tot0 + fp(2, n - 1 - i, cnt + 1)) % (cnt + 1);
res++;
res += f(x);
} else {
if (cnt != 1) {
int x = tot1 - rem1[i];
x = (x % (cnt - 1) + (cnt - 1)) % (cnt - 1);
res++;
res += f(x);
}
}
cout << res << endl;
}
}
int main() {
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // yukihana0416
int tc = 1;
// read(tc);
for (int ca = 1; ca <= tc; ca++) {
// printf("Case #%d: ", ca);
work();
}
return 0;
}
| replace | 178 | 179 | 178 | 180 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_popcount(unsigned int x) { return __popcnt(x); }
#endif // _MSC_VER
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N;
string X;
int cnt[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N >> X;
if (N == 1) {
if (X == "0")
cout << 1 << endl;
else
cout << 0 << endl;
return;
}
reverse(all(X));
rep(i, 1, 201010) {
int pp = __builtin_popcount(i);
cnt[i] = cnt[i % pp] + 1;
}
int cn = 0;
rep(i, 0, N) if (X[i] == '1') cn++;
if (cn == 0) {
rep(i, 0, N) printf("1\n");
} else if (cn == N) {
int tot = 0, p = 1;
rep(i, 0, N) {
if (X[i] == '1')
tot = (tot + p) % (N - 1);
p = (p * 2) % (N - 1);
}
rep(i, 0, N) printf("%d\n", cnt[tot] + 1);
} else {
vector<int> ans;
int tot1 = 0, p1 = 1;
int tot2 = 0, p2 = 1;
rep(i, 0, N) {
if (X[i] == '1')
tot1 = (tot1 + p1) % (cn + 1);
p1 = (p1 * 2) % (cn + 1);
if (X[i] == '1')
tot2 = (tot2 + p2) % (cn - 1);
p2 = (p2 * 2) % (cn - 1);
}
p1 = 1;
p2 = 1;
rep(i, 0, N) {
if (X[i] == '1') {
int x = (tot2 - p2 + cn - 1) % (cn - 1);
ans.push_back(cnt[x] + 1);
} else {
int x = (tot1 + p1 + cn + 1) % (cn + 1);
ans.push_back(cnt[x] + 1);
}
p1 = (p1 * 2) % (cn + 1);
p2 = (p2 * 2) % (cn - 1);
}
reverse(all(ans));
rep(i, 0, N) printf("%d\n", ans[i]);
}
}
| #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_popcount(unsigned int x) { return __popcnt(x); }
#endif // _MSC_VER
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N;
string X;
int cnt[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N >> X;
if (N == 1) {
if (X == "0")
cout << 1 << endl;
else
cout << 0 << endl;
return;
}
reverse(all(X));
rep(i, 1, 201010) {
int pp = __builtin_popcount(i);
cnt[i] = cnt[i % pp] + 1;
}
int cn = 0;
rep(i, 0, N) if (X[i] == '1') cn++;
if (cn == 0) {
rep(i, 0, N) printf("1\n");
} else if (cn == N) {
int tot = 0, p = 1;
rep(i, 0, N) {
if (X[i] == '1')
tot = (tot + p) % (N - 1);
p = (p * 2) % (N - 1);
}
rep(i, 0, N) printf("%d\n", cnt[tot] + 1);
} else if (cn == 1) {
vector<int> ans;
int tot1 = 0, p1 = 1;
rep(i, 0, N) {
if (X[i] == '1')
tot1 = (tot1 + p1) % (cn + 1);
p1 = (p1 * 2) % (cn + 1);
}
p1 = 1;
rep(i, 0, N) {
if (X[i] == '1') {
ans.push_back(0);
} else {
int x = (tot1 + p1 + cn + 1) % (cn + 1);
ans.push_back(cnt[x] + 1);
}
p1 = (p1 * 2) % (cn + 1);
}
reverse(all(ans));
rep(i, 0, N) printf("%d\n", ans[i]);
} else {
vector<int> ans;
int tot1 = 0, p1 = 1;
int tot2 = 0, p2 = 1;
rep(i, 0, N) {
if (X[i] == '1')
tot1 = (tot1 + p1) % (cn + 1);
p1 = (p1 * 2) % (cn + 1);
if (X[i] == '1')
tot2 = (tot2 + p2) % (cn - 1);
p2 = (p2 * 2) % (cn - 1);
}
p1 = 1;
p2 = 1;
rep(i, 0, N) {
if (X[i] == '1') {
int x = (tot2 - p2 + cn - 1) % (cn - 1);
ans.push_back(cnt[x] + 1);
} else {
int x = (tot1 + p1 + cn + 1) % (cn + 1);
ans.push_back(cnt[x] + 1);
}
p1 = (p1 * 2) % (cn + 1);
p2 = (p2 * 2) % (cn - 1);
}
reverse(all(ans));
rep(i, 0, N) printf("%d\n", ans[i]);
}
}
| insert | 78 | 78 | 78 | 100 | 0 | |
p02609 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define power 210000
int rm[power] = {}, rp[power] = {};
void Initialize(int c) {
rm[0] = 1;
rp[0] = 1;
for (int i = 1; i < power; i++) {
rm[i] = (rm[i - 1] * 2) % (c - 1);
rp[i] = (rp[i - 1] * 2) % (c + 1);
}
}
int main() {
int n;
string s;
cin >> n >> s;
int c = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1')
c++;
Initialize(c);
int val_minus = 0, val_plus = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
val_minus += 1;
val_plus += 1;
}
val_minus %= c - 1;
val_plus %= c + 1;
if (i == n - 1)
break;
val_minus *= 2;
val_plus *= 2;
}
int ans[power] = {};
for (int i = 1; i < power; i++) {
int count = 0;
for (int j = 0; j < 20; j++) {
if (i & (1 << j))
count++;
}
ans[i] = ans[i % count] + 1;
}
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
int rest = val_minus - rm[n - 1 - i];
rest += c - 1;
rest %= c - 1;
cout << ans[rest] + 1 << endl;
} else {
int rest = val_plus + rp[n - 1 - i];
rest %= c + 1;
cout << ans[rest] + 1 << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define power 210000
int rm[power] = {}, rp[power] = {};
void Initialize(int c) {
rm[0] = 1;
rp[0] = 1;
for (int i = 1; i < power; i++) {
rm[i] = (rm[i - 1] * 2) % (c - 1);
rp[i] = (rp[i - 1] * 2) % (c + 1);
}
}
int main() {
int n;
string s;
cin >> n >> s;
int c = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1')
c++;
if (c == 0) {
for (int i = 0; i < n; i++) {
cout << 1 << endl;
}
return 0;
}
if (c == 1) {
if (s[n - 1] == '1') {
for (int i = 0; i < n - 1; i++) {
cout << 2 << endl;
}
cout << 0 << endl;
} else {
for (int i = 0; i < n - 1; i++) {
if (s[i] == '1') {
cout << 0 << endl;
continue;
}
cout << 1 << endl;
}
cout << 2 << endl;
}
return 0;
}
Initialize(c);
int val_minus = 0, val_plus = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
val_minus += 1;
val_plus += 1;
}
val_minus %= c - 1;
val_plus %= c + 1;
if (i == n - 1)
break;
val_minus *= 2;
val_plus *= 2;
}
int ans[power] = {};
for (int i = 1; i < power; i++) {
int count = 0;
for (int j = 0; j < 20; j++) {
if (i & (1 << j))
count++;
}
ans[i] = ans[i % count] + 1;
}
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
int rest = val_minus - rm[n - 1 - i];
rest += c - 1;
rest %= c - 1;
cout << ans[rest] + 1 << endl;
} else {
int rest = val_plus + rp[n - 1 - i];
rest %= c + 1;
cout << ans[rest] + 1 << endl;
}
}
}
| insert | 21 | 21 | 21 | 45 | 0 | |
p02609 | C++ | Runtime Error | #include <cstdio>
int n;
char x[200005];
int popcount(int x) {
if (x == 0) {
return 0;
}
if (x % 2 == 0) {
return popcount(x / 2);
} else {
return popcount(x / 2) + 1;
}
}
int f(int s) {
int t = 1;
while (s) {
s %= popcount(s);
t++;
}
return t;
}
int pow(int x, int y, int z) {
if (y == 0) {
return 1;
}
if (y % 2 == 0) {
return pow(1ll * x * x % z, y / 2, z) % z;
} else {
return 1ll * x * pow(x, y - 1, z) % z;
}
}
int main() {
scanf("%d", &n);
scanf("%s", x);
int pc = 0;
for (int i = 0; x[i]; i++) {
if (x[i] == '1') {
pc++;
}
}
int res_p = 0;
int res_m = 0;
for (int i = 0; x[i]; i++) {
res_p = (res_p * 2 + (x[i] - '0')) % (pc + 1);
if (pc != 1)
res_m = (res_m * 2 + (x[i] - '0')) % (pc - 1);
}
for (int i = 0; i < n; i++) {
int pc_n = x[i] == '0' ? pc + 1 : pc - 1;
int diff = pow(2, n - i - 1, pc_n);
int res;
if (x[i] == '0') {
res = res_p + diff;
} else {
res = res_m - diff;
}
res = ((res % pc_n) + pc_n) % pc_n;
printf("%d\n", f(res));
}
}
| #include <cstdio>
int n;
char x[200005];
int popcount(int x) {
if (x == 0) {
return 0;
}
if (x % 2 == 0) {
return popcount(x / 2);
} else {
return popcount(x / 2) + 1;
}
}
int f(int s) {
int t = 1;
while (s) {
s %= popcount(s);
t++;
}
return t;
}
int pow(int x, int y, int z) {
if (y == 0) {
return 1;
}
if (y % 2 == 0) {
return pow(1ll * x * x % z, y / 2, z) % z;
} else {
return 1ll * x * pow(x, y - 1, z) % z;
}
}
int main() {
scanf("%d", &n);
scanf("%s", x);
int pc = 0;
for (int i = 0; x[i]; i++) {
if (x[i] == '1') {
pc++;
}
}
int res_p = 0;
int res_m = 0;
for (int i = 0; x[i]; i++) {
res_p = (res_p * 2 + (x[i] - '0')) % (pc + 1);
if (pc != 1)
res_m = (res_m * 2 + (x[i] - '0')) % (pc - 1);
}
for (int i = 0; i < n; i++) {
int pc_n = x[i] == '0' ? pc + 1 : pc - 1;
if (pc_n == 0) {
printf("0\n");
continue;
}
int diff = pow(2, n - i - 1, pc_n);
int res;
if (x[i] == '0') {
res = res_p + diff;
} else {
res = res_m - diff;
}
res = ((res % pc_n) + pc_n) % pc_n;
printf("%d\n", f(res));
}
}
| insert | 54 | 54 | 54 | 58 | 0 | |
p02609 | C++ | Runtime Error | #include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>
#define sqr(x) (x) * (x)
typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;
using namespace std;
using namespace std::chrono;
const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;
const long double eps = 1e-8;
const i64 MAXN = 200'000 + 5;
i64 simul(i64 v, i64 m) {
i64 R = 1;
while (v) {
R += 1;
i64 c = 0;
i64 t = v;
while (t) {
c += t % 2;
t /= 2;
}
v %= c;
}
return R;
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(15);
cout.setf(ios::fixed);
cerr.precision(15);
cerr.setf(ios::fixed);
i64 n;
string x;
cin >> n;
cin >> x;
bool fake = false;
if (fake) {
srand(time(0));
stringstream ss;
for (i64 i = 0; i < n; i++) {
ss << rand() % 2;
}
x = ss.str();
}
reverse(x.begin(), x.end());
i64 cnt = 0;
for (auto c : x) {
if (c == '1') {
cnt += 1;
}
}
vector<i64> gr(n + 1);
vector<i64> le(n + 1);
i64 vgr = 0;
i64 vle = 0;
if (cnt < n) {
for (i64 i = 0; i < n; i++) {
if (i == 0) {
gr[i] = 1 % (cnt + 1);
continue;
}
gr[i] = (gr[i - 1] * 2) % (cnt + 1);
}
for (i64 i = 0; i < n; i++) {
if (x[i] == '1') {
vgr += gr[i];
vgr %= (cnt + 1);
}
}
}
if (cnt > 0) {
for (i64 i = 0; i < n; i++) {
if (i == 0) {
le[i] = 1 % (cnt - 1);
continue;
}
le[i] = (le[i - 1] * 2) % (cnt - 1);
}
for (i64 i = 0; i < n; i++) {
if (x[i] == '1') {
vle += le[i];
vle %= (cnt - 1);
}
}
}
vector<i64> R;
for (i64 i = 0; i < n; i++) {
if (x[i] == '1') {
if (cnt == 1) {
R.push_back(0);
continue;
}
i64 r = vle;
i64 m = cnt - 1;
r = (r - le[i] + m) % m;
R.push_back(simul(r, m));
} else {
i64 r = vgr;
i64 m = cnt + 1;
r = (r + gr[i]) % m;
R.push_back(simul(r, m));
}
}
reverse(R.begin(), R.end());
stringstream ss;
for (auto t : R) {
ss << t << endl;
}
cout << ss.str();
return 0;
}
| #include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>
#define sqr(x) (x) * (x)
typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;
using namespace std;
using namespace std::chrono;
const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;
const long double eps = 1e-8;
const i64 MAXN = 200'000 + 5;
i64 simul(i64 v, i64 m) {
i64 R = 1;
while (v) {
R += 1;
i64 c = 0;
i64 t = v;
while (t) {
c += t % 2;
t /= 2;
}
v %= c;
}
return R;
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(15);
cout.setf(ios::fixed);
cerr.precision(15);
cerr.setf(ios::fixed);
i64 n;
string x;
cin >> n;
cin >> x;
bool fake = false;
if (fake) {
srand(time(0));
stringstream ss;
for (i64 i = 0; i < n; i++) {
ss << rand() % 2;
}
x = ss.str();
}
reverse(x.begin(), x.end());
i64 cnt = 0;
for (auto c : x) {
if (c == '1') {
cnt += 1;
}
}
vector<i64> gr(n + 1);
vector<i64> le(n + 1);
i64 vgr = 0;
i64 vle = 0;
if (cnt < n) {
for (i64 i = 0; i < n; i++) {
if (i == 0) {
gr[i] = 1 % (cnt + 1);
continue;
}
gr[i] = (gr[i - 1] * 2) % (cnt + 1);
}
for (i64 i = 0; i < n; i++) {
if (x[i] == '1') {
vgr += gr[i];
vgr %= (cnt + 1);
}
}
}
if (cnt > 1) {
for (i64 i = 0; i < n; i++) {
if (i == 0) {
le[i] = 1 % (cnt - 1);
continue;
}
le[i] = (le[i - 1] * 2) % (cnt - 1);
}
for (i64 i = 0; i < n; i++) {
if (x[i] == '1') {
vle += le[i];
vle %= (cnt - 1);
}
}
}
vector<i64> R;
for (i64 i = 0; i < n; i++) {
if (x[i] == '1') {
if (cnt == 1) {
R.push_back(0);
continue;
}
i64 r = vle;
i64 m = cnt - 1;
r = (r - le[i] + m) % m;
R.push_back(simul(r, m));
} else {
i64 r = vgr;
i64 m = cnt + 1;
r = (r + gr[i]) % m;
R.push_back(simul(r, m));
}
}
reverse(R.begin(), R.end());
stringstream ss;
for (auto t : R) {
ss << t << endl;
}
cout << ss.str();
return 0;
}
| replace | 125 | 126 | 125 | 126 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.