【UVALive-7040F】Color
题目大意:给定一个长度为 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的更多相关文章
- 【POJ 2054】 Color a Tree
[题目链接] http://poj.org/problem?id=2054 [算法] 贪心 [代码] #include <algorithm> #include <bitset> ...
- 【Uva 1625】Color Length
[Link]: [Description] 给你两个序列,都由大写字母组成; 每次,把两个序列中的一个的开头字母加在字符串的尾端,然后在那个序列中删掉那个开头字母; 最后得到一个字符串; 这个字符串显 ...
- 【POJ 2154】 Color (置换、burnside引理)
Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...
- 【全排列+子序列】Color
[题意] 这个题目就是问,是否存在每个人对应每一种颜色,如果存在则输出字典序最小的. 否则输出-1 [题解] 利用next_permutation来构造36种情况.记住最后还需要排序一遍. 然后用子序 ...
- 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)
[题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...
- 【Uvalive 5834】 Genghis Khan the Conqueror (生成树,最优替代边)
[题意] 一个N个点的无向图,先生成一棵最小生成树,然后给你Q次询问,每次询问都是x,y,z的形式, 表示的意思是在原图中将x,y之间的边增大(一定是变大的)到z时,此时最小生成数的值是多少.最后求Q ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- 【 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 ...
- 【UVALive - 5131】Chips Challenge(上下界循环费用流)
Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...
- 【UVALive - 3487】 Duopoly(网络流-最小割)
Description The mobile network market in country XYZ used to be dominated by two large corporations, ...
随机推荐
- CSharp实体生成器
专门为C#开发人员定制的一个实体生成器,以往的生成器功能达到了,但是很多细节未考虑到,所以我借鉴“先人”的一些已有的东西,重新定制了一个.当然,需要源码的同学,直接使用IL Spy这个小工具就可以看到 ...
- Spring switch的使用
首先在html开始标签中引入一个属性 1 xmlns:th="http://www.thymeleaf.org" 示例代码 <div th:switch="${us ...
- Charles系列一:Charles功能介绍、下载安装和界面简介
一:Charles主要功能介绍 Charles是一个HTTP代理/HTTP监视器/反向代理,使开发和测试人员能够查看机器和Internet之间所有的HTTP和SSL/HTTPS流量,这包括请求,响应. ...
- 46.前端html5标签学习
HTML:TR TD TH OL UL LI 这几个标签要区别 一.什么是HTML: 超文本标记语言(HyperText Markup Language),标准通用标记语言下的一个应用: 是 ...
- 剑指offer42:数组和一个数字S,输出两个数的乘积最小的
1 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. ...
- 怎样通过html标签名获取元素节点集合
方法1. 使用document.querySelectorAll(); 方法2. 使用document.getElementsByTagName(); document.querySelectorAl ...
- WinSockAPI多线程服务器
运行效果: 程序: // TcpServer.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream&g ...
- SQL将多行数据合并成一行【转】
转:https://blog.csdn.net/AntherFantacy/article/details/83824182 今天同事问了一个需求,就是将多行数据合并成一行进行显示,查询了一些资料,照 ...
- Shiro——入门Demo
Shiro——入门Demo 环境- 引入相关maven依赖, shiro-core,commons-logging 配置shiro配置文件:ini后缀 主方法测试: import org.apach ...
- java Spring boot项目简单说明
前言 一直从事.NET开发,但一直有种想去探索Java世界的冲动,今天终于有时间来尝试一下,以下是自己探索过程的简要记录. 一.开发工具 开发工具选用 IntelliJ IDEA社区版(免费),安装教 ...