题意

题目链接

Sol

一条重要的性质:如果某个区间覆盖了另一个区间,那么该区间是没有用的(不会对最大值做出贡献)

首先不难想到枚举最终的答案\(x\)。这时我们需要计算的是最大值恰好为\(x\)的概率。

发现不是很好搞,我们记\(P(x)\)表示最大值\(\leqslant x\)的概率,那么恰好为\(x\)的概率为\(P(x) - P(x - 1)\)

计算概率可以直接用定义:合法的方案/总方案(\(x^n\))

考虑如何计算合法方案:我们直接去枚举在询问区间中有多少个点\(\leqslant x\),设\(g(j)\)表示选出\(j\)个\(\leqslant x\)的点且覆盖了所有询问区间的方案,显然这样可以做到不重不漏。

接下来直接dp计算\(g[j]\),设\(f[i][j]\)表示覆盖了前\(i\)个位置,放了\(j\)个点的方案数,且\(i\)位置必须放的方案数。

\(f[i][j] = \sum_{fr[k] + 1 \leqslant fl[i]} f[k][j - 1]\)

\(fr[i]\)表示覆盖了\(i\)区间的最右区间的编号,\(fl[i]\)表示覆盖了\(i\)的最左区间的编号

转移的时候拿单调栈搞一下

复杂度\(O(n^2 logn)\)

#include<bits/stdc++.h>
#define Pair pair<LL, LL>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2001, mod =666623333, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, X, Q, flag[MAXN], fl[MAXN], fr[MAXN], g[MAXN], cnt, tmp[MAXN], f[MAXN][MAXN], que[MAXN], top, sum[MAXN];
int fp(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a);
a = mul(a, a); p >>= 1;
}
return base;
}
int inv(int x) {
return fp(x, mod - 2);
}
int solve(int x) {//find the probability that max <= x
int now = 0;
for(int i = 1; i <= N; i++) add2(now, mul(g[i], mul(fp(x, i), fp(X - x, N - i))));//choose j point staticfiac
return mul(now, inv(fp(X, N)));
}
Pair q[MAXN];
signed main() {
Fin(a);
N = read(); X = read(); Q = read();
for(int i = 1; i <= Q; i++) q[i].fi = read(), q[i].se = read();
for(int i = 1; i <= Q; i++)
for(int j = 1; j <= Q; j++)
if(!flag[j] && (i != j) && (q[i].fi <= q[j].fi && q[i].se >= q[j].se)) //不加!flag[j]会wa,因为可能左右端点都相同
flag[i] = 1;
for(int i = 1; i <= Q; i++) if(!flag[i]) q[++cnt] = q[i];
Q = cnt;
sort(q + 1, q + Q + 1);
memset(fl, 0x3f, sizeof(fl));
for(int i = 1; i <= Q; i++)
for(int j = 1; j <= N; j++)
if(q[i].fi <= j && q[i].se >= j) chmin(fl[j], i), chmax(fr[j], i);
else if(q[i].se < j) chmax(fr[j], i);
for(int i = 1; i <= N; i++) if(q[fr[i]].se < i) fl[i] = fr[i] + 1; /*
for(int i = 1; i <= N; i++)
for(int j = 1; j <= min(i, N); j++)
for(int k = 0; k < i; k++)
if(fr[k] + 1 >= fl[i]) add2(f[i][j], f[k][j - 1]);
*/
f[0][0] = 1;
int l = 1, r = 1; que[1] = 0; sum[0] = 1;
for(int i = 1; i <= N; i++) {
while(l <= r && fr[que[l]] + 1 < fl[i]) {
for(int j = 0; j <= N; j++)
add2(sum[j], -f[que[l]][j]);
l++;
}
for(int j = 1; j <= min(i, N); j++) f[i][j] = sum[j - 1];
for(int j = 1; j <= N; j++) add2(sum[j], f[i][j]);
que[++r] = i;
}
for(int i = 1; i <= N; i++)
if(fr[i] == Q)
for(int j = 1; j <= N; j++)
add2(g[j], f[i][j]);
LL ans = 0;
for(int i = 1; i <= N; i++) tmp[i] = solve(i);
for(int i = 1; i <= X; i++) add2(ans, mul(i, add(tmp[i], -tmp[i - 1])));
cout << ans;
return 0;
}
/*
32 4
*/

