HDU 6143 Killer Names (容斥)
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 10^9+7
Sample Input
2
3 2
2 3
1
2
3
Sample Output
2
18
1
2
题意
m
种颜色需要为两段长度为 n 的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况。
首先枚举fiist name 用了几个字母,算出此时的方案数
然后last name 可用的字母数就为 m-i 个,此时last name的方案为 m-i的n次方
每次 ans += 两者相乘
1 #include<bits/stdc++.h>
2 typedef long long LL;
3 using namespace std;
4 const int maxn =2005;
5 const int mod = 1e9+7;
6 LL res[maxn];
7 LL C[maxn][maxn];
8 LL mul[maxn][maxn];
9 LL mult(LL a,LL b)
10 {
11 if(mul[a][b])return mul[a][b];
12 LL ans=1,aa=a,bb=b;
13 a%=mod;
14 while(b)
15 {
16 if(b&1)ans=(ans*a)%mod;
17 a=(a*a)%mod;
18 b>>=1;
19 }
20 mul[aa][bb]=ans;
21 return ans;
22 }
23 void init()
24 {
25 C[0][0]=1;
26 for(int i=1; i<maxn; i++) for(int j=0; j<=i; j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
27 }
28 LL getget(LL n,LL m)
29 {
30 LL cnt=0;
31 for(int k=0; k<=m; k++)
32 {
33 if(k&1)cnt=(cnt-C[m][k]*mult(m-k,n)%mod)%mod;
34 else cnt=(cnt+C[m][k]*mult(m-k,n)%mod+mod)%mod;
35 }
36 return cnt;
37 }
38 template <class T> inline void scan_d(T &ret)
39 {
40 char c;
41 ret = 0;
42 while ((c = getchar()) < '0' || c > '9');
43 while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();
44 }
45 void Out(LL a)
46 {
47 if (a < 0)
48 {
49 putchar('-');
50 a = -a;
51 }
52 if (a >= 10) Out(a / 10);
53 putchar(a % 10 + '0');
54 }
55 int main()
56 {
57 init();
58 int T;
59 scan_d(T);
60 while(T--)
61 {
62 memset(res,0,sizeof(res));
63 int n,m;
64 LL ans=0;
65 scan_d(n);
66 scan_d(m);
67 for(int i=1; i<m; i++) res[i]=getget(n,i);
68 for(int i=1; i<m; i++)
69 {
70 LL aa=C[m][i]*res[i]%mod;
71 LL bb=1;
72
73 for(int j=1; j<=n; j++)
74 bb*=m-i,bb%=mod;
75
76 LL cnt = (aa*bb)%mod;
77 ans=(cnt+ans)%mod;
78
79 }
80 Out(ans);
81 putchar('\n');
82 }
83 return 0;
84 }