2015 asia xian regional F Color (容斥 + 组合数学)
2015 asia xian regional F Color (容斥 + 组合数学)
题目链接http://codeforces.com/gym/100548/attachments
Description
Recently, Mr. Bigrecieved n flowers from his fans. He wants to recolor those flowerswith m colors. The flowers are put in a line. It is not allowed tocolor any adjacent flowers with the same color. Flowers i and i + 1are said to be adjacent for every i, 1 ≤ i < n. Mr. Big alsowants the total number of different colors of the n flowers beingexactly k.
Two ways areconsidered different if and only if there is at least one flowerbeing colored
with differentcolors.
Input
The first line ofthe input gives the number of test cases, T. T test cases follow. Tis about 300 and in most cases k is relatively small.
For each test case,there will be one line, which contains three integers n, m, k (1 ≤n, m ≤ 10^9, 1 ≤ k ≤ 10^6, k ≤ n, m).
Output
For each test case,output one line containing “Case #x: y”, where x is the test casenumber (starting from 1) and y is the number of ways of differentcoloring methods modulo 10^9 + 7.
Sample Input
2
3 2 2
3 2 1
Sample Output
Case #1: 2
Case #2: 0
题意:
给你n个物品,最多有m种颜色可以使用,你需要把这n个物品染色,要求恰好使用k种颜色。问不同的染色方案数
题解:
首先我们需要使用k种颜色,那么就是在m种选择k种,然后第一个物品可以染k种颜色,后面每个可以染k-1种颜色,那么就是C(m,k)C(k,k)k*(k-1)^(n-1)。但是这个是最多使用k种颜色,不是恰好使用k种颜色,这时候就使用容斥了。首先这个结果包含了最多k-1种颜色的结果,最多k-1种种又包含了最多k-2种,这个时候我们就可以利用容斥的奇加偶减来计算从k~1。
求组合数可以使用逆元打表求得。至于组合数公式是很简单的求法。
代码:
#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const ll mod = 1e9+7 ;
const int maxn = 1e6 + 10 ;
ll pow(ll a, ll n)
{
ll ret = 1, cal = a ;
while (n){
if (n&1)
ret = ret*cal%mod ;
cal = cal*cal%mod ;
n >>= 1;
}
return ret ;
}
ll inv[maxn] ;
void getinv()
{
for (ll i = 1; i < maxn; i++)
inv[i] = pow(i,mod-2) ;
}
ll co[maxn] ;
void comb(ll n, ll k)
{
co[0] = 1;
for (ll i = 1; i <= k; i++)
co[i] = ((co[i-1] * (n-i+1)%mod) * inv[i])%mod ;
}
ll solve(ll n, ll m, ll k)
{
comb(k,k) ;
ll sign = 1;
ll ans = 0;
for (long long i = k; i >= 1; i--){
ans = (mod + ans + ((sign*co[i]%mod)*i%mod)*pow(i-1,n-1)%mod)%mod ;
sign = -sign ;
}
comb(m,k) ;
ans = ans*co[k]%mod ;
return ans ;
}
int main()
{
getinv() ;
int t;
scanf("%d",&t) ;
for (int _t = 1; _t <= t; _t++){
ll n,m,k;
scanf("%lld %lld %lld",&n,&m,&k) ;
printf("Case #%d: %lld\n",_t,solve(n,m,k)) ;
}
return 0 ;
}
2015 asia xian regional F Color (容斥 + 组合数学)的更多相关文章
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(部分题解)
摘要 本文主要给出了2014-2015 ACM-ICPC, Asia Xian Regional Contest的部分题解,说明了每题的题意.解题思路和代码实现,意即熟悉区域赛比赛题型. Built ...
- Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...
- [清华集训2015 Day1]主旋律-[状压dp+容斥]
Description Solution f[i]表示状态i所代表的点构成的强连通图方案数. g[i]表示状态i所代表的的点形成奇数个强连通图的方案数-偶数个强连通图的方案数. g是用来容斥的. 先用 ...
- BZOJ2839:集合计数(容斥,组合数学)
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数,答案模1000000007. ...
- 【BZOJ4559】[JLoi2016]成绩比较 动态规划+容斥+组合数学
[BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...
- [CTS2019]随机立方体(容斥+组合数学)
这题七次方做法显然,但由于我太菜了,想了一会发现也就只会这么多,而且别的毫无头绪.发现直接做不行,那么,容斥! f[i]为至少i个极值的方案,然后这里需要一些辅助变量,a[i]表示选出i个三维坐标均不 ...
- 51nod1667-概率好题【容斥,组合数学】
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1667 题目大意 两个人. 第一个人有\(k_1\)个集合,第\(i\)个 ...
- 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x ...
- Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)
题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...
随机推荐
- [Ext JS 4] contentEL,renderTo, applyTo 释义与区别
前言 若干年前,使用Ext JS 3 开发了一个系统. 随着Ext JS 4的出现,总是会看到或听到关于Ext 比较多的言论是 : Ext JS 4 较Ext JS 3 有较大的改变. Ext JS ...
- Go - Revel框架介绍
Go - Revel框架介绍 https://github.com/robfig/revel http://robfig.github.io/revel/ web框架:revel 数据库:mongod ...
- Effective C++ 第二版 1)const和inline 2)iostream
条款1 尽量用const和inline而不用#define >"尽量用编译器而不用预处理" Ex. #define ASPECT_R 1.653 编译器永远不会看到AS ...
- AS代码优化和Flex应用程序的性能
1.在局部变量够用时,不要使用全局变量.类静态变量也要少用.全局变量是开发者的恶梦.实在需要全局变量的话,我建议使用singleton设 计模式来进行管理. 2.读取数组中的数据,尽量使用for in ...
- CF 327E(Axis Walking-状态压缩Dp-lowbit的使用)
E. Axis Walking time limit per test 3 seconds memory limit per test 512 megabytes input standard inp ...
- 编程利用利用curses库编程开始
时间紧张,先记一笔,后续优化与完善. curses库常用函数: 注意编译时要用这样的格式:gcc xxx.c -l curses -o xxx 第一个小例子: include <stdio.h& ...
- 分享php工作中遇到的一些探究和技巧【2】
1 如何定义linux和window通用的文件分隔符号 DIRECTORY_SEPARATOR : 目录分隔符,是定义php的内置常量.在调试机器上,在windows我们习惯性的使用"\& ...
- Day1-python理论基础
一.python介绍 Python 的创始人为Guido van Rossum.Guido为了打发圣诞节的无趣,于1989年发明,在荷兰国家数学和计算机科学研究所设计出来的(作为ABC 语言的一种继 ...
- AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)
AngularCSS CSS on-demand for AngularJS Optimize the presentation layer of your single-page apps by d ...
- [pinyin4j] java版汉字转换拼音(大小写)
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2. ...
题目链接http://codeforces.com/gym/100548/attachments
Description
Recently, Mr. Bigrecieved n flowers from his fans. He wants to recolor those flowerswith m colors. The flowers are put in a line. It is not allowed tocolor any adjacent flowers with the same color. Flowers i and i + 1are said to be adjacent for every i, 1 ≤ i < n. Mr. Big alsowants the total number of different colors of the n flowers beingexactly k.
Two ways areconsidered different if and only if there is at least one flowerbeing colored
with differentcolors.
Input
The first line ofthe input gives the number of test cases, T. T test cases follow. Tis about 300 and in most cases k is relatively small.
For each test case,there will be one line, which contains three integers n, m, k (1 ≤n, m ≤ 10^9, 1 ≤ k ≤ 10^6, k ≤ n, m).
Output
For each test case,output one line containing “Case #x: y”, where x is the test casenumber (starting from 1) and y is the number of ways of differentcoloring methods modulo 10^9 + 7.
Sample Input
2
3 2 2
3 2 1
Sample Output
Case #1: 2
Case #2: 0
题意:
给你n个物品,最多有m种颜色可以使用,你需要把这n个物品染色,要求恰好使用k种颜色。问不同的染色方案数
题解:
首先我们需要使用k种颜色,那么就是在m种选择k种,然后第一个物品可以染k种颜色,后面每个可以染k-1种颜色,那么就是C(m,k)C(k,k)k*(k-1)^(n-1)。但是这个是最多使用k种颜色,不是恰好使用k种颜色,这时候就使用容斥了。首先这个结果包含了最多k-1种颜色的结果,最多k-1种种又包含了最多k-2种,这个时候我们就可以利用容斥的奇加偶减来计算从k~1。
求组合数可以使用逆元打表求得。至于组合数公式是很简单的求法。
代码:
#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const ll mod = 1e9+7 ;
const int maxn = 1e6 + 10 ;
ll pow(ll a, ll n)
{
ll ret = 1, cal = a ;
while (n){
if (n&1)
ret = ret*cal%mod ;
cal = cal*cal%mod ;
n >>= 1;
}
return ret ;
}
ll inv[maxn] ;
void getinv()
{
for (ll i = 1; i < maxn; i++)
inv[i] = pow(i,mod-2) ;
}
ll co[maxn] ;
void comb(ll n, ll k)
{
co[0] = 1;
for (ll i = 1; i <= k; i++)
co[i] = ((co[i-1] * (n-i+1)%mod) * inv[i])%mod ;
}
ll solve(ll n, ll m, ll k)
{
comb(k,k) ;
ll sign = 1;
ll ans = 0;
for (long long i = k; i >= 1; i--){
ans = (mod + ans + ((sign*co[i]%mod)*i%mod)*pow(i-1,n-1)%mod)%mod ;
sign = -sign ;
}
comb(m,k) ;
ans = ans*co[k]%mod ;
return ans ;
}
int main()
{
getinv() ;
int t;
scanf("%d",&t) ;
for (int _t = 1; _t <= t; _t++){
ll n,m,k;
scanf("%lld %lld %lld",&n,&m,&k) ;
printf("Case #%d: %lld\n",_t,solve(n,m,k)) ;
}
return 0 ;
}
摘要 本文主要给出了2014-2015 ACM-ICPC, Asia Xian Regional Contest的部分题解,说明了每题的题意.解题思路和代码实现,意即熟悉区域赛比赛题型. Built ...
Problem F. ColorDescriptionRecently, Mr. Big recieved n flowers from his fans. He wants to recolor th ...
Description Solution f[i]表示状态i所代表的点构成的强连通图方案数. g[i]表示状态i所代表的的点形成奇数个强连通图的方案数-偶数个强连通图的方案数. g是用来容斥的. 先用 ...
Description 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得它们的交集的元素个数为K,求取法的方案数,答案模1000000007. ...
[BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...
这题七次方做法显然,但由于我太菜了,想了一会发现也就只会这么多,而且别的毫无头绪.发现直接做不行,那么,容斥! f[i]为至少i个极值的方案,然后这里需要一些辅助变量,a[i]表示选出i个三维坐标均不 ...
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1667 题目大意 两个人. 第一个人有\(k_1\)个集合,第\(i\)个 ...
Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x ...
题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...
前言 若干年前,使用Ext JS 3 开发了一个系统. 随着Ext JS 4的出现,总是会看到或听到关于Ext 比较多的言论是 : Ext JS 4 较Ext JS 3 有较大的改变. Ext JS ...
Go - Revel框架介绍 https://github.com/robfig/revel http://robfig.github.io/revel/ web框架:revel 数据库:mongod ...
条款1 尽量用const和inline而不用#define >"尽量用编译器而不用预处理" Ex. #define ASPECT_R 1.653 编译器永远不会看到AS ...
1.在局部变量够用时,不要使用全局变量.类静态变量也要少用.全局变量是开发者的恶梦.实在需要全局变量的话,我建议使用singleton设 计模式来进行管理. 2.读取数组中的数据,尽量使用for in ...
E. Axis Walking time limit per test 3 seconds memory limit per test 512 megabytes input standard inp ...
时间紧张,先记一笔,后续优化与完善. curses库常用函数: 注意编译时要用这样的格式:gcc xxx.c -l curses -o xxx 第一个小例子: include <stdio.h& ...
1 如何定义linux和window通用的文件分隔符号 DIRECTORY_SEPARATOR : 目录分隔符,是定义php的内置常量.在调试机器上,在windows我们习惯性的使用"\& ...
一.python介绍 Python 的创始人为Guido van Rossum.Guido为了打发圣诞节的无趣,于1989年发明,在荷兰国家数学和计算机科学研究所设计出来的(作为ABC 语言的一种继 ...
AngularCSS CSS on-demand for AngularJS Optimize the presentation layer of your single-page apps by d ...
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2. ...