hdu 6395Sequence【矩阵快速幂】【分块】
Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1951 Accepted Submission(s): 750
Problem Description
Let us define a sequence as below
⎧⎩⎨⎪⎪⎪⎪⎪⎪F1F2Fn===ABC⋅Fn−2+D⋅Fn−1+⌊Pn⌋
Your job is simple, for each task, you should output Fn module 109+7.
Input
The first line has only one integer T, indicates the number of tasks.
Then, for the next T lines, each line consists of 6 integers, A , B, C, D, P, n.
1≤T≤200≤A,B,C,D≤1091≤P,n≤109
Sample Input
2 3 3 2 1 3 5 3 2 2 2 1 4
Sample Output
36 24
Source
2018 Multi-University Training Contest 7
Recommend
chendu | We have carefully selected several similar problems for you: 6408 6407 6406 6405 6404
学习了一个新的递推求数的方法
通过构造矩阵 用矩阵递推 用矩阵快速幂优化 可以用来求一个随机的很大的数的值
矩阵快速幂的写法 O(logn)
这道题还有一点特别的是 后面的常数项是会变化的 但是他是分段的
所以就分块的来求 因为要知道乘的次数所以每次要求一下这个区间有多少个数
c++TLE g++AC
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std;long long t, a, b, c, d, p, n;
const int mod = 1e9 + 7;
struct mat{long long m[3][3];mat(){memset(m, 0, sizeof(m));}void init(){memset(m, 0, sizeof(m));for(int i = 0; i < 3; i++){m[i][i] = 1;}}friend mat operator * (mat a, mat b){mat c;for(int i = 0; i < 3; i++){for(int j = 0; j < 3; j++){for(int k = 0; k < 3; k++){c.m[i][j] += a.m[i][k] * b.m[k][j];c.m[i][j] %= mod;}}}return c;}
};mat pow_mat(mat a, int b)
{mat c;c.init();while(b){if(b & 1){c = c * a;}a = a * a;b >>= 1;}return c;
}int main()
{scanf("%lld", &t);while(t--){scanf("%lld%lld%lld%lld%lld%lld", &a, &b, &c, &d, &p, &n);if(n == 1){printf("%lld\n", a);continue;}mat f;f.m[0][0] = d;f.m[1][0] = c;f.m[2][0] = f.m[0][1] = f.m[2][2] = 1;mat g;g.m[0][0] = b;g.m[0][1] = a;if(p >= n){for(long long i = 3, j; i <= n; i = j + 1){j = p / (p / i);//个数g.m[0][2] = p / i;mat po = pow_mat(f, min(j - i + 1, n - i + 1));g = g * po;}}else{for(long long i = 3, j; i <= p; i = j + 1){j = p / (p / i);g.m[0][2] = p / i;mat po = pow_mat(f, j - i + 1);g = g * po;}mat po;g.m[0][2] = 0;if(p < 3){po = pow_mat(f, n - 2);}else{po = pow_mat(f, n - p);}g = g * po;}printf("%lld\n", g.m[0][0]);}return 0;
}
转载于:https://www.cnblogs.com/wyboooo/p/9643388.html
hdu 6395Sequence【矩阵快速幂】【分块】相关推荐
- HDU 5411(矩阵快速幂)
本题目意思: 给定n个点,给定一个n*n的转移矩阵,要求求出所有长度不大于M的序列个数,(M<=1E5 , n <= 50) 分析: 记 d[i][j] 生成至多长度为i的且以j开头的所有 ...
- hdu 1757(矩阵快速幂)
题意:公式如下 If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a ...
- HDU 6185 Covering 矩阵快速幂 递推
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...
- hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x 求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...
- 数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂
Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only ...
- 【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)
题干: Read the program below carefully then answer the question. #pragma comment(linker, "/STACK ...
- HDU 1757 A Simple Math Problem(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + -- ...
- HDU6395 Sequence(矩阵快速幂+数论分块)
题意: F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n 给定ABCDPn,求F(n) mod 1e9+7 思路: P/n在一段n里是不变的,可以数论分块,再在每一段里 ...
- HDU 2276 Kiki Little Kiki 2 (位运算+矩阵快速幂)
HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意: 一排灯,开关 ...
最新文章
- 中国非动物胶市场来产销需求及发展潜力研究报告2022版
- 在.Net framework下遍历XML文挡树的两种算法
- 快手基于RocketMQ的在线消息系统建设实践
- STL--lower_bound()upper_bound();
- OpenCV学习笔记四-image的一些整体操作
- 技术管理者怎样跳出“泥潭”
- 在建工程直接费用化_临夏州开展建设工程安全专项整治
- java 方法 示例_Java方法参考类型和示例
- Tensorflow之计算tensor平均值
- MP4文件中提取H264码流保存成H264文件
- orbslam2稠密版建图
- pyqt5 笔记(三)py2exe 实现代码打包exe
- 聊聊pert图的那些事儿~
- SAP:SMARTFORM打开WORD文档出错,或无法编辑
- 大型网站--负载均衡架构
- 幼儿园体育游戏电子计算机教案,幼儿园体育游戏《学会跳绳》教案三篇
- 3.Timing Constraints
- Python之爬虫 搭建代理ip池
- jupyter notebook如何查看函数
- 精美好用的思维导图插件,无缝对接各种前端框架,快来围观吧
热门文章
- 在Python中使用MongoDB
- 甲骨文谷歌继续打官司:美最高法院同意复审 Java API 版权诉讼案
- Spring boot 项目目录结构
- java游戏将相_(Java)算法——位运算基础及基本应用
- established 太多_ss -s closed过多,NON_ESTABLISHED告警
- index.php后有乱码后缀,phpExcel在线下wamp环境下,正常导出,同样的代码到线上Linux导出文件无法打开,修改文件后缀为.xls后乱码...
- python mysql数据库的高级应用_Python之路第十二天,高级(5)-Python操作Mysql,SqlAlchemy...
- html5圆形导航菜单,圆滑细腻,那些使用圆形导航菜单的漂亮网页设计
- linux服务器调优列表,2018-10-24(linux服务器常见的各种参数调优)
- PHP被浏览器解释成注释,HTML+CSS入门 在HTML中嵌入的php代码会被浏览器注释掉如何解决...