K - Killer Names HDU - 6143 (dp)

K - Killer Names

 HDU - 6143 

 

> 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 nn characters, while each character is chosen from an alphabet of size mm. It appears that there are m2nm2n 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 TT (T≤10T≤10), denoting the number of test cases. 

Each test case contains two integers nn and mm (1≤n,m≤20001≤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 mod 109+7 

Sample Input

2
3 2
2 3

Sample Output

2 
18

题意:一个名字由左边n个字符和右边n个字符构成,一共有m个字符可用,问有多少个名字左边和右边没有共同的字符。

思路:左边最多能有j =  (1 ~ m-1)个不同字符,那右边就只能有(m - j) 个不同字符,有n个位置每个位置有(m-j)种放法,所以右边共有(m - j)^n 种不同放法,那左边确定右边就确定了,所以我们来考虑左边:

让dp[i][j] 表示在i个位置上放j不同种字符的放法,那dp[1][1]就等于m了,因为一个位置可以放第一种字符,也可以放第二种...第m种,所以有m种放法,那临界状态就确定了。考虑dp[i][j]时应该如何转移:

dp[i][j]表示在i个位置上放j不同种字符的放法,那么他肯定是由i-1个位置的放法增加一个位置而来,增加的这个位置可以放之前放过的j种字符 dp[i-1][j] * j ,也可以放之前没放过的字符 dp[i-1][j-1] * (m - (j-1) ) 。

那么总的放法就是dp[i][j] = dp[i-1][j] * j + dp[i-1][j-1] * (m - (j-1))

https://blog.csdn.net/qq_41117236/article/details/81515225

https://blog.csdn.net/mirror58229/article/details/77341252

#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
ll dp[2007][2007];
ll ksm(ll a, ll b) {
	a %= mod;
	ll sum = 1;
	while (b) {
		if (b&1) sum = sum*a%mod;
		a = a*a%mod;
		b >>= 1; 
	}
	return sum;
}
int main() {
	int t;
	scanf ("%d", &t);
	while (t--) {
		int n, m;
		scanf ("%d %d", &n, &m);
		memset(dp, 0, sizeof(dp));
		dp[1][1] = m;
		for (int i = 2; i <= n; ++i) {
			for (int j = 1; j <= m; ++j) {
				dp[i][j] = (dp[i-1][j] * j % mod + dp[i-1][j - 1] * (m - (j - 1)) % mod) % mod; 
			}
		}
		ll ans = 0;
		for (int i = 1; i < m; ++i) {
			ans += (dp[n][i] * ksm(m - i, n));
			ans %= mod;
		}
		printf ("%lld\n", ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值