题目链接

ZOJ3899

题解

比较累,做一道水题

还被卡常= =

我在\(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】的更多相关文章

  1. [ZOJ3899]State Reversing

    [ZOJ3899]State Reversing 试题描述 Yakumo Yukari is with no doubt one of the most powerful youkai in Gens ...

  2. 【ZOJ3899】State Reversing 解题报告

    [ZOJ3899]State Reversing Description 有\(N\)个不同的怪兽,编号从\(1\) 到\(N\).Yukari有\(M\)个相同的房间,编号为\(1\)到\(M\). ...

  3. 【XSY3306】alpha - 线段树+分治NTT

    题目来源:noi2019模拟测试赛(一) 题意: 题解: 这场三道神仙概率期望题……orzzzy 这题暴力$O(n^2)$有30分,但貌似比正解更难想……(其实正解挺好想的) 注意到一次操作实际上就是 ...

  4. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  5. Codeforces - 1199D - Welfare State - 单调栈 / 线段树

    https://codeforc.es/contest/1199/problem/D 其实后来想了一下貌似是个线段树的傻逼题. 单调栈是这样思考的,每次单点修改打上一个最终修改的时间戳.每次全体修改就 ...

  6. 【线段树区间合并】HDU1540-Tunnel Warfare

    一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  7. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  8. 【HDU4419 Colourful Rectangle】 线段树面积并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...

  9. JuQueen(线段树 lazy)

    JuQueen Time Limit: 5 Sec  Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...

随机推荐

  1. vbox虚拟机扩容(CentOS 7.2)

    Preface   My virtual machine was simply created by vagrant in default mode without anything about th ...

  2. Google Chrome插件分享

    前言 浏览器是大家日常使用最多的工具之一,对于程序员来说,Google Chrome浏览器当然是大家优选的最爱之一.面对Chrome丰富的插件真的是爱不释手,如何把自己的Chrome调教成自己心仪的样 ...

  3. DX孟虎点评新兴市场:巴西俄罗斯火爆背后

    [亿邦动力网讯]4月3日消息,在第九届中国中小企业电子商务大会暨2014中国(河南)跨境贸易电子商务峰会上,DX公司CEO孟虎对新兴市场做了详细的分析,指出在当今的跨境电商环境下,北美.西欧作为电商成 ...

  4. Python模块random使用详情

    python常用模块目录 1.random.random()#用于生成一个0到1的随机浮点数:0<= n < 1.0 import random mcw = random.random() ...

  5. redis rdb aof比较

    Redis中数据存储模式有2种:cache-only,persistence; cache-only即只做为“缓存”服务,不持久数据,数据在服务终止后将消失,此模式下也将不存在“数据恢复”的手段,是一 ...

  6. php作用域限定符

    双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别.::左边表示的是作用域,右边表示的是访问的成员. 系统定义了两个作用域,self和parent.self表示当前类的作用域,在类之外的 ...

  7. sprint3最终演示及团队贡献分

    团队名:在考虑 团队项目:复利计算 项目演示: 之前的功能都有演示过就不再一一截图,把我们新增加的功能说一下 首先用户进入我们的网页可以登录或者注册,注册的用户可以直接输入用户名及密码登录,没有注册的 ...

  8. "助成"招聘网站视频简介

    我们小组为我们的作品录制了一个一分多钟的电梯介绍视频,这是视频连接,我上传到了优酷上:http://v.youku.com/v_show/id_XMzIzMTc1ODc2NA==.html?spm=a ...

  9. mysql---时间类型详解

    mysql 日期类型 mysql 日期类型    ·  DATE (适用于"出生日期"等只需要年月日数据的日期字段) 日期.支持的范围为'1000-01-01'到'9999-12- ...

  10. equals()和hashcode()详解

    转载自http://www.cnblogs.com/Qian123/p/5703507.html java.lang.Object类中有两个非常重要的方法:   public boolean equa ...