QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729219 | #9614. 分治 | chenxinyang2006 | 100 ✓ | 2825ms | 12080kb | C++20 | 7.8kb | 2024-11-09 16:43:37 | 2024-11-09 16:43:40 |
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];
int _m[200005],_prev[200005],_cur[200005];
Z cof[200005],fact[200005],ifac[200005],inv[200005],PP[200005];
Z dp[200005],ddp[200005];
Z C(int N,int M){
if(N < M || M < 0) return 0;
return fact[N] * ifac[M] * ifac[N - M];
}
void solver(){
int cur = 1,prev = 1;
rep(i,2,n){
_m[i] = -1;
if(s[i - 1] == '1'){
int p = i;
while(p <= n && s[p] == '0') p++;
if(p <= n){
_m[i] = n - p;
_prev[i] = prev + p - i + 1;
_cur[i] = max(_prev[i],cur);
}
}
if(s[i] == '0'){
prev++;
cur = max(cur,prev);
}else{
prev = 0;
}
}
}
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];
_m[1] = n - 1;
solver();
Z answer = 0;
rep(i,1,n){
if(_m[i] == -1) continue;
answer += (_cur[i] + 1) * PP[_m[i]];
}
rep(k,0,B){
cof[0] = 1;
rep(i,0,n - 1){
cof[i + 1] = 2 * (1 + i) * cof[i];
if(i >= k + 1) cof[i + 1] -= (i + 1) * cof[i - k - 1];
cof[i + 1] *= inv[i + 1];
}
rep(i,1,n){
if(_m[i] == -1 || k < _cur[i]) continue;
answer += PP[_m[i]] - cof[_m[i]];
if(_m[i] >= k - _prev[i] + 1) answer += cof[_m[i] - (k - _prev[i] + 1)];
}
}
// printf("%d\n",answer.val());
rep(j,1,n / (B + 2)){
rep(i,0,n) dp[i] = C(i + j,i) * PP[i];
rep(i,j,n) dp[i] += dp[i - j];
rep(i,1,n){
if(_m[i] == -1) continue;
int mm = _m[i] - j * (2 + max(B + 1,_cur[i]));
if(mm < 0) continue;
if(j % 2) answer += dp[mm];
else answer -= dp[mm];
}
}
rep(j,0,n / (B + 2)){
rep(i,0,n) ddp[i] = C(i + j,i) * PP[i];
rep(i,j + 1,n) ddp[i] += ddp[i - j - 1];
rep(i,1,n){
if(_m[i] == -1) continue;
int mm = _m[i] + _prev[i] - 1 - (j + 1) * max(B + 1,_cur[i]) - 2 * j;
if(mm < 0) continue;
if(j % 2) answer -= ddp[mm];
else answer += ddp[mm];
}
}
printf("%d\n",answer.val());
return 0;
}
详细
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 0ms
memory: 10744kb
input:
110
output:
15
result:
ok 1 number(s): "15"
Test #2:
score: 10
Accepted
time: 0ms
memory: 11432kb
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: 1ms
memory: 11752kb
input:
111110
output:
198
result:
ok 1 number(s): "198"
Test #4:
score: 10
Accepted
time: 1ms
memory: 10940kb
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: 2ms
memory: 11044kb
input:
10100011000100111
output:
386882
result:
ok 1 number(s): "386882"
Test #6:
score: 20
Accepted
time: 1ms
memory: 11228kb
input:
111010011111010110
output:
1107742
result:
ok 1 number(s): "1107742"
Subtask #4:
score: 5
Accepted
Test #7:
score: 5
Accepted
time: 0ms
memory: 9904kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
412796008
result:
ok 1 number(s): "412796008"
Test #8:
score: 5
Accepted
time: 0ms
memory: 11096kb
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: 1ms
memory: 11000kb
input:
10000000100000010010011110111101101110000000000001100000011000111111010011010101010000101001110110010001100110000110111101000101001111101111001010001001011101011111010000100010111100110000001101111
output:
703266161
result:
ok 1 number(s): "703266161"
Test #10:
score: 5
Accepted
time: 1ms
memory: 10896kb
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: 2ms
memory: 10156kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
340672883
result:
ok 1 number(s): "340672883"
Test #12:
score: 5
Accepted
time: 3ms
memory: 10876kb
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: 3ms
memory: 10180kb
input:
110011100110101000000110101010111111001101101011010110100100110010111110110110000111011001110000101111110111011111000110001011011011101100001100100011010010111111010110010000101001001000100001100100000001000111110100000101001011100001100011011110110101101111110011100111001010001010001111001110111100...
output:
324123594
result:
ok 1 number(s): "324123594"
Test #14:
score: 10
Accepted
time: 0ms
memory: 10032kb
input:
110100110100110110001011100000011010000010000101100100001101100100110000101000111001111100001110001001101010110010111101000100111010001011001110101010001101111010000011000010110011000011100101110100000001011100111000101111010100001101011010100101110000010001101001000100111001101101110000101101011011...
output:
209285599
result:
ok 1 number(s): "209285599"
Subtask #8:
score: 10
Accepted
Dependency #6:
100%
Accepted
Test #15:
score: 10
Accepted
time: 781ms
memory: 10628kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
468567454
result:
ok 1 number(s): "468567454"
Test #16:
score: 10
Accepted
time: 1688ms
memory: 11780kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
12752860
result:
ok 1 number(s): "12752860"
Subtask #9:
score: 25
Accepted
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:
100%
Accepted
Test #17:
score: 25
Accepted
time: 2825ms
memory: 12080kb
input:
101100010100101011010110001111101101001010000111001111000100110110010111101100011011011111010110000000011110000010100110111110110001101001101101001110101110011000010100100101000011000010000101011001011011000000100111011110100010000100001101011110100101110000100011000101100000111111100110000111010000...
output:
711712397
result:
ok 1 number(s): "711712397"
Test #18:
score: 25
Accepted
time: 2823ms
memory: 11876kb
input:
110101110100100010101100000110000110101101111100110011100111111110000101111001101001111000110111100111110111010001000010111111110000001001011110101110001011010010010011101000110110000110110101000100111000100110101111011101111101000010000101001001000010011011000011001100111111011000111000010000100111...
output:
171668334
result:
ok 1 number(s): "171668334"
Test #19:
score: 25
Accepted
time: 1795ms
memory: 11824kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
397846555
result:
ok 1 number(s): "397846555"
Test #20:
score: 25
Accepted
time: 1852ms
memory: 11620kb
input:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
592103795
result:
ok 1 number(s): "592103795"
Extra Test:
score: 0
Extra Test Passed