**题目:**Problem - 6143
http://acm.hdu.edu.cn/showproblem.php?pid=6143
Killer Names
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1145 Accepted Submission(s): 562

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

2017 Multi-University Training Contest - Team 8

题意:从m个字符选出一些字符,组成姓与名。姓与名都是由n个字符组成的字符串。但是姓与名里面不能含有相同的字母。问你这样的明早由多少种。
思路:比赛的时候,其实很多自己考虑到了,但是就是ac不了,然后觉得自己是错的,很多时候,就是这样,自己怀疑自己方向错了,实际上是正确的边缘,只是有么一点点情况自己还是没有考虑到,导致思路混乱,所以吖,做题还是不够,还得加油。
方法:把问题先转化为2部分,先管姓,那么姓得问题就是。假如姓有i个字母组成。那么有i^n,但是会有重复得或者不合格得。所以要减掉。那么我们就假设一个数组f(i),f(i)的值表示,姓由i个字符组成的情况。那么
i=1时,f(1)=1;
i=2时,f(2)=2^n-c(2,1)*f(1);
i=3时,f(3)=3^n-c(3,1)*f(1)-c(3,2)*f(2);
依次推理。。
注意千万不要一步到位,认为减一次就把所有的减掉,那么很容易出错。
由于数据不多,这样,我们可以枚举前面n位有1个、2个字符组成的情况。后面的名部分就是(m-1)^n;。把前面的姓的情况,乘于后面的名部分就是这种情况的情况,然后加起来。
不懂可以看
Killer Names(HDU 6143) - 残阳止水 - CSDN博客
http://blog.csdn.net/luyehao1/article/details/77369118
这个除了理解这个,还有好多陷阱。比如,算排列组合,可以用杨辉三角去算,就是用数组存起来。记住那么公式。
还有算那个f()(下面代码里面是dp数组)的时候,我也错了好多次。
还有一个更加容易错的:所以(a-b)%mod=(a%mod-b%mod)%mod;但是在本题中,可能减的时候,会让结果变成负数,所以,最后要(dp[i]+mod)%mod;看起来多一个,其实不是,以后注意了。

附上我详细的代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define mod 1000000007
long long int dp[2002];
long long int c[2002][2002];
long long f(long long a,long long b){long long sum=1;while(b){if(b&1){sum=(sum%mod)*(a%mod)%mod;}b/=2;a=a%mod*(a%mod)%mod;}return sum;
}//快速幂法。
int main(){int t;int i,j;for(i=1;i<=2000;i++){c[i][1]=i;c[0][i]=0;c[i][0]=0;}for(i=2;i<=2000;i++){for(j=2;j<=i;j++){c[i][j]=(c[i-1][j]%mod+c[i-1][j-1]%mod)%mod;}}//记住求排列组合时,你一定用逆元了。可以用这个了。 scanf("%d",&t);long long int sum;long long ans;int n,m;while(t--){sum=0;scanf("%d%d",&n,&m);memset(dp,0,sizeof(dp));dp[1]=1;//这里易错,之前我直接乘于c(m,1)了。注意你这个意义。 for(i=2;i<m&&i<=n;i++){dp[i]=f(i,n);ans=0;for(j=1;j<=i-1;j++)dp[i]=(dp[i]-(c[i][j]*dp[j])%mod)%mod;dp[i]=(dp[i]+mod)%mod;//这里也易错,减法虽然满足,取模的交换率,但是可以会变成负数,所以你加上一个不会错的。 }// 这里计算dp。 for(i=1;i<m&&i<=n;i++){sum=(sum%mod+((dp[i]%mod*(c[m][i])%mod)%mod*(f(m-i,n)%mod))%mod)%mod;}printf("%lld\n",sum);}return 0;
}//求排列组合时,也可以用dp了,就不用那么麻烦去逆元了 

