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;
}
hdu 6395Sequence【矩阵快速幂】【分块】的更多相关文章
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- hdu6395 (矩阵快速幂+分块)
Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- 随手练——HDU 5015 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...
- HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识
求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...
- How many ways?? HDU - 2157 矩阵快速幂
题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...
- HDU 5950 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1757 矩阵快速幂 **
一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...
随机推荐
- 阿里云被挖矿使用,导致cpu长期处于100%,ddgs进程,xWx3T进程,关于redis密码
1.使用top命令,查看到一个叫xWx3T的进程cpu占用99.8%,由于我的阿里云是单核的,所以最高只能100%. 把它用kill命令杀死后,过一会儿又启动了,又占用100%. 使用ps -ef可以 ...
- 图表ASP:Chart
ASP:Chart 属性 System.Web.UI.DataVisualization.Charting 命名空间包含用于图表 Web 服务器控件的方法和属性. 详细了解Chart类http:// ...
- 创建Maven创建src/main/java提示反复
建立好一个Maven项目后.假设Java Resources资源文件下没有src/main/java目录,而且在手动创建这个文件时提示"已存在文件". 这说明,在这个项目配置中已经 ...
- Eclipse------新建文件时没有JSP File解决方法
1.为没有web选项的eclipse添加web and JavaEE插件 .在Eclipse中菜单help选项中选择install new software选项 .在work with 栏中输入 Ju ...
- Unique ID Generate Notes
Unique ID generation in distributed systems http://www.slideshare.net/davegardnerisme/unique-id-gene ...
- D盾 v2.0.6.42 测试记录
0x01 前言 之前发了一篇博客<Bypass D盾_IIS防火墙SQL注入防御(多姿势)>,D哥第一时间联系我,对问题进行修复.这段时间与D哥聊了挺多关于D盾这款产品的话题,实在是很佩服 ...
- IIS中采用ISAPI-Rewrite防盗链
本规则支持白名单排除式防盗链,搜索引擎友好(不屏蔽),被盗链后的错误提示转向,支持各种文件类型,经作者亲验真的能用,第一时间在itmop.com原创发表,请继续往下阅读. 近来小站遇到了盗链问题,至使 ...
- [Python] Python 调用 C 共享库
Linux/Unix 平台下共享库(Shared Library)文件后缀 .so:在 Windows 平台称为动态链接库(Dynamic Link Library),文件名后缀为 .dll. 利用 ...
- 在recycler中写的布局不起作用
把 LinearLayout 改成 RelativeLayout ok了 创建的两种方式 1.LayoutInflater.from(parent.getContext()).inflate(R. ...
- 【python3】Mac下selenium3+chrome驱动+python3
环境: python3.6.4 seleinum3.11 事先准备好python3 环境.安装谷歌浏览器 1 安装seleinum pip3 install selenium 2 安装chrome ...