Analysis: to seek with the capacity of repulsion.
Formula: C (m,k) * (k* (k-1) ^ (n-1) –c (k,k-1) * (k-1) * (k-2) ^ (n-1) + C (k,k-2) * (k-2) * (k-3) ^ (n-1) ...);
Explanation: C (m,k): M color selected K species. k* (k-1) ^ (n-1): At most, there are no more than the color of the K and the adjacent two different colors of the filling method.
/************************************************author:D arktongcreated time:2016/5/31 18:52:26File Name : ff.cpp*************************************************/#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespaceStd;typedefLong Longll;Constll MOD = 1e9 +7;Const intMAXN =1000000+Ten; ll FAC[MAXN], INV[MAXN], Qui[maxn];ll Qui (ll X, ll N) {if(n==0)return 1; if(x==0)return 0; ll ans=1; while(n) {if(n&1) ans=ans*x%MOD; X=x*x%MOD; N>>=1; } returnans;} LL INV (ll x) {returnQui (x, mod-2);}voidinit () {fac[0]=inv[0]=1; for(intI=1; i<maxn;++i) {Fac[i]=fac[i-1]*i%MOD; Inv[i]=INV (fac[i]); }}ll Cnm (ll N, ll m) {if(N<MAXN)returnFAC[N]*INV (fac[n-m]*fac[m]%mod)%MOD; LL A1=1, a2=1; for(LL i=n,j=1; J<=m;++j,--i) {A1=a1*i%MOD; A2=a2*j%MOD; } returnA1*INV (A2)%MOD;} ll CNM (ll N, ll m) {//return fac[n]* (INV (fac[m]) *INV (fac[n-m])%mod)%mod; returnfac[n]* (inv[m]*inv[n-m]%mod)%MOD;}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);init (); intCa=1, TT; scanf ("%d", &TT); while(ca<=TT) {ll n, M, K; scanf ("%lld%lld%lld", &n, &m, &k); ll Sum= K*qui (K-1, N-1)%MOD; for(inti=k-1, d=-1; i>=1;--i,d=-d) {ll S1= (CNM (k, i) *i)%mod*qui (i-1, N-1)%MOD; Sum= ((sum+d*s1)%mod+mod)%MOD; } Sum= SUM*CNM (M, k)%MOD; printf ("Case #%d:%lld\n", ca++, sum); } return 0;}
Uvalive 7040-color