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\)个位置:

  1. 如果只用了\(j-1\)种字符,那么第\(i\)个位置就必须用最后一种字符,但最后一种字符具体是那个是不确定的,因此有\(j\)种情况。
  2. 如果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)相关推荐

  1. HDU 6143 Killer Names(排列+容斥,dp)

    Killer Names HDU 6143 (容斥+排列组合,dp+整数快速幂) 2017ACM暑期多校联合训练 - Team 8 1011 Killer Names 题目链接 Time Limit: ...

  2. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)...

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  3. HDU 6143 Killer Names【容斥定理】【排列组合】

    题目来戳呀 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  4. HDU 6143 Killer Names(容斥+组合)

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

  5. 排列组合,字符串——Killer Names

    **题目:**Problem - 6143 http://acm.hdu.edu.cn/showproblem.php?pid=6143 Killer Names Time Limit: 2000/1 ...

  6. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. HDU6143 Killer Names(数论)

    Killer Names 传送门1 传送门2 Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith Lo ...

  8. HDU 6156 Palindrome Function 数位DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6156 题目描述: 求L~R所有的数的l~r进制的f(x), f(x) = 当前进制 如果回文串, f ...

  9. HDU 4652 Dice:期望dp(成环)【错位相减】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4652 题意: 给你一个有m个面的骰子. 两种询问: (1)"0 m n": &qu ...

最新文章

  1. Qt安装后配置环境变量(Mac)
  2. java编译POSTGRESQL_Java连接PostgreSQL数据库(安装环境 + 简易测试代码)
  3. 多核服务器的JVM优化选项(转载)
  4. matlab读取fortran文件夹,怎样用fortran获取一个文件夹里的全部文件名?
  5. jsr250-api_JSON处理的Java API(JSR-353)–流API
  6. [vue] 写出你知道的表单修饰符和事件修饰符
  7. P3185 [HNOI2007]分裂游戏
  8. 中控指纹考勤机软件登录用户名和密码忘记的解决办法
  9. 人人对战五子棋 C++
  10. 全网最全数据分析师面试干货-业务逻辑篇
  11. 邮箱地址采集的10个经典方法
  12. 修改WMB执行组监听端口
  13. Golang 原子操作与互斥锁
  14. 房价会象汽车电脑一样下降
  15. 计算机网络技术ip地址计算,计算机网络原理-IP地址计算题.doc
  16. sql server访问Excel97-2003 Excel2007
  17. 2016 威斯康星 计算机科学,威斯康星麦迪逊大学计算机科学本科申请条件及案例分析...
  18. RB-tree(红黑树)
  19. JavaWeb——JSP技术
  20. PIL gif 图片加文字 python

热门文章

  1. VMware中装Win2012并配置Hyper-v
  2. django Error: [Errno 10013]
  3. apache2 指令存取
  4. 漫谈天际网的用户体验
  5. MySQL 5.1以下如何动态抓取查询日志
  6. 使用component小程序
  7. 《认知突围》做复杂时代的明白人,读书分享
  8. idea 中使用@Autowired注入mybatis Mapper接口时报错
  9. js---25桥模式
  10. catch and batch