HDU 6143 Killer Names (第二类斯特林数or容斥)

来源:互联网 发布:手机淘宝明星店铺 编辑:程序博客网 时间:2024/05/16 10:24


Killer Names

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 235    Accepted Submission(s): 128


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 (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
23 22 3
 

Sample Output
2 18
 

Source
2017 Multi-University Training Contest - Team 8
 


题意:每个人的名字由两部分组成,每部分的长度是n,给你m种字符,最多能构成多少个人的名字。 

思路:第二斯特林数or最裸的容斥

第二斯特林数代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int mod = 1e9+7;const int maxn = 2e3+5;ll s[maxn][maxn], c[maxn][maxn], fac[maxn];void init(){    memset(s, 0, sizeof(s));    s[1][1] = 1;    for(int i = 2; i < maxn; i++)        for(int j = 1; j <= i; j++)            s[i][j] = (s[i-1][j-1]+(ll)j*s[i-1][j])%mod;    fac[0] = fac[1] = 1;    for(int i = 2; i < maxn; i++)        fac[i] = fac[i-1]*i%mod;    for(int i = 1; i < maxn; i++)        for(int j = 0; j <= i; j++)        {            if(i == j || !j) c[i][j] = 1;            else c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;        }}ll qpow(ll p, int q){    ll ans = 1;    while(q)    {        if(q%2) ans = ans*p%mod;        p = p*p%mod;        q /= 2;    }    return ans;}int main(void){    init();    int n, m, t;    cin >> t;    while(t--)    {        scanf("%d%d", &n, &m);        ll ans = 0;        int e = min(n, m-1);        for(int i = 1; i <= e; i++)        {            ans = (ans + s[n][i]*fac[i]%mod*qpow(m-i, n)%mod*c[m][i]%mod)%mod;        }        printf("%lld\n", ans%mod);    }    return 0;}

容斥分析:就是一个组合数。如果n个长度用x个字符表达,那就相当于n个不同小球放在x个不同的盒子里。设其结果为f(x),那么f(x) = x ^ n - C(x, 1) * f(x - 1) - C(x, 2) * f(x - 2) …… - C(x, x - 1) * f(1). 
然后就枚举两边各用了多少种字符统计结果。(转自:cysjing

容斥代码:

#include<cstdio>#define mo 1000000007#include<iostream>using namespace std;typedef long long ll;ll n, m;ll C[2005][2005];ll mul[10000];ll cal[2005];ll quickM(ll a, ll b){    ll ans = 1;    while(b){        if(b & 1) ans = ans * a % mo;        b >>= 1;        a = a * a % mo;    }    return ans;}int main(){    mul[0] = 1;    for(ll i = 1; i <= 3000; i++) mul[i] = mul[i] * (i - 1) % mo;    C[0][0] = 1;    for(int i = 1; i <= 2001; i++){        C[i][0] = 1;        for(int j = 1; j <= i; j++){            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mo;        }    }    int T;    scanf("%d", &T);    while(T--){        scanf("%lld%lld", &n, &m);        ll ans = 0;        for(ll i = 1; i <= n; i++){            cal[i] =  quickM(i, n);            for(int j = 1; j < i; j++){                cal[i] -= C[i][j] * cal[j] % mo;                cal[i] = (cal[i] + mo ) % mo;            }        }        for(ll i = 1; i <= n; i++){            if(i >= m) break;            for(ll j = 1; j <= n; j++){                if(j > m - i) break;                ans += C[m][i] * cal[i] % mo * cal[j] % mo * C[m - i][j] % mo;                ans %= mo;            }        }        printf("%lld\n", ans);    }    return 0;}


原创粉丝点击