[ZOJ3899]State Reversing
[ZOJ3899]State Reversing
试题描述
Yakumo Yukari is with no doubt one of the most powerful youkai in Gensokyo. Her ability is manipulating boundaries. She has \(N\) unknown creatures called "sinsack" or "the crime bag" which live in the basement of Yukari's house. Sinsacks are numbered from \(1\) to \(N\). There are \(M\) rooms with infinite capacity in the basement and each room has two states: available or unavailable. Yukari will reverse the state of consecutive rooms every day. Sinsacks can only live in available rooms. And each available room should contain at least one sinsack.
As a 17-year-old girl, recently Yukari is interested in how many different ways to arrange sinsacks in rooms. At the beginning, all rooms are available. In the following \(D\) days, Yukari will ask you the number of ways after reversing. Two ways \(A\) and \(B\) are regarded as the same if for any room in \(A\), there exists a room in \(B\) that the sinsacks in these two rooms have the same set of numbers. In other words, rooms are indistinguishable.
现有 \(N\) 只不同的怪兽,和 \(M\) 个相同的从 \(1\) 到 \(M\) 编号的房间,每个房间有空闲和不空闲两种状态,怪兽只能住进处于空闲状态的房间,房间容量无穷。现在给出 \(D\) 个操作,每次操作翻转一个区间 \([l_i, r_i]\) 内的房间的状态,求每次操作后分配怪兽的方案数(注意房间相同的意思是交换一个分配方案中的两个房间不算产生一个新的方案)。
所有答案均要对 \(880803841\) 取模。
输入
The first line of the input contains an integer \(T\) (\(T \le 10\)), indicating the number of cases. For each test case:
The first line contains three integers \(N\), \(M\) and \(D\) (\(1 \le M \le N\), \(D \le 100000\)). Their meanings are described above.
The following \(D\) lines each contain two integers \(l\) and \(r\) (\(1 \le l \le r \le M\)) denoting the consecutive rooms \(l, l + 1, \cdots , r\) which are reversed by Yukari on that day.
输出
For each query, output the number of different ways to arrange sinsacks. The answer should modulo \(880803841\) for it can be very large.
输入示例
2
3 3 2
2 2
1 3
5 5 3
1 3
2 2
1 5
输出示例
3
1
15
25
15
数据规模及约定
见“输入”
题解
这就是一个裸的第二类斯特林数,然后再强行加一个线段树。
第二类斯特林数 \(S(n, m)\) 表示将 \(n\) 个物品分成 \(m\) 个集合的方案数。如果暴力 \(O(n^2)\) dp 显然有 \(S(n, m) = S(n-1, m-1) + S(n-1, m) \cdot m\),但是要想快速求 \(S(n, 1) \sim S(n, n)\),就用不上这个 dp 了。
考虑 \(S(n, m)\) 的组合意义有
\]
翻译一下就是将 \(n\) 个不同的物品放入 \(k\) 个不同的篮子中,可以有篮子是空的。那么这个东西其实可以先枚举有 \(m\) 个篮子非空,然后分三步完成:从 \(k\) 个篮子中取出 \(m\) 个篮子,\(m\) 个篮子排一个顺序,将 \(n\) 个物品放入 \(m\) 个篮子中,然后乘法原理即可得到上式。
然后二项式反演可得
S(n, k) = \sum_{m=0}^k \frac{(-1)^{k-m}}{(k-m)!} \cdot \frac{m^n}{m!}
\]
于是成了一个卷积的形式。
至于那个区间操作用线段树维护应该没啥好讲的吧。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;
#define rep(i, s, t) for(int i = (s), mi = (t); i <= mi; i++)
#define dwn(i, s, t) for(int i = (s), mi = (t); i >= mi; i--)
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
#define maxn 262144
#define MOD 880803841
#define Groot 26
#define LL long long
int Pow(int a, int b) {
int ans = 1, t = a;
while(b) {
if(b & 1) ans = (LL)ans * t % MOD;
t = (LL)t * t % MOD; b >>= 1;
}
return ans;
}
int brev[maxn];
void FFT(int *a, int len, int tp) {
int n = (1 << len);
rep(i, 0, n - 1) if(i < brev[i]) swap(a[i], a[brev[i]]);
rep(i, 1, len) {
int wn = Pow(Groot, MOD - 1 >> i);
if(tp < 0) wn = Pow(wn, MOD - 2);
for(int j = 0; j < n; j += 1 << i) {
int w = 1;
rep(k, 0, (1 << i >> 1) - 1) {
int la = a[j+k], ra = (LL)w * a[j+k+(1<<i>>1)] % MOD;
a[j+k] = (la + ra) % MOD;
a[j+k+(1<<i>>1)] = (la - ra + MOD) % MOD;
w = (LL)w * wn % MOD;
}
}
}
if(tp < 0) {
int invn = Pow(n, MOD - 2);
rep(i, 0, n - 1) a[i] = (LL)a[i] * invn % MOD;
}
return ;
}
void Mul(int *A, int *B, int n, int m) {
int N = 1, len = 0;
while(N <= n + m) N <<= 1, len++;
rep(i, 0, N - 1) brev[i] = (brev[i>>1] >> 1) | ((i & 1) << len >> 1);
rep(i, n + 1, N - 1) A[i] = 0;
rep(i, m + 1, N - 1) B[i] = 0;
FFT(A, len, 1); FFT(B, len, 1);
rep(i, 0, N - 1) A[i] = (LL)A[i] * B[i] % MOD;
FFT(A, len, -1);
return ;
}
int ava[maxn<<2];
bool tag[maxn<<2];
void build(int o, int l, int r) {
tag[o] = 0;
if(l == r) ava[o] = 1;
else {
int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
build(lc, l, mid); build(rc, mid + 1, r);
ava[o] = ava[lc] + ava[rc];
}
return ;
}
void pushdown(int o, int l, int r) {
if(l == r || !tag[o]) return (void)(tag[o] = 0);
int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
tag[lc] ^= 1; ava[lc] = mid - l + 1 - ava[lc];
tag[rc] ^= 1; ava[rc] = r - mid - ava[rc];
tag[o] = 0;
return ;
}
void rev(int o, int l, int r, int ql, int qr) {
pushdown(o, l, r);
if(ql <= l && r <= qr) tag[o] ^= 1, ava[o] = r - l + 1 - ava[o];
else {
int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
if(ql <= mid) rev(lc, l, mid, ql, qr);
if(qr > mid) rev(rc, mid + 1, r, ql, qr);
ava[o] = ava[lc] + ava[rc];
}
return ;
}
int ifac[maxn], A[maxn], B[maxn];
void work() {
int n = read(), m = read(), q = read();
ifac[1] = 1;
rep(i, 2, n) ifac[i] = (LL)(MOD - MOD / i) * ifac[MOD%i] % MOD;
ifac[0] = 1;
rep(i, 1, n) ifac[i] = (LL)ifac[i-1] * ifac[i] % MOD;
rep(i, 0, n) {
A[i] = (LL)Pow(i, n) * ifac[i] % MOD;
B[i] = ifac[i]; if(i & 1) B[i] = MOD - B[i];
}
Mul(A, B, n, n);
build(1, 1, m);
while(q--) {
int l = read(), r = read();
rev(1, 1, m, l, r);
printf("%d\n", A[ava[1]]);
}
return ;
}
int main() {
int T = read();
while(T--) work();
return 0;
}
[ZOJ3899]State Reversing的更多相关文章
- ZOJ3899 State Reversing 【线段树 + NTT】
题目链接 ZOJ3899 题解 比较累,做一道水题 还被卡常= = 我在\(ZOJ\)交过的两道\(NTT\)都被卡常了.. 哦,题意就是求第二类斯特林数,然后线段树维护一下集合数量就可以了 #inc ...
- 【ZOJ3899】State Reversing 解题报告
[ZOJ3899]State Reversing Description 有\(N\)个不同的怪兽,编号从\(1\) 到\(N\).Yukari有\(M\)个相同的房间,编号为\(1\)到\(M\). ...
- 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...
- react+redux教程(五)异步、单一state树结构、componentWillReceiveProps
今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...
- 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)
说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...
- 2015年软件测试STATE报告
STATE OF TESTING 2015 Report 测试职业的地理位置分配 大部分有5年以上工作经验 大部分是Test Leader 测试工程师角色 测试工程师怎么工作的? 测试中的软件 ...
- React Native props & state
今天又敲了一丁点代码,看了一下props和state的用法 原本以为state只是一个状态,但是又阅读了一下原文,才知道state是一组状态,这些状态是开发者自己定义的,都统一在state这个大类底下 ...
- React Native知识11-Props(属性)与State(状态)
一:Props(属性) 大多数组件在创建时就可以使用各种参数来进行定制.用于定制的这些参数就称为props(属性).props是在父组件中指定,而且一经指定,在被指定的组件的生命周期中则不再改变 通过 ...
- Neural Pathways of Interaction Mediating the Central Control of Autonomic Bodily State 自主神经系统-大脑调节神经通路
Figure above: Critchley H D, Harrison N A. Visceral influences on brain and behavior[J]. Neuron, 201 ...
随机推荐
- 【vlan-给予mac地址认证】
根据项目需求搭建好如下的路由和交换拓扑图: 1:用pc1 ping pc2 使交换机捕捉到4台pc及的 mac地址 查看交换机学习到的mac地址情况 2:配置交换机和pc机之间的接口,根pc机的ma ...
- ZooKeeper(3)-内部原理
一. 节点类型 二. Stat结构体 1)czxid-创建节点的事务zxid 每次修改ZooKeeper状态都会收到一个zxid形式的时间戳,也就是ZooKeeper事务ID. 事务ID是ZooKee ...
- Spark 推送数据至 elasticsearch
1.工程依赖 <properties> <spark_version>2.3.1</spark_version> <!-- elasticsearch--&g ...
- hive的load命令
Hive Load语句不会在加载数据的时候做任何转换工作,而是纯粹的把数据文件复制/移动到Hive表对应的地址. 语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVE ...
- ATM购物车程序项目规范(更新到高级版)
ATM购物车程序(高级版) 之前的低级版本已经删除,现在的内容太多,没时间把内容上传,有时间我会把项目源码奉上! 我已经把整个项目源码传到群文件里了,需要的可以加主页qq群号.同时群内也有免费的学习资 ...
- 牛客网暑期ACM多校训练营(第四场) F
参考:http://www.cnblogs.com/Jadon97/p/9383027.html #include <iostream> #include <cstdio> # ...
- [Jmeter]jmeter数据库性能测试配置
学习jmeter过程中,记录一些学习过程中的点点滴滴,用于备忘.本文主要介绍的是如何创建一个简单的测试计划用户测试数据库服务器. 一.添加线程组 二.添加JDBC请求 1.在第一步里面定义并发用户以及 ...
- OpenCV入门:(五:更改图片对比度和亮度)
1. 理论 图片的转换就是将图片中的每个像素点经过一定的变换,得到新像素点,新像素点组合成一张新的图片. 改变图片对比度和亮度的变换如下: 其中α和β被称作增益参数(gain parameter)和偏 ...
- 【廖雪峰老师python教程】——OOP
概述 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 数据封装.继承和多态 ...
- 【廖雪峰老师python教程】——装饰器
装饰器 # 一个函数装饰器的列子 def log(func): def wrapper(*args,**kwargs): print('Name=%s'%func.__name__) return f ...