题目链接

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. 【linux报错】-bash: xhost: command not found

    参考自:http://blog.csdn.net/csdnones/article/details/51513163,感谢原作者解决了我的问题. 执行xhost +,报以下错误,原因是因未没有安装相关 ...

  2. youtube高清视频下载方法

    youtube下载方法有多种, 但都不支持1080P以上的高清下载, 今天找到一种支持1080P的, 记录一下 步骤1: 百度搜: Dooseen tubedown 下载该软件, 并安装, 一直下一步 ...

  3. linux命令提示符设置

    变成绿色 [root@localhost /usr/local]#vim /root/.bashrc # .bashrc # User specific aliases and functions a ...

  4. Phaser3 场景Scene之间的传值 -- HTML JAVASCRIPT 网页游戏开发

      PHASERJS3 一.首先当然得有至少有二个场景sceneA.js,sceneB.js 二.从场景A传值到场景B二种方法 1)通过事件this.events.emit('event key',{ ...

  5. JavaScript指定断点操作

    什么是断点操作(Breakpoint action) 做前端开发的小伙伴,或许对这个断点操作不是很熟悉.不过你要是问其他语言(比如C,C++ ,C #等)的开发者,他们应该都挺熟悉断点操作,这种断点操 ...

  6. python-property、__get__、__set__

    目录 property __set__ 和 __get__ property property装饰器的应用来自这样一个问题:如果对实例的属性值不加以限制,那么实例的属性值会出现明显不合理的情况,为了解 ...

  7. 从零开始的Python爬虫速成指南

    序 本文主要内容:以最短的时间写一个最简单的爬虫,可以抓取论坛的帖子标题和帖子内容. 本文受众:没写过爬虫的萌新. 入门 0.准备工作 需要准备的东西: Python.scrapy.一个IDE或者随便 ...

  8. CF刷刷水题找自信1

    CF 1108A Two distinct points 题目意思:给你两个线段的起点和终点,让你给出两个不同的点,这两点分别处于两个不同的线段之中.解题思路:题目说如果存在多种可能的点,随意一组答案 ...

  9. nginx配置,php安装

    yum -y install libxml2 libxml2-develyum -y install libxslt-devel yum -y install bzip2-devel yum -y i ...

  10. Java:有关try、catch和finally的学习(供自己参考)

    Java:有关try.catch和finally的学习 在看到书本的时候对finally的介绍是:不论是否在try块中产生异常,都会执行finally.当时对这句话的理解不够深,误以为在try...c ...