//#pragma GCC optimize ("-O3") #include using namespace std; //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned) clock() + (unsigned) time(NULL)); }; } __initon; //衝突対策 #define ws ___ws struct T { int f, s, t; T() { f = -1, s = -1, t = -1; } T(int f, int s, int t) : f(f), s(s), t(t) {} bool operator<(const T &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; //return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順 } bool operator>(const T &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; //return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 } bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; } int operator[](int i) { assert(i < 3); return i == 0 ? f : i == 1 ? s : t; } }; #define int long long #define ll long long #define double long double #define pow powl #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo= bool; #define au auto using P = pair; #define fi first #define se second #define vec vector #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == //マクロ省略系 コンテナ //マクロ省略系 コンテナ using vi = vector; using vb = vector; using vs = vector; using vd = vector; using vc = vector; using vp = vector

; using vt = vector; #define vec vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) vec> #define vvt1(t, a) vec>a #define vvt2(t, a, b) vec>a(b) #define vvt3(t, a, b, c) vec> a(b,vec(c)) #define vvt4(t, a, b, c, d) vec> a(b,vec(c,d)) #define vvi(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(ll,__VA_ARGS__) #define vvb(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(bool,__VA_ARGS__) #define vvs(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(string,__VA_ARGS__) #define vvd(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(double,__VA_ARGS__) #define vvc(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(char,__VA_ARGS__) #define vvp(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(P,__VA_ARGS__) #define vvt(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(T,__VA_ARGS__) template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } #define vni(name, ...) auto name = make_v(__VA_ARGS__) #define vnb(name, ...) auto name = make_v(__VA_ARGS__) #define vns(name, ...) auto name = make_v(__VA_ARGS__) #define vnd(name, ...) auto name = make_v(__VA_ARGS__) #define vnc(name, ...) auto name = make_v(__VA_ARGS__) #define vnp(name, ...) auto name = make_v

(__VA_ARGS__) #define v3i(a, b, c, d) vector> a(b, vector(c, vi(d))) #define v3d(a, b, c, d) vector> a(b, vector(c, vd(d))) #define v3m(a, b, c, d) vector> a(b, vector(c, vm(d))) #define _vvi vector #define _vvl vector #define _vvb vector #define _vvs vector #define _vvd vector #define _vvc vector #define _vvp vector #define PQ priority_queue, greater > #define tos to_string using mapi = map; using mapd = map; using mapc = map; using maps = map; using seti = set; using setd = set; using setc = set; using sets = set; using qui = queue; #define bset bitset #define uset unordered_set #define mset multiset #define umap unordered_map #define umapi unordered_map #define umapp unordered_map #define mmap multimap //マクロ 繰り返し #define _overloadrep(_1, _2, _3, _4, name, ...) name # define _rep(i, n) for(int i = 0,_lim=n; i < _lim ; i++) #define repi(i, m, n) for(int i = m,_lim=n; i < _lim ; i++) #define repadd(i, m, n, ad) for(int i = m,_lim=n; i < _lim ; i+= ad) #define rep(...) _overloadrep(__VA_ARGS__,repadd,repi,_rep,)(__VA_ARGS__) #define _rer(i, n) for(int i = n; i >= 0 ; i--) #define reri(i, m, n) for(int i = m,_lim=n; i >= _lim ; i--) #define rerdec(i, m, n, dec) for(int i = m,_lim=n; i >= _lim ; i-=dec) #define rer(...) _overloadrep(__VA_ARGS__,rerdec,reri,_rer,)(__VA_ARGS__) #define fora(a, b) for(auto&& a : b) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const int inf = (int) 1e9 + 100; const ll linf = (ll) 1e18 + 100; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits::min(); ll mi = numeric_limits::max(); const int y4[] = {-1, 1, 0, 0}; const int x4[] = {0, 0, -1, 1}; const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; //マクロ省略形 関数等 #define arsz(a) (sizeof(a)/sizeof(a[0])) #define sz(a) ((int)(a).size()) #define rs resize #define mp make_pair #define pb push_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() inline void sort(string &a) { sort(a.begin(), a.end()); } template inline void sort(vector &a) { sort(a.begin(), a.end()); }; template inline void sort(vector &a, int len) { sort(a.begin(), a.begin() + len); }; template inline void sort(vector &a, F f) { sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); }); }; enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; inline void sort(vector

