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. angularui 分页

    分页组件的使用 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> ...

  2. HDFS源码分析之NameNode(3)————RpcServer

    NameNodeRpcServer implements NamenodeProtocols NameNode支持核心即NameNodeRpcServer 实现ClientProtocol  支持客户 ...

  3. C++ sizeof 误区 大公司面试题

    1.C++ 无成员变量和函数的类型的实例,求该实例的sizeof? 答:是1.(不是0) 2.如果在题1的基础上有1个成员变量,sizeof是(1+成员变量的大小)吗? 答:不是,是成员变量的大小. ...

  4. Ionic3学习笔记(四)修改返回按钮文字、颜色

    本文为原创文章,转载请标明出处 目录 修改返回按钮文字 修改返回按钮颜色 1. 修改返回按钮文字 参考官网 Ionic API---Config 文档 可在 ./src/app/app.module. ...

  5. .NET DateTime 源码学习

    今天下载了微软.Net 源码,看了一下DateTime类,做下记录 DaysInMonth 这个方法是获取某年某月的天数,平时直接用觉得很简单,今天看到源码,发现设计的还是很好的 我想如果是我的话,封 ...

  6. Fix “Could not flush the DNS Resolver Cache: Function failed during execution” When Flushing DNS

    ipconfig /flushdns It is possible that you’re getting an error message “Could not flush the DNS Reso ...

  7. 用shell脚本新建shell文件并自动生成头说明信息

    目标: 新建文件后,直接给文件写入下图信息 代码实现: [root@localhost test]# vi AutoHead.sh #!/bin/bash#此程序的功能是新建shell文件并自动生成头 ...

  8. I/O输入输出流

    I/O(输入/输出) 在变量.数组和对象中存储的数据是暂时存在的,程序结束后它们就会消失.为了能够永久地保存创建的数据,需要将其保存在磁盘文件中,这样可以在其他程序中使用它们. Java的I/O技术可 ...

  9. 自适应 Tab 宽度可以滑动文字逐渐变色的 TabLayout(仿今日头条顶部导航)

    TabLayout相信大家都用过,2015年Google大会上发布了新的Android Support Design库里面包含了很多新的控件,其中就包含TabLayout,它可以配合ViewPager ...

  10. 简单的CSS颜色查看工具

    可以通过输入ARGB(A代表透明度)格式或者HEX格式查看颜色,也可以进行ARGB格式和者HEX格式转换,如下图 使用C#编写,我已将源代码压缩上传 下载地址:http://files.cnblogs ...