hdu 4686 Arc of Dream【矩阵快速幂】
来源:互联网 发布:查询淘宝账号安全 编辑:程序博客网 时间:2024/05/22 13:22
Arc of Dream
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3523 Accepted Submission(s): 1108
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?
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.
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
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
题目大意:让你求Aod(n)的值,其中每个元素的计算公式已给出。
分析:因为n比较大,即使是O(n)也是一样会超时,所以我们采用矩阵快速幂的方式来解题。
解题过程+思路:
我们设Fn=an*bn Sn=Sn-1+Fn-1.
这个时候我们能够通过Fn-1和Sn-2来得到Sn-1
Sn-1=Sn-2+Fn-1.
其中an和bn还有两个公式,我们将其展开。
Fn=(an*bn)=(an-1*ax+ay)*(bn-1*bx+by)=an-1*bn-1*ax*bx+an-1*ax*ay+ay*bn-1*bx+ay*by;
这个时候我们只需要五维矩阵就能求出Sn-1:
求得矩阵为:
这个时候我们只需要n-1幂矩阵然后乘上由F1 a1 b1 1 s0构成的矩阵即可得到所求解、
最终我们推出的式子清晰的描述为这样:
AC代码:
#include<stdio.h>#include<string.h>using namespace std;#define ll __int64#define mod 1000000007typedef struct Matrix{ ll mat[7][7];}matrix;matrix A,B,tmp;Matrix matrix_mul(matrix a,matrix b){ matrix c; memset(c.mat,0,sizeof(c.mat)); int i,j,k; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { for(int k=0;k<5;k++) { c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod; c.mat[i][j]%=mod; } } } return c;}Matrix matrix_quick_power(matrix a,ll k)//矩阵快速幂0.0{ matrix b; memset(b.mat,0,sizeof(b.mat)); for(int i=0;i<5;i++) b.mat[i][i]=1;//单位矩阵b while(k) { if(k%2==1) { b=matrix_mul(a,b); k-=1; } else { a=matrix_mul(a,a); k/=2; } } return b;}int main(){ ll n; ll a,ax,ay,b,bx,by; while(~scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&a,&ax,&ay,&b,&bx,&by)) { if(n==0) { printf("0\n"); continue; } ll a1=(a*ax+ay)%mod; ll b1=(b*bx+by)%mod; ll f1=(a1*b1)%mod; ll s0=(a*b)%mod; memset(tmp.mat,0,sizeof(tmp.mat)); tmp.mat[0][0]=f1; tmp.mat[1][0]=a1; tmp.mat[2][0]=b1; tmp.mat[3][0]=1; tmp.mat[4][0]=s0; memset(A.mat,0,sizeof(A.mat)); A.mat[0][0]=(ax*bx)%mod;A.mat[0][1]=(ax*by)%mod;A.mat[0][2]=(ay*bx)%mod;A.mat[0][3]=(ay*by)%mod;A.mat[0][4]=0; A.mat[1][0]=0; A.mat[1][1]=ax%mod; A.mat[1][2]=0; A.mat[1][3]=ay%mod; A.mat[1][4]=0; A.mat[2][0]=0; A.mat[2][1]=0; A.mat[2][2]=bx%mod; A.mat[2][3]=by%mod; A.mat[2][4]=0; A.mat[3][0]=0; A.mat[3][1]=0; A.mat[3][2]=0; A.mat[3][3]=1; A.mat[3][4]=0; A.mat[4][0]=1; A.mat[4][1]=0; A.mat[4][2]=0; A.mat[4][3]=0; A.mat[4][4]=1; B=matrix_quick_power(A,n-1); B=matrix_mul(B,tmp); printf("%I64d\n",B.mat[4][0]); }}
0 0
- hdu 4686 Arc of Dream(构造矩阵快速幂)
- hdu 4686 Arc of Dream 矩阵快速幂
- hdu 4686 Arc of Dream(矩阵快速幂)
- HDU 4686 Arc of Dream(矩阵快速幂)
- hdu 4686 Arc of Dream 矩阵快速幂
- Hdu 4686 Arc of Dream 矩阵快速幂
- HDU 4686 Arc of Dream 矩阵快速幂
- HDU 4686 Arc of Dream(矩阵快速幂)
- HDU - 4686 Arc of Dream 矩阵快速幂
- HDU 4686 Arc of Dream(矩阵快速幂)
- HDU 4686 Arc of Dream(矩阵快速幂)
- hdu 4686 Arc of Dream(矩阵快速幂)
- hdu 4686 Arc of Dream【矩阵快速幂】
- HDU 4686 Arc of Dream(矩阵快速幂)
- HDU 4686 Arc of Dream (矩阵快速幂)
- HDU 4686 Arc of Dream(矩阵快速幂)
- HDU 4686 Arc of Dream(矩阵快速幂!)
- hdu 4686 Arc of Dream 矩阵快速幂
- 欢迎使用CSDN-markdown编辑器
- webstorm快捷键大全(亲自整理)
- tomcat学习之request与response
- Jsoup学习-使用Jsoup抓取页面数据
- 完美解决Android的WebView加载失败(404,500),显示的自定义视图
- hdu 4686 Arc of Dream【矩阵快速幂】
- 如何修改Android Studio中默认生成的注释信息
- UITableView contentInset注解
- C#键值对集合
- ListView的addHeaderView和setAdapter的调用顺序
- 出现module 'xgboost' has no attribute 'DMatrix'的临时解决方法
- Server.Htmlencode和Server.HtmlDecode的用法
- Ubuntu Firefox 安装flash插件
- java 枚举类总结