Sequence

题目传送门

解题思路

可以比较容易的推出矩阵方程,但是由于p/i向下取整的值在变,所以要根据p/i的变化将矩阵分段快速幂。p/i一共有sqrt(p)种结果,所以最多可以分为sqrt(p)段进行快速幂。

代码如下

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; inline int read(){
int res = 0, w = 0; char ch = 0;
while(!isdigit(ch)){
w |= ch == '-', ch = getchar();
}
while(isdigit(ch)){
res = (res << 3) + (res << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -res : res;
} const int N = 100005;
const int mod = 1e9+7;
struct Matrix{
ll m[3][3];
Matrix(){
memset(m, 0, sizeof(m));
}
}; Matrix mul(Matrix& x, Matrix& y)
{
Matrix ans;
for(int i = 0; i < 3; i ++){
for(int j = 0; j < 3; j ++){
for(int k = 0; k < 3; k ++)
ans.m[i][j] = (x.m[i][k] * y.m[k][j] % mod + ans.m[i][j]) % mod;
}
}
return ans;
} Matrix sq_pow(Matrix& x, int k)
{
Matrix t = x;
Matrix ans;
ans.m[0][0] = 1, ans.m[1][1] = 1, ans.m[2][2] = 1;
while(k){
if(k & 1)
ans = mul(ans, t);
t = mul(t, t);
k >>= 1;
}
return ans;
} int main()
{
int t;
cin >> t;
while(t --){
ll a, b;
int c, d, p, n;
cin >> a >> b >> c >> d >> p >> n;
Matrix t;
t.m[0][0] = d, t.m[0][1] = c, t.m[1][0] = 1, t.m[2][2] = 1;
if(n == 1)
printf("%lld\n", a % mod);
else if(n == 2)
printf("%lld\n", b % mod);
else {
for(int i = 3; i <= n; ){
//printf("b: %lld\n", b % mod);
t.m[0][2] = p / i;
if(p / i == 0){
Matrix temp = sq_pow(t, n - i + 1);
b = (b * temp.m[0][0] + a * temp.m[0][1] + temp.m[0][2]) % mod;
break;
}
else {
int j = p / (p / i);
j = min(j, n);
Matrix temp = sq_pow(t, j - i + 1);
ll tb = (b * temp.m[0][0] + a * temp.m[0][1] + temp.m[0][2]) % mod;
ll ta = (b * temp.m[1][0] + a * temp.m[1][1] + temp.m[1][2]) % mod;
a = ta, b = tb;
i = j + 1;
}
}
printf("%lld\n", b % mod);
}
}
return 0;
}

hdu6395 Sequence(分段矩阵快速幂)的更多相关文章

  1. [hdu-6395]Sequence 分块+矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时  加的下取整的数随 ...

  2. HDU 6395 Sequence(分段矩阵快速幂)题解

    题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...

  3. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  4. BZOJ 2326 数学作业(分段矩阵快速幂)

    实际上,对于位数相同的连续段,可以用矩阵快速幂求出最后的ans,那么题目中一共只有18个连续段. 分段矩阵快速幂即可. #include<cstdio> #include<iostr ...

  5. HDU6395(分段+矩阵快速幂)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6395 给你一个式子,给出你A,B,C,D,P,n,让你求出第n项的式子Fn.(其中ABCDPn均在1e9的 ...

  6. 数学--数论--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 ...

  7. A - Number Sequence(矩阵快速幂或者找周期)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...

  8. HDU 5950 Recursive sequence(矩阵快速幂)

    题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...

  9. HDU 5667 Sequence(矩阵快速幂)

    Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...

  10. HDU 6395 Sequence 【矩阵快速幂 && 暴力】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)   ...

随机推荐

  1. xe5 for android 地理定位GPS

    先上源码,在解释. implementation uses androidapi.jni.JavaTypes, androidapi.jni.Location, FMX.helpers.android ...

  2. 预处理器#include 指令

    预处理器发现 #include 指令后,就会寻找后跟的文件名并把这个文件的内容包含到当前文件中.被包含文件中的文本将替换源代码文件中的#include指令,就像你把被包含文件中的全部内容键入到源文件中 ...

  3. Spring Type Conversion(Spring类型转换源码探究)

    1:概述 类型转换系统负责Spring框架中对象类型转换和格式化工作. ConversionService默认实现UML图如下所示: GenericConversionService(通用类型转换服务 ...

  4. shell把文件批量导入mysql

    for file in ./tmp_data/* do echo $file mysql -u'root' -p'wangbin' --default-character-set=utf8 -e&qu ...

  5. netty中的发动机--EventLoop及其实现类NioEventLoop的源码分析

    EventLoop 在之前介绍Bootstrap的初始化以及启动过程时,我们多次接触了NioEventLoopGroup这个类,关于这个类的理解,还需要了解netty的线程模型.NioEventLoo ...

  6. 在phpstorm中安装、配置和运行phpunit详细教程

    前提:安装了composer 一.安装phpunit组件 右键项目文件,composer---init composer,会生成一个composer.json文件 右键项目文件,composer--- ...

  7. 12 DOM操作的相关案例

    1.模态框案例 需求: 打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 代码如下: <!DOCTYPE html> <h ...

  8. spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ...

  9. Redis 在java中的使用(登录验证,5分钟内连续输错3次密码,锁住帐号,半小时后解封)(三)

    在java中使用redis,做简单的登录帐号的验证,使用string类型,使用redis的过期时间功能 1.首先进行redis的jar包的引用,因为用的是springBoot,springBoot集成 ...

  10. Facebook也炒币吗?Libra币是什么?

    Facebook 在上周发布了加密数字货币,称为 Libra币. 太火爆了,很多人都在关注和讨论,包括一些科技大佬们都很积极的讨论(当然,这里指的是真正的科技大佬,比如 马化腾.王兴等,而不是指哪些割 ...