题目来戳呀

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

Source

2017 Multi-University Training Contest - Team 8

题意

famliy name 和 last name长度各为n,现有m种字符供选择,规定famliy name 和 last name中不能有一样的字符。
如:aac bbb可以,但aab acc不可以。

想法

  1. 首先 ,不考虑重复,全部的情况是CimC_m^iini^n(m−i)n(m-i)^n *→**first name每个位子有i种可能的字符,last name每个位子有m-i种可能的字符;

  2. 然鹅会有重复的,举个栗子:
    可能在family name能拿a和b,last name拿剩下的其他时,出现family name只出a的情况 比如a a a c c c
    这和之前family name只拿a没别的出 重复了 因为这样也可以出现a a a c c c

  3. 为了解决去重 我们考虑用容斥定理
    假设有j种字符可能在前面出现过,记为∑i−1j=1(−1)i−jCji\sum_{j=1}^{i-1}(-1)^{i-j}C_i^jjnj^n(还是滚去好好看看容斥定理吧+_+)

  4. 最后用去重过的family name的总数*last name能拿的情况就是总结果了。(我他瓜竟然在这里被绕住了>_<)
    所以总的公式就是(CimC_m^iini^n+∑i−1j=1(−1)i−jCji\sum_{j=1}^{i-1}(-1)^{i-j}C_i^jjnj^n)*(m−i)n(m-i)^n

对着公式就可以愉快地敲代码了~虽然不知道为什么我用快速幂求n次方时tle了TAT

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=2100;
ll c[maxn][maxn],p[maxn][maxn];
int t,n,m;
void Combination()///求组合数
{c[0][0]=1;for(int i=1;i<maxn;i++){c[i][0]=c[i][i]=1;for(int j=1;j<i;j++){c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;}}
}
void mi()///求幂
{for(int i=1;i<maxn;++i){p[i][0]=1;for(int j=1;j<maxn;++j)p[i][j]=p[i][j-1]*i%mod;}
}
ll solve()
{ll ans=0,all;for(int i=1;i<m;++i){all=p[i][n];int tmp=0;for(int j=i-1;j>0;--j){if(tmp==0)///偶数次{all=(all-c[i][j]*p[j][n]%mod+mod)%mod;///这里要+mod,因为可能为负}else///奇数次{all=(all+c[i][j]*p[j][n]%mod)%mod;}tmp=1-tmp;}ans=(ans+c[m][i]*all%mod*p[m-i][n]%mod)%mod;}return ans;
}
int main()
{scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);Combination();mi();printf("%lld\n",solve());}return 0;
}

ps:去问在打排位的小班长,果不其然被骂了QAQ
不过鉴于没有打死我,还是遥远的给您笔芯(づ ̄3 ̄)づ╭❤~

但是真的感觉组合知识全都还给涛涛了o(╯□╰)o

HDU 6143 Killer Names【容斥定理】【排列组合】相关推荐

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

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

  2. Killer Names( 容斥定理,快速幂 )

    题目大意: m种不同颜色的球,填两组盒子,每组盒子有n个,两组盒子中不能有相同颜色的球,问方法总数有多少. 解题思路:利用容斥定理,m种颜色放入n个格子共有m^n个,但其中肯定有不满足m种颜色的,所以 ...

  3. HDU 6143 Killer Names(排列+容斥,dp)

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

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

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

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

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

  6. hdu 1796 How many integers can you find 容斥定理

    一开始看 这里 这个文章博主写得很好. 当举容斥定理的所谓 奇数为负 偶数为正的时候. 我直接就认为是 a*b 了.实际上是lcm(a,b). 由于博文中的因子都是互素的(素数之间).所以lcm(a, ...

  7. 二项式反演(广义容斥定理)学习笔记

    背景: 二项式反演又名广义容斥定理. 下次再看题解时要注意了. 解锁新姿势. 说白了就是之前会的姿势太少了. 正题: 说白了就是有这样两条恒等的式子: f n = ∑ i = 0 n ( − 1 ) ...

  8. UVA-10212 The Last Non-zero Digit. 分解质因子+容斥定理

    这个是参考了别人之后的代码,POJ上0MS过了.Orz......对于一个序列在提取了2,5之后,例如1,2,3,4,5,6,7,8,9,10,我们可以将其中的奇数和偶数分开来对待,对于偶数序列2,4 ...

  9. 最简真分数c语言,HihoCoder1655 : 第K小最简真分数([Offer收割]编程练习赛39)(唯一分解+容斥定理+二分)(不错的数学题)...

    描述 给一个整数N,请你求出以N为分母的最简(既约)真分数中第K小的是多少? 输入 两个整数N个K. 对于30%的数据,1 <= N <= 1000000 对于100%的数据,1 < ...

最新文章

  1. springboot filter and interceptor实战之mdc日志打印
  2. 1402 后缀数组 (hash+二分)
  3. 偷懒的inline-block解决方法
  4. Windows Mobile常用程序代码(串口、图象、网络、3D、数据库、音频视频等等)
  5. 【2016 Asia China-Final D题】
  6. 成功解决RuntimeError: Java is not installed, or the Java executable is not on system path
  7. 愿只有一个Grid Layout
  8. 长安大学研究生院计算机学院,研究生教育
  9. python绘制横向堆积柱状图_Python 堆叠柱状图绘制方法
  10. 前端学习(2628):node.js中LTS和Current的区别
  11. Linux / Windows应用方案不完全对照表
  12. 决策树的选择,哪个放在第一个需要决策的环节
  13. PYPL 11 月的 IDE 指数榜单
  14. Laravel 的 Redis 使用指南
  15. matlab中产生对角阵,关于matlab中的diag函数(矩阵对角元素的提取和创建对角阵)
  16. 易语言:游戏辅助 CF队伤“卡秒器“ 编程思路/开发者优化建议
  17. 实战腾讯云ORC文字识别
  18. cygwin使用apt-cyg
  19. 201871010134-周英杰《面向对象程序设计(java)》第一周学习总结
  20. 2016天津成考计算机试题,2016年天津事业单位考试《职业能力测验》(部分)试题及答案解析...

热门文章

  1. 破晓博客-自定义标签的开发
  2. python menu_Python——Menu控件
  3. 让windows保持常亮(不息屏,不锁屏,不进入屏保)
  4. 微信小程序--实现按钮跳转另一个页面
  5. python给女朋友_Python 给女朋友道歉的一天
  6. 两个方法做APP界面展示图片
  7. 【知识图谱】本周文献阅读笔记(3)——周二 2023.1.10:英文)知识图谱补全研究综述 + 网络安全知识图谱研究综述 + 知识图谱嵌入模型中的损失函数 + 图神经网络应用于知识图谱推理的研究综述
  8. linux nc传输目录文件,linux nc 命令传输文件
  9. 利用ChatGPT做Prompt自动优化
  10. 吸烟行为检测系统(Python+YOLOv5深度学习模型+清新界面)