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

题意:有n个人,1到m个数。这n个人。每人选一个数字,要求相邻的两个人选择的数要么不相等,要么相等时大于k

题解:dp[i][0]:第i个人选大于k的数的最优解,dp[i][1]:第i个人选小于等于k的数的最优解。

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

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

构造矩阵:  |  dp[i][0]     |           |  m-k ,m-k |         |  dp[i-1][0]  |

=                           *

|  dp[i][1]     |          |   k     ,k-1  |          |   dp[i-1][1]  |

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#define ll long long
#define mod 1000000007
using namespace std; typedef vector<ll>vec;
typedef vector<vec>mat; ll n,m,k; mat mul(mat &A,mat &B) {
mat C(A.size(),vec(B[0].size()));
for(int i=0; i<A.size(); i++) {
for(int k=0; k<B.size(); k++) {
for(int j=0; j<B[0].size(); j++) {
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
}
}
}
return C;
} mat pow_mod(mat A,ll x) {
mat B(A.size(),vec(A.size()));
for(int i=0; i<A.size(); i++) {
B[i][i]=1;
}
while(x>0) {
if(x&1)B=mul(B,A);
A=mul(A,A);
x>>=1;
}
return B;
} int main() {
//freopen("test.in","r",stdin);
while(~scanf("%lld%lld%lld",&n,&m,&k)) {
mat A(2,vec(2));
A[0][0]=m-k,A[0][1]=m-k;
A[1][0]=k,A[1][1]=k-1;
A=pow_mod(A,n-1);
ll ans=(A[0][0]+A[1][0])*(m-k)%mod+(A[0][1]+A[1][1])*k%mod;
printf("%lld\n",ans%mod);
}
return 0;
}

ZOJ 3690 Choosing number(dp矩阵优化)的更多相关文章

  1. ZOJ 3690 Choosing number(矩阵)

    Choosing number [题目链接]Choosing number [题目类型]矩阵 &题解: 这题就和已经dp极像了,所以找方程就很困难了.可以这样找: 设f(n)是前n-1个人已经 ...

  2. zoj 3690 Choosing number

    题意    就是说给你 N 个人站成一排,现在每个人都可以选择 1-M 中间的任意一个数字,相邻的两个人数字相同,则他必须是是 >  K 的  问方案总数: 方法    先求出递推式,然后用矩阵 ...

  3. hdu 4576(简单概率dp | 矩阵优化)

    艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle?  感觉很不公平.大家算法都一致,因为我程序没有那 ...

  4. CF1151F Sonya and Informatics (计数dp+矩阵优化)

    题目地址 Solution (duyi是我们的红太阳) (这里说一句:这题看上去是一个概率dp,鉴于这题的概率dp写法看上去不好写,我们其实可以写一个计数dp) 首先拿到这个题目我们要能设出一个普通d ...

  5. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  6. New Year and Old Subsequence CodeForces - 750E (dp矩阵优化)

    大意: 给定字符串, 每次询问区间[l,r]有子序列2017, 无子序列2016所需要删除的最小字符数 转移用矩阵优化一下, 要注意$(\mathbb{Z},min,+)$的幺元主对角线全0, 其余全 ...

  7. BZOJ4000 [TJOI2015]棋盘 【状压dp + 矩阵优化】

    题目链接 BZOJ4000 题解 注意题目中的编号均从\(0\)开始= = \(m\)特别小,考虑状压 设\(f[i][s]\)为第\(i\)行为\(s\)的方案数 每个棋子能攻击的只有本行,上一行, ...

  8. [Vijos1067]Warcraft III 守望者的烦恼(DP + 矩阵优化)

    传送门 可知 f[i] = f[i - 1] + f[i - 2] + ... + f[i - k] 直接矩阵优化就好了 #include <cstdio> #include <cs ...

  9. BZOJ 1009: [HNOI2008]GT考试(kmp+dp+矩阵优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 思路:真的是好题啊! 对于这种题目,很有可能就是dp,$f[i][j]$表示分析到第 ...

随机推荐

  1. HDU 3861--The King’s Problem【scc缩点构图 &amp;&amp; 二分匹配求最小路径覆盖】

    The King's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. Spring经常使用属性的注入及属性编辑器

    对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...

  3. ContextMenu的使用具体解释

    二话不说,先上图: 能够非常easy看到这是一个类似于Dialog悬浮在活动上的控件,它是由被注冊的view长按所触发的. 当然啦,也有其它的实现方式,这里就先介绍一下系统的ContextMenu:( ...

  4. 王立平--TF卡

    最终知道TF卡是什么了... TF卡又称microSD,是一种极细小的快闪存储器卡,由SanDisk(闪迪)公司发明创立. 这样的卡主要于手机使用.但因它拥有体积极小的长处,随着不断提升的容量. 它慢 ...

  5. 院校-美国:哈佛大学(Harvard University)

    ylbtech-院校-美国:哈佛大学(Harvard University) 哈佛大学(Harvard University),简称“哈佛”,坐落于美国马萨诸塞州波士顿都市区剑桥市,是一所享誉世界的私 ...

  6. Ubuntu18.04修改Hostname

    1. 设置新的hostnamesudo hostnamectl set-hostname newNameHere 2. 修改配置文件使hostname可以保存编辑这个文件: /etc/cloud/cl ...

  7. ubuntu下谷歌浏览器字体模糊解决方案

    一般来说,这种问题应该是缺少字体造成的,因此重新安装字体就可以解决问题. 1.双系统下,查找Windows C盘下:Windows/font/???? ???可在3中查找相应字体 2.分别双击安装那两 ...

  8. Hihocoder1458-Parentheses Matching(stack,vector)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given a string of balanced parentheses output all the matchin ...

  9. 一篇文章彻底弄懂Base64编码原理(转载)

    在互联网中的每一刻,你可能都在享受着Base64带来的便捷,但对于Base64的基础原理又了解多少?今天这篇博文带领大家了解一下Base64的底层实现. Base64的由来 目前Base64已经成为网 ...

  10. IOS - plist使用

    //1 可读取,不可写入工程下的plist文件: //    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"O ...