hdu 4686
来源:互联网 发布:javascript讲师 编辑:程序博客网 时间:2024/06/16 22:43
Arc of Dream
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2168 Accepted Submission(s): 682
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
11 2 34 5 621 2 34 5 631 2 34 5 6
Sample Output
41341902
Author
Zejun Wu (watashi)
Source
2013 Multi-University Training Contest 9
矩阵快速幂
ai = ai-1 * Ax + Ay;
bi = bi-1 * Bx + By;
ai * bi = ai-1 * bi-1 * (Ax * Bx) + ai-1 * (Ax * By) + bi-1 * (Bx * Ay) + AyBy;
Si = Si - 1 + ai * bi = Si - 1 + ai-1 * bi-1 * (Ax * Bx) + ai-1 * (Ax * By) + bi-1 * (Bx * Ay) + AyBy;
根据SI , 一定需要 Si - 1, ai - 1, bi - 1, ai - 1 * bi - 1, 常数。
矩阵大小为5,
构造 初始矩阵A 为 记 S0
[1, A0, B0, A0 * B0, A0 * B0] 即 [1, a0, b0, a0 * b0, S0]
[0 …… 0]
转换矩阵 B. 使得 A * B = S1 = [1, a1, b1, a1 * b1 , S1]
根据上面4个公式得。
第一列为 1, 0, 0, 0, 0;
第二列为 Ay, Ax, 0, 0, 0;
第三列为 By, 0, Bx, 0, 0;
第四列为 AyBy, Ax * By, Bx * Ay, Ax * Bx, 0;
第五列为 AyBy, Ax * By, Bx * Ay, Ax * Bx, 1;
即
N次递推得
现在要求 Sn - 1 , 所以 init * Pow ^ n - 1. 然后取第一行, 第5个元素。
#include<stdio.h>#include<string.h>#define mod 1000000007 struct matrix{ __int64 mat[6][6];};matrix multi(matrix a,matrix b,__int64 n){ __int64 i,j,k; matrix ans; memset(ans.mat,0,sizeof(ans.mat)); for(i=0;i<n;i++) for(j=0;j<n;j++) for(k=0;k<n;k++) { ans.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod; ans.mat[i][j]%=mod; } return ans;}matrix pow(matrix a,__int64 n,__int64 k){ matrix ans=a; k--; while(k) { if(k&1) ans=multi(ans,a,n); a=multi(a,a,n); k/=2; } return ans;}int main(){ __int64 a0,b0,ax,ay,bx,by,n; while(scanf("%I64d",&n)!=EOF) { matrix a,b,ans; memset(a.mat,0,sizeof(a.mat)); memset(b.mat,0,sizeof(b.mat)); scanf("%I64d%I64d%I64d",&a0,&ax,&ay); scanf("%I64d%I64d%I64d",&b0,&bx,&by); a0%=mod; ax%=mod; ay%=mod; b0%=mod; bx%=mod; by%=mod; if(n==0) {//n=0的时候特殊处理一下。 printf("0\n"); continue; } if(n==1) { printf("%I64d\n",a0*b0%mod); continue; } a.mat[0][0]=1; a.mat[1][0]=ax*bx%mod; a.mat[1][1]=ax*bx%mod; a.mat[2][0]=ax*by%mod; a.mat[2][1]=ax*by%mod; a.mat[2][2]=ax; a.mat[3][0]=ay*bx%mod; a.mat[3][1]=ay*bx%mod; a.mat[3][3]=bx; a.mat[4][0]=ay*by%mod; a.mat[4][1]=ay*by%mod; a.mat[4][2]=ay; a.mat[4][3]=by; a.mat[4][4]=1; b.mat[0][0]=a0*b0%mod; b.mat[0][1]=a0*b0%mod; b.mat[0][2]=a0; b.mat[0][3]=b0; b.mat[0][4]=1; ans=multi(b,pow(a,5,n-1),5); printf("%I64d\n",ans.mat[0][0]%mod); } return 0;}
0 0
- HDU 4686
- hdu 4686
- HDU 4686解题报告
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- 菜鸟一号报道,问一个很邪恶的问题!
- 算法重拾之路——大数乘法
- sparkSQL1.1入门之十:总结
- Apache Shiro Caching学习笔记
- LaTeX各种表格
- hdu 4686
- C/C++之回调函数
- 不行不行,不能再讨厌英语了,要爱上英语。。。I Love You。。。
- srotyboard之间传值
- windows OpenCV 配置 Python 并配置UliPad IDE
- Unable to execute dex: java.nio.BufferOverflowException
- 工厂方法模式(Factory Method)
- java 使用JUnit4进行单元测试
- oracle 用户权限