diff --git a/1829/a.cpp b/1829/a.cpp new file mode 100644 index 0000000..8fc05a8 --- /dev/null +++ b/1829/a.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace helpers { + +using namespace std; + +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 +inline void answer(const T& ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +} // namespace helpers + +namespace { + +using namespace std; +using namespace helpers; + +static const string S("codeforces"); + +int differs(const string& s) { + int counter = 0; + for (auto i = 0u; i < S.size(); ++i) { + if (s[i] != S[i]) { + ++counter; + } + } + + return counter; +} + +void solve() { + string s; + cin >> s; + answer(differs(s)); +} + +} // namespace + +// for single test case, comment out for ‹N› test cases +// #define SINGLE + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("examples") { + CHECK(differs("coolforsez") == 4); + CHECK(differs("cadafurcie") == 5); + CHECK(differs("codeforces") == 0); + CHECK(differs("paiuforces") == 4); + CHECK(differs("forcescode") == 9); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/b.cpp b/1829/b.cpp new file mode 100644 index 0000000..7858b84 --- /dev/null +++ b/1829/b.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace helpers { + +using namespace std; + +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 +inline void answer(const T& ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +} // namespace helpers + +namespace { + +using namespace std; +using namespace helpers; + +int longest(const vector& arr) { + int found = 0; + + int current = 0; + for (auto i = 0u; i < arr.size(); ++i) { + if (arr[i] == 0) { + ++current; + found = max(found, current); + } else { + current = 0; + } + } + + return found; +} + +void solve() { + int N; + cin >> N; + + vector arr(N); + for (auto i = 0; i < N; ++i) { + cin >> arr[i]; + } + + answer(longest(arr)); +} + +} // namespace + +// for single test case, comment out for ‹N› test cases +// #define SINGLE + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("examples") { + CHECK(longest(vector{1, 0, 0, 1, 0}) == 2); + CHECK(longest(vector{0, 1, 1, 1}) == 1); + CHECK(longest(vector{0}) == 1); + CHECK(longest(vector{1, 1, 1}) == 0); + CHECK(longest(vector{1, 0, 0, 0, 1, 0, 0, 0, 1}) == 3); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/c.cpp b/1829/c.cpp new file mode 100644 index 0000000..cf7c005 --- /dev/null +++ b/1829/c.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace helpers { + +using namespace std; + +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 +inline void answer(const T &ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +#define LOOP(n) for (auto i = 0; i < n; ++i) + +} // namespace helpers + +namespace { +using namespace std; +using namespace helpers; + +struct book { + int t; + string skills; +}; + +int find_minimum(const vector &books) { + const int UNDEFINED = 400001; + + int min_a = UNDEFINED; + int min_b = UNDEFINED; + int min_both = UNDEFINED; + + for (const book &b : books) { + if (b.skills == "01") { + min_b = min(min_b, b.t); + } else if (b.skills == "10") { + min_a = min(min_a, b.t); + } else if (b.skills == "11") { + min_both = min(min_both, b.t); + } + } + + if (min_a != UNDEFINED && min_b != UNDEFINED && min_a + min_b < min_both) { + return min_a + min_b; + } else if (min_both != UNDEFINED) { + return min_both; + } + + return -1; +} + +void solve() { + int N; + cin >> N; + + vector bs(N); + LOOP(N) { + int t; + string skills; + cin >> t >> skills; + bs[i] = {t, skills}; + } + + answer(find_minimum(bs)); +} + +} // namespace + +// for single test case, comment out for ‹N› test cases +// #define SINGLE + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("examples") { + CHECK(find_minimum( + vector{{2, "00"}, {3, "10"}, {4, "01"}, {4, "00"}}) == 7); + CHECK(find_minimum(vector{ + {3, "01"}, {3, "01"}, {5, "01"}, {2, "10"}, {9, "10"}}) == 5); + CHECK(find_minimum(vector{{5, "11"}}) == 5); + CHECK(find_minimum(vector{{9, "11"}, {8, "01"}, {7, "10"}}) == 9); + CHECK( + find_minimum(vector{ + {4, "01"}, {6, "01"}, {7, "01"}, {8, "00"}, {9, "01"}, {1, "00"}}) == + -1); + CHECK(find_minimum(vector{{{200000, "01"}, {200000, "10"}}}) == 400000); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/d.cpp b/1829/d.cpp new file mode 100644 index 0000000..8888590 --- /dev/null +++ b/1829/d.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace helpers { + +using namespace std; + +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 +inline void answer(const T &ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +inline void yesno(bool ans) { + if (ans) { + yes(); + } else { + no(); + } +} + +} // namespace helpers + +namespace { + +using namespace std; +using namespace helpers; + +bool can_split(int n, int m) { + if (n == m) { + // we already have a pile + return true; + } + + if (n < m) { + // cannot make bigger pile + return false; + } + + if (n % 3 != 0) { + // cannot split the pile + return false; + } + + int third = n / 3; + return can_split(third, m) || can_split(2 * third, m); +} + +void solve() { + int n, m; + cin >> n >> m; + yesno(can_split(n, m)); +} + +} // namespace + +// for single test case, comment out for ‹N› test cases +// #define SINGLE + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("examples") { + CHECK(can_split(6, 4)); + CHECK(can_split(9, 4)); + CHECK(!can_split(4, 2)); + CHECK(!can_split(18, 27)); + CHECK(can_split(27, 4)); + CHECK(can_split(27, 2)); + CHECK(!can_split(27, 10)); + CHECK(can_split(1, 1)); + CHECK(can_split(3, 1)); + CHECK(!can_split(5, 1)); + CHECK(!can_split(746001, 2984004)); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/e.cpp b/1829/e.cpp new file mode 100644 index 0000000..f9c5a20 --- /dev/null +++ b/1829/e.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace helpers { + +using namespace std; + +namespace 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; +} +} // namespace math + +namespace input { + +template +std::vector load_vector(std::size_t size) { + std::vector result{}; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + result.push_back(std::move(x)); + } + + return result; +} + +} // namespace input + +namespace output { + +template +inline void answer(const T& ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +inline void yesno(bool ans) { + if (ans) { + yes(); + } else { + no(); + } +} + +} // namespace output + +using namespace math; +using namespace input; +using namespace output; + +#define LOOP(n) for (auto i = 0; i < n; ++i) + +} // namespace helpers + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; +using namespace helpers; + +int run_bfs(vector>& grid, int y0, int x0) { + int volume = 0; + queue> q; + + // handle first element separately + volume += grid[y0][x0]; + grid[y0][x0] = 0; + q.push({y0, x0}); + + while (q.size()) { + auto [_y, _x] = q.front(); + q.pop(); + + for (auto [dy, dx] : + vector>{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}) { + auto y = _y + dy; + auto x = _x + dx; + + if (y < 0 || y >= static_cast(grid.size()) || x < 0 || + x >= static_cast(grid[y].size()) || grid[y][x] == 0) { + continue; + } + + volume += grid[y][x]; + grid[y][x] = 0; + q.push(pair{y, x}); + } + } + + return volume; +} + +int largest_volume(vector> grid) { + int largest = 0; + + for (auto y = 0; y < static_cast(grid.size()); ++y) { + for (auto x = 0; x < static_cast(grid[y].size()); ++x) { + if (grid[y][x] == 0) { + // ignore zeroes + continue; + } + + largest = max(largest, run_bfs(grid, y, x)); + } + } + + return largest; +} + +void solve() { + int n, m; + cin >> n >> m; + + vector> grid; + LOOP(n) { + auto row = load_vector(m); + grid.push_back(move(row)); + } + + answer(largest_volume(grid)); +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("examples") { + CHECK(largest_volume( + vector{vector{1, 2, 0}, vector{3, 4, 0}, vector{0, 0, 5}}) == 10); + CHECK(largest_volume(vector>{vector{0}}) == 0); + CHECK(largest_volume(vector{ + vector{0, 1, 1}, + vector{1, 0, 1}, + vector{1, 1, 1}, + }) == 7); + CHECK(largest_volume(vector{ + vector{1, 1, 1, 1, 1}, + vector{1, 0, 0, 0, 1}, + vector{1, 0, 5, 0, 1}, + vector{1, 0, 0, 0, 1}, + vector{1, 1, 1, 1, 1}, + }) == 16); + CHECK(largest_volume(vector{ + vector{1, 1, 1, 1, 1}, + vector{1, 0, 0, 0, 1}, + vector{1, 1, 4, 0, 1}, + vector{1, 0, 0, 0, 1}, + vector{1, 1, 1, 1, 1}, + }) == 21); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solution::solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solution::solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/f.cpp b/1829/f.cpp new file mode 100644 index 0000000..f6c44d3 --- /dev/null +++ b/1829/f.cpp @@ -0,0 +1,206 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &s, std::pair const &p) { + return s << p.first << " " << p.second; +} + +namespace helpers { + +using namespace std; + +namespace 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; +} + +} // namespace math + +namespace input { + +template +std::vector load_vector(std::size_t size) { + std::vector result{}; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + result.push_back(std::move(x)); + } + + return result; +} + +} // namespace input + +namespace output { + +template +inline void answer(const T &ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +inline void yesno(bool ans) { + if (ans) { + yes(); + } else { + no(); + } +} + +} // namespace output + +using namespace math; +using namespace input; +using namespace output; + +#define LOOP(n) for (auto i = 0; i < n; ++i) + +} // namespace helpers + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; +using namespace helpers; + +struct vertex { + set neighbours; +}; + +pair determine_snowflake(const map &vertices) { + map counts; + + int leaf_parent = -1; + for (const auto &mapping : vertices) { + auto v = mapping.second; + + if (v.neighbours.size() == 1) { + leaf_parent = *v.neighbours.begin(); + continue; + } + + counts[v.neighbours.size()]++; + } + + int y = vertices.at(leaf_parent).neighbours.size() - 1; + + // V = 1 + x + x * y + // V - 1 = x + x * y + // V - 1 = x * (1 + y) + // x = (V - 1) / (y + 1) + int x = (vertices.size() - 1) / (y + 1); + + return {x, y}; +} + +void solve() { + int V, E; + cin >> V >> E; + + map vertices; + LOOP(E) { + int u, v; + cin >> u >> v; + + vertices[u].neighbours.insert(v); + vertices[v].neighbours.insert(u); + } + + answer(determine_snowflake(vertices)); +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +static map edges_to_graph(vector> edges) { + map vertices; + + for (auto [u, v] : edges) { + vertices[u].neighbours.insert(v); + vertices[v].neighbours.insert(u); + } + + return vertices; +} + +TEST_CASE("examples") { + CHECK(determine_snowflake(edges_to_graph(vector>{ + {21, 20}, {21, 20}, {5, 20}, {13, 20}, {1, 3}, {11, 3}, + {10, 3}, {4, 8}, {19, 8}, {14, 8}, {9, 7}, {12, 7}, + {17, 7}, {18, 6}, {16, 6}, {2, 6}, {6, 15}, {7, 15}, + {8, 15}, {20, 15}, {3, 15}})) == pair{5, 3}); + CHECK(determine_snowflake(edges_to_graph(vector>{ + {7, 6}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {3, 7}})) == + pair{2, 2}); + CHECK(determine_snowflake(edges_to_graph(vector>{ + {9, 8}, + {9, 3}, + {3, 6}, + {6, 2}, + {2, 1}, + {5, 2}, + {2, 7}, + {4, 3}, + {3, 8}, + })) == pair{2, 3}); + CHECK(determine_snowflake(edges_to_graph(vector>{ + {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {3, 7}})) == pair{2, 2}); + CHECK(determine_snowflake(edges_to_graph(vector>{ + {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {2, 21}, {2, 22}, + {2, 23}, {2, 24}, {3, 31}, {3, 32}, {3, 33}, {3, 34}, {4, 41}, + {4, 42}, {4, 43}, {4, 44}, {5, 51}, {5, 52}, {5, 53}, {5, 54}, + {6, 61}, {6, 62}, {6, 63}, {6, 64}, + })) == pair{5, 4}); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solution::solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solution::solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/g.cpp b/1829/g.cpp new file mode 100644 index 0000000..aecd82b --- /dev/null +++ b/1829/g.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &s, std::pair const &p) { + return s << p.first << " " << p.second; +} + +namespace helpers { + +using namespace std; + +namespace math { + +uint32_t pow(uint32_t base, uint32_t exp) { + if (exp == 0) return 1; + uint32_t half = pow(base, exp / 2); + if (exp % 2 == 0) return half * half; + return half * half * base; +} + +} // namespace math + +namespace input { + +template +std::vector load_vector(std::size_t size) { + std::vector result{}; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + result.push_back(std::move(x)); + } + + return result; +} + +} // namespace input + +namespace output { + +template +inline void answer(const T &ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +inline void yesno(bool ans) { + if (ans) { + yes(); + } else { + no(); + } +} + +} // namespace output + +using namespace math; +using namespace input; +using namespace output; + +#define LOOP(n) for (auto i = 0; i < n; ++i) + +} // namespace helpers + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; +using namespace helpers; + +constexpr array ROWS = []() { + array starts; + + starts[0] = 1; + for (auto y = 1; y < 2023; ++y) { + starts[y] = starts[y - 1] + y; + } + + return starts; +}(); + +int32_t find_row(int n) { + return distance(ROWS.begin(), upper_bound(ROWS.begin(), ROWS.end(), n)) - 1; +} + +int32_t get_n(int y, int x) { return ROWS[y] + x; } + +uint64_t sum_of_n_squared(uint64_t n) { return n * (n + 1) * (2 * n + 1) / 6; } + +uint64_t add_row(int32_t lower, int32_t upper) { + return sum_of_n_squared(upper) - sum_of_n_squared(lower - 1); +} + +uint64_t find_sum(int32_t n) { + auto y = find_row(n); + auto x_u = n - ROWS[y]; + auto x_l = x_u; + + uint64_t sum = 0; + for (; y >= 0; --y) { + auto lower = get_n(y, x_l); + auto upper = get_n(y, x_u); + + sum += add_row(lower, upper); + + x_l = max(0, x_l - 1); + x_u = min(x_u, y - 1); + } + + return sum; +} + +void solve() { + int n; + cin >> n; + answer(find_sum(n)); +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("find row") { + CHECK(ROWS[find_row(1)] == 1); + + CHECK(ROWS[find_row(2)] == 2); + CHECK(ROWS[find_row(3)] == 2); + + for (int i = 11; i <= 15; ++i) { + CHECK(ROWS[find_row(i)] == 11); + } +} + +TEST_CASE("examples") { + CHECK(find_sum(9) == 156); + CHECK(find_sum(1) == 1); + CHECK(find_sum(2) == 5); + CHECK(find_sum(3) == 10); + CHECK(find_sum(4) == 21); + CHECK(find_sum(5) == 39); + CHECK(find_sum(6) == 46); + CHECK(find_sum(10) == 146); + CHECK(find_sum(1434) == 63145186); + + LOOP(1000) { CHECK(find_sum(1000000) == 58116199242129511); } + LOOP(100000) { find_sum(900000 + i); } +} + +#else + +int main(void) { + +#ifdef SINGLE + + solution::solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solution::solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/h.cpp b/1829/h.cpp new file mode 100644 index 0000000..84b972b --- /dev/null +++ b/1829/h.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &s, std::pair const &p) { + return s << p.first << " " << p.second; +} + +namespace helpers { + +using namespace std; + +namespace math { + +static constexpr int MODULO = 1000000007; + +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; +} + +} // namespace math + +namespace input { + +template +std::vector load_vector(std::size_t size) { + std::vector result{}; + + for (auto i = 0u; i < size; ++i) { + T x; + std::cin >> x; + result.push_back(std::move(x)); + } + + return result; +} + +} // namespace input + +namespace output { + +template +inline void answer(const T &ans) { + cout << ans << "\n"; +} + +inline void yes() { cout << "YES\n"; } +inline void no() { cout << "NO\n"; } + +inline void yesno(bool ans) { + if (ans) { + yes(); + } else { + no(); + } +} + +} // namespace output + +using namespace math; +using namespace input; +using namespace output; + +#define LOOP(n) for (auto i = 0; i < n; ++i) + +} // namespace helpers + +// for ‹N› test cases, uncomment for single test case +// #define SINGLE + +namespace solution { + +using namespace std; +using namespace helpers; + +int find_ways(int k, const vector &numbers) { + std::array counters{}; + + // try all possible ways + for (int x : numbers) { + std::array new_counters{}; + + for (int mask = 0; mask < 64; ++mask) { + new_counters[mask] = (new_counters[mask] + counters[mask]) % MODULO; + new_counters[mask & x] = + (new_counters[mask & x] + counters[mask]) % MODULO; + } + + new_counters[x] = (new_counters[x] + 1) % MODULO; + counters = move(new_counters); + } + + // sum up the found results + int counts = 0; + for (auto i = 0u; i < 64; ++i) { + if (popcount(i) == k) { + counts = (counts + counters[i]) % MODULO; + } + } + return counts; +} + +void solve() { + int n, k; + cin >> n >> k; + auto numbers = load_vector(n); + answer(find_ways(k, numbers)); +} + +} // namespace solution + +using namespace solution; + +#ifdef TEST + +#include "../.common/cpp/catch_amalgamated.hpp" + +TEST_CASE("examples") { + CHECK(find_ways(1, vector{1, 1, 1, 1, 1}) == 31); + CHECK(find_ways(0, vector{0, 1, 2, 3}) == 10); + CHECK(find_ways(1, vector{5, 5, 7, 4, 2}) == 10); + CHECK(find_ways(2, vector{3}) == 1); + CHECK(find_ways(0, vector{0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2}) == 4032); + CHECK(find_ways(6, vector{63, 0, 63, 5, 5, 63, 63, 4, 12, 13}) == 15); +} + +#else + +int main(void) { + +#ifdef SINGLE + + solution::solve(); + +#else + + // for multiple test cases + int N; + std::cin >> N >> std::ws; + + for (auto i = 0; i < N; ++i) { + solution::solve(); + } + +#endif + + return 0; +} + +#endif diff --git a/1829/index.html b/1829/index.html new file mode 100644 index 0000000..9136759 --- /dev/null +++ b/1829/index.html @@ -0,0 +1,1320 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Problems - Codeforces + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+
+ + +
+ +
Codeforces Round 871 (Div. 4)
+
+ +
+
+ + + +
+
+ +
+ +
A. Love Story
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Timur loves codeforces. That's why he has a string $$$s$$$ having length $$$10$$$ made containing only lowercase Latin letters. Timur wants to know how many indices string $$$s$$$ differs from the string "codeforces".

For example string $$$s =$$$ "coolforsez" differs from "codeforces" in $$$4$$$ indices, shown in bold.

Help Timur by finding the number of indices where string $$$s$$$ differs from "codeforces".

Note that you can't reorder the characters in the string $$$s$$$.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

Each test case is one line and contains the string $$$s$$$, consisting of exactly $$$10$$$ lowercase Latin characters.

Output

For each test case, output a single integer — the number of indices where string $$$s$$$ differs.

Example
Input
5
coolforsez
cadafurcie
codeforces
paiuforces
forcescode
Output
4
+5
+0
+4
+9
+

+
+ + + + + +
+
+ + +
+ +
B. Blank Space
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a binary array $$$a$$$ of $$$n$$$ elements, a binary array is an array consisting only of $$$0$$$s and $$$1$$$s.

A blank space is a segment of consecutive elements consisting of only $$$0$$$s.

Your task is to find the length of the longest blank space.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of the array.

The second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$0 \leq a_i \leq 1$$$) — the elements of the array.

