結果
| 問題 | No.3753 Certainly a Cretan |
| コンテスト | |
| ユーザー |
👑 hamamu
|
| 提出日時 | 2026-10-02 23:49:19 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
WA
不安定
|
| 実行時間 | - |
| コード長 | 44,940 bytes |
| 記録 | |
| コンパイル時間 | 3,303 ms |
| コンパイル使用メモリ | 387,828 KB |
| 実行使用メモリ | 61,536 KB |
| 最終ジャッジ日時 | 2026-10-02 23:49:45 |
| 合計ジャッジ時間 | 7,861 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 5 WA * 41 |
ソースコード
#ifndef MYLOCAL
//# pragma GCC target("avx2")//yukiではNG
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")
#endif
#if defined(NDEBUG)
#undef NDEBUG
#endif
#include "bits/stdc++.h"
using namespace std;
using ll=long long;
using dd=long double;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using qll=tuple<ll,ll,ll,ll>;
using namespace chrono;
constexpr ll INF = 1201001001001001001;
struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(numeric_limits<double>::max_digits10); } } fast;
#define EXPAND( x ) x//VS用おまじない
#define overload3(_1,_2,_3,name,...) name
#define overload4(_1,_2,_3,_4,name,...) name
#define overload5(_1,_2,_3,_4,_5,name,...) name
#define rep1(N) for (ll dmyi = 0; dmyi < (N); dmyi++)
#define rep2(i, N) for (ll i = 0; i < (N); i++)
#define rep3(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep4(i, S, E, t) for (ll i = (S); i <= (E); i+=(t))
#define rep(...) EXPAND(overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__))
#define dep3(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep4(i, E, S, t) for (ll i = (E); i >= (S); i-=(t))
#define dep(...) EXPAND(overload4(__VA_ARGS__, dep4, dep3,_,_)(__VA_ARGS__))
#define ALL1(v) (v).begin(), (v).end()
#define ALL2(v,E) (v).begin(), (v).begin()+((E)+1)
#define ALL3(v,S,E) (v).begin()+(S), (v).begin()+((E)+1)
#define all(...) EXPAND(overload3(__VA_ARGS__, ALL3, ALL2, ALL1)(__VA_ARGS__))
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }
template<class T> [[nodiscard]] inline T limithi(T a,T b){ return min(a,b); }
template<class T> [[nodiscard]] inline T limitlo(T a,T b){ return max(a,b); }
template<class T> inline bool chlimithi(T &a,T b){ return chmin(a,b); }
template<class T> inline bool chlimitlo(T &a,T b){ return chmax(a,b); }
template<class T> inline auto maxe(T &&v,ll S,ll E){ return *max_element(all(v,S,E)); }
template<class T> inline auto maxe(T &&v){ return *max_element(all(v)); }
template<class T> inline auto mine(T &&v,ll S,ll E){ return *min_element(all(v,S,E)); }
template<class T> inline auto mine(T &&v){ return *min_element(all(v)); }
template<class T,class U=typename remove_reference<T>::type::value_type>
inline U sum(T &&v,ll S,ll E) {return accumulate(all(v,S,E),U());}
template<class T> inline auto sum(T &&v) {return sum(v,0,v.end()-v.begin()-1);}
template<class T> inline ll sz(T &&v){ return (ll)v.size(); }
//cin
struct cinutil{
template<class T> static void cin1core(T &a){ cin>>a; }
template<class T,class S> static void cin1core(pair<T,S> &a){
cin1core(a.first),cin1core(a.second);
}
template<class... Args> static void cin1core(tuple<Args...> &a){
cinTplRec<tuple<Args...>,sizeof...(Args)-1>()(a);
}
template<class T,size_t N>
static void cin1core(array<T,N> &a){ for (int i=0; i<(int)N; ++i) cin>>a[i]; }
private:
template<class Tpl,int i> struct cinTplRec{
void operator()(Tpl &a){ cinTplRec<Tpl,i-1>()(a); cin1core(get<i>(a)); }
};
template<class Tpl> struct cinTplRec<Tpl,0>{
void operator()(Tpl &a){ cin1core(get<0>(a)); }
};
};
template<class T> T cin1(){ T a; cinutil::cin1core(a); return a; }
template<class... Args> tuple<Args...> cins(){ return cin1<tuple<Args...>>(); }
//cout
template<class T,class S> inline ostream &operator<<(ostream &os,const pair<T,S> &a){ return os << a.first << ' ' << a.second; }
template<class T,class S,class R> inline ostream &operator<<(ostream &os,const tuple<T,S,R> &a){ return os << get<0>(a) << ' ' << get<1>(a) << ' ' << get<2>(a); }
template<class T,class S,class R,class Q> inline ostream &operator<<(ostream &os,const tuple<T,S,R,Q> &a){ return os << get<0>(a) << ' ' << get<1>(a) << ' ' << get<2>(a) << ' ' << get<3>(a); }
template<class T> inline ostream &operator<<(ostream &os,const vector<T> &a){ for (ll i=0; i<(ll)a.size(); i++) os<<(i>0?" ":"")<<a[i]; return os; }
inline struct{
system_clock::time_point st = system_clock::now();
ll operator()()const{return duration_cast<microseconds>(system_clock::now()-st).count()/1000;}
} timeget;
template<long long MOD> struct mll_{
using Int = long long;
using ll = long long;
ll val_=0;
/*---- utility ----*/
mll_ &norm(){ return normR().normS(); }//正規化
mll_ &normR(){ val_%=MOD; return *this; }//剰余正規化のみ
mll_ &normS(){ if (val_<0) val_+=MOD; return *this; }//正負正規化のみ
mll_ &normP(){ if (val_>=MOD) val_-=MOD; return *this; }//加算時正規化
mll_ &invsg(){ val_=-val_; return normS(); }//正負反転
ll modinv(int a){//a^-1 mod MOD
int ypre=0,y=1,apre=MOD;
while (a>1){
int t=apre/a;
apre-=a*t,swap(a,apre);
ypre-=y*t,swap(y,ypre);
}
return y<0 ? y+MOD : y;
}
/*---- I/F ----*/
mll_(){}
mll_(ll v): val_(v){ norm(); }
mll_(ll v,bool b): val_(v){} //正規化無のコンストラクタ
Int val()const{ return (Int)val_; }
bool isnone() const { return val_==-1; } //true:値なし
mll_ &none() { val_=-1; return *this; } //値なしにする
mll_ &inv(){ val_=modinv((int)val_); return *this; }
mll_ &operator+=(mll_ b){ val_+=b.val_; return normP(); }
mll_ &operator-=(mll_ b){ val_-=b.val_; return normS(); }
mll_ &operator*=(mll_ b){ val_*=b.val_; return normR(); }
mll_ &operator/=(mll_ b){ return *this*=b.inv(); }
mll_ &operator+=(ll b){ return *this+=mll_(b); }
mll_ &operator-=(ll b){ return *this-=mll_(b); }
mll_ &operator*=(ll b){ return *this*=mll_(b); }
mll_ &operator/=(ll b){ return *this/=mll_(b); }
mll_ operator-()const{ return mll_(*this).invsg(); }
mll_ operator+(mll_ b)const{ return mll_(*this)+=b; }
mll_ operator-(mll_ b)const{ return mll_(*this)-=b; }
mll_ operator*(mll_ b)const{ return mll_(*this)*=b; }
mll_ operator/(mll_ b)const{ return mll_(*this)/=b; }
mll_ operator+(ll b)const{ return mll_(*this)+=b; }
mll_ operator-(ll b)const{ return mll_(*this)-=b; }
mll_ operator*(ll b)const{ return mll_(*this)*=b; }
mll_ operator/(ll b)const{ return mll_(*this)/=b; }
friend mll_ operator+(ll a,mll_ b){ return b+a; }
friend mll_ operator-(ll a,mll_ b){ return -b+a; }
friend mll_ operator*(ll a,mll_ b){ return b*a; }
friend mll_ operator/(ll a,mll_ b){ return mll_(a)/b; }
bool operator==(mll_ b)const{ return val_==b.val_; }
bool operator!=(mll_ b)const{ return val_!=b.val_; }
bool operator==(ll b)const{ return *this==mll_(b); }
bool operator!=(ll b)const{ return *this!=mll_(b); }
friend bool operator==(ll a,mll_ b){ return mll_(a)==b; }
friend bool operator!=(ll a,mll_ b){ return mll_(a)!=b; }
friend ostream &operator<<(ostream &os,mll_ a){ return os << a.val_; }
friend istream &operator>>(istream &is,mll_ &a){ return is >> a.val_; }
mll_ pow(ll k)const{
mll_ ret(1,false),a(*this);
for (; k>0; k>>=1,a*=a) if (k&1)ret*=a;
return ret;
}
static constexpr int mod() { return MOD; }
//enum{ modll=MOD };
};
template<class T> struct Vector: vector<T>{
using Int = long long;
using vT=vector<T>;
using cvT=const vector<T>;
using cT=const T;
using vT::vT; //親クラスのコンストラクタの隠蔽を回避
using vT::begin,vT::end,vT::insert,vT::erase;
auto it(Int i){ return begin()+i; }
auto it(Int i)const{ return begin()+i; }
Vector(cvT& b):vT(b){}
Vector(vT&& b):vT(move(b)){}
Vector(int n,cT& x):vT(n,x){}// ┬ 型推論のためラッパー
Vector(long long n,cT& x):vT(n,x){}
template<class S> Vector(const Vector<S>& b):vT(b.begin(),b.end()){}
template<class S> Vector(const vector<S>& b):vT(b.begin(),b.end()){}
Vector(Int n,T s,T d){ iota(n,s,d); }
Vector(Int n,function<T(Int)> g):vT(n){ for(Int i=0;i<n;++i) (*this)[i]=g(i); }
Vector &operator+=(cvT &b){ assert(size()==b.size()); for(Int i=0;i<size();++i) (*this)[i]+=b[i]; return *this; }
Vector &operator-=(cvT &b){ assert(size()==b.size()); for(Int i=0;i<size();++i) (*this)[i]-=b[i]; return *this; }
Vector &operator*=(cvT &b){ assert(size()==b.size()); for(Int i=0;i<size();++i) (*this)[i]*=b[i]; return *this; }
Vector &operator/=(cvT &b){ assert(size()==b.size()); for(Int i=0;i<size();++i) (*this)[i]/=b[i]; return *this; }
Vector &operator%=(cvT &b){ assert(size()==b.size()); for(Int i=0;i<size();++i) (*this)[i]%=b[i]; return *this; }
Vector &operator+=(const Vector<T> &b){ return *this+=(cvT&)b; }
Vector &operator-=(const Vector<T> &b){ return *this-=(cvT&)b; }
Vector &operator*=(const Vector<T> &b){ return *this*=(cvT&)b; }
Vector &operator/=(const Vector<T> &b){ return *this/=(cvT&)b; }
Vector &operator%=(const Vector<T> &b){ return *this%=(cvT&)b; }
Vector operator+(cvT &b){ return Vector(*this)+=b; }
Vector operator-(cvT &b){ return Vector(*this)-=b; }
Vector operator*(cvT &b){ return Vector(*this)*=b; }
Vector operator/(cvT &b){ return Vector(*this)/=b; }
Vector operator%(cvT &b){ return Vector(*this)%=b; }
Vector operator+(const Vector<T> &b){ return Vector(*this)+=b; }
Vector operator-(const Vector<T> &b){ return Vector(*this)-=b; }
Vector operator*(const Vector<T> &b){ return Vector(*this)*=b; }
Vector operator/(const Vector<T> &b){ return Vector(*this)/=b; }
Vector operator%(const Vector<T> &b){ return Vector(*this)%=b; }
template<class S> Vector &operator+=(S x){ for(T &e: *this) e+=x; return *this; }
template<class S> Vector &operator-=(S x){ for(T &e: *this) e-=x; return *this; }
template<class S> Vector &operator*=(S x){ for(T &e: *this) e*=x; return *this; }
template<class S> Vector &operator/=(S x){ for(T &e: *this) e/=x; return *this; }
template<class S> Vector &operator%=(S x){ for(T &e: *this) e%=x; return *this; }
template<class S> Vector operator+(S x)const{ return Vector(*this)+=x; }
template<class S> Vector operator-(S x)const{ return Vector(*this)-=x; }
template<class S> Vector operator*(S x)const{ return Vector(*this)*=x; }
template<class S> Vector operator/(S x)const{ return Vector(*this)/=x; }
template<class S> Vector operator%(S x)const{ return Vector(*this)%=x; }
Vector &operator--(int){ return *this-=1; }
Vector &operator++(int){ return *this+=1; }
Vector operator-()const{ return Vector(*this)*=-1; }
template<class S> friend Vector operator-(S x,const Vector &a){ return -a+=x; }
T& at(Int i){ assert(i>=0); if(n()<=i)vT::resize(i+1); return vT::operator[](i); }
Vector slice(Int l,Int r,Int d=1)const{
Vector ret;
for(Int i=l;(d>0&&i<=r)||(d<0&&r<=i);i+=d) ret.push_back((*this)[i]);
return ret;
}
Int size()const{ return (Int)vT::size(); }
Int n()const{ return size(); }
Vector &push_back(cT& x,Int n=1){ for(Int i=0;i<n;++i){ vT::push_back(x); } return *this; }
Vector &pop_back(Int n=1){ for(Int i=0;i<n;++i){ vT::pop_back(); } return *this; }
Vector &push_front(cT& x,Int n=1){ this->insert(0,x,n); return *this; }
Vector &pop_front(Int n=1){ erase(0,n-1); return *this; }
T pull_back(){ T x=move(vT::back()); vT::pop_back(); return x; }
T pull_front(){ T x=move(vT::front()); erase(0); return x; }
Vector &insert(Int i,cT& x,Int n=1){ insert(it(i),n,x); return *this; }
Vector &insert(Int i,cvT& b){ insert(it(i),b.begin(),b.end()); return *this; }
Vector &erase(Int i){ erase(it(i)); return *this; }
Vector &erase(Int l,Int r){ erase(it(l),it(r+1)); return *this; }
Vector &erase(const Vector<Int> &idxs){
for (Int I=0; I<idxs.n(); ++I){
Int l=idxs[I]+1, r = (I<idxs.n()-1) ? idxs[I+1] : this->n();
copy(it(l),it(r),it(l-I-1));//[l,r)を前にI+1個ずらす
}
vT::resize(this->n()-idxs.n());
return *this;
}
Vector &eraseall(cT& x){ return eraseall(0,size()-1,x); }
Vector &eraseall(Int l,Int r,cT& x){ erase(remove(it(l),it(r+1),x),it(r+1)); return *this; }
template<class Pr> Vector &eraseif(Pr pr){ return eraseif(0,size()-1,pr); }
template<class Pr> Vector &eraseif(Int l,Int r,Pr pr){ erase(remove_if(it(l),it(r+1),pr),it(r+1)); return *this; }
Vector &concat(cvT &b,Int n=1){
cvT B = (&b==this) ? *this : vT{};
for(int i=0;i<n;++i) this->insert(size(),(&b==this)?B:b);
return *this;
}
Vector repeat(Int n){ return Vector{}.concat(*this,n); }
Vector &reverse(Int l=0,Int r=-1){ r+=r<0?size():0; std::reverse(it(l),it(r+1)); return *this; }
Vector &rotate(Int m){ return rotate(0,size()-1,m); }
Vector &rotate(Int l,Int r,Int m){ std::rotate(it(l),it(m),it(r+1)); return *this; }
Vector &sort(Int l=0,Int r=-1){ r+=r<0?size():0; std::sort(it(l),it(r+1)); return *this; }
Vector &rsort(Int l=0,Int r=-1){ return sort(l,r).reverse(l,r); }
template<class Pr> Vector &sort(Pr pr){ return sort(0,size()-1,pr); }
template<class Pr> Vector &sort(Int l,Int r,Pr pr){ std::sort(it(l),it(r+1),pr); return *this; }
template<int key> Vector &sortbykey(Int l=0,Int r=-1){
r+=r<0?size():0;
sort(l,r,[](cT &x,cT &y){return get<key>(x)<get<key>(y);});
return *this;
}
Vector &uniq(){ erase(unique(begin(),end()),end()); return *this; }
Vector &sortq(){ return sort().uniq(); }
Vector &fill(cT& x){ return fill(0,size()-1,x); }
Vector &fill(Int l,Int r,cT& x){ std::fill(it(l),it(r+1),x); return *this; }
Vector ©(Int i,cvT &b,Int n=1){//A[i]スタートでbをn回分コピー
for (int t=0; t<n; ++t) for (int j=0; j<(int)b.size(); ++j){
if (i>=size()) return *this;
if (i>=0) (*this)[i]=b[j];
i++;
}
return *this;
}
template<class S=Int> Vector &iota(Int n,T s=0,S d=1){
vT::resize(n);
if(n==0) return *this;
(*this)[0]=s;
for(int i=1;i<n;++i) (*this)[i]=(*this)[i-1]+d;
return *this;
}
Int count(cT& x)const{ return count(0,size()-1,x); }
Int count(Int l,Int r,cT& x)const{ return Int(std::count(it(l),it(r+1),x)); }
template<class Pr> Int countif(Pr pr)const{ return countif(0,size()-1,pr); }
template<class Pr> Int countif(Int l,Int r,Pr pr)const{ return Int(count_if(it(l),it(r+1),pr)); }
Int find(cT& x)const{ return find(0,size()-1,x); }
Int find(Int l,Int r,cT& x)const{ return Int(std::find(it(l),it(r+1),x)-begin()); }
Int rfind(cT& x)const{ return rfind(0,size()-1,x); }
Int rfind(Int l,Int r,cT& x)const{
for (int i=r;i>=l;--i) if ((*this)[i]==x) return i;
return l-1;
}
template<class Pr> Int findif(Pr pr)const{ return findif(0,size()-1,pr); }
template<class Pr> Int findif(Int l,Int r,Pr pr)const{ return Int(find_if(it(l),it(r+1),pr)-begin()); }
Vector<Int> findall(cT& x)const{ return findall(0,size()-1,x); }
Vector<Int> findall(Int l,Int r,cT& x)const{ return findallif(l,r,[&](cT& y){return y==x;}); }
template<class Pr> Vector<Int> findallif(Pr pr)const{ return findallif(0,size()-1,pr); }
template<class Pr> Vector<Int> findallif(Int l,Int r,Pr pr)const{
Vector<Int> ret;
for(Int i=l;i<=r;++i) if(pr((*this)[i])) ret.push_back(i);
return ret;
}
Int flooridx(cT& x)const{ return Int(upper_bound(begin(),end(),x)-begin()-1); }
Int ceilidx(cT& x)const{ return Int(lower_bound(begin(),end(),x)-begin()); }
Int leftnmof(cT& x)const{ return flooridx(x)+1; }
Int rightnmof(cT& x)const{ return size()-ceilidx(x); }
bool contains(cT& x)const{ Int i=flooridx(x); return i>=0 && (*this)[i]==x; }
template<class Pr> Int flooridx(cT& x,Pr pr)const{ return Int(upper_bound(begin(),end(),x,pr)-begin()-1); }
template<class Pr> Int ceilidx(cT& x,Pr pr)const{ return Int(lower_bound(begin(),end(),x,pr)-begin()); }
template<class Pr> Int leftnmof(cT& x,Pr pr)const{ return flooridx(x,pr)+1; }
template<class Pr> Int rightnmof(cT& x,Pr pr)const{ return size()-ceilidx(x,pr); }
template<class Pr> bool contains(cT& x,Pr pr)const{ Int i=flooridx(x,pr); return i>=0 && (*this)[i]==x; }
template<class S> using VV = Vector<Vector<S>>; template<class S> using sVV = vector<vector<S>>;
template<class S> using VVV = Vector<VV<S>>; template<class S> using sVVV = vector<sVV<S>>;
template<class S> using VVVV = Vector<VVV<S>>; template<class S> using sVVVV = vector<sVVV<S>>;
template<class S> using VVVVV = Vector<VVVV<S>>; template<class S> using sVVVVV = vector<sVVVV<S>>;
auto tostd()const{ return tov(*this); }
template <class S> static vector<S> tov(const Vector<S>&v){ return v; }
template <class S> static sVV<S> tov(const VV<S> &v){ sVV<S> ret; for(auto&& e:v) ret.push_back(e); return ret; }
template <class S> static sVVV<S> tov(const VVV<S> &v){ sVVV<S> ret; for(auto&& e:v) ret.push_back(e.tostd()); return ret; }
template <class S> static sVVVV<S> tov(const VVVV<S> &v){ sVVVV<S> ret; for(auto&& e:v) ret.push_back(e.tostd()); return ret; }
template <class S> static sVVVVV<S> tov(const VVVVV<S> &v){ sVVVVV<S> ret; for(auto&& e:v) ret.push_back(e.tostd()); return ret; }
};
#if 0
#define MODLL (1000000007LL)
#else
#define MODLL (998244353LL)
#endif
using mll = mll_<MODLL>;
//using mll = fraction;
namespace SolvingSpace{
template<class T> using vector = Vector<T>;
using vll=vector< ll>; using vmll=vector< mll>; using vdd=vector< dd>;
using vvll=vector< vll>; using vvmll=vector< vmll>; using vvdd=vector< vdd>;
using vvvll=vector< vvll>; using vvvmll=vector< vvmll>; using vvvdd=vector< vvdd>;
using vvvvll=vector<vvvll>; using vvvvmll=vector<vvvmll>; using vvvvdd=vector<vvvdd>;
using vpll=vector< pll>; using vtll=vector< tll>; using vqll=vector< qll>;
using vvpll=vector< vpll>; using vvtll=vector< vtll>; using vvqll=vector< vqll>;
using vss=vector<string>;
template<class T> vector<T> cinv(ll nm){ return vector<T>(nm,[](ll i){ (void)i; return cin1<T>(); }); }
template<class T> vector<vector<T>> cinvv(ll H,ll W){ return vector<vector<T>>(H,[&](ll i){ (void)i; return cinv<T>(W); }); }
/*■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■*/
template<class T> struct combination{
vector<T> f,g; ll mxN=0;
combination(){}
combination(ll maxN): f(maxN+1,1),g(maxN+1),mxN(maxN) {
for (ll i=1; i<=mxN; ++i) { f[i]=f[i-1]*i; }
g[mxN]=1/f[mxN];
for (ll i=mxN; i>=1; --i) { g[i-1]=g[i]*i; }
}
T P(ll n,ll r){ return (n<0 || r<0 || n<r) ? T(0) : f[n]*g[n-r]; } //nPr
T H(ll n,ll r){ return operator()(n+r-1,n-1); }//nHr
T inv(ll n) { return f[n-1] * g[n]; } //1/n
T fact(ll n) { return f[n]; } //n!
T finv(ll n) { return g[n]; } //1/n!
T operator()(ll n,ll r){
if (r<0) return 0;
if (n<0) return operator()(-n+r-1,r) * ((r&1) ? -1 : 1); //-nCr = (-1)^r * n+r-1Cr
if (n<r) return 0;
if (n<=mxN) return f[n]*g[n-r]*g[r]; //通常
//n巨大、rかn-r小
if (n-r<r) r=n-r;
T bunsi=1,bunbo=1;
for (ll i=0; i<r; ++i) bunsi*=n-i;
for (ll i=0; i<r; ++i) bunbo*=i+1;
return bunsi/bunbo;
}
template<class SP>
vector<T> CnLnR(long long nL,long long nR,long long r,SP sp){
if (nR-nL+1<=0) return vector<T>();
if (r<0) return vector<T>(nR-nL+1,0);
vector<T> v=sp(nL-r+1,nR-r+1,r);
for (T& e: v) e*=finv(r);
return v;
}
template<class SP>
vector<T> HrLrR(long long n,long long rL,long long rR,SP sp){//r<0不可
return CnLnR(n-1+rL,n-1+rR,n-1,sp);
}
};
namespace atcoder {
//======== from internal_bit.hpp
namespace internal {
using std::bit_ceil;
inline int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index,n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
//======== from segtree.hpp
#if __cplusplus >= 201703L
template <class S,auto op,auto e> struct segtree {
static_assert(std::is_convertible_v<decltype(op),std::function<S(S,S)>>,
"op must work as S(S, S)");
static_assert(std::is_convertible_v<decltype(e),std::function<S()>>,
"e must work as S()");
#else
template <class S,S (*op)(S,S),S (*e)()> struct segtree {
#endif
public:
segtree(): segtree(0) {}
explicit segtree(int n): segtree(std::vector<S>(n,e())) {}
explicit segtree(const std::vector<S>& v): _n(int(v.size())) {
size = (int)internal::bit_ceil((unsigned int)(_n));
log = internal::countr_zero((unsigned int)size);
d = std::vector<S>(2 * size,e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p,S x) {
assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) const {
assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l,int r) const {
assert(0 <= l && l <= r && r <= _n);
S sml = e(),smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml,d[l++]);
if (r & 1) smr = op(d[--r],smr);
l >>= 1;
r >>= 1;
}
return op(sml,smr);
}
S all_prod() const { return d[1]; }
template <bool (*f)(S)> int max_right(int l) const {
return max_right(l,[](S x) { return f(x); });
}
template <class F> int max_right(int l,F f) const {
assert(0 <= l && l <= _n);
assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm,d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm,d[l]))) {
sm = op(sm,d[l]);
l++;
}
}
return l - size;
}
sm = op(sm,d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) const {
return min_left(r,[](S x) { return f(x); });
}
template <class F> int min_left(int r,F f) const {
assert(0 <= r && r <= _n);
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r],sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r],sm))) {
sm = op(d[r],sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r],sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n,size,log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k],d[2 * k + 1]); }
};
} // namespace atcoder
using atcoder::segtree;
namespace segtreeUtils{
using Int=long long;
using ll=long long;
template<class T> T assign(T x,T y){ (void)x; return y; };
template<class T> T add(T x,T y){ return x+y; };
/*---- ACLセグ木のwrap ----*/
template<class T,auto op,auto e,auto f=assign<T>>
struct segtreeWrap{
atcoder::segtree<T,op,e> sgt;
ll N;
ll offset=0; //位置iの値をi-offsetに格納
segtreeWrap(Int N):sgt(int(N)),N(N){}
segtreeWrap(ll l,ll r):sgt(int(r-l+1)),N(r-l+1),offset(l){} //i=l~r
segtreeWrap(const vector<T> &v,ll l=0):sgt(v),N((ll)v.size()),offset(l){}
ll imin()const{ return offset; }
ll imax()const{ return N-1+offset; }
void set(Int i,const T &x){ sgt.set(int(i-offset),x); }
template<class E> void apply(Int i,const E &y){ set(i,f(get(i),y)); }
T get(Int i)const{ return sgt.get(int(i-offset)); }
T operator[](Int i)const{ return get(i); }
T prod(Int l,Int r)const{ return sgt.prod(int(l-offset),int(r+1-offset)); }
T get(Int l,Int r)const{ return prod(l,r); }
T allprod()const{ return sgt.all_prod(); }
template<class IsOK> Int maxRight(Int l,Int R,IsOK isOK)const{
ll pos=sgt.max_right((int)normalizeL(l),isOK)-1;
return normalizeR(R)<pos ? max(R,l-1) : pos+offset;
}
template<class IsOK> ll minRight(Int l,Int R,IsOK isOK)const{
ll pos=maxRight(l,R,[&isOK](const T &prd){return !isOK(prd); })+1;
return min(pos,R+1);
}
template<class IsOK> ll minLeft(Int L,Int r,IsOK isOK)const{
ll pos=sgt.min_left((int)normalizeR(r)+1,isOK);
return pos<normalizeL(L) ? min(L,r+1) : pos+offset;
}
template<class IsOK> ll maxLeft(Int L,Int r,IsOK isOK)const{
ll pos=minLeft(L,r,[&isOK](const T &prd){return !isOK(prd); })-1;
return max(pos,L-1);
}
#if 0
void dump()const{
vector<T> v;
for (ll i=imin(); i<=imax(); ++i) v.push_back(get(i));
dumpstring::dumpNd("",v,args().sx(imin()).labels({"i"}));
}
#endif
private:
ll normalizeL(ll l)const{ return clamp(l-offset,0LL,N); }
ll normalizeR(ll r)const{ return clamp(r-offset,-1LL,N-1); }
};
/*---- max/min特化セグ木 ----*/
template<class T,bool IsMAX,T E,auto f=add<T>,
auto op=[](T x,T y){return IsMAX ? max(x,y) : min(x,y); },
auto e=[]{return E; }>
struct SegtreeMaxOrMin: segtreeWrap<T,op,e,f>{
using Base = segtreeWrap<T,op,e,f>;
using Base::Base;
Int findNextGE(Int i,T x)const{
return Base::minRight(i,Base::imax(),[&](T va){return IsMAX ? va>=x : va<=x; });
}
Int findPrevGE(Int i,T x)const{
return Base::maxLeft(Base::imin(),i,[&](T va){return IsMAX ? va>=x : va<=x; });
}
Int findPeakL(Int l,Int r)const{ return findNextGE(l,Base::prod(l,r)); }
Int findPeakR(Int l,Int r)const{ return findPrevGE(r,Base::prod(l,r)); }
};
using segtreemax=SegtreeMaxOrMin<ll,true,-INF>;
using segtreemin=SegtreeMaxOrMin<ll,false,INF>;
}//namespace
using segtreeUtils::segtreeWrap; using segtreeUtils::SegtreeMaxOrMin;
using segtreeUtils::segtreemax; using segtreeUtils::segtreemin;
/*
ACLのsegtreeにoffset機能と各種I/Fを追加
適用関数fは不使用時省略可(set使えばよい)
- ---- 定義 一般のセグ木 ----
using VAL=ll;
constexpr auto opfunc=[](const VAL& x,const VAL& y){return x^y;}; //合成関数
constexpr auto efunc=[]{ return 0ll; }; //単位元
constexpr auto ffunc=[](ll x,pll y){auto[a,b]=y; return x*a+b;}; //適用関数 省略可
segtreeWrap<VAL,opfunc,efunc,ffunc> sgt(N); //i=0~N-1 全て単位元
segtreeWrap<VAL,opfunc,efunc,ffunc> sgt(l,r); //i=l~r 全て単位元
segtreeWrap<VAL,opfunc,efunc,ffunc> sgt(ini); //i=0~N-1 v[0]~v[N-1]で初期化
segtreeWrap<VAL,opfunc,efunc,ffunc> sgt(ini,l); //i=l~l+N-1 v[0]~v[N-1]で初期化
segtreeWrap<VAL,opfunc,efunc> sgt(ini); //f不使用(代入しかしないとき)
- ---- 定義 一般のmax/minセグ木 ----
・引数は一般のセグ木と同様
・適用関数のデフォルトは加算
using VAL=ll;
constexpr auto ffunc=[](ll x,pll y){auto[a,b]=y; return x*a+b;}; //適用関数 省略可
SegtreeMaxOrMin<VAL,true,-INF,ffunc> sgt(v);
. 型┘ ↑ └単位元
. true:max,false:min
SegtreeMaxOrMin<ll,true,0> sgt(v); //一点加算maxセグ木 単位元0
SegtreeMaxOrMin<dd,false,1e300> sgt(v); //double型一点加算minセグ木
- ---- 定義 ll型一点加算maxセグ木 ----
segtreemax sgt(N); //i=0~N-1 全て単位元
segtreemax sgt(l,r); //i=l~r 全て単位元
segtreemax sgt(v); //i=0~N-1 v[0]~v[N-1]で初期化
segtreemax sgt(v,l); //i=l~l+N-1 v[0]~v[N-1]で初期化
- ---- 定義 ll型一点加算minセグ木 ----
segtreemin sgt(N); //
segtreemin sgt(l,r); //引数はmaxセグ木と同様
segtreemin sgt(v); //
segtreemin sgt(v,l); //
- ---- 操作 ----
ll i=sgt.imin(); //iの下限
ll i=sgt.imax(); //iの上限
sgt.offset+=d; //全iについて位置iの要素をi+dにシフト
sgt.set(i,x); //a[i]←x 位置iにx代入
sgt.apply(i,y); //a[i]←f(a[i],y) 適用関数使用
ll x=sgt.get(i); //a[i] 位置iの値
ll x=sgt[i]; //a[i] 位置iの値
ll x=sgt.prod(l,r); //[l,r]の値
ll x=sgt.get(l,r); // 〃
ll x=sgt.allprod(); //全区間の値
- ---- max(min)セグ木のみの操作 ----
ll j=sgt.findNextGE(i,x); //i以降で初めてx以上(以下)になる位置
ll j=sgt.findPrevGE(i,x); //i以前で 〃
ll i=sgt.findPeakL(l,r); //区間[l,r]内max(min)になる最左位置
ll i=sgt.findPeakR(l,r); //区間[l,r]内max(min)になる最右位置
- ---- 二分探索 ----
[L,R]内でラムダ式isOKが○(true)になる位置 L,Rは範囲外や逆転も可
- 使用例
ll i=sgt.minRight(L,R,[&](ll x){return x>=th;}); // L×→×→×ⓡ○R
ll i=sgt.maxRight(L,R,[&](ll x){return x<=th;}); // L○→○→ⓡ××R
ll i=sgt.maxLeft (L,R,[&](ll x){return x>=th;}); //L○ⓛ×←×←×R
ll i=sgt.minLeft (L,R,[&](ll x){return x<=th;}); //L××ⓛ←○←○R
- 要件・挙動
minRight: isOK([L,r])=○(l≦r≦R)となる最左のr、ないときR+1 要件:isOK(e)=×
maxRight: isOK([L,r])=○(l≦r≦R)となる最右のr、ないときL-1 要件:isOK(e)=○
maxLeft: isOK([l,R])=○(L≦l≦r)となる最右のl、ないときL-1 要件:isOK(e)=×
minLeft: isOK([l,R])=○(L≦l≦r)となる最左のl、ないときR+1 要件:isOK(e)=○
*/
namespace BITSpace{
//generated by GPT-5.6 Sol modified by hamamu
using ll = long long;
using Int = long long;
// デフォルトの加算・減算
template<class T> struct BITAdd{
T operator()(const T &a,const T &b)const{ return a+b; }
};
template<class T> struct BITSub{
T operator()(const T &a,const T &b)const{ return a-b; }
};
template<class T,class Ope=BITAdd<T>,class Sub=BITSub<T>>
struct BIT{
// sub=nullptr_t のとき、逆演算が必要な関数を requires で制限する。
static constexpr bool HAS_SUB=!is_same_v<Sub,nullptr_t>;
ll n=0;
T e;
Ope op;
Sub sub;
// bit[i]は1-origin Fenwick Tree のノード
// 公開添字iはbit[i+1]側に対応
vector<T> bit;
// デフォルトは加算・減算。iniが空なら全要素eとして構築を省略する。
BIT(T,ll n_,const vector<T> &ini={},const T &e_=T()):n(n_),e(e_),op(Ope()),sub(Sub()){ init(ini); }
// op指定。sub省略時(nullptr)は逆演算なし。
BIT(T,ll n_,const vector<T> &ini,const T &e_,Ope op_,Sub sub_=nullptr):n(n_),e(e_),op(move(op_)),sub(move(sub_)){ init(ini); }
void apply(ll i,const T &x){
assert(0<=i && i<n);
for (ll j=i+1; j<=n; j+=j&-j) bit[j]=op(bit[j],x);
}
void set(ll i,const T &x) requires(HAS_SUB){
assert(0<=i && i<n);
apply(i,sub(x,get(i))); //現在値との差分を追加してA[i]=xにする
}
T get(ll i)const requires(HAS_SUB){
if (i<0 || n<=i) return e; // 範囲外は単位元
return sub(prefix(i),prefix(i-1)); //1点はiからi-1を引く
}
T operator[](ll i)const requires(HAS_SUB){ return get(i); }
T get(ll l,ll r)const requires(HAS_SUB){
if (r<l) return e;
l=max(l,0LL),r=min(r,n-1);
return r<l ? e : sub(prefix(r),prefix(l-1));
}
T prefix(ll r)const{
if (r<0 || n==0) return e;
r=min(r,n-1);
T ret=e;
for (ll j=r+1; j>0; j-=j&-j) ret=op(ret,bit[j]);
return ret;
}
pair<Int,T> floorGroup(ll l,T x)const requires(HAS_SUB){
assert(0<=l && l<=n);
//prefix(l-1)+x以下となる最大prefixをBIT上で二分探索する
T base=prefix(l-1),th=op(base,x);
auto [p,pref]=maxPrefix([&](const T &v){ return !(th<v); });
T range=sub(pref,base);
return {(Int)(p-1),sub(x,range)};
}
pair<Int,T> ceilGroup(ll l,T x)const requires(HAS_SUB){
assert(0<=l && l<=n);
if (x==e) return {(Int)(l-1),e}; //x=0なら空区間r=l-1が最小の答
//閾値prefix(l-1)+x未満となる最大prefixを二分探索で得る
//その次の要素(=p)が初めて閾値以上になる位置
T base=prefix(l-1),th=op(base,x);
auto [p,pref]=maxPrefix([&](const T &v){ return v<th; });
if (p==n) return {(Int)n,e}; // 全要素を使っても届かなければ番兵値 n。
T range=sub(prefix(p),base);
return {(Int)p,sub(range,x)};
}
pair<Int,T> floorGroupRev(ll r,T x)const requires(HAS_SUB){
assert(-1<=r && r<n);
//sum[l,r]=prefix(r)-prefix(l-1)なのでprefix(l-1)>=prefix(r)-xを探す
T total=prefix(r),th=sub(total,x);
ll l=maxPrefix([&](const T &v){ return v<th; }).first+1;
T range=sub(total,prefix(l-1));
return {(Int)l,sub(x,range)};
}
pair<Int,T> ceilGroupRev(ll r,T x)const requires(HAS_SUB){
assert(-1<=r && r<n);
if (x==e) return {(Int)(r+1),e}; //x=0なら、空区間l=r+1が最大の答
T total=prefix(r);
//sum[l,r]=prefix(r)-prefix(l-1)なのでprefix(l-1)<=prefix(r)-xを探す
T th=sub(total,x);
auto [l,pref]=maxPrefix([&](const T &v){ return !(th<v); });
T range=sub(total,pref);
return {(Int)l,sub(range,x)};
}
private:
// iniが空なら全要素eとして構築省略、そうでなければO(N)構築。
void init(const vector<T> &ini){
assert(n>=0);
bit.assign(n+1,e);
if (ini.empty()) return;
assert((ll)ini.size()==n);
//iniを葉として入れ、各要素を一度だけ親へ集約
for (ll i=0; i<n; ++i) bit[i+1]=ini[i];
for (ll i=1; i<=n; ++i){
ll p=i+(i&-i);
if (p<=n) bit[p]=op(bit[p],bit[i]);
}
}
// P[p]=op(A[0],...,A[p-1]) とする。
// 単調な条件 ok(P[p]) を満たす最大 p と P[p] を binary lifting で求める。なければp=-1。
template<class F>
pair<ll,T> maxPrefix(F ok)const{
if (!ok(e)) return {-1,e};
ll p=0;
T cur=e;
for (ll step=(ll)bit_floor((unsigned long long)n); step; step>>=1){
ll np=p+step;
if (np<=n){
T nxt=op(cur,bit[np]);
if (ok(nxt)) p=np,cur=move(nxt);
}
}
return {p,cur};
}
};
// custom opでsubを省略したときは逆演算なし。
template<class T,class Ope> BIT(T,ll,const vector<T>&,const T&,Ope) -> BIT<T,Ope,nullptr_t>;
}
using BITSpace::BIT;
/*
- ---- 定義 ----
BIT bit(ll(),N); //データ型ll、[0,N-1]、初期値0で構築省略
BIT bit(ll(),N,ini); //iniからO(N)構築
BIT bit(ll(),N,ini,e); //単位元e指定
BIT bit(ll(),N,ini,e,op); //op指定,逆演算sub無 一部操作不可となる
BIT bit(ll(),N,ini,e,op,sub); //op指定,逆演算sub有
BIT bit(ll(),N,vll(),e); //iniを空vectorにすると全要素eで構築省略
//op=maxの例
BIT bit(ll(),N,ini,-INF,[](ll a,ll b){return max(a,b);});
//op=XORの例
BIT bit(ll(),N,ini, 0,
[](ll a,ll b){return a^b;}, //op
[](ll a,ll b){return a^b;}); //sub
- ---- 操作 ----
bit.apply(i,x); //A[i]<-op(A[i],x)
bit.set(i,x); //A[i]<-x sub有時のみ
- ---- 取得 ---- [0,N)外は単位元として扱う
T x=bit.get(i); //A[i] sub有時のみ
T x=bit[i]; //A[i] sub有時のみ
T x=bit.get(l,r); //[l,r] sub有時のみ
T x=bit.prefix(r);//[0,r]
- ---- BIT上二分探索 ---- op,subが加減算、x>=0、全Ai>=0が前提
auto [r,rem]=bit.floorGroup(l,x); //Al+A_{l+1}+…+Ar+rem=xなる最大のr
auto [r,rem]=bit. ceilGroup(l,x); //Al+A_{l+1}+…+Ar=x+remなる最小のr ない時N
auto [l,rem]=bit.floorGroupRev(r,x); //Ar+A_{r-1}+…+Al+rem=xなる最小のl
auto [l,rem]=bit. ceilGroupRev(r,x); //Ar+A_{r-1}+…+Al=x+remなる最大のl ない時-1
*/
namespace GridEnumerate{
using ll = long long;
template<class MLL,class CMB> MLL CutL(ll N,ll M,ll L,CMB &cmb){
if (L>min(N,M)) return MLL(0);
return cmb(N+M,N)-cmb(N+M,L-1);
}
template<class MLL,class CMB> MLL CutLK(ll N,ll M,ll L,ll K,CMB &cmb){
MLL ret=0;
if (L>min(N,M) || K>min(N,M)) return ret;
ll P=N+M-K-L+2;
for (ll i= N%P; i<=N+M; i+=P) ret+=cmb(N+M,i);
for (ll i=(K-1)%P; i<=N+M; i+=P) ret-=cmb(N+M,i);
return ret;
}
template<class MLL,class CMB> MLL turnKtimes___UpStart(ll N,ll M,ll K,CMB &cmb){
if (K==0) return M==0 ? 1 : 0; //上startでK=0のとき 横幅0なら1経路、1以上なら0経路
else {//K>=1のとき
if (N==0 || M==0) return MLL(0); //幅0だと曲がれない→0
return cmb(N-1,K/2)*cmb(M-1,(K-1)/2); //右折K/2回、左折(K-1)/2回
}
}
template<class MLL,class CMB> MLL turnKtimesRightStart(ll N,ll M,ll K,CMB &cmb){
if (K==0) return N==0 ? 1 : 0; //右startでK=0のとき 縦幅0なら1経路、1以上なら0経路
else{//K>=1のとき
if (N==0 || M==0) return MLL(0); //幅0だと曲がれない→0
return cmb(N-1,(K-1)/2)*cmb(M-1,K/2); //右折(K-1)/2回、左折K/2回
}
}
template<class MLL,class CMB> MLL turnKtimes(ll N,ll M,ll K,CMB &cmb){
MLL ret(0);
if (N==0 && M==0 && K==0) return MLL(1); //始点終点一致の時の上/右start二重count回避
ret+=turnKtimes___UpStart<MLL>(N,M,K,cmb);
ret+=turnKtimesRightStart<MLL>(N,M,K,cmb);
return ret;
}
template<class MLL,class CMB>
MLL turnKtimesRightStartRightEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){
if (K%2==1) return MLL(0);
ll KK=(K+2)/2;
return cmb(N-1,KK-2)*cmb(M-1,KK-1)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK-1);
}
template<class MLL,class CMB>
MLL turnKtimesRightStart___UpEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){
if (K%2==0) return MLL(0);
ll KK=(K+1)/2;
return cmb(N-1,KK-1)*cmb(M-1,KK-1)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK);
}
template<class MLL,class CMB>
MLL turnKtimes___UpStartRightEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){
if (K%2==0) return MLL(0);
ll KK=(K+3)/2;
return cmb(N-1,KK-2)*cmb(M-1,KK-2)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK-2);
}
template<class MLL,class CMB>
MLL turnKtimes___UpStart___UpEndCutL(ll N,ll M,ll L,ll K,CMB &cmb){
if (K%2==1) return MLL(0);
ll KK=(K+2)/2;
return cmb(N-1,KK-1)*cmb(M-1,KK-2)-cmb(M+N-L-1,KK-2)*cmb(L-1,KK-1);
}
template<class MLL,class CMB>
MLL turnKtimesCutL(ll N,ll M,ll L,ll K,CMB &cmb){
MLL ret(0);
if (L>min(N,M)) return ret;
if (L<=0) return turnKtimes<MLL>(N,M,K,cmb);//L=0だと合わないため別計算
ret+=turnKtimesRightStartRightEndCutL<MLL>(N,M,L,K,cmb);
ret+=turnKtimesRightStart___UpEndCutL<MLL>(N,M,L,K,cmb);
ret+=turnKtimes___UpStartRightEndCutL<MLL>(N,M,L,K,cmb);
ret+=turnKtimes___UpStart___UpEndCutL<MLL>(N,M,L,K,cmb);
return ret;
}
}//namespace
using GridEnumerate::CutL; using GridEnumerate::turnKtimes;
using GridEnumerate::CutLK; using GridEnumerate::turnKtimesCutL;
/*
N*Mグリッド辺上でのs→t経路数
-- 要件 --
- cmb(ll n,ll r)がbinom{n}{r}をMLL型で返すこと
- cmb(ll n,ll r)がn≦M+N-1で動作すること
- ------------------------- 欠損なし -------------------------
┌┬┬┬┬┬t --
├┼┼┼┼┼┤ ↑
├┼┼┼┼┼┤ N
├┼┼┼┼┼┤ ↓
s┴┴┴┴┴┘ --
.←---M---→
combination<mll> cmb(M+N+10);
mll x=turnKtimes<mll>(N,M,K,cmb); //K回曲がる
- ----------------------- 左上斜め欠損 -----------------------
. N=4,M=7,L=3 || N=M=L=4
=====================||===============
.←L→┌┬┬┬t -- || ←-L-→t --
. ┌┼┼┼┼┤ ↑ || ┌┤ ↑
. ┌┼┼┼┼┼┤ N || ┌┼┤ N
┌┼┼┼┼┼┼┤ ↓ || ┌┼┼┤ ↓
s┴┴┴┴┴┴┘ -- || s┴┴┴┘ --
.←----M----→ || ←-M-→
combination<mll> cmb(M+N+10);
mll x=CutL<mll>(N,M,L,cmb); //任意回曲がる
mll x=turnKtimesCutL<mll>(N,M,L,K,cmb); //K回曲がる
- --------------------- 左上&右下斜め欠損 --------------------
.←L→┌┬┬┬t --
. ┌┼┼┼┼┤ ↑ N=4,M=7,L=3,K=2
. ┌┼┼┼┼┼┘ N
┌┼┼┼┼┼┘ ↓
s┴┴┴┴┘-K- --
.←----M----→
combination<mll> cmb(M+N+10);
mll x=CutLK<mll>(N,M,L,K,cmb); //任意回曲がる
*/
void cin2solve()
{
auto [N,Q]=cins<ll,ll>();
auto S=cin1<string>();
auto query=cinv<pair<ll,ll>>(Q);
combination<mll> cmb(N+10);
;/*
mll v=cmb(n,r); // nCr 負も可、n<rも可、n巨大も愚直計算
mll v=cmb.P(n,r); // nPr
mll v=cmb.H(n,r); // nHr x1+…+xn=rの個数 初期化時cmb(n+r-1)以上必要
mll v=cmb.inv(n); // nの逆数
mll v=cmb.finv(n); // n!の逆数
mll v=cmb.fact(n); // n!
*/
BIT bitP(ll(),N); //Y1 N0
BIT bitN(ll(),N); //Y0 N1
rep(i,0,N-1){
if (S[i]=='Y') bitP.apply(i,1);
else bitN.apply(i,1);
}
auto lpos=[&](ll i){//[i]を含むランの左端
auto &bit=(S[i]=='Y') ? bitN : bitP;
auto [l,rem]=bit.floorGroupRev(i,0); //Ar+A_{r-1}+…+Al+rem=xなる最小のl
return l;
};
auto rpos=[&](ll i){//[i]を含むランの右端
auto &bit=(S[i]=='Y') ? bitN : bitP;
auto [r,rem]=bit.floorGroup(i,0); //Al+A_{l+1}+…+Ar+rem=xなる最大のr
return r;
};
using VAL=mll;
constexpr auto opfunc=[](const VAL& x,const VAL& y){return x*y; }; //合成関数
constexpr auto efunc=[]{ return 1; }; //単位元
segtreeWrap<VAL,opfunc,efunc> sgt(N); //i=0~N-1 v[0]~v[N-1]で初期化
auto f=[&](ll len)->mll{//カタラン数(len/2)
if (len%2==1) return 0;
len/=2;
return cmb(len*2,len) * cmb.inv(len+1);
};
{
ll i=0;
while (i<=N-1){
ll r=rpos(i);
ll len=r-i+1;
sgt.set(i,f(len));
i=r+1;
}
}
auto Q1=[&](ll qi){
auto [ki,i]=query[qi];
i--;
//---- セグ木から元を削除
//左 不要
//中
if (1<=i and S[i-1]!=S[i]){
sgt.set(i,1);
}
//右
if (i+1<=N-1 and S[i]!=S[i+1]){
sgt.set(i+1,1);
}
//---- セグ木に新値セット
//左
if (1<=i){
ll l=lpos(i-1);
if (S[i-1]==S[i]){//中左で止まるとき
ll len=i-l;
sgt.set(l,f(len));
}
else if(i==N-1 or S[i]==S[i+1]){//中右で止まるとき
ll len=i-l+1;
sgt.set(l,f(len));
}
else{//右までいくとき
ll r=rpos(i+1);
ll len=r-l+1;
sgt.set(l,f(len));
}
}
//中
if (i==0 or S[i-1]==S[i]){
if (i+1<=N-1 and S[i]!=S[i+1]){//右までいくとき
ll r=rpos(i+1);
ll len=r-i+1;
sgt.set(i,f(len));
}
else{//中右で止まるとき
sgt.set(i,f(1));
}
}
//右
if (i+1<=N-1 and S[i]==S[i+1]){
ll r=rpos(i+1);
ll len=r-i;
sgt.set(i+1,f(len));
}
//---- S,bit
if (S[i]=='Y'){
S[i]='N';
bitP.apply(i,-1);
bitN.apply(i,1);
}
else{
S[i]='Y';
bitP.apply(i,1);
bitN.apply(i,-1);
}
};
auto Q2=[&](ll qi){
auto [ki,K]=query[qi];
//右端だけ修正
ll l=lpos(N-1);
ll len=N-l;
ll h=abs(N-2*K);
if ((len+h)%2==1){
cout << 0 << '\n';
return;
}
ll a=(len+h)/2;
ll b=len-a;
mll x=CutL<mll>(b/*N*/,a/*M*/,b/*L*/,cmb);//任意回曲がる
sgt.set(l,x);
mll ans=sgt.allprod();
cout << ans << '\n';
};
rep(qi,0,Q-1){
ll ki=get<0>(query[qi]);
if (ki==1) Q1(qi);
if (ki==2) Q2(qi);
}
return;
}
}//SolvingSpace
//////////////////////////////////////////
int main(){
#if defined(RANDOM_TEST)
SolvingSpace::cin2solve();
SolvingSpace::generand();
#else
#if 1
//SolvingSpace::labo();
SolvingSpace::cin2solve();
#else
ll t; cin >> t;
rep(i,0,t-1){
SolvingSpace::cin2solve();
}
#endif
#endif
cerr << timeget() <<"ms"<< '\n';
return 0;
}
hamamu