Killer Names

HDU 6143 (容斥+排列组合,dp+整数快速幂) 2017ACM暑期多校联合训练 - Team 8 1011 Killer Names

题目链接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1107 Accepted Submission(s): 545

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的字符串,要求这两个字符串中不含有相同的字符。

求有多少种方式组成这两个字符串。

dp求解
嘿嘿 在学姐博客复制过来的 学姐 博客
还是dp代码短呀,就是不好想。

#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;
}

分析:
容斥+排列组合

#include <bits/stdc++.h>
#define siz 1005
#define maxn 2000
const long long  mod = 1e9 + 7;
typedef long long LL;
using namespace std;
int n, m;
LL deposit[maxn + 5][maxn + 5], f[maxn + 5], comd[maxn + 5][maxn + 5];
void Init()
{for(LL i = 1; i <= maxn; i++){deposit[i][0] = 1;for(int j = 1; j <= maxn; j++)deposit[i][j] = deposit[i][j - 1] * i % mod;///deposit[i]保存i的i的阶乘}for(int i = 0; i <= maxn; i++){comd[i][0] = 1;for(int j = 1; j <= i; j++)comd[i][j] = (comd[i - 1][j] + comd[i - 1][j - 1]) % mod;///Comd[i]保存i的从i里面取j个的排列组合数的个数}
}
void solve()
{LL sum = 0;memset(f, 0, sizeof(f));for(int i = 1; i <= m && i <= n; i++){sum = 0;for(int j = 1; j <= i; j++){sum = (sum + f[j] * comd[i][j] % mod) % mod; }f[i] = (deposit[i][n] - sum + mod) % mod;}LL ans = 0;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(i + j > m) break;ans = (ans + (((f[i] * f[j]) % mod) * ((comd[m][i] * comd[m - i][j]) % mod)) % mod) % mod;///组合的方法}}printf("%lld\n", ans);
}
int main()
{int T;Init();scanf("%d", &T);while(T--){scanf("%d %d", &n, &m);solve();}return 0;
}

转载于:https://www.cnblogs.com/nanfenggu/p/7900045.html

HDU 6143 Killer Names(排列+容斥,dp)相关推荐

  1. HDU 6143 Killer Names【容斥定理】【排列组合】

    题目来戳呀 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  2. HDU 6143 Killer Names(容斥+组合)

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

  3. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)...

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  4. HDU 6143 Killer Names (组合数学+DP)

    Description 字母表的长度为\(m\),用表中的字母构造长度为\(2n\)的字符串,要求同一种字母能同时出现在前\(n\)个字符中和后\(n\)个字符中.输出方案数,结果模\(10^9+7\ ...

  5. LOJ#3124. 「CTS2019 | CTSC2019」氪金手游 容斥+DP

    神仙容斥+DP可还行. code: #include <cstdio> #include <cmath> #include <vector> #include &l ...

  6. 840C - On the Bench (容斥 + DP)

    840C - On the Bench 题意 给定序列 a[1...n]a[1...n]a[1...n],求有多种 aaa 的排列,满足任意两个相邻的数乘积不是完全平方数,答案对 109+710^9+ ...

  7. 洛谷P3349:小星星(容斥dp)

    解析 先安利一波洛谷上我介绍如何用暴力日过去的博客 现在开始务正业 考虑把dp记录状态的一维s去掉 这样单次转移复杂度变成n3n^3n3 但是这样显然会算多啊! 因为一个编号可能会用很多次 考虑容斥 ...

  8. 牛客练习赛71C-数学考试【容斥,dp】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/7745/C 题目大意 求一nnn的排列,给mmm个限制pip_ipi​表示1∼pi1\sim p_i1∼pi​ ...

  9. P4707 重返现世 扩展 MinMax 容斥+DP

    题目传送门 https://www.luogu.org/problem/P4707 题解 很容易想到这是一个 MinMax 容斥的题目. 设每一个物品被收集的时间为 \(t_i\),那么集齐 \(k\ ...

最新文章

  1. Java 正则表达式使用详解
  2. tensorflowgpu利用率为0_直流电压利用率的提高方法-梯形波调制法
  3. vmware esxi主机经常出现的警示“vsphere distributed switch mtu支持状态, vlan中继状态”求大神回复。
  4. idea 初始界面_IDEA 初始配置教程
  5. android 自定义加载圈,Android自定义加载控件实现数据加载动画
  6. 下载 SharePoint Server 2016
  7. 下载的jar包找不到源码时从这里下载
  8. 【律联云知产课堂】商标注册需要什么条件?
  9. linux识别riser卡,一种具有多接口的Riser卡的制作方法
  10. 微信小程序怎么开发自己的小程序?
  11. 【全教程】qt连接mysql——从qt编译mysql驱动到qt连接mysql数据库(一、编译连接前准备)
  12. SSM框架二手车交易网站源码+论文
  13. android 关机 流程_android 关机 流程分析
  14. 区块链共识机制:分布式系统的Paxos协议
  15. 巴什博奕(Bash Game)入门
  16. vray 用于室内渲染的10大技巧,看进来!
  17. [javascript] 深入理解js闭包
  18. win10 用户设置cmd 管理员权限
  19. 利用程序计算2+22+222+2222+22222的和
  20. 网络安全笔记-POC与EXP的Python实现

热门文章

  1. C++ Time类重载运算符
  2. STC89C52单片机 点亮两个数码管,可以显示 00——10 共 11 个数字
  3. 远程登录的机器不是域控制器的方法
  4. mysql -- 学习记录
  5. python标准模块--os
  6. QLable显示图片 和 QLabel自适应 QLabel 文字居中
  7. android 应用间共享数据,调用其他app数据资源
  8. Cocos2dx-demo演示项目:Part1
  9. Vue组件通信的7个方法
  10. 技术分享连载(六十)