Output

For each test case, output a single integer — the length of the longest blank space.

Example
Input
5
5
1 0 0 1 0
4
0 1 1 1
1
0
3
1 1 1
9
1 0 0 0 1 0 0 0 1
Output
2
+1
+1
+0
+3
+

+
+ + + + + +
+
+ + +
+ +
C. Mr. Perfectly Fine
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Victor wants to become "Mr. Perfectly Fine". For that, he needs to acquire a certain set of skills. More precisely, he has $$$2$$$ skills he needs to acquire.

Victor has $$$n$$$ books. Reading book $$$i$$$ takes him $$$m_i$$$ minutes and will give him some (possibly none) of the required two skills, represented by a binary string of length $$$2$$$.

What is the minimum amount of time required so that Victor acquires all of the two skills?

Input

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of books available.

Then $$$n$$$ lines follow. Line $$$i$$$ contains a positive integer $$$m_i$$$ ($$$1 \leq m_i \leq 2 \cdot 10^5$$$) and a binary string of length $$$2$$$, where $$$s_{i1} = 1$$$ if reading book $$$i$$$ acquires Victor skill $$$1$$$, and $$$s_{i1} = 0$$$ otherwise, and $$$s_{i2} = 1$$$ if reading book $$$i$$$ acquires Victor skill $$$2$$$, and $$$s_{i2} = 0$$$ otherwise.

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output a single integer denoting the minimum amount of minutes required for Victor to obtain both needed skills and $$$-1$$$ in case it's impossible to obtain the two skills after reading any amount of books.

