CF1153F Serval and Bonus Problem FFT
CF1153F Serval and Bonus Problem
官方的解法是\(O(n ^ 2)\)的,这里给出一个\(O(n \log n)\)的做法。
首先对于长度为\(l\)的线段,显然它的答案就是长度为\(1\)的线段的答案\(\times l\),这样做只是为了方便计算。
考虑对于数轴上区间\([0,1]\)内任意一个点\(x\),它被一条随机线段覆盖的概率是多少:线段的两个端点都在它左边的概率是\(x ^ 2\)、都在它右边的概率是\((1 - x) ^ 2\),那么它被覆盖的概率即为\(p(x) = 1 - x^2 - (1 - x) ^ 2 = 2 x (1 - x)\)。
那么他被\(\ge k\)条线段覆盖的概率为\(f(x) = \sum \limits _{i = k} ^ n \binom{n}{i} p(x) ^ i (1 - p(x)) ^ {n - i}\)。
根据定积分的定义就得到区间\([0,1]\)内被\(\ge k\)条线段覆盖的长度期望为\(\int _{0} ^{1} f(x) \mathrm{d} x\)。
现在我们的问题就是怎么算这个东西了:
\]
推到这里,就可以把积分去掉了,这是一个Beta Function的形式:记结论吧
\]
代入得:
\]
令\(t = i + j\),\(f[i] = \frac {1} {i}\),\(g[j] = \frac {(-1) ^ j} {j !}\),\(h[t] = \frac{2^t (t !) ^ 2} {(2 t + 1) ! (n - t) !}\),考虑枚举\(t\):
\]
这后面就是一个非常显然的卷积式子了,直接FFT即可。
答案记得\(\times l\)。
//written by newbiechd
#include <cstdio>
#include <cctype>
#include <algorithm>
#define BUF 1000000
using namespace std;
const int N = 100003, yyb = 998244353, Gg = 3, Gi = 332748118;
char buf[BUF], *p1, *p2;
inline char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF, stdin), p1 == p2) ? EOF : *p1++; }
inline int rd() {
register int f = 0;
register char c;
while (!isdigit(c = gc())) {}
do
f = f * 10 + (c ^ 48);
while (isdigit(c = gc()));
return f;
}
int rev[N], G[2][N];
inline int power(int x, int y) {
register int o = 1;
for (; y; y >>= 1, x = 1ll * x * x % yyb)
if (y & 1)
o = 1ll * o * x % yyb;
return o;
}
inline void ntt(int *f, int len, int opt) {
register int i, j, k, x, y, p, q;
for (i = 1; i < len; ++i)
if (i < rev[i])
swap(f[i], f[rev[i]]);
for (i = 1; i < len; i <<= 1) {
p = G[opt][i];
for (j = 0; j < len; j += i << 1)
for (k = 0, q = 1; k < i; ++k, q = 1ll * p * q % yyb)
x = f[j | k], y = 1ll * q * f[i | j | k] % yyb, f[j | k] = (x + y) % yyb, f[i | j | k] = (x - y) % yyb;
}
}
int fac[N], invFac[N], f[N], g[N], h[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int n = rd(), m = n << 1 | 1, k = rd(), l = rd(), i, len, tmp, ans = 0;
fac[0] = 1;
for (i = 1; i <= m; ++i)
fac[i] = 1ll * fac[i - 1] * i % yyb;
invFac[m] = power(fac[m], yyb - 2);
for (i = m; i; --i)
invFac[i - 1] = 1ll * invFac[i] * i % yyb;
for (i = k; i <= n; ++i)
f[i] = invFac[i];
for (i = 0; i <= n; ++i)
g[i] = i & 1 ? yyb - invFac[i] : invFac[i];
for (len = 1; len <= m; len <<= 1) {}
for (i = 1; i < len; ++i)
rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? len >> 1 : 0);
for (i = 1; i < len; i <<= 1)
G[0][i] = power(Gg, (yyb - 1) / (i << 1)), G[1][i] = power(Gi, (yyb - 1) / (i << 1));
ntt(f, len, 0), ntt(g, len, 0);
for (i = 0; i < len; ++i)
f[i] = 1ll * f[i] * g[i] % yyb;
ntt(f, len, 1), tmp = power(len, yyb - 2);
for (i = 1; i <= n; ++i)
f[i] = 1ll * f[i] * tmp % yyb;
for (i = 1, tmp = 2; i <= n; tmp = (tmp << 1) % yyb, ++i)
h[i] = 1ll * fac[i] * fac[i] % yyb * tmp % yyb * invFac[i << 1 | 1] % yyb * invFac[n - i] % yyb;
for (i = k; i <= n; ++i)
ans = (1ll * f[i] * h[i] % yyb + ans) % yyb;
ans = 1ll * ans * fac[n] % yyb * l % yyb, printf("%d\n", (ans + yyb) % yyb);
return 0;
}
CF1153F Serval and Bonus Problem FFT的更多相关文章
- CF1153F Serval and Bonus Problem
Serval and Bonus Problem 1.转化为l=1,最后乘上l 2.对于一个方案,就是随便选择一个点,选在合法区间内的概率 3.对于本质相同的所有方案考虑在一起,贡献就是合法区间个数/ ...
- CF1153F Serval and Bonus Problem 【期望】
题目链接:洛谷 作为一只沉迷数学多年的蒟蒻OIer,在推柿子和dp之间肯定要选推柿子的! 首先假设线段长度为1,最后答案乘上$l$即可. 对于$x$这个位置,被区间覆盖的概率是$2x(1-x)$(线段 ...
- Codeforces Round #551 (Div. 2) F. Serval and Bonus Problem (DP/FFT)
yyb大佬的博客 这线段期望好神啊... 还有O(nlogn)FFTO(nlogn)FFTO(nlogn)FFT的做法 Freopen大佬的博客 本蒟蒻只会O(n2)O(n^2)O(n2) CODE ...
- Codeforces1153F Serval and Bonus Problem 【组合数】
题目分析: 我们思考正好被k个区间覆盖的情况,那么当前这个子段是不是把所有的点分成了两个部分,那么在两个部分之间相互连k条线,再对于剩下的分别连线就很好了?这个东西不难用组合数写出来. 然后我们要证明 ...
- CF1153 F. Serval and Bonus Problem(dp)
题意 一个长为 \(l\) 的线段,每次等概率选择线段上两个点,共选出 \(n\) 条线段,求至少被 \(k\) 条线段覆盖的长度期望. 数据范围 \(1 \le k \le n \le 2000, ...
- Codeforces 1153F Serval and Bonus Problem [积分,期望]
Codeforces 思路 去他的DP,暴力积分多好-- 首先发现\(l\)没有用,所以不管它. 然后考虑期望的线性性,可以知道答案就是 \[ \int_0^1 \left[ \sum_{i=k}^n ...
- @codeforces - 1153F@ Serval and Bonus Problem
目录 @description@ @solution@ @accepted code@ @details@ @description@ 从一条长度为 l 的线段中随机选择 n 条线段,共 2*n 个线 ...
- 2016 acm香港网络赛 A题. A+B Problem (FFT)
原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...
- XJTUOJ wmq的A×B Problem FFT/NTT
wmq的A×B Problem 发布时间: 2017年4月9日 17:06 最后更新: 2017年4月9日 17:07 时间限制: 3000ms 内存限制: 512M 描述 这是一个非常简 ...
随机推荐
- MagicApp说明
title: MagicApp说明 date: 2017-12-06 05:41:00 tags: IT 技术 MagicApp是日常处理的程序,协助进行日常工作处理 批量重命名模块 说明 该模块是根 ...
- 深度理解select、poll和epoll
在linux 没有实现epoll事件驱动机制之前,我们一般选择用select或者poll等IO多路复用的方法来实现并发服务程序.在大数据.高并发.集群等一些名词唱得火热之年代,select和poll的 ...
- 常用Linux 服务器命令--各种性能指标命令
如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个专业的 Linux 系统管理员. 监控命令## iostat### iostat命令用来显示存储系统的 ...
- windows服务器设置文件属性设置去掉隐藏已知文件类型的扩展名(即文件后缀名可见)
摘要: 1.文件后缀名不可见,系统运维过程容易发生同名不同后缀的文件操作混淆的情况 2.windows系统默认是文件后缀名不可见 3.所以需要更改一下配置. 4.操作步骤如下图: (1)点击组织-文件 ...
- 【PAT】B1036 跟奥巴马一起编程(15)(15 分
#include<stdio.h> int main() { int row,col; char c; scanf("%d %c",&col,&c); ...
- ABAP 中JSON格式的转换与解析
RT,JSON是当今十分流行的一种轻量数据格式,广泛地应用于各种数据交换场景中.本文会介绍一种比较简单的将ABAP中的数据转换为JSON格式的方法. (如果你是因为引号的问题搜索到了这篇文章,请直接拉 ...
- 在VUE应用中配置ESLint(代码检查)
eslint配置方式 注释配置:使用js注释来直接嵌入ESLint配置信息到一个文件里 配置文件:使用一个js文件,JSON或者YAML文件来给整个目录和它的子目录指定配置信息.这些配置可以写在一个文 ...
- DP E - Cheapest Palindrome
Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate ...
- HDFS的namenode从单节点扩展为HA需要注意的问题
扩展为HA需要注意的问题 原Namenode称为namenode1,新增的Namenode称为namenode2. 从namenode单节点扩展为HA模式官网上有详细的教程,下面是扩展过程中疏忽的地方 ...
- activemq5.14+zookeeper3.4.9实现高可用
一.activeMQ主要的几类部署方式比较1.默认的单机部署(kahadb)activeMQ的默认存储的单机方式,以本地kahadb文件的方式存储,所以性能指标完全依赖本地磁盘IO,不能提供高可用. ...