# codeforces 111D

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:

solution:

hint:

m = 1 也要考虑

code:

 1 #include<bits/stdc++.h>
2 using namespace std;
3 char ch; bool ok;
5     for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
6     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
7     if (ok) x=-x;
8 }
9 const int maxk=1000005;
10 const int mod=1E9+7;
11 int fac[maxk],inv[maxk],invfac[maxk];
12 int n,m,k;
13 int ksm(int a,int b){
14     int t;
15     for (t=1;b;b>>=1,a=1LL*a*a%mod) if (b&1) t=1LL*t*a%mod;
16     return t;
17 }
18 int C(int n,int m){
19     int res=1LL*fac[n]*invfac[m]%mod*invfac[n-m]%mod;
20     return res;
21 }
22 int main(){
24     int lim=max(n,k);
25     fac[0]=1;
26     for (int i=1;i<=lim;i++) fac[i]=1LL*fac[i-1]*i%mod;
27     inv[1]=1;
28     for (int i=2;i<=lim;i++) inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
29     invfac[0]=1;
30     for (int i=1;i<=lim;i++) invfac[i]=1LL*invfac[i-1]*inv[i]%mod;
31     int ans=0;
32     if (m==1){
33         printf("%d\n",ksm(k,n));
34         return 0;
35     }
36     for (int a=1;a<=min(n,k);a++){
37         int res=0;
38         for (int i=0;i<a;i++){
39             int tmp=1LL*C(a,i)*ksm(a-i,n)%mod;
40             if (i&1) tmp=mod-tmp;
41             res=(res+tmp)%mod;
42         }
43         res=1LL*res*res%mod;
44         res=1LL*res*C(k,a)%mod;
45         for (int b=max(2*a-k,0);b<=a;b++){
46             ans=(ans+1LL*res*C(a,b)%mod*C(k-a,a-b)%mod*ksm(b,n*(m-2))%mod)%mod;
47         }
48     }
49     printf("%d\n",ans);
50     return 0;
51 }

posted @ 2017-11-14 21:56  chenyushuo  阅读(356)  评论(0编辑  收藏  举报