Example
Input
6
4
2 00
3 10
4 01
4 00
5
3 01
3 01
5 01
2 10
9 10
1
5 11
3
9 11
8 01
7 10
6
4 01
6 01
7 01
8 00
9 01
1 00
4
8 00
9 10
9 11
8 11
Output
7
+5
+5
+9
+-1
+8
+
Note

In the first test case, we can use books $$$2$$$ and $$$3$$$, with a total amount of minutes spent equal to $$$3 + 4 = 7$$$.

In the second test case, we can use the books $$$1$$$ and $$$4$$$, with a total amount of minutes spent equal to $$$3 + 2 = 5$$$.

In the third test case, we have only one option and that is reading book $$$1$$$ for a total amount of minutes spent equal to $$$5$$$.

+
+ + + + + +
+
+ + +
+ +
D. Gold Rush
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Initially you have a single pile with $$$n$$$ gold nuggets. In an operation you can do the following:

  • Take any pile and split it into two piles, so that one of the resulting piles has exactly twice as many gold nuggets as the other. (All piles should have an integer number of nuggets.)

One possible move is to take a pile of size $$$6$$$ and split it into piles of sizes $$$2$$$ and $$$4$$$, which is valid since $$$4$$$ is twice as large as $$$2$$$.

Can you make a pile with exactly $$$m$$$ gold nuggets using zero or more operations?
Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^7$$$) — the starting and target pile sizes, respectively.

