Killer Names

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 342    Accepted Submission(s): 178

Problem Description
> 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 n characters, while each character is chosen from an alphabet of size m. It appears that there are m2n 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), denoting the number of test cases.

Each test case contains two integers n 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+7

 
Sample Input
2
3 2
2 3
 
Sample Output
2
18
 
Source

/*
* @Author: lyuc
* @Date: 2017-08-17 11:02:49
* @Last Modified by: lyuc
* @Last Modified time: 2017-08-17 15:07:25
*/
/*
题意:一个人的名字由名和姓组成,每个部分都严格的有n个字母,现在有m个字母可以选择,
并且名和姓不能有相同的字母 思路:枚举姓中用的字母数量,最小1,最大min(n,m)个,现在的问题就是 i (i<=n)个字母可
重复的使用能组成多少种名字,这里用到了容斥(看了两天的容斥,竟然用到了,哈哈哈)
如果不考虑i个必须都用到,那么就有i^n中情况,但是中间多了很多这种,要减去最多用
i-1种字母的情况,但是又减多了,最后总的情况就是:
res=C(i,i)*i^n-C(i,i-1)*(i-1)^n+C(i,i-2)*(i-2)^n...C(i,1)*1^n;
然后这是姓的情况,剩下m-i个字母在名中可以随意用所以就是,(m-i)^n
*/ #include <bits/stdc++.h> #define LL long long
#define MAXN 2010
const LL MOD=1e9+; using namespace std; int t;
LL n,m;
LL len;
LL res; LL F[MAXN], Finv[MAXN], inv[MAXN];//F是阶乘,Finv是逆元的阶乘
LL X[MAXN][MAXN]; void init(){
memset(F,,sizeof F);
memset(Finv,,sizeof Finv);
memset(inv,,sizeof inv);
memset(X,,sizeof X);
inv[] = ;
for(LL i = ; i < MAXN; i ++){
inv[i] = (MOD - MOD / i) * 1LL * inv[MOD % i] % MOD;
}
F[] = Finv[] = ;
for(LL i = ; i < MAXN; i ++){
F[i] = F[i-] * 1LL * i % MOD;
Finv[i] = Finv[i-] * 1LL * inv[i] % MOD;
}
X[][]=;
for(LL i=;i<=;i++){
X[i][]=;
for(LL j=;j<=;j++){
X[i][j]=(X[i][j-]%MOD*i)%MOD;
}
}
} LL Comb(LL n,LL m){//comb(n, m)就是C(n, m)
if(m < || m > n) return ;
return F[n] * 1LL * Finv[n - m] % MOD * Finv[m] % MOD;
}
//
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
init();
scanf("%d",&t);
while(t--){
scanf("%lld%lld",&n,&m);
res=;
len=min(n,m); for(LL i=;i<=len;i++){//枚举名字中放的字母数
LL cnt=;
LL F=;
for(LL j=i;j>=;j--){//容斥求i个物品,放在n个格子内的种类数
cnt=((cnt+Comb(i,j)*F*X[j][n]+MOD)%MOD)%MOD;
F *=-;
}
// [这个是取出i个字母放到名中] [这是剩余的字母在后n中随便放]
res = ( (res + cnt * Comb(m,i) % MOD * X[m - i][n] +MOD) % MOD) % MOD;
}
printf("%lld\n",res);
}
return ;
}

HDU 6143 Killer Names的更多相关文章

  1. HDU 6143 - Killer Names | 2017 Multi-University Training Contest 8

    /* HDU 6143 - Killer Names [ DP ] | 2017 Multi-University Training Contest 8 题意: m个字母组成两个长为n的序列,两序列中 ...

  2. HDU 6143 Killer Names DP+快速密

    Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human apprentice ...

  3. HDU 6143 Killer Names(容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意: 用m个字母去取名字,名字分为前后两部分,各由n个字符组成,前后两部分不能出现相同字符,问合法的组成 ...

  4. 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...

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

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

  6. hdu 6143: Killer Names (2017 多校第八场 1011)

    题目链接 题意,有m种颜色,给2n个位置染色,使左边n个和右边n个没有共同的颜色. 可以先递推求出恰用i种颜色染n个位置的方案数,然后枚举两边的染色数就可以了,代码很简单. #include<b ...

  7. HDU - 6143 Killer Names(dp记忆化搜索+组合数)

    题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的 ...

  8. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  9. hdu6143 Killer Names 容斥+排列组合

    /** 题目:hdu6143 Killer Names 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:有m种字符(可以不用完),组成两个长度 ...

随机推荐

  1. postman安装使用教程---图文讲解

    一.安装postman 1,安装包安装 官网下载地址:https://www.getpostman.com 选择好对应的版本下载,下载完后直接安装 2,插件包安装 可以在谷歌的应用商店里面找到,或者在 ...

  2. [转]IOS开发中的CGFloat、CGPoint、CGSize和CGRect

    http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference ...

  3. 使用VUE模仿BOSS直聘APP

    一.碎碎念: 偶尔在群里看到一个小伙伴说:最近面试的人好多都说用vue做过一个饿了么.当时有种莫名想笑. 为何不知道创新一下?于是想写个DEMO演练一下.那去模仿谁呢?还是BOSS直聘(跟我没关系,不 ...

  4. 《JavaScript闯关记》视频版硬广

    <JavaScript闯关记>视频版硬广 stone 在菜航工作时,兼任内部培训讲师,主要负责 JavaScript 基础培训,2016年整理的<JavaScript闯关记>课 ...

  5. thinkphp的select和find的区别

    hinkphp是比较好的php开发框架,能比较快速的开发MVC架构的管理系统,我们需要用到 select()和find()方法,两个方法都能返回数据集数组,但有什么不同呢?先看一下我的代码对比:$te ...

  6. mui框架移动开发初体验

      前  言 博主最近在接触移动APP,学习了几个小技巧,和大家分享一下. 1. 状态栏设置 现在打开绝大多数APP,状态栏都是与APP一体,不仅美观,而且与整体协调.博主是个中度强迫症患者,顶部那个 ...

  7. clone github报Permission denied (publickey) 解决方案

    问题描述 问题产生的原因,不是很清楚,就不管了.在执行git clone git@github.com:****.git 的时候报了Permission denied (publickey). War ...

  8. ubuntu中运行python脚本

    1. 运行方式一 新建test.py文件: touch test.py 然后vim test.py打开并编辑: print 'Hello World' 打开终端,输入命令: python test.p ...

  9. 关于Struts与Ajax整合时的异常处理

     关于Struts与Ajax整合时的异常处理问题: 问题还原: 从而当有异常发出时,会将异常信息发送到页面上.如下图所示:这是一个比较经典的过程: 错误提示页面: 由于sendError()方法里 ...

  10. 【笔记】【VSCode】Windows下VSCode编译调试c/c++

    转载自http://m.2cto.com/kf/201606/516207.html 首先看效果 设置断点,变量监视,调用堆栈的查看: 条件断点的使用: 下面是配置过程: 总体流程: 下载安装vsco ...