QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#729095 | #9614. 分治 | chenxinyang2006 | 65 | 588ms | 17768kb | C++20 | 7.8kb | 2024-11-09 16:29:09 | 2024-11-09 16:29:10 |
Judging History
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][2005],fact[200005],ifac[200005],inv[200005],PP[200005];
Z dp[450][2005],ddp[450][2005];
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][mm];
else answer -= dp[j][mm];
}
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: 10
Accepted
Test #1:
score: 10
Accepted
time: 0ms
memory: 17568kb
input:
110
output:
15
result:
ok 1 number(s): "15"
Test #2:
score: 10
Accepted
time: 0ms
memory: 17708kb
input:
101
output:
12
result:
ok 1 number(s): "12"
Subtask #2:
score: 10
Accepted
Dependency #1:
100%
Accepted
Test #3:
score: 10
Accepted
time: 0ms
memory: 17568kb
input:
111110
output:
198
result:
ok 1 number(s): "198"
Test #4:
score: 10
Accepted
time: 0ms
memory: 17768kb
input:
1001001
output:
253
result:
ok 1 number(s): "253"
Subtask #3:
score: 20
Accepted
Dependency #2:
100%
Accepted
Test #5:
score: 20
Accepted
time: 0ms
memory: 17572kb
input:
10100011000100111
output:
386882
result:
ok 1 number(s): "386882"
Test #6:
score: 20
Accepted
time: 3ms
memory: 17520kb
input:
111010011111010110
output:
1107742
result:
ok 1 number(s): "1107742"
Subtask #4:
score: 5
Accepted
Test #7:
score: 5
Accepted
time: 3ms
memory: 17696kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
412796008
result:
ok 1 number(s): "412796008"
Test #8:
score: 5
Accepted
time: 3ms
memory: 17516kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
818656648
result:
ok 1 number(s): "818656648"
Subtask #5:
score: 5
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #9:
score: 5
Accepted
time: 0ms
memory: 17500kb
input:
10000000100000010010011110111101101110000000000001100000011000111111010011010101010000101001110110010001100110000110111101000101001111101111001010001001011101011111010000100010111100110000001101111
output:
703266161
result:
ok 1 number(s): "703266161"
Test #10:
score: 5
Accepted
time: 3ms
memory: 17380kb
input:
110100000100001000101000010010101000110111101010110000101001001100100111000011100101110110010000001111010011101001111110110010001110011101001111010101100100010011101010101111111111010110001100100110
output:
330527406
result:
ok 1 number(s): "330527406"
Subtask #6:
score: 5
Accepted
Dependency #4:
100%
Accepted
Test #11:
score: 5
Accepted
time: 0ms
memory: 17400kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
340672883
result:
ok 1 number(s): "340672883"
Test #12:
score: 5
Accepted
time: 0ms
memory: 17496kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
555946758
result:
ok 1 number(s): "555946758"
Subtask #7:
score: 10
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Test #13:
score: 10
Accepted
time: 0ms
memory: 17576kb
input:
110011100110101000000110101010111111001101101011010110100100110010111110110110000111011001110000101111110111011111000110001011011011101100001100100011010010111111010110010000101001001000100001100100000001000111110100000101001011100001100011011110110101101111110011100111001010001010001111001110111100...
output:
324123594
result:
ok 1 number(s): "324123594"
Test #14:
score: 10
Accepted
time: 4ms
memory: 17496kb
input:
110100110100110110001011100000011010000010000101100100001101100100110000101000111001111100001110001001101010110010111101000100111010001011001110101010001101111010000011000010110011000011100101110100000001011100111000101111010100001101011010100101110000010001101001000100111001101101110000101101011011...
output:
209285599
result:
ok 1 number(s): "209285599"
Subtask #8:
score: 0
Wrong Answer
Dependency #6:
100%
Accepted
Test #15:
score: 0
Wrong Answer
time: 588ms
memory: 17604kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
272409264
result:
wrong answer 1st numbers differ - expected: '468567454', found: '272409264'
Subtask #9:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Dependency #7:
100%
Accepted
Dependency #8:
0%