Output

For each test case, output "YES" if you can make a pile of size exactly $$$m$$$, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Example
Input
11
6 4
9 4
4 2
18 27
27 4
27 2
27 10
1 1
3 1
5 1
746001 2984004
Output
YES
+YES
+NO
+NO
+YES
+YES
+NO
+YES
+YES
+NO
+NO
+
Note

The first test case is pictured in the statement. We can make a pile of size $$$4$$$.

In the second test case, we can perform the following operations: $$$\{\color{red}{9}\} \to \{\color{red}{6},3\} \to \{4,2,3\}$$$. The pile that is split apart is colored red before each operation.

In the third test case, we can't perform a single operation.

In the fourth test case, we can't end up with a larger pile than we started with.

+
+ + + + + +
+
+ + +
+ +
E. The Lakes
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an $$$n \times m$$$ grid $$$a$$$ of non-negative integers. The value $$$a_{i,j}$$$ represents the depth of water at the $$$i$$$-th row and $$$j$$$-th column.

A lake is a set of cells such that:

  • each cell in the set has $$$a_{i,j} > 0$$$, and
  • there exists a path between any pair of cells in the lake by going up, down, left, or right a number of times and without stepping on a cell with $$$a_{i,j} = 0$$$.

The volume of a lake is the sum of depths of all the cells in the lake.

