HDU 1588 斐波那契數列數列變形和矩陣連乘
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 100
2 0 4 100
Sample Output
21
12
解題思路:
方法如下:
另外值得一提的是:我們在構造矩陣的時候要用 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 100
2 0 4 100
Sample Output
21
12
相關文章
- hdu 3117矩陣+斐波那契數列矩陣
- 斐波那契數列Ⅳ【矩陣乘法】矩陣
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- 斐波那契數列
- HDU 4549M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- 斐波那契數列 (C#)C#
- PHP 與斐波那契數列PHP
- 斐波那契數列詳解
- js實現斐波那契數列JS
- 斐波那契數列js 實現JS
- 斐波那契數列演算法演算法
- 演算法(1)斐波那契數列演算法
- 面試題9-斐波那契數列面試題
- 大數斐波那契數列的演算法演算法
- 斐波那契數列的第N項(1≤n≤10^18 矩陣快速冪)矩陣
- 使用Python實現斐波那契數列Python
- JavaScript 實現:輸出斐波那契數列JavaScript
- js迭代器實現斐波那契數列JS
- 演算法一:斐波那契阿數列演算法
- 斐波那契數列的分治法計算
- 斐波那契數列的python實現Python
- 斐波那契數列三種實現函式函式
- 計算斐波那契數列的演算法演算法
- 劍指offer-9-斐波那契數列-javaJava
- 斐波那契數列演算法 JS 實現演算法JS
- HDU2813Interesting Fibonacci(斐波那契數列+迴圈節)REST
- 斐波那契數列魔法塔數字量與層數的關係
- 從斐波那契到矩陣快速冪矩陣
- 斐波那契數列的通項公式及證明公式
- 每日一算 -- 斐波那契數列型別題型別
- 斐波那契數列 多語言實現 筆記筆記
- js計算斐波那契數列程式碼例項JS
- 斐波那契數列的遞迴和非遞迴實現遞迴
- [譯] 斐波那契數列中的偶數 (Python vs. JavaScript)PythonJavaScript
- 如何將斐波那契數列應用到排版設計中
- Python 實現 動態規劃 /斐波那契數列Python動態規劃
- 資料結構之斐波那契數列java實現資料結構Java
- “斐波那契數列”問題的遞推演算法演算法