Sequence(数论分块 + 矩阵加速)

题目

在这里插入图片描述
在这里插入图片描述

思路

很简单的一道数论分块加矩阵加速。

Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 5
#define LL long long
#define Mod 1000000007
#define Int register long long
using namespace std;
inline void read(LL &x)
{
	x = 0;
	LL f = 1;
	char s = getchar();
	while (s < '0' || s > '9')
	{
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9')
	{
		x = (x << 3) + (x << 1) + (s ^ 48);
		s = getchar();
	}
	x *= f;
}
inline LL Max(LL x,LL y)
{
	return x > y ? x : y;
}
inline LL Min(LL x,LL y)
{
	return x < y ? x : y;
}
struct Matrix
{
	LL c[MAXN][MAXN], n, m;
	Matrix(){memset(c, 0, sizeof c);}
	inline void Clear(){memset(c, 0, sizeof c);}
	friend Matrix operator *(Matrix Tx,Matrix Ty)
	{
		Matrix Res;
		Res.n = Tx.n, Res.m = Ty.m;
		for (Int i = 1; i <= Tx.n; ++ i)
			for (Int k = 1; k <= Tx.m; ++ k)
				for (Int j = 1; j <= Ty.m; ++ j)
					if (Tx.c[i][k])
						Res.c[i][j] += Tx.c[i][k] * Ty.c[k][j] % Mod,
						Res.c[i][j] %= Mod;
		return Res;
	}
}Fuck, Shit;
inline Matrix QuickPow_Matrix(Matrix x,LL y)
{
	Matrix Res;
	Res.n = Res.m = x.m;
	for (Int i = 1; i <= Res.n; ++ i)
		Res.c[i][i] = 1;
	while ( y )
	{
		if (y & 1)
			Res = Res * x;
		x = x * x;
		y /= 2;
	}
	return Res;
}
LL A, B, C, D, P, n;
int main()
{
	LL T;
	read( T );
	while (T --)
	{
		Fuck.Clear();
		read( A ); read( B ); read( C );
		read( D ); read( P ); read( n );
		Fuck.n = 1, Fuck.m = 3;
		Fuck.c[1][1] = B, Fuck.c[1][2] = A, Fuck.c[1][3] = 1;
		for (Int l = 3, r; l <= n; l = r + 1)
		{
			Shit.Clear();
			LL t = P / l;
			if (! t)
				r = n;
			else r = Min(n, P / t);
			Shit.n = Shit.m = 3;
			Shit.c[1][1] = D, Shit.c[2][1] = C, Shit.c[3][1] = t;
			Shit.c[1][2] = Shit.c[3][3] = 1;
			Shit = QuickPow_Matrix(Shit, r - l + 1);
			Fuck = Fuck * Shit;
		}
		printf("%lld\n", Fuck.c[1][1]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值