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

Output

36

24

Sample Input

2

3 3 2 1 3 5

3 2 2 2 1 4

Sample Output

36

24

首先处理递推式这里,因为直接递推会超时,我们考虑矩阵快速幂,然后看题,有三个未知量,我们构造3*3的矩阵,然后因为还有一个数论分块,不能直接使用矩阵快速幂,应该相等的位置使用矩阵快速幂,然后完事了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
int a, b, c, d, p, n, t; struct mat{
int m[3][3];
mat(){
memset(m, 0, sizeof(mat));
}
friend mat operator*(mat a, mat b){
mat c;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
ll t = 0;
for(int k=0; k<3; k++){
t += (ll)a.m[i][k]*b.m[k][j];
}
c.m[i][j] = t%mod;
}
}
return c;
}
}I; mat pow_mat(mat a, int b){
mat c = I;
while(b){
if(b&1){
c = c*a;
}
a = a*a;
b >>= 1;
}
return c;
} int main(){
I.m[0][0] = I.m[1][1] = I.m[2][2] = 1;
scanf("%d", &t);
while(t--){
scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &p, &n);
if(n == 1){
printf("%d\n", a);
continue;
}
mat f;
f.m[0][0] = d;
f.m[0][1] = c;
f.m[1][0] = 1;
f.m[2][2] = 1;
int flag = 0;
for(int i=3; i<=n;){
if(p/i == 0){
mat w = f;
w = pow_mat(w, n-i+1);
ll ans = w.m[0][0]*(ll)b%mod + w.m[0][1]*(ll)a + w.m[0][2]%mod;
ans %= mod;
printf("%lld\n", ans);
flag = 1;
break;
}
int j = min(n, p/(p/i));
mat w = f;
w.m[0][2] = p/i;
w = pow_mat(w, j-i+1);
ll tmp1 = (w.m[1][0]*(ll)b + w.m[1][1]*(ll)a + w.m[1][2]) % mod;
ll tmp2 = (w.m[0][0]*(ll)b + w.m[0][1]*(ll)a + w.m[0][2]) % mod;
a = tmp1; b = tmp2;
i = j+1;
}
if(!flag)
printf("%d\n", b);
} }

数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂的更多相关文章

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

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

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

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

  3. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

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

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

  6. HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意: 给出m个模式串,求长度不超过n的且至少包含一个模式串的字符串个数. 思路: 如果做过poj2778 ...

  7. HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)

    背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...

  8. HDU 2243 考研路茫茫――单词情结 ——(AC自动机+矩阵快速幂)

    和前几天做的AC自动机类似. 思路简单但是代码200余行.. 假设solve_sub(i)表示长度为i的不含危险单词的总数. 最终答案为用总数(26^1+26^2+...+26^n)减去(solve_ ...

  9. HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和

    Clarke and digits  Accepts: 16  Submissions: 29  Time Limit: 5000/3000 MS (Java/Others)  Memory Limi ...

随机推荐

  1. html字体大小与颜色设置

    代码架构:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> //浏览器识 ...

  2. PHP中debug基本方法

    一.检查是否有语法错误 php -l test.php 二.基本调试基本调试 API: var_dump($var);print_r($var);echo $var; 基本的配置: display_e ...

  3. 10.6 IoStudentManager

    package day11_io_student.student_demo; public class Student { private String id; private String name ...

  4. nginx内置高可用配置与第三方高可用模块nginx_ustream_check_mudule配置

    1. nginx 第三方高可用模块 IP 备注 10.0.0.63 proxy 10.0.0.64 web1 10.0.0.65 web2 这里会讲解一些nignx常用高可用方案,以及引入第三方高可用 ...

  5. jetCache 基本使用

    1.pom引用 <!--jetcache缓存 lettuce--> <dependency> <groupId>com.alicp.jetcache</gro ...

  6. 将函数作为返回值的方法 - Python

    有的时候,我们需要将函数作为返回值,以下为代码: def superfunc(): i = 0 def wrapper(): nonlocal i i +=1 return i return wrap ...

  7. JMeter分布式压测实战(2020年清明假期学习笔记)

    一.常用压力测试工具对比 简介:目前用的常用测试工具对比 1.loadrunner 性能稳定,压测结果及颗粒度大,可以自定义脚本进行压测,但是太过于重大,功能比较繁多. 2.Apache ab(单接口 ...

  8. 5. iphone 的:active样式

    如果给按钮定义 :hover 样式,在 iPhone 上按钮点击一次是 hover 态,再点击一次 hover 态才会消失,这不是我们想要的,继而想通过定义 :active 样式来实现按钮按下时的效果 ...

  9. 刷题记录:[V&N2020 公开赛]TimeTravel

    题目复现链接:https://buuoj.cn/challenges 参考链接:2020 年 V&N 内部考核赛 WriteUp V&N公开赛2020 writeup httpoxy ...

  10. 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对

    面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...