// template reference: https://qiita.com/sorachandu/items/041169d34b9f9b99bcf7 // solve() is around line 540 //for library checker //#define ATCODER 1 #include using namespace std; //https://github.com/atcoder/ac-library/blob/864245a00b00dd008d1abfdc239618fdb7d139da/atcoder/dsu.hpp #ifndef ATCODER_DSU_HPP #define ATCODER_DSU_HPP 1 namespace atcoder {struct dsu {public:dsu() : _n(0) {}explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}int merge(int a, int b) {assert(0 <= a && a < _n);assert(0 <= b && b < _n);int x = leader(a), y = leader(b);if (x == y) return x;if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);parent_or_size[x] += parent_or_size[y];parent_or_size[y] = x;return x;}bool same(int a, int b) {assert(0 <= a && a < _n);assert(0 <= b && b < _n);return leader(a) == leader(b);}int leader(int a) {assert(0 <= a && a < _n);return _leader(a);}int size(int a) {assert(0 <= a && a < _n);return -parent_or_size[leader(a)];}std::vector> groups() {std::vector leader_buf(_n), group_size(_n);for (int i = 0; i < _n; i++) {leader_buf[i] = leader(i);group_size[leader_buf[i]]++;}std::vector> result(_n);for (int i = 0; i < _n; i++) {result[i].reserve(group_size[i]);}for (int i = 0; i < _n; i++) {result[leader_buf[i]].push_back(i);}result.erase(std::remove_if(result.begin(), result.end(),[&](const std::vector& v) { return v.empty(); }),result.end());return result;}private:int _n;std::vector parent_or_size;int _leader(int a) {if (parent_or_size[a] < 0) return a;return parent_or_size[a] = _leader(parent_or_size[a]);}};} #endif // ATCODER_DSU_HPP namespace atcoder {} using namespace atcoder; #ifdef ATCODER #include #include #include #include #include #include #include #include #include using mint10 = modint1000000007; using mint = modint998244353; #endif using ll = long long; using ull = unsigned long long; #define INF (1 << 30) #define INFL (1LL << 48) #define INFLL (1LL << 60) #define LL18 (1000000000000000000LL) #define LL9 (1000000000LL) #define walk2(dr, dc) for (auto [dr, dc] : vector>{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}) #define walk3(dr, dc, i) for (auto [dr, dc, i] : vector>{{1, 0, 0}, {0, 1, 1}, {-1, 0, 2}, {0, -1, 3}}) #define walk_selector(_1, _2, _3, NAME, ...) NAME #define walk(...) walk_selector(__VA_ARGS__, walk3, walk2)(__VA_ARGS__) #define walkx(dr, dc) for (auto [dr, dc] : vector>{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}) #define within(l,v,r) (l <= v && v <= r) #define rep2(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep3(i, x, n) for (ll i = (ll)(x); i < (ll)(n); i++) #define rep4(i, x, n, s) for (ll i = (ll)(x); i < (ll)(n); i += s) #define rep_selector(_1, _2, _3, _4, NAME, ...) NAME #define rep(...) rep_selector(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__) #define reprev2(i, n) for (ll i = (ll)(n)-1; i >= 0; i--) #define reprev3(i, x, n) for (ll i = (ll)(n)-1; i >= (ll)(x); i--) #define reprev4(i, x, n, s) for (ll i = (ll)(n)-1; i >= (ll)(x); i -= s) #define reprev_selector(_1, _2, _3, _4, NAME, ...) NAME #define reprev(...) reprev_selector(__VA_ARGS__, reprev4, reprev3, reprev2)(__VA_ARGS__) #define repsubset(i, s) for (ll i = (ll)(s); i >= 0; i--, i &= (i >= 0 ? s : i)) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ansyn(x) cout << (x ? "yes" : "no") << endl #define ansYn(x) cout << (x ? "Yes" : "No") << endl #define ansYN(x) cout << (x ? "YES" : "NO") << endl #define cin1(a) a; cin >> a #define cin2(a, b) a, b; cin >> a >> b #define cin3(a, b, c) a, b, c; cin >> a >> b >> c #define cin4(a, b, c, d) a, b, c, d; cin >> a >> b >> c >> d #define cin5(a, b, c, d, e) a, b, c, d, e; cin >> a >> b >> c >> d >> e #define cin6(a, b, c, d, e, f) a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f #define cin7(a, b, c, d, e, f, g) a, b, c, d, e, f, g; cin >> a >> b >> c >> d >> e >> f >> g #define cin8(a, b, c, d, e, f, g, h) a, b, c, d, e, f, g, h; cin >> a >> b >> c >> d >> e >> f >> g >> h #define cin_selector(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME #define cin(...) cin_selector(__VA_ARGS__, cin8, cin7, cin6, cin5, cin4, cin3, cin2, cin1)(__VA_ARGS__) #ifdef LOCAL #define debug1(x) debug_cerr(#x, x) #define debug2(name, x) debug_cerr(name, x) #define debug_selector(_1, _2, NAME, ...) NAME #define debug(...) debug_selector(__VA_ARGS__, debug2, debug1)(__VA_ARGS__) #define debugs(...) debug_cerr(#__VA_ARGS__, make_tuple(__VA_ARGS__)) #define debugsn(name, ...) debug_cerr(name, make_tuple(__VA_ARGS__)) #define dmsg(x) cerr << "- " << x << endl; #define local if constexpr (false) {} else #else #define debug(...) ; #define debugs(...) ; #define debugsn(...) ; #define dmsg(x) ; #define local if constexpr (true) {} else #define LOCAL 0 #endif #ifndef CPHEXT #define CPHEXT 0 #endif constexpr char el = '\n'; constexpr char sp = ' '; #include #include #include using namespace __gnu_pbds; template> using indexed_set = tree; template using pqueue = priority_queue,T2>; template auto Vec(T init, int n, Args... args) { if constexpr (sizeof...(args) == 0) { return std::vector(n, init); } else { return std::vector(n, Vec(init, args...)); } } template struct is_tuple_like : std::false_type {}; template struct is_tuple_like::value)>> : std::true_type {}; template void read_element(T& val) { if constexpr (is_tuple_like::value) { std::apply([](auto&... args) { (read_element(args), ...); }, val); } else { std::cin >> val; } } template inline vector vread(int n, int offset = 0) { vector result(n + offset); for (int i = 0; i < n; i++) { read_element(result[i + offset]); } return result; } template inline vector> vread2d(int h, int w, int offset = 0, int offset2 = -1) { if (offset2 == -1) offset2 = offset; vector> result(h + offset, vector(w + offset2)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { read_element(result[i + offset][j + offset2]); } } return result; } std::ostream& operator<<(std::ostream& os, __uint128_t n) { if (n == 0) return os << "0"; std::string s; while (n > 0) { s += (char)('0' + (n % 10)); n /= 10; } std::reverse(s.begin(), s.end()); return os << s; } std::ostream& operator<<(std::ostream& os, __int128_t n) { if (n == 0) return os << "0"; if (n < 0) { os << "-"; n = -n; } return os << (__uint128_t)n; } std::istream& operator>>(std::istream& is, __int128_t& n) { std::string s; is >> s; n = 0; bool neg = false; for (size_t i = 0; i < s.size(); i++) { if (s[i] == '-') neg = true; else n = n * 10 + (s[i] - '0'); } if (neg) n = -n; return is; } std::istream& operator>>(std::istream& is, __uint128_t& n) { std::string s; is >> s; n = 0; for (size_t i = 0; i < s.size(); i++) { n = n * 10 + (s[i] - '0'); } return is; } template inline T div_ceil(T a, T b) { return (a + b - 1) / b; } template inline bool chmax(T &ref, Args&&... args) { auto old = ref; ref = std::max({ref, (T)args...}); return old != ref; } template inline bool chmin(T &ref, Args&&... args) { auto old = ref; ref = std::min({ref, (T)args...}); return old != ref; } template inline T set1(T &val, Args&&... bits) { return (val | ... | (1LL< inline T set0(T &val, Args&&... bits) { return (val & ... & ~(1LL< inline T flip(T &val, Args&&... bits) { return (val ^ ... ^ (1LL< inline bool isset(T &val, Args&&... bits) { return (... && (bool)(val & (1LL< inline bool isset_any(T &val, Args&&... bits) { return (... || (bool)(val & (1LL< inline bool isnset(T &val, Args&&... bits) { return (... && !(bool)(val & (1LL< inline bool isnset_any(T &val, Args&&... bits) { return (... || !(bool)(val & (1LL<& f) { while (std::abs(ok - ng) > 1) { auto mid = std::min(ok, ng) + std::abs(ok - ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } template struct CustomComp { template bool operator()(const T& lhs, const T& rhs) const { bool result = false; bool found = false; (( [&]() { if (found) return; constexpr int idx = (Is > 0 ? Is : -Is) - 1; auto&& val_l = std::get(lhs); auto&& val_r = std::get(rhs); if (Is > 0) { if (val_l < val_r) { result = true; found = true; } else if (val_r < val_l) { result = false; found = true; } } else { if (val_r < val_l) { result = true; found = true; } else if (val_l < val_r) { result = false; found = true; } } }() ), ...); return result; } }; // 正のとき l constexpr CustomComp comp; // 正のとき l using Comp = decltype(comp); void multicases(function f) { int T; cin >> T; for (int t = 0; t < T; t++) { f(); } } template concept Streamable = requires(std::ostream& os, T x) { os << x; }; template concept Container = std::ranges::range && !Streamable; template concept ValStreamable = requires(std::ostream& os, T x) { os << x.val(); }; #ifdef ATCODER template struct is_acl_segtree : std::false_type {}; template struct is_acl_segtree> : std::true_type {}; template struct is_acl_lazy_segtree : std::false_type {}; template struct is_acl_lazy_segtree> : std::true_type {}; #endif struct DebugImpl { template static void run(const T& value, int nest) { if constexpr (ValStreamable) { run(value.val(), nest); } else if constexpr (Streamable) { if constexpr (std::is_unsigned_v) { if (value >= INFL) std::cerr << "INF"; else if (value == INF) std::cerr << "inf"; else std::cerr << value; } else if constexpr (std::is_integral_v) { if (value >= INFL) std::cerr << "INF"; else if (value <= -INFL) std::cerr << "-INF"; else if (value == INF) std::cerr << "inf"; else if (value == -INF) std::cerr << "-inf"; else std::cerr << value; } else std::cerr << value; } else if constexpr (Container) { std::cerr << std::string(nest, ' ') << "{" << endl << std::string(nest + 1, ' '); bool first = true; for (const auto& v : value) { if (!first) std::cerr << ", "; run(v, nest + 1); first = false; } std::cerr << endl << std::string(nest, ' ') << "}"; } #ifdef ATCODER else if constexpr (is_acl_segtree::value || is_acl_lazy_segtree::value) { auto& nc_value = const_cast(value); int n = nc_value.max_right(0, [](auto){ return true; }); std::cerr << std::string(nest, ' ') << "{"; for (int i = 0; i < n; i++) { if (i > 0) std::cerr << ", "; run(nc_value.get(i), nest + 1); } std::cerr << "}"; } #endif else { handle_tuple_or_pair(value, nest); } } template static void handle_tuple_or_pair(const T& value, int nest) { if constexpr (requires { typename tuple_size::type; } && !Container) { std::cerr << "["; std::apply([&](const auto&... args) { size_t n = 0; ((std::cerr << (n++ == 0 ? "" : ", "), run(args, nest)), ...); }, value); std::cerr << "]"; } else { std::cerr << "?"; } } }; void debug_cerr_vonly(auto const& value, int nest) { DebugImpl::run(value, nest); } void debug_cerr(std::string name, auto value) { std::cerr << "# " << name << " = "; debug_cerr_vonly(value, 0); std::cerr << endl; } template struct shifted_vector : std::vector { int origin; shifted_vector(int l, int r) : std::vector(r - l + 1), origin(-l) {} T& operator[](int i) { return std::vector::at(i + origin); } }; namespace ahc { std::chrono::system_clock::time_point now() { return std::chrono::system_clock::now(); } double elapsed(std::chrono::system_clock::time_point start) { auto now = ahc::now(); return std::chrono::duration_cast(now - start).count(); } unsigned int rand() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned int t = x ^ (x << 11); x = y; y = z; z = w; return w = ((w ^ (w >> 19)) ^ (t ^ (t >> 8))); } bool sa_min(long long prev_score, long long new_score, double current_time, double end_time, double start_temp, double end_temp) { if (new_score < prev_score) return true; double progress = current_time / end_time; if (progress >= 1.0) progress = 1.0; double temp = start_temp + (end_temp - start_temp) * progress; if (temp <= 1e-9) return false; double delta = (double)(new_score - prev_score); double prob = std::exp(-delta / temp); double p = (double)(rand() % 100000) / 100000; return p < prob; } bool sa_max(long long prev_score, long long new_score, double current_time, double end_time, double start_temp, double end_temp) { return sa_min(-prev_score, -new_score, current_time, end_time, start_temp, end_temp); } } class Random { public: std::mt19937_64 engine; Random() { std::random_device seed_gen; engine.seed(seed_gen()); } long long gen_ll(long long min, long long max) { std::uniform_int_distribution dist(min, max); return dist(engine); } std::string gen_str(std::string chars, int length) { std::ostringstream oss; int arrlen = chars.length(); for (int i = 0; i < length; i++) { oss << chars[gen_ll(0, arrlen - 1)]; } return oss.str(); } }; class GraphGenerator { Random random; std::stringstream& ss; public: std::map> graph; std::set> edges; std::set> unique_edges; atcoder::dsu uf; //デフォルトは単純無向グラフ bool loop = false; bool multigraph = false; bool directed = false; bool cycle = true; bool connected = true; GraphGenerator(std::stringstream& ss_in): ss(ss_in) {} long long build_tree(long long n_min, long long n_max) { assert(n_min >= 2); assert(n_max >= 2); assert(n_max >= n_min); auto loop = false, multigraph = false, directed = false, cycle = false; std::swap(this->loop, loop); std::swap(this->multigraph, multigraph); std::swap(this->directed, directed); std::swap(this->cycle, cycle); long long n, m; do { n = random.gen_ll(n_min, n_max); m = n - 1; } while (!try_build(n, m)); std::swap(this->loop, loop); std::swap(this->multigraph, multigraph); std::swap(this->directed, directed); std::swap(this->cycle, cycle); return n; } long long build_tree(long long n_max) { return build_tree(2, n_max); } pair build(long long n_min, long long n_max, long long m_min, long long m_max) { assert(n_min >= 2); assert(n_max >= 2); assert(n_max >= n_min); assert(m_min >= 1); assert(m_max >= 1); assert(m_max >= m_min); long long n, m; do { n = random.gen_ll(n_min, n_max); m = random.gen_ll(m_min, m_max); } while (!try_build(n, m)); return {n, m}; } pair build(long long n_max, long long m_max) { return build(2, n_max, 1, m_max); } bool try_build(long long n, long long m) { std::vector nodes(n); std::iota(nodes.begin(), nodes.end(), 1); for (int i = n; i < 2 * m; i++) { auto node = random.gen_ll(1, n); nodes.push_back(node); } std::ranges::shuffle(nodes, random.engine); bool success = true; graph.clear(); edges.clear(); unique_edges.clear(); uf = atcoder::dsu(n+1); for (int i = 0; i < 2 * m; i += 2) { auto u = nodes[i]; auto v = nodes[i + 1]; if (!loop && u == v) { success = false; break; } if (!multigraph && edges.contains({u, v})) { success = false; break; } if (!cycle && !directed && uf.same(u, v)) { success = false; break; } graph[u].push_back(v); edges.insert({u, v}); unique_edges.insert({u, v}); if (!directed) { graph[v].push_back(u); edges.insert({v, u}); } uf.merge(u, v); } if (!success) return false; if (connected) { for (int i = 2; i <= n; i++) { if (!uf.same(1, i)) return false; } } if (!cycle && directed) { set seen, finished; function dfs = [&](int u){ seen.insert(u); for (auto v : graph[u]) { if (finished.contains(v)) continue; if (seen.contains(v) && !finished.contains(v)) return true; if (dfs(v)) return true; } finished.insert(u); return false; }; for (int i = 1; i <= n; i++) { if (!finished.contains(i) && dfs(i)) { success = false; break; } } } return success; } void put_edges(function after = NULL) { auto itr = unique_edges.begin(); while (itr != unique_edges.end()) { if (itr != unique_edges.begin()) ss << endl; auto [u, v] = *itr; ss << u << " " << v << " "; if (after != NULL) after(); itr++; } } }; class CaseGenerator { std::stringstream& ss; public: Random random; CaseGenerator(std::stringstream& ss_in): ss(ss_in) {} void lb() { ss << endl; } template void put(T value) { ss << value << " "; } long long put_ll(long long min, long long max) { auto value = random.gen_ll(min, max); ss << value << " "; return value; } long long put_ll(long long min, long long max, function check) { long long value; do { value = random.gen_ll(min, max); } while (!check(value)); ss << value << " "; return value; } std::string put_str(std::string chars, int length) { auto value = random.gen_str(chars, length); ss << value << " "; return value; } std::string put_str(std::string chars, int length, function check) { std::string value; do { value = random.gen_str(chars, length); } while (!check(value)); ss << value << " "; return value; } GraphGenerator make_graph() { return GraphGenerator(ss); } }; // *-*-*-*-*-*-*-*-*-*-*-* // // _ // // ___ ___ | |_ _____ // // / __|/ _ \| \ \ / / _ \ // // \__ \ (_) | |\ V / __/ // // |___/\___/|_| \_/ \___| // // // // *-*-*-*-*-*-*-*-*-*-*-* // //#define int ll //#pragma GCC target("avx") //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") #define MODE -1 // -2: test(check) -1: test(naive) 0:solve 1:naive 2:check 3:next_case 4:jikken #define SHOW_EVERY_TESTCASES false #define HIDE_CERR false void solve() { multicases([&](){ ll cin(k); if(k>100)k=100; string cin(s); ll len=s.length(); s=string(k,s[0])+s; s=s.substr(0,len); cout<= 0) { if (LOCAL == 0 || CPHEXT == 1 || MODE == 0) solve();else if (MODE == 1) naive();else if (MODE == 2) check();else if (MODE == 4) jikken();else if (MODE == 3) {stringstream ss;CaseGenerator cg(ss);next_case(cg);cout << ss.str() << endl;}}else {string discarded_input;auto cin_rdbuf = cin.rdbuf();auto cout_rdbuf = cout.rdbuf();istream std_in(cin_rdbuf);ostream std_out(cout_rdbuf);stringstream ss_in;stringstream ss_out;cin.rdbuf(ss_in.rdbuf());cout.rdbuf(ss_out.rdbuf());stringstream ss_gen;CaseGenerator cg(ss_gen);auto clear_ss = [&](stringstream& ss){ss.str("");ss.clear(std::stringstream::goodbit);};auto read_ss = [&](stringstream& ss){auto result = ss.str();clear_ss(ss);return result;};auto generate = [&]{next_case(cg);ss_gen << endl;return read_ss(ss_gen);};std_out << "- Example of Generated Case:" << endl;std_out << generate() << endl;stringstream ans_solve_ss;string ans_solve;int i_target = 1;for (int i = 1;; i++) {auto testcase = generate();if (SHOW_EVERY_TESTCASES) {std_out << "- Case " << i << ":" << endl;std_out << testcase << endl;}ss_in << testcase;solve();ans_solve_ss << read_ss(ss_out);ans_solve = ans_solve_ss.str();clear_ss(ss_in);if (MODE == -1){stringstream ans_naive_ss;string ans_naive;ss_in << testcase;naive();ans_naive_ss << read_ss(ss_out);ans_naive = ans_naive_ss.str();clear_ss(ss_in);bool pass;while (true) {string s_solve, s_naive;ans_solve_ss >> s_solve;ans_naive_ss >> s_naive;if (s_solve != s_naive) {pass = false;break;}if (ans_solve_ss.eof() && ans_naive_ss.eof()) {pass = true;break;}else if (ans_solve_ss.eof() || ans_naive_ss.eof()) {pass = false;break;}}clear_ss(ans_solve_ss);clear_ss(ans_naive_ss);if (!pass) {std_out << "! Counterexample has Found:" << endl;std_out << testcase << endl;std_out << "- Answer of solve():" << endl;std_out << ans_solve << endl;std_out << "- Answer of naive():" << endl;std_out << ans_naive << endl;std_in >> discarded_input;std_out << endl;i--;continue;}}else if (MODE == -2) {stringstream out_check_ss;string out_check;ss_in << testcase << endl;ss_in << ans_solve;auto pass = check();out_check_ss << read_ss(ss_out);out_check = out_check_ss.str();clear_ss(ss_in);clear_ss(ans_solve_ss);clear_ss(out_check_ss);if (!pass) {std_out << "! Counterexample has Found:" << endl;std_out << testcase << endl;std_out << "- Answer of solve():" << endl;std_out << ans_solve << endl;std_out << "- Output of check():" << endl;std_out << out_check << endl;std_in >> discarded_input;std_out << endl;i--;continue;}}if (i == i_target) {std_out << "- " << i << " Cases Passed" << endl << endl;i_target *= 2;}}} } // modの取り忘れがないか? // 998と10^9+7を間違えてないか? // 計算途中で値がオーバーフローしないか? // bit全探索で1<<60とかやってないか?(1LL<<60) // 入力制約がllの範囲でオーバーフローしていないか? // 入力の型を間違えていないか?(ll cin(s)とか) // 定数倍 map→unordered_map→vector // 参照絡みで事故ってないか?