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. WPF ItemsControl 手动刷新

    原文:WPF ItemsControl 手动刷新 遇到这样一个问题, 我的ItemsSource是绑定到一个ObservableCollection<T>类型的实力上去的. 但是T类型没有 ...

  2. MySQL数据库服务器逐渐变慢分析与解决

    一.检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状态,因为cpu可 ...

  3. 【数据库】 SQL 使用注意点

    [数据库] SQL 使用注意点 一. 索引 1. 常用的搜索条件,都建议加上索引,但状态列除外(该列只有0,1或几个值,不需要加索引,因为没效果) 2. 查询时, 索引列不能做函数处理,会不走索引 3 ...

  4. 【紫书】(UVa1347)Tour

    继续考虑dp题目. 题意分析 其实这里只是更加仔细的做一个lrj的复读机(Orz 他分析了一个很重要的结果:如果是一个人从左到右再回来,并且每个点恰经过一次,那么等价于两个人从左到右每个点经过一次地遍 ...

  5. 虚拟现实-VR-UE4-获取UE4

    UE4现在虽然是开源,但是并不是免费的,在你的游戏成功后,回收取5%费用和每个月19美元的费用 所以,第一步,进去UE4官网:https://www.unrealengine.com/zh-CN/wh ...

  6. django中判断当前user具有是否有对模块的增删改查权限

    首先简单了解一下user的一些属性 User对象 User对象是认证系统的核心.用户对象通常用来代表网站的用户,并支持例如访问控制.注册用户.关联创建者和内容等.在Django认证框架中只有一个用户类 ...

  7. 树莓派的WIFI配置

    参考网址: http://www.cnblogs.com/iusmile/archive/2013/03/30/2991139.html http://my.oschina.net/pikeman/b ...

  8. web3无法安装的额解决方案-----yarn命令安装web3

    凡是可以用 JavaScript 来写的应用,最终都会用 JavaScript 来写. --Atwood定律(Jeff Atwood在2007年提出) yarn命令详解 https://yarnpkg ...

  9. php serialize讲解与json性能测试

    [序列化的概念] 序列化是将对象状态转换为可保持或可传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数据. 将对象的状态信息转换为可以存储或传输的 ...

  10. vue-cli项目里npm安装font-awesome

    第一步:进入到项目目录里,运行 cnpm i font-awesome -s 第二步:在main.js里面 import 'font-awesome/css/font-awesome.min.css'