ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)
We consider problems concerning the number of ways in which a number can be written as a sum. If the order of the terms in the sum is taken into account the sum is called a composition and the number of compositions of n is denoted by c(n). Thus, the compositions of 3 are
- 3 = 3
- 3 = 1 + 2
- 3 = 2 + 1
- 3 = 1 + 1 + 1
So that c(3) = 4.
Suppose we denote by c(n, k) the number of compositions of n
with all summands at least k. Thus, the compositions of 3 with all
summands at least 2 are
- 3 = 3
The other three compositions of 3 all have summand 1, which is less than 2. So that c(3, 2) = 1.
Input
The first line of the input is an integer t (t <= 30), indicate the number of cases.
For each case, there is one line consisting of two integers n k (1 <= n <= 109, 1 <= k <= 30).
Output
Output c(n, k) modulo 109 + 7.
Sample Input
2
3 1
3 2
Sample Output
4
1
题意:给定N,K,问N可以由多少个不小于K的数组合起来。
思路:当K=1时,就是隔板法,组合数之和,答案是2^(N-1) ;当K>1;可以得到方程dp[i]=dp[i-1]+dp[i-k];
我们用dpi表示和为i有多少种方案,那么考虑最后一个数,如果最后一个数=k,那么其方案数=dp[i-k];如果>k,那么其方案数=dp[i-1]; 想到这里就知道用矩阵乘法来做了。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int Mod=1e9+;
int L;
int qpow(int a,int x)
{
int res=; while(x){
if(x&) res=1LL*res*a%Mod;
a=1LL*a*a%Mod; x>>=;
} return res;
}
struct mat
{
int mp[][];
mat(){memset(mp,,sizeof(mp));}
mat friend operator*(mat a,mat b){
mat res;
rep(k,,L)
rep(i,,L)
rep(j,,L)
(res.mp[i][j]+=1LL*a.mp[i][k]*b.mp[k][j]%Mod)%=Mod;
return res;
}
mat friend operator^(mat a,int x){
mat res; rep(i,,L) res.mp[i][i]=;
while(x){
if(x&) res=res*a;
a=a*a; x>>=;
} return res;
}
};
int main()
{
int T,N,K;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&K);
if(N<K) {puts(""); continue;}
if(K==){ printf("%d\n",qpow(,N-)); continue;}
mat ans,base; L=K;
ans.mp[][]=;
base.mp[][]=base.mp[][K]=;
rep(i,,K) base.mp[i][i-]=;
ans=(base^(N-K))*ans;
printf("%d\n",ans.mp[][]);
}
return ;
}
ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)的更多相关文章
- 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2745 Solved: 1694[Submit][Statu ...
- 【BZOJ 2323】 2323: [ZJOI2011]细胞 (DP+矩阵乘法+快速幂*)
2323: [ZJOI2011]细胞 Description 2222年,人类在银河系外的某颗星球上发现了生命,并且携带了一个细胞回到了地球.经过反复研究,人类已经完全掌握了这类细胞的发展规律: 这种 ...
- BZOJ-1875 HH去散步 DP+矩阵乘法快速幂
1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1196 Solved: 553 [Submit][Statu ...
- Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)
/* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...
- 洛谷 P4910 帕秋莉的手环 矩阵乘法+快速幂详解
矩阵快速幂解法: 这是一个类似斐波那契数列的矩乘快速幂,所以推荐大家先做一下下列题目:(会了,差不多就是多倍经验题了) 注:如果你不会矩阵乘法,可以了解一下P3390的题解 P1939 [模板]矩阵加 ...
- 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2
1732 Fibonacci数列 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 在“ ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...
- [vijos1725&bzoj2875]随机数生成器<矩阵乘法&快速幂&快速乘>
题目链接:https://vijos.org/p/1725 http://www.lydsy.com/JudgeOnline/problem.php?id=2875 这题是前几年的noi的题,时间比较 ...
随机推荐
- Codeforces 839B - Game of the Rows
839B - Game of the Rows 思路:先放4个的,然后再放2个的,最后再放1个的. 代码: #include<bits/stdc++.h> using namespace ...
- box-shadow四周都有阴影
<style> .shadow{ -webkit-box-shadow: #666 0px 0px 10px; -moz-box-shadow: #666 0px 0px 10px; bo ...
- Xshell Xftp 免费版 (xshell6 评估期已过 解决办法)
xshell6 评估期已过,因为下载的版本是evaluation版本,是有期限的. 大家可以修改为Home and school use 的版本,这样就不会出现这个提示了. 具体的操作步骤如下: 1. ...
- English trip -- Review Unit6 Time 时间
It's at seven o'clock 整点 7点整 It's at half past seven or It's seven-thirty7点30 It's at seven fi ...
- (Gorails)vuejs系列视频: Webpacker/vue-resource(不再为官方推荐)。
频:https://gorails.com/episodes/using-vuejs-for-nested-forms-part-1?autoplay=1 在嵌套表格上使用vue.js. 在appli ...
- 1月11日Atom 插件安装。
查看已安装的Atom插件(前提:已经安装Atom) 打开终端 输入apm ls命令,回车. 未安装任何插件时,显示如下 Built-in Atom packages (89) ...此处省略... / ...
- 广播 (Broadcast)
广播 :在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内 ...
- Spring web flow的意义
为什么要使用Spring web flow呢? 这里需要强调的一点就是,但凡一个技术的出现和流行,必有其适用的环境和存在的意义. Spring web flow加强了中央集权,这个该怎么理解呢?以往我 ...
- 从0开始接触html--第一天学习内容总结
第一天 总结: h1-h6 p 段落 hr br 有序 ol li 无序 ul li 定义列表 dl dt dd 块级元素:独占一行,h1-h6 p hr div 行内元素:共占一行, em和i st ...
- Logger.getLogger()和LogFactory.getLog()的区别
第一.Logger.getLogger()和LogFactory.getLog()的区别 1.Logger.getLogger()是使用log4j的方式记录日志: 2.LogFactory.getLo ...