ZOJ 3216 Compositions (矩阵快速幂)
题意:求把 n 拆成几个大于等于 k 的数的和的方案数。
析:根据题目很容易写出递推式,f[i] = f[i-1] + f[i-k],什么意思呢,f[i-1] 表示是进行加 1 操作,那么可以给 n-1 中拆分的任何一个数加1,还有一个就是再加一个数,那么就是 f[i-k]。然后进行构造矩阵。

代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 100 + 10;
const int maxm = 1e6 + 10;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } struct Matrix{
LL a[30][30];
int n;
void init(){ ms(a, 0); }
void normal(){ FOR(i, n, 0) a[i][i] = 1; }
Matrix operator * (const Matrix &rhs){
Matrix res; res.init(); res.n = n;
FOR(i, n, 0) FOR(j, n, 0) FOR(k, n, 0)
res.a[i][j] = (res.a[i][j] + a[i][k] * rhs.a[k][j]) % mod;
return res;
}
}; Matrix fast_pow(Matrix a, int n){
Matrix res; res.n = a.n; res.init(); res.normal();
while(n){
if(n&1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
} LL fast_pow(LL a, int n){
LL res = 1;
while(n){
if(n&1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
if(n < m){ puts("0"); continue; }
else if(n < m + m){ puts("1"); continue; }
else if(m == 1){ printf("%lld\n", fast_pow(2LL, n-1)); continue; }
Matrix x, y; x.init(); y.init(); x.n = y.n = m;
for(int i = 0; i < m; ++i) y.a[0][i] = 1;
x.a[0][0] = x.a[m-1][0] = 1;
for(int i = 1; i < m; ++i) x.a[i-1][i] = 1;
x = y * fast_pow(x, n - m - m + 1);
printf("%lld\n", x.a[0][0]);
}
return 0;
}
ZOJ 3216 Compositions (矩阵快速幂)的更多相关文章
- Choosing number ZOJ - 3690 (矩阵快速幂)
题意:n个人站成一排,每个人任意从1——m中任意取一个数,要求相邻两个人的如果数字相同,数字要大于k. 分划思想推导表达式: 假设 i 个人时.第i个人的选择有两种一种是选择小于等于k的数,另一种 ...
- zoj 2974 Just Pour the Water (矩阵快速幂,简单)
题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...
- ZOJ 2794 Just Pour the Water 【矩阵快速幂】
给你n个杯子,每次有特定的到水规则,倒m次请问最后每个被子里还有多少水 我们很容易发现每次变化的规则相同,那么可以set 一个矩阵存放 然后多次倒水就相当于矩阵相乘,在m 范围达到(1<= M ...
- ZOJ - 2853 Evolution 线性变换变成矩阵快速幂
题意:给你N个数,1~N分别为num[i], 以及T个 (i,j,P) 对于每组(i,j,P),让你将 num[i] 减去 P*num[i] 再把 P*num[i] 加到 num[j] 上.T个 ...
- zoj 2974 Just Pour the Water矩阵快速幂
Just Pour the Water Time Limit: 2 Seconds Memory Limit: 65536 KB Shirly is a very clever girl. ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
随机推荐
- jmeter操作数据库,分布式,在Linux上运行
jmeter操作数据库: 1.在测试计划中导入数据库jar包 2.添加链接数据库信息 3.mysql:jdc:mysql://192.168.1.116:3307/bugfree?allowMulti ...
- java_11接口
1接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成.这样将功能的定义 ...
- Aspose.words四 bookmark
通过添加bookmark书签来添加数据,首先通过方法MoverToBookmark移动指定的标签位置,然后添加数据,添加完成后清除掉bookmark标示. string templateFile = ...
- EF语句拦截器-匹配当前的Controller,Action,User
示例代码,ps:一切都能实现,关键是你尝试的方向,别把简单问题复杂化导致进入死胡同出不来. using Mobile360.Core.Interfaces; using Mobile360.Core. ...
- migrantion
Enable-Migrations - ConfigurationTypeName namespace.DbContext Enable-Migrations命令创建了一个新的Migrations文件 ...
- 9.10 h5日记
9.10 1.什么是属性 属性是表示某些事物的一些特征 2.属性分为标签属性和样式属性,二者的区别在于哪里 标签属性:<img src="01.jpg" width=&quo ...
- 关于opencv中的颜色模型转换之CV_BGR2HSV
1.opencv函数cvCvtColor(rgb_im,hsv_im,CV_BGR2HSV)中使用的RGB颜色空间转到HSV算法: max=max(R,G,B) min=min(R,G,B) if R ...
- Informatica_(4)工作流
三.workflow执行.监控 workflow是PowerCenter的执行单元: 一个workflow包括一个或者多个session(或task). 1.session session是mappi ...
- Informatica_(1)安装
安装961 server和client 0.informatica卸载保证服务(informatica9.6.1)在关闭状态:卸载客户端,应用程序-->informatica-->unin ...
- IIS7中的站点、应用程序和虚拟目录详细介绍
IIS7中的站点.应用程序和虚拟目录详细介绍 这里说的不是如何解决路径重写或者如何配置的问题,而是阐述一下站点(site),应用程序(application)和虚拟目录 (virtual direct ...