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. Win7 在安装vs2010后向sql2008添加SQL_Server_Management详解

    VS2010自带sql server 2008,但自带的版本缺少SQL_Server_Management,解决如下: 安装的先决条件: 1.SQLManagementStudio_x86_CHS(h ...

  2. docker in centos error

    centos 7 Docker 启动了一个web服务 但是启动时 报 WARNING: IPv4 forwarding is disabled. Networking will not work. 网 ...

  3. RAC的搭建(一)--安装环境准备

    软硬件环境准备: 1.1 虚拟环境: VirtualBox上两个虚拟机,3G内存1核 1.2 软件环境: 数据库安装软件:p10404530_112030_LINUX_1of7.zip  p10404 ...

  4. [Windows] 解决 COM Surrogate 错误提示

    运行环境:Windows 8.1 (64bits) 异常描述:打开 "Windows 照片查看器",试图放大或缩小窗体,弹出 "COM Surrogate” 错误. 解决 ...

  5. Java网络编程之查找Internet地址

    一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...

  6. 当本机通过代理服务器上网时,本机无法打开在本机上的虚拟机(oracle vm)linux系统上的网站,但是局域网里的其他机器却可以打开

    当本机通过代理服务器上网时,本机无法打开在本机上的虚拟机(oracle vm)linux系统上的网站,但是局域网里的其他机器却可以打开. 只有取消代理,本机才能访问其虚拟机linux系统上的网站

  7. openldap slapd.conf参数

    已安装系统的 /etc/openldap/slapd.conf 中包含 LDAP 服务器的完整配置文件.在此简述了其中的各个项并说明了必要的调整.以符号 (#) 为前缀的项处于非活动状态.必须取消这个 ...

  8. 基于VLAN的二三层转发

    [章节内容]1 MAC地址2 冲突域和广播域3 集线器.交换机.路由器   3.1 集线器   3.2 网桥和交换机   3.3 路由器4 VLAN   4.1 VLAN帧格式      4.1.1 ...

  9. 【面试题】新东方.NET工程师面试题总结

    1.学校几本(是否统招).英语等级.大学成绩排名Top%几.当前月薪(入职前是否能提供薪资证明材料).期望月薪 二本,统招英语四级排名top10 2.做过的项目技术栈是什么?(例如 .NET.Sql ...

  10. css笔记 - 张鑫旭css课程笔记之 absolute 篇

    absolute地址 absolute绝对定位 绝对定位与浮动鲜为人知的兄弟关系 即是说,absolute后,元素和浮动元素的特性差不多,只不过absolute脱离文档流,元素飘在天上,float还在 ...