-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp.j2
269 lines (225 loc) · 7.61 KB
/
main.cpp.j2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Problem: {{ contest_number }}{{ problem_letter_upper }}. {{ problem_title }}
// Problem statement: https://codeforces.com/problemset/problem/{{ contest_number }}/{{ problem_letter_upper }}?locale=en
// Solution author: Artem Zhurikhin (https://codeforces.com/profile/Ashpool)
// Solution license: the Unlicense (Public Domain)
// More solutions: https://github.com/ashpool37/codeforces-cxx20
/* #region templates */
#include <iostream>
#include <charconv>
#include <ranges>
#include <type_traits>
#include <vector>
#include <algorithm>
#include <list>
#include <forward_list>
#include <array>
#include <limits>
#include <iterator>
#include <unordered_set>
using ll = long long;
using ull = unsigned long long;
unsigned digit_to_unsigned(char c) {
char cs[2] = "\0";
cs[0] = c;
unsigned result;
std::from_chars(cs, cs+1, result);
return result;
}
char unsigned_to_digit(unsigned n) {
char cs[2] = "\0";
std::to_chars(cs, cs+1, n);
return cs[0];
}
template<std::integral T>
auto counted(T start, std::size_t count, T step = 1) {
using namespace std::ranges::views;
return iota(0u, count) | transform([=](std::size_t i){ return start + step * static_cast<T>(i); });
}
auto counted(std::size_t count) {
return counted(0ull, count, 1ull);
}
template<std::integral BaseT, std::integral ExponentT>
auto power(BaseT base, ExponentT exponent) {
std::common_type_t<BaseT, ExponentT> result = 1;
while(exponent--) result *= base;
return result;
}
template <typename T>
concept ReadableFromStream = requires(std::istream is, T value) {
{ is >> value };
};
class FromIstreamHelper {
std::istream& input_stream;
std::optional<std::size_t> count;
FromIstreamHelper(FromIstreamHelper const&) = delete;
FromIstreamHelper(FromIstreamHelper&&) = delete;
FromIstreamHelper& operator=(FromIstreamHelper const&) = delete;
FromIstreamHelper& operator=(FromIstreamHelper&&) = delete;
FromIstreamHelper(std::istream& input_stream, std::size_t count)
: input_stream(input_stream), count(count)
{}
FromIstreamHelper(std::istream& input_stream)
: input_stream(input_stream), count(std::nullopt)
{}
friend auto from_istream(std::istream& input_stream);
friend auto from_istream(std::istream& input_stream, std::size_t count);
public:
template<ReadableFromStream T>
operator T() && {
T result;
input_stream >> result;
return result;
}
template<ReadableFromStream T>
operator std::vector<T>() && {
std::vector<T> result;
if(count) {
result.reserve(*count);
std::copy_n(std::istream_iterator<T>(input_stream), *count, std::back_inserter(result));
}
return result;
}
template<ReadableFromStream T>
operator std::list<T>() && {
std::list<T> result;
if(count) {
std::copy_n(std::istream_iterator<T>(input_stream), *count, std::back_inserter(result));
}
return result;
}
template<ReadableFromStream T>
operator std::forward_list<T>() && {
std::forward_list<T> result;
auto result_it = result.before_begin();
auto input_it = std::istream_iterator<T>(input_stream);
if(count) {
result_it = result.insert_after(result_it, *input_it);
for(auto const _ : counted(*count - 1uz)) {
input_it++;
result_it = result.insert_after(result_it, *input_it);
}
}
return result;
}
template<ReadableFromStream T, std::size_t array_size>
operator std::array<T, array_size>() && {
std::array<T, array_size> result;
std::copy_n(std::istream_iterator<T>(input_stream), array_size, std::begin(result));
return result;
}
};
auto from_istream(std::istream& input_stream) {
return FromIstreamHelper(input_stream);
}
auto from_istream(std::istream& input_stream, std::size_t count) {
return FromIstreamHelper(input_stream, count);
}
auto from_cin() {
return from_istream(std::cin);
}
auto from_cin(std::size_t count) {
return from_istream(std::cin, count);
}
std::string line_from_istream(std::istream& input_stream) {
std::string result;
std::getline(input_stream, result);
return result;
}
std::string line_from_cin() {
return line_from_istream(std::cin);
}
void istream_skip_line(std::istream& input_stream) {
input_stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void cin_skip_line() {
istream_skip_line(std::cin);
}
bool space_p(char ch)
{
return static_cast<bool>(std::isspace(static_cast<unsigned char>(ch)));
}
void istream_skip_ws(std::istream& input_stream) {
while(space_p(input_stream.peek())) input_stream.get();
}
void cin_skip_ws() {
istream_skip_ws(std::cin);
}
template<std::integral T, std::output_iterator<T> OutputIt>
void generate_multiples(T limit, T base, OutputIt it) {
T multiple;
for(T i = 2; (multiple = base * i) <= limit; i++) {
*it = multiple;
it++;
}
}
template<std::integral T>
std::vector<T> multiples_to_vector(T limit, T base, bool include_base = true) {
std::vector<T> result;
if(include_base) result.push_back(base);
generate_multiples(limit, base, std::back_inserter(result));
return result;
}
template<std::integral T, std::output_iterator<T> OutputIt>
void generate_primes(T limit, OutputIt it) {
std::unordered_set<T> composite;
for(T i = 2; i <= limit; i++) {
if(not composite.contains(i)) {
*it = i;
it++;
if(i * i <= limit) generate_multiples(limit, i, std::inserter(composite, composite.begin()));
}
}
}
template<std::integral T>
std::vector<T> primes_to_vector(T limit) {
std::vector<T> result;
generate_primes(limit, std::back_inserter(result));
return result;
}
template <typename T>
concept Summable = requires(T lhs, T rhs) {
{ lhs + rhs };
};
template<std::ranges::range R>
requires Summable<std::ranges::range_value_t<R>>
auto sum(R const& range) {
return std::ranges::fold_left(range, 0, std::plus());
}
template<std::unsigned_integral T>
T ceil_div(T lhs, T rhs) {
if(rhs == 0u) throw std::runtime_error("Division by zero");
return (lhs + rhs - 1u) / rhs;
}
template <typename T>
concept WriteableToStream = requires(std::ostream os, T value) {
{ os << value };
};
template<WriteableToStream T, WriteableToStream D>
class OptionalPrintHelper {
std::optional<T> const& optional;
D const& default_value;
OptionalPrintHelper(std::optional<T> const& optional, D const& default_value)
: optional(optional), default_value(default_value)
{}
template<WriteableToStream T_, WriteableToStream D_>
friend std::ostream& operator<<(std::ostream& output_stream, OptionalPrintHelper<T_, D_> const& opt_print_helper);
template<WriteableToStream T_, WriteableToStream D_>
friend OptionalPrintHelper<T_, D_> print_opt(std::optional<T_> const& optional, D_ const& default_value);
};
template<WriteableToStream T, WriteableToStream D>
std::ostream& operator<<(std::ostream& output_stream, OptionalPrintHelper<T, D> const& opt_print_helper) {
if(opt_print_helper.optional.has_value())
output_stream << *(opt_print_helper.optional);
else output_stream << opt_print_helper.default_value;
return output_stream;
}
template<WriteableToStream T, WriteableToStream D>
OptionalPrintHelper<T, D> print_opt(std::optional<T> const& optional, D const& default_value) {
return OptionalPrintHelper(optional, default_value);
}
/* #endregion */
int main() {
unsigned const test_count = from_cin();
for(auto const _ : counted(test_count)) {
}
}