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的题,时间比较 ...
随机推荐
- JDK 12又来了,我学不动了...
写在前面 看到 JDK 12又发布了,萌新不知不觉感觉瑟瑟发抖,从 Java 1.8的函数式编程思维和范式 到 Java 1.9的模块化特性的加持 以及还没来得及深切感受一下 Java 1.11 的 ...
- 《剑指offer》第十五题(二进制中1的个数)
// 面试题:二进制中1的个数 // 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如 // 把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. #inclu ...
- Apache配置文件httpd.conf细说
1.httpd.conf文件位于apache安装目录/conf下2.Listen 88表示监听端口88 此处可以连续写多个端口监听如下: Listen 88 Listen 809 3.目录配置如下: ...
- splice的多种用法
(一)splice的多种用法: splice(n,m) 从索引n开始删除m个.返回删除项组成新数组 splice(n) 从索引n开始删除到末尾 splice(n,m,x) 从索引n开始删除m个,并且把 ...
- Oracle 使用GSON库解析复杂json串
在前文中讲到了如何使用JSON标准库解析json串,参考: Oracle解析复杂json的方法(转) 现补充一篇使用GSON库在Oracle中解析复杂json的方法. GSON串的使用教程参考官方文档 ...
- iOS UI-微博案例(通过代码自定义Cell)
一.Model BWWeiBo数据模型 #import <Foundation/Foundation.h> @interface BWWeiBo : NSObject @property ...
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- 简话Angular 03 Angular内置表达式大全
一句话: 大多数html标签属性和事件都有一个对应的ng指令 说明:这些指令和原生html最大的区别就是可以动态更新.比如一个div的样式用ng-class后,我们就可以随意应用css class. ...
- Python中变量、赋值、浅拷贝、深拷贝
https://www.cnblogs.com/LetMe/p/6724555.html 在理解浅拷贝和深拷贝之前,首先要理解学习一下变量在Python中是怎样存储的: 变量的类型是分值引用与地址引用 ...
- Vysor安装
Vysor安装 Vysor安装