hdu6395 Sequence(分段矩阵快速幂)
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(分段矩阵快速幂)的更多相关文章
- [hdu-6395]Sequence 分块+矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时 加的下取整的数随 ...
- HDU 6395 Sequence(分段矩阵快速幂)题解
题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- BZOJ 2326 数学作业(分段矩阵快速幂)
实际上,对于位数相同的连续段,可以用矩阵快速幂求出最后的ans,那么题目中一共只有18个连续段. 分段矩阵快速幂即可. #include<cstdio> #include<iostr ...
- HDU6395(分段+矩阵快速幂)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6395 给你一个式子,给出你A,B,C,D,P,n,让你求出第n项的式子Fn.(其中ABCDPn均在1e9的 ...
- 数学--数论--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 ...
- A - Number Sequence(矩阵快速幂或者找周期)
Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...
- HDU 5950 Recursive sequence(矩阵快速幂)
题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...
- HDU 5667 Sequence(矩阵快速幂)
Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...
- HDU 6395 Sequence 【矩阵快速幂 && 暴力】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) ...
随机推荐
- 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))
简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...
- qlineedit设置背景颜色(使用QPalette的方法不行,必须使用QSS)
使用QPalette的方法不行, ui->le_text->setAutoFillBackground(true);qDebug() << ui->le_text-> ...
- python网络编程(转)
本文代码转自廖雪峰老师的python教程 网络编程底层其实就是一个socket,代表两台机器之间的一个连接. s = socket.socket(socket.AF_INET, socket.SOCK ...
- 多进程界面开发-Qt试玩儿
目录 一.概述 二.效果展示 三.使用方法 1.启动外部进程 2.创建Qt窗口 3.加入到主进程布局 四.嵌入NotePad 五.调用Ping命令 六.嵌入其他QWidget窗体 七.相关文章 一.概 ...
- Servlet 3.0异步特性初探
Servlet 是 Java 为了编写服务端程序而定义的一个接口规范,在 Servlet 3.0 以后支持了异步的操作. 最近项目添加了一个代码热部署的功能,在客户端输入信号,信号到达 Web 服务器 ...
- js 数组去重方法
var arr = ['a',1,2,3,'a',4,2,3,1,4,2,8,10,null,'a']; // 方法一 var newArr = [...new Set(arr)]; console. ...
- java基础知识总结(一)
满满的干货=-= (一)环境变量的作用: 每个人刚开始学习java的时候,肯定都是安装JDK,配置环境变量,怎么配置网上教程很多很多,但是为什么这么配置呢? 我配置的环境变量: JAVA_HOME:C ...
- webpack打包(一)
1.安装webpack打包工具 webpack是使用npm安装 npm install webpack -g //全局安装 在命令行中就可以使用webpack这个命令了. 提示:由于npm安装会去找国 ...
- Codeforces Round #564 (Div. 2)B
B. Nauuo and Chess 题目链接:http://codeforces.com/contest/1173/problem/B 题目 Nauuo is a girl who loves pl ...
- 基于STM32之UART串口通信协议(二)发送
一.前言 1.简介 在上一篇UART详解中,已经有了关于UART的详细介绍了,也有关于如何使用STM32CubeMX来配置UART的操作了,而在该篇博客,主要会讲解一下如何实现UART串口的发送功能. ...