HDU4686 Arc of Dream 矩阵

欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - HDU4686


题意概括

  

  a0 = A0
  ai = ai-1*AX+AY
  b0 =
B0
  bi = bi-1*BX+BY

  求AoD(n)

  n=0时答案为0!!!!


题解

  具体的矩阵构建思路指导可以参考例题链接

  这里仅提供运算过程。

  Ai=Ai-1*AX+AY

  Bi=Bi-1*BX+BY

  AiBi=(Ai-1*AX+AY)(Bi-1*BX+BY)

        =AX*BX*Ai-1*Bi-1+AX*BY*Ai-1+BX*AY*Bi-1+AY*BY

        Si           AiBi       Ai     Bi        K

Si-1            1     0       0    0     0

Ai-1Bi-1     AX*BX   AX*BX     0    0     0

Ai-1         AX*BY   AX*BY       AX     0     0

Bi-1          BX*AY   BX*AY      0     BX    0

K          AY*BY   AY*BY       AY    BY    1

  初始矩阵:

  S0        A0B0       A0     B0   K

  A0*B0       A0*B0           A0     B0     1



代码

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long LL;
const LL mod=1000000007,m=5;
LL n,A0,AX,AY,B0,BX,BY;
struct Mat{
	LL v[m][m];
	Mat (){}
	Mat (LL x){
		(*this).set(x);
	}
	void set(LL x){
		memset(v,0,sizeof v);
		if (x==1)
			for (int i=0;i<m;i++)
				v[i][i]=1;
	}
	Mat operator * (Mat x){
		Mat ans(0);
		for (int i=0;i<m;i++)
			for (int j=0;j<m;j++)
				for (int k=0;k<m;k++)
					ans.v[i][j]=(ans.v[i][j]+v[i][k]*x.v[k][j])%mod;
		return ans;
	}
	void operator *= (Mat x){
		(*this)=(*this)*x;
	}
}M,Md;
Mat MatPow(Mat x,LL y){
	Mat ans(1),now=x;
	while (y){
		if (y&1LL)
			ans*=now;
		now*=now;
		y>>=1;
	}
	return ans;
}
int main(){
	while (~scanf("%lld%lld%lld%lld%lld%lld%lld",&n,&A0,&AX,&AY,&B0,&BX,&BY)){
		if (!n){
			puts("0");
			continue;
		}
		A0%=mod,AX%=mod,AY%=mod,B0%=mod,BX%=mod,BY%=mod;
		LL NewArr[m][m]={{1         ,0         ,0  ,0  ,0},
						 {AX*BX%mod ,AX*BX%mod ,0  ,0  ,0},
						 {AX*BY%mod ,AX*BY%mod ,AX ,0  ,0},
						 {BX*AY%mod ,BX*AY%mod ,0  ,BX ,0},
						 {AY*BY%mod ,AY*BY%mod ,AY ,BY ,1}};
		LL NewArr2[m]=	 {A0*B0%mod ,A0*B0%mod ,A0 ,B0 ,1};
		memcpy(Md.v,NewArr,sizeof NewArr);
		memcpy(M.v[0],NewArr2,sizeof NewArr2);
		Md=MatPow(Md,n-1);
		M*=Md;
		printf("%lld\n",M.v[0][0]);
	}
	return 0;
}

  

时间: 2024-11-13 03:57:39

HDU4686 Arc of Dream 矩阵的相关文章

hdu4686 Arc of Dream 矩阵快速幂

An Arc of Dream is a curve defined by following function: wherea0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BYWhat is the value of AoD(N) modulo 1,000,000,007? 矩阵快速幂 1 #include<stdio.h> 2 #include<string.h> 3 typedef long long ll; 4 const int mod

【HDOJ 4686】 Arc of Dream (矩阵快速幂)

[HDOJ 4686] Arc of Dream (矩阵快速幂) 两个公式 a(i) = a(i-1)*Ax+Ay b(i) = b(i-1)*Bx+By 求 0~(n-1) 的a(i)*b(i) 初始矩阵为                                       求幂矩阵为 a0                                                      Ax          0           0          0        

S - Arc of Dream 矩阵快速幂

An Arc of Dream is a curve defined by following function: where a 0 = A0 a i = a i-1*AX+AY b 0 = B0 b i = b i-1*BX+BY What is the value of AoD(N) modulo 1,000,000,007? InputThere are multiple test cases. Process to the End of File. Each test case con

HDOJ 4686 Arc of Dream 矩阵高速幂

矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2164    Accepted Submission(s): 680 Problem Description An Arc of Dream is a curve defined by following fun

HDOJ 4686 Arc of Dream 矩阵快速幂

矩阵快速幂: 根据关系够建矩阵 , 快速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2164    Accepted Submission(s): 680 Problem Description An Arc of Dream is a curve defined by following fun

HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目已经给出, Ai*Bi=Ax*Bx*(Ai-1*Bi-1)+Ax*By*Ai-1+Bx*Ay*Bi-1+Ay*By AoD(n)=AoD(n-1)+AiBi 构造向量I{AoD(i-1),Ai*Bi,Ai,Bi,1} 初始向量为I0={0,A0*B0,A0,B0,1} 构造矩阵A{ 1,0,0,0,

HDU4686—Arc of Dream

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题目意思:给出一个n,算这个式子,给出A0,B0,AX,AY,然后存在以下的递推关系. a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY 构造矩阵的思路先算ai,bi,然后算Aod(i),然后再求和,说明这个一定是一个4*4的矩阵,我们可以构造以下系数矩阵(构造矩阵技巧可能会准备专门总结一下): [    1 ,          0 ,    0 ,

HDU 4686 Arc of Dream(矩阵快速幂)

题目地址:HDU 4686 我去..因为忘记把函数里的k定义成64位的,导致TLE了一晚上...晕.. 这题没什么技巧,就是根据公式构造就行. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #in

HDOJ Arc of Dream 4686【矩阵快速幂】

Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 3126    Accepted Submission(s): 982 Problem Description An Arc of Dream is a curve defined by following function: where a0 = A0 ai =