codeforces 111D
5 seconds
256 megabytes
standard input
standard output
Little Petya loves counting. He wants to count the number of ways to paint a rectangular checkered board of size n × m (n rows, mcolumns) 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
description:
一个矩阵有N行M列,现有k种颜色,求符合下列要求的填色方案有多少种
要求:将矩阵竖直的切分成非空的两部分,两部分所包含的颜色数相同(只是颜色数相同,并没有要求是有相同的颜色)
solution:
考虑每种合法的填色方案
假设最左边一列的颜色种类为 a ,则剩余的部分也只能有k种颜色
此时左边的两列的颜色数必定不小于 a(多了一列),除去左边两列所剩的矩阵的颜色数必定不超过 a(少了一列)
又因为这个矩阵符合要求,故左边两列的颜色 = 除去左边两列的 = a
也就说明左边的第二列的颜色必定在左边第一列出现过
如此重复下去,可以证明,除了左边第一列和右边第一列,矩阵剩余部分的颜色必定都在左边第一列(右边第一列)出现过
不妨设左边第一列和右边第一列公共的颜色数为 b ,显然矩阵剩余部分的颜色数不超过 b ,于是中间那部分的填色方案肯定为 b ^ (n (m - 2))
现在要去算左边第一列和右边第一列的填色方案,显然这两个都是相等的,所以现在就是考虑一个1*n的数组用正好a种颜色填充的方案数
这个就是容斥原理就可以知道,设方案数为 F(a, n) = a ^ n - C(a, 1) * (a - 1) ^ n + C(a, 2) * (a - 2) ^ n - ...
于是总方案数为 ∑ C(k, a) * C(a, b) * C(k - a, a - b) F(a, n) ^ 2 * b ^ (n (m - 2))
hint:
注意考虑a和b的枚举范围 //我就是在这里WA了很久……
注意取模 //现在弱爆了,这个都写错
m = 1 也要考虑
code:
#include<bits/stdc++.h>
using namespace std;
char ch; bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxk=;
const int mod=1E9+;
int fac[maxk],inv[maxk],invfac[maxk];
int n,m,k;
int ksm(int a,int b){
int t;
for (t=;b;b>>=,a=1LL*a*a%mod) if (b&) t=1LL*t*a%mod;
return t;
}
int C(int n,int m){
int res=1LL*fac[n]*invfac[m]%mod*invfac[n-m]%mod;
return res;
}
int main(){
read(n),read(m),read(k);
int lim=max(n,k);
fac[]=;
for (int i=;i<=lim;i++) fac[i]=1LL*fac[i-]*i%mod;
inv[]=;
for (int i=;i<=lim;i++) inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
invfac[]=;
for (int i=;i<=lim;i++) invfac[i]=1LL*invfac[i-]*inv[i]%mod;
int ans=;
if (m==){
printf("%d\n",ksm(k,n));
return ;
}
for (int a=;a<=min(n,k);a++){
int res=;
for (int i=;i<a;i++){
int tmp=1LL*C(a,i)*ksm(a-i,n)%mod;
if (i&) tmp=mod-tmp;
res=(res+tmp)%mod;
}
res=1LL*res*res%mod;
res=1LL*res*C(k,a)%mod;
for (int b=max(*a-k,);b<=a;b++){
ans=(ans+1LL*res*C(a,b)%mod*C(k-a,a-b)%mod*ksm(b,n*(m-))%mod)%mod;
}
}
printf("%d\n",ans);
return ;
}
codeforces 111D的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- javascript数据相关处理,序列化反序列化,数据编码与解码
对象序列化简而言之,将对象转为字符串.在数据的传输过程中,经常会使用到对象序列化. javascript中常用的对象序列化:JSON.stringify(); javascript中常用的对象反序列化 ...
- 使用fiddler对手机上的APP进行抓包
前提: 1.必须确保安装fiddler的电脑和手机在同一个wifi环境下 备注:如果电脑用的是台式机,可以安装一个随身wifi,来确保台式机和手机在同一wifi环境下 安装配置步骤: 1.下载一个fi ...
- Appium iOS万能的定位方式--Predicate(iOSNsPredicate)
所谓Predicate定位即Java-Client -5.0.版本以及Appium-Python-Client 0.31版本更新后增加的新的定位方式: 举个例子: JAVA代码: //输入账号和密码 ...
- hdu5863 cjj's string game
矩阵快速幂 #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MOD = ...
- 1087 All Roads Lead to Rome (30 分)(最短路径)
直接用Dijkstra做 #include<bits/stdc++.h> using namespace std; int n,m; map<string,int>si; ma ...
- Alpha项目冲刺(团队作业5)
团队成员 组 员 学号 朱世杰 211414141 曹晔宁 211306302 一.冲刺(7次 Scrum) [Alpha版本]冲刺阶段--Day 1 [Alpha版本]冲刺阶段--Day 2 [Al ...
- Android之ViewPager 第一课
想要了解Android新版本的的新特性,从头开始吧,这是Android3.0新加入的widget,以前也接触过,但是没有好好的研究过,今天写了一个小程序,研究一下ViewPager. 这个程序是支持左 ...
- Ubuntu如何进入命令模式
Ctrl+Alt+T 或者Ctrl+Alt+F2~F6进入命命令模式 Ctrl+Alt+F7返回桌面
- Java使用泛型的困顿
原文有点儿胡说的意味,删了,有空再次更新这篇博文~
- 教你一步一步在linux中正确的安装Xcache加速php
#第一步,下载Xcache wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz #第一步非常简单,如果你下载不 ...