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【矩阵快速幂】【分块】的更多相关文章

  1. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  2. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  3. hdu6395 (矩阵快速幂+分块)

    Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...

  4. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  5. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  6. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

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

  8. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  9. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. hdu 1757 矩阵快速幂 **

    一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...

随机推荐

  1. SPREAD for Windows Forms 下箭头追加行

    ''' <summary> ''' 下矢印の動作クラス ''' </summary> ''' <remarks></remarks> Public Cl ...

  2. jackson 转换 enum 类型

    REST API 接口要求 requster json 的 lifeCycle 域只能填 YOUNG, OLD,对于其他的 lifeCycle,都要给 requester 返回 bad request ...

  3. Linux环境SVN命令行使用经验总结(转)

    在windows机器上开发得差不多了之后,打包传送到开发机编译,在开发机上解决编译错误. [缺点] 浪费时间在打包解包,机器间传输代码. 在windows机器上开发之后,check in代码进分支,在 ...

  4. MongoDB副本集的工作原理

    在MongoDB副本集中,主节点负责处理客户端的读写请求,备份节点则负责映射主节点的数据. 备份节点的工作原理过程可以大致描述为,备份节点定期轮询主节点上的数据操作,然后对自己的数据副本进行这些操作, ...

  5. iOS 使用动态库

    苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks ...

  6. MyBatis入门程序之整合Spring

    1.引入相关jar包(版本对应关系3.3.1版本的mybatis和1.1.1的spring-mybatis匹配,3.4.1版本的mybatis和1.3.1的spring-mybatis匹配) 如果不对 ...

  7. 流媒体服务器+EasyDarwin+EasyPusher+VLC+Red5+OBS+Unity+RTSP+RTMP+FFMPEG

    最近有个需求在Unity中直播桌面,着用到了视频流. ------------------------------ VLC自身有流服务器功能,但是非常慢非常慢,还是用VLC拉流吧,好像大家也是这么做的 ...

  8. UITableView-FDTemplateLayoutCell 学习笔记

    本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文  http://everettjf.github.io/2016/03/24/learn-uitabl ...

  9. spring mvc 篇

    [1]spring mvc 实现多文件上传 http://blog.csdn.net/a1314517love/article/details/24183273 http://bbs.csdn.net ...

  10. Git学习之Git 暂存区

    ============================= 修改文件后是否可以直接提交 ============================ (1) 向文件中追加一行内容  $ echo &quo ...