D. Petya and Coloring
time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains space-separated integers nm and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 106) — the board's vertical and horizontal sizes and the number of colors respectively.

Output

Print the answer to the problem. As the answer can be quite a large number, you should print it modulo 109 + 7 (1000000007).

Examples
input
2 2 1
output
1
input
2 2 2
output
8
input
3 2 2
output
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的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. Git中从远程的分支获取最新的版本到本地——两种命令

    Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge Git fetch origin master git log ...

  2. java Vector向量

    9.3.4 Vector向量 [专业IT培训机构,真正零首付入学www.bjsxt.com] Vector底层是用数组实现的List,相关的方法都加了同步检查,因此“线程安全,效率低”. 比如,ind ...

  3. 修改npm全局安装模式的路径

    由于npm全局模块的存放路径及cache的路径默认是放在C盘下,这样肯定会增加C盘的负担,那么如果需要修改其存放路径应该怎么做呢? 第一步:在nodejs安装目录(也可以指定其它目录)下创建”node ...

  4. CSP201403-1:相反数

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的“计算机职业资格认证”考试,针对计算机软件开发. ...

  5. [OpenCV]DMatch类和KeyPoints类:特征点匹配

    DMatch struct CV_EXPORTS_W_SIMPLE DMatch { CV_WRAP DMatch() : queryIdx(-), trainIdx(-), imgIdx(-), d ...

  6. PAT——乙级1015/甲级1062:德才论

    这两个题是一模一样的 1015 德才论 (25 point(s)) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德 ...

  7. java设计模式之装饰器模式以及在java中作用

    在JAVA I/O类库里有很多不同的功能组合情况,这些不同的功能组合都是使用装饰器模式实现的,下面以FilterInputStream为例介绍装饰器模式的使用  FilterInputStream和F ...

  8. python json模块 超级详解

    JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.JSON的数据格式其实就是python里面的字典格式,里面可以包含方括号括起来的数组,也 ...

  9. POJ 2082 Terrible Sets(栈)

    Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...

  10. 并查集——poj2236(带权并查集)

    题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N ...