# ZOJ 3690 Choosing number

Choosing number

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

f[i][0]表示第i个人，在1到k之间选择一个数，f[i][1]表示第i个人在k+1到m之间选择一个数。此时前i个人共有f[i][0]+f[i][1]种方法

f[i][0]=f[i-1][0]*(k-1)+f[i-1][1]*k

f[i][1]=f[i-1][0](m-k)+f[i-1][1]*(m-k)

f[1][0]=k,f[1][1]=m-k;

ans=f[i][0]+f[i][1]

ans=(f[1][0]，f[1][1]) |k-1，m-k|^(n-1)

|k，   m-k|

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
const ll mod=1e9+7;

using namespace std;

struct matrix
{
ll f[2][2];
};

matrix mul(matrix a,matrix b)//矩阵乘法
{
matrix c;
memset(c.f,0,sizeof c.f);

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int 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 fun(matrix a,ll n)//快速幂
{
matrix s;
s.f[0][0]=1;s.f[1][1]=1;
s.f[0][1]=0;s.f[1][0]=0;

while(n)
{
if(n&1)
s=mul(s,a);
a=mul(a,a);
n=n>>1;
}
return s;
}

int main()
{
ll n,k,m;
while(cin>>n>>m>>k)
{
matrix e;
e.f[0][0]=k-1;e.f[1][1]=m-k;
e.f[0][1]=m-k;e.f[1][0]=k;
e=fun(e,n-1);
ll ans;
ans=( k*e.f[0][0]+(m-k)*e.f[1][0]+k*e.f[0][1]+(m-k)*e.f[1][1] )%mod;

cout<<ans<<endl;

}
return 0;
}


#### 【ZOJ 3690】 Choosing number (矩阵快速幂)

2015-10-27 20:02:33

#### zoj 3690 Choosing number

2013-08-26 19:11:26

#### ZOJ 3690 Choosing number（矩阵快速幂）

2014-09-27 21:49:03

#### 【矩阵快速幂】ZOJ 3690 Choosing number

2015-07-11 18:54:21

#### ZOJ - 3690 Choosing number 矩阵快速幂

2015-05-30 18:30:26

#### zoj 3690 Choosing number(矩阵乘法+dp)

2014-03-12 20:07:39

#### zoj 3690 Choosing number 递推+矩阵快速幂

2013-03-31 15:10:10

#### ZOJ 3690 Choosing number（dp矩阵优化）

2015-08-17 01:55:46

#### POJ 2718 Smallest Difference(贪心 or next_permutation()暴力枚举)

2015-12-15 15:54:52

#### ZOJ 3690

2013-09-12 08:17:07