HDU 6143 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.

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 109+7

Sample Input

2
3 2
2 3

Sample Output

2
18

Source

2017 Multi-University Training Contest - Team 8

題意

famliy name 和 last name長度各爲n,現有m種字符供選擇,規定famliy name 和 last name中不能有一樣的字符。
如:aac bbb可以,但aab acc不可以。

想法

  1. 首先 ,不考慮重複,全部的情況是Cimin(mi)n *→**first name每個位子有i種可能的字符,last name每個位子有m-i種可能的字符;

  2. 然鵝會有重複的,舉個栗子:
    可能在family name能拿a和b,last name拿剩下的其他時,出現family name只出a的情況 比如a a a c c c
    這和之前family name只拿a沒別的出 重複了 因爲這樣也可以出現a a a c c c

  3. 爲了解決去重 我們考慮用容斥定理
    假設有j種字符可能在前面出現過,記爲i1j=1(1)ijCjijn (還是滾去好好看看容斥定理吧+_+)

  4. 最後用去重過的family name的總數*last name能拿的情況就是總結果了。(我他瓜竟然在這裏被繞住了>_<)
    所以總的公式就是(Cimin +i1j=1(1)ijCjijn )*(mi)n

對着公式就可以愉快地敲代碼了~雖然不知道爲什麼我用快速冪求n次方時tle了TAT

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=2100;
ll c[maxn][maxn],p[maxn][maxn];
int t,n,m;
void Combination()///求組合數
{
     c[0][0]=1;
     for(int i=1;i<maxn;i++)
     {
          c[i][0]=c[i][i]=1;
          for(int j=1;j<i;j++)
          {
              c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
          }
     }
}
void mi()///求冪
{
    for(int i=1;i<maxn;++i)
    {
        p[i][0]=1;
        for(int j=1;j<maxn;++j)
            p[i][j]=p[i][j-1]*i%mod;
    }
}
ll solve()
{
    ll ans=0,all;
    for(int i=1;i<m;++i)
    {
        all=p[i][n];
        int tmp=0;
        for(int j=i-1;j>0;--j)
        {
            if(tmp==0)///偶數次
            {
               all=(all-c[i][j]*p[j][n]%mod+mod)%mod;///這裏要+mod,因爲可能爲負
            }
            else///奇數次
            {
                all=(all+c[i][j]*p[j][n]%mod)%mod;
            }
            tmp=1-tmp;
        }
        ans=(ans+c[m][i]*all%mod*p[m-i][n]%mod)%mod;
    }
    return ans;
}
int main()
{

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        Combination();
        mi();
        printf("%lld\n",solve());
    }
    return 0;
}

ps:去問在打排位的小班長,果不其然被罵了QAQ
不過鑑於沒有打死我,還是遙遠的給您筆芯(づ ̄3 ̄)づ╭❤~

但是真的感覺組合知識全都還給濤濤了o(╯□╰)o

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章