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. Get started with ros -- 1

    原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ 一.Introduction: 机器人操作系统(ROS)是使机器人系统的不同部分能够发 ...

  2. _DataStructure_C_Impl:图的邻接矩阵存储

    //_DataStructure_C_Impl:邻接矩阵 #include<stdio.h> #include<stdlib.h> #include<string.h&g ...

  3. hibernate4.3 无法获取数据库最新值

    在用ssh框架的时候遇到一个问题(hibernate版本号4.3) 问题描写叙述:web端和应用程序都能够读写数据库.当应用程序改动数据库后.hibernate无法读取最新值,读出来的一直都是旧数据. ...

  4. iOS 添加Resource bundle target(静态库中使用图片等资源)

    一.首先将资源文件打包成bundle 新建工程:File -> New -> Project... -> OS X -> Framework & Library -&g ...

  5. linux修改文件所属用户和用户组

    使用chown命令可以修改文件或目录所属的用户:       命令:chown 用户 目录或文件名       例如:chown testAdmin /home/work  (把home目录下的xua ...

  6. 发送http请求

    public static String httpGetSend(String url) { String responseMsg = ""; HttpClient httpCli ...

  7. 《Lua程序设计》第7章 迭代器与泛型for 学习笔记

    本章将介绍如何编写适用于泛型for的迭代其(Iterator).7.1 迭代器与closurehttp://www.cnblogs.com/moonlightpoet/p/5685275.html 7 ...

  8. 【ipad神坑】ipad麦克风听不到声音怎么回事 微信QQ语音视频对方都听不到

    今天遇到了这个问题 说话听不见,但是敲击ipad,可以明显的听到击打的声音 siri也是可以听到 上网上找,大多都是说恢复设置,重启,隐私麦克风权限等解决方案 都是浪费感情 全部尝试过了,依然没有用. ...

  9. 【python】flask 开启 debug 模式

    方法一: 直接在run的时候添加debug from flask import Flask app = Flask(__name__) @app.route('/') def hello_world( ...

  10. Esper学习之七:EPL语法(三)

    1.Aggregation 和SQL一样,EPL也有Aggregation,即聚合函数.语法如下: aggregate_function([all|distinct] expression) aggr ...