HDU6143 Killer Names(数论)

Killer Names

传送门1
传送门2

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(T10) , denoting the number of test cases.

Each test case contains two integers n and m(1n,m2000) .

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


题意

m 种颜色需要为两段长度为 n 的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况。

思路

组合数+第二类 Stirling 数.
枚举为两段分配的颜色 i,j(i+j<=m) 则第一段有 Cim 种方案,第二段有 Cjmi 种方案。
在每段中使用的颜色数量为x,则染色方案数就为 x!×S(n,x). 不明白的戳这里
于是结果为所有 (Cim×i!×S(n,i))×(Cjmi×j!×S(n,j)) 的和。
参考

CODE

#include<cstdio>
#include<memory.h>
#define N 2005
#define FOR(x,a,b) for(int x=(a),x##_END_=(b);x<=x##_END_;x++)
long long P=1000000007,S[N][N],C[N][N],A[N];
int main() {
    A[0]=A[1]=S[0][0]=S[1][1]=C[0][0]=C[1][0]=C[1][1]=1;
    S[1][0]=0;
    FOR(i,2,N-1) {
        A[i]=(1LL*A[i-1]*i)%P;
        C[i][0]=C[i][i]=S[i][i]=1;
        S[i][0]=0;
        FOR(j,1,i-1) {
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%P;
            S[i][j]=(S[i-1][j-1]+1LL*j*S[i-1][j]%P)%P;
        }
    }
    int T,n,m;
    scanf("%d",&T);
    while(T--) {
        scanf("%d %d",&n,&m);
        long long ans=0;
        FOR(i,1,(m+1)/2)FOR(j,i,m-i) {
            long long cnt=((C[m][i]*A[i]%P)*S[n][i]%P)*
                        ((C[m-i][j]*A[j]%P)*S[n][j]%P)%P;
            if(i!=j)cnt=(cnt<<1)%P;
            ans=(ans+cnt)%P;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
发布了34 篇原创文章 · 获赞 44 · 访问量 5080

猜你喜欢

转载自blog.csdn.net/zzyyyl/article/details/78416999