Killer Names

传送门1
传送门2

Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith Lord Darth Vader. A powerful Force-user who lived during the era of the Galactic Empire, Marek originated from the Wookiee home planet of Kashyyyk as the sole offspring of two Jedi Knights—Mallie and Kento Marek—who deserted the Jedi Order during the Clone Wars. Following the death of his mother, the young Marek’s father was killed in battle by Darth Vader. Though only a child, Marek possessed an exceptionally strong connection to the Force that the Dark Lord of the Sith sought to exploit.

When Marek died in 2 BBY, shortly after the formation of the Alliance, Vader endeavored to recreate his disciple by utilizing the cloning technologies of the planet Kamino. The accelerated cloning process—an enhanced version of the Kaminoan method which allowed for a rapid growth rate within its subjects—was initially imperfect and many clones were too unstable to take Marek’s place as the Dark Lord’s new apprentice. After months of failure, one particular clone impressed Vader enough for him to hope that this version might become the first success. But as with the others, he inherited Marek’s power and skills at the cost of receiving his emotions as well, a side effect of memory flashes used in the training process.

— Wookieepedia

Darth Vader is finally able to stably clone the most powerful soilder in the galaxy: the Starkiller. It is the time of the final strike to destroy the Jedi remnants hidden in every corner of the galaxy.

However, as the clone army is growing, giving them names becomes a trouble. A clone of Starkiller will be given a two-word name, a first name and a last name. Both the first name and the last name have exactly nn characters, while each character is chosen from an alphabet of size mm. It appears that there are m2nm^{2n} possible names to be used.

Though the clone process succeeded, the moods of Starkiller clones seem not quite stable. Once an unsatisfactory name is given, a clone will become unstable and will try to fight against his own master. A name is safe if and only if no character appears in both the first name and the last name.

Since no two clones can share a name, Darth Vader would like to know the maximum number of clones he is able to create.

Input

The First line of the input contains an integer T(T≤10)T (T≤10), denoting the number of test cases.

Each test case contains two integers n and m(1≤n,m≤2000)n\mbox{ and }m(1≤n,m≤2000).

Output

For each test case, output one line containing the maximum number of clones Vader can create.

Output the answer mod 109+710^9+7

Sample Input

2
3 2
2 3

Sample Output

2
18


题意

m 种颜色需要为两段长度为 n 的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况。

思路

组合数+第二类Stirling\mbox{Stirling}数.
枚举为两段分配的颜色i,j(i+j<=m)i,j(i+j则第一段有CimC_m^i种方案,第二段有Cjm−iC_{m-i}^j种方案。
在每段中使用的颜色数量为x,则染色方案数就为x!×S(n,x).x!\times S(n,x).不明白的戳这里
于是结果为所有(Cim×i!×S(n,i))×(Cjm−i×j!×S(n,j))\Bigl(C_m^i\times i!\times S(n,i)\Bigr)\times \Bigl(C_{m-i}^j\times j! \times S(n,j)\Bigr)的和。
参考

CODE

#include<cstdio>
#include<memory.h>
#define N 2005
#define FOR(x,a,b) for(int x=(a),x##_END_=(b);x<=x##_END_;x++)
long long P=1000000007,S[N][N],C[N][N],A[N];
int main() {A[0]=A[1]=S[0][0]=S[1][1]=C[0][0]=C[1][0]=C[1][1]=1;S[1][0]=0;FOR(i,2,N-1) {A[i]=(1LL*A[i-1]*i)%P;C[i][0]=C[i][i]=S[i][i]=1;S[i][0]=0;FOR(j,1,i-1) {C[i][j]=(C[i-1][j-1]+C[i-1][j])%P;S[i][j]=(S[i-1][j-1]+1LL*j*S[i-1][j]%P)%P;}}int T,n,m;scanf("%d",&T);while(T--) {scanf("%d %d",&n,&m);long long ans=0;FOR(i,1,(m+1)/2)FOR(j,i,m-i) {long long cnt=((C[m][i]*A[i]%P)*S[n][i]%P)*((C[m-i][j]*A[j]%P)*S[n][j]%P)%P;if(i!=j)cnt=(cnt<<1)%P;ans=(ans+cnt)%P;}printf("%lld\n",ans);}return 0;
}

HDU6143 Killer Names(数论)相关推荐

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

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

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

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

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

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

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

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

  5. HDU 6143 Killer Names (组合数学+DP)

    Description 字母表的长度为\(m\),用表中的字母构造长度为\(2n\)的字符串,要求同一种字母能同时出现在前\(n\)个字符中和后\(n\)个字符中.输出方案数,结果模\(10^9+7\ ...

  6. Killer Names( 容斥定理,快速幂 )

    题目大意: m种不同颜色的球,填两组盒子,每组盒子有n个,两组盒子中不能有相同颜色的球,问方法总数有多少. 解题思路:利用容斥定理,m种颜色放入n个格子共有m^n个,但其中肯定有不满足m种颜色的,所以 ...

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

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

  8. 计算机视觉 响应_视觉响应式布局的自动化测试

    计算机视觉 响应 Today it's all about testing. In 2015, many developers knows about TDD and I personally thi ...

  9. 数论(一)——素数,GCD,LCM

    这是一个数论系列:) 一.素数 ×费马小定理 Theorem: 设 p 是一个素数,a 是一个整数且不是 p 的倍数,那么 很遗憾,费马小定理的逆定理是不成立的.对 a = 2,满足的非素数 n 是存 ...

最新文章

  1. 为 PHP 应用提速、提速、再提速
  2. 开源代码维护人员大诉苦水,超半正考虑或已经决定放弃
  3. SQLSERVER DISTINCT的反例
  4. 【不采用】人工智能如何帮助银行反欺诈
  5. php 彩蛋 flag,php4的彩蛋
  6. 二叉堆(binary heap)—— 优先队列的实现
  7. WPF之HierarchicalDataTemplate(转)
  8. Java开发笔记(一百二十八)Swing的图标
  9. Oracle之分析函数
  10. 第十七章 - 垃圾回收器
  11. 在Unity3D中控制动画播放
  12. 微信小程序抓包-夜神模拟器结合BurpSuite抓包(可用于现在最新版本微信)
  13. PostGreSQL安装压缩包格式
  14. 数学建模债券投资组合_最优投资组合问题的数学模型
  15. 支付宝集五福最全攻略,五分钟集齐五福!
  16. 百度代码配置化实践:配置化是业务架构三化之一
  17. JDK1.8源码分析:LinkedList
  18. 【html】css字体样式
  19. opencv计算图像亮度调节_opencv调整图像亮度对比度
  20. CVE-2022-0847 Linux内核提权漏洞分析

热门文章

  1. 补单平台开发搭建源码_补单系统开发搭建IDEA导入jdk8源码学习(报错解决方案)
  2. 3)数据科学的数学之序列与极限--阶乘/指数增长比较
  3. 使用Cronjobs的综合指南
  4. GUEST帐号“反客为主”
  5. python求共轭复数_python怎么print出共轭复数
  6. 关于yd ui 移动端自适应的方案
  7. AutoRunner4.5结合真实项目crm客户管理系统进行界面功能自动化测试教学资料(登录)
  8. 【多元统计分析与R语言】【详解】使用教材P84页表3-2进行多元数据简单R分析:定量变量的分析(直方图、散点图)、定性变量的分析并绘制绘制均值条图、箱尾图、星相图、调和曲线图
  9. 自己做量化交易软件(9通通量化框架的雏形建立
  10. 春暖花开,微信公众号图文排版素材礼包已到!