HDU 6143 Killer Names
Killer Names
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 342 Accepted Submission(s): 178
>
> 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.
Each test case contains two integers n and m (1≤n,m≤2000).
Output the answer mod 109+7
3 2
2 3
18
/*
* @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的更多相关文章
- HDU 6143 - Killer Names | 2017 Multi-University Training Contest 8
/* HDU 6143 - Killer Names [ DP ] | 2017 Multi-University Training Contest 8 题意: m个字母组成两个长为n的序列,两序列中 ...
- HDU 6143 Killer Names DP+快速密
Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human apprentice ...
- HDU 6143 Killer Names(容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意: 用m个字母去取名字,名字分为前后两部分,各由n个字符组成,前后两部分不能出现相同字符,问合法的组成 ...
- 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...
- 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 (2017 多校第八场 1011)
题目链接 题意,有m种颜色,给2n个位置染色,使左边n个和右边n个没有共同的颜色. 可以先递推求出恰用i种颜色染n个位置的方案数,然后枚举两边的染色数就可以了,代码很简单. #include<b ...
- HDU - 6143 Killer Names(dp记忆化搜索+组合数)
题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的 ...
- HDU 6143 17多校8 Killer Names(组合数学)
题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...
- hdu6143 Killer Names 容斥+排列组合
/** 题目:hdu6143 Killer Names 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:有m种字符(可以不用完),组成两个长度 ...
随机推荐
- 翻译连载 | 第 9 章:递归(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...
- 树状数组初步_ZERO
原博客:树状数组 1 一维树状数组 1 什么是树状数组 树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组A[1..n],那么查询A[1]+-+A[n]的时,间是log级 ...
- mysql操作sql的小技巧
本篇集中整理一下执行sql的小技巧,这种方式不仅带来了操作上的便捷,也可以保证数据可以数据的安全性. 1:查询数据(保证查询性能) 首先想先解释一下 SELECT * 和 SELECT t.id , ...
- 洗礼灵魂,修炼python(3)--从一个简单的print代码揭露编码问题,运行原理和语法习惯
前期工作已经准备好后,可以打开IDE编辑器了,你可以选择python自带的IDLE,也可以选择第三方的,这里我使用pycharm--一个专门为python而生的IDE 按照惯例,第一个python代码 ...
- 认识 Java Message Service
1. Java Message Service : 是一个消息服务的标准或者说是规范,允许应用程序组件基于JavaEE平台创建.发送.接收和读取消息. 实现Java 程序与MQ Server 之间互相 ...
- Rectangles hdu2461容斥定理
Rectangles Time Limit: 5000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- S2_SQL_第三章
3.1:修改表 3.1.1:修改表 语法: Alter table <旧表名> rename [ TO] <新表名>; 例子:Alter table `demo01` rena ...
- css常见布局方式
CSS常见布局方式 以下总结一下CSS中常见的布局方式.本人才疏学浅,如有错误,请留言指出. 如需转载,请注明出处:CSS常见布局方式 目录: 使用BFC隐藏属性 float + margin abs ...
- webpack——devtool里的7种SourceMap模式
我们先来看看文档对这 7 种模式的解释: 模式 解释 eval 每个module会封装到 eval 里包裹起来执行,并且会在末尾追加注释 //@ sourceURL. source-map 生成一个S ...
- Kettle文本文件输出和输入控件使用中,换行符导致的问题处理
1.如下图通过输入控件从数据库读取数据然后生成TXT文本文件,TXT文件生成原则是每一条数据生成一行数据,第二条数据换行保存 2.如下图所示,使用文本文件输入控件读入上图生成的文件,文件读入原则是按行 ...