#line 1 "code.cpp" /*#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops")//*/ //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #line 2 "/home/wa_haya_exe/CP_Library/C++/template.hpp" #ifndef TEMPLATE #define TEMPLATE #endif #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wdeprecated-copy" #include #include #include namespace VvyLw { inline void wa_haya_exe() noexcept { std::cin.tie(nullptr) -> sync_with_stdio(false); } void solve(); } // VvyLw std::mt19937 EhaL(std::hash()("Huitloxopetl")); std::mt19937 Random() { std::random_device seed_gen; std::mt19937 engine {seed_gen()}; return engine; } using Timer = std::chrono::system_clock::time_point; [[maybe_unused]] Timer start, stop; #if local void now(Timer &t){ t = std::chrono::system_clock::now(); } void time(const Timer &t1, const Timer &t2){ std::cerr << std::chrono::duration_cast(t2-t1).count() << "ms\n"; } #else [[maybe_unused]] void now(Timer &t){ void(0); } [[maybe_unused]] void time(const Timer &t1, const Timer &t2){ void(0); } #endif // -------------------------------------------------------------------------------------------------------------- #define overload4(_1,_2,_3,_4,name,...) name #define overload3(_1,_2,_3,name,...) name #define rep1(n) for(ll i=0; i<(n); ++i) #define rep2(i,n) for(ll i=0; i<(n); ++i) #define rep3(i,a,b) for(ll i=(a); i<=(b); ++i) #define rep4(i,a,b,c) for(ll i=(a); i<=(b); i+=(c)) #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__) #define rvp1(n) for(ll i=(n); --i>=0;) #define rvp2(i,n) for(ll i=(n); --i>=0;) #define rvp3(i,a,b) for(ll i=(a); i>=(b); i--) #define rvp4(i,a,b,c) for(ll i=(a); i>=(b); i-=(c)) #define rvp(...) overload4(__VA_ARGS__,rvp4,rvp3,rvp2,rvp1)(__VA_ARGS__) #define all1(v) v.begin(),v.end() #define all2(v,a) v.begin(),v.begin()+a #define all3(v,a,b) v.begin()+a,v.begin()+b #define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__) #define rall1(v) v.rbegin(),v.rend() #define rall2(v,a) v.rbegin(),v.rbegin()+a #define rall3(v,a,b) v.rbegin()+a,v.rbegin()+b #define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__) #define each1(elem,v) for(auto &elem: v) #define each2(x,y,v) for(auto &[x,y]: v) #define each3(x,y,z,v) for(auto &[x,y,z]: v) #define each(...) overload4(__VA_ARGS__,each3,each2,each1)(__VA_ARGS__) #define sqrp1(n) for(ll i=0; i*i<(n); ++i) #define sqrp2(i,n) for(ll i=0; i*i<(n); ++i) #define sqrp3(i,a,b) for(ll i=(a); i*i<=(b); ++i) #define sqrp(...) overload3(__VA_ARGS__,sqrp3,sqrp2,sqrp1)(__VA_ARGS__) using ll = long long; using ld = long double; using uint = unsigned; using ul = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; template using L = std::numeric_limits; constexpr int dx[]={0,0,0,-1,1,-1,-1,1,1}; constexpr int dy[]={0,-1,1,0,0,-1,1,-1,1}; constexpr int MOD = 0x3b800001; constexpr int M0D = 1e9 + 7; constexpr int INF = 1 << 30; constexpr ll LINF = (1LL << 61) - 1; constexpr ld DINF = L::infinity(); template constexpr T LIM = L::max(); #if __cplusplus >= 202100L constexpr ld PI = std::numbers::pi; constexpr ld E = std::numbers::e; #else const ld PI = std::acos(-1); const ld E = 2.718281828459045; #endif namespace vectors { template using V = std::vector; using vi = V; using vu = V
    ; using vd = V; using vc = V; using vs = V; using vb = V; using wi = V; using wu = V; using wd = V; using wc = V; using ws = V; using wb = V; template inline V ndiv(T&& n, U&& v) noexcept { return V(std::forward(n), std::forward(v)); } template inline decltype(auto) ndiv(T&& n, Ts&&... v) noexcept { return V(v)...))>(std::forward(n), ndiv(std::forward(v)...)); } template constexpr V& operator++(V& v) noexcept { each(el,v) el++; return v; } template constexpr V& operator--(V& v) noexcept { each(el,v) el--; return v; } template constexpr V& operator+=(V& v, const U x) noexcept { each(el,v) el+=x; return v; } template constexpr V& operator-=(V& v, const U x) noexcept { each(el,v) el-=x; return v; } template constexpr V& operator*=(V& v, const U x) noexcept { each(el,v) el*=x; return v; } template constexpr V& operator/=(V& v, const U x) noexcept { each(el,v) el/=x; return v; } template constexpr V& operator%=(V& v, const U x) noexcept { each(el,v) el%=x; return v; } template constexpr V operator+(const V& v, const U x) noexcept { V res = v; res+=x; return res; } template constexpr V operator-(const V& v, const U x) noexcept { V res = v; res-=x; return res; } template constexpr V operator*(const V& v, const U x) noexcept { V res = v; res*=x; return res; } template constexpr V operator/(const V& v, const U x) noexcept { V res = v; res/=x; return res; } template constexpr V operator%(const V& v, const U x) noexcept { V res = v; res%=x; return res; } } // vectors using namespace vectors; namespace pairs { template using P = std::pair; template using PP = P; using pi = PP; using pd = PP; using pc = PP; using ps = PP; template constexpr PP operator+(const PP& a, const PP& b) noexcept { return {a.first + b.first, a.second + b.second}; } template constexpr PP operator-(const PP& a, const PP& b) noexcept { return {a.first - b.first, a.second - b.second}; } template constexpr PP operator-(const PP& a) noexcept { return {-a.first, -a.second}; } template constexpr PP operator*(const PP& a, const U& b) noexcept { return {a.first * b, a.second * b}; } template constexpr PP operator/(const PP& a, const U& b) noexcept { return {a.first / b, a.second / b}; } template constexpr PP& operator+=(PP& a, const PP& b) noexcept { return a = a + b; } template constexpr PP& operator-=(PP& a, const PP& b) noexcept { return a = a - b; } template constexpr PP& operator*=(PP& a, const U& b) noexcept { return a = a * b; } template PP& operator/=(PP& a, const U& b) noexcept { return a = a / b; } template constexpr bool operator==(const PP &p, const PP &q) noexcept { return p.first==q.first && p.second==q.second; } template constexpr bool operator!=(const PP &p, const PP &q) noexcept { return !(p==q); } template constexpr bool operator<(const PP &p, const PP &q) noexcept { if(p.first==q.first) return p.second constexpr bool operator<=(const PP &p, const PP &q) noexcept { if(p.first==q.first) return p.second<=q.second; return p.first constexpr bool operator>(const PP &p, const PP &q) noexcept { if(p.first==q.first) return p.second>q.second; return p.first>q.first; } template constexpr bool operator>=(const PP &p, const PP &q) noexcept { if(p.first==q.first) return p.second>=q.second; return p.first>q.first; } template constexpr bool operator==(const P &p, const P &q) noexcept { return p.first==q.first && p.second==q.second; } template constexpr bool operator!=(const P &p, const P &q) noexcept { return !(p==q); } template constexpr bool operator<(const P &p, const P &q) noexcept { if(p.first==q.first) return p.second constexpr bool operator<=(const P &p, const P &q) noexcept { if(p.first==q.first) return p.second<=q.second; return p.first constexpr bool operator>(const P &p, const P &q) noexcept { if(p.first==q.first) return p.second>q.second; return p.first>q.first; } template constexpr bool operator>=(const P &p, const P &q) noexcept { if(p.first==q.first) return p.second>=q.second; return p.first>q.first; } template inline PP rotate(const PP& a){ return {-a.second, a.first}; } // 90 degree ccw template inline pd rotate(const PP& a, const int ang) { assert(0<=ang && ang<360); const ld rad=PI*ang/180; return {a.first*std::cos(rad)-a.second*std::sin(rad), a.first*std::sin(rad)+a.second*std::cos(rad)}; } template inline T dot(const PP& a, const PP& b){ return a.first * b.first + a.second * b.second; } template inline T cross(const PP& a, const PP& b){ return dot(rotate(a), b); } template inline T square(const PP& a){ return dot(a, a); } template inline ld grad(const PP& a){ assert(a.first); return 1.0L * a.second / a.first; } template inline ld abs(const PP& a){ return std::hypotl(a.first, a.second); } template inline T lcm(const PP& a){ return std::lcm(a.first, a.second); } template inline T gcd(const PP& a){ return std::gcd(a.first, a.second); } template inline PP extgcd(const PP &p) { T x=1,y=0,t1=0,t2=0,t3=1,a,b; std::tie(a,b)=p; while(b) { t1=a/b,a-=t1*b; std::swap(a,b); x-=t1*t2; std::swap(x,t2); y-=t1*t3; std::swap(y,t3); } return {x,y}; } template inline PP normalize(PP a) { if(a == PP{}) return a; a /= gcd(a); if(a < PP{}) a = -a; return a; } template inline P swap(const P &p){ P ret={p.second,p.first}; return ret; } template inline V> swap(const V> &vp) { V> ret; each(el,vp) ret.emplace_back(swap(el)); return ret; } template inline V first(const V> &vp) { V res; each(el,vp) res.emplace_back(el.first); return res; } template inline V second(const V> &vp) { V res; each(el,vp) res.emplace_back(el.second); return res; } } // pairs using namespace pairs; template using ti = std::array; using tri = ti<3>; template using pq = std::priority_queue; template using pqr = std::priority_queue,std::greater>; template using Tree = __gnu_pbds::tree,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template using TREE = __gnu_pbds::tree,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template inline bool chmax(T& a, const U& b){ if(a inline bool chmin(T& a, const U& b){ if(a>b){ a=b; return 1; } return 0; } template inline bool overflow_if_add(const T a, const U b){ return (LIM-a) inline bool overflow_if_mul(const T a, const U b){ return (LIM/a)sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } template std::istream& operator>>(std::istream &is, P &p){ is >> p.first >> p.second; return is; } template std::istream& operator>>(std::istream &is, std::array &a){ each(el,a) is >> el; return is; } template std::istream& operator>>(std::istream &is, V &v){ each(el,v) is >> el; return is; } template std::istream& operator>>(std::istream &is, std::deque &dq){ each(el,dq) is >> el; return is; } template inline bool in(T& x){ std::cin >> x; return 1; } template inline bool in(Head& head, Tail&... tail){ in(head); in(tail...); return 1; } template std::ostream& operator<<(std::ostream &os, const P &p){ os << p.first << ' ' << p.second; return os; } template std::ostream& operator<<(std::ostream &os, const std::array &a){ if(a.size()){ os << a.front(); for(auto i=a.begin(); ++i!=a.end();){ os << ' ' << *i; } } return os; } template std::ostream& operator<<(std::ostream &os, const V &v){ if(v.size()){ os << v.front(); for(auto i=v.begin(); ++i!=v.end();){ os << ' ' << *i; } } return os; } template std::ostream& operator<<(std::ostream &os, const std::map &m){ if(m.size()){ os << m.begin()->first << ' ' << m.begin()->second; for(auto i=m.begin(); ++i!=m.end();){ os << '\n' << i->first << ' ' << i->second; } } return os; } template std::ostream& operator<<(std::ostream &os, const std::set &st){ if(st.size()){ os << *st.begin(); for(auto i=st.begin(); ++i!=st.end();){ os << ' ' << *i; } } return os; } template std::ostream& operator<<(std::ostream &os, const std::multiset &ms){ if(ms.size()){ os << *ms.begin(); for(auto i=ms.begin(); ++i!=ms.end();){ os << ' ' << *i; } } return os; } template std::ostream& operator<<(std::ostream &os, const std::deque &dq){ if(dq.size()){ os << dq.front(); for(auto i=dq.begin(); ++i!=dq.end();){ os << ' ' << *i; } } return os; } inline void out(){ std::cout << '\n'; } template inline void out(const T& x){ std::cout << x << '\n'; if(flush) std::cout.flush(); } template inline void out(const Head& head, const Tail&... tail){ std::cout << head << ' '; out(tail...); } template inline void vout(const T& v){ std::cout << v << '\n'; if(flush) std::cout.flush(); } template inline void vout(const V& v){ rep(v.size()) std::cout << v[i] << '\n'; if(flush) std::cout.flush(); } template inline void vout(const Head& head, const Tail&... tail){ std::cout << head << '\n'; vout(tail...); } inline void fix(const short x){ std::cout << std::fixed << std::setprecision(x); } inline void alpha(){ std::cout << std::boolalpha; } #define INT(...) int __VA_ARGS__; in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; in(__VA_ARGS__) #define UL(...) ul __VA_ARGS__; in(__VA_ARGS__) #define LD(...) ld __VA_ARGS__; in(__VA_ARGS__) #define CHR(...) char __VA_ARGS__; in(__VA_ARGS__) #define STR(...) std::string __VA_ARGS__; in(__VA_ARGS__) #define VEC(type,name,size) V name(size); in(name) #define WEC(type,name,h,w) V> name(h,V(w)); in(name) #define fin(...) do{ out(__VA_ARGS__); return; }while(false) } // IO using namespace IO; namespace zia_qu { #ifdef local //https://gist.github.com/naskya/1e5e5cd269cfe16a76988378a60e2ca3 #include <../debug_print.hpp> #define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) static_cast(0) #endif #define elif else if #define eid(el,v) size_t(&el-&v[0]) #define bif(bit,tar) if(((bit)>>(tar))&1) #define nxp(x) std::next_permutation(all(x)) #define prp(x) std::prev_permutation(all(x)) #define strpl(s,a,b) std::regex_replace(s,std::regex(a),b) #define rgxsr(s,rgx) std::regex_search(s,std::regex(rgx)) inline void YES(const bool ok=1){ out(ok?"YES":"NO"); } inline void NO(const bool ok=1){ YES(!ok); } inline void Yes(const bool ok=1){ out(ok?"Yes":"No"); } inline void No(const bool ok=1){ Yes(!ok); } inline void yes(const bool ok=1){ out(ok?"yes":"no"); } inline void no(const bool ok=1){ yes(!ok); } template inline T sqr(const T x){ return x*x; } template inline T cub(const T x){ return x*x*x; } template inline T Mod(const T x, const T m){ return (x+m)%m; } template inline T Pow(T a, T b, const T mod=0) { T res=1; if(mod) { res%=mod; a%=mod; } while(b>0) { if(b&1) res*=a; if(mod) res%=mod; a*=a; if(mod) a%=mod; b>>=1; } return res; } inline ll Ceil(const ld x, const ll m){ return std::ceil(x/m); } inline ld Round(const ld x, const ll m, const short fx=0){ if(!fx) return std::round(x/m); const ul y=Pow
      (10,fx); return std::round((x*y)/m)/y; } inline ld Log(const ll x, const ld base=2){ return std::log2(x)/std::log2(base); } inline int bitdigit(const ll x){ return 64-__builtin_clzll(x); } inline int popcnt(const ll x){ return __builtin_popcountll(x); } inline int fione(const ll x){ return __builtin_ffsll(x); } inline int zrcnt(const ll x){ return __builtin_ctzll(x); } template inline bool scope(const T a, const T x, const T b){ return a <= x && x <= b; } inline bool isupper(const char c){ return std::isupper(c); } inline bool isupper(const std::string &s){ bool ok=1; each(el,s) ok&=isupper(el); return ok; } inline bool islower(const char c){ return std::islower(c); } inline bool islower(const std::string &s){ bool ok=1; each(el,s) ok&=islower(el); return ok; } inline bool isalpha(const char c){ return std::isalpha(c); } inline bool isalpha(const std::string &s){ bool ok=1; each(el,s) ok&=isalpha(el); return ok; } inline bool isdigit(const char c){ return std::isdigit(c); } inline bool isdigit(const std::string &s){ bool ok=1, neg=s.front()=='-'; each(el,s){ if(neg){ neg=0; continue; } ok&=isdigit(el); } return ok; } inline bool isalnum(const char c){ return std::isalnum(c); } inline bool isalnum(const std::string &s){ bool ok=1; each(el,s) ok&=isalnum(el); return ok; } inline bool isspace(const char c){ return std::isspace(c); } inline bool isspace(const std::string &s){ bool ok=1; each(el,s) ok&=isspace(el); return ok; } inline bool ispunct(const char c){ return std::ispunct(c); } inline bool ispunct(const std::string &s){ bool ok=1; each(el,s) ok&=ispunct(el); return ok; } inline bool isprint(const char c){ return std::isprint(c); } inline bool isprint(const std::string &s){ bool ok=1; each(el,s) ok&=isprint(el); return ok; } inline ll strins(std::string &s, const int id, const std::string &t){ s.insert(id,t); return s.size(); } inline std::string toupper(std::string s){ each(c,s) c=std::toupper(c); return s; } inline std::string tolower(std::string s){ each(c,s) c=std::tolower(c); return s; } inline vi ten_to_adic(ll n, const short base) { vi res; while(n) { res.emplace_back(n%base); n/=base; } return res; } inline ll adic_to_ten(const vi &v, const short base) { ll res=0; each(el,v) { res+=Pow(base,eid(el,v))*el; } return res; } inline std::string to_hex(const ll x) { std::stringstream ss; ss << std::hex << x; std::string s = ss.str(); //s=toupper(s); return s; } inline std::string to_oct(const ll x) { std::stringstream s; s << std::oct <(x); std::string s=ss.str(); std::reverse(all(s)); s.resize(ten_to_adic(x,2).size()); std::reverse(all(s)); return s; } inline ll to_ten(const std::string &s, const short base){ return std::stoll(s,nullptr,base); } inline i128 stoL(const std::string &s) { assert(isdigit(s)); bool neg=s.front()=='-'; i128 ret = 0; each(el,s) { if(neg) { neg=0; continue; } ret = 10 * ret + el - '0'; } if(s.front()=='-') ret=-ret; return ret; } template constexpr ul sygcd(const Ts... a) noexcept { std::vector v=std::initializer_list>{a...}; ul g=0; each(el,v) g=std::gcd(g,el); return g; } template constexpr ul sylcm(const Ts... a) noexcept { std::vector v=std::initializer_list>{a...}; ul l=1; each(el,v) l=std::lcm(l,el); return l; } template constexpr auto symin(const Ts... a) noexcept { return std::min(std::initializer_list>{a...}); } template constexpr auto symax(const Ts... a) noexcept { return std::max(std::initializer_list>{a...}); } template inline V kyl(const std::map &m, const U val) { V keys; for(auto it=m.cbegin(); it!=m.cend(); ++it) { if(it->second==val) keys.emplace_back(it->first); } return keys; } template inline K kymin(const std::map &m){ return m.begin()->first; } template inline K kymax(const std::map &m){ return m.rbegin()->first; } template inline V kymin_v(const std::map &m){ return m.begin()->second; } template inline V kymax_v(const std::map &m){ return m.rbegin()->second; } template inline V vlmin(const std::map &m){ const auto pr = *std::min_element(all(m),[](P const &x, P const &y){ return x.second < y.second; }); return pr.second; } template inline V vlmax(const std::map &m){ const auto pr = *std::max_element(all(m),[](P const &x, P const &y){ return x.second < y.second; }); return pr.second; } template inline K vlmin_k(const std::map &m){ const auto pr = *std::min_element(all(m),[](P const &x, P const &y){ return x.second < y.second; }); return pr.first; } template inline K vlmax_k(const std::map &m){ const auto pr = *std::max_element(all(m),[](P const &x, P const &y){ return x.second < y.second; }); return pr.first; } } // zia qu namespace Lady_sANDy { template inline void rev(T& v){ std::reverse(all(v)); } template inline void rev(T& v, const int a, const int b){ std::reverse(all(v,a,b+1)); } template inline T revd(const T& v){ auto c=v; rev(c); return c; } template inline T revd(const T& v, const int a, const int b){ auto c=v; rev(c,a,b); return c; } template inline void Sort(T& v){ std::sort(all(v)); } template inline void Sort(T& v, const int a, const int b){ std::sort(all(v,a,b+1)); } template inline T sorted(const T& v){ auto c=v; Sort(c); return c; } template inline T sorted(const T& v, const int a, const int b){ auto c=v; Sort(c,a,b); return c; } template inline void Sortt(T& v){ std::sort(rall(v)); } template inline void Sortt(T& v, const int a, const int b){ std::sort(rall(v,v.size()-b-1,v.size()-a)); } template inline T Sorted(const T& v){ auto c=v; Sortt(c); return c; } template inline T Sorted(const T& v, const int a, const int b){ auto c=v; Sortt(c,a,b); return c; } template inline void Sorth(T& v){ std::make_heap(all(v)); std::sort_heap(all(v)); } template inline T mrg(const T& a, const T& b){ T res; std::merge(all(a),all(b),std::back_inserter(res)); return res; } template inline T Min(const V& v){ return *std::min_element(all(v)); } template inline T Min(const V& v, const int a, const int b){ return *std::min_element(all(v,a,b+1)); } template inline T Max(const V& v){ return *std::max_element(all(v)); } template inline T Max(const V& v, const int a, const int b){ return *std::max_element(all(v,a,b+1)); } template inline int Min_i(const T& v){ return std::min_element(all(v))-v.begin(); } template inline int Max_i(const T& v){ return std::max_element(all(v))-v.begin(); } template inline int LB(const T& v, const U x){ return std::lower_bound(all(v),x)-v.begin(); } template inline int UB(const T& v, const U x){ return std::upper_bound(all(v),x)-v.begin(); } template inline bool BS(const T& v, const U x){ return std::binary_search(all(v),x); } template inline bool BS(const T& v, const U x, const Boolean &fn){ return std::binary_search(all(v),x,fn); } template inline bool All(const T& v, const Boolean &fn){ return std::all_of(all(v),fn); } template inline bool Exist(const T& v, const Boolean &fn){ return std::any_of(all(v),fn); } template inline int pt(T& v, const Boolean &fn){ auto p=std::partition(all(v),fn); return p-v.begin(); } template inline int ptp(const T& v, const Boolean &fn){ return std::partition_point(all(v),fn)-v.begin(); } template inline int fnd(const T& v, const U x){ auto itr=std::find(all(v),x); return itr!=v.end()?itr-v.begin():-1; } template inline int src(const T& s, const T& t){ auto itr=std::search(all(s),all(t)); return itr!=s.end()?itr-s.begin():-1; } template inline void rpl(T& v, const U fn, const U r){ std::replace(all(v),fn,r); } template inline void rplif(T& v, const Boolean &fn, const U r){ std::replace_if(all(v),fn,r); } template inline ul cntif(const T& v, const Boolean &fn){ return std::count_if(all(v),fn); } template inline T Count(V& v, ll x){ if(!std::is_sorted(all(v))) Sort(v); return UB(v,x)-LB(v,x); } template inline T IP(const V& v, const V& u, T init){ return std::inner_product(all(v),u.begin(),init); } inline vi iot(int n, ll init=0){ vi a(n); std::iota(all(a),init); return a; } template inline V trans(const V& v, const F &fn){ V res; std::transform(all(v),std::back_inserter(res),fn); return res; } template inline int ers(T& v, const U x){ v.erase(std::remove(all(v),x),v.end()); return v.size(); } template int ersif(T& v, const Boolean &fn){ v.erase(std::remove_if(all(v),fn),v.end()); return v.size(); } template inline int unq(T& v){ if(!std::is_sorted(all(v))) Sort(v); v.erase(unique(all(v)),v.end()); return v.size(); } template inline T cp(const T& v){ T res; copy(all(v),back_inserter(res)); return res; } template inline T cp(const T& v, const int a, const int b){ T res; std::copy(all(v,a,b),std::back_inserter(res)); return res; } template inline void rtt(T& s, const int idx){ const int id=zia_qu::Mod(idx,s.size()); std::rotate(all(s,id),s.end()); } template inline void rtt(T& s, const int a, const int b, const int c){ std::rotate(all(s,a,b),s.end()-c); } template inline T setdif(const T& s, const T& t){ assert(std::is_sorted(all(s))&&std::is_sorted(all(t))); T res; std::set_difference(all(s),all(t),std::inserter(res,std::end(res))); return res; } template inline T setsum(const T& s, const T& t){ assert(std::is_sorted(all(s))&&std::is_sorted(all(t))); T res; std::set_union(all(s),all(t),std::inserter(res,std::end(res))); return res; } template inline T setmul(const T& s, const T& t){ assert(std::is_sorted(all(s))&&std::is_sorted(all(t))); T res; std::set_intersection(all(s),all(t),std::inserter(res,std::end(res))); return res; } template inline V adf(const V& v){ V a; std::adjacent_difference(all(v),std::back_inserter(a)); rtt(a,1); a.pop_back(); return a; } template inline V isum(const V &v, const F &fn){ V s{0}; std::inclusive_scan(all(v),std::back_inserter(s),fn); return s; } template inline V rext(const V& v, const int size){ V res; std::sample(all(v),std::back_inserter(res),size,Random()); return res; } template inline T rext(const V& v){ V res; std::sample(all(v),std::back_inserter(res),1,Random()); return res.front(); } template inline ll vsum(const T &v){ return std::accumulate(all(v),0LL); } template inline ll vsum(const T &v, ll a, ll b){ return std::accumulate(all(v,a,b),0LL); } template inline ld vdsum(const T &v){ return std::accumulate(all(v),0.0L); } template inline ld vdsum(const T &v, ll a, ll b){ return std::accumulate(all(v,a,b),0.0L); } template inline ll vmul(const T &v){ return std::accumulate(all(v),1LL,[](ll acc,ll i){ return acc*i; }); } template inline ll vmul(const T &v, ll a, ll b){ return std::accumulate(all(v,a,b),1LL,[](ll acc,ll i){ return acc*i; }); } template inline ld vdmul(const T &v){ return std::accumulate(all(v),1.0L,[](ll acc,ll i){ return acc*i; }); } template inline ld vdmul(const T &v, ll a, ll b){ return std::accumulate(all(v,a,b),1.0L,[](ll acc,ll i){ return acc*i; }); } } // Lady s&y namespace Heileden { template inline V div(const T n) { V d; sqrp(i,1,n) { if(n%i==0) { d.emplace_back(i); if(i*i!=n) d.emplace_back(n/i); } } Lady_sANDy::Sort(d); return d; } template inline V> prmfct(T n) { V> res; sqrp(i,2,n) { if(n%i!=0) continue; T tmp=0; while(n%i==0) { tmp++; n/=i; } res.emplace_back(i,tmp); } if(n!=1) res.emplace_back(n,1); return res; } template inline T euler_phi(T n) { T res = n; for(T i = 2; i * i <= n; ++i) { if(n % i == 0) { res -= res / i; while(n % i == 0) { n /= i; } } } if(n > 1) { res -= res / n; } return res; } struct p_table { vb SoE; p_table(int n): SoE(n+1,1){ SoE[0]=SoE[1]=0; rep(i,2,n) { if(!SoE[i]) continue; rep(j,i*i,n,i) SoE[j] = 0; } } vi get() { vi p; rep(i,2,SoE.size()-1) if(SoE[i]) p.emplace_back(i); return p; } }; struct p_fact { vi spf; p_fact(int n): spf(n + 1){ std::iota(all(spf),0); sqrp(i,2,n) { if(spf[i]==i) { rep(j,i*i,n,i) { if(spf[j]==j) spf[j]=i; } } } } std::map get(int n) { std::map m; while(n!=1) { m[spf[n]]++; n/=spf[n]; } return m; } }; inline ul kthrooti(const ul n, const int k) { if(k==1) { return n; } const auto chk=[=](const uint x) { ul mul=1; rep(k) { if(overflow_if_mul(mul, x)) { return false; } mul*=x; } return mul<=n; }; ul ret=0; rvp(32) { if(chk(ret|(1U<=b) { return 0; } ll sum=(a-2)*(a+1)/2-(b-2)*(b+1)/2; rep(i,a,b-1) { sum += pi(x/primes[i]); } return sum; } ll phi(const ll m, const ll n) { if(m<1) { return 0; } if(n>m) { return 1; } if(n<1) { return m; } if(m<=zia_qu::sqr(primes[n-1])) { return pi(m)-n+1; } if(m<=zia_qu::cub(primes[n-1]) && m <= sq) { const ll sx = pi(kthrooti(m, 2)); ll ans=pi(m)-(sx+n-2)*(sx-n+1)/2; rep(i,n,sx-1) { ans+=pi(m/primes[i]); } return ans; } return phi(m, n-1)-phi(m/primes[n-1], n-1); } public: p_count(const ll lim): sq(kthrooti(lim, 2)), prime_sum(sq + 1) { prime = p_table(sq).SoE; for(int i = 1; i <= sq; ++i) { prime_sum[i] = prime_sum[i - 1] + prime[i]; } primes.reserve(prime_sum[sq]); for(int i = 1; i <= sq; ++i) { if(prime[i]) { primes.emplace_back(i); } } } ll pi(const ll n) { if(n <= sq) { return prime_sum[n]; } const ll m = kthrooti(n, 3); const ll a = pi(m); return phi(n, a) + a - 1 - p2(n, m); } }; struct asum { private: vi s; public: asum(const vi& v): s{0} { std::partial_sum(all(v),back_inserter(s)); } vi get() const { return s; } // [l, r] ll query(int l, int r) const { return s[r]-s[l]; } }; template inline T tetration(const T a, const T b, const T m) { if(m == 1) { return 0; } if(a == 0) { return (b & 1) ? 0 : 1; } if(b == 0) { return 1; } if(b == 1) { return a % m; } if(b == 2) { return zia_qu::Pow(a, a, m); } const auto phi = euler_phi(m); auto tmp = tetration(a, b - 1, phi); if(!tmp) { tmp += phi; } return Pow(a, tmp, m); } struct phi_table { private: int n; std::vector euler; public: phi_table(const int n_): n(n_), euler(n_ + 1) { std::iota(euler.begin(), euler.end(), 0); for(int i = 2; i <= n; ++i) { if(euler[i] == i) { for(int j = i; j <= n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } std::vector get() const { return euler; } std::vector acc() const { std::vector ret(n + 1); ret[1] = 2; for(int i = 2; i <= n; ++i) { ret[i] = ret[i - 1] + euler[i]; } return ret; } }; template inline T bins(T ok, T ng, const Boolean &fn, const ld eps = 1) { while(std::abs(ok-ng)>eps) { T mid=(ok+ng)/2; (fn(mid)?ok:ng) = mid; } return ok; } template inline V press(const V& v) { V res,cp=v; Lady_sANDy::unq(cp); each(el,v) res.emplace_back(Lady_sANDy::LB(cp,el)); return res; } template inline V press(V &c1, V &c2) { V res; const int n = c1.size(); rep(n) { for(T d = 0; d < 1; d++) { T tc1 = c1[i]+d; T tc2 = c2[i]+d; res.emplace_back(tc1); res.emplace_back(tc2); } } Lady_sANDy::unq(res); rep(n) { c1[i]=Lady_sANDy::LB(res,c1[i]); c2[i]=Lady_sANDy::LB(res,c2[i]); } return res; } inline vs rtt(const vs &s) { const int h=s.size(), w=s.front().size(); vs t(w,std::string(h,{})); rep(h) rep(j,w) t[j][i]=s[i][j]; rep(w) Lady_sANDy::rev(t[i]); return t; } template inline V> rtt(const V>& v) { const int h=v.size(), w=v.front().size(); V> res(w,V(h)); rep(h) rep(j,w) res[j][i]=v[i][j]; rep(w) Lady_sANDy::rev(res[i]); return res; } template inline T factor(T n, const T mod=0) { T res=1; while(n>0) { res*=n--; if(mod) res%=mod; } return res; } template inline T perm(T n, const T r, const T mod=0) { const T tmp=n; T res=1; while(n>tmp-r) { res*=n--; if(mod) res%=mod; } return res; } template inline T binom(T n, const T r, const T mod=0) { if(r < 0 || n < r) return 0; T res = 1; rep(i,1,r) { res*=n--; if(mod) res%=mod; res/=i; if(mod) res%=mod; } return res; } inline bool is_prime(const ul n) { if(n==1) return 0; sqrp(i,2,n) if(n%i==0) return 0; return 1; } inline bool is_int(const ld n){ ll r=std::floor(n); return r==n; } inline bool is_sqr(const ll n){ return is_int(std::sqrt(n)); } } // Heileden #line 2 "/home/wa_haya_exe/CP_Library/C++/Matrix.hpp" // inspired by Luzhiled( https://ei1333.github.io/luzhiled/snippets/math/matrix.html ) #line 7 "/home/wa_haya_exe/CP_Library/C++/Matrix.hpp" template struct Matrix { private: const size_t h, w; std::vector> mat; public: Matrix(const size_t n): h(n), w(n), mat(h, std::vector(w)){} Matrix(const size_t h, const size_t w): h(h), w(w), mat(h, std::vector(w)){} Matrix(const std::vector> &m): h(m.size()), w(m[0].size()), mat(h, std::vector(w)) { for(size_t i = 0; i < m.size(); ++i) { for(size_t j = 0; j < m[0].size(); ++j) { mat[i][j] = m[i][j]; } } } static Matrix E(const size_t n) { Matrix m(n); for(size_t i = 0; i < n; ++i) { m[i][i] = 1; } return m; } std::vector> get() const { return mat; } inline const std::vector &operator[](const size_t i) const { return mat[i]; } inline std::vector &operator[](const size_t i){ return mat[i]; } Matrix &operator+=(const Matrix &m) { assert(h == m.h && m == m.w); for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { (*this)[i][j] += m[i][j]; } } return *this; } Matrix &operator-=(const Matrix &m) { assert(h == m.h && m == m.w); for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { (*this)[i][j] -= m[i][j]; } } return *this; } Matrix &operator*=(const Matrix &m) { assert(w == m.h); std::vector> mt(h, std::vector(m.w)); for(size_t i = 0; i < h; ++i) { for(size_t j = 0; j < m.w; ++j) { for(size_t k = 0; k < w; ++k) { mt[i][j] += (*this)[i][k] * m[k][j]; } } } mat.swap(mt); return *this; } Matrix &operator^=(long long k) { Matrix m = Matrix::E(h); while(k > 0) { if(k & 1) { m *= *this; } *this *= *this; k >>= 1LL; } mat.swap(m.mat); return *this; } bool operator==(const Matrix &m) const { if(h != m.h || w != m.w) { return false; } for(size_t i = 0; i < h; ++i) { for(size_t j = 0; j < w; ++j) { if((*this)[i][j] != m[i][j]) { return false; } } } return true; } Matrix operator+(const Matrix &m) const { return Matrix(*this) += m; } Matrix operator-(const Matrix &m) const { return Matrix(*this) -= m; } Matrix operator*(const Matrix &m) const { return Matrix(*this) *= m; } Matrix operator^(const long long k) const { return Matrix(*this) ^= k; } friend std::ostream &operator<<(std::ostream &os, const Matrix &m) { for(size_t i = 0; i < m.h; ++i) { os << '[' << m[i][0]; for(size_t j = 0; ++j < m.w;) { os << ' ' << m[i][j]; } os << ']'; if(i + 1 < m.h) { os << '\n'; } } return os; } }; #line 6 "code.cpp" using namespace std; using namespace zia_qu; using namespace Lady_sANDy; using namespace Heileden; int main() { VvyLw::wa_haya_exe(); now(start); int T=1; //in(T); while(T--) VvyLw::solve(); now(stop); time(start, stop); } // -------------------------------------------------------------------------------------------------------------- void VvyLw::solve() { WEC(ll,m,2,2); Matrix M(m); vout((M^3).get()); }