hdu4686 Arc of Dream 矩阵快速幂

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?

矩阵快速幂

 1 #include<stdio.h>
 2 #include<string.h>
 3 typedef long long ll;
 4 const int mod=1000000007;
 5
 6 struct mat{
 7     int r,c;
 8     ll m[6][6];
 9     void clear(){
10         for(int i=1;i<=r;i++)memset(m[i],0,sizeof(m[i]));
11     }
12 };
13
14 mat MatMul(mat &m1,mat &m2){
15     mat tmp;
16     tmp.r=m1.r;
17     tmp.c=m2.c;
18     int i,j,k;
19     for(i=1;i<=tmp.r;i++){
20         for(j=1;j<=tmp.c;j++){
21             ll t=0;
22             for(k=1;k<=m1.c;k++){
23                 t=(t+(m1.m[i][k]*m2.m[k][j])%mod)%mod;
24             }
25             tmp.m[i][j]=t;
26         }
27     }
28     return tmp;
29 }
30
31 mat MatQP(mat &a,ll n){
32     mat ans,tmp=a;
33     ans.r=ans.c=a.r;
34     memset(ans.m,0,sizeof(ans.m));
35     for(int i=1;i<=ans.r;i++){
36         ans.m[i][i]=1;
37     }
38     while(n){
39         if(n&1)ans=MatMul(ans,tmp);
40         n>>=1;
41         tmp=MatMul(tmp,tmp);
42     }
43     return ans;
44 }
45
46 int main(){
47     ll n;
48     mat a;
49     a.r=5,a.c=1;
50     mat tmp;
51     tmp.c=tmp.r=5;
52     ll a0,b0,ax,ay,bx,by;
53     while(scanf("%lld",&n)!=EOF){
54         scanf("%lld%lld%lld%lld%lld%lld",&a0,&ax,&ay,&b0,&bx,&by);
55         a0%=mod;
56         ax%=mod;
57         ay%=mod;
58         b0%=mod;
59         bx%=mod;
60         by%=mod;
61         a.clear();
62         a.m[1][1]=0;
63         a.m[2][1]=(a0*b0)%mod;
64         a.m[3][1]=b0;
65         a.m[4][1]=a0;
66         a.m[5][1]=1;
67         tmp.clear();
68         tmp.m[1][1]=tmp.m[1][2]=tmp.m[5][5]=1;
69         tmp.m[2][2]=(ax*bx)%mod;
70         tmp.m[2][3]=(ay*bx)%mod;
71         tmp.m[2][4]=(ax*by)%mod;
72         tmp.m[2][5]=(ay*by)%mod;
73         tmp.m[3][3]=bx;
74         tmp.m[3][5]=by;
75         tmp.m[4][4]=ax;
76         tmp.m[4][5]=ay;
77 //        tmp.m[1][3]=tmp.m[1][4]=tmp.m[1][5]=tmp.m[2][1]=tmp.m[3][1]=tmp.m[3][2]=tmp.m[3][4]=tmp.m[4][1]=tmp.m[4][2]=tmp.m[4][3]=tmp.m[5][1]=tmp.m[5][2]=tmp.m[5][3]=tmp.m[5][4]=0;
78         tmp=MatQP(tmp,n);
79         a=MatMul(tmp,a);
80         printf("%lld\n",a.m[1][1]);
81     }
82     return 0;
83 }

时间: 2024-08-06 09:23:43

hdu4686 Arc of Dream 矩阵快速幂的相关文章

【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

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,

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

HDU4686Arc 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

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

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 =