HDU 6143 17多校8 Killer Names(组合数学)
题目传送:Killer Names
>
> 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.
Each test case contains two integers n and m (1≤n,m≤2000).
Output the answer mod 109+7
题意:有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(组合数学)的更多相关文章
- HDU 6140 17多校8 Hybrid Crystals(思维题)
题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...
- HDU 6045 17多校2 Is Derek lying?
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- HDU 6124 17多校7 Euler theorem(简单思维题)
Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...
- 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 ...
- 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 ...
- HDU 6034 17多校1 Balala Power!(思维 排序)
Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...
- 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− ...
- 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. ...
- HDU 6106 17多校6 Classes(容斥简单题)
Problem Description The school set up three elective courses, assuming that these courses are A, B, ...
随机推荐
- java骰子求和算法
//扔 n 个骰子,向上面的数字之和为 S.给定 Given n,请列出所有可能的 S 值及其相应的概率public class Solution { /** * @param n an intege ...
- WDA基础十三:常用模板管理
常用的模板一般是SMW0和OAOR,根据不同需求来的. WAD有个不好的地方就是不支持GUI上的OLE和DOI,所以需要做转换,下面是常用的方式: FUNCTION ZCRM_DOWNLOAD_TEM ...
- java前后向查找个人理解
举一个最简单的栗子 这个前后说的是0宽所在的位置,是在:前还是后 http://www.sb.com 1.前向正向查找 (1) 如果用:.*(?=:) 首先(?=:)被称作0宽度断言,所谓0宽度应该是 ...
- 一、JAVA内存区域与内存溢出异常
在虚拟机自动内存管理机制的帮助下,不在需要为每一个操作区写相对应的delete/free代码来进行内存释放.进而不容易出现内存泄露和内存溢出的问题,由虚拟机管理内存,貌似这一切看起来很好.也正是因为j ...
- 解决QPainter::drawText修改文字方向
今天在绘制双坐标曲线的时候需要修改y轴文字提示 QPainter的drawText()函数提供了绘制文本的功能. 它有几种重载形式,我们使用了其中的一种,即制定文本的坐标然后绘制 正常我们的文字书写方 ...
- windows 下的命令操作
删除文件夹 RD /S D:\aaaaa 删除文件夹下的文件 DEL D:\aaaaa\*.*
- Mysql计算并保留两位小数
如:123456.789 转成 123456.79 自动,));
- 在用mybatis向MySQL数据库中插入时间时报错:Incorrect datetime value: '' for column '' at row 1
问题说明:使用的MySQL是5.1.37版本,用的mysql-connector-java-5.0.4.jar版本,在java文件中定义的字段是Date类型,MySQL中定义的字段类型是datetim ...
- python+ajaxFileUpload 无刷新上传文件
需要准备文件 http://pan.baidu.com/s/1bp4N3nL qqi0 html <script src="{% static 'js/jquery.js' %}& ...
- linux下free命令详解
free 命令显示系统内存的使用情况,包括物理内存.交换内存(swap)和内核缓冲区内存. 如果加上 -h 选项,输出的结果会友好很多: 有时我们需要持续的观察内存的状况,此时可以使用 -s 选项并指 ...