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 ...
随机推荐
- [转]玩转Google开源C++单元测试框架Google Test系列(gtest)(总)
文章转载自CoderZh的技术博客 地址:https://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Goog ...
- Oracle Flashback Transaction Query with Oracle Flashback Version Query
Oracle Flashback Transaction Query with Oracle Flashback Version Query In this example, a database a ...
- Ajax初探
一.AJAX准备知识:JSON 1.stringify与parse方法 2.和XML的比较 二.AJAX简介 AJAX常见应用情景 AJAX的优缺点 优点: 三.jQuery实现的AJAX $.aja ...
- django配置mysql报错 no model named "MySQLdb"
官网上面连接mysql数据库的参数很少,入了不少坑,一直排错和检查参数都没有问题,只能manage.py mirgrate 更新数据库的信息创建数据库的表. 很是郁闷.报了一大堆的错误,大概意思就是说 ...
- java如何台生成二维码详解
现在呢说明页面上展示二维码的两种方式: 1.使用img标签的src来请求生成二维码,后台会直接返回: 2.此处跟上方意思相似,获取到url给img标签设置src属性: 特别注意:如果url有amp;, ...
- mysql修改库、表、字段 字符集,中文排序
查看字段编码: show full columns from t2;show variables like '%character%';show variables like 'collation_% ...
- selenium自动新增店铺
说明:仅作为参考练习,代码中涉及数据均为测试数据. from selenium import webdriver from selenium.webdriver import ActionChains ...
- c++知识点总结3
http://akaedu.github.io/book/ week1 引用:相当于变量的别名.下面r和n就相当于同一回事 ; int &r=n; 引用做函数参数: void swap(int ...
- C# 中常见的控件以及功能
1.StatusBar控件——显示各种状态信息. StatusBar控件可以有状态栏面板(用于显示图标以指示状态)或一系列动画图标(用于指示某个进程正在工作,例如,表示正在保存文档的 Microsof ...
- latex如何给表格添加注释
在latex中,想给表格添加注释,可以使用threeparttable这个包 代码如下: \usepackage{threeparttable} \begin{table*} \begin{three ...