HDU 6143 排列组合
来源:互联网 发布:c语言中结构体的使用 编辑:程序博客网 时间:2024/06/08 16:12
Killer Names
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 180 Accepted Submission(s): 96
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 exactlyn 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.
>
> 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
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 integersn and m (1≤n,m≤2000 ).
Each test case contains two integers
Output
For each test case, output one line containing the maximum number of clones Vader can create.
Output the answer mod 109+7
Output the answer
Sample Input
23 22 3
Sample Output
2 18
简单来说就是把字母 组合分堆 后,两个堆进行排列。
一开始我想的是, 总共有 2 * n 个位置去填,那么总共就有 pow(m, 2 - n) 种方法,然后从中删去不符合要求的
于是我计算出了每个字母不符合的情况,所有字母的不符合情况数都是一样,但是又发现,这些不符合的情况里有重复的案例被多算了,又非常地难容斥。
新思路是只对其中一堆进行考虑,这一堆使用了 x 个字母,那么另一堆就随便使用剩下的字母就可以了。
用 一个数组记录其中一堆使用 i 个字母的时候的(排列)种数 令F[i] = pow(i,n) , F[1] 固然是 = 1 的
那么 对于一堆X为 1 的时候的情况数就是 1 * C[m][1] (选出这一个数) 剩下的另一堆就是 pow(m - 1,n)
那么对于 F[2] 呢? 我们发现 F[2]中使用 2 个数排列的时候包括了 F[1]的情况
例如 数字 1 和 2 在 n = 2 的时候可以有 11 12 21 22 其中 11 和 22 是属于 F[1]的,所以要减去它
所以 F[i] 要减去 F[j] * C[i][j] ( j >= 1 && j < i)
然后对于每个 F[i] ans += ( F[i] * C[m][i] * pow(m - i,n) )
做的时候打个组合数表,再用个快速幂就可以了
#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<map>using namespace std;#define ll long long#define mod 1000000007#define maxn 2010ll c[maxn][maxn];void init() {memset(c,0,sizeof(c));for(ll i = 0; i <= 2005; i++) {c[i][0] = c[i][i] = 1;for(ll j = 1; j < i; j++)c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;}}ll Pow(ll a,ll b) {ll tmp = a;ll ans = 1;while(b) {if(b & 1) {ans *= tmp;ans %= mod;}tmp *= tmp;tmp %= mod;b >>= 1;}return ans;}ll cas[maxn];int main() {init();int t;ll n,m,ans;scanf("%d",&t);while(t--){scanf("%lld %lld",&n,&m);ans = 0;for(ll i = 1;i < m;i++){if(i > n)// 字母种数大于空间个数的时候没必要去算了 break;cas[i] = Pow(i,n);//首先把所有情况列出 for(ll j = 1;j < i;j++){ //对于当前数量的字母数去排列组合而言,它会多排了前面所有的情况 cas[i] -= (cas[j] * c[i][j]); //而当前的 i 种字母中要进行组合 然后去除 cas[i] %= mod;}ans += ((cas[i] * c[m][i]) % mod * Pow(m - i,n)) % mod;// 只考虑一边的,另一半的剩下的字母随便用 ans %= mod;}ans = (ans + mod) % mod;printf("%lld\n",ans);}return 0;}
阅读全文
1 0
- HDU 6143 排列组合
- HDU 6143 排列组合
- hdu 1521 排列组合
- HDU 1521 排列组合
- hdu 1521 排列组合
- hdu 1521 排列组合 组合
- hdu 1261(排列组合)
- hdu 2047 排列组合
- ACM~排列组合&&hdu样例
- HDU 1521 排列组合
- hdu 5366 排列组合
- HDU 1799 排列组合
- hdu 5366(排列组合)
- hdu 5155(DP+排列组合)
- HDU 1521排列组合
- HDU 6143 Killer Names【容斥定理】【排列组合】
- 2017多校八 1011题 hdu 6143 Killer Names 排列组合
- 多校8 HDU-6143 Killer Names 排列组合&斯特灵数
- RxVolley源码学习
- Javacollection
- 两个有序链表合并
- multi-university 第八场 经验总结
- Spring和jedis整合出现的问题
- HDU 6143 排列组合
- DOM事件处理跨浏览器(兼容IE)封装
- 树莓派实现远程监控
- hdu 6143 组合数学+dp
- ImmutableMap
- 字符串冒泡排序
- hdu 6143 (2017多校联赛8-11)
- 十个MySQL 数据库经典错误
- 【DP】POJ_3616_Milking Time