From 99042fd37af04144da0984dc7f436b3b89cb934c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 28 Feb 2024 12:26:23 +0100 Subject: [PATCH] 1933: add solutions from the contest Signed-off-by: Matej Focko --- 1933/a.cpp | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1933/b.cpp | 355 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1933/c.cpp | 347 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1042 insertions(+) create mode 100644 1933/a.cpp create mode 100644 1933/b.cpp create mode 100644 1933/c.cpp diff --git a/1933/a.cpp b/1933/a.cpp new file mode 100644 index 0000000..b07062f --- /dev/null +++ b/1933/a.cpp @@ -0,0 +1,340 @@ +#pragma region includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#pragma endregion includes + +#pragma region helpers + +#pragma region aliases +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +template +using V = std::vector; + +template +using M = std::map; + +template +using S = std::set; + +template +using Q = std::deque; + +using i8 = std::int8_t; +using u8 = std::uint8_t; +using i16 = std::int16_t; +using u16 = std::uint16_t; +using i32 = std::int32_t; +using u32 = std::uint32_t; +using i64 = std::int64_t; +using u64 = std::uint64_t; +#pragma endregion /* aliases */ + +#pragma region data structures +template +struct max_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end()); + } + + T pop() { + std::pop_heap(h.begin(), h.end()); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; + +template +struct min_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end(), std::greater<>{}); + } + + T pop() { + std::pop_heap(h.begin(), h.end(), std::greater<>{}); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; +#pragma endregion /* data structures */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#pragma region input +template +void collect(Container &c, std::size_t size) { + auto it = std::inserter(c, c.begin()); + + for (auto i = 0u; i < size; ++i) { + typename Container::value_type x; + std::cin >> x; + it = std::move(x); + } +} + +template +Container collect(std::size_t size) { + Container c{}; + collect(c, size); + return c; +} + +template +std::map collect_count(std::size_t size) { + std::map counts; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + ++counts[x]; + } + + return counts; +} +#pragma endregion /* input */ + +#pragma region math +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} + +template +T isqrt(T x) { + assert(x >= 0); + + auto max_shift = 8 * sizeof(T) - 1; + auto shift = (max_shift - std::countl_zero(x)) & ~1; + auto bit = 1 << shift; + + T result = 0; + while (bit != 0) { + if (x >= (result + bit)) { + x -= result + bit; + result = (result >> 1) + bit; + } else { + result = (result >> 1); + } + bit = bit >> 2; + } + + return result; +} + +template +struct Z { + Z(std::int64_t x = 0) : x(x % MODULO) {} + + Z pow(std::uint32_t exp) const { + auto ans = 1; + auto base = x; + + for (; exp > 0; exp >>= 1) { + if (exp % 2 == 1) { + ans *= base; + } + + base *= base; + } + + return ans; + } + + Z inv() const { + assert(x != 0); + return pow(MODULO - 2); + } + + Z operator-() const { return {-x}; } + Z operator+=(const Z &rhs) { x = (x + rhs.x) % MODULO; } + Z operator-=(const Z &rhs) { x = (x - rhs.x) % MODULO; } + Z operator*=(const Z &rhs) { x = (x * rhs.x) % MODULO; } + Z operator/=(const Z &rhs) { x = (x * rhs.inv().x) % MODULO; } + + friend Z operator+(Z lhs, const Z &rhs) { return lhs += rhs; } + friend Z operator-(Z lhs, const Z &rhs) { return lhs -= rhs; } + friend Z operator*(Z lhs, const Z &rhs) { return lhs *= rhs; } + friend Z operator/(Z lhs, const Z &rhs) { return lhs /= rhs; } + + friend std::istream &operator>>(std::istream &is, Z &z) { + is >> z.x; + z.x %= MODULO; + return is; + } + + friend std::ostream &operator<<(std::ostream &os, const Z &z) { + return os << z.x; + } + + private: + std::int64_t x; +}; +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { +#ifdef LOCAL + std::cout << "Answer: "; +#endif + + std::cout << ans << "\n"; +} + +inline void yes() { answer("YES"); } +inline void no() { answer("NO"); } +inline void yesno(bool ans) { answer(ans ? "YES" : "NO"); } +#pragma endregion /* output */ + +#pragma region rng +std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); +#pragma endregion /* rng */ + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +void solve() { + size_t n; + cin >> n; + + V a; + collect(a, n); + + long long int sum = 0; + for (const auto x: a) { + sum += abs(x); + } + answer(sum); +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif diff --git a/1933/b.cpp b/1933/b.cpp new file mode 100644 index 0000000..e65c744 --- /dev/null +++ b/1933/b.cpp @@ -0,0 +1,355 @@ +#pragma region includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#pragma endregion includes + +#pragma region helpers + +#pragma region aliases +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +template +using V = std::vector; + +template +using M = std::map; + +template +using S = std::set; + +template +using Q = std::deque; + +using i8 = std::int8_t; +using u8 = std::uint8_t; +using i16 = std::int16_t; +using u16 = std::uint16_t; +using i32 = std::int32_t; +using u32 = std::uint32_t; +using i64 = std::int64_t; +using u64 = std::uint64_t; +#pragma endregion /* aliases */ + +#pragma region data structures +template +struct max_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end()); + } + + T pop() { + std::pop_heap(h.begin(), h.end()); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; + +template +struct min_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end(), std::greater<>{}); + } + + T pop() { + std::pop_heap(h.begin(), h.end(), std::greater<>{}); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; +#pragma endregion /* data structures */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#pragma region input +template +void collect(Container &c, std::size_t size) { + auto it = std::inserter(c, c.begin()); + + for (auto i = 0u; i < size; ++i) { + typename Container::value_type x; + std::cin >> x; + it = std::move(x); + } +} + +template +Container collect(std::size_t size) { + Container c{}; + collect(c, size); + return c; +} + +template +std::map collect_count(std::size_t size) { + std::map counts; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + ++counts[x]; + } + + return counts; +} +#pragma endregion /* input */ + +#pragma region math +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} + +template +T isqrt(T x) { + assert(x >= 0); + + auto max_shift = 8 * sizeof(T) - 1; + auto shift = (max_shift - std::countl_zero(x)) & ~1; + auto bit = 1 << shift; + + T result = 0; + while (bit != 0) { + if (x >= (result + bit)) { + x -= result + bit; + result = (result >> 1) + bit; + } else { + result = (result >> 1); + } + bit = bit >> 2; + } + + return result; +} + +template +struct Z { + Z(std::int64_t x = 0) : x(x % MODULO) {} + + Z pow(std::uint32_t exp) const { + auto ans = 1; + auto base = x; + + for (; exp > 0; exp >>= 1) { + if (exp % 2 == 1) { + ans *= base; + } + + base *= base; + } + + return ans; + } + + Z inv() const { + assert(x != 0); + return pow(MODULO - 2); + } + + Z operator-() const { return {-x}; } + Z operator+=(const Z &rhs) { x = (x + rhs.x) % MODULO; } + Z operator-=(const Z &rhs) { x = (x - rhs.x) % MODULO; } + Z operator*=(const Z &rhs) { x = (x * rhs.x) % MODULO; } + Z operator/=(const Z &rhs) { x = (x * rhs.inv().x) % MODULO; } + + friend Z operator+(Z lhs, const Z &rhs) { return lhs += rhs; } + friend Z operator-(Z lhs, const Z &rhs) { return lhs -= rhs; } + friend Z operator*(Z lhs, const Z &rhs) { return lhs *= rhs; } + friend Z operator/(Z lhs, const Z &rhs) { return lhs /= rhs; } + + friend std::istream &operator>>(std::istream &is, Z &z) { + is >> z.x; + z.x %= MODULO; + return is; + } + + friend std::ostream &operator<<(std::ostream &os, const Z &z) { + return os << z.x; + } + + private: + std::int64_t x; +}; +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { +#ifdef LOCAL + std::cout << "Answer: "; +#endif + + std::cout << ans << "\n"; +} + +inline void yes() { answer("YES"); } +inline void no() { answer("NO"); } +inline void yesno(bool ans) { answer(ans ? "YES" : "NO"); } +#pragma endregion /* output */ + +#pragma region rng +std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); +#pragma endregion /* rng */ + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +void solve() { + size_t n; + cin >> n; + + V a; + collect(a, n); + + V counters(3, 0); + + long long int sum = 0; + for (const auto x: a) { + ++counters[x % 3]; + sum += x; + } + + auto rem = sum % 3; + if (rem == 0) { + answer(0); + return; + } + + if (counters[rem] > 0) { + answer(1); + return; + } + + answer(3 - rem); +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif diff --git a/1933/c.cpp b/1933/c.cpp new file mode 100644 index 0000000..5a288ac --- /dev/null +++ b/1933/c.cpp @@ -0,0 +1,347 @@ +#pragma region includes +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#pragma endregion includes + +#pragma region helpers + +#pragma region aliases +#define LOOP(var, n) for (auto var = 0; var < n; ++var) + +template +using V = std::vector; + +template +using M = std::map; + +template +using S = std::set; + +template +using Q = std::deque; + +using i8 = std::int8_t; +using u8 = std::uint8_t; +using i16 = std::int16_t; +using u16 = std::uint16_t; +using i32 = std::int32_t; +using u32 = std::uint32_t; +using i64 = std::int64_t; +using u64 = std::uint64_t; +#pragma endregion /* aliases */ + +#pragma region data structures +template +struct max_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end()); + } + + T pop() { + std::pop_heap(h.begin(), h.end()); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; + +template +struct min_heap { + using container = std::vector; + + typename container::size_type size() const { return h.size(); } + + void push(T item) { + h.push_back(item); + std::push_heap(h.begin(), h.end(), std::greater<>{}); + } + + T pop() { + std::pop_heap(h.begin(), h.end(), std::greater<>{}); + T item = std::move(h.back()); + h.pop_back(); + return item; + } + + private: + container h; +}; +#pragma endregion /* data structures */ + +#pragma region debug +void dbg_out() { std::cerr << std::endl; } +template +void dbg_out(Head H, Tail... T) { + std::cerr << ' ' << H; + dbg_out(T...); +} +#ifdef LOCAL +#define dbg(...) \ + std::cerr << '[' << __FILE__ << ':' << __LINE__ << "] (" << #__VA_ARGS__ \ + << "):", \ + dbg_out(__VA_ARGS__) +#else +#define dbg(...) +#endif +#pragma endregion debug + +#pragma region functional +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html +template +class y_combinator_result { + Fun fun_; + + public: + template + explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} +#pragma endregion /* functional */ + +#pragma region input +template +void collect(Container &c, std::size_t size) { + auto it = std::inserter(c, c.begin()); + + for (auto i = 0u; i < size; ++i) { + typename Container::value_type x; + std::cin >> x; + it = std::move(x); + } +} + +template +Container collect(std::size_t size) { + Container c{}; + collect(c, size); + return c; +} + +template +std::map collect_count(std::size_t size) { + std::map counts; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + ++counts[x]; + } + + return counts; +} +#pragma endregion /* input */ + +#pragma region math +long pow(long base, long exp) { + if (exp == 0) return 1; + long half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} + +template +T isqrt(T x) { + assert(x >= 0); + + auto max_shift = 8 * sizeof(T) - 1; + auto shift = (max_shift - std::countl_zero(x)) & ~1; + auto bit = 1 << shift; + + T result = 0; + while (bit != 0) { + if (x >= (result + bit)) { + x -= result + bit; + result = (result >> 1) + bit; + } else { + result = (result >> 1); + } + bit = bit >> 2; + } + + return result; +} + +template +struct Z { + Z(std::int64_t x = 0) : x(x % MODULO) {} + + Z pow(std::uint32_t exp) const { + auto ans = 1; + auto base = x; + + for (; exp > 0; exp >>= 1) { + if (exp % 2 == 1) { + ans *= base; + } + + base *= base; + } + + return ans; + } + + Z inv() const { + assert(x != 0); + return pow(MODULO - 2); + } + + Z operator-() const { return {-x}; } + Z operator+=(const Z &rhs) { x = (x + rhs.x) % MODULO; } + Z operator-=(const Z &rhs) { x = (x - rhs.x) % MODULO; } + Z operator*=(const Z &rhs) { x = (x * rhs.x) % MODULO; } + Z operator/=(const Z &rhs) { x = (x * rhs.inv().x) % MODULO; } + + friend Z operator+(Z lhs, const Z &rhs) { return lhs += rhs; } + friend Z operator-(Z lhs, const Z &rhs) { return lhs -= rhs; } + friend Z operator*(Z lhs, const Z &rhs) { return lhs *= rhs; } + friend Z operator/(Z lhs, const Z &rhs) { return lhs /= rhs; } + + friend std::istream &operator>>(std::istream &is, Z &z) { + is >> z.x; + z.x %= MODULO; + return is; + } + + friend std::ostream &operator<<(std::ostream &os, const Z &z) { + return os << z.x; + } + + private: + std::int64_t x; +}; +#pragma endregion /* math */ + +#pragma region output +template +std::ostream &operator<<(std::ostream &os, std::pair const &p) { + return os << p.first << " " << p.second; +} + +template ::value, + typename C::value_type>::type> +std::ostream &operator<<(std::ostream &os, const C &v) { + std::string sep; + for (const T &x : v) { + os << sep << x, sep = " "; + } + + return os; +} + +template +inline void answer(const T &ans) { +#ifdef LOCAL + std::cout << "Answer: "; +#endif + + std::cout << ans << "\n"; +} + +inline void yes() { answer("YES"); } +inline void no() { answer("NO"); } +inline void yesno(bool ans) { answer(ans ? "YES" : "NO"); } +#pragma endregion /* output */ + +#pragma region rng +std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); +#pragma endregion /* rng */ + +#pragma endregion /* helpers */ + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; + +auto count_ks(S& ks, int l, int a, int b) -> int { + ks.insert(l); + + if (l % a == 0) { + count_ks(ks, l / a, a, b); + } + if (l % b == 0) { + count_ks(ks, l / b, a, b); + } + + return static_cast(ks.size()); +} + +void solve() { + int a, b, l; + cin >> a >> b >> l; + + S ks; + answer(count_ks(ks, l, a, b)); +} + +void tests() { + // TODO +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +int main(void) { + tests(); + return 0; +} + +#else + +int main(void) { + int N = 1; + +#ifndef SINGLE + + std::cin >> N; + +#endif + + while (N-- > 0) { + solve(); + } + + return 0; +} + +#endif