题目大意:给定一个长度为 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. Spring switch的使用

    首先在html开始标签中引入一个属性 1 xmlns:th="http://www.thymeleaf.org" 示例代码 <div th:switch="${us ...

  2. Linux下配置APUE的编译 报错之后如何处理

    APUE即Unix环境高级编程,本书中几乎所有的程序都包含一个apue.h的头文件,那如何配置这个apue.h呢? 官方地址:http://www.apuebook.com/apue3e.html 1 ...

  3. [CF306C]White, Black and White Again_排列组合

    White, Black and White Again 题目链接:https://www.luogu.org/problem/CF306C 数据范围:略. 题解: 记得不要看错题,容易看成来回交替下 ...

  4. android 自动化测试 ---python wrapper(python 包装)

    关于有道云笔记复制的东西不能直接copy到博客园,可以选择使用txt文件做个媒介 1.appium 2.monkeyrunner 3.uiautomator2 前面两种种方式都要加载androidsd ...

  5. 最长回文 HDU - 3068(马拉车算法)

    Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入 ...

  6. npm—入门指导

    npm npm是什么? NPM(node package manager),通常称为node包管理器.顾名思义,它的主要功能就是管理node包,包括:安装.卸载.更新.查看.搜索.发布等. npm的背 ...

  7. varnish HTTP头

    Cache-Control:指定了缓存如何处理内容.varnish关心max-age参数,并用它来计算对象的TTL.“Cache-Control:no-cache”是被忽略的.Age:varnish添 ...

  8. Oracle笔记2

    24.复杂查询的三道题 # 案例一:列出与SCOTT从事相同工作的所有员工及部门名称.人数.平均工资 GROUP BY使用限制: 查询语句中没有GROUP BY,则SELECT子句中只能出现统计函数; ...

  9. Spring实战(四)Spring高级装配中的bean profile

    profile的原意为轮廓.剖面等,软件开发中可以译为“配置”. 在3.1版本中,Spring引入了bean profile的功能.要使用profile,首先要将所有不同的bean定义整理到一个或多个 ...

  10. 怎样在页面关闭时发起HTTP请求

    比如有需求是要让页面关闭时, 在数据库中记录用户的一些数据或log日志. 这时就需要在用户关闭页面时发起HTTP请求. 做法是对window.onunload设置事件监听函数, 在函数内发起AJAX请 ...