zoukankan      html  css  js  c++  java
  • HDU 6143 17多校8 Killer Names(组合数学)

    题目传送: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 (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。求可以取多少个不重复的名字。

    题解:一开始的思路:姓里面放i个字符,就是i^n;名里面还可以选m-i个字符,就是(m-i)^n;再乘上组合数,答案就是sum(C(m,i)*i^n*(m-i)^n),i∈[1,m]。

    上面那个就是公式,写几个后会发现,姓里面有重复计算的部分,要减去这一部分。

    dp[i]:m里面取i个放在姓中,这i个都必须出现(i^n包含了出现小于i个字符的情况)。

    比如dp[3]=3^n-C(3,2)*(2^n-C(2,1)*1^n)-C(3,1)*1^n。这里好好理解一下,是去重)。

    //即可取三个字符的情况 - 可取两个字符的情况 - 可取一个字符的情况,只剩下必须用三个字符的情况

    上式转化就是:dp[3]=3^n-C(3,2)*dp[2]-C(3,1)*dp[1]。

    所以有递推方程:

    dp[i]=i^n-C(i,i-1)*dp[i-1]-C(i,i-2)*dp[i-2]-...-C(i,1)*dp[1]

    答案就是sum(C(m,i)*dp[i]*(m-i)^n),i∈[1,m](组合数*姓*名)。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<string>
     6 using namespace std;
     7 const int mod = 1e9+7;
     8 const int maxn=2005;
     9 long long dp[maxn];
    10 long long c[maxn][maxn];
    11 void init()
    12 {
    13     memset(c,0,sizeof(c));
    14     for(int i=1;i<maxn;i++)
    15     {
    16         c[i][0]=1;c[i][i]=1;
    17         for(int j=1;j<i;j++)//杨辉三角的应用
    18             c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
    19     }
    20 }
    21 long long quickmod(long long a,long long b,long long m) 
    22 { 
    23     long long ans = 1; 
    24     while(b)//用一个循环从右到左遍历b的所有二进制位 
    25     { 
    26         if(b&1)//判断此时b[i]的二进制位是否为1 
    27         { 
    28             ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m 
    29             b--;//把该为变0 
    30         } 
    31         b/=2; 
    32         a = a*a%m; 
    33     } 
    34     return ans; 
    35 }
    36 int main()
    37 {
    38     int T,n,m;
    39     scanf("%d",&T);
    40     init();
    41     while(T--)
    42     {
    43         scanf("%d%d",&n,&m);
    44         memset(dp,0,sizeof(dp));
    45         //求dp
    46         for(int i=1;i<=m;i++)
    47         {
    48             dp[i]=quickmod(i,n,mod);
    49             for(int j=1;j<i;j++)
    50             {
    51                 dp[i]=((dp[i]-c[i][j]*dp[j])%mod+mod)%mod;//如果只是单纯%mod会WA 
    52             }
    53         }
    54         //求结果
    55         long long ans=0,tmp;
    56         for(int i=1;i<=m;i++)
    57         {
    58             tmp=(c[m][i]*dp[i]/*姓部分*/)%mod;
    59             ans+=(tmp*quickmod(m-i,n,mod)/*名部分*/)%mod;
    60             ans%=mod;
    61         }
    62         printf("%lld
    ",ans);
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    编译类与解释类语言区别
    计算机核心/系统/python运行
    Python_报错:TypeError: write() argument must be str, not int
    Python_报错:ValueError: binary mode doesn't take an encoding argument
    Python_报错:EOFError: Ran out of input
    Linux安装Redis报错`cc:命令未找到`
    Linux(CentOS-8)安装MySQL8.0.11
    linux安装MySQL报 error while loading shared libraries: libtinfo.so.5 解决办法
    SSM整合大体步骤
    JSON数据显示在jsp页面上中文乱码的解决办法
  • 原文地址:https://www.cnblogs.com/Annetree/p/7418246.html
Copyright © 2011-2022 走看看