Find the largest volume of a lake in the grid.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.

The first line of each test case contains two integers $$$n, m$$$ ($$$1 \leq n, m \leq 1000$$$) — the number of rows and columns of the grid, respectively.

Then $$$n$$$ lines follow each with $$$m$$$ integers $$$a_{i,j}$$$ ($$$0 \leq a_{i,j} \leq 1000$$$) — the depth of the water at each cell.

It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^6$$$.

Output

For each test case, output a single integer — the largest volume of a lake in the grid.

Example
Input
5
3 3
1 2 0
3 4 0
0 0 5
1 1
0
3 3
0 1 1
1 0 1
1 1 1
5 5
1 1 1 1 1
1 0 0 0 1
1 0 5 0 1
1 0 0 0 1
1 1 1 1 1
5 5
1 1 1 1 1
1 0 0 0 1
1 1 4 0 1
1 0 0 0 1
1 1 1 1 1
Output
10
+0
+7
+16
+21
+

+
+ + + + + +
+
+ + +
+ +
F. Forever Winter
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A snowflake graph is generated from two integers $$$x$$$ and $$$y$$$, both greater than $$$1$$$, as follows:

  • Start with one central vertex.
  • Connect $$$x$$$ new vertices to this central vertex.
  • Connect $$$y$$$ new vertices to each of these $$$x$$$ vertices.
