题目:给你一个n行m列的网格,再给你k中颜色,问让你对其染色有多少种方法。要求对于任意1<=i<=m,都有1~i的颜色种数等于i+1~m的颜色种数。
n,m<=1000,k<=1e6,答案对1e9+7取模
思路:我们可以发现第1列和第m列的颜色种数一定要一样,否则不符合要求。而且2~m-1列中出现的颜色一定要是第1列和第m列的共有的颜色。
dp[i]表示用i种颜色染n格的方法数
dp[i]=i^n-sigma(C(j,i)*dp[j])
然后枚举共有的颜色i,异色j,累加
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
// 0x3f3f3f3f
const LL MOD=1e9+7;
const int maxn=1e6+50;
LL fac[maxn],Inv[maxn],dp[1050];
LL pow_mod(LL a,LL b){
LL res=1;
while(b){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b/=2;
}
return res;
}
LL C(int n,int m){
return fac[n]*Inv[m]%MOD*Inv[n-m]%MOD;
}
int main(){
int n,m,k;
fac[0]=1;
Inv[0]=1;
for(int i=1;i<=1000000;i++){
fac[i]=fac[i-1]*(LL)i%MOD;
Inv[i]=pow_mod(fac[i],MOD-2);
}
while(~scanf("%d%d%d",&n,&m,&k)){
if(m==1){
printf("%lld\n",pow_mod((LL)k,(LL)n));
continue;
}
for(int i=1;i<=k&&i<=n;i++){
dp[i]=pow_mod((LL)i,(LL)n);
for(int j=1;j<i;j++){
dp[i]=(dp[i]-(C(i,j)*dp[j]%MOD))%MOD;
dp[i]=(dp[i]+MOD)%MOD;
}
}
LL ans=0;
if(m==2){
for(int i=1;i<=n&&i<=k;i++){
LL tmp=dp[i]*dp[i]%MOD;
tmp=tmp*C(k,i)%MOD*C(k,i)%MOD;
ans=(ans+tmp)%MOD;
}
}
else{
for(int i=1;i<=n;i++)
for(int j=0;j<=n;j++){
if(i+2*j>k||i+j>n) break;
LL tmp=dp[i+j]*dp[i+j]%MOD;
tmp=tmp*C(k,i)%MOD*C(k-i,j)%MOD*C(k-i-j,j)%MOD*pow_mod((LL)i,(LL)n*(m-2));
ans=(ans+tmp)%MOD;
}
}
printf("%lld\n",ans);
}
return 0;
}