Solution -「Gym 102956B」Beautiful Sequence Unraveling
\(\mathcal{Description}\)
Link.
求长度为 \(n\),值域为 \([1,m]\) 的整数序列 \(\lang a_n\rang\) 的个数,满足 \(\not\exist i\in[1,n),~\max_{j=1}^i\{a_j\}=\min_{j=i+1}^n\{a_j\}\),答案对大素数 \(p\) 取模。
\(n\le400\),\(m\le10^8\)。
\(\mathcal{Solution}\)
前几天刚胡了一个 "DP and DP" 的 trick,这不又遇见一道。(
注意到 \(m\) 很大,一副拒 DP 状态于千里之外的样子,尝试先回避它——将某个合法 \(\lang a_n\rang\) 离散化,此时值域不超过 \([1,n]\)。但“离散化”又要求值域中每个位置出现,那我们折中:只要求值域为 \([1,n]\),而不限制每个位置出现。可是为了将值域还原为 \([1,m]\),我们又势必得知“有多少个合法 \(\lang a_n\rang\),其中 \(a\) 有 \(x\) 种不同取值”。
一步步来,先考虑求出 \(f(i,j)\),表示长度为 \(i\) 的序列,值域在 \([1,j]\) 时的合法数量。总数减去不合法数,枚举最后一个不合法位置 \(k-1\),以及此时前缀 \(\max\) 和后缀 \(\min\) 的值 \(t\),可以发现 \(a_{k..n}\) 构成子问题,得到转移
\]
合理处理系数的指标和转移状态的下标,可以利用前缀和将求 \(f\) 的过程优化至 \(\mathcal O(n^3)\)。
如刚才透露的一般,我们在目标状态 \(f(n,1..n)\) 的基础下求“有 \(x\) 个不同取值”。令 \(g(i)\) 表示长度为 \(n\) 的序列中,离散化后满足值域恰为 \([1,i]\) 的合法数量,简单地
\]
答案变得显而易见
\]
其中组合数展开成下降幂暴力算即可。总复杂度 \(\mathcal O(n^3)\)。
\(\mathcal{Code}\)
/*~Rainybunny~*/
#include <cstdio>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
const int MAXN = 400;
int n, m, P, fac[MAXN + 5], ifac[MAXN + 5];
int pwr[MAXN + 5][MAXN + 5], ipwr[MAXN + 5][MAXN + 5];
int f[MAXN + 5][MAXN + 5], g[MAXN + 5];
inline int mul( const int a, const int b ) { return 1ll * a * b % P; }
inline void subeq( int& a, const int b ) { ( a -= b ) < 0 && ( a += P ); }
inline int add( int a, const int b ) { return ( a += b ) < P ? a : a - P; }
inline void addeq( int& a, const int b ) { ( a += b ) >= P && ( a -= P ); }
inline int sub( int a, const int b ) { return ( a -= b ) < 0 ? a + P : a; }
inline int mpow( int a, int b ) {
int ret = 1;
for ( ; b; a = mul( a, a ), b >>= 1 ) ret = mul( ret, b & 1 ? a : 1 );
return ret;
}
inline void init() {
fac[0] = 1;
rep ( i, 1, n ) fac[i] = mul( i, fac[i - 1] );
ifac[n] = mpow( fac[n], P - 2 );
per ( i, n - 1, 0 ) ifac[i] = mul( i + 1, ifac[i + 1] );
rep ( i, 0, n ) {
pwr[i][0] = ipwr[i][0] = 1;
ipwr[i][1] = mpow( pwr[i][1] = i, P - 2 );
rep ( j, 2, n ) {
pwr[i][j] = mul( i, pwr[i][j - 1] );
ipwr[i][j] = mul( ipwr[i][j - 1], ipwr[i][1] );
}
}
}
inline int comb( const int a, const int b ) {
return a < b ? 0 : mul( fac[a], mul( ifac[b], ifac[a - b] ) );
}
int main() {
scanf( "%d %d %d", &n, &m, &P ), init();
rep ( j, 1, n ) {
static int sum[MAXN + 5][2];
rep ( i, 1, n ) sum[i][0] = sum[i][1] = 0;
rep ( i, 1, n ) {
int& cur = f[i][j] = pwr[j][i];
rep ( t, 1, j ) {
subeq( cur, mul( pwr[t][i], sum[t][0] ) );
addeq( cur, mul( pwr[t - 1][i], sum[t][1] ) );
}
rep ( t, 1, j ) {
addeq( sum[t][0], mul( ipwr[t][i],
sub( f[i][j - t + 1], f[i][j - t] ) ) );
addeq( sum[t][1], mul( ipwr[t - 1][i],
sub( f[i][j - t + 1], f[i][j - t] ) ) );
}
}
}
int ans = 0, c = 1;
rep ( i, 1, n ) {
int& cur = g[i] = f[n][i];
rep ( j, 1, i - 1 ) subeq( cur, mul( comb( i, j ), g[j] ) );
c = mul( c, mul( m - i + 1, ipwr[i][1] ) );
addeq( ans, mul( c, g[i] ) );
}
printf( "%d\n", ans );
return 0;
}
Solution -「Gym 102956B」Beautiful Sequence Unraveling的更多相关文章
- Solution -「Gym 102979E」Expected Distance
\(\mathcal{Description}\) Link. 用给定的 \(\{a_{n-1}\},\{c_n\}\) 生成一棵含有 \(n\) 个点的树,其中 \(u\) 连向 \([1, ...
- Solution -「Gym 102979L」 Lights On The Road
\(\mathcal{Description}\) Link. 给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...
- Solution -「Gym 102956F」Find the XOR
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \(m\) 条边的连通无向图 \(G\),边有边权.其中 \(u,v\) 的距离 \(d(u,v)\) ...
- Solution -「Gym 102956F」Border Similarity Undertaking
\(\mathcal{Description}\) Link. 给定一张 \(n\times m\) 的表格,每个格子上写有一个小写字母.求其中长宽至少为 \(2\),且边界格子上字母相同的矩 ...
- Solution -「Gym 102956A」Belarusian State University
\(\mathcal{Description}\) Link. 给定两个不超过 \(2^n-1\) 次的多项式 \(A,B\),对于第 \(i\in[0,n)\) 个二进制位,定义任意一个二元 ...
- Solution -「Gym 102798I」Sean the Cuber
\(\mathcal{Description}\) Link. 给定两个可还原的二阶魔方,求从其中一个状态拧到另一个状态的最小步数. 数据组数 \(T\le2.5\times10^5\). ...
- Solution -「Gym 102798K」Tree Tweaking
\(\mathcal{Description}\) Link. 给定排列 \(\{p_n\}\),求任意重排 \(p_{l..r}\) 的元素后,将 \(\{p_n\}\) 依次插入二叉搜索树 ...
- Solution -「Gym 102798E」So Many Possibilities...
\(\mathcal{Description}\) Link. 给定非负整数序列 \(\{a_n\}\) 和 \(m\),每次随机在 \(\{a\}\) 中取一个非零的 \(a_i\)(保证存 ...
- Solution -「Gym 102759I」Query On A Tree 17
\(\mathcal{Description}\) Link. 给定一棵含 \(n\) 个结点的树,结点 \(1\) 为根,点 \(u\) 初始有点权 \(a_u=0\),维护 \(q\) 次 ...
随机推荐
- linux光标命令快捷键(常用)
Ctrl+a 移动到首行 Ctrl+e 移动到尾行 Ctrl+u 将当前光标后面的内容全部删除 (剪辑) ctrl+k 将当前光标前面的内容全部删除 (剪辑) Ctrl+→(左右同理) 移动到下个空格 ...
- HDU 1106 (1.3.5) 排序 (C语言描述)
排序 Problem Description 输入一行数字,如果我们把这行数字中的'5'都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以'0'开头,这些头部的'0'应该被忽略掉,除非 ...
- Python垃圾回收和Linux Fork
前言 在口袋助理看到了其他部门的同事针对Python2内存占用做的一点优化工作,自己比较感兴趣,遂记录下. Linux fork简介 fork是Linux提供的创建子进程的系统调用.为了优化创建进程速 ...
- RocketMQ 介绍与安装
目录 RocketMQ 介绍 MQ 介绍 MQ 作用 MQ 缺点 MQ 常见产品 RocketMQ 简介 RocketMQ 架构 RocketMQ 安装 RocketMQ 介绍 MQ 介绍 定义: M ...
- 《剑指offer》面试题10- II. 青蛙跳台阶问题
问题描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶.求该青蛙跳上一个 n 级的台阶总共有多少种跳法. 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008, ...
- NPOI导出例子
public static string ExportAOrder(ExportData data) { var cellHeard = new Dictionary<string, strin ...
- 使用redis+lua实现SQL中的select intersect的效果
公众号文章地址 1.需求 业务中需要实现在两个集合中搜索数据,并返回交集. 用SQL的伪代码可以描述如下: select key from set1 where sorted_key between ...
- pytest文档3-测试用例setup和teardown
用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效(不在 ...
- k8s通过Service访问Pod
如何创建服务 1.创建Deployment #启动三个pod,运行httpd镜像,label是run:mcw-httpd,Seveice将会根据这个label挑选PodapiVersion: apps ...
- golang中的标准库http
Go语言内置的net/http包十分的优秀,提供了HTTP客户端和服务端的实现. http客户端 基本的HTTP/HTTPS请求 Get.Head.Post和PostForm函数发出HTTP/HTTP ...