For example, below is a snowflake graph for $$$x=5$$$ and $$$y=3$$$.

The snowflake graph above has a central vertex $$$15$$$, then $$$x=5$$$ vertices attached to it ($$$3$$$, $$$6$$$, $$$7$$$, $$$8$$$, and $$$20$$$), and then $$$y=3$$$ vertices attached to each of those.

Given a snowflake graph, determine the values of $$$x$$$ and $$$y$$$.
Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 200$$$; $$$1 \leq m \leq \min\left(1000, \frac{n(n-1)}{2}\right)$$$) — the number of vertices and edges in the graph, respectively.

The next $$$m$$$ lines each contain two integers each $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$, $$$u \neq v$$$) — the numbers of vertices connected by an edge. The graph does not contain multiple edges and self-loops.

It is guaranteed that this graph is a snowflake graph for some integers $$$x$$$ and $$$y$$$ both greater than $$$1$$$.

Output

For each test case, on a separate line output the values of $$$x$$$ and $$$y$$$, in that order, separated by a space.

Example
Input
3
21 20
21 20
5 20
13 20
1 3
11 3
10 3
4 8
19 8
14 8
9 7
12 7
17 7
18 6
16 6
2 6
6 15
7 15
8 15
20 15
3 15
7 6
1 2
1 3
2 4
2 5
3 6
3 7
9 8
9 3
3 6
6 2
2 1
5 2
2 7
4 3
3 8
Output
5 3
+2 2
+2 3
+
Note

