QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#728961 | #9614. 分治 | chenxinyang2006 | 0 | 0ms | 0kb | C++20 | 7.8kb | 2024-11-09 16:16:42 | 2024-11-09 16:16:47 |
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define mem(a,b) memset(a,b,sizeof(a))
#define mpy(a,b) memcpy(a,b,sizeof(b))
#define dbg(...) cerr<<"#"<<__LINE__<<": "<<__VA_ARGS__<<endl
#define Fi(s) freopen(s,"r",stdin)
#define Fo(s) freopen(s,"w",stdout)
#define Fio(s) Fi(s".in"),Fo(s".out")
#define SZ(S) (int)S.size()
#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;
template<int P>
class mod_int{
using Z=mod_int;
private:
static int mo(int x){return x<0?x+P:x;}
public:
int x;
int val()const{return x;}
mod_int():x(0){}
template<class T>mod_int(const T&x_):x(x_>=0&&x_<P?static_cast<int>(x_):mo(static_cast<int>(x_%P))){}
bool operator==(const Z&rhs)const{return x==rhs.x;}
bool operator!=(const Z&rhs)const{return x!=rhs.x;}
Z operator-()const{return Z(x?P-x:0);}
Z pow(long long k)const{Z res=1,t=*this;while(k){if(k&1)res*=t;if(k>>=1)t*=t;}return res;}
Z&operator++(){x<P-1?++x:x=0;return *this;}
Z&operator--(){x?--x:x=P-1;return *this;}
Z operator++(int){Z ret=x;x<P-1?++x:x=0;return ret;}
Z operator--(int){Z ret=x;x?--x:x=P-1;return ret;}
Z inv()const{return pow(P-2);}
Z&operator+=(const Z&rhs){(x+=rhs.x)>=P&&(x-=P);return *this;}
Z&operator-=(const Z&rhs){(x-=rhs.x)<0&&(x+=P);return *this;}
Z&operator*=(const Z&rhs){x=1ULL*x*rhs.x%P;return *this;}
Z&operator/=(const Z&rhs){return *this*=rhs.inv();}
#define setO(T,o) friend T operator o(const Z&lhs,const Z&rhs){Z res=lhs;return res o##=rhs;}
setO(Z,+)setO(Z,-)setO(Z,*)setO(Z,/)
#undef setO
};
const int P = 998244353;
using Z = mod_int<P>;
ll qpow(ll x, ll k){
ll ret = 1;
while(k){
if(k & 1) (ret *= x) %= mod;
(x *= x) %= mod, k >>= 1;
}
return ret;
}
namespace Poly_space{
Z inv2 = (P + 1) / 2;
vector<int> rev;
vector<Z> gg = {0, 1};
Z rt = 3;
void setroot(Z x){rt = x;}
void dft(vector<Z> &a){
int n = a.size();
if((int)rev.size() != n){
int k = __builtin_ctz(n) - 1; rev.resize(n);
for(int i = 0; i < n; i++){rev[i] = rev[i >> 1] >> 1 | (i & 1 ? (1 << k) : 0);}
}
for(int i = 0; i < n; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
if((int)gg.size() < n){
int k = __builtin_ctz(gg.size()); gg.resize(n);
while((1 << k) < n){
Z e = rt.pow((P - 1) >> (k + 1));
for(int i = (1 << (k - 1)); i < (1 << k); i++) gg[i << 1] = gg[i], gg[(i << 1) | 1] = gg[i] * e;
k++;
}
}
for(int mid = 1; mid < n; mid <<= 1) for(int i = 0; i < n; i += (mid << 1)) for(int j = 0; j < mid; j++){
Z x = a[i + j], y = a[i + j + mid] * gg[mid + j];
a[i + j] = x + y, a[i + j + mid] = x - y;
}
}
void idft(vector<Z> &a){
int n = a.size(); reverse(a.begin() + 1, a.end()), dft(a);
Z inv = Z(1 - P) / Z(n); for(int i = 0; i < n; i++) a[i] *= inv;
}
struct Poly{
vector<Z> a;
Poly(){} Poly(const vector<Z> &x): a(x){} Poly(const initializer_list<Z> &x): a(x){}
int size()const{return a.size();} void resize(int x){a.resize(x);}
Z operator [](int ind)const{if(ind < 0 || ind >= size()) return 0; return a[ind];}
Z&operator [](int ind){return a[ind];}
Poly modxk(int k)const{k = min(k, size()); return Poly(vector<Z>(a.begin(), a.begin() + k));}
Poly mulxk(int k)const{vector<Z> b = a; b.insert(b.begin(), k, 0); return b;}
Poly divxk(int k)const{if(size() <= k) return Poly(); return Poly(vector<Z>(a.begin() + k, a.end()));}
friend Poly operator + (const Poly &a, const Poly &b){
vector<Z> ret(max(a.size(), b.size()));
for(int i = 0; i < ret.size(); i++) ret[i] = a[i] + b[i];
return Poly(ret);
}
friend Poly operator - (const Poly &a, const Poly &b){
vector<Z> ret(max(a.size(), b.size()));
for(int i = 0; i < ret.size(); i++) ret[i] = a[i] - b[i];
return Poly(ret);
}
friend Poly operator * (const Poly &a, const Z &b){
vector<Z> ret(a.size());
for(int i = 0; i < ret.size(); i++) ret[i] = a[i] * b;
return Poly(ret);
}
friend Poly operator * (Poly a, Poly b){
if(a.size() == 0 || b.size() == 0) return Poly();
int sz = 1, n = a.size() + b.size() - 1;
while(sz < n) sz <<= 1;
a.resize(sz), b.resize(sz), dft(a.a), dft(b.a);
for(int i = 0; i < sz; i++) a.a[i] = a[i] * b[i];
idft(a.a), a.resize(n); return a;
}
Poly inv(int deg)const{
if(deg == 1) return Poly({a[0].pow(P - 2)});
Poly res = inv((deg + 1) >> 1), tmp = *this;
int sz = 1; while(sz < (deg << 1)) sz <<= 1;
tmp = tmp.modxk(deg), tmp.resize(sz), res.resize(sz);
dft(tmp.a), dft(res.a);
for(int i = 0; i < sz; i++) res[i] = 2 * res[i] - res[i] * res[i] * tmp[i];
idft(res.a), res.resize(deg);
return res;
}
Poly derivative()const{
if(size() == 1) return Poly();
Poly ret(vector<Z>(size() - 1));
for(int i = 1; i < size(); i++) ret.a[i - 1] = a[i] * i;
return ret;
}
Poly integrate()const{
Poly ret(vector<Z>(size() + 1));
for(int i = 1; i < ret.size(); i++) ret.a[i] = a[i - 1] * Z(i).inv();
return ret;
}
Poly ln(int deg){
Poly res = derivative(), tmp = inv(deg);
res = (res * tmp).integrate(), res.resize(deg);
return res;
}
Poly exp(int deg){
Poly ret(vector<Z>(1)); ret[0] = 1; int i = 1;
while(i < deg) i <<= 1, ret = (ret * (Poly({1}) - ret.ln(i) + modxk(i))).modxk(i);
return ret.modxk(deg);
}
};
}
using namespace Poly_space;
Z power(Z p,ll k){
Z ans = 1;
while(k){
if(k % 2 == 1) ans *= p;
p *= p;
k /= 2;
}
return ans;
}
int n,B;
char s[200005];
Z cof[450][200005],fact[200005],ifac[200005],inv[200005],PP[200005];
Z dp[450][200005],ddp[450][200005];
Z C(int N,int M){
if(N < M || M < 0) return 0;
return fact[N] * ifac[M] * ifac[N - M];
}
Z calc(int m,int prev,int cur){
if(!m) return cur + 1;
assert(cur >= prev);
Z answer = (cur + 1) * PP[m],temp;
rep(k,cur,B){
answer += PP[m] - cof[k][m];
if(m >= k - prev + 1) answer += cof[k][m - (k - prev + 1)];
}
rep(j,1,n / (B + 2)){
int mm = m - j * (2 + max(B + 1,cur));
if(mm < 0) break;
if(j % 2) answer += dp[j][m];
else answer -= dp[j][m];
}
rep(j,0,n / (B + 2)){
int mm = m + prev - 1 - (j + 1) * max(B + 1,cur) - 2 * j;
if(mm < 0) break;
if(j % 2) answer -= ddp[j][mm];
else answer += ddp[j][mm];
}
return answer;
}
Z solver(int cur,int prev){
Z answer = 0;
rep(i,2,n){
if(s[i - 1] == '1'){
int p = i;
while(p <= n && s[p] == '0') p++;
if(p <= n) answer += calc(n - p,prev + p - i + 1,max(prev + p - i + 1,cur));
}
if(s[i] == '0'){
prev++;
cur = max(cur,prev);
}else{
prev = 0;
}
}
return answer;
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
scanf("%s",s + 1);
n = strlen(s + 1);
B = sqrt(n);
fact[0] = 1;
rep(i,1,n) fact[i] = fact[i - 1] * i;
ifac[n] = Z(1) / fact[n];
per(i,n - 1,0) ifac[i] = ifac[i + 1] * (i + 1);
rep(i,1,n) inv[i] = ifac[i] * fact[i - 1];
PP[0] = 1;
rep(i,1,n) PP[i] = PP[i - 1] + PP[i - 1];
rep(k,0,B){
cof[k][0] = 1;
rep(i,0,n - 1){
cof[k][i + 1] = 2 * (1 + i) * cof[k][i];
if(i >= k + 1) cof[k][i + 1] -= (i + 1) * cof[k][i - k - 1];
cof[k][i + 1] *= inv[i + 1];
}
// rep(i,1,n) printf("%d ",cof[k][i].val());
// printf("\n");
}
rep(j,0,n / (B + 2)){
rep(i,0,n) dp[j][i] = C(i + j,i) * PP[i];
rep(i,j,n) dp[j][i] += dp[j][i - j];
rep(i,0,n) ddp[j][i] = C(i + j,i) * PP[i];
rep(i,j + 1,n) ddp[j][i] += ddp[j][i - j - 1];
}
// calc(n - 1,0,0);
Z answer = calc(n - 1,0,0) + solver(1,1);
printf("%d\n",answer.val());
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Memory Limit Exceeded
Test #1:
score: 0
Memory Limit Exceeded
input:
110
output:
15
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Memory Limit Exceeded
Test #7:
score: 0
Memory Limit Exceeded
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
339821850
result:
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Skipped
Dependency #4:
0%
Subtask #7:
score: 0
Skipped
Dependency #5:
0%
Subtask #8:
score: 0
Skipped
Dependency #6:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
0%