cf111D Petya and Coloring 组合数学,二项式反演
http://codeforces.com/contest/111/problem/D
Little Petya loves counting. He wants to count the number of ways to paint a rectangular checkered board of size n × m (n rows, m columns) in k colors. Besides, the coloring should have the following property: for any vertical line that passes along the grid lines and divides the board in two non-empty parts the number of distinct colors in both these parts should be the same. Help Petya to count these colorings.
The first line contains space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 106) — the board's vertical and horizontal sizes and the number of colors respectively.
Print the answer to the problem. As the answer can be quite a large number, you should print it modulo 109 + 7 (1000000007).
2 2 1
1
2 2 2
8
3 2 2
40 题意:
给出一个 n*m 的矩阵,用sum种颜色染色
满足:任意一条竖直线(纵线)把矩阵划分成的2个部分,2个部分的不同的颜色数相同
求方案数
n,m <= 10^3,sum <= 10^6 solution:
注意到n,m的范围不大 显然有以下性质:
1.第1列和第m列的颜色数一定相等
2.2~m-1列的颜色只能从1,m列的颜色的交集中选择 m=1的时候,特殊处理,ans=k^n
m>1的时候,我们只需要考虑1,m列的颜色选择还有交集大小 预处理g[i]表示恰好用i种颜色涂满n个格子的方案数
(此时n是一个常量) 如果用递推式,求g[i]需要O(n^2),求g数组需要O(n^3),tle
考虑二项式反演求g数组:
设h(i)表示用i种颜色染n个格子的方案数,则h(i) = i^n
g(i)表示恰好用i种颜色染n个格子的方案数,
有:h(y) = sigma(i=0,i<=y)(C(y,i)*g(i))
则:g(y) = sigma(i=0,i<=y)((-1)^(y-i) * C(y,i) * h(i))
= sigma(i=0,i<=y)((-1)^(y-i) * C(y,i) * i^n) 这样求g[i]需要O(nlogn),求g数组需要O(n^2*logn)
当然也可以优化到O(n^2)求g数组 主要的预处理部分搞定了,然后就是答案了
退下公式,得到:
ans = sigma(j=0,j<=min(n,sum))C(sum,j) * j^(m*n-2*n) *
(sigma(i=0,i<=min(n-j,(sum-j)/2))(C(sum-j,i)*C(sum-j-i,i)*g(i+j)^2))
//File Name: cf111D.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月16日 星期一 01时13分24秒 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h> #define LL long long using namespace std; const int MAXN = + ;
const int MAXM = + ;
const int MOD = (int)1e9 + ; LL jie[MAXM];
LL g[MAXN]; LL qp(LL x,LL y){
LL res = ;
while(y){
if(y & ) res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} LL get_c(LL x,LL y){
if(x < y) return ;
if(x == y || y == ) return ;
return jie[x] * qp(jie[y] * jie[x - y] % MOD,MOD - ) % MOD;
} void init(int sum,int n){
jie[] = ;
for(int i=;i<MAXM;i++)
jie[i] = jie[i-] * i % MOD;
int ma = min(sum,n);
LL now;
for(int i=;i<=ma;i++){
for(int k=;k<=i;k++){
now = get_c(i,k) * qp(k,n) % MOD;
if((i - k) % )
g[i] = (g[i] - now + MOD) % MOD;
else
g[i] = (g[i] + now) % MOD;
}
}
} LL solve(int n,int m,int sum){
if(m == ) return qp(sum,n);
init(sum,n);
LL ans = ,now,tmp;
int ma = min(sum,n);
for(int j=,ma2;j<=ma;j++){
now = ;
ma2 = min(n - j,(sum - j) / );
for(int i=;i<=ma2;i++){
(now += jie[sum-j] * qp(jie[sum-j-*i]*jie[i]%MOD*jie[i]%MOD,MOD - ) % MOD
* g[i+j] % MOD * g[i+j] % MOD) %= MOD;
}
tmp = qp(j,(m - ) * n) % MOD;
(ans += get_c(sum,j) * tmp % MOD * now % MOD) %= MOD;
//cout << j << " " <<ans << endl;
}
return ans;
} int main(){
int n,m,k;
scanf("%d %d %d",&n,&m,&k);
printf("%d\n",(int)solve(n,m,k));
return ;
}
cf111D Petya and Coloring 组合数学,二项式反演的更多相关文章
- 洛谷 P5400 - [CTS2019]随机立方体(组合数学+二项式反演)
洛谷题面传送门 二项式反演好题. 首先看到"恰好 \(k\) 个极大值点",我们可以套路地想到二项式反演,具体来说我们记 \(f_i\) 为钦定 \(i\) 个点为极大值点的方案数 ...
- [LOJ3119][CTS2019|CTSC2019]随机立方体:组合数学+二项式反演
分析 感觉这道题的计数方法好厉害.. 一个直观的思路是,把题目转化为求至少有\(k\)个极大的数的概率. 考虑这样一个事实,如果钦定\((1,1,1),(2,2,2),...,(k,k,k)\)是那\ ...
- codeforces111D. Petya and Coloring(组合数学,计数问题)
传送门: 解题思路: 要求一条直线分割矩阵时左右颜色数一样,那么就说明一个问题.直线左右移动时是不会改变左右矩阵的颜色集合的.所以说明:2~m-1列的颜色集一定属于第一列与第m列颜色集的交集.而且第一 ...
- UVAlive-7040 color(组合数学,二项式反演)
链接:vjudge 题目大意:有一排方格共 $n$ 个,现在有 $m$ 种颜色,要给这些方格染色,要求相邻两个格子的颜色不能相同.现在问恰好用了 $k$ 种颜色的合法方案数.答案对 $10^9+7$ ...
- [模板] 容斥原理: 二项式反演 / Stirling 反演 / min-max 容斥 / 子集反演 / 莫比乌斯反演
//待更qwq 反演原理 二项式反演 若 \[g_i=\sum_{j=1}^i {\binom ij} f_j\] , 则有 \[ f_i=\sum_{j=1}^i (-1)^{i-j} {i \ch ...
- P4859 已经没有什么好害怕的了(dp+二项式反演)
P4859 已经没有什么好害怕的了 啥是二项式反演(转) 如果你看不太懂二项式反演(比如我) 那么只需要记住:对于某两个$g(i),f(i)$ ---------------------------- ...
- 【LOJ#6374】网格(二项式反演,容斥)
[LOJ#6374]网格(二项式反演,容斥) 题面 LOJ 要从\((0,0)\)走到\((T_x,T_y)\),每次走的都是一个向量\((x,y)\),要求\(0\le x\le M_x,0\le ...
- 2018.11.07 hdu1465不容易系列之一(二项式反演)
传送门 其实标签只是搞笑的. 没那么难. 二项式反演只是杀鸡用牛刀而已. 这道题也只是让你n≤20n\le20n≤20的错排数而已. 还记得那个O(n)O(n)O(n)的递推式吗? 没错那个方法比我今 ...
- 洛谷4859 BZOJ3622 已经没什么好害怕的了(DP,二项式反演)
题目链接: 洛谷 BZOJ 题目大意:有两个长为 $n$ 的序列 $a,b$,问有多少种重排 $b$ 的方式,使得满足 $a_i>b_i$ 的 $i$ 的个数比满足 $a_i<b_i$ 的 ...
随机推荐
- MySQL性能优化之max_connections配置参数浅析
这篇文章主要介绍了MySQL性能优化之max_connections配置参数浅析,本文着重讲解了3种配置max_connections参数的方法,需要的朋友可以参考下 MySQL的max_connec ...
- 记事本写JAVA程序
编写程序源码: 1.新建记事本程序,修改文件名称为HelloWorld.java 打开编辑以下内容,保存. public class HelloWorld { public static void m ...
- Linux驱动设计——内存与IO访问
名词解释 内存空间与IO空间 内存空间是计算机系统里面非系统内存区域的地址空间,现在的通用X86体系提供32位地址,寻址4G字节的内存空间,但一般的计算机只安装256M字节或者更少的内存,剩下的高位内 ...
- 三步将Node应用部署到Heroku上 --转载
Heroku是一个提供快速部署服务的云平台.支持Node,Ruby,Java,PHP,Python,Go多种语言,今天体验了下,简直不要太爽.下面简单的介绍一下. 首先还是要注册一个账号:https: ...
- sellect、poll、epoll
http://www.cnblogs.com/alex3714/p/4372426.html select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多 ...
- 【转】iOS websocket 及时通讯实现
原文网址:http://blog.csdn.net/manyxh/article/details/48196209 2015-09-03 by 木易哥哥 开发一个类似QQ.微信的即时IM聊天功能,做到 ...
- Object-c 语言
字符串操作: http://www.myexception.cn/mobile/455287.html 1,判断两字符串是否相同 NSString *str1 = @"hello pepe& ...
- 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版!
转--http://www.2cto.com/kf/201402/277535.html 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版! 2014-02-11 ...
- nginx反向代理原理和配置讲解
最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一 概述 反向代理(Reverse Proxy)方式 ...
- OCR文字识别软件 怎么识别包含非常规符号的文本
ABBYY FineReader 12 是一款OCR图文识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,有时文本中可能会包含一些非常规的符号,此时ABBYY ...