(本题计算那个排列组合有其他的方法,可以看看这个Hdu 6143 Killer Names【思维+斯特灵数】 - mengxiang000000 - CSDN博客
http://blog.csdn.net/mengxiang000000/article/details/77341835学点新东西)

排列组合,字符串——Killer Names相关推荐

  1. C语言打印字符串的所有排列组合(附完整源码)

    C语言打印字符串的所有排列组合 字符串的所有排列问题 C语言打印字符串的所有排列组合的完整源码(定义,实现,main函数测试) 字符串的所有排列问题 示例:ABC的排列是ABC,ACB,BCA,BAC ...

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

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

  3. HDU 6143 Killer Names(排列+容斥,dp)

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

  4. 无重复字符串的排列组合

    无重复字符串的排列组合.编写一种方法,计算某字符串的所有排列组合,字符串每个字符均不相同. 示例1: 输入:S = "qwe" 输出:["qwe", " ...

  5. java 字符串排列组合_Java 程序计算列出字符串的所有排列组合

    Java 程序计算列出字符串的所有排列组合 在此示例中,我们将学习计算Java中字符串的所有排列组合. 要理解此示例,您应该了解以下Java编程主题: 字符串的排列是指可以通过互换字符串字符的位置来形 ...

  6. python实现不重复排列组合_Python实现输入字符串,返回其任意排列组合

    在脉脉上看到的一道算法题,要求如下图: 以下是用Python实现这一算法: def str_rank_combination(s=""): """输入 ...

  7. 字符串所有排列组合暴力递归

    给你一个字符串"acb",可以打印出六种排列组合,这里又是一种index推动的递归,但是这里有一些小trick,就是从第一个开始,在后面的字符串的每一个字符进行交换,这样就可以省很 ...

  8. leetcode面试题 08.08. 有重复字符串的排列组合(回溯)

    有重复字符串的排列组合.编写一种方法,计算某字符串的所有排列组合. 示例1: 输入:S = "qqe" 输出:["eqq","qeq",&q ...

  9. 程序员面试金典 - 面试题 08.07. 无重复字符串的排列组合(回溯)

    1. 题目 无重复字符串的排列组合.编写一种方法,计算某字符串的所有排列组合,字符串每个字符均不相同. 示例1:输入:S = "qwe"输出:["qwe", & ...

最新文章

  1. mysql配置文件没有spring_spring cloud config使用mysql存储配置文件
  2. Numpy学习笔记(下篇)
  3. python语言程序设计2019版第二章课后答案-《Python语言程序设计》 —2.7 课后习题...
  4. 图解内存搜索工具初步使用
  5. android中仿qq最新版抽屉,Android 自定义View实现抽屉效果
  6. 软定时器的删除与状态查询
  7. 强化学习笔记:Policy-based Approach
  8. 一文读懂大数据平台——写给大数据开发初学者的话!
  9. 5.1.8 缓冲区管理
  10. MySQL主备复制原理、实现及异常处理
  11. 第3步 (请先看第2步再看第3步) 新建完spring+springmvc+mybatis项目 需要推送gitee仓库进行管理 巨详细
  12. window+Apache 配置虚拟主机(2)
  13. [翻译]自定义Sharepoint的登陆页面
  14. 【英语学习】【English L06】U08 News L3 Was it drunk driving again?
  15. Linux内核编译与安装[转]
  16. MongoDB简单CRUD场景
  17. 中继器是什么计算机网络,中继器是什么
  18. Word插入Latex公式的几种方式~(TeXsWord、EqualX、Aurora、向Office插入LaTeX公式的工具)...
  19. Win10 Office2016 激H失败错误代码0x80080005
  20. Xubuntu22.04之连接filco蓝牙键盘

热门文章

  1. 小白学习latex的辅助资料
  2. 电脑分区了如何恢复?电脑新手村教程
  3. 旧手机(小米4)装linux的一个记录
  4. VS中*.clw *.ncb *.opt *.aps这些文件是做什么用的?
  5. go语言爬虫解析html,Python爬虫【如何爬取内容(html)和解析内容】-Go语言中文社区...
  6. 室内定位:基于NB/LTE Cat.1蜂窝网络的穿戴设备定位 BLE-4
  7. 【PIL处理图片】小技巧之画虚线、加粗字体、长文本自动分行(符号处理)
  8. 面向模式的软件体系结构(卷1-5
  9. 网易云音乐上关于摇滚歌单的各种排行榜
  10. 蓝牙耳机什么牌子好?性价比最高的蓝牙耳机排行榜