From 96254947699986c59f0dc63d69fd4b76bd3ed43e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 6 Jul 2026 19:08:08 +0200 Subject: Initial commit --- 01_introductory_problems/a.out | Bin 0 -> 18512 bytes 01_introductory_problems/apple_division_1623.cpp | 21 +++ 01_introductory_problems/bit_strings_1617.cpp | 10 ++ .../chessboard_and_queens_1624.cpp | 42 ++++++ 01_introductory_problems/coin_piles_1754.cpp | 18 +++ 01_introductory_problems/creating_strings_1622.cpp | 30 +++++ 01_introductory_problems/digit_queries_2431.cpp | 23 ++++ 01_introductory_problems/gray_code_2205.cpp | 22 +++ 01_introductory_problems/grid_coloring_i_3311.cpp | 17 +++ .../grid_path_description_1625.cpp | 150 +++++++++++++++++++++ 01_introductory_problems/increasing_array_1094.cpp | 12 ++ .../knight_moves_grid_3217.cpp | 40 ++++++ .../mex_grid_construction_3419.cpp | 30 +++++ 01_introductory_problems/missing_number_1083.cpp | 9 ++ 01_introductory_problems/number_spiral_1071.cpp | 27 ++++ .../palindrome_reorder_1755.cpp | 35 +++++ 01_introductory_problems/permutations_1070.cpp | 15 +++ 01_introductory_problems/raab_game_i_3399.cpp | 67 +++++++++ 01_introductory_problems/repetitions_1069.cpp | 20 +++ 01_introductory_problems/string_reorder_1743.cpp | 38 ++++++ 01_introductory_problems/tower_of_hanoi_2165.cpp | 17 +++ 01_introductory_problems/trailing_zeros_1618.cpp | 9 ++ 01_introductory_problems/two_knights_1072.cpp | 24 ++++ 01_introductory_problems/two_sets_1092.cpp | 53 ++++++++ 01_introductory_problems/weird_algorithm_1068.cpp | 12 ++ 25 files changed, 741 insertions(+) create mode 100755 01_introductory_problems/a.out create mode 100644 01_introductory_problems/apple_division_1623.cpp create mode 100644 01_introductory_problems/bit_strings_1617.cpp create mode 100644 01_introductory_problems/chessboard_and_queens_1624.cpp create mode 100644 01_introductory_problems/coin_piles_1754.cpp create mode 100644 01_introductory_problems/creating_strings_1622.cpp create mode 100644 01_introductory_problems/digit_queries_2431.cpp create mode 100644 01_introductory_problems/gray_code_2205.cpp create mode 100644 01_introductory_problems/grid_coloring_i_3311.cpp create mode 100644 01_introductory_problems/grid_path_description_1625.cpp create mode 100644 01_introductory_problems/increasing_array_1094.cpp create mode 100644 01_introductory_problems/knight_moves_grid_3217.cpp create mode 100644 01_introductory_problems/mex_grid_construction_3419.cpp create mode 100644 01_introductory_problems/missing_number_1083.cpp create mode 100644 01_introductory_problems/number_spiral_1071.cpp create mode 100644 01_introductory_problems/palindrome_reorder_1755.cpp create mode 100644 01_introductory_problems/permutations_1070.cpp create mode 100644 01_introductory_problems/raab_game_i_3399.cpp create mode 100644 01_introductory_problems/repetitions_1069.cpp create mode 100644 01_introductory_problems/string_reorder_1743.cpp create mode 100644 01_introductory_problems/tower_of_hanoi_2165.cpp create mode 100644 01_introductory_problems/trailing_zeros_1618.cpp create mode 100644 01_introductory_problems/two_knights_1072.cpp create mode 100644 01_introductory_problems/two_sets_1092.cpp create mode 100644 01_introductory_problems/weird_algorithm_1068.cpp (limited to '01_introductory_problems') diff --git a/01_introductory_problems/a.out b/01_introductory_problems/a.out new file mode 100755 index 0000000..8c1bdbd Binary files /dev/null and b/01_introductory_problems/a.out differ diff --git a/01_introductory_problems/apple_division_1623.cpp b/01_introductory_problems/apple_division_1623.cpp new file mode 100644 index 0000000..fb367b4 --- /dev/null +++ b/01_introductory_problems/apple_division_1623.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +long long md(std::vector& a, long long p, long long t, size_t i) { + if (2*p >= t) + return 2*p - t; + if (i == a.size()) + return t; + return std::min(md(a, p, t, i+1), md(a, p+a[i], t, i+1)); +} + +int main() { + size_t n; + std::cin >> n; + std::vector a(n); + for (size_t i = 0; i < n; i++) + std::cin >> a[i]; + auto s = md(a, 0, std::accumulate(a.begin(), a.end(), (long long)0), 0); + std::cout << s << "\n"; +} diff --git a/01_introductory_problems/bit_strings_1617.cpp b/01_introductory_problems/bit_strings_1617.cpp new file mode 100644 index 0000000..6be46ac --- /dev/null +++ b/01_introductory_problems/bit_strings_1617.cpp @@ -0,0 +1,10 @@ +#include + +int main() { + constexpr long long M = 1e9+7; + long long n, a{1}; + std::cin >> n; + while (--n >= 0) + a = (a * 2) % M; + std::cout << a << std::endl; +} diff --git a/01_introductory_problems/chessboard_and_queens_1624.cpp b/01_introductory_problems/chessboard_and_queens_1624.cpp new file mode 100644 index 0000000..d595232 --- /dev/null +++ b/01_introductory_problems/chessboard_and_queens_1624.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +void set_bit(std::bitset<64>& r, int i, int j) { + if (i >= 0 && i < 8 && j >= 0 && j < 8) + r |= 1ULL << (unsigned long long)(8*i + j); +} + +std::bitset<64> maskall(const std::bitset<64>& b, int i, int j) { + std::bitset<64> r{b}; + r |= 0x0101010101010101ULL << (unsigned long long)j; + for (int k = 0; k < 8; k++) { + set_bit(r, k, k+j-i); + set_bit(r, k, -k+j+i); + } + return r; +} + +long long f(std::bitset<64>& b, int i) { + if (i == 8) + return 1; + long long s{0}; + for (int j = 0; j < 8; j++) { + if (!b[8*i+j]) { + auto rr = maskall(b, i, j); + s += f(rr, i+1); + } + } + return s; +} + +int main() { + std::bitset<64> b{0}; + std::string s; + for (int i = 0; i < 8; i++) { + std::cin >> s; + for (int j = 0; j < 8; j++) + b[8*i+j] = s[j] == '*'; + } + std::cout << f(b, 0) << "\n"; +} diff --git a/01_introductory_problems/coin_piles_1754.cpp b/01_introductory_problems/coin_piles_1754.cpp new file mode 100644 index 0000000..c54cdf1 --- /dev/null +++ b/01_introductory_problems/coin_piles_1754.cpp @@ -0,0 +1,18 @@ +#include +#include + +bool f(int a, int b) { + auto max = std::max(a, b); + auto min = std::min(a, b); + + return max <= 2*min && (2*min - max) % 3 == 0; +} + +int main() { + int t, a, b; + std::cin >> t; + for (int i = 0; i < t; i++) { + std::cin >> a >> b; + std::cout << (f(a, b) ? "YES" : "NO") << std::endl; + } +} diff --git a/01_introductory_problems/creating_strings_1622.cpp b/01_introductory_problems/creating_strings_1622.cpp new file mode 100644 index 0000000..689e083 --- /dev/null +++ b/01_introductory_problems/creating_strings_1622.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +void gen(std::map& a, int count, + std::string start, std::vector& res) { + if (count == 0) + res.push_back(start); + for (auto [k, v] : a) { + if (v > 0) { + a[k]--; + gen(a, count - 1, start + k, res); + a[k]++; + } + } +} + +int main() { + std::string s; + std::map a; + std::vector sol; + std::cin >> s; + for (auto c : s) + a[c]++; + gen(a, s.size(), "", sol); + std::cout << sol.size() << "\n"; + for (auto x : sol) + std::cout << x << "\n"; +} diff --git a/01_introductory_problems/digit_queries_2431.cpp b/01_introductory_problems/digit_queries_2431.cpp new file mode 100644 index 0000000..28c4a2f --- /dev/null +++ b/01_introductory_problems/digit_queries_2431.cpp @@ -0,0 +1,23 @@ +#include + +int f(long long k) { + long long n{1}, p{1}, d{1}, q{9}; + while (p + d*q <= k) { + n += q; + p += d * q; + d++; + q *= 10; + } + long long x{(k-p) / d + n}; + long long m{(k-p) % d}; + for (long long j = 0; j < d-m-1; j++) + x /= (long long)10; + return x % (long long)10; +} + +int main() { + long long k; + std::cin >> k; + while (std::cin >> k) + std::cout << f(k) << "\n"; +} diff --git a/01_introductory_problems/gray_code_2205.cpp b/01_introductory_problems/gray_code_2205.cpp new file mode 100644 index 0000000..4fa8a2d --- /dev/null +++ b/01_introductory_problems/gray_code_2205.cpp @@ -0,0 +1,22 @@ +#include +#include + +void print(int number, int ndigits) { + std::cout << std::format("{0:0{1}b}", number, ndigits) << "\n"; +} + +void print_all(int& start, int digit, int ndigits) { + if (digit == 0) { + print(start, ndigits); + } else { + print_all(start, digit-1, ndigits); + start ^= 1 << (digit-1); + print_all(start, digit-1, ndigits); + } +} + +int main() { + int n, start{0}; + std::cin >> n; + print_all(start, n, n); +} diff --git a/01_introductory_problems/grid_coloring_i_3311.cpp b/01_introductory_problems/grid_coloring_i_3311.cpp new file mode 100644 index 0000000..b6c59a4 --- /dev/null +++ b/01_introductory_problems/grid_coloring_i_3311.cpp @@ -0,0 +1,17 @@ +#include +#include + +int main() { + int n, m; + std::cin >> n >> m; + for (int i = 0; i < n; i++) { + std::string s; + std::cin >> s; + for (int j = 0; j < m; j++) { + char c = 'A' + 2*((i+j)%2); + c += c == s[j]; + std::cout << c; + } + std::cout << "\n"; + } +} diff --git a/01_introductory_problems/grid_path_description_1625.cpp b/01_introductory_problems/grid_path_description_1625.cpp new file mode 100644 index 0000000..b483168 --- /dev/null +++ b/01_introductory_problems/grid_path_description_1625.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include + +/* +The official solution uses the heuristic: if both adjacent squares +in horizontal direction are visited or wall, and both in vertical direction +are not visited (or the other way round), then we stop because we borked +the square. I did not think of this criterion, so to check if the square +is borked I do a full visit from the bottom-left corner. This is too slow, +so I do this only at depths 10, 20, 30 and 40. This is good enough. +*/ + +class Tile { +public: + int i; + int j; + + bool valid() const { return i >= 0 && i < 7 && j >= 0 && j < 7; } + bool end() const { return i == 6 && j == 0; } + bool operator==(const Tile& t) const { return i == t.i && j == t.j; } + Tile u() const { return Tile{i-1, j}; } + Tile d() const { return Tile{i+1, j}; } + Tile l() const { return Tile{i, j-1}; } + Tile r() const { return Tile{i, j+1}; } + static Tile err() { return Tile{-1, -1}; } + + Tile move(char c) const { + if (c == 'U') return u(); + if (c == 'D') return d(); + if (c == 'L') return l(); + if (c == 'R') return r(); + return err(); + } + + std::vector neighbors() const { + return std::vector { u(), d(), l(), r() }; + } +}; + +class Map { +public: + Map() : b(), v(49, 4) { + for (int i = 0; i < 7; i++) { + v[index(Tile{i, 0})]--; + v[index(Tile{i, 6})]--; + v[index(Tile{0, i})]--; + v[index(Tile{6, i})]--; + } + } + + bool visited(Tile t) const { + return !t.valid() || b.test(index(t)); + } + + void set(Tile t) { + if (!t.valid()) return; + b.set(index(t)); + for (auto u : t.neighbors()) + if (u.valid()) + v[index(u)]--; + } + + void reset(Tile t) { + if (!t.valid()) return; + b.reset(index(t)); + for (auto u : t.neighbors()) + if (u.valid()) + v[index(u)]++; + } + + std::vector locked_neighbors(Tile t) const { + std::vector r{}; + for (auto u : t.neighbors()) + if (locked(u)) + r.push_back(u); + return r; + } + + int count() const { return b.count(); } + + bool borked() const { + std::bitset<49> vv{}; + std::queue q; + q.push(Tile{6, 0}); + vv.set(index(Tile{6, 0})); + int c{1}; + while (!q.empty()) { + Tile t = q.front(); + q.pop(); + for (auto u : t.neighbors()) { + if (!visited(u) && !vv.test(index(u))) { + vv.set(index(u)); + c++; + q.push(u); + } + } + } + + return c + count() < 49; + } + +private: + std::bitset<49> b; + std::vector v; + static int index(Tile t) { return 7*t.i + t.j; } + + bool locked(Tile t) const { + return t.valid() && !visited(t) && !t.end() && v[index(t)] < 2; + } +}; + +int f(Map& m, const std::string& s, size_t n, Tile t) { + if (n == 48) return t.end(); + if (m.visited(t) || t.end()) return 0; + + m.set(t); + if (n % 10 == 0 && m.borked()) { + m.reset(t); + return 0; + } + + auto ln = m.locked_neighbors(t); + + int r{0}; + if (s[n] != '?') { + Tile nt = t.move(s[n]); + if (ln.size() == 0 || (ln.size() == 1 && ln[0] == nt)) + r = f(m, s, n+1, nt); + } else { + if (ln.size() == 0) + r = f(m, s, n+1, t.u()) + f(m, s, n+1, t.d()) + + f(m, s, n+1, t.l()) + f(m, s, n+1, t.r()); + if (ln.size() == 1) + r = f(m, s, n+1, ln[0]); + } + + m.reset(t); + return r; +} + +int main() { + Map m; + std::string s; + std::cin >> s; + std::cout << f(m, s, 0, Tile{0, 0}) << "\n"; +} diff --git a/01_introductory_problems/increasing_array_1094.cpp b/01_introductory_problems/increasing_array_1094.cpp new file mode 100644 index 0000000..314c686 --- /dev/null +++ b/01_introductory_problems/increasing_array_1094.cpp @@ -0,0 +1,12 @@ +#include +#include + +int main() { + long long n, prev{0}, sum{0}; + std::cin >> n; + while (std::cin >> n) { + sum += std::max(0, prev - n); + prev = std::max(prev, n); + } + std::cout << sum << std::endl; +} diff --git a/01_introductory_problems/knight_moves_grid_3217.cpp b/01_introductory_problems/knight_moves_grid_3217.cpp new file mode 100644 index 0000000..212e82c --- /dev/null +++ b/01_introductory_problems/knight_moves_grid_3217.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +struct Node { + int i; + int j; + int d; +}; + +int main() { + int n; + std::cin >> n; + std::vector> a(n, std::vector(n, 1e7)); + std::queue q; + + q.push(Node{0, 0, 0}); + while (!q.empty()) { + auto v = q.front(); + q.pop(); + if (v.i < 0 || v.j < 0 || v.i >= n || v.j >= n || a[v.i][v.j] <= v.d) + continue; + a[v.i][v.j] = v.d; + q.push(Node{v.i-2, v.j-1, v.d+1}); + q.push(Node{v.i-2, v.j+1, v.d+1}); + q.push(Node{v.i-1, v.j-2, v.d+1}); + q.push(Node{v.i-1, v.j+2, v.d+1}); + q.push(Node{v.i+2, v.j-1, v.d+1}); + q.push(Node{v.i+2, v.j+1, v.d+1}); + q.push(Node{v.i+1, v.j-2, v.d+1}); + q.push(Node{v.i+1, v.j+2, v.d+1}); + } + + for (auto& v : a) { + for (auto& x : v) + std::cout << x << " "; + std::cout << "\n"; + } +} diff --git a/01_introductory_problems/mex_grid_construction_3419.cpp b/01_introductory_problems/mex_grid_construction_3419.cpp new file mode 100644 index 0000000..feb1171 --- /dev/null +++ b/01_introductory_problems/mex_grid_construction_3419.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +int firstzero(const std::bitset<200>& b) { + for (int i = 0; i < 200; i++) + if (!b.test(i)) + return i; + return -1; +} + +int next(int i, int j, std::vector>& col, + std::vector>& row) { + int r = firstzero(row[i] | col[j]); + row[i][r] = col[j][r] = 1; + return r; +} + +int main() { + int n; + std::cin >> n; + std::vector> col(n), row(n); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) + std::cout << next(i, j, col, row) << " "; + std::cout << "\n"; + } +} diff --git a/01_introductory_problems/missing_number_1083.cpp b/01_introductory_problems/missing_number_1083.cpp new file mode 100644 index 0000000..527abb2 --- /dev/null +++ b/01_introductory_problems/missing_number_1083.cpp @@ -0,0 +1,9 @@ +#include + +int main() { + long long n, m, sum{0}; + std::cin >> n; + while (std::cin >> m) + sum += m; + std::cout << n*(n+1)/2 - sum << std::endl; +} diff --git a/01_introductory_problems/number_spiral_1071.cpp b/01_introductory_problems/number_spiral_1071.cpp new file mode 100644 index 0000000..d4df631 --- /dev/null +++ b/01_introductory_problems/number_spiral_1071.cpp @@ -0,0 +1,27 @@ +#include +#include + +long long f(long long x, long long y) { + long long c = std::max(x, y); + if (c % 2) { + if (y >= x) + return (c-1)*(c-1)+x; + else + return c*c-y+1; + } else { + if (y >= x) + return c*c-x+1; + else + return (c-1)*(c-1)+y; + } +} + +int main() { + int t; + std::cin >> t; + for (int i = 0; i < t; i++) { + long long x, y; + std::cin >> y >> x; + std:: cout << f(x, y) << std::endl; + } +} diff --git a/01_introductory_problems/palindrome_reorder_1755.cpp b/01_introductory_problems/palindrome_reorder_1755.cpp new file mode 100644 index 0000000..8ebf4f4 --- /dev/null +++ b/01_introductory_problems/palindrome_reorder_1755.cpp @@ -0,0 +1,35 @@ +#include +#include + +void printn(char c, int n) { + for (int i = 0; i < n; i++) + std::cout << c; +} + +int main() { + std::string s; + int odd{0}, a['Z'+1] = {0}; + char oddc{0}; + std::cin >> s; + for (auto c : s) + a[(size_t)c]++; + for (char c = 'A'; c <= 'Z'; c++) { + if (a[(size_t)c] % 2) { + odd++; + oddc = c; + } + } + if (odd > 1) { + std::cout << "NO SOLUTION\n"; + } else { + for (char c = 'A'; c <= 'Z'; c++) + if (c != oddc) + printn(c, a[(size_t)c]/2); + if (odd) + printn(oddc, a[(size_t)oddc]); + for (char c = 'Z'; c >= 'A'; c--) + if (c != oddc) + printn(c, a[(size_t)c]/2); + std::cout << std::endl; + } +} diff --git a/01_introductory_problems/permutations_1070.cpp b/01_introductory_problems/permutations_1070.cpp new file mode 100644 index 0000000..4438795 --- /dev/null +++ b/01_introductory_problems/permutations_1070.cpp @@ -0,0 +1,15 @@ +#include + +int main() { + int n; + std::cin >> n; + if (n == 2 || n == 3) { + std::cout << "NO SOLUTION" << std::endl; + } else { + for (int i = 2; i <= n; i += 2) + std::cout << i << " "; + for (int i = 1; i <= n; i += 2) + std::cout << i << " "; + std::cout << std::endl; + } +} diff --git a/01_introductory_problems/raab_game_i_3399.cpp b/01_introductory_problems/raab_game_i_3399.cpp new file mode 100644 index 0000000..bfa5193 --- /dev/null +++ b/01_introductory_problems/raab_game_i_3399.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +class Game { +public: + bool y; + std::vector a; + std::vector b; + int apts = 0; + int bpts = 0; + + Game(int n, bool w) + : y{w}, a{std::vector(n)}, b{std::vector(n)} {} + + void play(int i, int j) { + this->a[this->next] = i; + this->b[this->next] = j; + this->apts += i > j; + this->bpts += j > i; + this->next++; + } + + friend std::ostream& operator<<(std::ostream& os, const Game g) { + if (!g.y) { + os << "NO\n"; + } else { + os << "YES\n"; + for (auto x : g.a) + os << x << " "; + os << "\n"; + for (auto x : g.b) + os << x << " "; + os << "\n"; + } + return os; + } +private: + int next = 0; +}; + +Game play(int n, int a, int b) { + if (a + b > n) + return Game(1, false); + + Game g(n, true); + for (int i = 0; i < n - (a+b); i++) + g.play(i+1, i+1); + for (int i = 0; i < a; i++) + g.play(n-a+i+1, n-(a+b)+i+1); + for (int i = 0; i < b; i++) + g.play(n-(a+b)+i+1, n-b+i+1); + + if (g.apts != a || g.bpts != b) + g.y = false; + + return g; +} + +int main() { + int n, a, b, t; + std::cin >> t; + for (int i = 0; i < t; i++) { + std::cin >> n >> a >> b; + std::cout << play(n, a, b); + } +} diff --git a/01_introductory_problems/repetitions_1069.cpp b/01_introductory_problems/repetitions_1069.cpp new file mode 100644 index 0000000..790f315 --- /dev/null +++ b/01_introductory_problems/repetitions_1069.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +int main() { + char cur{'x'}; + int n{0}, m{0}; + std::string str; + std::cin >> str; + for (auto c : str) { + if (c == cur) { + n++; + } else { + m = std::max(m, n); + n = 1; + cur = c; + } + } + std::cout << std::max(m, n) << std::endl; +} diff --git a/01_introductory_problems/string_reorder_1743.cpp b/01_introductory_problems/string_reorder_1743.cpp new file mode 100644 index 0000000..d22041d --- /dev/null +++ b/01_introductory_problems/string_reorder_1743.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +int main() { + std::array a{}; + std::string s; + std::cin >> s; + for (auto c : s) a[c-'A']++; + + size_t i{0}, j{1}, tot{s.size()}, c{99}; + std::stringstream ss{}; + while (*std::max_element(a.begin(), a.end()) <= tot / 2) { + while (a[i] == 0) i++; + while (a[j] == 0 || j <= i) j++; + c = i == c ? j : i; + ss << (char)('A' + c); + a[c]--; + tot--; + } + + j = std::distance(a.begin(), std::max_element(a.begin(), a.end())); + while (a[j] > 1) { + if (tot == a[j]) { + std::cout << "-1\n"; + return 0; + } + while (a[i] == 0 || i == j) i++; + ss << (char)('A' + j) << (char)('A' + i); + a[i]--; + a[j]--; + tot -= 2; + } + std::cout << ss.str() << (char)('A' + j) << "\n"; +} diff --git a/01_introductory_problems/tower_of_hanoi_2165.cpp b/01_introductory_problems/tower_of_hanoi_2165.cpp new file mode 100644 index 0000000..2b83788 --- /dev/null +++ b/01_introductory_problems/tower_of_hanoi_2165.cpp @@ -0,0 +1,17 @@ +#include + +void do_hanoi(int n, int l, int m, int r) { + if (n != 0) { + do_hanoi(n-1, l, r, m); + std::cout << l << " " << r << "\n"; + do_hanoi(n-1, m, l, r); + } +} + +int main() { + int n; + std::cin >> n; + + std::cout << (1 << n) - 1 << "\n"; + do_hanoi(n, 1, 2, 3); +} diff --git a/01_introductory_problems/trailing_zeros_1618.cpp b/01_introductory_problems/trailing_zeros_1618.cpp new file mode 100644 index 0000000..04cf85b --- /dev/null +++ b/01_introductory_problems/trailing_zeros_1618.cpp @@ -0,0 +1,9 @@ +#include + +int main() { + int n, c{0}; + std::cin >> n; + while (n > 0) + c += (n /= 5); + std::cout << c << std::endl; +} diff --git a/01_introductory_problems/two_knights_1072.cpp b/01_introductory_problems/two_knights_1072.cpp new file mode 100644 index 0000000..15d5f55 --- /dev/null +++ b/01_introductory_problems/two_knights_1072.cpp @@ -0,0 +1,24 @@ +#include +#include + +int main() { + long long n, a; + std::vector b = {0, 0, 6, 28, 96}; + std::cin >> n; + + for (long long k = 1; k <= n; k++) { + if (k <= 4) { + a = b[k]; + } else { + // Both knights in new strip + a += (2*k - 1)*(k - 1) - 2; + + // One knight in new strip, one in previous square + const long long s = (k-1)*(k-1); + a += 5*(s - 2); + a += 4*(s - 3); + a += (2*k - 10)*(s - 4); + } + std::cout << a << std::endl; + } +} diff --git a/01_introductory_problems/two_sets_1092.cpp b/01_introductory_problems/two_sets_1092.cpp new file mode 100644 index 0000000..4314654 --- /dev/null +++ b/01_introductory_problems/two_sets_1092.cpp @@ -0,0 +1,53 @@ +#include +#include + +void printarr(const std::vector& a) { + std::cout << a.size() << std::endl; + for (auto x : a) + std::cout << x << " "; + std::cout << std::endl; +} + +int main() { + int n; + std::cin >> n; + + switch (n % 4) { + case 1: + case 2: + std::cout << "NO\n"; + break; + case 0: + std::cout << "YES\n"; + { + std::vector a(n/2), b(n/2); + for (int i = 0; i < n/4; i++) { + a[2*i] = 4*i+1; + a[2*i+1] = 4*i+4; + b[2*i] = 4*i+2; + b[2*i+1] = 4*i+3; + } + printarr(a); + printarr(b); + } + break; + case 3: + std::cout << "YES\n"; + if (n == 3) { + std::cout << "2\n1 2\n1\n3\n"; + } else { + std::vector a(n/2+1), b(n/2); + a[0] = 1; a[1] = 2; a[2] = 4; a[3] = 7; + b[0] = 3; b[1] = 5; b[2] = 6; + for (int i = 1; i < n/4; i++) { + a[2*i+2] = 4*i+4; + a[2*i+3] = 4*i+7; + b[2*i+1] = 4*i+5; + b[2*i+2] = 4*i+6; + } + printarr(a); + printarr(b); + } + break; + } +} diff --git a/01_introductory_problems/weird_algorithm_1068.cpp b/01_introductory_problems/weird_algorithm_1068.cpp new file mode 100644 index 0000000..35cbdfa --- /dev/null +++ b/01_introductory_problems/weird_algorithm_1068.cpp @@ -0,0 +1,12 @@ +#include + +int main() { + long long n; + std::cin >> n; + + while (n != 1) { + std::cout << n << " "; + n = (n % 2) ? n * 3 + 1 : n / 2; + } + std::cout << 1 << std::endl; +} -- cgit v1.3