[Codeforces 1251F]Red-White Fence
Description
给你 \(n\) 块白木板,\(k\) 块红木板,分别有各自的长度 \(h_i\)。让你用这些木板组成一段围栏,要满足:
- 只用一块红木板,且所有白木板的长度均严格小于红木板长度;
- 红木板左边的白木板长度严格单调递增;
- 红木板右边的白木板长度严格单调递减
现在给出 \(q\) 组询问,问周长为 \(x_i\) 的围栏有多少种。
\(1\leq n,h_i,q\leq 3\cdot 10^5,4\leq x_i\leq 12\cdot 10^5,1\leq k\leq 5\)
Solution
如果我们选的红木板长度为 \(L\),并且这段围栏由 \(m\) 块板子构成,显然周长为 \(2\times (L+m)\)。
那么问题就转化为了,求用 \(m-1\) 块长度 \(<L\) 的白木板形成两段长度严格单调递增序列的方案数。
因为木板长度是离散的,我们可以考虑每种长度的木板的放法。
我们把所有长度 \(<L\) 的白木板取出。若某种长度的木板只有一块。那么显然,这块木板可以放在红木板的左边或右边(即任意一个序列中)。
对于某种长度有两块以上的时候,我们可以把他放在左边、右边或者两边都放。并且我们最多只会用 \(2\) 块这样的木板,所以多余的可以除去。
假设第一种情况(该长度的木板只有一块)下的木板个数为 \(sa\)。显然用这 \(sa\) 块木板构成两段序列总长度为 \(i\) 的方案数 \(a_i={sa \choose i}\times 2^i\)。
假设第二种情况下的木板个数为 \(sb\)(除去多余的木板)。用这 \(sb\) 块木板构成两段序列总长度为 \(i\) 的的方案数 \(b_i={sb \choose i}\)。
记两种情况长度总和为 \(i\) 的方案数为 \(c_i\),那么容易发现我们要求的就是
\[
c_{m-1}=\sum_{i=0}^{m-1}a_ib_{m-1-i}
\]
这是一个卷积式,那么我们设
\[
\begin{aligned}
A(x)&=\sum_i a_i x^i\\
B(x)&=\sum_i b_i x^i\\
C(x)&=A(x)\otimes B(x)\\
&=\sum_i c_i x^i
\end{aligned}
\]
那么我们就可以用 \(\text{NTT}\) 来求出选该红木板时,对应选不同白木板个数的方案数了。
因此我们可以枚举每个红木板,做一次 \(\text{NTT}\) 累计到答案中,\(O(1)\) 回答询问。
总复杂度 \(O(k\times n\log n +q)\)。
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 12e5+5, yzh = 998244353;
int n, k, q, cnt[N], x, ans[N], fac[N], ifac[N];
int A[N], B[N], a, b, L, R[N];
int quick_pow(int a, int b) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%yzh;
b >>= 1, a = 1ll*a*a%yzh;
}
return ans;
}
int C(int n, int m) {return 1ll*fac[n]*ifac[m]%yzh*ifac[n-m]%yzh; }
void NTT(int *A, int o) {
for (int i = 0; i < n; i++) if (i < R[i]) swap(A[i], A[R[i]]);
for (int i = 1; i < n; i <<= 1) {
int gn = quick_pow(3, (yzh-1)/(i<<1)), x, y;
if (o == -1) gn = quick_pow(gn, yzh-2);
for (int j = 0; j < n; j += (i<<1)) {
int g = 1;
for (int k = 0; k < i; k++, g = 1ll*g*gn%yzh) {
x = A[j+k], y = 1ll*g*A[j+k+i]%yzh;
A[j+k] = (x+y)%yzh;
A[j+k+i] = (x-y)%yzh;
}
}
}
}
int main() {
scanf("%d%d", &n, &k);
fac[0] = ifac[0] = ifac[1] = 1;
for (int i = 2; i <= n; i++) ifac[i] = -1ll*yzh/i*ifac[yzh%i]%yzh;
for (int i = 1; i <= n; i++)
fac[i] = 1ll*i*fac[i-1]%yzh,
ifac[i] = 1ll*ifac[i-1]*ifac[i]%yzh;
for (int i = 1; i <= n; i++) scanf("%d", &x), cnt[x]++;
while (k--) {
scanf("%d", &x); a = b = 0;
for (int i = 1; i < x; i++)
if (cnt[i] >= 2) a += 2;
else if (cnt[i] == 1) b++;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
for (int i = 0; i <= a; i++) A[i] = C(a, i);
for (int i = 0; i <= b; i++) B[i] = 1ll*C(b, i)*quick_pow(2, i)%yzh;
a += b; L = 0;
for (n = 1; n <= a; n <<= 1) ++L;
for (int i = 0; i < n; i++) R[i] = (R[i>>1]>>1)|((i&1)<<(L-1));
NTT(A, 1), NTT(B, 1);
for (int i = 0; i < n; i++) A[i] = 1ll*A[i]*B[i]%yzh;
NTT(A, -1);
int inv = quick_pow(n, yzh-2);
for (int i = 0; i <= a; i++) A[i] = 1ll*A[i]*inv%yzh;
for (int i = 0; i <= a; i++)
(ans[(x+1+i)<<1] += A[i]) %= yzh;
}
scanf("%d", &q);
while (q--) scanf("%d", &x), printf("%d\n", (ans[x]+yzh)%yzh);
return 0;
}
[Codeforces 1251F]Red-White Fence的更多相关文章
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- codeforces 399B. Red and Blue Balls 解题报告
题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
- 【贪心】Codeforces 349B.Color the Fence题解
题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...
- Codeforces 1132C - Painting the Fence - [前缀和优化]
题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...
- Codeforces 349B - Color the Fence
349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...
- Codeforces 448 C. Painting Fence
递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...
- Codeforces D. Color the Fence(贪心)
题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- MYSQL --Subquery returns more than 1 row查询结果多于一行
Subquery returns more than 1 row表示子查询返回了多行数据 例如: select * from table1 where table1.colums=(select co ...
- 《学渣Linux笔记》——关于.bashrc与profile(涉及交互式与非交互式、登录与非登录shell)
<学渣Linux笔记>--关于.bashrc与profile(涉及交互式与非交互式.登录与非登录shell) 1.基本概念(个人理解) 交互式shell:等待用户输入,并执行相应操作的sh ...
- 英语insuraunce保险
中文名:保险 外文名:insurance或insuraunce 类型:保障机制,商业行为 作用:资金融通.损失补偿等 原则:分摊.代位.大数法则等原则 性质:契约经济关系 意义:市场经济条件下风险管理 ...
- Hive 系列(三)—— Hive CLI 和 Beeline 命令行的基本使用
一.Hive CLI 1.1 Help 使用 hive -H 或者 hive --help 命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=va ...
- Java 阿拉伯数字转换为中文大写数字
Java 阿拉伯数字转换为中文大写数字 /** * <html> * <body> * <P> Copyright 1994 JsonInternational&l ...
- win 修改notebook路径
开始发现 notebook 默认的路径是 C:\Users\Administrator 需要修改 将目标中的%USERPROFILE% 直接删掉了
- AVI文件格式
AVI文件采用的是RIFF文件结构方式.波形音频wave,MIDI和数字视频AVI都采用这种格式存储. AVI文件的整体结构如下图所示 构造RIFF文件的基本单元叫做数据块(Chunk),每个数据块包 ...
- activiti用户手册
http://www.mossle.com/docs/activiti/index.html
- Matlab组合模式
组合模式(Composite),将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.组合模式的目的是让客户端不再区分操作的是组合对象(Compos ...
- SQL server 2008数据库的备份与还原(亲测,效果良好)注意采用单用户模式呀
.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studio → 数据库:Dsidea ...