洛谷P3600 随机数生成器(期望dp 组合数)的更多相关文章

  1. 洛谷P3600随机数生成器——期望+DP

    原题链接 写到一半发现写不下去了... 所以orz xyz32768,您去看这篇题解吧,思路很清晰,我之前写的胡言乱语与之差距不啻天渊 #include <algorithm> #incl ...

  2. 洛谷 P3600 - 随机数生成器(期望 dp)

    题面传送门 我竟然独立搞出了这道黑题!incredible! u1s1 这题是我做题时间跨度最大的题之一-- 首先讲下我四个月前想出来的 \(n^2\log n\) 的做法吧. 记 \(f(a)=\m ...

  3. luogu P3600 随机数生成器【dp】

    把期望改成方案数最后除一下,设h[i]为最大值恰好是i的方案数,那么要求的就是Σh[i]*i 首先包含其他区间的区间是没有意义的,用单调栈去掉 然后恰好不好求,就改成h[i]表示最大值最大是i的方案数 ...

  4. [洛谷P5147]随机数生成器

    题目大意:$$f_n=\begin{cases}\frac{\sum\limits_{i=1}^nf_i}n+1&(n>1)\\0&(n=1)\end{cases}$$求$f_n ...

  5. 洛谷P3306 随机数生成器

    题意:给你一个数列,a1 = x,ai = (A * ai-1 + B) % P,求第一个是t的是哪一项,或者永远不会有t. 解:循环节不会超过P.我们使用BSGS的思想,预处理从t开始跳√P步的,插 ...

  6. Luogu P3600 随机数生成器

    Luogu P3600 随机数生成器 题目描述 sol研发了一个神奇的随机数系统,可以自动按照环境噪音生成真·随机数. 现在sol打算生成\(n\)个\([1,x]\)的整数\(a_1...a_n\) ...

  7. 洛谷 P5279 - [ZJOI2019]麻将(dp 套 dp)

    洛谷题面传送门 一道 dp 套 dp 的 immortal tea 首先考虑如何判断一套牌是否已经胡牌了,考虑 \(dp\)​​​​​.我们考虑将所有牌按权值大小从大到小排成一列,那我们设 \(dp_ ...

  8. P3600 随机数生成器

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. 洛谷2344 奶牛抗议(DP+BIT+离散化)

    洛谷2344 奶牛抗议 本题地址:http://www.luogu.org/problem/show?pid=2344 题目背景 Generic Cow Protests, 2011 Feb 题目描述 ...

随机推荐

  1. shell 文件操作

      序   在linux平台下开发,我们经常会接触到一些任务性质的工作,而处理方式多样化.现积累各个案例.   从远程服务器拉取文件到本地服务器   scp work@cp01-xx-dev.com: ...

  2. WPF常用样式总结

    常用控件样式: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation ...

  3. 杂七杂八的JavaScript

    一.input 焦点定位 1.定位input:(this.$refs.searchInput as HTMLInputElement).focus();   2.定位search,根据css选择器: ...

  4. C# 获取Header中的token值

    public CurrentUser currentUser { get { CurrentUser result = new CurrentUser(); //jwt 解密token IJsonSe ...

  5. C# 获取媒体文件播放时长

    引用: Interop.Shell32.dll 方法: /// <summary> /// 获取媒体文件播放时长 /// </summary> /// <param na ...

  6. POJ 2909

    #include<iostream> #include<stdio.h> #define M 35000 #include<math.h> #define N 38 ...

  7. WebView使用_WebView监听网页下载_DownloadManager使用

    最近在做一个较简单的项目:通过一个webview来显示一个网页的App 这个网页有下载的功能,关于这一功能需要用到两个知识点: 1.webview监听网页的下载链接.(webview默认情况下是没有开 ...

  8. postgres 更新数据表

    新增非空列: alter table t_test add column user_id integer; update t_test set user_id=0; alter table t_tes ...

  9. Java工程师学习指南 完结篇

    Java工程师学习指南 完结篇 先声明一点,文章里面不会详细到每一步怎么操作,只会提供大致的思路和方向,给大家以启发,如果真的要一步一步指导操作的话,那至少需要一本书的厚度啦. 因为笔者还只是一名在校 ...

  10. Linux下网络文件系统NFS服务搭建易错点总结

    一.环境准备: 1 [root@czh ~]# cat /etc/redhat-release 2 CentOS release 6.7 (Final) 3 [root@czh ~]# uname - ...