2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
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的字符串,要求这两个字符串中不含有相同的字符。
求有多少种方式组成这两个字符串。
分析:思路一,容斥+排列组合
利用容斥的思想来计算一下下面的式子:
定义:f[n][i]表示长度为n的字符串用i种字符填充的方法数。
f[n][1]=1
f[n][2] = 2^n - C(2,1)f[n][1]; 两种的所有填充方式-一种的填充方式。
f[n][3] = 3^n - C(3,1)f[n][1] - C(3,2)f[n][2];...
...
f[n][m] = m^n - sigma[1<=i<m]C(m,i)f[n][i];
那么可以枚举左边这个n长度字符串的组合方式用去i种字符,那么剩下那个字符串用剩下的字符任意组合即可。
注意m大于n的情况。以及在我们求C(m,i)的时候,这个式子可以转换为m!/(i!*(m-i)!),这里涉及到除法的操作,这样的话可能在除的过程中会造成精度的丢失,所以我们将除数转换为它们本身的逆元。又因为取余的数只一个质数,可以直接用费马小定理求逆元。
代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2005;
const int mod = 1e9 + 7;
LL fac[N], inv[N],f[N][N],ans;
LL Pow(LL a,LL b)///整数快速幂
{
LL ans = 1;
while(b)
{
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
}
void init()
{
fac[0] = 1;
for(int i = 1; i < N; i++)
fac[i] = fac[i-1]*i%mod;///i!
inv[0] = inv[1] = 1;
for(int i = 2; i < N; i++)
{
inv[i] = Pow(fac[i],mod-2);///i!的逆元,因为后期求C(n,m)的时候要用到相除,可能会丢失精度,所以将除数转换为本身的逆元
}
}
LL C(int n,int m)///n!/(m!*(n-m)!)
{
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
void solve(int n,int m)
{
for(int i = 1; i <= min(n,m); i++)
{
f[n][i] = Pow(i,n);///最开始的时候是i^n
for(int j = 1; j < i; j++)///逐渐减去C(i,j)*f[n][j]
{
f[n][i] = (f[n][i]-f[n][j]*C(i,j)%mod+mod)%mod;
}
}
}
int main()
{
int T;
int n, m;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
solve(n,m);
ans = 0;
int mis;
if(m>n) mis = n;
else mis = m-1;
for(int i = 1; i <= mis; i++)
{
ans = (ans+C(m,i)*f[n][i]%mod*Pow(m-i,n)%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
思路二:dp递推
设定义:dp[i][j],前i个位置使用j种字符的方案数,dp在求解的过程中就已经不存在相互矛盾的情况了。
当前位置,只考虑使用使用过的j种 或者 考虑没有使用过的 m-j种
n*n的转移
前半部分用了j种, 剩下 的 m-j 全用在 后半部分,快速密 乘起来 累加就是ans
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 2010;
ll dp[maxn][maxn];
ll quick_pow(ll a, ll b)///整数快速幂
{
ll ans = 1;
while (b)
{
if (b & 1)
ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
dp[1][1]=m;///第一个位置放置一种字符的方案数有且仅有一个
for(int i=2; i<=n; i++)///从第二个位置遍历到第n个位置
{
for(int j=1; j<=i&&j<=m; j++)///前i个位置可以选择放的不同字符的个数最少为1个,最多为i个(每个位置上的字符都不一样)
{
/*
要使前i个位置上有j个不同的字符,考虑两种方案数
1.前i-1个位置上就已经有了j个不同的字符了,那么第j个位置上就可以从这j个字符中任意选择一个
2.前i-1个位置上只有j-1个不同的字符,那么第j个位置上的字符就不能选择已经选过的j-1个字符了,
需要从剩余的(m-(j-1))个字符里面任意的选择一个
这两种方案数共同构成
*/
dp[i][j]=(dp[i-1][j]*j%mod+dp[i-1][j-1]*(m-j+1)%mod)%mod;
}
}
/*
dp求出来的只是组成姓这n个字符的不同的方案数,我们还要考虑组成名的不同的方案数
组成名的字符要从剩余的(m-j)个字符里面选择,每个字符都对应着n中摆放位置,两两组合的方案数肯定是乘的关系
最后将求得的结果累加
*/
ll ans=0;
for(int j=1; j<m; j++)
{
ans=(ans+dp[n][j]*quick_pow(m-j,n)%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 6 1011 HDU 6106 Classes (容斥公式)
题目链接 Problem Description The school set up three elective courses, assuming that these courses are A ...
- 2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)
题目链接 Problem Description You are given an array A , and Zhu wants to know there are how many differe ...
- 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)
题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...
- 2017ACM暑期多校联合训练 - Team 1 1011 HDU 6043 KazaQ's Socks (找规律)
题目链接 Problem Description KazaQ wears socks everyday. At the beginning, he has n pairs of socks numbe ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
随机推荐
- Django之ORM其他骚操作
Django ORM执行原生SQL # extra # 在QuerySet的基础上继续执行子语句 # extra(self, select=None, where=None, params=None, ...
- Delphi定位TDataSet数据集最后一条记录
dst_temp.last ;//最后一条dst_temp.first ;//第一条dst_temp.next ;//下一条dst_temp.prior;//上一条
- ADB抓取日志和日志过滤
建议抓取日志之前先清除缓存的log数据:adb logcat -c 网上有人介绍可以这样写:adb logcat -c && adb logcat 1.使用V.D.I.W.E.F.S优 ...
- 打印实例对象的名字 默认调用父类的toString 可重写
- 51nod 1804 小C的多边形(构造)
首先可以算出无解的充分不必要条件,所有边的和为sum=3*((n-1)*n)/2,如果sum%n!=0显然无解. 也就是说n为奇数必然无解.现在考虑n为偶数的情况. 不妨假设n为偶数有解,现在考虑如何 ...
- P3984 高兴的津津
题目描述 津津上高中了.她在自己的妈妈的魔鬼训练下,成为了一个神犇,每次参加一次OI比赛必拿Au虐全场.每次她拿到一个Au后就很高兴.假设津津不会因为其它事高兴,并且她的高兴会持续T天(包包含获奖当天 ...
- pbuilder编译构建工具分析
1. 简介 pbuilder(personal Debian package builder)是ubuntu环境下维护debian包的专业工具,能够为每个deb包创建纯净的编译构建环境,自动解析和安装 ...
- 【刷题】HDU 1695 GCD
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...
- Redis的Set无序集合命令
Set是集合,它是string类型的无序集合.set是通过hash table实现的,添加.删除和查找的复杂度都是0(1).对集合我们可以取并集.交集.差集.通过这些操作我们可以实现sns中的好友推荐 ...
- Linux内核设计与实现第六周读书笔记
第三章 进程管理 3.1 进程 进程是处于执行期的代码.通常进程还要包含其他资源,像打开的文件.挂起的信号.内核的内部数据.处理器状态.一个或多个具有内存映射的内存地址空间及一个或多个执行线程,当然还 ...