Choosing number 写出递推公式,然后用矩阵的快速幂。

博客描述了一个选数问题,有n个人站成一排,有m个数字1到m可供选择,相邻两人选相同数字时该数字不能小于等于k,需计算符合规则的选数方式数量。输入包含多组测试用例,每组含n、m、k三个整数,输出为方案数取模1000000007的结果。

摘要生成于 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
***********************************************************************************************************************************************************
矩阵的快速幂dp[n][1]即取>=k的数时的方案总数,dp[n][2]即取<k时的方案总数
dp[n][1]=dp[n-1][1]*(m-k)+dp[n-1][2]*(m-k);
dp[n][2]=dp[n-1]*k+dp[n-1][2]*(k-1);
列出矩阵
: m-k m-k dp[n-1][1] dp[n][1]
k k-1 dp[n-1][2] dp[n][2]
可得由矩阵的快速幂快速求解
***********************************************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<algorithm>
 7 #define LL long long
 8 #define MOD 1000000007
 9 using namespace std;
10 LL n,m,k;
11 LL save[5][5];
12 LL mod(LL m,LL n)
13 {
14     LL sum=1;
15     while(n)
16     {
17         if(n&1)
18            sum=(sum*m)%MOD;
19         m=(m*m)%MOD;
20         n=n>>1;
21     }
22     return sum;
23 }
24 int main()
25 {
26     while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF)
27     {
28         if(k==0)
29         {
30             printf("%lld\n",mod(m,n)%MOD);
31             continue;
32         }
33         if(n==1)
34         {
35             printf("%lld\n",m);
36             continue;
37         }
38         save[1][1]=m-k;
39         save[1][2]=m-k;
40         save[2][1]=k;
41         save[2][2]=k-1;
42         LL ans1=m-k;
43         LL ans2=k;
44         n--;
45         while(n)
46         {
47             if(n&1)
48             {
49                 LL temp1=(((ans1*save[1][1])%MOD)+((ans2*save[1][2])%MOD))%MOD;
50                 LL temp2=(((ans1*save[2][1])%MOD)+((ans2*save[2][2])%MOD))%MOD;
51                 ans1=temp1;
52                 ans2=temp2;
53             }
54             n=n>>1;
55             LL a1=(((save[1][1]*save[1][1])%MOD)+((save[1][2]*save[2][1])%MOD))%MOD;
56             LL a2=(((save[1][2]*save[2][2])%MOD)+((save[1][1]*save[1][2])%MOD))%MOD;
57             LL b1=(((save[2][2]*save[2][1])%MOD)+((save[2][1]*save[1][1])%MOD))%MOD;
58             LL b2=(((save[2][2]*save[2][2])%MOD)+((save[2][1]*save[1][2])%MOD))%MOD;
59             save[1][1]=a1; save[1][2]=a2;
60             save[2][1]=b1; save[2][2]=b2;
61 
62         }
63         printf("%lld\n",(ans1+ans2)%MOD);
64     }
65     return 0;
66 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3446454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值