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. 一款简单而不失强大的前端框架——【Vue.js的详细入门教程①】

    ↓— Vue.js框架魅力 —↓ 前言       Vue.js 是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.V ...

  2. Nodejs最好的ORM - TypeORM

    TypeORM是一个采用TypeScript编写的用于Node.js的优秀ORM框架,支持使用TypeScript或Javascript(ES5, ES6, ES7)开发.目标是保持支持最新的Java ...

  3. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---简介

    这一章描述如何初始化一个新线程,如何停止一个执行中的线程,以及如何了解并调整线程优先权.    读过这一章之后,你将有能力回答一个 Win32 多线程程序设计的最基本问题.你一定曾经在 Usenet ...

  4. 【Revit API】梁构件支座检查算法

    一.前言         应该是第二次写关于Revit API的博文了.虽然在BIM企业中工作,从事桌面BIM软件开发,但是我是不怎么喜欢写Revit API相关的代码.平时更多的是在写界面展示,架构 ...

  5. Palindrome poj3974

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 3280   Accepted: 1188 Descr ...

  6. 退出psql时,报psql_history的错

    数据库版本:Enterprisedb 9.2(postgreSQL) 错误如下所示: postgres=# exitcould not save history to file "/opt/ ...

  7. Java面向对象 继承(下)

     Java面向对象   继承(下) 知识概要:               (1)抽象类 1.1 抽象类概述                            1.2 抽象类的特点       ...

  8. var let const 的区别

    Var let const 的区别 1.Var 定义的变量存在变量提升,而了let和const不存在变量提升.即在定义的变量代码上使用该变量,var的会输出undefined,而let的会报错. 2. ...

  9. wpf值转换器IValueConverter例子

    转载:http://blog.163.com/wangzhenguo2005@126/blog/static/37140526201085113430862/ 值转换器可以把一种类型转换成另一种类型. ...

  10. win10 UWP 序列化

    将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象. .NET Framewor ...