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. Delphi xe5 StyleBook的用法(待续)

    首先要在FORM里拖进来一个StyleBook1,然后在Form里设置属性,记住一定要在单击form,在OBject Inspector里设置StyleBook  [StyleBook1]. 下一个属 ...

  2. 基于QT的在线打字练习软件助手(C/S模型)good

    简介   通过基于QT中QTcpServer和QTcpSocket以及UI编程,实现了基于TCP协议的C/S模型在线打字练习软件助手,服务端处理各客户端打字数据,以及显示在线打字客户列表即实时更新打字 ...

  3. Silverlight ItemsControl详细解析+解惑

    Silverlight最强大的地方就在于定义控件了,Silverlight提供了非常灵活和高效的控件定义方式,几乎可以实现任何复杂的控件实现,对于快速开发应用程序有着重要的意义.在Silverligh ...

  4. Java代码消除switch/case,if/else语句的几种实现方式

    转自:https://my.oschina.net/stefanzhlg/blog/372413 我们在平时的编码中,我们经常会遇到这样的情况: 使用过多的switch/case 或者 if else ...

  5. CDMA子钟

    SYN6103型 CDMA子钟 产品概述 SYN6103型CDMA子钟是由西安同步电子科技有限公司精心设计.自行研发生产的一套从CDMA网络获取标准时间信号信息的子钟,能方便部署在任何有CDMA信号的 ...

  6. 基于Bert的文本情感分类

    详细代码已上传到github: click me Abstract:    Sentiment classification is the process of analyzing and reaso ...

  7. ZooKeeper学习第八期——ZooKeeper伸缩性(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4143306.html 一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我 ...

  8. Spring Boot:实现MyBatis分页

    综合概述 想必大家都有过这样的体验,在使用Mybatis时,最头痛的就是写分页了,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真的不想花双倍 ...

  9. Django多对多表的三种创建方式,MTV与MVC概念

    MTV与MVC MTV模型(django): M:模型层(models.py) T:templates V:views MVC模型: M:模型层(models.py) V:视图层(views.py) ...

  10. 【对象属性复制】BeanUtils.copyProperties(obj1, obj2);

    实现对象的属性值复制,只会复制命名相同的文件. import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(obj1, o ...