Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题
Problem F. Color
Description
Recently, Mr. Big recieved n flowers from his fans. He wants to recolor those flowers with
m colors. The flowers are put in a line. It is not allowed to color any adjacent flowers with
the same color. Flowers i and i + 1 are said to be adjacent for every i, 1 ≤ i < n. Mr. Big
also wants the total number of different colors of the n flowers being exactly k.
Two ways are considered different if and only if there is at least one flower being colored
with different colors.
Input
The first line of the input gives the number of test cases, T. T test cases follow. T is 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 ≤
109
, 1 ≤ k ≤ 106
, k ≤ n, m).
Output
For each test case, output one line containing “Case #x: y”, where x is the test case
number (starting from 1) and y is the number of ways of different coloring methods modulo
109 + 7.
Samples
Sample Input Sample Output
2
3 2 2
3 2 1
Case #1: 2
Case #2: 0
题意:给你N朵花,M种颜料(n,m<=1e9),要求给所有花染色,且相邻的花不能用同样的颜色,求出最后恰好用了k种
颜料的方案数(k<=1e5)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e6+10;
const int mod=1e9+7;
int cas,n,m,k,kk;
ll C[N],inv[N]; ll _pow(ll a,ll b)
{
ll res=1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
} void init_yuan()
{
inv[1]=1;
for(int i=2;i<=1e6;i++){
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
}
}//求逆元模板 void init_cki()
{
C[0]=1;
for(int i=0;i<k;i++){
C[i+1]=C[i]*(k-i)%mod*inv[i+1]%mod;
}
}//预处理C(k,i) ll c(ll m,ll k)
{
ll res=1;
k=min(k,m-k);
for(int i=0;i<k;i++){
res=res*(m-i)%mod;
}
for(int i=0;i<k;i++){
res=res*inv[i+1]%mod;
}
return res;
}//求C(m,k) int main()
{
kk=0;
init_yuan();
SC("%d",&cas);
while(cas--) {
SC("%d%d%d",&n,&m,&k);
init_cki();
ll ans=k*_pow(k-1,n-1)%mod,res=0;
for(int i=1;i<=k-2;i++) {
if(i%2) res+=C[i]*(k-i)%mod*_pow(k-i-1,n-1)%mod;
else res=(res-C[i]*(k-i)%mod*_pow(k-i-1,n-1)+mod)%mod;
}
ans=(ans-res+mod)%mod;
ans=(ans*c(m,k))%mod;
printf("Case #%d: %lld\n",++kk,ans);
}
return 0;
}
错因分析:刚开始想的是直接在n上容斥,,果然是太过复杂,,,就挂了;;
解答:1.可以转换下思路,,如果当前选了k种,那么涂完后花的颜色不超过k种的方案数为S=k*_pow(k-1,n-1),想象其是一个集合,设a[i](1<=i<=k)代表第i种颜料并没有用到,那么那么此情况下答案就为S-(a[1]并a[2]并...a[k])的面积,但是后面这个部分肯定是a[1]和a[2]等这些存在交集,所以就需要容斥了,最后因为存在C(m,k)种情况所以答案出来后还要乘C(m,k)
2.除法取模需要用到逆元,见资料
Gym 100548F Color 给花染色 容斥+组合数学+逆元 铜牌题的更多相关文章
- 2015 asia xian regional F Color (容斥 + 组合数学)
2015 asia xian regional F Color (容斥 + 组合数学) 题目链接http://codeforces.com/gym/100548/attachments Descrip ...
- P4491 [HAOI2018]染色 容斥+NTT
$ \color{#0066ff}{ 题目描述 }$ 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度为 \(N\) 的序列, 每个位置都可以被染成 ...
- LOJ.6160.[美团CodeM初赛 RoundA]二分图染色(容斥 组合)
题目链接 \(Description\) 求在\(2n\)个点的完全二分图(两边各有\(n\)个点)上确定两组匹配,使得两个匹配没有交集的方案数. \(n\leq10^7\). \(Solution\ ...
- LOJ2527 HAOI2018 染色 容斥、生成函数、多项式求逆
传送门 调了1h竟然是因为1004535809写成了998244353 "恰好有\(K\)种颜色出现了\(S\)次"的限制似乎并不容易达到,考虑容斥计算. 令\(c_j\)表示强制 ...
- 2019.02.09 codeforces gym 100548F. Color(容斥原理)
传送门 题意简述:对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数.(n,m≤1e9,k≤1e6n,m\le1e9,k\le1e6n,m≤ ...
- Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)
题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...
- 组队赛Day1第一场 GYM 101350 G - Snake Rana (容斥)
[题意] 给一个N×M的矩阵, K个地雷的坐标.求不含地雷的所有矩形的总数. T组数据. N M都是1e4,地雷数 K ≤ 20 Input 3 2 2 1 2 2 6 6 2 5 2 2 5 100 ...
- 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的整数.一 ...
随机推荐
- select key from table 一直出错
key和keys 为mysql 关键字,数据库设计字段的时候尽量避免
- python爬取网页数据并存储到mysql数据库
#python 3.5 from urllib.request import urlopen from urllib.request import urlretrieve from bs4 impor ...
- Python Socket套接字编程
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- Linux 创建用户 用户组 用户权限
首先 你要有个root账号 然后才能做下面几条操作: useradd username 创建用户usernamepasswd user_pwd 给已创建的用户username设置密码 关于us ...
- Js 判断数组中是否包含某个值
includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false. JavaScript Array includes() 方法
- 转载一篇让你全面了解什么是.NET。
转载于:https://www.52abp.com/BlogDetails/10009 阅读文本大概需要 8 分钟. 持续进化的 .NET 这张图即是一个学习的路线图同样他也是 .NET 平台的进化图 ...
- MUI 结合layui实现分页
mui自带有分页,在ui上我还是认为layui的友好点. 第三方插件: template-web.js-----------------前端数据绑定 layui.js.layui.css------- ...
- OnePlus5刷 TWRP
# 安装adb apt install adb # 安装fastboot apt install fastboot # 进入bootloader模式 adb reboot bootloader # 刷 ...
- VC文件扩展名
.APS:存放二进制资源的中间文件,VC把当前资源文件转换成二进制格式,并存放在APS文件中,以加快资源装载速度. .BMP:位图资源文件. .BSC:浏览信息文件,由浏览信息维护工具(BSCMAKE ...
- 2019年C题 视觉情报信息分析
2019 年第十六届中国研究生数学建模竞赛C 题 任务1中 图三:图3 中拍照者距离地面的高度 目录: 0.试题分析: 1.构建摄像机模型 2.摄像机参数假定 3.像平面坐标计算 4.图像标定及数值测 ...