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的整数.一 ...
随机推荐
- 【Python基础】08_Python中的列表
1.列表的定义 List(列表)是Python中使用的 最频繁 的数据类型,其他语言通常叫数组 专门用于存储 一串信息 列表用 [] 定义,数据 之间用 , 分割 列表的 索引(位置) 从 0 开始 ...
- Timezone offset does not match system offset: 0 != -32400. Please, check your config files
apscheduler使用uWSGI的mule模块部署的时候报错, 因为系统时区和代码运行时区不一样导致. 解决办法:在初始化的时候指定上海的时区 scheduler = BlockingSchedu ...
- Ubuntu使用Shadow socks-qt5
由于大多数朋友都问我在Ubuntu上面怎么kexueshangwang,为了防止以后忘记,故此记录. 本教程使用的配置 Ubuntu 16.10Shadowsocks-qt5一个可用的ss账号一根能够 ...
- aspectcore 简单解析
.netcore 下aspectcore 的使用 动态代理: static void Main(string[] args) { Console.WriteLine("Hello Worl ...
- 编写Dockerfile自定义镜像
要求 编写一个Dockerfile自定义centos镜像,要求在容器内部可以使用vim和ifconfig命令,并且登入落脚点为/usr/local 编写Dockerfile FROM centos M ...
- SpringBoot的启动配置原理
一.启动流程 创建SpringApplication对象 public class SpringApplication { public SpringApplication(Class... prim ...
- 关于小程序去除view/navigator 点击后默认阴影效果
hover:class :定义容器在被触发时的样式 通常无用,但若不去除则影响用户体验: 为避免被覆盖,约定在wxss底部添加class,比如: <!-- wxml --> <na ...
- vue-Elementui引入
安装命令 npm install --save element-ui 可以直接复制官网的引用,复制到main.js里面:就可以忽略下面所有步骤 import Vue from 'vue'; impor ...
- 垃圾分类,javascript和python
首先,实现的步骤,首先在微信applet中设计一个简单的界面,开始映射到python服务器.有关具体界面,请参阅微信小程序设计指南.以下主要讨论后台服务器交互和处理点. 1.使用js将图像上传到pyt ...
- 1 java 笔记
第一java的版本: J2ME主要用于移动设备和信息家电 J2SE整个Java技术的核心 J2EE java技术应用最广泛的部分,主要应用与企业的开发 第二:基于java语言的开源框架 struts ...