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. P1189 SEARCH(逃跑的拉尔夫)

    P1189 SEARCH 题目描述 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置. 那个装置太旧了,以至于只能发射关于那辆车的移 ...

  2. LeetCode:7. Reverse Integer(Easy)

    题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...

  3. Spring MVC 开发 配置

    1.首先在web.xml中配置一个DispatcherServlet,并通过<servlet-mapping>指定需要拦截的url. 下面xml中配置一个拦截.html为后缀的url. & ...

  4. 虚拟现实-VR-UE4-创建一个自定义的角色 Character

    我学习的资料使用的是老版本的ue4 新版本有好多都是不一样的,好多东西需要自己来摸索, 比如,在老板版本中,默认创建一个GameMode 是回自动创建构造函数发的,而新版本,是没有的,需要自己手动填写 ...

  5. Mybatis 异常记录(1): Invalid bound statement (not found)

    错误信息: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pingan.cr ...

  6. LINQ学习笔记——(1)添加扩展方法

    目的:  对已存在类型的行为进行扩展 注意事项:    扩展方法是一种特殊的静态方法    扩展方法必须在静态类中定义    扩展方法的优先级低于同名的类方法    扩展方法只在特定的命名空间内有效 ...

  7. HDU 4010 Query on The Trees(动态树LCT)

    Problem Description We have met so many problems on the tree, so today we will have a query problem ...

  8. Performanced C++ 经验规则

    http://www.cnblogs.com/ccdev/archive/2012/12/27/2836448.html Performanced C++,意为“高性能C++“编程,是笔者和所在团队多 ...

  9. C# 利用WMI对象获取物理内存和可用内存大小

    下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下: //获取总物理内存大小 M ...

  10. Flink State的两张图

    streamTask的invoke方法中,会循环去调用task上的每个operator的initializeState方法,在这个方法中,会真正创建除了savepointStream的其他三个对象, ...