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) ...
随机推荐
- Oracle 宣布 Java 7 生命周期终结
快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中. <HTML开发Mac OS App 视频教程> 土豆网同步更新:http: ...
- baiduMap试手《办理进京证和市区警察查询进京证的地址浏览》
没用过baidu的map api其实挺简单,申请一个key,然后根据坐标在地图上生成对象,看了官方的dome多少知道有些什么功能了,没什么可说的直接贴效果. <!DOCTYPE html> ...
- JavaScript-DOM续
一.路由的跳转 <body> <a href="#/course">课程</a> <a href="#/main"&g ...
- web页面的时间传入servlet如何转换为可以存入MySQL的Date类型
在web页面中当使用如下语句: <input type="date" name="startTime"/> 提交到servlet中 在servlet ...
- pybind11在Windows下的使用
Pybind11算是目前最方便的Python调用C++的工具了, 介绍一下在vs2019上写Python的扩展的HelloWorld 1. 去下载pybind11 https://github ...
- node.js + mssql 简易封装操作
时间吧,总是这么凑巧,在我学习[node.js]还没几天,我的 Microsoft SQL Server Management Studio 18 就歇菜了,至于怎么歇菜的吧....它可能的意思就是想 ...
- Linux 中 IDEA 不能调试(Debug)项目
问题描述: can't debug project on idea linux. 在Linux 中, IDEA能运行项目,但是点击调试项目,弹出警告.警告内容如下: Required connecto ...
- SCIgen与野鸡期刊的梗
layout: post title: "SCIgen与野鸡期刊的梗" date: 2019-04-28 19:06:21 +0800 --- 作者:吴甜甜 个人博客网站: wut ...
- 微信小程序在ios下Echarts图表不能滑动的解决方案
问题现象 这个问题的现象说起来很简单. 小程序页面中有一篇很长的文章,内部有一个Echarts图表,手指上下滑动观看内容. 但是手指滑动区域在Echarts图表上时,页面却不能滑动了. 如下图: 追踪 ...
- 微服务-springcloud-注册中心
创建服务注册中心(eureka-server) 1.创建项目,选择 Eureka Server 别的都不要选择,next-finish 2.application.yml中写入如下信息:通过eurek ...