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. 首先 ,不考虑重复,全部的情况是 Cim in (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)ijCji jn (还是滚去好好看看容斥定理吧+_+)

  4. 最后用去重过的family name的总数*last name能拿的情况就是总结果了。(我他瓜竟然在这里被绕住了>_<)
    所以总的公式就是( Cim in + i1j=1(1)ijCji jn )* (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

### HDU OJ 排列组合问题解法 排列组合问题是算法竞赛中的常见题型之一,涉及数学基础以及高效的实现技巧。以下是关于如何解决此类问题的一些通用方法和具体实例。 #### 数学基础知识 在处理排列组合问题时,需要熟悉以下几个基本概念: - **阶乘计算**:用于求解全排列的数量 $ n! = n \times (n-1) \times ... \times 1 $[^4]。 - **组合数公式**:$ C(n, k) = \frac{n!}{k!(n-k)!} $ 表示从 $ n $ 中选取 $ k $ 的方案数[^5]。 - **快速幂运算**:当涉及到模运算时,可以利用费马小定理优化逆元的计算[^6]。 #### 题目推荐与分析 以下是一些典型的 HDU OJ 上的排列组合题目及其可能的解法: ##### 1. 基础排列组合计数 - **HDU 2039 近似数** - 描述:给定两个整数 $ a $ 和 $ b $,统计区间内的近似数数量。 - 方法:通过枚举每一位上的可能性来构建合法数字并计数[^7]。 ```cpp #include <iostream> using namespace std; long long comb(int n, int r){ if(r > n || r < 0)return 0; long long res=1; for(int i=1;i<=r;i++)res=res*(n-i+1)/i; return res; } int main(){ int t,n,k; cin>>t; while(t--){ cin>>n>>k; cout<<comb(n+k-1,k)<<endl; // 组合数应用 } } ``` ##### 2. 动态规划的应用 - **HDU 1028 Ignatius and the Princess III** - 描述:给出正整数 $ m $ 和 $ n $,问有少种方式把 $ m $ 分成最 $ n $ 份。 - 方法:定义状态转移方程 $ dp[i][j]=dp[i-1][j]+dp[i][j-i] $ 来表示当前总和为 $ j $ 并分成至 $ i $ 份的情况数目[^8]。 ```cpp #include<bits/stdc++.h> using namespace std; const int MAXN=1e3+5; long long c[MAXN][MAXN]; void init(){ memset(c,0,sizeof(c)); 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])%(1e9+7); } } int main(){ init(); int T,m,n; scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); printf("%lld\n",c[m+n-1][min(m,n)]); } } ``` #### 总结 针对不同类型的排列组合问题,可以选择合适的工具和技术加以应对。无论是简单的直接计算还是复杂的动态规划模型,都需要扎实的基础知识作为支撑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值