The first test case is pictured in the statement. Note that the output 3 5 is incorrect, since $$$x$$$ should be output before $$$y$$$.

+
+ + + + + +
+
+ + +
+ +
G. Hits Different
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In a carnival game, there is a huge pyramid of cans with $$$2023$$$ rows, numbered in a regular pattern as shown.

If can $$$9^2$$$ is hit initially, then all cans colored red in the picture above would fall.

You throw a ball at the pyramid, and it hits a single can with number $$$n^2$$$. This causes all cans that are stacked on top of this can to fall (that is, can $$$n^2$$$ falls, then the cans directly above $$$n^2$$$ fall, then the cans directly above those cans, and so on). For example, the picture above shows the cans that would fall if can $$$9^2$$$ is hit.

What is the sum of the numbers on all cans that fall? Recall that $$$n^2 = n \times n$$$.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The only line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — it means that the can you hit has label $$$n^2$$$.

Output

For each test case, output a single integer — the sum of the numbers on all cans that fall.

Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). For all valid inputs, the answer will always fit into 64-bit integer type.

Example
Input
10
9
1
2
3
4
5
6
10
1434
1000000
Output
156
+1
+5
+10
+21
+39
+46
+146
+63145186
+58116199242129511
+
Note

The first test case is pictured in the statement. The sum of the numbers that fall is $$$$$$1^2 + 2^2 + 3^2 + 5^2 + 6^2 + 9^2 = 1 + 4 + 9 + 25 + 36 + 81 = 156.$$$$$$

