HDU4686—Arc of Dream

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686

题目意思:给出一个n,算这个式子,给出A0,B0,AX,AY,然后存在以下的递推关系。

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

构造矩阵的思路先算ai,bi,然后算Aod(i),然后再求和,说明这个一定是一个4*4的矩阵,我们可以构造以下系数矩阵(构造矩阵技巧可能会准备专门总结一下):

[    1 ,          0 ,    0 , 0 ,  0]
[Ax*Bx ,Ax*Bx , 0 , 0 ,  0]
[Ay*Bx ,Ay*Bx ,Bx ,0 ,  0]
[Ax*By ,Ax*By ,0 ,Ax ,  0]
[Ay*By ,Ay*By ,By ,Ay ,1]

有这个系数矩阵以后就很简单,就是普通矩阵快速幂,其实这个矩阵挺容易构造的。

代码:

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 #define maxx INT_MAX
 4 #define minn INT_MIN
 5 #define inf 0x3f3f3f3f
 6 #define n 5
 7 #define mod 1000000007
 8 using namespace std;
 9 typedef long long ll;
10 ll a0,b0,ax,bx,ay,by;
11 struct Matrix{
12     ll mat[15][15];
13     Matrix operator * (const Matrix & m) const{
14         Matrix tmp;
15         for(int i=0;i<n;i++)
16             for(int j=0;j<n;j++){
17                 tmp.mat[i][j]=0;
18                 for(int k=0;k<n;k++){
19                     tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%mod;
20                     tmp.mat[i][j]%=mod;
21                 }
22             }
23         return tmp;
24     }
25 };
26 Matrix q_power(Matrix a,ll k){
27     Matrix ans;
28     memset(ans.mat,0,sizeof(ans.mat));
29     for(int i=0;i<n;i++) ans.mat[i][i]=1;
30     while(k){
31         if(k&1) ans=ans*a;
32         k/=2;
33         a=a*a;
34     }
35     return ans;
36
37 }
38 int main() {
39     ios::sync_with_stdio(false);cin.tie(0);
40     ll T;
41     while(cin>>T>>a0>>ax>>ay>>b0>>bx>>by){
42         if(T==0){cout<<0<<endl;continue;}
43         Matrix m;
44         memset(m.mat,0,sizeof(m.mat));
45         m.mat[0][0]=m.mat[0][4]=ax*bx%mod;
46         m.mat[1][0]=ax*by%mod;m.mat[1][1]=ax%mod;m.mat[1][4]=ax*by%mod;
47         m.mat[2][0]=bx*ay%mod;m.mat[2][2]=bx%mod; m.mat[2][4]=bx*ay%mod;
48         m.mat[3][0]=by*ay%mod;m.mat[3][1]=ay%mod;m.mat[3][2]=by%mod;m.mat[3][3]=1;m.mat[3][4]=by*ay%mod;
49         m.mat[4][4]=1;
50         Matrix p=q_power(m,T-1);
51         Matrix f;
52         memset(f.mat,0,sizeof(f.mat));
53         f.mat[0][0]=f.mat[0][4]=a0*b0%mod;f.mat[0][1]=a0%mod;f.mat[0][2]=b0%mod;f.mat[0][3]=1;
54         f=f*p;
55         cout<<f.mat[0][4]<<endl;
56     }
57     return 0;
58 }

时间: 2024-10-30 05:05:18

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

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

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 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 =

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(快速幂矩阵)

题目链接 再水一发,构造啊,初始化啊...wa很多次啊.. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 1000000007 #define

hdu 4686 Arc of Dream(矩阵快速幂乘法)

Problem Description An Arc of Dream is a curve defined by following function: where a0 = A0 ai = ai-1*AX+AY b0 = B0 bi = bi-1*BX+BY What is the value of AoD(N) modulo 1,000,000,007? Input There are multiple test cases. Process to the End of File. Eac

【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