結果

問題 No.2720 Sum of Subarray of Subsequence of...
ユーザー i_am_noobi_am_noob
提出日時 2024-04-14 17:46:02
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 287 ms / 4,000 ms
コード長 8,460 bytes
コンパイル時間 4,997 ms
コンパイル使用メモリ 275,896 KB
実行使用メモリ 12,924 KB
最終ジャッジ日時 2024-10-03 18:42:46
合計ジャッジ時間 11,392 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())
using ll=long long;
using u32=unsigned int;
using u64=unsigned long long;
using i128=__int128;
using u128=unsigned __int128;
using f128=__float128;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<typename T> using vc=vector<T>;
template<typename T> using vvc=vc<vc<T>>;
template<typename T> using vvvc=vc<vvc<T>>;
using vi=vc<int>;
using vll=vc<ll>;
using vvi=vc<vi>;
using vvll=vc<vll>;
#define vv(type,name,n,...) \
vector<vector<type>> name(n,vector<type>(__VA_ARGS__))
#define vvv(type,name,n,m,...) \
vector<vector<vector<type>>> name(n,vector<vector<type>>(m,vector<type>(__VA_ARGS__)))
template<typename T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<typename T> using max_heap=priority_queue<T>;
// https://trap.jp/post/1224/
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define per1(n) for(ll i=((ll)n)-1; i>=0; --i)
#define per2(i,n) for(ll i=((ll)n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=((ll)a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=((ll)a)-1; i>=(ll)(b); i-=(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define rep_subset(i,s) for(ll i=(s); i>=0; i=(i==0?-1:(i-1)&(s)))
template<typename T, typename S> constexpr T ifloor(const T a, const S b){return a/b-(a%b&&(a^b)<0);}
template<typename T, typename S> constexpr T iceil(const T a, const S b){return ifloor(a+b-1,b);}
template<typename T>
void sort_unique(vector<T> &vec){
sort(vec.begin(),vec.end());
vec.resize(unique(vec.begin(),vec.end())-vec.begin());
}
template<typename T, typename S> constexpr bool chmin(T &a, const S b){if(a>b) return a=b,true; return false;}
template<typename T, typename S> constexpr bool chmax(T &a, const S b){if(a<b) return a=b,true; return false;}
template<typename T, typename S> istream& operator >> (istream& i, pair<T,S> &p){return i >> p.first >> p.second;}
template<typename T, typename S> ostream& operator << (ostream& o, const pair<T,S> &p){return o << p.first << ' ' << p.second;}
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
template<typename T> void print(vector<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(set<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(unordered_set<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}
template<typename T> istream& operator >> (istream& i, vector<T> &vec){for(auto &x: vec) i >> x; return i;}
vvi read_graph(int n, int m, int base=1){
vvi adj(n);
for(int i=0,u,v; i<m; ++i){
cin >> u >> v,u-=base,v-=base;
adj[u].pb(v),adj[v].pb(u);
}
return adj;
}
vvi read_tree(int n, int base=1){return read_graph(n,n-1,base);}
template<typename T, typename S> pair<T,S> operator + (const pair<T,S> &a, const pair<T,S> &b){return {a.first+b.first,a.second+b.second};}
template<typename T> constexpr T inf=0;
template<> constexpr int inf<int> = 0x3f3f3f3f;
template<> constexpr ll inf<ll> = 0x3f3f3f3f3f3f3f3f;
template<typename T> vector<T> operator += (vector<T> &a, int val){for(auto &i: a) i+=val; return a;}
template<typename T> T isqrt(const T &x){T y=sqrt(x+2); while(y*y>x) y--; return y;}
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#include<atcoder/all>
using namespace atcoder;
using mint=modint998244353;
//using mint=modint1000000007;
template<int mod=998244353>
using poly=vector<static_modint<mod>>;
template<int mod=998244353>
poly<mod> add(poly<mod> a, poly<mod> b){
poly<mod> res=a;
if(b.size()>a.size()) res.resize(b.size());
for(int i=0; i<b.size(); ++i) res[i]+=b[i];
return res;
}
template<int mod=998244353>
poly<mod> sub(poly<mod> a, poly<mod> b){
poly<mod> res=a;
if(b.size()>a.size()) res.resize(b.size());
for(int i=0; i<b.size(); ++i) res[i]-=b[i];
return res;
}
template<int mod=998244353>
poly<mod> inverse(poly<mod> a){
int n=a.size();
assert(a[0]!=0);
poly<mod> res(1,a[0].inv());
for(int m=1; m<n; m<<=1){
if(n<m*2) a.resize(m*2);
poly<mod> v1(a.begin(),a.begin()+m*2),v2=res;
v1=convolution(convolution(v1,v2),v2);
res.resize(m*2);
for(int i=0; i<m; ++i) res[i]+=res[i];
for(int i=0; i<m*2; ++i) res[i]-=v1[i];
}
res.resize(n);
return res;
}
template<int mod=998244353>
pair<poly<mod>,poly<mod>> divide(poly<mod> a, poly<mod> b){
int n=a.size(),m=b.size(),k=n-m+1;
if(n<m) return {{0},a};
poly<mod> ra=a,rb=b;
reverse(all(ra)),reverse(all(rb));
ra.resize(k),rb.resize(k);
poly<mod> Q=convolution(ra,inverse(rb));
Q.resize(k);
reverse(all(Q));
poly<mod> res=convolution(b,Q),R(m-1);
for(int i=0; i<m-1; ++i) R[i]=a[i]-res[i];
return {Q,R};
}
template<int mod=998244353>
poly<mod> Derivative(poly<mod> a){
int n=a.size();
poly<mod> res(n-1);
for(int i=0; i<n-1; ++i) res[i]=a[i+1]*(i+1);
return res;
}
template<int mod=998244353>
poly<mod> Integral(poly<mod> a){
int n=a.size();
poly<mod> res(n+1);
for(int i=0; i<n; ++i) res[i+1]=a[i]/(i+1);
return res;
}
template<int mod=998244353>
poly<mod> Ln(poly<mod> a){
// a[0] = 1
int n=a.size();
if(n==1) return {};
poly<mod> d=Derivative(a);
a.pop_back();
poly<mod> res=convolution(d,inverse(a));
res.resize(n-1);
return Integral(res);
}
template<int mod=998244353>
poly<mod> Exp(poly<mod> a){
// a[0] = 0
int n=a.size();
poly<mod> q(1,1);
a[0]+=1;
for(int m=1; m<n; m<<=1){
if(n<m*2) a.resize(m*2);
poly<mod> g(a.begin(),a.begin()+m*2),h(all(q));
h.resize(m*2),h=Ln(h);
for(int i=0; i<m*2; ++i) g[i]-=h[i];
q=convolution(g,q);
q.resize(m*2);
}
q.resize(n);
return q;
}
template<int mod=998244353>
poly<mod> pow(poly<mod> a, ll k){
int n=a.size(),m=0;
poly<mod> res(n,0);
while(m<n&&a[m]==0) m++;
if(k&&m&&(k>=n||k*m>=n)) return res;
if(m==n) return res[0]=1,res;
int lead=k*m;
poly<mod> b(a.begin()+m,a.end());
static_modint<mod> base=b[0].pow(k),inv=b[0].inv();
for(int i=0; i<n-m; ++i) b[i]*=inv;
b=Ln(b);
if(b.empty()) b.pb(0);
for(int i=0; i<n-m; ++i) b[i]*=k;
b=Exp(b);
for(int i=lead; i<n; ++i) res[i]=b[i-lead]*base;
return res;
}
void ahcorz(){
int n,m; cin >> n >> m;
vi a(n); cin >> a;
string s; cin >> s;
reverse(all(s));
s+='a';
vi tmp1,tmp2;
int cur=0;
for(auto c: s){
if(c=='s') cur++;
else{
tmp1.pb(cur),tmp2.pb(cur+1);
}
}
auto solve=[&](auto &self, int l, int r, bool flag) -> vc<mint>{
if(l+1==r){
if(flag) return {1,-tmp2[l]};
else return {1,-tmp1[l]};
}
int mid=l+r>>1;
return convolution(self(self,l,mid,flag),self(self,mid,r,flag));
};
vc<mint> vec1=solve(solve,0,sz(tmp1),0),vec2=solve(solve,0,sz(tmp2),1);
vec1.resize(n+1),vec2.resize(n+1);
// 1 / (1 - cur * x)
vc<mint> vec3(n+1);
vec3[0]=1;
rep(n) vec3[i+1]=vec3[i]*cur;
vc<mint> tmp=convolution(vec1,vec3);
tmp.resize(n+1);
vc<mint> P=convolution(tmp,inverse(vec2));
mint res;
rep(n) res+=a[i]*P[i]*P[n-1-i];
print(res.val());
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cout << fixed << setprecision(20);
int t=1;
//cin >> t;
while(t--) ahcorz();
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0