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的整数.一 ...
随机推荐
- Django 在Python3.5 下报 没有模块MySQLdb
在整个项目站点下的__init__.py 文件里(即和setting.py在同一个文件下)写入以下代码: import pymysql pymysql.install_as_MySQLdb() 需要提 ...
- 通过ImageReader进行图像裁剪时出现NoSuchElementException异常
首先放上最初的Image工具类 package util; import java.awt.Rectangle; import java.awt.image.BufferedImage; import ...
- Unable to find optional library: org.apache.http.legacy 错误
在目录adt-bundle-windows-x86_64-20140702\sdk\platforms\android-20或者 C:\Users\Administrator\AppData\Loca ...
- MyBatis MyBatis Generator入门
一.MGB功能简介 MyBatis Generator是一个代码生成工具. MBG是如何运行的呢?它会检查所连接到的数据库的一个或者多个table,然后生成可用来访问这些table的构建(Java代码 ...
- 8-MySQL DBA笔记-测试基础
第三部分 测试篇 测试需要掌握的知识面很广泛,本篇的关注点是数据库的性能测试和压力测试,对于其他领域的测试,由于涉猎不多,笔者就不做叙述了.DBA的工作职责之一就是评估软硬件,这往往是一项很耗时的工作 ...
- 作业13:Map相关知识点(一)
一 Map相关类图 二 Map接口 1 Map接口中的方法 jdk 方法名 简单描述 put(K,V):V 添加value,当Key对应无值,返回null;有值则返回上一个值.(覆盖式,可以反复覆盖前 ...
- html5中本地存储概念是什么?
html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于本地存储一个会话中的数据,这些数据只有在同一个会话中的页 ...
- 总结 String、StringBuffer与StringBuilder类中常用的方法
一.String类的常用方法 1.获取: 1)获取字符串str长度 int i = str.length(); 2)根据位置(index)获取字符 char c = str.charAt(index) ...
- 【Struts2】进阶
一.Action处理请求参数 1.1 属性驱动 1.2 模型驱动 1.3 扩展 将数据封装到List集合 将数据封装到Map集合 二.类型转换 2.1 自定义类型转换器: 1.创建一个自定义类型转换器 ...
- SpringAOP的实现方式
1.使用SpringAPI实现AOP <aop:config> <!-- 切入点:需要操作的目标类中的目标方法 execution中只需要修改全类名 --> <aop:p ...