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- ...
随机推荐
- static类成员(变量和函数)
0. 使用背景 对于特定类类型的全体对象而言,访问一个全局对象有时是必要的.也许,在程序的任意点需要统计已创建的特定类类型对象的数量:或者,全局对象可能是指向类的错误处理例程的一个指针:或者,它是指向 ...
- Linux centos7环境下安装MySQL的步骤详解
Linux centos7环境下安装MySQL的步骤详解 安装MySQL mysql 有两个跟windows不同的地方 1).my.ini 保存到/etc/my.ini 2).用户权限,单独用户执行 ...
- 采用apicloud开发移动端项目心得体会
作为第一批吃螃蟹的,来说一说apicloud做移动端项目的一些体会. 刚开始接到项目,需要移动端开发两个项目,公司人员里面没有一个会原生android或者ios开发的,目前能出的技术也就是web,后端 ...
- 使用CATransformLayer制作3D图像和动画
之前我们讲过可以用CALayer搭配CATransform3D来实现将View做3D旋转, 今天我们再看一个3D的新东西 CATransformLayer, 看名字就知道这个layer跟旋转有关, 那 ...
- 【算法专题】工欲善其事必先利其器—— C++ STL中vector(向量/不定长数组)的常用方法总结
#include<iostream> #include<cstdio> #include<string> #include<vector>//不定长数组 ...
- JS 脚本应该放在页面哪个位置 head body foot
我们平时在页面上写JS 是放在头部<head>中呢 还是放到body 最下面 能更优化? 查了一番资料,推荐 放在页面底部如: <html> <head> < ...
- Unity在安卓的一些路径
APK安装之后找不到路径 公司的测试机(安卓)基本都是不带SD卡的. APK在安卓手机上安装之后,使用手机助手类的软件打开文件管理,打开 内置SDK卡/Android/data/ 在这个目录下却发现 ...
- Sql Server 日期格式化
select CONVERT(date,myDtae,23),ID from myFirstTable; http://www.cnblogs.com/hantianwei/archive/2009/ ...
- ORA-01940无法删除当前已连接用户
原文地址:ORA-01940无法删除当前已连接用户作者:1736188794 1)查看用户的连接状况 select username,sid,serial# from v$session ------ ...
- Token注解防止表单的重复提交
注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...
题目链接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- ...
0. 使用背景 对于特定类类型的全体对象而言,访问一个全局对象有时是必要的.也许,在程序的任意点需要统计已创建的特定类类型对象的数量:或者,全局对象可能是指向类的错误处理例程的一个指针:或者,它是指向 ...
Linux centos7环境下安装MySQL的步骤详解 安装MySQL mysql 有两个跟windows不同的地方 1).my.ini 保存到/etc/my.ini 2).用户权限,单独用户执行 ...
作为第一批吃螃蟹的,来说一说apicloud做移动端项目的一些体会. 刚开始接到项目,需要移动端开发两个项目,公司人员里面没有一个会原生android或者ios开发的,目前能出的技术也就是web,后端 ...
之前我们讲过可以用CALayer搭配CATransform3D来实现将View做3D旋转, 今天我们再看一个3D的新东西 CATransformLayer, 看名字就知道这个layer跟旋转有关, 那 ...
#include<iostream> #include<cstdio> #include<string> #include<vector>//不定长数组 ...
我们平时在页面上写JS 是放在头部<head>中呢 还是放到body 最下面 能更优化? 查了一番资料,推荐 放在页面底部如: <html> <head> < ...
APK安装之后找不到路径 公司的测试机(安卓)基本都是不带SD卡的. APK在安卓手机上安装之后,使用手机助手类的软件打开文件管理,打开 内置SDK卡/Android/data/ 在这个目录下却发现 ...
select CONVERT(date,myDtae,23),ID from myFirstTable; http://www.cnblogs.com/hantianwei/archive/2009/ ...
原文地址:ORA-01940无法删除当前已连接用户作者:1736188794 1)查看用户的连接状况 select username,sid,serial# from v$session ------ ...
注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...