結果
| 問題 | No.3831 01 flip |
| コンテスト | |
| ユーザー |
Luke02561
|
| 提出日時 | 2026-11-28 01:21:33 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 56 ms / 2,000 ms |
| + 632µs | |
| コード長 | 28,392 bytes |
| 記録 | |
| コンパイル時間 | 3,587 ms |
| コンパイル使用メモリ | 370,564 KB |
| 実行使用メモリ | 11,508 KB |
| 最終ジャッジ日時 | 2026-11-28 01:21:39 |
| 合計ジャッジ時間 | 5,736 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge6_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 |
ソースコード
// Library reference: https://github.com/Luke256/cp-template
// meow (^・ω・^)
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define ALL(x) x.begin(), x.end()
#define BACK(x) x.rbegin(), x.rend()
#define MOD1 1000000007
#define MOD1_BASE 131
#define MOD2 998244353
#define INF (LLONG_MAX / 2)
#define FLOAT_ANS fixed << setprecision(20)
#define TORAD(x) (x * acosl(-1) / 180.0)
#define TODEG(x) (x * 180.0 / acosl(-1))
using namespace std;
using ll = long long;
using LL = __int128_t;
using ull = unsigned long long;
using ld = long double;
template <class T> concept ArrayLike = requires(const T& x, size_t i, size_t j) { { ranges::size(x) } -> convertible_to<size_t>; { x[i] == x[j] } -> convertible_to<bool>; };
template <class T, class U> concept ArrayLikeT = ArrayLike<T> && is_same_v<ranges::range_value_t<T>, U>;
template <typename T> using p_que = priority_queue<T, vector<T>, greater<T>>;
struct Condition {
bool state;
constexpr Condition(bool v) noexcept : state(v) {}
constexpr explicit operator bool() const { return state; }
template <class F> constexpr Condition then(F&& func) const noexcept(is_nothrow_invocable_v<F>) { if (state) { invoke(forward<F>(func)); } return *this; }
template <class F> constexpr Condition otherwise(F&& func) const noexcept(is_nothrow_invocable_v<F>) { if (!state) { invoke(forward<F>(func)); } return *this; }
};
template <typename T> Condition chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template <typename T> Condition chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
inline auto range(size_t n) { return views::iota(0u, n); }
template <typename T> void RotateVec2(vector<vector<T>>& v) { ll h = v.size(); ll w = v[0].size(); vector<vector<T>> t(w, vector<T>(h)); rep(i, h) { rep(j, w) { t[j][h - i - 1] = v[i][j]; } } v = t; }
template <class T> bool InRange(T x, T mn, T mx) { return (mn <= x && x <= mx); }
template <typename T> vector<T>& merged(vector<T>& a, vector<T>& b) { vector<T> res; merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; }
inline ll popcount(ll x) { ll res = 0; while (x) { res += x % 2; x >>= 1; } return res; }
inline ll isqrt(ll n) { if (n <= 0) return 0; ll x = sqrt(n); while ((x + 1) * (x + 1) <= n) x++; while (x * x > n) x--; return x; }
template <class T, class U> ostream& operator<<(ostream& stream, const pair<T, U>& p);
template <class T> ostream& operator<<(ostream& stream, const vector<T>& a);
template <class T> ostream& operator<<(ostream& stream, const deque<T>& a);
template <class T> ostream& operator<<(ostream& stream, const set<T>& a);
template <class K, class V> ostream& operator<<(ostream& stream, const map<K, V>& a);
template <class T, class U> istream& operator>>(istream& stream, pair<T, U>& a);
template <class T> istream& operator>>(istream& stream, vector<T>& a);
template <class C, class U> concept EmplaceBackSink = requires(C& c, U&& x) { c.emplace_back(forward<U>(x)); };
template <class C, class U> concept EmplaceSink = requires(C& c, U&& x) { c.emplace(forward<U>(x)); };
template <class C, class U> concept PushBackSink = requires(C& c, U&& x) { c.push_back(forward<U>(x)); };
template <class C, class U> concept PushSink = requires(C& c, U&& x) { c.push(forward<U>(x)); };
template <class C, class U> concept InsertSink = requires(C& c, U&& x) { c.insert(forward<U>(x)); };
template <class C, class U> concept Sink = EmplaceBackSink<C, U> || EmplaceSink<C, U> || PushBackSink<C, U> || PushSink<C, U> || InsertSink<C, U>;
template <class T> concept Readable = requires(istream& is, T& x) { is >> x; };
template <class C, class U> requires Sink<C, U> constexpr C& put(C& c, U&& x) { if constexpr (EmplaceBackSink<C, U>) c.emplace_back(forward<U>(x)); else if constexpr (EmplaceSink<C, U>) c.emplace(forward<U>(x)); else if constexpr (PushBackSink<C, U>) c.push_back(forward<U>(x)); else if constexpr (PushSink<C, U>) c.push(forward<U>(x)); else c.insert(forward<U>(x)); return c; }
template <class C, class U> requires Sink<C, U> C& operator<<(C& c, U&& x) { return put(c, forward<U>(x)); }
template <class C, ranges::input_range R> requires Sink<C, ranges::range_reference_t<R>> void operator<<=(C& c, R&& r) { for (auto&& x : r) c << std::forward<decltype(x)>(x); }
template <class T, class U> ostream& operator<<(ostream& stream, const pair<T, U>& p) { stream << "( " << p.first << ", " << p.second << " )"; return stream; }
template <class T> ostream& operator<<(ostream& stream, const vector<T>& a) { stream << "{ "; for (const T& i : a) stream << i << " "; stream << "}"; return stream; }
template <class T> ostream& operator<<(ostream& stream, const deque<T>& a) { stream << "{ "; for (const T& i : a) stream << i << " "; stream << "}"; return stream; }
template <class T> ostream& operator<<(ostream& stream, const set<T>& a) { stream << "{ "; for (const T& i : a) stream << i << " "; stream << "}"; return stream; }
template <class K, class V> ostream& operator<<(ostream& stream, const map<K, V>& a) { stream << "{ "; for (const auto& [k, v] : a) stream << "{ " << k << " : " << v << " } "; stream << "}"; return stream; }
template <class T, class U> istream& operator>>(istream& stream, pair<T, U>& a) { stream >> a.first >> a.second; return stream; }
template <class T> istream& operator>>(istream& stream, vector<T>& a) { for (auto& i : a) stream >> i; return stream; }
template <class... Args> struct PredictedInput {
tuple<Args...> args;
template <class T> requires Readable<T> && constructible_from<T, Args...> operator T() && { T x = apply( [](auto&&... args) -> T { return T(forward<decltype(args)>(args)...); }, move(args)); cin >> x; return x; }
};
template <class... Args> auto in(Args&&... args) { return PredictedInput<decay_t<Args>...>{{forward<Args>(args)...}}; }
template <class T> T in() { T x; cin >> x; return x; }
template <class T, class U, class... Ts> auto in() { tuple<T, U, Ts...> x; apply( [](auto&... xs) { (cin >> ... >> xs); }, x); return x; }
template <ArrayLike T> vector<size_t> ZAlgorithm(const T& x) { const size_t n = ranges::size(x); if (n == 0) return {}; vector<size_t> z(n); z[0] = n; size_t l = 0, r = 0; for (size_t i = 1; i < n; ++i) { if (i < r) z[i] = min(r - i, z[i - l]); while (i + z[i] < n && x[z[i]] == x[i + z[i]]) ++z[i]; if (i + z[i] > r) l = i, r = i + z[i]; } return z; }
template <class T = char> struct AhoCorasick {
struct Node { unordered_map<T, size_t> next; size_t fail = 0; bool matches = false; };
vector<Node> nodes;
AhoCorasick(initializer_list<string_view> patterns) requires same_as<T, char> : AhoCorasick(views::all(patterns)) {}
AhoCorasick(initializer_list<initializer_list<T>> patterns) requires(!same_as<T, char>) : AhoCorasick(views::all(patterns)) {}
template <ranges::input_range R> AhoCorasick(R&& patterns) : nodes(1) { for (const auto& pattern : patterns) { size_t node = 0; for (const T& ch : [&] { if constexpr (same_as<T, char> && convertible_to<decltype(pattern), string_view>) return string_view(pattern); else return views::all(pattern); }()) { const auto [it, inserted] = nodes[node].next.try_emplace(ch, nodes.size()); node = it->second; if (inserted) nodes.emplace_back(); } nodes[node].matches = true; } queue<size_t> q; for (const auto& [ch, node] : nodes[0].next) { nodes[node].matches |= nodes[0].matches; q.push(node); } while (!q.empty()) { const size_t node = q.front(); q.pop(); for (const auto& [ch, next] : nodes[node].next) { const size_t fail = step(nodes[node].fail, ch); nodes[next].fail = fail; nodes[next].matches |= nodes[fail].matches; q.push(next); } } }
bool match(string_view text) const requires same_as<T, char> { return match(ranges::subrange(text.begin(), text.end())); }
template <ranges::input_range R> requires(!same_as<T, char> || !convertible_to<R, string_view>) bool match(R&& text) const { if (nodes[0].matches) return true; size_t node = 0; for (const T& ch : text) { node = step(node, ch); if (nodes[node].matches) return true; } return false; }
size_t step(size_t node, const T& ch) const { while (true) { const auto it = nodes[node].next.find(ch); if (it != nodes[node].next.end()) return it->second; if (!node) return 0; node = nodes[node].fail; } }
};
template <class T> AhoCorasick(initializer_list<initializer_list<T>>) -> AhoCorasick<T>;
template <ranges::input_range R> requires convertible_to<ranges::range_reference_t<R>, string_view> AhoCorasick(R&&) -> AhoCorasick<char>;
template <ranges::input_range R> requires(!convertible_to<ranges::range_reference_t<R>, string_view>) AhoCorasick(R&&) -> AhoCorasick<ranges::range_value_t<ranges::range_reference_t<R>>>;
struct UnionFind {
vector<ll> tree;
UnionFind(ll x) : tree(x, -1) {}
ll root(ll x) { if (tree[x] < 0) return x; return tree[x] = root(tree[x]); }
bool same(ll x, ll y) { return root(x) == root(y); }
ll size(ll x) { return -tree[root(x)]; }
void unite(ll x, ll y) { x = root(x), y = root(y); if (x == y) return; if (size(x) < size(y)) swap(x, y); tree[x] += tree[y]; tree[y] = x; }
};
template <class Monoid, class Action = Monoid> struct SegTree {
using MergeFunc = Monoid (*)(Monoid, Monoid);
using ApplyFunc = Monoid (*)(Monoid, Action);
ull n; vector<Monoid> tree; Monoid e; MergeFunc merge; ApplyFunc apply;
SegTree(ull n_, MergeFunc mf, Monoid e_, ApplyFunc af) : e(e_), merge(mf), apply(af) { ull s = 1; while (s < n_) s <<= 1; n = s; tree.assign(n * 2, e); }
Monoid query(ull l, ull r) { return query(l, r, 0, 0, n); }
void update(ull idx, Action x) { idx += n - 1; tree[idx] = apply(tree[idx], x); while (idx) { idx = (idx - 1) / 2; tree[idx] = merge(tree[idx * 2 + 1], tree[idx * 2 + 2]); } }
Monoid get(ull i) { return query(i, i + 1); }
private:
Monoid query(ull ql, ull qr, ull pos, ull rl, ull rr) { if (qr <= rl || rr <= ql) return e; if (ql <= rl && rr <= qr) return tree[pos]; return merge(query(ql, qr, pos * 2 + 1, rl, (rl + rr) >> 1), query(ql, qr, pos * 2 + 2, (rl + rr) >> 1, rr)); }
};
template <class Monoid, class Action, auto merge, auto apply, auto comp> struct LazySegTree {
static_assert(std::is_convertible_v<decltype(merge), std::function<Monoid(Monoid, Monoid)>>, "merge must work as Monoid(Monoid, Monoid)");
static_assert(std::is_convertible_v<decltype(apply), std::function<Monoid(Monoid, Action)>>, "mapping must work as Monoid(Monoid, Action)");
static_assert(std::is_convertible_v<decltype(comp), std::function<Action(Action, Action)>>, "composition must work as Action(Action, Action)");
ull n; vector<Monoid> tree; vector<Action> lazy; Monoid e; Action id;
LazySegTree(ull n_, Monoid e_, Action id_, const vector<Monoid>& initial_values = {}) : e(e_), id(id_) { ull s = 1; while (s < n_) s <<= 1; n = s; tree.assign(n * 2, e); lazy.assign(n * 2, id); for (ull i = 0; i < initial_values.size(); i++) { tree[i + n - 1] = initial_values[i]; } for (ull i = n - 2; i < n - 1; i--) { tree[i] = merge(tree[(i << 1) + 1], tree[(i << 1) + 2]); } }
Monoid query(ull l, ull r) { return query(l, r, 0, 0, n); }
void update(ull idx, Action x) { update(idx, idx + 1, x); }
void update(ull l, ull r, Action x) { update(l, r, 0, 0, n, x); }
Monoid get(ull i) { return query(i, i + 1); }
void clear() { fill(tree.begin(), tree.end(), e); fill(lazy.begin(), lazy.end(), id); }
void flush() { for (int i = 0; i < n * 2; i++) { fetch(i); } }
const Monoid& operator[](ull idx) { return tree[idx + n - 1]; }
private:
void fetch(ull pos) { if (lazy[pos] == id) return; if (pos < n - 1) { lazy[(pos << 1) + 1] = comp(lazy[(pos << 1) + 1], lazy[pos]); lazy[(pos << 1) + 2] = comp(lazy[(pos << 1) + 2], lazy[pos]); } tree[pos] = apply(tree[pos], lazy[pos]); lazy[pos] = id; }
Monoid query(ull ql, ull qr, ull pos, ull rl, ull rr) { if (rr <= ql || qr <= rl) return e; fetch(pos); if (ql <= rl && rr <= qr) return tree[pos]; return merge(query(ql, qr, (pos << 1) + 1, rl, (rl + rr) >> 1), query(ql, qr, (pos << 1) + 2, (rl + rr) >> 1, rr)); }
void update(ull ql, ull qr, ull pos, ull rl, ull rr, Action x) { fetch(pos); if (rr <= ql || qr <= rl) return; if (ql <= rl && rr <= qr) { lazy[pos] = comp(lazy[pos], x); fetch(pos); return; } update(ql, qr, (pos << 1) + 1, rl, (rl + rr) >> 1, x); update(ql, qr, (pos << 1) + 2, (rl + rr) >> 1, rr, x); tree[pos] = merge(tree[(pos << 1) + 1], tree[(pos << 1) + 2]); }
};
template <std::uint_fast64_t Modulus> class modint {
using u64 = std::uint_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus) + Modulus) % Modulus) {}
constexpr u64& value() noexcept { return a; }
constexpr const u64& value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; }
constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; }
constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; }
constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; }
constexpr modint& operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; }
constexpr modint& operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; }
constexpr modint& operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; }
constexpr modint& operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; }
constexpr modint pow(u64 exp) noexcept { modint res = 1, rhs = *this; while (exp) { if (exp & 1) { res *= rhs; } rhs *= rhs; exp >>= 1; } return res; }
constexpr modint& operator=(u64 x) { a = x % Modulus; return *this; }
constexpr bool operator==(const modint x) const { return a == x.a; }
friend istream& operator>>(istream& stream, modint<Modulus>& a) { u64 x; stream >> x; a = ((x % Modulus) + Modulus) % Modulus; return stream; }
friend ostream& operator<<(ostream& stream, const modint<Modulus> a) { stream << a.a; return stream; }
};
inline ll fastpow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; }
inline ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = (res * (a % mod)) % mod; a = ((a % mod) * (a % mod)) % mod; n >>= 1; } return res; }
inline void FFT998(vector<modint<MOD2>>& a, bool invert = false) { size_t n = a.size(); for (size_t i = 1, j = 0; i < n; i++) { size_t bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (size_t len = 2; len <= n; len <<= 1) { modint<MOD2> wlen = modpow(3, (MOD2 - 1) / len, MOD2); if (invert) wlen = wlen.pow(MOD2 - 2); for (size_t i = 0; i < n; i += len) { modint<MOD2> w = 1; for (size_t j = 0; j < len / 2; j++) { modint<MOD2> u = a[i + j], v = a[i + j + len / 2] * w; a[i + j] = u + v; a[i + j + len / 2] = u - v; w *= wlen; } } } if (invert) { modint<MOD2> n_inv = modpow(n, MOD2 - 2, MOD2); for (modint<MOD2>& x : a) x *= n_inv; } }
inline vector<modint<MOD2>> Convolution998(vector<modint<MOD2>> a, vector<modint<MOD2>> b) { size_t n = 1; size_t m = a.size() + b.size() - 1; while (n < m) n <<= 1; a.resize(n); b.resize(n); FFT998(a); FFT998(b); for (size_t i = 0; i < n; i++) a[i] *= b[i]; FFT998(a, true); a.resize(m); return a; }
namespace Geometry2D {
template <class T = ll> struct Vector2D {
T x, y;
Vector2D() : x(0), y(0) {}
Vector2D(T x_, T y_) : x(x_), y(y_) {}
long double length() const { return sqrt((long double)x * x + y * y); };
T lengthp() const { return x * x + y * y; };
bool inrange(const Vector2D a, const Vector2D b) const { return (InRange(x, a.x, b.x) and InRange(y, a.y, b.y)); }
Vector2D yx() const { return Vector2D{y, x}; }
Vector2D rotated90() const { return Vector2D{-y, x}; }
Vector2D normalized() const { return *this / length(); }
T cross(const Vector2D a) const { return x * a.y - y * a.x; }
Vector2D operator-(const Vector2D a) const { return Vector2D(*this) -= a; }
Vector2D operator+(const Vector2D a) const { return Vector2D(*this) += a; }
T operator*(const Vector2D a) const { return x * a.x + y * a.y; }
Vector2D operator*(const T a) const { return Vector2D(*this) *= a; }
Vector2D operator/(const T a) const { return Vector2D(*this) /= a; }
Vector2D& operator+=(const Vector2D a) { x += a.x; y += a.y; return *this; }
Vector2D& operator-=(const Vector2D a) { x -= a.x; y -= a.y; return *this; }
Vector2D& operator-=(const T a) { x -= a; y -= a; return *this; }
Vector2D& operator*=(const T a) { x *= a; y *= a; return *this; }
Vector2D& operator/=(const T a) { x /= a; y /= a; return *this; }
bool operator==(const Vector2D a) const { return (x == a.x and y == a.y); }
bool operator!=(const Vector2D a) const { return not(x == a.x and y == a.y); }
bool operator>(const Vector2D a) const { return a < *this; }
bool operator<(const Vector2D a) const { return make_pair(x, y) < make_pair(a.x, a.y); }
friend ostream& operator<<(ostream& stream, const Vector2D<T>& x) { stream << "(" << x.x << ", " << x.y << ")"; return stream; }
friend istream& operator>>(istream& stream, Vector2D<T>& x) { stream >> x.x >> x.y; return stream; }
};
using Scalar = long double;
using Vec2 = Vector2D<Scalar>;
constexpr Scalar EPS = 1e-12L;
inline bool InRange(Scalar x, Scalar a, Scalar b) { return min(a, b) - EPS <= x and x <= max(a, b) + EPS; }
inline int Sign(Scalar x) { if (x > EPS) return 1; if (x < -EPS) return -1; return 0; }
struct Line {
Vec2 p1, p2, d;
Line(Vec2 p1, Vec2 p2) : p1(p1), p2(p2), d(p2 - p1) {}
bool onSegment(const Vec2& p) const { return Sign(d.cross(p - p1)) == 0 and InRange(p.x, p1.x, p2.x) and InRange(p.y, p1.y, p2.y); }
bool segIntersects(const Line& line) const { int a = Sign(d.cross(line.p1 - p1)); int b = Sign(d.cross(line.p2 - p1)); int c = Sign(line.d.cross(p1 - line.p1)); int d = Sign(line.d.cross(p2 - line.p1)); return (a * b < 0 and c * d < 0) or (a == 0 and onSegment(line.p1)) or (b == 0 and onSegment(line.p2)) or (c == 0 and line.onSegment(p1)) or (d == 0 and line.onSegment(p2)); }
bool lineIntersects(const Line& line) const { return Sign(d.cross(line.d)) != 0 or Sign(d.cross(line.p1 - p1)) == 0; }
Vec2 intersectsAt(const Line& line) const { if (!lineIntersects(line) or Sign(d.cross(line.d)) == 0) return Vec2{}; return p1 + d * (line.p1 - p1).cross(line.d) / d.cross(line.d); }
Vec2 projected(const Vec2& p) const { return Sign(d.lengthp()) ? p1 + d * ((p - p1) * d) / d.lengthp() : p1; }
Scalar lineDistance(const Vec2& p) const { return (p - projected(p)).length(); }
Scalar lineDistance(const Line& l) const { return lineIntersects(l) ? 0 : lineDistance(l.p1); }
Scalar segDistance(const Vec2& p) const { Scalar t = (p - p1) * d / d.lengthp(); if (t <= 0) return (p - p1).length(); else if (t >= 1) return (p - p2).length(); else return (p1 + d * t - p).length(); }
Scalar segDistance(const Line& l) const { return segIntersects(l) ? 0 : min({segDistance(l.p1), segDistance(l.p2), l.segDistance(p1), l.segDistance(p2)}); }
};
struct Polygon {
vector<Vec2> points; size_t n;
Polygon(const vector<Vec2>& points) : points(points), n(points.size()) {}
const Vec2& vertex(size_t idx) const { return points[idx % n]; }
Vec2& vertex(size_t idx) { return points[idx % n]; }
Scalar Area() const { Scalar area = 0; for (size_t i = 0; i < n; i++) { area += vertex(i).cross(vertex(i + 1)); } return area * Sign(area) / 2; }
bool IsConvex() const { for (size_t i = 0; i < n; i++) if ((vertex(i) - vertex(i + 1)).cross(vertex(i + 1) - vertex(i + 2)) < 0) return false; return true; }
bool Contains(const Vec2& p) { Scalar acc = 0; for (size_t i = 0; i < n; i++) { Vec2 a = vertex(i) - p; Vec2 b = vertex(i + 1) - p; if (Sign(a.cross(b)) == 0 and (a * b) <= EPS) return true; acc += atan2(a.cross(b), a * b); } return abs(acc) > acosl(-1L); }
bool OnBoundary(const Vec2& p) const { for (size_t i = 0; i < n; i++) { if (Line(vertex(i), vertex(i + 1)).onSegment(p)) return true; } return false; }
Scalar Diameter() const { if (n == 2) return (points[0] - points[1]).length(); Scalar res = 0; size_t i = 0, j = 1; while (i <= n) { res = max(res, (vertex(i) - vertex(j)).length()); if (Sign((vertex(i + 1) - vertex(i)).cross(vertex(j + 1) - vertex(j))) > 0) j++; else i++; } return res; }
pair<Polygon, Polygon> ConvexCut(const Line& line) const { vector<Vec2> left, right; for (size_t i = 0; i < n; i++) { const Vec2& a = vertex(i); const Vec2& b = vertex(i + 1); int sa = Sign(line.d.cross(a - line.p1)); int sb = Sign(line.d.cross(b - line.p1)); if (sa >= 0) left.push_back(a); if (sa <= 0) right.push_back(a); if (sa * sb < 0) left.push_back(line.intersectsAt(Line(a, b))), right.push_back(line.intersectsAt(Line(a, b))); } return {Polygon(left), Polygon(right)}; }
};
inline Polygon ConvexHull(vector<Vec2> points) { if (points.size() < 3) return Polygon{{}}; sort(points.begin(), points.end(), [](Vec2 a, Vec2 b) { if (Sign(a.y - b.y)) return Sign(a.y - b.y) < 0; return Sign(a.x - b.x) < 0; }); points.erase(unique(ALL(points)), points.end()); auto half = [](auto begin, auto end) { vector<Vec2> p(begin, begin + 2); for (auto itr = begin + 2; itr != end; itr++) { while (p.size() >= 2 and Sign((*itr - p.back()).cross(p[p.size() - 2] - p.back())) < 0) p.pop_back(); p.push_back(*itr); } p.pop_back(); return p; }; auto lower = half(points.begin(), points.end()), upper = half(points.rbegin(), points.rend()); lower.insert(lower.end(), upper.begin(), upper.end()); return Polygon{lower}; }
}
template <class T> struct Grid {
vector<T> data; vector<ull> shape;
Grid() {}
Grid(ull h, ull w, T x = T()) : data(h * w, x), shape({h, w}) {}
Grid(initializer_list<ull> shape_, T x = T()) : data(accumulate(shape_.begin(), shape_.end(), 1, multiplies<ull>()), x), shape(shape_) {}
T& operator[](ull i) { return data.data()[i]; }
template <class Container, class = decltype(begin(declval<Container>()))> T& operator[](const Container& idx) { ull i = 0; for (ull j = 0; j < shape.size(); ++j) { i = i * shape.data()[j] + idx[j]; } return data.data()[i]; }
template <class U> T& operator[](initializer_list<U> idx) { ull i = 0; auto it = idx.begin(); for (ull j = 0; j < shape.size(); ++j, ++it) { i = i * shape.data()[j] + *it; } return data.data()[i]; }
template <class U> T& operator[](const Geometry2D::Vector2D<U>& idx) { return data.data()[idx.x * shape.data()[1] + idx.y]; }
template <class... Args> T& operator()(Args... args) { ull idxs[] = {static_cast<ull>(args)...}; ull i = 0; for (ull j = 0; j < sizeof...(Args); ++j) { i = i * shape.data()[j] + idxs[j]; } return data[i]; }
friend istream& operator>>(istream& stream, Grid<T>& grid) { for (auto& i : grid.data) stream >> i; return stream; }
};
#define __DEBUGGER_OUTPUT cerr
namespace Debugger {
class TableManager {
struct Table { std::vector<std::string> columns; std::vector<std::vector<std::string>> rows; };
std::map<int, Table> tables;
static TableManager& getInstance() { static TableManager instance; return instance; }
public:
static void addRow(int line, const std::vector<std::string>& header, const std::vector<std::string>& row) { auto& table = getInstance().tables[line]; if (table.columns.empty()) { table.columns = header; } table.rows.push_back(row); }
static void flush() { auto& instance = getInstance(); for (const auto& [line, table] : instance.tables) { vector<size_t> colWidths(table.columns.size(), 0); size_t width = 0; for (size_t i = 0; i < table.columns.size(); ++i) colWidths[i] = table.columns[i].size(); for (const auto& row : table.rows) for (size_t i = 0; i < row.size(); ++i) colWidths[i] = std::max(colWidths[i], row[i].size()); for (size_t i = 0; i < colWidths.size(); ++i) width += colWidths[i] + 2; __DEBUGGER_OUTPUT << std::left << "[DEBUG TABLE] L" << line << "\n"; for (size_t i = 0; i < table.columns.size(); ++i) { __DEBUGGER_OUTPUT << '+' << std::string(colWidths[i] + 2, '-') << (i + 1 < table.columns.size() ? "" : "+\n"); } for (size_t i = 0; i < table.columns.size(); ++i) { __DEBUGGER_OUTPUT << "| " << std::setw(colWidths[i]) << table.columns[i] << (i + 1 < table.columns.size() ? " " : " |\n"); } for (size_t i = 0; i < table.columns.size(); ++i) { __DEBUGGER_OUTPUT << '+' << std::string(colWidths[i] + 2, '-') << (i + 1 < table.columns.size() ? "" : "+\n"); } for (const auto& row : table.rows) { __DEBUGGER_OUTPUT << "| "; for (size_t i = 0; i < row.size(); ++i) __DEBUGGER_OUTPUT << std::setw(colWidths[i]) << row[i] << (i + 1 < row.size() ? " | " : " |\n"); } for (size_t i = 0; i < table.columns.size(); ++i) { __DEBUGGER_OUTPUT << '+' << std::string(colWidths[i] + 2, '-') << (i + 1 < table.columns.size() ? "" : "+\n"); } } __DEBUGGER_OUTPUT.flush(); instance.tables.clear(); }
~TableManager() { flush(); }
};
template <class T> std::string toStr(const T& value) { std::ostringstream oss; oss << value; return oss.str(); }
inline std::string strip(const std::string& str) { size_t start = str.find_first_not_of(" \t\n\r"); size_t end = str.find_last_not_of(" \t\n\r"); return (start == std::string::npos) ? "" : str.substr(start, end - start + 1); }
inline std::vector<std::string> split(const std::string& str) { std::vector<std::string> res; size_t depth = 0, start = 0; for (size_t i = 0; i < str.size(); ++i) { if (str[i] == ',' && depth == 0) { res.push_back(strip(str.substr(start, i - start))); start = i + 1; } else if (str[i] == '(' || str[i] == '[' || str[i] == '{' || str[i] == '<') depth++; else if (str[i] == ')' || str[i] == ']' || str[i] == '}' || str[i] == '>') depth--; } res.push_back(strip(str.substr(start))); return res; }
template <class... Args> void tdebug_helper(int line, const char* names, const Args&... args) { std::vector<std::string> nameList = split(names); std::vector<std::string> valueList = {toStr(args)...}; TableManager::addRow(line, nameList, valueList); }
template <class... Args> void debug_impl(int line, const char* names, const Args&... args) { __DEBUGGER_OUTPUT << "[DEBUG] L" << line << ": "; auto nameList = split(names); size_t i = 0; ((__DEBUGGER_OUTPUT << (i++ ? ", " : "") << nameList[i - 1] << " = " << args), ...); __DEBUGGER_OUTPUT << endl; }
}
#define debug(...) Debugger::debug_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#define tdebug(...) Debugger::tdebug_helper(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#define tflush() Debugger::TableManager::flush();
using namespace std;
using mint = modint<MOD2>;
using Vec2 = Geometry2D::Vector2D<ll>;
const Vec2 Angles[] = {{0,1}, {-1,0}, {0,-1}, {1,0}};
int main () {
auto [n, s, a] = in<ll,string,ll>();
ll m = n - n / 2;
ll upper = 0, lower = 0;
rep(i, n) {
if (i < n / 2) {
upper <<= 1;
upper += s[i] - '0';
}
else {
lower <<= 1;
lower += s[i] - '0';
}
}
// debug(upper, lower);
ll ans = INF;
vector<ll>lower_mods(min(a,1ll<<m), INF);
rep(target, 1ll << m) {
chmin(lower_mods[target % a], popcount(target ^ lower));
if (target != 0 and target % a == 0) {
chmin(ans, popcount(upper) + popcount(target ^ lower));
}
}
rep (target, 1ll << n / 2) {
if (target == 0) continue;
ll k = target << m;
if ((a - k % a) % a < lower_mods.size())
{
chmin(ans, lower_mods[(a - k % a) % a] + popcount(target ^ upper));
}
// .then([&]{
// debug(lower, upper, bitset<10>(target), m);
// });
}
if (ans == INF) cout << -1 << endl;
else cout << ans << endl;
}
Luke02561