HDU 1588 斐波那契数列数列变形和矩阵连乘
来源:互联网 发布:数据分析范例 编辑:程序博客网 时间:2024/05/18 11:01
http://acm.hdu.edu.cn/showproblem.php?pid=1588
Problem Description
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.
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.
Input
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.
Each of them will not exceed 1,000,000,000.
Output
For each line input, out the value described above.
Sample Input
2 1 4 1002 0 4 100
Sample Output
2112
解题思路:
方法如下:
另外值得一提的是:我们在构造矩阵的时候要用 1 1 f(n-2) f[n-1]
=
1 0 f[n-1] f[n] 的形式,这样一来我们就不用再讨论n是否为零的情况了,若f[n-1]在f[n-2]上方那么f[0]是无法用矩阵运算表示的。
代码如下:
#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>using namespace std;typedef long long LL;LL k,b,n,MOD;const int N=4;const int M=2;struct Matrix1{ LL m[N][N];};struct Matrix2{ LL m[M][M];};Matrix2 I2={ 1,0, 0,1};Matrix1 I1={ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};Matrix1 multi1(Matrix1 a,Matrix1 b){ Matrix1 c; for(int i=0; i<N; i++) for(int j=0; j<N; j++) { c.m[i][j]=0; for(int k=0; k<N; k++) { c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD; } c.m[i][j]=c.m[i][j]%MOD; } return c;}Matrix1 quick_mod1(Matrix1 a,LL k){ Matrix1 ans=I1; while(k!=0) { if(k&1) { ans=multi1(ans,a); } k>>=1; a=multi1(a,a); } return ans;}Matrix2 multi2(Matrix2 a,Matrix2 b){ Matrix2 c; for(int i=0; i<M; i++) for(int j=0; j<M; j++) { c.m[i][j]=0; for(int k=0; k<M; k++) { c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD; } c.m[i][j]=c.m[i][j]%MOD; } return c;}Matrix2 quick_mod2(Matrix2 a,LL k){ Matrix2 ans=I2; while(k!=0) { if(k&1) { ans=multi2(ans,a); } k>>=1; a=multi2(a,a); } return ans;}int main(){ while(~scanf("%I64d%I64d%I64d%I64d",&k,&b,&n,&MOD)) { Matrix2 A={0,1, 1,1 }; Matrix1 P={0,0,1,0, 0,0,0,1, 0,0,1,0, 0,0,0,1}; Matrix2 t1=quick_mod2(A,b); Matrix2 t2=quick_mod2(A,k); P.m[0][0]=t2.m[0][0]; P.m[0][1]=t2.m[0][1]; P.m[1][0]=t2.m[1][0]; P.m[1][1]=t2.m[1][1]; Matrix1 t3=quick_mod1(P,n); LL x=(t3.m[0][3]*t1.m[0][0]%MOD+t3.m[1][3]*t1.m[0][1]%MOD)%MOD; if(x<MOD) x+MOD; printf("%I64d\n",x); } return 0;}
Problem Description
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.
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.
Input
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.
Each of them will not exceed 1,000,000,000.
Output
For each line input, out the value described above.
Sample Input
2 1 4 1002 0 4 100
Sample Output
2112
0 0
- HDU 1588 斐波那契数列数列变形和矩阵连乘
- hdu3117(斐波那契数列,矩阵连乘)
- 寂寞的素数 斐波那契数列性质+矩阵连乘+欧拉定理 经典啊
- HDU 3936 斐波那契性质矩阵连乘
- 斐波那契数列的变形
- hdu 4549 M斐波那契数列 数论 矩阵
- hdu 3117矩阵+斐波那契数列
- 斐波拉契数列矩阵连乘(模板)
- 矩阵-斐波那契数列
- 矩阵专题:斐波那契数列
- 矩阵-斐波那契数列
- 斐波那契数列-矩阵乘法
- 矩阵与斐波那契数列
- HDU 3117(数论,斐波那契+矩阵连乘)
- 斐波那契数列(兔子问题)的变形
- 斐波那契数列及变形题
- 斐波那契数列变形之跳台阶问题
- 斐波那契数列变形之青蛙跳台阶
- 5中颜色的球若干,一次摸三个,求3种不同颜色的球的取法
- HDU 2043 密码
- android button禁止连续点击
- Linux 目录导航技巧
- 杰米棋牌教你棋牌游戏成功运营的十大步骤
- HDU 1588 斐波那契数列数列变形和矩阵连乘
- 7/26
- tar.bz2 解压命令
- python 进阶学习之14
- aul 文件操作
- 【程序员编程艺术】学习记录3:字符串包含问题
- hdu 2565
- UVa11609 - Teams(快速求幂)
- unity3d input输入