hdu 3306 结合本题说下矩阵构造方法
2012-11-20 15:03
162 查看
Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 994 Accepted Submission(s): 397
[align=left]Problem Description[/align]
As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate
S(N) , S(N) = A(0)2 +A(1)2+……+A(n)2.
[align=left]Input[/align]
There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1
[align=left]Output[/align]
For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.
[align=left]Sample Input[/align]
2 1 1
3 2 3
[align=left]Sample Output[/align]
6
196
由题意
s(n)=s(n-1)+a(n)^2
=s(n-1)+x^2*a(n-1)^+y^2^a(n-2)^2+2xy*a(n-1)*a(n-2)
右边共有4项 所以构造4*4矩阵
则 用矩形A*[s(n-1), a(n-1)^2, a(n-2)^2, a(n-1)*a(n-2)]=[s(n), a(n)^2 ,a(n-1)^2, a(n)*a(n-1)]
后者比前者全部大一个层次 (即n-1变n 常数除外 这个木有常数)
借用下下面博客的图片
http://www.darkswordzone.com/?p=1432
![](http://img.my.csdn.net/uploads/201211/20/1353395483_6764.png)
根据s(n)=s(n-1)+x^2*a(n-1)^+y^2^a(n-2)^2+2xy*a(n-1)*a(n-2) 很容易知道第一行填什么
根据a(n)^2=x^2*a(n-1)^+y^2^a(n-2)^2+2xy*a(n-1)*a(n-2) 也很容易知道第二行填什么
第三行更简单
第四行 不好直接找出来 这时候就要充分利用题目所给条件了
把后者分开 即a(n)*a(n-1)=(X * A(N - 1) + Y * A(N - 2))*a(n-1)
=x*a(n-1)^2+y*a(n-2)*a(n-1) 这时候很容易就能看出来矩阵应该是什么 了
矩阵构造完毕 我以前不知道如何去构造矩阵 煞是头疼啊,我想这种矩阵构造方法应该是可以的 没有人教我 只能 自己参考了一些资料 不知道有没有更简单快速的方法 求大神教授
AC代码
#include<stdio.h> #define N 4 #define mod 10007 struct mat { int mar[4][4]; }; mat a,b,c,init,temp; mat res= { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; mat mul(mat a1,mat b1) { int i,j,l; mat c1; for (i=0;i<N;i++) { for (j=0;j<N;j++) { c1.mar[i][j]=0; for (l=0;l<N;l++) { c1.mar[i][j]+=(a1.mar[i][l]*b1.mar[l][j])%mod; c1.mar[i][j]%=mod; } } } return c1; } mat er_fun(mat e,int x) { mat tp; tp=e; e=res; while(x) { if(x&1) e=mul(e,tp); tp=mul(tp,tp); x>>=1; } return e; } int main() { int n,x,y,i,j; while(scanf("%d %d %d",&n,&x,&y)!=EOF) { x=x%mod; y=y%mod; init.mar[0][0]=1; init.mar[0][1]=(x*x)%mod; init.mar[0][2]=(y*y)%mod; init.mar[0][3]=(2*x*y)%mod; init.mar[1][0]=0; init.mar[1][1]=(x*x)%mod; init.mar[1][2]=(y*y)%mod; init.mar[1][3]=(2*x*y)%mod; init.mar[2][0]=0; init.mar[2][1]=1; init.mar[2][2]=0; init.mar[2][3]=0; init.mar[3][0]=0; init.mar[3][1]=x; init.mar[3][2]=0; init.mar[3][3]=y; a=init; b=er_fun(a,n-1); printf("%d\n",(2*b.mar[0][0]+b.mar[0][1]+b.mar[0][2]+b.mar[0][3])%mod); } return 0; }
hnust_xiehonghao
相关文章推荐
- 【HDU】1757 - A Simple Math Problem(矩阵构造方法 & 快速幂)
- hdu 3306 矩阵构造
- HDU 3306 矩阵构造
- HDU 4549 M斐波那契数列(构造矩阵)
- [HDU 3306] Another kind of Fibonacci · 矩阵快速幂
- 压缩感知测量矩阵构造方法研究
- 静态代码块构造方法结合笔试题
- hdu 5015 233 Matrix(矩阵构造加快速幂)
- 【转】 矩阵构造方法
- hdu 3306(矩阵快速幂)
- HDU 3306 Another kind of Fibonacci(矩阵快速幂)
- HDU 3306 Another kind of Fibonacci(矩阵快速幂)
- 考研路茫茫——单词情结 HDU - 2243 AC自动机/特征字符串构造计数/等比矩阵求和
- So Easy! HDU - 4565(矩阵快速幂,无理数矩阵构造,共轭构造)
- HDU 4359 Easy Tree DP? 带权二叉树的构造方法 dp
- hdu 1005 Number Sequence 构造矩阵 + 矩阵快速幂
- A Simple Math Problem(HDU 1757 构造矩阵)
- HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)
- HDU 5015 233Matrix (构造矩阵)
- HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)