hdu 1588 Gauss Fibonacci 矩阵的高次高速求幂,矩阵的高速求和,斐波那契公式
2013-09-11 20:48
459 查看
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1778 Accepted Submission(s): 772
[align=left]Problem Description[/align]
Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.
Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.
Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)
The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.
[align=left]Input[/align]
The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.
[align=left]Output[/align]
For each line input, out the value described above.
[align=left]Sample Input[/align]
2 1 4 100
2 0 4 100
[align=left]Sample Output[/align]
21
12
[align=left]Author[/align]
DYGG
[align=left]Source[/align]
HDU
“Valentines Day” Open Programming Contest 2007-02-14
这个题目我真心觉得超级难,我做完这道题时真心精力耗尽了。
这个题目用了三个知识,斐波那契数,矩阵的高次幂求法,还有就是矩阵的求和的快速公式,后两个全部是二分法的思路:
嘿嘿,这个题目的主要思路是f(k*i+b)=a^(k*i+b),所以求其和就是a^b+a^(k+b)+a^(2*k+b)+(省略)。。。。。。,这样就是了嘛。所以把a^b提出来,可以写成a^b(
1+a^k+a^(2*k)+a^(3*k)),先求出a^b是多少,再求出a^k 是多少,在求其和,再与a^b相乘就是了嘛,很简单嘛。这里的a矩阵是{1,1,1,0};
写起来可是真心不容易啊!!!!
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int i,j,k,b,n,m; struct info { long long int a[3][3]; }; info unit; info mat; info tp; info mut(info x,info y) { info c; for(i=1;i<3;i++) for(j=1;j<3;j++) c.a[i][j]=0; for(i=1;i<3;i++) for(j=1;j<3;j++) for(k=1;k<3;k++) { c.a[i][j]+=x.a[i][k]*y.a[k][j]; c.a[i][j]=c.a[i][j]%m; } return c; } info pow(info p,int k) { tp=p; p=unit; while(k) { if(k&1) p=mut(p,tp); tp=mut(tp,tp); k>>=1; } return p; } info add(info x,info y) { info c; for(i=1;i<3;i++) { for(j=1;j<3;j++) { c.a[i][j]=(x.a[i][j]+y.a[i][j])%m; } } return c; } info f(int k,info p) { if(k==1) return p; else if(k&1) return add(f(k-1,p),pow(p,k)); else return mut(f(k>>1,p),add(pow(p,k>>1),unit)); } int main() { info ak,ab,ans; unit.a[1][1]=1;unit.a[1][2]=0; unit.a[2][1]=0;unit.a[2][2]=1; mat.a[1][1]=1;mat.a[1][2]=1; mat.a[2][1]=1;mat.a[2][2]=0; while(scanf("%d%d%d%d",&k,&b,&n,&m)!=EOF) { ak=pow(mat,k); ab=pow(mat,b); ans=add(unit,f(n-1,ak)); ans=mut(ans,ab); printf("%d\n",ans.a[2][1]); } return 0; }
相关文章推荐
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
- HDU 1588 Gauss Fibonacci ★(矩阵 && 求和)
- hdu 1588 Gauss Fibonacci(函数嵌套、转换、等比矩阵求和)
- HDU 2254 奥运(矩阵高速幂+二分等比序列求和)
- hdu 1588 Gauss Fibonacci(等比矩阵二分求和)
- hdu_1588_矩阵_求和_乘法_快速幂_斐波那契数列求前n项和
- HDU 1588 二分矩阵连乘求和
- HDU 1588 Gauss Fibonacci(矩阵快速幂+二分等比序列求和)
- hdu 1588 Gauss Fibonacci(斐波那契,等比数列求和,矩阵快速幂)
- HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)
- HDU 1588(数论,构造二分矩阵+求幂运算+二分求和)
- HDU 1588 Gauss Fibonacci ★(矩阵 && 求和)
- poj3233 hdu 1588 (矩阵)
- hdu 2604 Queuing (矩阵高速幂)
- hdu 2254 奥运(矩阵降幂+二分求和)
- HDOJ(HDU) 2156 分数矩阵(嗯、求和)
- hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)
- hdu 1005 Number Sequence(矩阵连乘+二分快速求幂)
- hdu 1588 矩阵
- [HDU 5411] CRB and Puzzle (矩阵加速DP + 前缀和矩阵|等比求和快速幂 )