HDU 6143 Killer Names (组合数学+DP)
Description
字母表的长度为\(m\),用表中的字母构造长度为\(2n\)的字符串,要求同一种字母能同时出现在前\(n\)个字符中和后\(n\)个字符中。输出方案数,结果模\(10^9+7\)。
Input
第一行给出用例组数\(T\),每组用例给出两个整数\(n\)和\(m\)。\(1 \leqslant n,m \leqslant 2000\)。
Output
对于每组用例,输出方案数。
Sample Input
2
3 2
2 3
Sample Output
2
18
Solution
字母表中的每中字符有三种情况,在前\(n\)个字符中出现,在后\(n\)个字符中出现和不出现。设有\(p\)中字符在前\(n\)个字符中出现,\(q\)种字符在后\(n\)个字符中出现,则这种分配方案数量为
\[\binom{m}{p,q,m-p-q}=\frac{m!}{p! \cdot q! \cdot (m-p-q)!}\]
现在只需要解决,\(n\)个位置,\(m\)种字符,每种字符至少用一次的方案数。
设\(dp[i][j]\)表示前\(i\)个位置,\(j\)种字符每种至少用一种的方案数。
考虑前\(i-1\)个位置:
- 如果只用了\(j-1\)种字符,那么第\(i\)个位置就必须用最后一种字符,但最后一种字符具体是那个是不确定的,因此有\(j\)种情况。
- 如果j种字符全都出现过,那么第\(i\)个位置放任意一个字符都可以,同样有\(j\)种情况。
综上,
\[dp[i][j]=(dp[i-1][j-1]+dp[i-1][j]) \cdot j\]
最终答案为
\[ans = \sum_{p,q \leqslant n,p+q \leqslant m}{\binom{m}{p,q,m-p-q} \cdot dp[n][p] \cdot dp[n][q]}\]
简单化简整理得
\[ans = m! \cdot \sum_{p,q \leqslant n,p+q \leqslant m}{dp[n][p] \cdot dp[n][q] \cdot inv(p!) \cdot inv(q!) \cdot inv((m-p-q)!)}\]
其中\(inv(i)\)表示\(i\)的逆元。\(O(n^2)\)的时间得到\(dp\)数组,\(O(n)\)的时间预处理\(0\)到\(2000\)的阶乘,再用\(O(n)\)的时间得到\(0\)到\(2000\)阶乘的逆元,枚举每个\(p\)和\(q\),累加即为答案。
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 2e3 + 10;ll power(ll a, ll b, ll mod)
{ll ans = 1;while (b){if (b & 1) ans = ans * a % mod;a = a * a % mod;b >>= 1;}return ans;
}ll dp[N][N], fac[N], ifac[N];void init(int n)
{fac[0] = 1;for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;ifac[0] = 1;for (int i = 1; i <= n; i++) ifac[i] = power(fac[i], mod - 2, mod);for (int i = 1; i <= n; i++) dp[i][1] = 1, dp[i][i] = fac[i];for (int i = 3; i <= n; i++)for (int j = 2; j < i; j++)dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) * j % mod;
}int main()
{init(2000);int T;scanf("%d", &T);while (T--){int n, m;scanf("%d%d", &n, &m);ll ans = 0;for (int p = 1; p <= n && p <= m; p++)for (int q = 1; q <= n && p + q <= m; q++){ll s = dp[n][p] % mod * dp[n][q] % mod;s = s * ifac[p] % mod * ifac[q] % mod * ifac[m - p - q] % mod;ans = (ans + s) % mod;}ans = ans * fac[m] % mod;printf("%lld\n", ans);}return 0;
}
Link
http://acm.hdu.edu.cn/showproblem.php?pid=6143
转载于:https://www.cnblogs.com/dadamrx/p/7384560.html
HDU 6143 Killer Names (组合数学+DP)相关推荐
- HDU 6143 Killer Names(排列+容斥,dp)
Killer Names HDU 6143 (容斥+排列组合,dp+整数快速幂) 2017ACM暑期多校联合训练 - Team 8 1011 Killer Names 题目链接 Time Limit: ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)...
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- HDU 6143 Killer Names【容斥定理】【排列组合】
题目来戳呀 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- HDU 6143 Killer Names(容斥+组合)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- 排列组合,字符串——Killer Names
**题目:**Problem - 6143 http://acm.hdu.edu.cn/showproblem.php?pid=6143 Killer Names Time Limit: 2000/1 ...
- HDU 1231.最大连续子序列-dp+位置标记
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU6143 Killer Names(数论)
Killer Names 传送门1 传送门2 Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith Lo ...
- HDU 6156 Palindrome Function 数位DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6156 题目描述: 求L~R所有的数的l~r进制的f(x), f(x) = 当前进制 如果回文串, f ...
- HDU 4652 Dice:期望dp(成环)【错位相减】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4652 题意: 给你一个有m个面的骰子. 两种询问: (1)"0 m n": &qu ...
最新文章
- Qt安装后配置环境变量(Mac)
- java编译POSTGRESQL_Java连接PostgreSQL数据库(安装环境 + 简易测试代码)
- 多核服务器的JVM优化选项(转载)
- matlab读取fortran文件夹,怎样用fortran获取一个文件夹里的全部文件名?
- jsr250-api_JSON处理的Java API(JSR-353)–流API
- [vue] 写出你知道的表单修饰符和事件修饰符
- P3185 [HNOI2007]分裂游戏
- 中控指纹考勤机软件登录用户名和密码忘记的解决办法
- 人人对战五子棋 C++
- 全网最全数据分析师面试干货-业务逻辑篇
- 邮箱地址采集的10个经典方法
- 修改WMB执行组监听端口
- Golang 原子操作与互斥锁
- 房价会象汽车电脑一样下降
- 计算机网络技术ip地址计算,计算机网络原理-IP地址计算题.doc
- sql server访问Excel97-2003 Excel2007
- 2016 威斯康星 计算机科学,威斯康星麦迪逊大学计算机科学本科申请条件及案例分析...
- RB-tree(红黑树)
- JavaWeb——JSP技术
- PIL gif 图片加文字 python