bzoj5015 [Snoi2017]礼物 矩阵快速幂+二项式展开
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=5015
题解
设 \(f_i\) 表示第 \(i\) 个朋友的礼物,\(s_i\) 表示从 \(1\) 到 \(i\) 的 \(f_i\) 的和。
\]
考虑用矩阵维护转移,但是这个 \(i^k\) 不太方便转移。
发现 \(k \leq 10\),可以考虑使用二项式展开。
\]
所以可以用矩阵维护一下 \(i^j(0 \leq j \leq k)\) 转移就可以了。
时间复杂度 \(O(k^3\log n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}
const int N = 13 + 3;
const int P = 1e9 + 7;
ll T;
int k, n;
inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
	return ans;
}
struct Matrix {
	int a[N][N];
	inline Matrix() { memset(a, 0, sizeof(a)); }
	inline Matrix(const int &x) {
		memset(a, 0, sizeof(a));
		for (int i = 1; i <= n; ++i) a[i][i] = x;
	}
	inline Matrix operator * (const Matrix &b) {
		Matrix c;
		for (int k = 1; k <= n; ++k)
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= n; ++j) sadd(c.a[i][j], (ll)a[i][k] * b.a[k][j] % P);
		return c;
	}
} A, B;
inline Matrix fpow(Matrix x, ll y) {
	Matrix ans(1);
	for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
	return ans;
}
inline void work() {
	B.a[3][1] = 1;
	A.a[1][1] = 0, A.a[1][2] = 1;
	A.a[2][1] = 0, A.a[2][2] = 2;
	A.a[3][3] = 1;
	for (int i = 4; i <= k + 3; ++i)
		for (int j = 3; j <= i; ++j) A.a[i][j] = smod(A.a[i - 1][j - 1] + A.a[i - 1][j]);
	for (int i = 1; i <= n; ++i) sadd(A.a[1][i], A.a[n][i]), sadd(A.a[2][i], A.a[n][i]);
	printf("%d\n", B.a[1][1]);
}
inline void init() {
	read(T), read(k);
	n = k + 3;
}
int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
bzoj5015 [Snoi2017]礼物 矩阵快速幂+二项式展开的更多相关文章
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
		题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ... 
- VOJ 1049送给圣诞夜的礼物——矩阵快速幂模板
		题意 顺次给出 $m$个置换,反复使用这 $m$ 个置换对一个长为 $n$ 初始序列进行操作,问 $k$ 次置换后的序列.$m<=10, k<2^31$. 题目链接 分析 对序列的置换可表 ... 
- hdu3483之二项式展开+矩阵快速幂
		A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ... 
- HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)
		Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ... 
- 广工十四届校赛 count 矩阵快速幂
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ... 
- 【BZOJ3328】PYXFIB(单位根反演,矩阵快速幂)
		[BZOJ3328]PYXFIB(单位根反演,矩阵快速幂) 题面 BZOJ 题解 首先要求的式子是:\(\displaystyle \sum_{i=0}^n [k|i]{n\choose i}f_i\ ... 
- 一些特殊的矩阵快速幂 hdu5950 hdu3369 hdu 3483
		思想启发来自, 罗博士的根据递推公式构造系数矩阵用于快速幂 对于矩阵乘法和矩阵快速幂就不多重复了,网上很多博客都有讲解.主要来学习一下系数矩阵的构造 一开始,最一般的矩阵快速幂,要斐波那契数列Fn=F ... 
- hdu5171(矩阵快速幂)
		传送门:GTY's birthday gift 题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法 ... 
- HDU4686——Arc of Dream矩阵快速幂
		题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4686 题目大意: 已知a0=A0, ai=Ax*ai-1+Ay; b0=B0, bi=Bx*bi-1 ... 
随机推荐
- 基于vue模块化开发后台系统——构建项目
			文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 在熟悉上一篇说到准备工具之后, ... 
- C++ Boost库的编译及使用
			https://www.jianshu.com/p/de1fda741beb https://www.cnblogs.com/weizhixiang/p/5804778.html Windows编译 ... 
- 安装 windows 2008 解决 gpt 分区问题
			新服务器,4T硬盘,U盘安装Windows Server 2008 R2. 把2008的镜像用UltraISO写入U盘. 安装到分区那块,主分区200G,剩余分区系统自动给分为: 2T + 剩余 两块 ... 
- Node - 模块加载与 lerna 提升
			从node_modules 加载模块的过程 如果要加载的模块非核心模块,并且路径不是'/'. '../'和'./'开头,这个模块就会从当前文件夹递归向上在node_modules文件夹中寻找这个模块. ... 
- 【MM系列】SAP MM模块-委外采购订单 把Warning转换成Error信息提示
			公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-委外采购订单 把W ... 
- URIs, URLs, and URNs
			首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体 ... 
- python基础-9.1 面向对象进阶 super 类对象成员 类属性 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理
			上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ... 
- (4.14)mysql备份还原——mysql物理热备工具之ibbackup
			关键词:mysql热备工具,ibbackup,mysql物理备份工具 1. 准备 ibbackup 是 InnoDB 提供的收费工具,它支持在线热备 InnoDB 数据,主要有以下特性: * Onli ... 
- Vue.js 源码学习笔记
			最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ... 
- ssh远程钥匙对连接
			1.服务器必须启动ssh服务 2.在客户机执行命令:ssh-keygen -t rsa 两次回车即可 3.在客户机家目录下的.ssh\下生成钥匙对 4.将公钥传输到要连接的服务器主机要连接的用户家目录 ... 
