题目大意:给定一个长度为 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. 子组件props接受父组件传递的值,能不能修改的问题

    参考链接:https://www.cnblogs.com/pangchunlei/p/11139356.html

  2. 【leetcode算法-简单】9. 回文数

    [题目描述] 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: ...

  3. Collection集合常用的功能

    package demo06; import java.util.ArrayList;import java.util.Collection; /** java.util接口 Collection&l ...

  4. JZOJ.1153【贪心算法】硬币交换

    好难啊!!! 可聪明的我还是解出来了!(逃 题目描述 小z最近迷上了一款游戏――To Be A Farmer,他在游戏中控制的人物是一个叫FZ的Farmer.FZ身上有G1个金币.S1个银币和B1个铜 ...

  5. NOIP2012 DAY1 T2 国王游戏

    题目描述 恰逢 H国国庆,国王邀请n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最前面 ...

  6. 【bitset】Kth Minimum Clique

    #include<bits/stdc++.h> #define B bitset<105> using namespace std; typedef long long ll ...

  7. Django(一)安装启动

    Django下载/启动 1.下载安装 pip install django 或者 压缩文件下载地址:https://github.com/django/django/releases python s ...

  8. Wannafly挑战赛22

    B. 字符路径 给一个含n个点m条边的有向无环图(允许重边,点用1到n的整数表示),每条边上有一个字符,问图上有几条路径满足路径上经过的边上的字符组成的的字符串去掉空格后以大写字母开头,句号 '.' ...

  9. hdu 1502 大数dp

    对于每一个dp的问题 从其最优解的结构(分哪几种形式或者情况)入手 然后分析状态 这样就比较好找出状态转方程这里数据结构的选择很简单 顺序数组就可以 填充的方式顺序填充就可以 然后这道题目卡了我大数. ...

  10. std::string与char*之临时缓冲区

    std::string与char*之临时缓冲区 原文:https://blog.csdn.net/hsshh1988/article/details/80689330 c++文件读取流程如下: ifs ...