2018 Multi-University Training Contest 7 (HDU6395) Sequence 矩阵快速幂 + 分块

                                               Sequence
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1855    Accepted Submission(s): 714


Problem Description
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 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≤20  0≤A,B,C,D≤1e9  1≤P,n≤1e9
 

Sample Input

2
3 3 2 1 3 5
3 2 2 2 1 4

 

Sample Output

36
24

 

Source
2018 Multi-University Training Contest 7
 

题解:
1.分块 + 矩阵快速幂 分成P^1/2块
2.迭代的时候注意跃度


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxn = 1e5  + 100;
const int maxm = 4e5 + 100;
const int INF = 0x3f3f3f3f;
//const int mod = 1e9 + 7;
typedef pair<int,int> P;
typedef long long LL;
const LL mod = 1e9 + 7;

#define PI 3.1415926
#define sc(x)  scanf("%d",&x)
#define pf(x)  printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfln(x) printf("%I64d\n",x)
#define pfs(x) printf("%d ",x)
#define rep(i,a,n) for(int i = a; i < n; i++)
#define per(i,a,n) for(int i = n-1; i >= a; i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb(x)  push_back(x);

// const int BUF=40000000;
// char Buf[BUF], *buf=Buf;
// inline void read(int& a) {for(a=0;*buf<48;buf++); while(*buf>47) a=a*10+*buf++-48;}
//  fread(Buf,1,BUF,stdin);

void read(LL &x){
	char ch = getchar();x = 0;
	for (; ch < '0' || ch > '9'; ch = getchar());
	for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}


struct Node
{
	LL a[3][3];
};

Node Mul(Node x, Node y)
{
	Node z;
	rep(i,0,3) rep(j,0,3) z.a[i][j] = 0;
	rep(i,0,3)  rep(j,0,3) rep(k,0,3)
  z.a[i][j] = (z.a[i][j] + (x.a[i][k]*y.a[k][j])%mod)%mod;
	return z;
}

Node QMpow(Node x, LL y)
{
  Node res = x;
	Node ans;
	rep(i,0,3) rep(j,0,3) if(i == j) ans.a[i][j] = 1; else ans.a[i][j] = 0;
	while(y)
	{
		//cout << "*" << res.a[0][0] << " " <<res.a[0][1] << " " << res.a[0][2] << endl;
		if(y&1) ans = Mul(res,ans);
		res = Mul(res,res);
		y >>= 1;

	}
	return ans;
}

int main()
{

  LL T;
	read(T);
	while(T--)
	{
    	 LL a,b,c,d,p,n;
			 LL ans = 0;
			 read(a);read(b);read(c);read(d);read(p);read(n);
			 if(n == 1)  {pfln(a);continue;}
			 Node t;
			 t.a[0][0] = d;t.a[0][1] = c;
			 t.a[0][2] = t.a[1][0] = t.a[2][2] = 1;
			 t.a[1][1] = t.a[1][2] = t.a[2][0] = t.a[2][1] = 0;

       LL F[3],f[3];
			 f[0] = b;f[1] = a;
			 LL end = 2;
			 for(LL i = 3; i <= p && p/(p/i) <= n; i = end+1)
			 {
				   end = p/(p/i);
				   f[2] = p/i;
					 Node temp = QMpow(t,end-i+1);
					 F[0] = (((temp.a[0][0]*f[0])%mod + (temp.a[0][1]*f[1])%mod)%mod + (temp.a[0][2]*f[2])%mod)%mod;
					 F[1] = (((temp.a[1][0]*f[0])%mod + (temp.a[1][1]*f[1])%mod)%mod + (temp.a[1][2]*f[2])%mod)%mod;
					 f[0] = F[0]; f[1] = F[1];
			 }

			f[2] = p/(end+1);
			Node temp = QMpow(t,n-end);
			F[0] = (((temp.a[0][0]*f[0])%mod + (temp.a[0][1]*f[1])%mod)%mod + (temp.a[0][2]*f[2])%mod)%mod;
			ans = F[0];
			pfln(ans);
 	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值