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- ...
随机推荐
- 探讨C++ 变量生命周期、栈分配方式、类内存布局、Debug和Release程序的区别
探讨C++ 变量生命周期.栈分配方式.类内存布局.Debug和Release程序的区别(一) 今天看博客园的文章,发现博问栏目中有一个网友的问题挺有趣的,就点进去看了下,标题是“C++生存期问题”,给 ...
- C# 加密总结 一些常见的加密方法
C# 加密总结 一些常见的加密方法 一 散列数据 代码如下: ? private static string CalculateSHA512Hash(string input) { ...
- Memcache的一些学习
Memcache的一些学习 首先,Memcache是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动 ...
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
用字符数组方法: 基本思路是,先判断字符的结束标志'\0',然后从该位置向前输出. 实现代码: #include<iostream> using namespace std; int ma ...
- Linq 更改主键值
有一个班级表,主键是class_id,在管理班级时要进行逻辑删除,而只是单纯的is_del字段(记录每条数据是否有效)更改为true,主键class_id如果不变动,在再次增加一个班级时,其主键如果和 ...
- 在VirtualBox上安装CentOS7
文章的出处:http://jingyan.baidu.com/article/9c69d48f8ec01613c8024e58.html 工具: VirtualBox-5.1.2-108956-Win ...
- 【6】锋利的 jQuery 笔记
1. 代码技巧 1. 利用 id, class 实现同级隐藏显示 效果如下: 2. 字体放大效果 效果图: 3. tab 切换 效果图: 4. 切换样式 添加 Cookie 效果图: 5. 编写插件 ...
- Python 自动给数字前面补0
为了排版方便或者是输出文件命名整洁,通常需要给数字前面补0来做统一.Python中有一个zfill函数用来给字符串前面补0,非常有用,这个zfill看起来也就是zero fill的缩写吧,看一下如何使 ...
- const 修饰成员函数体
经过const修饰的变量表示不能被修改这个容易理解,例如 const int kInt = 0; // kInt 不能再被赋予其他值 const int getValue(const char *ke ...
- android 控件ui
公共参数: android:id="@+id/text_view" 给当前控件定义了 一个唯一标识符如:text_view android:layout_width=" ...
题目链接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- ...
探讨C++ 变量生命周期.栈分配方式.类内存布局.Debug和Release程序的区别(一) 今天看博客园的文章,发现博问栏目中有一个网友的问题挺有趣的,就点进去看了下,标题是“C++生存期问题”,给 ...
C# 加密总结 一些常见的加密方法 一 散列数据 代码如下: ? private static string CalculateSHA512Hash(string input) { ...
Memcache的一些学习 首先,Memcache是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动 ...
用字符数组方法: 基本思路是,先判断字符的结束标志'\0',然后从该位置向前输出. 实现代码: #include<iostream> using namespace std; int ma ...
有一个班级表,主键是class_id,在管理班级时要进行逻辑删除,而只是单纯的is_del字段(记录每条数据是否有效)更改为true,主键class_id如果不变动,在再次增加一个班级时,其主键如果和 ...
文章的出处:http://jingyan.baidu.com/article/9c69d48f8ec01613c8024e58.html 工具: VirtualBox-5.1.2-108956-Win ...
1. 代码技巧 1. 利用 id, class 实现同级隐藏显示 效果如下: 2. 字体放大效果 效果图: 3. tab 切换 效果图: 4. 切换样式 添加 Cookie 效果图: 5. 编写插件 ...
为了排版方便或者是输出文件命名整洁,通常需要给数字前面补0来做统一.Python中有一个zfill函数用来给字符串前面补0,非常有用,这个zfill看起来也就是zero fill的缩写吧,看一下如何使 ...
经过const修饰的变量表示不能被修改这个容易理解,例如 const int kInt = 0; // kInt 不能再被赋予其他值 const int getValue(const char *ke ...
公共参数: android:id="@+id/text_view" 给当前控件定义了 一个唯一标识符如:text_view android:layout_width=" ...