\(\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(i,j)=j^i-\sum_{k=2}^i\sum_{t=1}^j(t^{k-1}-(t-1)^{k-1})(f(i-k+1,j-t+1)-f(i-k+1,j-t)).
\]

合理处理系数的指标和转移状态的下标,可以利用前缀和将求 \(f\) 的过程优化至 \(\mathcal O(n^3)\)。

  如刚才透露的一般,我们在目标状态 \(f(n,1..n)\) 的基础下求“有 \(x\) 个不同取值”。令 \(g(i)\) 表示长度为 \(n\) 的序列中,离散化后满足值域恰为 \([1,i]\) 的合法数量,简单地

\[g(i)=f(n,i)-\sum_{j=1}^{i-1}\binom{i}{j}g(j).
\]

  答案变得显而易见

\[\textit{answer}=\sum_{i=1}^n\binom{m}{i}g_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的更多相关文章

  1. Solution -「Gym 102979E」Expected Distance

    \(\mathcal{Description}\)   Link.   用给定的 \(\{a_{n-1}\},\{c_n\}\) 生成一棵含有 \(n\) 个点的树,其中 \(u\) 连向 \([1, ...

  2. Solution -「Gym 102979L」 Lights On The Road

    \(\mathcal{Description}\)   Link.   给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...

  3. Solution -「Gym 102956F」Find the XOR

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \(m\) 条边的连通无向图 \(G\),边有边权.其中 \(u,v\) 的距离 \(d(u,v)\) ...

  4. Solution -「Gym 102956F」Border Similarity Undertaking

    \(\mathcal{Description}\)   Link.   给定一张 \(n\times m\) 的表格,每个格子上写有一个小写字母.求其中长宽至少为 \(2\),且边界格子上字母相同的矩 ...

  5. Solution -「Gym 102956A」Belarusian State University

    \(\mathcal{Description}\)   Link.   给定两个不超过 \(2^n-1\) 次的多项式 \(A,B\),对于第 \(i\in[0,n)\) 个二进制位,定义任意一个二元 ...

  6. Solution -「Gym 102798I」Sean the Cuber

    \(\mathcal{Description}\)   Link.   给定两个可还原的二阶魔方,求从其中一个状态拧到另一个状态的最小步数.   数据组数 \(T\le2.5\times10^5\). ...

  7. Solution -「Gym 102798K」Tree Tweaking

    \(\mathcal{Description}\)   Link.   给定排列 \(\{p_n\}\),求任意重排 \(p_{l..r}\) 的元素后,将 \(\{p_n\}\) 依次插入二叉搜索树 ...

  8. Solution -「Gym 102798E」So Many Possibilities...

    \(\mathcal{Description}\)   Link.   给定非负整数序列 \(\{a_n\}\) 和 \(m\),每次随机在 \(\{a\}\) 中取一个非零的 \(a_i\)(保证存 ...

  9. Solution -「Gym 102759I」Query On A Tree 17

    \(\mathcal{Description}\)   Link.   给定一棵含 \(n\) 个结点的树,结点 \(1\) 为根,点 \(u\) 初始有点权 \(a_u=0\),维护 \(q\) 次 ...

随机推荐

  1. bind 标签

    <select id="finduserbylikename"  parameterType="string"  resultMap="cour ...

  2. spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403

    1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...

  3. 新增访客数量MR统计之NewInstallUserMapper相关准备

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...

  4. Java安全之Spring内存马

    Java安全之Spring内存马 基础知识 Bean bean 是 Spring 框架的一个核心概念,它是构成应用程序的主干,并且是由 Spring IoC 容器负责实例化.配置.组装和管理的对象. ...

  5. 安装DataX的管理控制台(转)

    原文地址 https://github.com/WeiYe-Jing/datax-web/blob/master/doc/datax-web/datax-web-deploy.md 环境准备 1)基础 ...

  6. 猫与ThinkPad

    高中时候看见过家里橘猫谁在舅舅的ThinkPad笔记本了,可惜没拍下来,我也不喜欢那只猫,更喜欢幼时的白猫和黑白猫. ThinkPad宣传图片诚不欺我. 怀念青春与当年陪我游戏的IBM的ThinkPa ...

  7. Cesium中级教程9 - Advanced Particle System Effects 高级粒子系统效应

    Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ 要了解粒子系统的基础知识,请参见粒子系统入门教程. Weathe ...

  8. 什么是VPC

    1 什么是私有网络(VPC) 私有网络是一块可用户自定义的网络空间,您可以在私有网络内部署云主机.负载均衡.数据库.Nosql快存储等云服务资源.您可自由划分网段.制定路由策略.私有网络可以配置公网网 ...

  9. Kubernetes集群PV和PVC详解

    Kubernetes集群高级存储资源PV及PVC 文章目录 Kubernetes集群高级存储资源PV及PVC 1.高级存储PV和PVC概念部分 2.PV和PVC资源的生命周期 3.PV资源介绍与案例配 ...

  10. Linux无写权限打zip

    opt下tiger.txt没权限得时候可以这样直接用zip打包 zip /tmp/1.zip /opt/tiger.txt