题目链接

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. 手摸手,和你一起学习 UiPath Studio

    学习 RPA 的路上坑比较多,让我们手摸手,一起走…… 以下是一些学习 UiPath 和 RPA 的资源, 拿走不用谢! UiPath Studio 中文文档 机器人流程自动化其实是很好的概念和技术, ...

  2. Jmeter接口测试(三)接口测试实践

    Jmeter 脚本编写一般分五个步骤: 1. 添加线程组 2. 添加 http 请求 3. 在 http 请求中写入接入 url.路径.请求方式和参数 4. 添加查看结果树 5. 调用接口.查看返回值 ...

  3. idea scala 报 with UTF-8 Please try specifying another one using the -encoding option

    现象如下图, 代码里有汉字,执行代码报错,说编码格式不对, 修改方式如上面,将右下角的编码格式修改成 u8即可.

  4. 我想这次我真的理解了 JavaScript 的单线程机制

    今天面试的时候被问到一个问题,是关于 JS 异步的.当时我脑海中闪过了一个单线程的概念,但却没有把真正的原理阐述清楚.所以回来特意重新回顾了前面单线程和异步相关的一些知识点. 虽然之前学习的时候也接触 ...

  5. leetcode27_C++Remove Element

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  6. 复利计算器4.0之再遇JUnit

    复利计算器4.0之再遇JUnit 前言    虽然之前的复利计算器版本已经尝试过使用JUnit单元测试,但由于没有系统性地学习过JUnit的使用,用得并不好,主要问题表现在测试的场景太少,并没有达到测 ...

  7. Alpha发布用户使用报告【欢迎来怼】

    目录 用户统计表 部分用户评论截图 用户统计图 总结 一.用户统计表 目前,博客园安卓版的用户已达到11位.为了采集到更加客观公正的用户评价,并没有将团队内部人员的评价统计进来.同时,为了更好地保护用 ...

  8. OpenCV学习笔记——Mat类型数据存储

    CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number] 比如 CV_8UC3 表示 ...

  9. 缓存-MemoryCache Class

    这是使用MemoryCache缓存的一个例子. private void btnGet_Click(object sender, EventArgs e) { ObjectCache cache = ...

  10. lintcode-129-重哈希

    129-重哈希 哈希表容量的大小在一开始是不确定的.如果哈希表存储的元素太多(如超过容量的十分之一),我们应该将哈希表容量扩大一倍,并将所有的哈希值重新安排.假设你有如下一哈希表: size=3, c ...