题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=5015

题解

设 \(f_i\) 表示第 \(i\) 个朋友的礼物,\(s_i\) 表示从 \(1\) 到 \(i\) 的 \(f_i\) 的和。

\[f_i = s_{i-1}+i^k\\s_i = s_{i-1}+f_i = 2s_{i-1}+i^k
\]

考虑用矩阵维护转移,但是这个 \(i^k\) 不太方便转移。

发现 \(k \leq 10\),可以考虑使用二项式展开。

\[(i+1)^k = \sum_{j=0}^k \binom{k}{i}i^j
\]

所以可以用矩阵维护一下 \(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]礼物 矩阵快速幂+二项式展开的更多相关文章

  1. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

  2. VOJ 1049送给圣诞夜的礼物——矩阵快速幂模板

    题意 顺次给出 $m$个置换,反复使用这 $m$ 个置换对一个长为 $n$ 初始序列进行操作,问 $k$ 次置换后的序列.$m<=10, k<2^31$. 题目链接 分析 对序列的置换可表 ...

  3. hdu3483之二项式展开+矩阵快速幂

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  4. HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)

    Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ...

  5. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  6. 【BZOJ3328】PYXFIB(单位根反演,矩阵快速幂)

    [BZOJ3328]PYXFIB(单位根反演,矩阵快速幂) 题面 BZOJ 题解 首先要求的式子是:\(\displaystyle \sum_{i=0}^n [k|i]{n\choose i}f_i\ ...

  7. 一些特殊的矩阵快速幂 hdu5950 hdu3369 hdu 3483

    思想启发来自, 罗博士的根据递推公式构造系数矩阵用于快速幂 对于矩阵乘法和矩阵快速幂就不多重复了,网上很多博客都有讲解.主要来学习一下系数矩阵的构造 一开始,最一般的矩阵快速幂,要斐波那契数列Fn=F ...

  8. hdu5171(矩阵快速幂)

    传送门:GTY's birthday gift 题意:GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法 ...

  9. 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 ...

随机推荐

  1. SpringMVC传参注解@RequestParam,@RequestBody,@ResponseBody,@ModelAttribute

    参考文档:https://blog.csdn.net/walkerjong/article/details/7946109 https://www.cnblogs.com/daimajun/p/715 ...

  2. Java语言支持的变量类型有哪几种

    Java语言支持的变量类型有: 类变量:独立于方法之外的变量,用 static 修饰. 实例变量:独立于方法之外的变量,不过没有 static 修饰. 局部变量:类的方法中的变量. 实例: publi ...

  3. 深入探究JVM(2) - 探秘Metaspace

    Java 8彻底将永久代移除出了HotSpot JVM,将其原有的数据迁移至Java Heap或Metaspace.这一篇文章我们来总结一下Metaspace(元空间)的特性.如有错误,敬请指出,谢谢 ...

  4. sqlserver 获取存储过程执行时间

    use [数据库名]   select last_execution_time '最近一次执行时间'  from sys.dm_exec_procedure_stats  where type='P' ...

  5. gsxt滑动验证码

    最后,谈谈滑动验证码. 目前,工商网站已经全面改版,全部采用了滑动验证码,上面绝大多数思路都失效了.对于滑动验证码,网上能搜到的解决方案基本都是下载图片,还原图片,算出滑动距离,然后模拟js来进行拖动 ...

  6. C#配置IIS站点

    一.源码特点       1.  一些基于ASP.NET应用产品,在用户环境中都无可避免的涉及到部署到目标环境的应用服务器上,而配置站点是此过程的核心步骤,此源码对过程进行了高度封装,从创建IIS所需 ...

  7. Python编程:从入门到实践—类

    创建类 #!/usr/bin/env python# --*-- encoding:utf-8 --*-- class Dog(): """一次模拟小狗的简单尝试&quo ...

  8. 在Linux环境中运行python 项目

    1首先创建一个虚拟环境或者在一个已有的虚拟环境中创建一个django项目 1.1 创建一个虚拟环境: mkvirtualenv my_django115 这会在 ~/Envs 中创建 my_djang ...

  9. Week 8 - 338.Counting Bits & 413. Arithmetic Slices

    338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...

  10. delphi 遍历窗口

    http://blog.163.com/t_form/blog/static/12348523220115132155814/ function EnumWindowsProc_2(hwnd: HWN ...