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种字符(可以不用完),组成两个长度 ...
随机推荐
- STS安装
在eclipse中安装spring tool Suite插件需要根据eclipse版本找到对应的spring tool Suite安装包. spring tool Suite 官网地址:http:// ...
- HDU2688-Rotate
Recently yifenfei face such a problem that give you millions of positive integers,tell how many pair ...
- Codeforce E. Fire
E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Spring框架(二)
Spring反射机制: 1, 通过spring来获取一个对象的实例 <bean id="user" class="com.model.User"> ...
- java之装饰器模式
Decorator Pattern(装饰器模式),定义:Attach additional responsibilities to an object dynamically. Decorators ...
- activemq的安装与使用
一.activemq的安装 环境:CentOS 6.JDK8 1. 确保系统已安装了可用的jdk版本2. 从网上下载 Linux 版的 ActiveMQ( apache-activemq-5.11.1 ...
- php获取音悦台视频
<?php $url=isset($_GET['url'])?trim($_GET['url']):''; $url = "http://v.yinyuetai.com/video/6 ...
- linux下c语言的多线程编程
我们在写linux的服务的时候,经常会用到linux的多线程技术以提高程序性能 多线程的一些小知识: 一个应用程序可以启动若干个线程. 线程(Lightweight Process,LWP),是程序执 ...
- PHP字符串替换str_replace()函数4种用法详解
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )该函数返回一个字符串 ...
- VUE实现请求数据
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...