HDU 6143 Killer Names DP+快速密
Killer Names
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.
>
> 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).
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
Output the answer mod 109+7
Sample Input
2
3 2
2 3
Sample Output
2
18
题解:
只考虑前半部分
设定dp[i][j],前i个位置使用j种颜色的方案数
当前位置,只考虑使用 使用过的j种 或者 考虑没有使用过的 m-j种
n*n的转移
前半部分用了j种, 剩下 的 m-j 全用在 后半部分,快速密 乘起来 累加就是ans
#include<bits/stdc++.h> using namespace std; #define ls i<<1 #define rs ls | 1 #define mid ((ll+rr)>>1) #define pii pair<int,int> #define MP make_pair typedef long long LL; typedef unsigned long long ULL; const long long INF = 1e18+1LL; const double pi = acos(-1.0); const int N=2000+20,M=1e6+10,inf=2147483647; const LL MOD = 1e9 + 7LL; LL quick_pow(LL x,LL p) { if(!p) return 1LL; LL ans = quick_pow(x,p>>1); ans = ans*ans%MOD; if(p & 1) ans = ans*x%MOD; return ans; } LL dp[N][N],ans,fn[N],fn1[N]; int n,m; void init() { for(int i = 1; i <= m; ++i) fn[i] = quick_pow(i,n) % MOD; fn[0] = 0; memset(dp,0,sizeof(dp)); dp[0][0] = 1LL; for(int i = 0; i < n; ++i) { for(int j = 0; j <= i; ++j) { if(m>=j)dp[i+1][j+1] += dp[i][j] * (m - j) % MOD; dp[i+1][j] += dp[i][j]*j%MOD; dp[i+1][j+1] %= MOD; dp[i+1][j] %= MOD; } } for(int j = 1; j <= n; ++j){ if(m>j)ans = (ans + (dp[n][j] * fn[m-j])%MOD)%MOD; } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); ans = 0; init(); printf("%lld\n",ans); } return 0; }