In the second test case, only the can labeled $$$1^2$$$ falls, so the answer is $$$1^2=1$$$.

In the third test case, the cans labeled $$$1^2$$$ and $$$2^2$$$ fall, so the answer is $$$1^2+2^2=1+4=5$$$.

In the fourth test case, the cans labeled $$$1^2$$$ and $$$3^2$$$ fall, so the answer is $$$1^2+3^2=1+9=10$$$.

In the fifth test case, the cans labeled $$$1^2$$$, $$$2^2$$$, and $$$4^2$$$ fall, so the answer is $$$1^2+2^2+4^2=1+4+16=21$$$.

+
+ + + + + +
+
+ + +
+ +
H. Don't Blame Me
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sadly, the problem setter couldn't think of an interesting story, thus he just asks you to solve the following problem.

Given an array $$$a$$$ consisting of $$$n$$$ positive integers, count the number of non-empty subsequences for which the bitwise $$$\mathsf{AND}$$$ of the elements in the subsequence has exactly $$$k$$$ set bits in its binary representation. The answer may be large, so output it modulo $$$10^9+7$$$.

Recall that the subsequence of an array $$$a$$$ is a sequence that can be obtained from $$$a$$$ by removing some (possibly, zero) elements. For example, $$$[1, 2, 3]$$$, $$$[3]$$$, $$$[1, 3]$$$ are subsequences of $$$[1, 2, 3]$$$, but $$$[3, 2]$$$ and $$$[4, 5, 6]$$$ are not.

Note that $$$\mathsf{AND}$$$ represents the bitwise AND operation.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$0 \le k \le 6$$$) — the length of the array and the number of set bits that the bitwise $$$\mathsf{AND}$$$ the counted subsequences should have in their binary representation.

The second line of each test case consists of $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 63$$$) — the array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output a single integer — the number of subsequences that have exactly $$$k$$$ set bits in their bitwise $$$\mathsf{AND}$$$ value's binary representation. The answer may be large, so output it modulo $$$10^9+7$$$.

Example
Input
6
5 1
1 1 1 1 1
4 0
0 1 2 3
5 1
5 5 7 4 2
1 2
3
12 0
0 2 0 2 0 2 0 2 0 2 0 2
10 6
63 0 63 5 5 63 63 4 12 13
Output
31
+10
+10
+1
+4032
+15
+

+
+ + + + + +
+
+ + + +
+
+ + +