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;
}
ZOJ3899 State Reversing 【线段树 + NTT】的更多相关文章
- [ZOJ3899]State Reversing
[ZOJ3899]State Reversing 试题描述 Yakumo Yukari is with no doubt one of the most powerful youkai in Gens ...
- 【ZOJ3899】State Reversing 解题报告
[ZOJ3899]State Reversing Description 有\(N\)个不同的怪兽,编号从\(1\) 到\(N\).Yukari有\(M\)个相同的房间,编号为\(1\)到\(M\). ...
- 【XSY3306】alpha - 线段树+分治NTT
题目来源:noi2019模拟测试赛(一) 题意: 题解: 这场三道神仙概率期望题……orzzzy 这题暴力$O(n^2)$有30分,但貌似比正解更难想……(其实正解挺好想的) 注意到一次操作实际上就是 ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- Codeforces - 1199D - Welfare State - 单调栈 / 线段树
https://codeforc.es/contest/1199/problem/D 其实后来想了一下貌似是个线段树的傻逼题. 单调栈是这样思考的,每次单点修改打上一个最终修改的时间戳.每次全体修改就 ...
- 【线段树区间合并】HDU1540-Tunnel Warfare
一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- 【HDU4419 Colourful Rectangle】 线段树面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...
- JuQueen(线段树 lazy)
JuQueen Time Limit: 5 Sec Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...
随机推荐
- 【MySQL安装】MySQL5.6在centos6.4上的安装
卸载原来安装的mysql 安装从官网下载的mysql rpm包 发现有依赖,需要先安装libaio包和libnuma包 再装mysql就可以了 安装客户端 安装完成后,启动mysql 但是发现用没有m ...
- Struts 2(四):类型转换
类型转换是Struts 2的一个非常重要的部分,通过类型转换能够将表单参数转换成Java中的各种类型,本文将详细介绍Struts 2的内建类型转换器和自定义类型转换器. 第一节 Struts 2内建类 ...
- 八、EnterpriseFrameWork框架基础功能之自定义报表
本章写关于框架中的“自定义报表”,类似上章“字典管理”也是三部分功能组成,包括配置报表.对报表按角色授权.查看报表:其核心思想就是实现新增一个报表而不用修改程序代码.不用升级,只需要编写一个存储过程, ...
- jvm之GC知识点
GCRoots: 虚拟机栈(栈帧中的局部变量表)引用的对象 方法区中静态属性引用的对象 方法去中常量引用的对象 本地方法栈中JNI(NATIVE方法) ...
- CsvHelper文档-4映射
CsvHelper文档-4映射 类映射 有时候你的类成员和csv的header不一定对应,有时候你的csv文件根本就没有header行,你需要特别制定一个成员的index,你不能依靠.net中默认的顺 ...
- Atom 插件 Sync Settings 备份与恢复
当使用 Atom IDEA.随着使用的越来越多,安装的插件也越来越多,一旦电脑重装后需要复原开发环境,这将是一件比较头疼的事.「Sync Settings」插件可以帮助我们解决这个问题. 操作流程 安 ...
- [转]如何设计自适应屏幕大小的网页 Responsive Web Design
随着3G的普及,越来越多的人使用手机上网. 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小的设备上呈现同样的网页? 手机的屏幕比较小,宽度通 ...
- [leetcode-908-Smallest Range I]
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- 作业要求20181113-4 Beta阶段第1周/共2周 Scrum立会报告+燃尽图 02
作业要求:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 版本控制:[https://git.coding.net/lglr201 ...
- "Hello World"团队召开的第三周第七次会议
今天是我们团队“Hello World!”团队召开的第三周的第七次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时 ...