From 90c9c7026003bd8ebada8232e30fa23bdb7a62f8 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 16 Feb 2025 17:24:20 -0500 Subject: [PATCH] 938 g --- codeforces/938/.clang-format | 9 ++ codeforces/938/.clangd | 8 ++ codeforces/938/a.cc | 99 +++++++++++++ codeforces/938/a.in | 5 + codeforces/938/a.out | 7 + codeforces/938/b.cc | 219 ++++++++++++++++++++++++++++ codeforces/938/b.in | 11 ++ codeforces/938/b.out | 12 ++ codeforces/938/c.cc | 127 ++++++++++++++++ codeforces/938/c.in | 13 ++ codeforces/938/c.out | 9 ++ codeforces/938/compile_flags.txt | 5 + codeforces/938/d.cc | 230 +++++++++++++++++++++++++++++ codeforces/938/d.in | 16 +++ codeforces/938/d.out | 8 ++ codeforces/938/e.cc | 119 +++++++++++++++ codeforces/938/e.in | 11 ++ codeforces/938/e.out | 10 ++ codeforces/938/f.cc | 95 ++++++++++++ codeforces/938/f.in | 6 + codeforces/938/f.out | 8 ++ codeforces/938/g.cc | 240 +++++++++++++++++++++++++++++++ codeforces/938/g.in | 11 ++ codeforces/938/g.out | 6 + 24 files changed, 1284 insertions(+) create mode 100644 codeforces/938/.clang-format create mode 100644 codeforces/938/.clangd create mode 100644 codeforces/938/a.cc create mode 100644 codeforces/938/a.in create mode 100644 codeforces/938/a.out create mode 100644 codeforces/938/b.cc create mode 100644 codeforces/938/b.in create mode 100644 codeforces/938/b.out create mode 100644 codeforces/938/c.cc create mode 100644 codeforces/938/c.in create mode 100644 codeforces/938/c.out create mode 100644 codeforces/938/compile_flags.txt create mode 100644 codeforces/938/d.cc create mode 100644 codeforces/938/d.in create mode 100644 codeforces/938/d.out create mode 100644 codeforces/938/e.cc create mode 100644 codeforces/938/e.in create mode 100644 codeforces/938/e.out create mode 100644 codeforces/938/f.cc create mode 100644 codeforces/938/f.in create mode 100644 codeforces/938/f.out create mode 100644 codeforces/938/g.cc create mode 100644 codeforces/938/g.in create mode 100644 codeforces/938/g.out diff --git a/codeforces/938/.clang-format b/codeforces/938/.clang-format new file mode 100644 index 0000000..e7350c4 --- /dev/null +++ b/codeforces/938/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: Google +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: false +AllowShortLoopsOnASingleLine: false diff --git a/codeforces/938/.clangd b/codeforces/938/.clangd new file mode 100644 index 0000000..4fab872 --- /dev/null +++ b/codeforces/938/.clangd @@ -0,0 +1,8 @@ +CompileFlags: + Add: + - -std=c++23 + - -Wall + - -Wextra + - -Wpedantic + - -Wshadow + - -Wno-unknown-pragmas \ No newline at end of file diff --git a/codeforces/938/a.cc b/codeforces/938/a.cc new file mode 100644 index 0000000..93c15a0 --- /dev/null +++ b/codeforces/938/a.cc @@ -0,0 +1,99 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto &&x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto &&x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args &&...args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args &&...args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const &t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n, a, b; + cin >> n >> a >> b; + + int ans = MAX; + for (int i = 0; i <= 100; ++i) { + for (int j = 0; j <= 100; ++j) { + int spent = i * a + j * b; + int bought = i + 2 * j; + if (bought == n) { + ans = min(ans, spent); + } + } + } + + prln("{}", ans); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/a.in b/codeforces/938/a.in new file mode 100644 index 0000000..9db5ef7 --- /dev/null +++ b/codeforces/938/a.in @@ -0,0 +1,5 @@ +4 +2 5 9 +3 5 9 +3 5 11 +4 5 11 diff --git a/codeforces/938/a.out b/codeforces/938/a.out new file mode 100644 index 0000000..7a8fc31 --- /dev/null +++ b/codeforces/938/a.out @@ -0,0 +1,7 @@ +9 +14 +15 +20 + +[code]: 0 +[time]: 4.37689 ms \ No newline at end of file diff --git a/codeforces/938/b.cc b/codeforces/938/b.cc new file mode 100644 index 0000000..0aa3d7f --- /dev/null +++ b/codeforces/938/b.cc @@ -0,0 +1,219 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto &&x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto &&x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args &&...args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args &&...args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const &t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multiset = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +void solve() { + int n, c, d; + cin >> n >> c >> d; + + hashtable b; + int first = MAX; + for (int i = 0; i < n * n; ++i) { + int x; + cin >> x; + first = min(first, x); + ++b[x]; + } + + int col = first - c; + + for (int i = 0; i < n; ++i) { + if (b[col + c] == 0) { + prln("NO"); + return; + } + col += c; + --b[col]; + int last = col; + for (int j = 0; j < n - 1; ++j) { + int next = last + d; + if (b[next] == 0) { + prln("NO"); + return; + } + --b[next]; + last = next; + } + if (i == n - 1) + break; + } + + prln("YES"); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/b.in b/codeforces/938/b.in new file mode 100644 index 0000000..376f2ed --- /dev/null +++ b/codeforces/938/b.in @@ -0,0 +1,11 @@ +5 +3 2 3 +3 9 6 5 7 1 10 4 8 +3 2 3 +3 9 6 5 7 1 11 4 8 +2 100 100 +400 300 400 500 +3 2 3 +3 9 6 6 5 1 11 4 8 +4 4 4 +15 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15 diff --git a/codeforces/938/b.out b/codeforces/938/b.out new file mode 100644 index 0000000..a056cdc --- /dev/null +++ b/codeforces/938/b.out @@ -0,0 +1,12 @@ +NO +YES +YES +NO +NO + +[code]: 0 +[code]: 0 +[time]: 6.98614 ms +[code]: 0 +[time]: 2.65217 ms +[time]: 3.42894 ms \ No newline at end of file diff --git a/codeforces/938/c.cc b/codeforces/938/c.cc new file mode 100644 index 0000000..875a61d --- /dev/null +++ b/codeforces/938/c.cc @@ -0,0 +1,127 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args&&... args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args&&... args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const& t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + ll n, k; + cin >> n >> k; + ll ans = 0; + vec a(n); + for (auto& e : a) { + cin >> e; + } + + ll left = ceil(k / 2.0); + for (int i = 0; i < sz(a) && left > 0; ++i) { + int take = min(a[i], left); + a[i] -= take; + left -= take; + if (a[i] == 0) + ++ans; + } + + ll right = k / 2; + + for (int i = sz(a) - 1; i >= 0 && a[i] > 0 && right > 0; --i) { + int take = min(a[i], right); + a[i] -= take; + right -= take; + if (a[i] == 0) + ++ans; + } + + prln("{}", ans); + + // prln("{} left attacks and {} right", left, right); + // for (auto x : prefix) + // pr("{} ", x); + // prln(); + // for (auto x : postfix) + // pr("{} ", x); + // prln(); + + // ll left_sunk = distance(prefix.begin(), upper_bound(all(prefix), left)) - + // 1; ll right_sunk = + // distance(postfix.begin(), upper_bound(all(postfix), right)) - 1; + + // prln("{} {} {}", left_sunk, right_sunk, left_sunk + right_sunk); + // prln("{}", left_sunk + right_sunk); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/c.in b/codeforces/938/c.in new file mode 100644 index 0000000..b411a1f --- /dev/null +++ b/codeforces/938/c.in @@ -0,0 +1,13 @@ +6 +4 5 +1 2 4 3 +4 6 +1 2 4 3 +5 20 +2 7 1 8 2 +2 2 +3 2 +2 15 +1 5 +2 7 +5 2 diff --git a/codeforces/938/c.out b/codeforces/938/c.out new file mode 100644 index 0000000..3e92115 --- /dev/null +++ b/codeforces/938/c.out @@ -0,0 +1,9 @@ +2 +3 +5 +0 +2 +2 + +[code]: 0 +[time]: 11.8582 ms \ No newline at end of file diff --git a/codeforces/938/compile_flags.txt b/codeforces/938/compile_flags.txt new file mode 100644 index 0000000..0adea68 --- /dev/null +++ b/codeforces/938/compile_flags.txt @@ -0,0 +1,5 @@ +-std=c++23 +-Wall +-Wextra +-Wpedantic +-Wshadow diff --git a/codeforces/938/d.cc b/codeforces/938/d.cc new file mode 100644 index 0000000..1559412 --- /dev/null +++ b/codeforces/938/d.cc @@ -0,0 +1,230 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto &&x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto &&x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args &&...args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args &&...args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const &t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multiset = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +void solve() { + ll n, m, k; + + cin >> n >> m >> k; + + vec a(n); + hashtable to_match; + for (auto &e : a) + cin >> e; + for (int i = 0; i < m; ++i) { + ll x; + cin >> x; + ++to_match[x]; + } + + hashtable window; + ll match_count = 0; + ll ans = 0; + + for (int i = 0; i < m - 1; ++i) { + // prln("adding {} to starting iwndow", a[i]); + if (++window[a[i]] <= to_match[a[i]] && + to_match.find(a[i]) != to_match.end()) { + ++match_count; + } + } + + int l = 0; + for (int r = m - 1; r < n; ++r) { + // prln("adding {} to w", a[r]); + if (to_match.find(a[r]) != to_match.end() && + ++window[a[r]] <= to_match[a[r]]) { + ++match_count; + } + + // prln("{}", match_count); + + if (match_count >= k) { + ++ans; + } + + if (window.find(a[l]) != window.end() && --window[a[l]] < to_match[a[l]]) { + --match_count; + } + ++l; + } + + prln("{}", ans); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/d.in b/codeforces/938/d.in new file mode 100644 index 0000000..20a4135 --- /dev/null +++ b/codeforces/938/d.in @@ -0,0 +1,16 @@ +5 +7 4 2 +4 1 2 3 4 5 6 +1 2 3 4 +7 4 3 +4 1 2 3 4 5 6 +1 2 3 4 +7 4 4 +4 1 2 3 4 5 6 +1 2 3 4 +11 5 3 +9 9 2 2 10 9 7 6 3 6 3 +6 9 7 8 10 +4 1 1 +4 1 5 6 +6 diff --git a/codeforces/938/d.out b/codeforces/938/d.out new file mode 100644 index 0000000..1c47d71 --- /dev/null +++ b/codeforces/938/d.out @@ -0,0 +1,8 @@ +4 +3 +2 +4 +1 + +[code]: 0 +[time]: 4.46129 ms \ No newline at end of file diff --git a/codeforces/938/e.cc b/codeforces/938/e.cc new file mode 100644 index 0000000..4422cf1 --- /dev/null +++ b/codeforces/938/e.cc @@ -0,0 +1,119 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args&&... args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args&&... args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const& t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int n; + cin >> n; + string s; + cin >> s; + + int ans = 1; + + for (int k = sz(s); k >= 2; --k) { + string S = s; + vec toggle(S.size(), 0); + + int cumulative = 0; + for (int i = 0; i < sz(S) - k; ++i) { + cumulative += toggle[i]; + int actual = (S[i] - '0') ^ (cumulative & 1); + if (actual == 0) { + ++cumulative; + ++toggle[i + k]; + } + } + + int zeroes = 0; + for (int j = sz(S) - k; j < sz(S); ++j) { + cumulative += toggle[j]; + int actual = (S[j] - '0') ^ (cumulative & 1); + zeroes += actual == 0; + } + + if (zeroes == 0 || zeroes == k) { + ans = k; + break; + } + } + + prln("{}", ans); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/e.in b/codeforces/938/e.in new file mode 100644 index 0000000..1bf7644 --- /dev/null +++ b/codeforces/938/e.in @@ -0,0 +1,11 @@ +5 +5 +00100 +5 +01000 +7 +1011101 +3 +000 +2 +10 diff --git a/codeforces/938/e.out b/codeforces/938/e.out new file mode 100644 index 0000000..dd1d599 --- /dev/null +++ b/codeforces/938/e.out @@ -0,0 +1,10 @@ +3 +2 +4 +3 +1 + +[code]: 0 +[code]: 0 +[time]: 8.4703 ms +[time]: 2.82001 ms \ No newline at end of file diff --git a/codeforces/938/f.cc b/codeforces/938/f.cc new file mode 100644 index 0000000..d7eb099 --- /dev/null +++ b/codeforces/938/f.cc @@ -0,0 +1,95 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto&& x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto&& x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args&&... args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args&&... args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const& t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +void solve() { + int ones, twos, threes, fours; + cin >> ones >> twos >> threes >> fours; + + ll ans = 0; + + if (ones & 1 && twos & 1 && threes & 1) + ++ans; + + ans += ones / 2 + twos / 2 + threes / 2 + fours / 2; + + prln("{}", ans); +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/f.in b/codeforces/938/f.in new file mode 100644 index 0000000..accadf7 --- /dev/null +++ b/codeforces/938/f.in @@ -0,0 +1,6 @@ +5 +1 1 1 0 +1 0 1 2 +2 2 2 0 +3 3 2 0 +0 9 9 9 diff --git a/codeforces/938/f.out b/codeforces/938/f.out new file mode 100644 index 0000000..9389e8b --- /dev/null +++ b/codeforces/938/f.out @@ -0,0 +1,8 @@ +1 +1 +3 +3 +12 +31783 ms +[code]: 0 +[time]: 4.47631 ms \ No newline at end of file diff --git a/codeforces/938/g.cc b/codeforces/938/g.cc new file mode 100644 index 0000000..dca718c --- /dev/null +++ b/codeforces/938/g.cc @@ -0,0 +1,240 @@ +#include // {{{ + +// https://codeforces.com/blog/entry/96344 + +#pragma GCC optimize("O2,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +using namespace std; + +template +constexpr T MIN = std::numeric_limits::min(); + +template +constexpr T MAX = std::numeric_limits::max(); + +template +[[nodiscard]] static T sc(auto &&x) { + return static_cast(x); +} + +template +[[nodiscard]] static T sz(auto &&x) { + return static_cast(x.size()); +} + +template +void pr(std::format_string fmt, Args &&...args) { + std::print(fmt, std::forward(args)...); +} + +template +void pr(std::format_string fmt) { + std::print(fmt); +} + +template +void prln(std::format_string fmt, Args &&...args) { + std::println(fmt, std::forward(args)...); +} + +template +void prln(std::format_string fmt) { + std::println(fmt); +} + +void prln() { + std::println(); +} + +void prln(auto const &t) { + std::println("{}", t); +} + +using ll = long long; +using ld = long double; +template +using vec = std::vector; +template +using arr = std::array; + +#define ff first +#define ss second +#define eb emplace_back +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +// }}} + +#include +#include + +using namespace __gnu_pbds; + +// https://mirror.codeforces.com/blog/entry/124683 + +namespace hashing { +using i64 = std::int64_t; +using u64 = std::uint64_t; +static const u64 FIXED_RANDOM = + std::chrono::steady_clock::now().time_since_epoch().count(); + +#if USE_AES +std::mt19937 rd(FIXED_RANDOM); +const __m128i KEY1{(i64)rd(), (i64)rd()}; +const __m128i KEY2{(i64)rd(), (i64)rd()}; +#endif + +template +struct custom_hash {}; + +template +inline void hash_combine(u64 &seed, T const &v) { + custom_hash hasher; + seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 12) + (seed >> 4); +}; + +template +struct custom_hash::value>::type> { + u64 operator()(T _x) const { + u64 x = _x; +#if USE_AES + __m128i m{i64(u64(x) * 0xbf58476d1ce4e5b9u64), (i64)FIXED_RANDOM}; + __m128i y = _mm_aesenc_si128(m, KEY1); + __m128i z = _mm_aesenc_si128(y, KEY2); + return z[0]; +#else + x += 0x9e3779b97f4a7c15 + FIXED_RANDOM; + x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; + x = (x ^ (x >> 27)) * 0x94d049bb133111eb; + return x ^ (x >> 31); +#endif + } +}; + +template +struct custom_hash()))>> { + u64 operator()(T const &a) const { + u64 value = FIXED_RANDOM; + for (auto &x : a) + hash_combine(value, x); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(const std::tuple &a) const { + u64 value = FIXED_RANDOM; + std::apply( + [&value](T const &...args) { + (hash_combine(value, args), ...); + }, + a); + return value; + } +}; + +template +struct custom_hash> { + u64 operator()(std::pair const &a) const { + u64 value = FIXED_RANDOM; + hash_combine(value, a.first); + hash_combine(value, a.second); + return value; + } +}; +}; // namespace hashing + +#ifdef PB_DS_ASSOC_CNTNR_HPP +template +using hashtable = gp_hash_table< + Key, Value, hashing::custom_hash, std::equal_to, + direct_mask_range_hashing<>, linear_probe_fn<>, + hash_standard_resize_policy, + hash_load_check_resize_trigger<>, true>>; + +#endif +#ifdef PB_DS_TREE_POLICY_HPP +template +using multiset = tree, rb_tree_tag, + tree_order_statistics_node_update>; +template +using rbtree = tree, rb_tree_tag, + tree_order_statistics_node_update>; +#endif + +void solve() { + int n, m; + cin >> n >> m; + + vec> grid(n, vec(m)); + + for (auto &row : grid) { + for (auto &cell : row) + cin >> cell; + } + + int x = gcd(grid[n - 1][m - 1], grid[0][0]); + + vec one, two; + for (int i = 1; i * i <= x; ++i) { + if (x % i == 0) { + one.eb(i); + if (i != x / i) { + two.eb(x / i); + } + } + } + + vec> dp(n, vec(m, false)); + + auto DP = [&](int factor) -> bool { + for (int i = 0; i < sz(dp); ++i) + dp[i].assign(m, false); + dp[0][0] = grid[0][0] % factor == 0; + + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + if (grid[i][j] % factor) { + continue; + } + if (j) + dp[i][j] = dp[i][j] || dp[i][j - 1]; + if (i) + dp[i][j] = dp[i][j] || dp[i - 1][j]; + } + } + + return dp[n - 1][m - 1]; + }; + + for (auto it = two.begin(); it != two.end(); ++it) { + if (DP(*it)) { + prln("{}", *it); + return; + } + } + + for (auto it = one.rbegin(); it != one.rend(); ++it) { + if (DP(*it)) { + prln("{}", *it); + return; + } + } +} + +int main() { // {{{ + cin.tie(nullptr)->sync_with_stdio(false); + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + return 0; +} +// }}} diff --git a/codeforces/938/g.in b/codeforces/938/g.in new file mode 100644 index 0000000..f5722c6 --- /dev/null +++ b/codeforces/938/g.in @@ -0,0 +1,11 @@ +3 +2 3 +30 20 30 +15 25 40 +3 3 +12 4 9 +3 12 2 +8 3 12 +2 4 +2 4 6 8 +1 3 6 9 diff --git a/codeforces/938/g.out b/codeforces/938/g.out new file mode 100644 index 0000000..a1c2e1b --- /dev/null +++ b/codeforces/938/g.out @@ -0,0 +1,6 @@ +10 +3 +1 + +[code]: 0 +[time]: 11.7426 ms \ No newline at end of file