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 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- 斐波那契數列
- 斐波那契數列(Java)Java
- 斐波那契數列數與等冪和
- 斐波那契數列詳解
- 著名的斐波那契數列
- 斐波那契數列 (C#)C#
- PHP 與斐波那契數列PHP
- [C103] 斐波那契數列
- 力扣之斐波那契數列力扣
- 斐波那契數列js 實現JS
- 劍指offer——斐波那契數列
- js實現斐波那契數列JS
- 斐波那契數列演算法演算法
- 第十題:斐波那契數列
- 斐波那契數列的來源——數兔子
- 使用Python實現斐波那契數列Python
- 演算法(1)斐波那契數列演算法
- 大數斐波那契數列的演算法演算法
- JavaScript 實現:輸出斐波那契數列JavaScript
- js迭代器實現斐波那契數列JS
- offer通過--9斐波那契數列-2
- 演算法一:斐波那契阿數列演算法
- 斐波那契數列:7數5層魔法塔(3)
- 斐波那契數列:7數5層魔法塔(2)
- 斐波那契數列:7數5層魔法塔(5)
- 斐波那契數列:7數5層魔法塔(8)
- 斐波那契數列:7數5層魔法塔(13)
- 斐波那契數列:7數5層魔法塔(12)
- 斐波那契數列:7數5層魔法塔(10)
- 斐波那契數列:7數5層魔法塔(11)
- 斐波那契數列:7數5層魔法塔(7)
- 斐波那契數列:7數5層魔法塔(15)
- 斐波那契數列:7數5層魔法塔(14)
- 斐波那契數列:7數5層魔法塔(6)
- 斐波那契數列:7數5層魔法塔(9)
- 斐波那契數列:7數5層魔法塔(16)
- 斐波那契數列:7數5層魔法塔(1)