首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂

数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂

作者头像
风骨散人Chiam
发布2020-11-06 00:32:08
发布2020-11-06 00:32:08
42600
代码可运行
举报
文章被收录于专栏:CSDN旧文CSDN旧文
运行总次数:0
代码可运行

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的矩阵,然后因为还有一个数论分块,不能直接使用矩阵快速幂,应该相等的位置使用矩阵快速幂,然后完事了

代码语言:javascript
代码运行次数:0
运行
复制
#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);
	}
	
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档