HDU 3059 Fibonacci數列與矩陣求和 矩陣大小不固定
http://acm.hdu.edu.cn/showproblem.php?pid=3509
Problem Description
snowingsea is having Buge’s discrete mathematics lesson, Buge is now talking about the Fibonacci Number. As a bright student, snowingsea, of course, takes it as a piece of cake. He feels boring and soon comes over drowsy.
Buge,feels unhappy about him, he knocked at snowingsea’s head, says:”Go to solve the problem on the blackboard!”, snowingsea suddenly wakes up, sees the blackboard written :

snowingsea thinks a moment,and writes down:

snowingsea has a glance at Buge,Buge smiles without talking, he just makes a little modification on the original problem, then it becomes :

The modified problem makes snowingsea nervous, and he doesn't know how to solve it. By the way,Buge is famous for failing students, if snowingsea cannot solve it properly, Buge is very likely to fail snowingsea. But snowingsea has many ACM friends. So,snowingsea is asking the brilliant ACMers for help. Can you help him?
Buge,feels unhappy about him, he knocked at snowingsea’s head, says:”Go to solve the problem on the blackboard!”, snowingsea suddenly wakes up, sees the blackboard written :

snowingsea thinks a moment,and writes down:

snowingsea has a glance at Buge,Buge smiles without talking, he just makes a little modification on the original problem, then it becomes :

The modified problem makes snowingsea nervous, and he doesn't know how to solve it. By the way,Buge is famous for failing students, if snowingsea cannot solve it properly, Buge is very likely to fail snowingsea. But snowingsea has many ACM friends. So,snowingsea is asking the brilliant ACMers for help. Can you help him?
Input
The input consists of several test cases. The first line contains an integer T representing the number of test cases. Each test case contains 7 integers, they are f1, f2, a, b, k, n, m which were just mentioned above, where 0 < f1, f2, a, b, n, m < 1000 000
000, and 0 ≤ k < 50.
Output
For each case, you should print just one line, which contains S(n,k) %m.
Sample Input
3
1 1 1 1 1 2 100000
1 1 1 1 1 3 100000
1 1 1 1 1 4 100000
Sample Output
2
4
7
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
typedef long long LL;
const int MAX=54;
LL MOD,n_size;//n_size是變化矩陣的規模
struct Matrix
{
long long m[MAX][MAX];
};
Matrix P;
Matrix I;
LL quick_mod(LL m,LL n,LL k)
{
if(n==0)
return 1;
int b=1;
while(n>0)
{
if(n&1)
b=(b*m)%k;
n=n>>1;
m=(m*m)%k;
}
return b;
}
Matrix matrixmul(Matrix a,Matrix b)
{
int i,j,k;
Matrix c;
for(i=0; i<n_size; i++)
for(int j=0; j<n_size; j++)
{
c.m[i][j]=0;
for(k=0; k<n_size; k++)
c.m[i][j]+=((a.m[i][k]%MOD)*(b.m[k][j]%MOD))%MOD;
c.m[i][j]%=MOD;
}
return c;
}
Matrix quickpow(Matrix m, LL n)
{
Matrix b=I;
while(n>=1)
{
if(n&1)
b=matrixmul(b,m);
n=n>>1;
m=matrixmul(m,m);
}
return b;
}
LL c[50][50];
int main()
{
Matrix tmp;
LL sum=0,temp1,temp2;
LL f1,f2,a,b,k,n,m;
memset(c,0,sizeof(c));
for(int i=0; i<=49; i++)
{
c[i][0]=1;
c[i][i]=1;
}
for(int i=1; i<=49; i++)
for(int j=1; j<i; j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
int T;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%I64d %I64d %I64d %I64d %I64d %I64d %I64d",&f1,&f2,&a,&b,&k,&n,&MOD);
if(k==0)
printf("%I64d\n",n%MOD);
if(k>=1)
{
if(n==1)
{
printf("%I64d\n",quick_mod(f1,k,MOD));
continue;
}
if(n==2)
{
printf("%I64d\n",(quick_mod(f1,k,MOD)+quick_mod(f2,k,MOD))%MOD);
continue;
}
n_size=k+2;
memset(P.m,0,sizeof(P.m));
memset(I.m,0,sizeof(I.m));
for(int i=0; i<n_size; i++)
I.m[i][i]=1;
P.m[0][0]=1;
P.m[0][n_size-1]=1;
for(int u=1; u<n_size-1; u++)
P.m[0][u]=0;
for(int j=1; j<n_size; j++)
for(int k=n_size-j,w=0; k<n_size; k++,w++)
P.m[j][k]=(((c[j-1][w]%MOD)*quick_mod(a,w,MOD))%MOD*quick_mod(b,j-1-w,MOD))%MOD;
tmp=quickpow(P,n-1);
sum=(sum+(tmp.m[0][0]%MOD)*quick_mod(f1,k,MOD))%MOD;
for(int i=1; i<n_size; i++)
{
temp1=(quick_mod(f1,n_size-1-i,MOD)*quick_mod(f2,i-1,MOD))%MOD;
temp2=(temp1*tmp.m[0][i]%MOD)%MOD;
sum=(sum+temp2)%MOD;
}
printf("%I64d\n",sum%MOD);
}
}
return 0;
}
相關文章
- 資料結構之陣列和矩陣--矩陣&不規則二維陣列資料結構陣列矩陣
- 矩陣和陣列矩陣陣列
- 演算法-陣列與矩陣演算法陣列矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 數學建模例題2.30 矩陣元素求和示例矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- 巨大的矩陣(矩陣加速)矩陣
- 鄰接矩陣、度矩陣矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)矩陣
- 斐波那契數列Ⅳ【矩陣乘法】矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- python輸入詳解(陣列、矩陣)Python陣列矩陣
- 矩陣矩陣
- 求任意矩陣的伴隨矩陣矩陣
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- 奇異矩陣與非奇異矩陣的定義與區別矩陣
- 高等代數1 矩陣矩陣
- 線性代數--矩陣矩陣
- 理解矩陣矩陣
- 海浪矩陣矩陣
- 矩陣相乘矩陣
- 稀疏矩陣矩陣
- 螺旋矩陣矩陣
- 矩陣乘法矩陣
- 8.6 矩陣?矩陣
- 找矩陣矩陣
- 矩陣分解矩陣
- 第四章:多維陣列和矩陣 ------------- 4.8 子矩陣的最大累加和陣列矩陣
- 第三章,矩陣,03-矩陣與行列式矩陣
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- 6.5陣列--模擬、偏移量-螺旋矩陣陣列矩陣
- 列舉(矩陣消除、七段數碼管)矩陣
- C++ 練氣期之二維陣列與矩陣運算C++陣列矩陣
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣指數的定義矩陣
- c語言中實現4行3列矩陣和3行4列矩陣的運算C語言矩陣