题目大意:给定一个长度为 N 的序列,现有 M 种颜色,选出一些颜色对这个序列进行染色,要求相邻元素颜色不相同。求最终序列恰好有 K 种不同颜色的染色方案数,结果对1e9+7取模。

题解:二项式反演

代码如下

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const LL mod = 1e9 + 7;
const int maxn = 1e6 + 10; LL fpow(LL a, LL b) {
LL ret = 1 % mod;
for (; b; b >>= 1, a = a * a % mod) {
if (b & 1) {
ret = ret * a % mod;
}
}
return ret;
} LL fac[maxn], ifac[maxn]; void prework() {
int n = 1e6;
fac[0] = fac[1] = 1;
for (int i = 2; i <= n; i++) {
fac[i] = fac[i - 1] * i % mod;
}
ifac[n] = fpow(fac[n], mod - 2);
for (int i = n - 1; i >= 0; i--) {
ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
}
inline LL comb1(int x, int y) {
if (y > x) {
return 0;
}
return fac[x] * ifac[x - y] % mod * ifac[y] % mod;
}
inline LL comb2(int x, int y) {
if (y > x) {
return 0;
}
LL ret = 1;
for (int i = x; i >= x - y + 1; i--) {
ret = ret * i % mod;
}
return ret * ifac[y] % mod;
} int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
prework();
int T, kase = 0;
cin >> T;
while (T--) {
int n, m, K;
cin >> n >> m >> K;
auto f = [&](int x) {
LL ret = x;
return ret * fpow(x - 1, n - 1) % mod;
};
LL ans = 0;
if (K == 1) {
ans = (n == 1) ? 1 : 0;
} else {
for (int i = 1; i <= K; i++) {
if ((K - i) & 1) {
ans = (ans - comb1(K, i) * f(i) % mod + mod) % mod;
} else {
ans = (ans + comb1(K, i) * f(i) % mod) % mod;
}
}
}
cout << "Case #" << ++kase << ": " << ans * comb2(m, K) % mod << endl;
}
return 0;
}

【UVALive-7040F】Color的更多相关文章

  1. 【POJ 2054】 Color a Tree

    [题目链接] http://poj.org/problem?id=2054 [算法] 贪心 [代码] #include <algorithm> #include <bitset> ...

  2. 【Uva 1625】Color Length

    [Link]: [Description] 给你两个序列,都由大写字母组成; 每次,把两个序列中的一个的开头字母加在字符串的尾端,然后在那个序列中删掉那个开头字母; 最后得到一个字符串; 这个字符串显 ...

  3. 【POJ 2154】 Color (置换、burnside引理)

    Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...

  4. 【全排列+子序列】Color

    [题意] 这个题目就是问,是否存在每个人对应每一种颜色,如果存在则输出字典序最小的. 否则输出-1 [题解] 利用next_permutation来构造36种情况.记住最后还需要排序一遍. 然后用子序 ...

  5. 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)

    [题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...

  6. 【Uvalive 5834】 Genghis Khan the Conqueror (生成树,最优替代边)

    [题意] 一个N个点的无向图,先生成一棵最小生成树,然后给你Q次询问,每次询问都是x,y,z的形式, 表示的意思是在原图中将x,y之间的边增大(一定是变大的)到z时,此时最小生成数的值是多少.最后求Q ...

  7. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  8. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...

  9. 【UVALive - 5131】Chips Challenge(上下界循环费用流)

    Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...

  10. 【UVALive - 3487】 Duopoly(网络流-最小割)

    Description The mobile network market in country XYZ used to be dominated by two large corporations, ...

随机推荐

  1. POJ 1840:Eqs

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53= The coe ...

  2. TemplateView , ListView ,DetailView三种常用类视图用法

    参考博客: https://blog.csdn.net/weixin_36571185/article/details/74280102

  3. 使用Vue自定义指令实现Select组件

    完成的效果图如下: 一.首先,我们简单布局一下: <template> <div class="select"> <div class="i ...

  4. linux系统调用、库函数和内核函数关系与区别

    看系统调用,还有库函数,以前一直不明白,总是以为 系统调用跟库函数是一样的,但是今天才知道是不一样的. 库函数也就是我们通常所说的应用编程接口API,它其实就是一个函数定义,比如常见read().wr ...

  5. 【洛谷】P4883 mzf的考验

    [洛谷]P4883 mzf的考验 最近忽然放弃治疗开始随机跳题了 感觉还行 就是必须吸氧感觉有点糟糕... 这题翻转和求和都是平衡树基本操作,那个异或可以通过维护树中\(2\)进制下第\(2^{i}\ ...

  6. php gd实现简单图片验证码与图片背景文字水印

    1.让水印文字铺满图片: 大致效果: 代码: <?php function appendSpreadTextMark($imageDir, $markText) { $fontFile = &q ...

  7. git 使用2

    安装 1.下载对应版本:https://git-scm.com/download 2.安装git:在选取安装路径的下一步选取 Use a TrueType font in all console wi ...

  8. - 多次点击事件 MD

  9. spark application调度机制(spreadOutApps,oneExecutorPerWorker 算法)

    1.要想明白spark application调度机制,需要回答一下几个问题: 1.谁来调度? 2.为谁调度? 3.调度什么? 3.何时调度? 4.调度算法 前四个问题可以用如下一句话里来回答:每当集 ...

  10. 函数——箭头函数&自执行函数(二)

    一.箭头函数是在es6中添加的一种规范,它相当于匿名函数,简化了函数的定义. 1.语法 a.function用var,let,cost来表示: b.参数要写在第一个等号后面:   参数有多个,需要加一 ...