Solution -「UOJ #450」复读机
\(\mathcal{Description}\)
Link.
求从 \(m\) 种颜色,每种颜色无限多的小球里选 \(n\) 个构成排列,使得每种颜色出现次数为 \(d\) 的倍数的排列方案数,对 \(19491001\) 取模。
\(n\le10^9\),
\(m\le10^3\),\(d=3\);
\(m\le5\times10^5\),\(d\le2\)。
\(\mathcal{Solution}\)
分 \(d=1,2,3\) 求解。
当 \(d=1\),每个位置 \(m\) 种方案,答案为 \(m^n\)。
当 \(d=2\),偶数序列的 EGF 为 \(G(x)=\sum_{i=0}^{+\infty}\frac{x^{2i}}{(2i)!}=\frac{e^x+e^{-x}}2\),那么答案为:
n![x^n]G^m(x)&=n![x^n]\left( \frac{e^x+e^{-x}}2 \right)^m\\
&=\frac{n!}{2^m}[x^n]\left( \sum_{i=0}^m\binom{m}ie^{(2i-m)x} \right)\\
&=2^{-m}\sum_{i=0}^m\binom{m}i(2i-m)^n
\end{aligned}
\]
第二步到第三步用到常见的 \(e^{ax}=\sum_{i=0}^{+\infty}\frac{a^i}{i!}x^i\)。此时就能 \(\mathcal O(m\log n)\) 求出答案了。
当 \(d=3\),\(3\) 的倍数数的 EGF 为 \(G(x)=\sum_{i=0}^{+\infty}[3|i]\frac{x^i}{i!}\),这个不太好算,来一发单位根反演:
G(x)&=\sum_{i=0}^{+\infty}[3|i]\frac{x^i}{i!}\\
&=\frac{1}3\sum_{i=0}^{+\infty}\frac{x^i}{i!}\sum_{j=0}^2\omega_3^{ij}\\
&=\frac{1}3\sum_{j=0}^2\sum_{i=0}^{+\infty}\frac{(\omega_3^jx)^i}{i!}\\
&=\frac{1}3\sum_{j=0}^2e^{\omega_3^jx}
\end{aligned}
\]
接着求答案,暴力展开三项式幂:
n![x^n]G^m(x)&=\frac{n!}{3^m}[x^n]\left( \sum_{j=0}^2e^{\omega_3^jx} \right)^m\\
&=\frac{n!}{3^m}[x^n]\left( \sum_{a+b+c=m}\binom{m}{a,b,c}e^{(a\omega_3^0+b\omega_3^1+c\omega_3^2)x} \right)\\
&=3^{-m}\sum_{a+b+c=m}\binom{m}{a,b,c}(a\omega_3^0+b\omega_3^1+c\omega_3^2)^n
\end{aligned}
\]
注意到 \(c=m-a-b\),所以多重组合数的值就是 \(\frac{m!}{a!b!c!}\),该式能在 \(\mathcal O(m^2\log n)\) 的时间内算出。实际上该式就是 \(d=2\) 的情况的扩展,由于 \(\omega_2^{0,1}=\pm1\),所以亦能从该式推回 \(d=2\) 的情况。
\(\mathcal{Code}\)
/* Clearink */
#include <cstdio>
#define rep( i, l, r ) for ( int i = l, rpbound##i = r; i <= rpbound##i; ++i )
#define per( i, r, l ) for ( int i = r, rpbound##i = l; i >= rpbound##i; --i )
const int MOD = 19491001, MAXM = 5e5, INV2 = MOD + 1 >> 1, INV3 = 12994001;
const int W[] = { 1, 663067, 18827933 };
int n, m, d, fac[MAXM + 5], ifac[MAXM + 5];
inline int mul ( long long a, const int b ) { return a * b % MOD; }
inline int sub ( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
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, m ) fac[i] = mul ( i, fac[i - 1] );
ifac[m] = mpow ( fac[m], MOD - 2 );
per ( i, m - 1, 0 ) ifac[i] = mul ( i + 1, ifac[i + 1] );
}
inline int comb ( const int n, const int m ) {
return n < m ? 0 : mul ( fac[n], mul ( ifac[m], ifac[n - m] ) );
}
int main () {
scanf ( "%d %d %d", &n, &m, &d ), init ();
if ( d == 1 ) return printf ( "%d\n", mpow ( m, n ) ), 0;
if ( d == 2 ) {
int ans = 0;
rep ( i, 0, m ) {
ans = add ( ans, mul ( comb ( m, i ), mpow ( sub ( i << 1, m ), n ) ) );
}
printf ( "%d\n", mul ( ans, mpow ( INV2, m ) ) );
return 0;
}
// $d is now smaller than 1000.
int ans = 0;
rep ( i, 0, m ) rep ( j, 0, m - i ) {
int k = m - i - j;
ans = add ( ans, mul ( mul ( ifac[i], mul ( ifac[j], ifac[k] ) ),
mpow ( add (
mul ( i, W[0] ), add ( mul ( j, W[1] ), mul ( k, W[2] ) ) ), n ) ) );
}
printf ( "%d\n", mul ( ans, mul ( fac[m], mpow ( INV3, m ) ) ) );
return 0;
}
Solution -「UOJ #450」复读机的更多相关文章
- Solution -「UOJ #46」玄学
\(\mathcal{Description}\) Link. 给定序列 \(\{a_n\}\) 和 \(q\) 次操作,操作内容如下: 给出 \(l,r,k,b\),声明一个修改方案,表示 ...
- Solution -「UOJ #87」mx 的仙人掌
\(\mathcal{Description}\) Link. 给出含 \(n\) 个结点 \(m\) 条边的仙人掌图.\(q\) 次询问,每次询问给出一个点集 \(S\),求 \(S\) 内 ...
- UOJ #450「集训队作业2018」复读机
UOJ #450 题意 有$ k$台复读机,每时每刻有且只有一台复读机进行复读 求$ n$时刻后每台复读机的复读次数都是$ d$的倍数的方案数 $ 1\leq d \leq 3,k \leq 5·10 ...
- Solution -「ARC 104E」Random LIS
\(\mathcal{Description}\) Link. 给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...
- Solution -「UNR #5」「UOJ #671」诡异操作
\(\mathcal{Desciprtion}\) Link. 给定序列 \(\{a_n\}\),支持 \(q\) 次操作: 给定 \(l,r,v\),\(\forall i\in[l,r], ...
- Solution -「JOISC 2020」「UOJ #509」迷路的猫
\(\mathcal{Decription}\) Link. 这是一道通信题. 给定一个 \(n\) 个点 \(m\) 条边的连通无向图与两个限制 \(A,B\). 程序 Anthon ...
- Solution -「CERC 2016」「洛谷 P3684」机棚障碍
\(\mathcal{Description}\) Link. 给一个 \(n\times n\) 的网格图,每个点是空格或障碍.\(q\) 次询问,每次给定两个坐标 \((r_1,c_1), ...
- Solution -「UR #21」「UOJ #632」挑战最大团
\(\mathcal{Description}\) Link. 对于简单无向图 \(G=(V,E)\),定义它是"优美"的,当且仅当 \[\forall\{a,b,c,d\ ...
- Solution -「UR #2」「UOJ #32」跳蚤公路
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的带权有向图,每条边还有属性 \(s\in\{-1,0,1\}\).对于每个 \(u ...
随机推荐
- java.exe and -classpth or -cp
mydirname=$(dirname $0) java -cp $classes_dir:$lib_dir/*:$config_dir -Doracle.net.wallet_location=${ ...
- react中异步组件以及withRouter的使用
什么是异步组件?简单来说就是异步加载一个组件,正常情况浏览器加载的是我们打包好的bundle.js文件,那么这个文件是集合了所有js是代码,然而我们首屏加载并不需要一次性加载所有的组件,这会造成性能的 ...
- console.log(a)和console.log(window.a)的区别?
console.log(window.l); //undefined console.log(l); //Uncaught ReferenceError: l is not defined js对于未 ...
- 【小问题】为啥乱搞就不行,golang没安装在系统目录下,导致go get出现"package bytes: directory "/home/ahfu/go/src/bytes" is not using a known version control system"
想在自己的账号下安装golang开发环境,于是这样配置: wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz cd /home/ahfu ...
- JavaCV推流实战(MP4文件)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- golang中将函数当做函数参数使用
package main import ( "fmt" "strings" ) // 使用type关键字让函数变成一个自定义类型 type caseFunc f ...
- IDEA2020.1破解
IDEA2020.1破解 安装 下载idea idea官方下载地址:https://www.jetbrains.com/webstorm/download/other.html 下载破解插件 链接:h ...
- nginx缓冲区参数配置优化
目录 一:nginx缓冲区优化 1.proxy_buffering 2.proxy_buffer_size 3.proxy_buffers 4.proxy_busy_buffers_size 5.pr ...
- linux文件创建删除与基础命令使用
目录 一:linux介绍 二:系统介绍 三:系统命令语法格式 四:系统目录结构 五:系统路径的类型 六:系统运行命令 七:查看系统命令帮助man手册 八:系统快捷方式 九:常用系统命令 十:进行目录创 ...
- 带你十天轻松搞定 Go 微服务系列(五)
序言 我们通过一个系列文章跟大家详细展示一个 go-zero 微服务示例,整个系列分十篇文章,目录结构如下: 环境搭建 服务拆分 用户服务 产品服务 订单服务(本文) 支付服务 RPC 服务 Auth ...