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.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 1018, and all the other integers are no more than 2×109.

Output

For each test case, output AoD(N) modulo 1,000,000,007.

Sample Input

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

Sample Output

4
134
1902

Author

Zejun Wu (watashi)

Source

2013 Multi-University Training Contest 9

因为:a[i]*b[i]=(a[i-1]*AX+AY)*(b[i-1]*BX+BY)

      =(a[i-1]*b[i-1]*AX*BX+a[i-1]*AX*BY+b[i-1]*BX*AY+AY*BY)

构造矩阵:

                                                          |  1         0    0     0      0   |

                                                          |  AX*BY   AX   0     AX*BY    0   |

           {AoD(n-1),a[i-1],b[i-1],a[i-1]*b[i-1],1}*      |  BX*AY    0   BX    BX*AY    0   |      ={AoD(n),a[i],b[i],a[i]*b[i],1}

                                                          |  AX*BX    0   0      AX*BX   0   |

                                                          |  AY*BY   AY   BY    AY*BY    1   |

 

 

另外注意:

if(n==0){//这个判断条件很重要,没有就会超时
printf("0\n");
continue;
}

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<math.h>
  7 #include<algorithm>
  8 #include<queue>
  9 #include<set>
 10 #include<bitset>
 11 #include<map>
 12 #include<vector>
 13 #include<stdlib.h>
 14 #include <stack>
 15 using namespace std;
 16 #define PI acos(-1.0)
 17 #define max(a,b) (a) > (b) ? (a) : (b)
 18 #define min(a,b) (a) < (b) ? (a) : (b)
 19 #define ll long long
 20 #define eps 1e-10
 21 #define MOD 1000000007
 22 #define N 1000000
 23 #define inf 1e12
 24 ll n;
 25 ll A0,Ax,Ay,B0,Bx,By;
 26 struct Matrix{
 27    ll mp[5][5];
 28 };
 29 Matrix Mul(Matrix a,Matrix b){
 30    Matrix res;
 31    for(ll i=0;i<5;i++){
 32       for(ll j=0;j<5;j++){
 33          res.mp[i][j]=0;
 34          for(ll k=0;k<5;k++){
 35             res.mp[i][j]=(res.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%MOD+MOD)%MOD;
 36          }
 37       }
 38    }
 39    return res;
 40 }
 41 Matrix fastm(Matrix a,ll b){
 42    Matrix res;
 43    memset(res.mp,0,sizeof(res.mp));
 44    for(ll i=0;i<5;i++){
 45       res.mp[i][i]=1;
 46    }
 47    while(b){
 48       if(b&1){
 49          res=Mul(res,a);
 50       }
 51       a=Mul(a,a);
 52       b>>=1;
 53    }
 54    return res;
 55 }
 56 int main()
 57 {
 58    while(scanf("%I64d",&n)==1){
 59       scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&A0,&Ax,&Ay,&B0,&Bx,&By);
 60
 61       if(n==0){//这个判断条件很重要,没有就会超时
 62          printf("0\n");
 63          continue;
 64       }
 65
 66
 67       ll a0=A0;
 68       ll b0=B0;
 69
 70       Matrix tmp;
 71       memset(tmp.mp,0,sizeof(tmp.mp));
 72       tmp.mp[0][0]=1%MOD;
 73       tmp.mp[1][0]=Ax*By%MOD;
 74       tmp.mp[1][1]=Ax%MOD;
 75       tmp.mp[1][3]=Ax*By%MOD;
 76       tmp.mp[2][0]=Bx*Ay%MOD;
 77       tmp.mp[2][2]=Bx%MOD;
 78       tmp.mp[2][3]=Bx*Ay%MOD;
 79       tmp.mp[3][0]=Ax*Bx%MOD;
 80       tmp.mp[3][3]=Ax*Bx%MOD;
 81       tmp.mp[4][0]=Ay*By%MOD;
 82       tmp.mp[4][1]=Ay%MOD;
 83       tmp.mp[4][2]=By%MOD;
 84       tmp.mp[4][3]=Ay*By%MOD;
 85       tmp.mp[4][4]=1%MOD;
 86
 87       Matrix cnt=fastm(tmp,n-1);
 88
 89       Matrix g;
 90       memset(g.mp,0,sizeof(g.mp));
 91       g.mp[0][0]=a0*b0%MOD;
 92       g.mp[0][1]=a0%MOD;
 93       g.mp[0][2]=b0%MOD;
 94       g.mp[0][3]=a0*b0%MOD;
 95       g.mp[0][4]=1%MOD;
 96       Matrix ans=Mul(g,cnt);
 97       printf("%I64d\n",ans.mp[0][0]);
 98    }
 99     return 0;
100 }

时间: 2024-08-24 07:58:05

hdu 4686 Arc of Dream(矩阵快速幂乘法)的相关文章

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

【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

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 矩阵高速幂

矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. 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 1575 Tr A(矩阵快速幂乘法优化算法)

Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output 对应每组数据,输出Tr(A^k)%9973. Sample Input 2 2 2 1 0 0 1 3 99999

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 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在