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
 
题解:
  只考虑前半部分
  设定dp[i][j],前i个位置使用j种颜色的方案数
  当前位置,只考虑使用 使用过的j种 或者 考虑没有使用过的 m-j种
  n*n的转移
  前半部分用了j种, 剩下 的 m-j 全用在 后半部分,快速密 乘起来 累加就是ans
#include<bits/stdc++.h>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0); const int N=+,M=1e6+,inf=; const LL MOD = 1e9 + 7LL; LL quick_pow(LL x,LL p) {
if(!p) return 1LL;
LL ans = quick_pow(x,p>>);
ans = ans*ans%MOD;
if(p & ) ans = ans*x%MOD;
return ans;
} LL dp[N][N],ans,fn[N],fn1[N];
int n,m;
void init() { for(int i = ; i <= m; ++i) fn[i] = quick_pow(i,n) % MOD;
fn[] = ;
memset(dp,,sizeof(dp));
dp[][] = 1LL;
for(int i = ; i < n; ++i) {
for(int j = ; j <= i; ++j) { if(m>=j)dp[i+][j+] += dp[i][j] * (m - j) % MOD;
dp[i+][j] += dp[i][j]*j%MOD;
dp[i+][j+] %= MOD;
dp[i+][j] %= MOD;
}
}
for(int j = ; j <= n; ++j){
if(m>j)ans = (ans + (dp[n][j] * fn[m-j])%MOD)%MOD;
}
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
ans = ;
init();
printf("%lld\n",ans);
}
return ;
}

HDU 6143 Killer Names DP+快速密的更多相关文章

  1. HDU 6143 - Killer Names | 2017 Multi-University Training Contest 8

    /* HDU 6143 - Killer Names [ DP ] | 2017 Multi-University Training Contest 8 题意: m个字母组成两个长为n的序列,两序列中 ...

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

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

  3. HDU 6143 Killer Names

    Killer Names Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. HDU - 6143 Killer Names(dp记忆化搜索+组合数)

    题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的 ...

  5. HDU 6143 Killer Names(容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意: 用m个字母去取名字,名字分为前后两部分,各由n个字符组成,前后两部分不能出现相同字符,问合法的组成 ...

  6. 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...

  7. hdu 6143: Killer Names (2017 多校第八场 1011)

    题目链接 题意,有m种颜色,给2n个位置染色,使左边n个和右边n个没有共同的颜色. 可以先递推求出恰用i种颜色染n个位置的方案数,然后枚举两边的染色数就可以了,代码很简单. #include<b ...

  8. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  9. hdu6143 Killer Names 容斥+排列组合

    /** 题目:hdu6143 Killer Names 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:有m种字符(可以不用完),组成两个长度 ...

随机推荐

  1. POJ-2594 Treasure Exploration,floyd+最小路径覆盖!

                                                 Treasure Exploration 复见此题,时隔久远,已忘,悲矣! 题意:用最少的机器人沿单向边走完( ...

  2. Python之静态语法检查

    Python是一门动态语言.在给python传参数的时候并没有严格的类型限制.写python程序的时候,发现错误经常只能在执行的时候发现.有一些错误由于隐藏的比较深,只有特定逻辑才会触发,往往导致需要 ...

  3. Kubernetes对象

    Kubernetes对象 在之前的文章已经讲到了很多Kubernets对象,包括pod,service,deployment等等.Kubernets对象是一种持久化,表示集群状态的实体.它是一种声明式 ...

  4. BZOJ 4811 [Ynoi2017]由乃的OJ ——Link-Cut Tree

    直接维护按照顺序经过每一段,初始的1可以变成什么,初始为0可以变成什么. 然后答案就可以和起床困难综合征一样贪心处理了. 写起来并不好写. 发现交换左右子树之后答案会改变,GG 调了一天,最后还是T掉 ...

  5. Error处理: “非法字符: \65279”的解决办法

    将eclipse项目转为maven项目的时候,编译时遇到 “非法字符: \65279”的报错. 出错内容是: *.java:1: 非法字符: \65279    [javac] package com ...

  6. linux 抓取访问量排行

    需求: 分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小 语句: awk '{a[$1]+=$10;}END{for(i in a){p ...

  7. Linux,以逗号为分隔符,打印文件file.txt中的第一个和第三个字符

    https://zhidao.baidu.com/question/1883257355267391828.html

  8. Codeforces 837D Round Subset(背包)

    题目链接  Round Subset 题意  在n个数中选择k个数,求这k个数乘积末尾0个数的最大值. 首先我们预处理出每个数5的因子个数c[i]和2的因子个数d[i] 然后就可以背包了. 设f[i] ...

  9. 洛谷P1061 Jam的计数法

    题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...

  10. 前端和后端采用接口访问时的调用验证机制(基于JWT的前后端验证)(思路探讨)

    说明:基于前后端,尤其是使用Ajax请求的接口,现在市面上网页上调用的Ajax基本都是没有验证的,如果单独提取之后可以无线的刷数据. 继上一篇http://www.cnblogs.com/EasonJ ...