题意:给定 m 种颜色,把 n 盆花排成一直线的花涂色。要求相邻花的颜色不相同,且使用的颜色恰好是k种。问一共有几种涂色方法。

析:首先是先从 m 种颜色中选出 k 种颜色,然后下面用的容斥原理,当时没想出来,如果是只用一种颜色,那么肯定不行,如果用两种颜色,可以有这么方法,

2 * (2-1) ^ (n-1)种,如果是只用 i 种那么就是  i * (i-1) ^ (n-1)。然后依次求就好。再就是求组合数的时候,由于太大,不能用递推,所以要用逆元。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <ctime>
#include <cstdlib>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL f[maxn];
LL cm[maxn], ck[maxn]; void init(){
f[1] = 1;
for(int i = 2; i < maxn; ++i)
f[i] = (mod - mod/i) * f[mod%i] % mod;
} LL quick_pow(LL a, LL n){
LL ans = 1LL;
while(n){
if(n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
return ans;
} LL solve(LL n, LL m, LL k){
ck[0] = cm[0] = 1;
for(int i = 1; i <= k; ++i){
cm[i] = cm[i-1] * (m - i + 1) % mod * f[i] % mod;
ck[i] = ck[i-1] * (k - i + 1) % mod * f[i] % mod;
}
LL ans = 0;
LL cnt = 1;
for(int i = k; i >= 1; --i, cnt = -cnt)
ans = (mod + ans + ck[i] * i % mod * quick_pow(i-1, n-1) * cnt % mod) % mod;
return ans * cm[k] % mod;
} int main(){
init();
int T; cin >> T;
LL n, m, k;
for(int kase = 1; kase <= T; ++kase){
scanf("%I64d %I64d %I64d", &n, &m, &k);
printf("Case #%d: %I64d\n", kase, solve(n, m, k));
}
return 0;
}

Gym 100548F Color (数论容斥原理+组合数)的更多相关文章

  1. 2019.02.09 codeforces gym 100548F. Color(容斥原理)

    传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤ ...

  2. Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)

    题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...

  3. Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题

    Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...

  4. Codeforces 100548F - Color (组合数+容斥)

    题目链接:http://codeforces.com/gym/100548/attachments 有n个物品 m种颜色,要求你只用k种颜色,且相邻物品的颜色不能相同,问你有多少种方案. 从m种颜色选 ...

  5. 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理

    这题设$f(i)$为$gcd(i,j)=x$的个数,根据容斥原理,我们只需减掉$f(i×2),f(i×3)\cdots$即可 那么这道题:$$ans=\sum_{i=1}^n(f(i)×((i-1)× ...

  6. HDU 2841 Visible Trees 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. xdoj-1149(多重集合+容斥原理+组合数取模)

    #include <iostream> #include <algorithm> #include <cstdio> using namespace std; ty ...

  8. Bzoj4710 分特产(容斥原理+组合数)

    题面 Bzoj 题解 考虑容斥原理,所有人都有特产的方案数等于: 至少零个人没有特产\(-\)至少一个人没有特产\(+\)至少两个人有特产\(-...\) 接着考虑其中一种情况怎么求(假设现在至少有\ ...

  9. 【BZOJ2839】集合计数 容斥原理+组合数

    Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 它们的交集的元素个数为K,求取法的方案数,答案模1000000007 ...

随机推荐

  1. 【2018 Multi-University Training Contest 4】

    01: 02:https://www.cnblogs.com/myx12345/p/9407551.html 03: 04:https://www.cnblogs.com/myx12345/p/940 ...

  2. hdu4405:Aeroplane chess

    题目大意:有编号为0-n的格子,从0开始,扔骰子扔到几就走几格.有m个瞬移点,每个点可以从格x直接飞到格y,若瞬移到另一个瞬移点可以继续瞬移.求到达格n的期望扔骰子次数. 题解:期望DP入门好题.网上 ...

  3. make only output error/warning message( 编译时,只输出错误信息和警告信息)

    make > /dev/null 这样,正常的信息被重定向输出到/dev/null,错误和警告信息会输出到标准错误设备(standard error,相对于标准输入/输出设备来说).

  4. Mysqli的常用函数

    Mysqli的常用函数 连接数据库: $res = @mysqli_connect($host,$username,$pass,$db_name); if (mysqli_connect_errno( ...

  5. 从零开始写STL-二叉搜索树

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...

  6. 2017CodeM初赛A场

    A.最长树链(loj6159) 分析: 对于每个质因数,取出所有是它倍数的点组成一个树,然后找最长路径 每个数操作次数是其质因数的个数 所以总的复杂度不超过O(nlogA) B.二分图染色(loj61 ...

  7. 【TFS 2017 CI/CD系列 - 01】-- Agent篇

    一.环境要求: PowerShell 3.0 或者更高版本 VS2015或者更高版本 二.下载Agent: 用浏览器打开TFS,[Settings]--> [Agent Pools]--> ...

  8. 查看linux接口进出口流量的命令;linux 网络监控;流量监控

    1.nload,左右键切换网卡 2.sudo iftop 3.sudo iptraf 按连接/端口查看流量 4.sudo nethogs: 按进程查看流量占用 5.ss: 连接查看工具 6.dstat ...

  9. Meteor模板

    Meteor模板使用三个顶级标签.前两个是 head 和 body 标签.这些标签和在普通的HTML中做的工作一样.第三个标签 template.这是我们将HTML连接到JavaScript的地方. ...

  10. Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)

    Spring4.0的新特性我们在上一章已经介绍过了. 包含它对jdk8的支持,Groovy Bean Definition DSL的支持.核心容器功能的改进,Web开发改进.測试框架改进等等.这张我们 ...