ZOJ3899 State Reversing 【线段树 + NTT】
题目链接
题解
比较累,做一道水题
还被卡常= =
我在\(ZOJ\)交过的两道\(NTT\)都被卡常了。。
哦,题意就是求第二类斯特林数,然后线段树维护一下集合数量就可以了
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define ls (u << 1)
#define rs (u << 1 | 1)
#define res register
using namespace std;
const int maxn = 400005,maxv = 100000,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int sum[maxn],rev[maxn];
inline void upd(int u){sum[u] = sum[ls] + sum[rs];}
inline void pd(int u,int l,int r){
int mid = l + r >> 1;
if (rev[u]){
sum[ls] = (mid - l + 1) - sum[ls]; rev[ls] ^= 1;
sum[rs] = (r - mid) - sum[rs]; rev[rs] ^= 1;
rev[u] = 0;
}
}
void modify(int u,int l,int r,int L,int R){
if (l >= L && r <= R){sum[u] = (r - l + 1) - sum[u]; rev[u] ^= 1; return;}
pd(u,l,r);
int mid = l + r >> 1;
if (mid >= L) modify(ls,l,mid,L,R);
if (mid < R) modify(rs,mid + 1,r,L,R);
upd(u);
}
void build(int u,int l,int r){
rev[u] = 0;
if (l == r){sum[u] = 1; return;}
int mid = l + r >> 1;
build(ls,l,mid);
build(rs,mid + 1,r);
upd(u);
}
const int G = 26,P = 880803841;
int R[maxn];
inline int qpow(int a,int b){
int re = 1;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) re = 1ll * re * a % P;
return re;
}
void NTT(int* a,int n,int f){
for (res int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
for (res int i = 1; i < n; i <<= 1){
int gn = qpow(G,(P - 1) / (i << 1));
for (res int j = 0; j < n; j += (i << 1)){
int g = 1,x,y;
for (res int k = 0; k < i; k++,g = 1ll * g * gn % P){
x = a[j + k],y = 1ll * g * a[j + k + i] % P;
a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
}
}
}
if (f == 1) return;
int nv = qpow(n,P - 2); reverse(a + 1,a + n);
for (res int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int N,M,D,fac[maxn],fv[maxn];
int S[maxn],A[maxn];
void init(){
fac[0] = 1;
for (res int i = 1; i <= maxv; i++)
fac[i] = 1ll * fac[i - 1] * i % P;
fv[maxv] = qpow(fac[maxv],P - 2); fv[0] = 1;
for (res int i = maxv - 1; i; i--)
fv[i] = 1ll * fv[i + 1] * (i + 1) % P;
}
int main(){
init();
int T = read(),l,r;
while (T--){
N = read(); M = read(); D = read();
build(1,1,M);
for (res int i = 0; i <= M; i++){
S[i] = (((i & 1) ? -1 : 1) * fv[i] % P + P) % P;
A[i] = 1ll * qpow(i,N) * fv[i] % P;
}
int n = 1,L = 0;
while (n <= (M << 1)) n <<= 1,L++;
for (res int i = M + 1; i < n; i += 4){
S[i] = A[i] = 0;
S[i + 1] = A[i + 1] = 0;
S[i + 2] = A[i + 2] = 0;
S[i + 3] = A[i + 3] = 0;
}
for (res int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
NTT(S,n,1); NTT(A,n,1);
for (res int i = 0; i < n; i += 4){
S[i] = 1ll * S[i] * A[i] % P;
S[i + 1] = 1ll * S[i + 1] * A[i + 1] % P;
S[i + 2] = 1ll * S[i + 2] * A[i + 2] % P;
S[i + 3] = 1ll * S[i + 3] * A[i + 3] % P;
}
NTT(S,n,-1);
while (D--){
l = read(); r = read();
modify(1,1,M,l,r);
if (sum[1] > N) puts("0");
else printf("%d\n",S[sum[1]]);
}
}
return 0;
}