&a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); break; case fisd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); break; case fdsi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); break; case fdsd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); break; case sifi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); break; case sifd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); break; case sdfi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); break; case sdfd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); break; } }; inline void sort(vector &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); break; case fisd: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); break; case fdsi: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); break; case fdsd: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); break; case sifi: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); break; case sifd: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); break; case sdfi: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); break; case sdfd: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); break; } }; template inline void rsort(vector &a) { sort(a.begin(), a.end(), greater()); }; template inline void rsort(vector &a, int len) { sort(a.begin(), a.begin() + len, greater()); }; template inline void rsort(vector &a, F f) { sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); }); }; template inline void sortp(vector &a, vector &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second;; } }; //F = T //例えばreturn p.fi + p.se; template inline void sortp(vector &a, vector &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template inline void sortp(vector &a, vector &b, char type) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c, type); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template inline void rsortp(vector &a, vector &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template inline void rsortp(vector &a, vector &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template inline void sortt(vector &a, vector &b, vector &c) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); sort(r); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template inline void sortt(vector &a, vector &b, vector &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); sort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template inline void rsortt(vector &a, vector &b, vector &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); rsort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template inline void sort2(vector> &a) { for (int i = 0, n = a.size(); i < n; i++)sort(a[i]); } template inline void rsort2(vector> &a) { for (int i = 0, n = a.size(); i < n; i++)rsort(a[i]); } template void fill(A (&a)[N], const T &v) { rep(i, N)a[i] = v; } template void fill(A (&a)[N][O], const T &v) { rep(i, N)rep(j, O)a[i][j] = v; } template void fill(A (&a)[N][O][P], const T &v) { rep(i, N)rep(j, O)rep(k, P)a[i][j][k] = v; } template void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)a[i][j][k][l] = v; } template void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)rep(m, R)a[i][j][k][l][m] = v; } template void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)rep(m, R)rep(n, S)a[i][j][k][l][m][n] = v; } template void fill(V &xx, const T vall) { xx = vall; } template void fill(vector &vecc, const T vall) { for (auto &&vx: vecc) fill(vx, vall); } //@汎用便利関数 入力 template T _in() { T x; cin >> x; return (x); } #define _overloadin(_1, _2, _3, _4, name, ...) name #define in0() _in() #define in1(a) cin>>a #define in2(a, b) cin>>a>>b #define in3(a, b, c) cin>>a>>b>>c #define in4(a, b, c, d) cin>>a>>b>>c>>d #define in(...) _overloadin(__VA_ARGS__,in4,in3,in2 ,in1,in0)(__VA_ARGS__) #define _overloaddin(_1, _2, _3, _4, name, ...) name #define din1(a) int a;cin>>a #define din2(a, b) int a,b;cin>>a>>b #define din3(a, b, c) int a,b,c;cin>>a>>b>>c #define din4(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d #define din(...) _overloadin(__VA_ARGS__,din4,din3,din2 ,din1)(__VA_ARGS__) #define _overloaddind(_1, _2, _3, _4, name, ...) name #define din1d(a) int a;cin>>a;a-- #define din2d(a, b) int a,b;cin>>a>>b;a--,b-- #define din3d(a, b, c) int a,b,c;cin>>a>>b>>c;a--,b--,c-- #define din4d(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d;;a--,b--,c--,d-- #define dind(...) _overloaddind(__VA_ARGS__,din4d,din3d,din2d ,din1d)(__VA_ARGS__) string sin() { return _in(); } ll lin() { return _in(); } #define na(a, n) a.resize(n); rep(nai,n) cin >> a[nai]; #define nao(a, n) a.resize(n+1); rep(i,n) cin >> a[i+1]; #define nad(a, n) a.resize(n); rep(i,n){ cin >> a[i]; a[i]--;} #define na2(a, b, n) a.resize(n),b.resize(n);rep(i, n)cin >> a[i] >> b[i]; #define na2d(a, b, n) a.resize(n),b.resize(n);rep(i, n){cin >> a[i] >> b[i];a[i]--,b[i]--;} #define na3(a, b, c, n) a.resize(n),b.resize(n),c.resize(n); rep(i, n)cin >> a[i] >> b[i] >> c[i]; #define na3d(a, b, c, n) a.resize(n),b.resize(n),c.resize(n); rep(i, n){cin >> a[i] >> b[i] >> c[i];a[i]--,b[i]--,c[i]--;} #define nt(a, h, w) a.resize(h);rep(i,h)a[i].resize(w); rep(hi,h)rep(wi,w) cin >> a[hi][wi]; #define ntd(a, h, w) rs(a,h,w);rep(hi,h)rep(wi,w) cin >> a[hi][wi], a[hi][wi]--; #define ntp(a, h, w) fill(a,'#');rep(hi,1,h+1)rep(wi,1,w+1) cin >> a[hi][wi]; #ifdef _DEBUG template void err2(T &&head) { cerr << head; } template void err2(T &&head, U &&... tail) { cerr << head << " "; err2(tail...); } template void err(T &&head, U &&... tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } template string out_m2(vector &a, ll W = inf) { stringstream ss; if (W == inf)W = min(sz(a), 12ll); if (sz(a) == 0)return ss.str(); rep(i, W) { ss << a[i] << " "; } return ss.str(); } template string out_m2(vector > &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1)ss << " *|"; else ss << " " << key << "|"; rep(w, W)ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3)ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:off*/ template string out_m2(vector > > &a, ll H = inf, ll W = inf, ll U = inf) {stringstream ss; if (H == inf)H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str();} string out_m2(int a) {stringstream ss;ss << a;return ss.str();} template string out_m2(T &a) {stringstream ss;ss << a;return ss.str();} template string out_m(vector &a, ll W = inf) {stringstream ss; if (W == inf)W = min(sz(a), 12ll); if(sz(a)==0)return ss.str(); rep(i, W) { ss << a[i] << " "; } ss << "" << endl; return ss.str();} template string out_m(vector > &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1)ss << " *|"; else ss << " " << key << "|"; rep(w, W)ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3)ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]),12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } ss << endl; return ss.str(); } /*@formatter:off*/ template string out_m(vector > > &a, ll H = inf, ll W = inf, ll U = inf) {stringstream ss; if (H == inf)H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m(a[i], W, U, i); } ss << endl; return ss.str();} string out_m(int a) {stringstream ss;ss << a << endl;return ss.str();} template string out_m(T &a) {stringstream ss;ss << a << endl;return ss.str();} template void outv(vector &a, ll W=inf) {cout << out_m(a,W) << endl;} template void outv(vector > &a, ll H = linf, ll W = linf,int key=-1) { cout << out_m(a,H,W,key) << endl;} template void outv(vector > > &a, ll H = linf, ll W = linf,ll U = linf) {cout << out_m(a,H,W,U)<< endl;} #else template void outv(vector &a, ll W = inf) { rep(i, min(W, sz(a))) { cout << a[i] << " "; } cout << "" << endl; } template void outv(vector > &a, ll H = linf, ll W = linf, int key = -1) { rep(i, min(H, sz(a))) { outv(a[i], W); }} template void outv(vector > > &a, ll H = linf, ll W = linf, ll U = linf) { ; } #define err(...); #endif #define debugName(VariableName) # VariableName #define deb1(x) debugName(x)<<" = "< void rev(vector &a) { reverse(all(a)); } void rev(string &a) { reverse(all(a)); } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else return (a + b - 1) / b; } ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll) std::sqrt(a); while (res * res < a)res++; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return (1 + t) * t / 2; } ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; } vi divisors(int v) { vi res; double lim = std::sqrt(v); for (int i = 1; i <= lim; ++i) { if (v % i == 0) { res.pb(i); if (i != v / i)res.pb(v / i); } } return res; } vb isPrime; vi primes; void setPrime() { int len = 4010101; isPrime.resize(4010101); fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i <= sqrt(len) + 5; ++i) { if (!isPrime[i])continue; for (int j = 2; i * j < len; ++j) { isPrime[i * j] = false; } } rep(i, len)if (isPrime[i])primes.pb(i); } vi factorization(int v) { int tv = v; vi res; if (isPrime.size() == 0)setPrime(); for (auto &&p :primes) { if (v % p == 0)res.push_back(p); while (v % p == 0) { v /= p; } if (v == 1 || p * p > tv)break; } if (v > 1)res.pb(v); return res; } inline bool inside(int h, int w, int H, int W) { return h >= 0 && w >= 0 && h < H && w < W; } inline bool inside(int v, int l, int r) { return l <= v && v < r; } #define ins inside ll u(ll a) { return a < 0 ? 0 : a; } template vector u(const vector &a) { vector ret = a; fora(v, ret)v = u(v); return ret; } #define MIN(a) numeric_limits::min() #define MAX(a) numeric_limits::max() void yn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a)cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a)cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } template set &operator+=(set &a, U v) { a.insert(v); return a; } template vector &operator+=(vector &a, U v) { a.pb(v); return a; } template T sum(vector &v, int s = 0, int t = inf) { T ret = 0; rep(i, s, min(sz(v), t))ret += v[i]; return ret; } void mod(int &a, int m) { a = (a % m + m) % m; } template inline int mgr(int ok, int ng, F f) { #define _mgrbody int mid = (ok + ng) / 2; if (f(mid))ok = mid; else ng = mid; if (ok < ng)while (ng - ok > 1) { _mgrbody } else while (ok - ng > 1) { _mgrbody } return ok; } template inline int mgr(int ok, int ng, int second, F f) { #define _mgrbody2 int mid = (ok + ng) / 2; if (f(mid, second))ok = mid; else ng = mid; if (ok < ng) while (ng - ok > 1) { _mgrbody2 } else while (ok - ng > 1) { _mgrbody2 } return ok; } template ostream &operator<<(ostream &os, vector &m) { for (auto &&v:m) os << v << " "; return os; } constexpr bool bget(ll m, int keta) { return (m >> keta) & 1; } int bget(ll m, int keta, int sinsuu) { m /= (ll) pow(sinsuu, keta); return m % sinsuu; } ll bit(int n) { return (1LL << (n)); } ll bit(int n, int sinsuu) { return (ll) pow(sinsuu, n); } int mask(int n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll template vector ruiv(vector &a) { vector ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] + a[i]; return ret; } template inline bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template inline bool chma(const U &b) { return chma(ma, b); } template inline bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template inline bool chmi(const U &b) { return chmi(mi, b); } #define unique(v) v.erase( unique(v.begin(), v.end()), v.end() ); int max(vi &a) { int res = a[0]; fora(v, a) { res = max(res, v); } return res; } int min(vi &a) { int res = a[0]; fora(v, a) { res = min(res, v); } return res; } template class fixed_point : T {public: explicit constexpr fixed_point(T &&t) noexcept: T(std::forward(t)) {} template constexpr decltype(auto) operator()(Args &&... args) const { return T::operator()(*this, std::forward(args)...); }};template static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point{std::forward(t)}; } /*@formatter:on*/ int N, K, H, W; vi A, B, C; //@formatter:off //よく使うクラス、構造体 struct unionfind { vector par; vector siz; vector es; ll n, trees;//連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); }} bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes(){ vi cou(n); vi ret; ret.reserve(n); rep(i, n){ cou[root (i)]++; } rep(i, n){ if(cou[i])ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vec sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1)ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r)continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; /*@formatter:off*/ #define forg(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) for (ll gi = 0, f, t, c; gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)if(t!=p) #define fore(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c,ty, id; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, id=ve[gi].id, ty = ve[gi].ty, true); ++gi) //typeが追加される #define forg2(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c,ty; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c,ty=ve[gi].ty, true); ++gi) #define fort2(gi, ve) for (ll gi = 0, f, t, c,ty; gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c,ty=ve[gi].ty, true); ++gi)if(t!=p) template struct edge { int f, t; T c; int id; int ty; edge(int f, int t, T c = 1, int ty = -1,int id = -1) : f(f), t(t), c(c), id(id), ty(ty) {} bool operator<(const edge &b) const { return c < b.c; } bool operator>(const edge &b) const { return c > b.c; }}; template ostream &operator<<(ostream &os, edge &e) { os << e.f << " " << e.t << " " << e.c; return os;} template class graph {protected: vector usedv;public : vector>> g; vector> edges; int n; graph(int n) : n(n) { g.resize(n), usedv.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); usedv.resize(n); } int size() { return g.size(); } vector > &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c, int ty ,int id) = 0; virtual bool used(edge &e) = 0; virtual bool used(int id) = 0; virtual void del(edge &e) = 0; virtual void del(int id) = 0; virtual void set_edges() = 0;}; template class digraph : public graph { public: using graph::g; using graph::n; using graph::edges; using graph::usedv; int eid = 0; digraph(int n) : graph(n) {} void add(int f, int t, T c = 1, int ty = -1,int id = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, c, ty,id); ole(); } if (id == -1)id = eid++; g[f].emplace_back(f, t, c, ty,id); edges.emplace_back(f, t, c, ty,id); } void ing(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t,c); } } void ingct(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c,ty; cin >> f >> t >> c>>ty; f -= minus; t -= minus; ty -= minus; add(f, t,c,ty); } } void ingtc(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c,ty; cin >> f >> t >> ty>>c; f -= minus; t -= minus; ty -= minus; add(f, t,c,ty); } } bool used(edge &e) { return usedv[e.id]; } bool used(int id) { return usedv[id]; } void del(edge &e) { usedv[e.id] = 1; } void del(int id) { usedv[id] = 1; } void set_edges() { if (sz(edges))return; rep(i, n)fora(e, g[i])edges.push_back(e); } }; template class undigraph : public graph { public: using graph::g; using graph::n; using graph::edges; using graph::usedv; int eid = 0; undigraph(int n) : graph(n) {} // f < t void add(int f, int t, T c = 1, int ty = -1, int id = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c, ty, id); ole(); } if (id == -1)id = eid++; g[f].emplace_back(f, t, c, ty, id); g[t].emplace_back(t, f, c, ty, id); edges.emplace_back(f, t, c, ty, id);// edges.emplace_back(t, f, c, ty, id); } void add(edge &e) { int f = e.f, t = e.t, ty = e.ty; T c = e.c; add(f, t, c, ty); } void ing(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) {rep(i, m) { this->resize(n); int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); }} void ingct(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c, ty; cin >> f >> t >> c >> ty; f -= minus; t -= minus; ty -= minus; add(f, t, c, ty); } } void ingtc(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c, ty; cin >> f >> t >> ty >> c; f -= minus; t -= minus; ty -= minus; add(f, t, c, ty); } } bool used(edge &e) { return usedv[e.id]; } bool used(int id) { return usedv[id]; } void del(edge &e) { usedv[e.id] = 1; } void del(int id) { usedv[id] = 1; } void set_edges() { if (sz(edges))return; rep(i, n)fora(e, g[i])edges.push_back(e); } }; template class MinOp { public: T operator()(T a, T b) { return min(a, b); }}; template struct SparseTable { OpFunc op; signed size; vector lg; vector>> table; void init(const vector> &array, OpFunc opfunc) { signed n = array.size(); op = opfunc; lg.assign(n + 1, 0); for (signed i = 1; i <= n; i++) { lg[i] = 31 - __builtin_clz(i); } table.assign(lg[n] + 1, array); for (signed i = 1; i <= lg[n]; i++) { for (signed j = 0; j < n; j++) { if (j + (1 << (i - 1)) < n) { table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } else { table[i][j] = table[i - 1][j]; }}} } pair query(signed l, signed r) { assert(l < r); return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]); } }; struct PMORMQ { vector a; SparseTable > > sparse_table; vector > > lookup_table; vector block_type; signed block_size, n_block; void init(const vector &array) { a = array; signed n = a.size(); block_size = std::max(1, (31 - __builtin_clz(n)) / 2); while (n % block_size != 0) { a.push_back(a.back() + 1); n++; } n_block = n / block_size; vector > b(n_block, make_pair(INT_MAX, INT_MAX)); for (signed i = 0; i < n; i++) { b[i / block_size] = min(b[i / block_size], make_pair(a[i], i)); } sparse_table.init(b, MinOp >()); block_type.assign(n_block, 0); for (signed i = 0; i < n_block; i++) { signed cur = 0; for (signed j = 0; j < block_size - 1; j++) { signed ind = i * block_size + j; if (a[ind] < a[ind + 1]) { cur |= 1 << j; } } block_type[i] = cur; } lookup_table.assign(1 << (block_size - 1), vector >(block_size, vector(block_size + 1))); for (signed i = 0; i < (1 << (block_size - 1)); i++) { for (signed j = 0; j < block_size; j++) { signed res = 0; signed cur = 0; signed pos = j; for (signed k = j + 1; k <= block_size; k++) { lookup_table[i][j][k] = pos; if (i & (1 << (k - 1))) { cur++; } else { cur--; } if (res > cur) { pos = k; res = cur; } } } } } signed query(signed l, signed r) { assert(l < r); signed lb = l / block_size; signed rb = r / block_size; if (lb == rb) { return lb * block_size + lookup_table[block_type[lb]][l % block_size][r % block_size]; } signed pl = lb * block_size + lookup_table[block_type[lb]][l % block_size][block_size]; signed pr = rb * block_size + lookup_table[block_type[rb]][0][r % block_size]; signed pos = pl; if (r % block_size > 0 && a[pl] > a[pr]) { pos = pr; } if (lb + 1 == rb) { return pos; } signed spv = sparse_table.query(lb + 1, rb).second; if (a[pos] > a[spv]) { return spv; } return pos; } }; template class tree : public undigraph { PMORMQ rmq; int cnt; vector id, in; bool never = true; bool never_hld = true; void dfs(int x, int p, int d, int dis = 0) { id[cnt] = x; par[x] = p; dep.push_back(d); disv[x] = dis; in[x] = cnt++; forg(gi, g[x]) { if (t == p) { continue; } dfs(t, x, d + 1, dis + c); id[cnt] = x; dep.push_back(d); cnt++; } } void precalc() { never = false; cnt = 0; dep.clear(); disv.assign(n, 0); in.assign(n, -1); id.assign(2 * n - 1, -1); par.assign(n, -1); dfs(root, -1, 0); rmq.init(dep); #ifdef _DEBUG if(n>=100)return; cerr << "---tree---" << endl; //abcd rep(i, n) { if (!(i == root || sz(g[i]) > 1))continue; cerr << i << " -> "; vi ts; forg(gi, g[i]) { if (t != par[i])ts.push_back(t); } rep(i, sz(ts) - 1)cerr << ts[i] << ", "; if (sz(ts))cerr << ts.back() << endl; } cerr << endl; #endif } int pos; void hld_build() { never_hld = false; if (never)precalc(); pos = 0; vid.assign(n, -1); head.assign(n, 0); sub.assign(n, 1); hvy.assign(n, -1); hdep.assign(n, 0); inv.assign(n, 0); type.assign(n, 0); build(); #ifdef _DEBUG if(n>=100)return; cerr << "---hld_index---" << endl; vi inds; rep(i, n) if (sz(g[i]))inds.push_back(i); rep(i, sz(inds)) { str s = tos(bel(inds[i])); cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i]; } cerr << endl; rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; } cerr << endl << endl; cerr << "---hld_edge_index---" << endl; fora(e, edges) { if (e.f <= e.t) cerr << e.f << "-" << e.t << " " << bel(e) << endl; } cerr << endl << endl; #endif } void build(vector rs = vector(1, 0)) { int c = 0; for (int r:rs) { dfs(r); bfs(r, c++); } } void dfs(int rt) { stack

