Choosing number

本文探讨了一个涉及特定排列组合限制条件的问题,并通过矩阵快速幂算法提供了高效的解决方案。详细介绍了问题背景、数学模型建立、算法设计及实现过程,并通过示例输入输出展示了算法的应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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];
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值