n Grid row, there are M color, ask the exact k color to dye, so that the adjacent lattice color of the scheme number.
Integers n, m, K (1≤n, M≤10^9, 1≤k≤10^6, K≤n, M).
M color take K species C (M, k) This can be put in the last multiply then the problem becomes only k color
The first lattice has K-type coating method The second one has k-1 species, the third is k-1 species.
Altogether is k* (k-1) ^ (n-1) This algorithm only guarantees the adjacent color is different, the total color number does not exceed the K kind of, and does not guarantee exactly to appear the K color namely many forget exactly appears 2 kinds of exactly appear 3 kinds .... Happen to appear k-1 species
We were asking for the right kind of k, and now we're asking for k-1.
So that's (k-1) * (k-2) ^ (n-1) and this is a lot of things.
And so on, then you can use the repulsion principle.
For example, there are 5 colors, choose 4 kinds is
C (5, 4) * (C (4, 4) *4*3^4-c (4, 3) *3*2^4 + C (4, 2) *2*1^4)
Sample Input
2
3 2 2//N m k
3 2 1
Sample Output
Case #1:2
Case #2:0
1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <string>6# include <cmath>7# include <queue>8# include <list>9# define LLLong LongTen using namespacestd; One A Const intMOD =1000000007 ; - - intN, M, K; the LL CM; -LL ck[1000010] ; -LL inv[1000010] ; - + - ll Pow_mod (ll P, ll K) + { ALL ans =1; at while(k) { - if(K &1) ans = ans * p%MOD; -p = (LL) p*p%MOD; -K >>=1; - } - returnans; in } - toll EXT_GCD (ll a,ll b,ll &x,ll &y) {//Extended Euclid + if(a==0&&b==0)return-1; - if(b==0) {x=1, y=0;returnA;} theLL d= EXT_GCD (b,a%b,y,x); *y-= a/b*x; $ returnD;Panax Notoginseng } - //ax = 1 (mod m) thell Inv (ll a,ll m) {//finding inverse of inverse element A to M +LL d,x,y,t =m; AD=EXT_GCD (a,t,x,y); the if(d==1)return(x%t+t)%T; + return-1; - } $ $ -ll Cm (ll N, ll M, ll P)//To find the number of combinations - { theLL a=1, b=1; - if(m>n)return 0;Wuyi while(m) the { -A= (a*n)%p; Wub= (b*m)%p; -m--; Aboutn--; $ } - return(LL) A*INV (b,p)%p;//(A/b)%p equivalent to A * (b,p) Inverse - } - A intLucas (ll N, ll M, ll P)//multiply the N-piecewise recursive solution + { the if(m==0)return 1; - return(LL) Cm (n%p,m%p,p) * (LL) Lucas (n/p,m/p,p)%p; $ } the the voidInit () the { theinv[1] =1 ; - inti; in for(i =2; I <1000010; i++) theInv[i] =INV (i,mod); the } About the intMain () the { the //freopen ("In.txt", "R", stdin); + intT; -scanf"%d", &T); the intCase =0 ;Bayi init (); the while(t--) the { -case++ ; -scanf"%d%d%d", &n, &m, &k); the if(n = =1) the { theprintf"Case #%d:%d\n", case, m); the Continue ; - } the inti; theCM =Cm (m,k,mod); theck[0] =1 ;94 for(i =1; I <= K; i++) theCk[i] = (ck[i-1] * (k-i+1)%mod * Inv[i])%MOD; theLL ans =0, t =1 ; the for(i = k; I >=2; i--)98 { AboutAns = (ans + t*ck[i]*i%mod*pow_mod (i-1, N-1)%mod+mod)%MOD; -T *=-1 ;101 }102printf"Case #%d:%i64d\n", case,ans*cm%MOD);103 104 } the return 0;106}
View Code
CF GYM100548 (number of schemes with different adjacent lattice colors) 2014 XI ' an regional race F-question tolerance principle)