Description
There are n people standing in a row. And There are m numbers, 1.2...m. Every one should choose a number. But if two persons standing adjacent to each other choose the same number, the number shouldn't equal or less than k. Apart from this rule, there are no more limiting conditions.
And you need to calculate how many ways they can choose the numbers obeying the rule.
Input
There are multiple test cases. Each case contain a line, containing three integer n (2 ≤ n ≤ 108), m (2 ≤ m ≤ 30000), k(0 ≤ k ≤ m).
Output
One line for each case. The number of ways module 1000000007.
Sample Input
4 4 1
Sample Output
216
#include<stdio.h> #include<algorithm> #include<map> #include<string.h> #include<math.h> #define lson l,m,rt <<1 #define rson m+1,r,rt << 1 | 1 #define LL long long #define INF 0x3f3f3f3f const int maxn = 100005; using namespace std; #define LL long long const LL mod=1e9+7; struct matrix { LL f[2][2]; }; matrix mul(matrix a,matrix b)//矩阵乘法 { matrix c; LL i,j,k; memset(c.f,0,sizeof(c.f)); for(i=0; i<2; i++) { for(j=0; j<2; j++) for(k=0; k<2; k++) c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j])%mod; } return c; } matrix ksm(matrix e,LL n)//快速幂 { matrix s; s.f[0][0]=s.f[1][1]=1; s.f[0][1]=s.f[1][0]=0; while(n) { if(n&1) s=mul(s,e); e=mul(e,e); n=n>>1; } return s; } matrix e; int main() { LL n,m,k; while(cin>>n>>m>>k) { e.f[0][0]=m-k; e.f[0][1]=k; e.f[1][0]=m-k; e.f[1][1]=k-1; e=ksm(e,n-1); LL ans; ans=((m-k)*e.f[0][0]+k*e.f[1][0]+(m-k)*e.f[0][1]+k*e.f[1][1])%mod; cout<<ans<<endl; } return 0; } /* 思路: f[i],表示前i-1个人的选择,且第i个人选择大于k的值 g[i], 表示前i-1个人的选择,且第i个人选择小于k的值 可得到递推式 f[i]=(m-k)*f[i-1]+(m-k)*g[i-1] g[i]=k*f[i-1]+(k-1)*g[i-1] 可得到矩阵: |f[i] g[i]|=|f[i-1] g[i-1]|*|m-k k | |m-k k-1| |f[n] g[n]|=|f[1] g[1]|* |m-k k |^(n-1) |m-k k-1| ans=f[n]+g[n]; */