st; hdep[rt] = 0; st.emplace(rt, 0); while (!st.empty()) { int v = st.top().first; int &i = st.top().second; if (i < (int) g[v].size()) { int u = g[v][i++].t; if (u == par[v]) continue; hdep[u] = hdep[v] + 1; st.emplace(u, 0); } else { st.pop(); int res = 0; forg(gi, g[v]) { int u = t; if (u == par[v]) continue; sub[v] += sub[u]; if (res < sub[u]) res = sub[u], hvy[v] = u; } } } } void bfs(int r, int c) { int &k = pos; queue q({r}); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = hvy[i]) { type[i] = c; vid[i] = k++; inv[vid[i]] = i; head[i] = h; forg(gi, g[i])if (t != par[i] && t != hvy[i]) q.push(t); } } } vi vid; public: using undigraph::g; using undigraph::n; using undigraph::edges; using undigraph::usedv; vector disv; vector dep, par; vector head, sub, hvy, inv, type, hdep/*おそらくグループ内のdep*/;/*vid := bel()*/ int root; tree(int n_, int root = 0) : undigraph(n_), root(root) { n = n_; } int lca(int a, int b) { if (never)precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); return id[pos]; } /*@formatter:on*/ int dis(int a, int b) { if (never)precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); int p = id[pos]; return disv[a] + disv[b] - disv[p] * 2; } /*@formatter:off*/ /*O(N) hldを使わず木を普通にたどる*/ void for_each_l(int u, int v, function fnode) { int r = lca(u, v); while (u != r) { fnode(u); u = par[u]; } while (v != r) { fnode(v); v = par[v]; } fnode(r); } void for_each_edge_l/*O(N) 頂点に対しての処理順が可換*/(int u, int v, function &)> fedge) { int r = lca(u, v); auto sub = [&](int u, int r) { while (u != r) { forg(gi, g[u]) { if (t == par[u]) { fedge(g[u][gi]); u = par[u]; break; } } } }; sub(u, r); sub(v, r); } /*Fは半開 (u,v)は木の頂点* / /*中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう*/ void for_each_(int u, int v, const function &f) { if (never_hld)hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); int l = max(vid[head[v]], vid[u]); int r = vid[v] + 1; f(l, r); if (head[u] != head[v]) v = par[head[v]]; else break; } } void for_each_edge/*[l,r) O(log(N)) 辺を頂点として扱っている 上と同じ感じで使える*/(int u, int v, const function &f) { if (never_hld)hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); if (head[u] != head[v]) { int l = vid[head[v]]; int r = vid[v] + 1; f(l, r); v = par[head[v]]; } else { if (u != v) { int l = vid[u] + 1; int r = vid[v] + 1; f(l, r); } break; } } } int bel(int v) { /*hld内での頂点番号を返す*/ if (never_hld)hld_build(); return vid[v]; } int bel(int f, int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld)hld_build(); return hdep[f] > hdep[t] ? vid[f] : vid[t]; } int bel(edge &e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld)hld_build(); return hdep[e.f] > hdep[e.t] ? vid[e.f] : vid[e.t]; } template int operator()(U ... args) { return bel(args...); } };; //辺によりメモリを大量消費ためedgesを消している //頂点10^6でメモリを190MB(制限の8割)使う //@formatter:off //constexpr int MOD = 1000000007; struct mint { int x; mint() : x(0) {} mint(int a) { x = a % MOD; if (x < 0) x += MOD; } mint &operator+=(mint that) { x = (x + that.x) % MOD; return *this; } mint &operator-=(mint that) { x = (x + MOD - that.x) % MOD; return *this; } mint &operator*=(mint that) { x = (int) x * that.x % MOD; return *this; } mint &operator/=(mint that) { return *this *= that.inverse(); } mint operator-() { return mint(-this->x); } friend ostream &operator<<(ostream &out, mint m) { return out << m.x; } mint inverse() { int a = x, b = MOD, u = 1, v = 0; while (b) { int t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } return mint(u); } operator int() const { return x; } template mint &operator+=(T that) { return operator+=((mint) that); } template mint &operator-=(T that) { return operator-=((mint) that); } template mint &operator*=(T that) { return operator*=((mint) that); } template mint &operator/=(T that) { return operator/=(that); } mint operator+(mint that) { return mint(*this) += that; } mint operator-(mint that) { return mint(*this) -= that; } mint operator*(mint that) { return mint(*this) *= that; } mint operator/(mint that) { return mint(*this) /= that; } template mint operator+(T that) { return mint(*this) += (mint) that; } template mint operator-(T that) { return mint(*this) -= (mint) that; } template mint operator*(T that) { return mint(*this) *= (mint) that; } template mint operator/(T that) { return mint(*this) /= (mint) that; } bool operator==(mint that) const { return x == that.x; } bool operator==(signed that) const { return x == that; } bool operator!=(mint that) const { return x != that.x; } bool operator<(mint that) const { return x < that.x; } bool operator<=(mint that) const { return x <= that.x; } bool operator>(mint that) const { return x > that.x; } bool operator>=(mint that) const { return x >= that.x; }}; istream &operator>>(istream &i, mint &a) { i >> a.x; return i;} typedef vector vm; vector fac, finv; int mint_len = 1400000; //既に変更されていたら変えない void setmod(int mod = 1e9 + 7) { /*mint_len>=modの時fac[mint_len]が0になり、下の1/fac[mint_len]で0徐算になってしまう*/ if (mint_len >= mod - 1)mint_len = mod - 1; MOD = mod; fac = vector(mint_len + 1); finv = vector(mint_len + 1); fac[0] = 1; rep(i, 1, mint_len + 1) { fac[i] = fac[i - 1] * i; } finv[mint_len] = (mint) 1 / fac[mint_len]; rer(i, mint_len, 1) { finv[i - 1] = finv[i] * i; }} mint com(int a, int b) { if (a < 0) return 0; if (b < 0 || b > a) return 0; return fac[a] * finv[a - b] * finv[b];} mint hom(int a, int b) { return com(a + b - 1, b);} template mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res;}template inline mint mpow(const T a, const mint b) { int x = a, res = 1; int p = b.x; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res;} using PM = pair; using vm = vector; #define vvm(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(mint,__VA_ARGS__) #define vnm(name, ...) auto name = make_v(__VA_ARGS__) string yuri(const mint &a) { stringstream st; rep(i, 300) {rep(j, 300) {if ((mint) i / j == a) {st << i << " / " << j;return st.str();}}} rep(i, 1000) {rep(j, 1000) {if ((mint) i / j == a) {st << i << " / " << j;return st.str();}}} return st.str();} #define smod setmod //setmodを呼ぶ @formatter:on ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } signed main() { din(D, L, R, K); setmod(); //1 index tree<> g(bit(D), 1); rep(i, 1, bit(D) - bit(D - 1)) { g.add(i, i * 2); g.add(i, i * 2 + 1); } int dl = log2(hbit(L)); int dr = log2(hbit(R)); //d(L) != d(R) if (dl != dr) { //l kotei int rst = hbit(R); mint cou = 0; rep(r, rst, rst * 2) { if (g.dis(L, r) == K) { cou+=1; } } mint res = 1; rep(d, D) { if (d != dr) { res *= fac[bit(d)]; } else { res *= fac[bit(d) - 1]; } } res *= cou; cout << res << endl; } else { //l kotei int rst = hbit(R); mint cou = 0; rep(r, rst, rst * 2) { if (g.dis(L, r) == K) { cou+=1; } } mint res = 1; rep(d, D) { if (d != dr) { res *= fac[bit(d)]; } else { res *= fac[bit(d) - 2] * bit(d); res *= cou; } } cout << res << endl; } }