HDU 6395(区间矩阵快速幂-多校第七场1010)

下午请教了学长还有和队友讨论已经有了完整的思路就是时间不够敲不出来了

最终要的就是解决p/n会变的问题,将其进行分块处理,然后同区间的p/n为一个常数,直接进行矩阵快速幂,最后得出答案即可。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define maxn 205000
#define maxnn 6000
#define juzheng 300
#define line cout << "-------------------------" << endl;
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define fill_(a,b,n) fill(a,a + n,b)
#define esp 1e-9

#define ri(n) scanf("%d",&n)
#define ri2(a,b) scanf("%d %d",&a,&b)
#define ri3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define rd(n) scanf("%lf",&n)
#define rd2(a,b) scanf("%lf %lf",&a,&b)
#define rd3(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define rl(n) scanf("%lld",&n)
#define rl2(a,b) scanf("%lld %lld",&a,&b)
#define rl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define rui(n) scanf("%u",&n)
#define rui2(a,b) scanf("%u %u",&a,&b)
#define rui3(a,b,c) scanf("%u %u %u",&a,&b,&c)
#define rs(str) scanf("%s",str)
#define pr(n) cout << n << endl
#define ll long long
#define int64 __int64
#define ui unsigned int

using namespace std;

const ll mod = 1e9 + 7;

//Date:2018-8-13
//Author:HarryBlackCat

struct matrix {
	ll m[5][5];
} a,b;

ll A,B,C,D,P,n;
ll f[maxn];

matrix operator * (matrix a,matrix b) {
	matrix ans;
	int nn = 3;//两个矩阵最大的行或列方便套版 
	for(int i = 0; i < nn; i++) {
		for(int j = 0; j < nn; j++) {
			ans.m[i][j] = 0;
			for(int k = 0; k < nn; k++)
				ans.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;
			ans.m[i][j] %= mod;
		}
	}

	return ans;
}

matrix power_mod(ll n) {
	while(n) {
		if(n & 1) {
			b = a * b;
		}

		n >>= 1;
		a = a * a;
	}
	
	return b;
}

void matrix_init(ll f2,ll f1,ll pn) {//b为答案矩阵,a为构造的矩阵,初始化矩阵 
	a.m[0][0] = D;
	a.m[0][1] = C;
	a.m[0][2] = 1;
	
	a.m[1][0] = 1;
	a.m[1][1] = 0;
	a.m[1][2] = 0;
	
	a.m[2][0] = 0;
	a.m[2][1] = 0;
	a.m[2][2] = 1;
	
	b.m[0][0] = f2;
	b.m[1][0] = f1;
	b.m[2][0] = pn;
}

struct xx {
	ll r,l,pn;
} interval[maxn];

void init() {
	f[1] = A;
	f[2] = B;
	for(int i = 3; i <= 100000; i++) {
		f[i] = (C * f[i - 2] % mod + D * f[i - 1] % mod + P / i) % mod;
	}
}

int main() {
	//cin.sync_with_stdio(false);//降低cin,cout时间
	int t;
	while(~ri(t)) {
		while(t--) {
			rl3(A,B,C);
			rl3(D,P,n);

			init();
			if(n <= 100000)//前 100000直接暴力算出 
				printf("%lld\n",f[n]);
			else {
				int counter = 0;

				ll r = 1,l = 2;
				ll pn = 1;
				while(P / l > 100000) {//对P分块 
					interval[counter].r = P / r;
					interval[counter].l = P / l + 1;
					interval[counter++].pn = pn;
					r++,l++,pn++;
				}
				interval[counter].r = P / r;
				interval[counter].l = P / l + 1;
				interval[counter++].pn = pn;
				
				ll index = interval[counter - 1].l;
				
				ll f1 = f[index - 1];
				ll f2 = f[index - 2];
				f2 = (C * f2 % mod + D * f1 % mod + P / index) % mod;
				
				for(int i = counter - 1;i >= 0;i--){//区间矩阵快速幂 
					matrix_init(f2,f1,interval[i].pn);
					
					if(interval[i].l <= n && interval[i].r >= n){//n在区间内直接break答案 
						matrix tp = power_mod(n - interval[i].l);
						f2 = tp.m[0][0];
						break;
					}
					
					matrix tp = power_mod(interval[i].r - interval[i].l);
					ll f12 = tp.m[0][0];
					ll f11 = tp.m[1][0];

					if(i != 0){
						ll temp = f12;
						f12 = (C * f11 % mod + D * f12 % mod + interval[i - 1].pn) % mod;
						f11 = temp;
					}
						
					f2 = f12;
					f1 = f11; 
				}
				
				if(P < n){//若P < n后面的 P / n 都是0直接进行矩阵快速幂 
					ll temp = f2;
					f2 = (C * f1 % mod + D * f2 % mod) % mod;
					f1 = temp; 
					matrix_init(f2,f1,0);
					matrix tp = power_mod(n - (P + 1));
					f2 = tp.m[0][0];
				}
				
				printf("%lld\n",f2 % mod);
			}
		}
	}
	return 0;
}

//9215647 1236548 456781 15648 45612387 100001

 

你可能感兴趣的:(HDU 6395(区间矩阵快速幂-多校第七场1010))