Description

题库链接

给你 \(n\) 块白木板,\(k\) 块红木板,分别有各自的长度 \(h_i\)。让你用这些木板组成一段围栏,要满足:

  1. 只用一块红木板,且所有白木板的长度均严格小于红木板长度;
  2. 红木板左边的白木板长度严格单调递增;
  3. 红木板右边的白木板长度严格单调递减

现在给出 \(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的更多相关文章

  1. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  2. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  3. codeforces 399B. Red and Blue Balls 解题报告

    题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...

  4. codeforces B. Color the Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...

  5. 【贪心】Codeforces 349B.Color the Fence题解

    题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...

  6. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  7. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  8. Codeforces 448 C. Painting Fence

    递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...

  9. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. mysql 多个字段的查询处理

    https://blog.csdn.net/zzzgd_666/article/details/81101548

  2. status 和 typedef

  3. Linux查看CPU信息计算CPU核数量

    1. 物理CPU的个数: cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l 2. 每个物理CPU的核心数量: ...

  4. IDEA远程调试Ambari Server

    1.配置端口 Ambari Server默认配置了服务端的debug参数,端口为5005.如果要修改端口,可以在/usr/sbin/ambari_server_main.py文件中对应地方修改,直接改 ...

  5. 无法定位 Local Database Runtime 安装。请验证 SQL Server Express 是否正确安装以及本地数据库运行时功能是否已启用。

    错误描述: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provide ...

  6. MyBatis和spring整合简单实现

    spring和MyBatis整合: 导入spring和MyBatis的整合jar包,以及其依赖jar包: 导入MyBatis和spring的整合jar包. spring的核心jar包. 定义Mybat ...

  7. docker安装mysql笔记

    首先 查找镜像 docker search mysql 拉取镜像 : docker pull mysql 拉取成功后,查看本地镜像: docker images 可以看到本地有两个镜像(redis是我 ...

  8. sublime中Vue高亮插件安装

    1.准备语法高亮插件vue-syntax-highlight. 下载地址: https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vu ...

  9. JavaWeb 之 JSON

    一.概述 1.概念 JSON:JavaScript Object Notation  JavaScript对象表示法 2.基本格式 var p = {"name":"张三 ...

  10. Flink原理、实战与性能优化读书笔记

    第一章 ApacheFlink介绍 一.Flink优势 1. 目前唯一同时支持高吞吐.低延迟.高性能的分布式流式数据处理框架 2. 支持事件事件概念 3. 支持有状态计算,保持了事件原本产生的时序性, ...