题目传送: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
 
以下题意和题解摘自此博客。

题意:有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](组合数*姓*名)。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int mod = 1e9+;
const int maxn=;
long long dp[maxn];
long long c[maxn][maxn];
void init()
{
memset(c,,sizeof(c));
for(int i=;i<maxn;i++)
{
c[i][]=;c[i][i]=;
for(int j=;j<i;j++)//杨辉三角的应用
c[i][j]=(c[i-][j-]+c[i-][j])%mod;
}
}
long long quickmod(long long a,long long b,long long m)
{
long long ans = ;
while(b)//用一个循环从右到左遍历b的所有二进制位
{
if(b&)//判断此时b[i]的二进制位是否为1
{
ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m
b--;//把该为变0
}
b/=;
a = a*a%m;
}
return ans;
}
int main()
{
int T,n,m;
scanf("%d",&T);
init();
while(T--)
{
scanf("%d%d",&n,&m);
memset(dp,,sizeof(dp));
//求dp
for(int i=;i<=m;i++)
{
dp[i]=quickmod(i,n,mod);
for(int j=;j<i;j++)
{
dp[i]=((dp[i]-c[i][j]*dp[j])%mod+mod)%mod;//如果只是单纯%mod会WA
}
}
//求结果
long long ans=,tmp;
for(int i=;i<=m;i++)
{
tmp=(c[m][i]*dp[i]/*姓部分*/)%mod;
ans+=(tmp*quickmod(m-i,n,mod)/*名部分*/)%mod;
ans%=mod;
}
printf("%lld\n",ans);
}
return ;
}

HDU 6143 17多校8 Killer Names(组合数学)的更多相关文章

  1. HDU 6140 17多校8 Hybrid Crystals(思维题)

    题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...

  2. HDU 6045 17多校2 Is Derek lying?

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  3. HDU 6124 17多校7 Euler theorem(简单思维题)

    Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...

  4. HDU 3130 17多校7 Kolakoski(思维简单)

    Problem Description This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This seq ...

  5. HDU 6038 17多校1 Function(找循环节/环)

    Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. D ...

  6. HDU 6034 17多校1 Balala Power!(思维 排序)

    Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...

  7. HDU 6103 17多校6 Kirinriki(双指针维护)

    Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n− ...

  8. HDU 6098 17多校6 Inversion(思维+优化)

    Problem Description Give an array A, the index starts from 1.Now we want to know Bi=maxi∤jAj , i≥2. ...

  9. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

随机推荐

  1. Navicat 的安装及破解

    本地环境: ubutun 14 1.安装. ①先老实选择官方试用版安装(不推荐在各个下载平台去下载) 中文版:http://www.navicat.com.cn/download/navicat-fo ...

  2. Oracle 用户,角色,权限等

    权限管理是 Oracle 系统的精华,不同用户登录到同一数据库中,可能看到不同数量的表,拥有不同的权限.Oracle 的权限分为系统权限和数据对象权限,共一百多种,如果单独对用户授权,很囧,有一些用户 ...

  3. 美团点评MySQL数据库高可用架构从MMM到MHA+Zebra以及MHA+Proxy的演进

    本文介绍最近几年美团点评MySQL数据库高可用架构的演进过程,以及我们在开源技术基础上做的一些创新.同时,也和业界其它方案进行综合对比,了解业界在高可用方面的进展,和未来我们的一些规划和展望. MMM ...

  4. python中RabbitMQ的使用(工作队列)

    消息可以理解为任务,消息发送者可以看成任务派送者(sender),消息接收者可以看成工作者(worker). 当工作者接收到一个任务,还没完任务时分配者又发一个任务,此时需要多个工作者来共同处理这些任 ...

  5. Python条件判断和循环,range()函数

    条件判断经常使用if语句进行判断,表达方式为:if 条件语句:      :elif:else if...用于执行第一条不满足if的判断,继续执行其它的判断.比如一个简单的if判断 Python3取消 ...

  6. Python---字典常用方法总结

    字典是一种key-value的数据类型,字典里必须写Key和value,字典的优点是取数方便和速度快.字典的特性: 1.字典是无序的,因为它没有下标,用key来当索引,所以是无序的 2.字典的key必 ...

  7. [luogu P1438] 无聊的数列

    [luogu P1438] 无聊的数列 题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个 ...

  8. sigmoid belief network boltszmann machine

    because of explaining away, the hidden weights in sigmoid belief network is no longer independent

  9. zabbix3.4.7主动模式监控日志(多关键字)

    日志监控原理 1.Zabbix Server和Zabbix Agent会追踪日志文件的大小和最后修改时间,并且分别记录在字节计数器和最新的时间计数器中. 2.Agent会从上次读取日志的地方开始读取日 ...

  10. POJ 1088 滑雪(记忆化搜索+dp)

    POJ 1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 107319   Accepted: 40893 De ...