Buge's Fibonacci Number Problem
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 706 Accepted Submission(s): 197
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
Source
Recommend
比较简单的矩阵构造,再利用矩阵快速幂即可。
闲话不多说,写出矩阵转移方程即可。
参考代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
struct mat
{
ll d[55][55];
} A,B,E;
int t,f1,f2,a,b,k,n,m;
ll c[55][55];
mat multi(mat a,mat b)
{
mat ans;
rep(i,0,k+2)
{
rep(j,0,k+2)
{
ans.d[i][j]=0;
rep(k1,0,k+2)
{
if(a.d[i][k1]&&b.d[k1][j])
{
ans.d[i][j]=(ans.d[i][j]+a.d[i][k1]*b.d[k1][j])%m;
}
}
}
}
return ans;
}
mat quickmulti(mat a,int n)
{
mat ans=E;
while(n)
{
if(n&1)
{
n--;
ans=multi(ans,a);
}
else
{
n>>=1;
a=multi(a,a);
}
}
return ans;
}
ll pow_mod(ll x,ll n,int mod)
{
ll ans=1;
while(n)
{
if(n&1) ans=(ans*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return ans;
}
int main()
{
CLR(E.d);
REP(i,0,50)
E.d[i][i]=1;
read(t);
REP(i,0,50)
{
REP(j,0,i)
{
if(j==0||j==i) c[i][j]=1;
else c[i][j]=c[i-1][j]+c[i-1][j-1];
}
}
while(t--)
{
scanf("%d%d%d%d%d%d%d",&f1,&f2,&a,&b,&k,&n,&m);
if(n==1)
{
printf("%I64d\n",pow_mod(f1,k,m));
continue;
}
if(n==2)
{
ll ans=(pow_mod(f1,k,m)+pow_mod(f2,k,m))%m;
printf("%I64d\n",ans);
continue;
}
CLR(A.d);
A.d[0][0]=1;
REP(i,1,k+1)
A.d[0][i]=((((c[k][i-1]%m)*pow_mod(a,k+1-i,m))%m)*pow_mod(b,i-1,m))%m;
REP(i,1,k+1)
REP(j,1,k+2-i)
A.d[i][j]=((((c[k+1-i][j-1]%m)*pow_mod(a,k+2-i-j,m))%m)*pow_mod(b,j-1,m))%m;
CLR(B.d);
B.d[0][0]=(pow_mod(f1,k,m)+pow_mod(f2,k,m))%m;
REP(i,1,k+1)
B.d[i][0]=(pow_mod(f2,k+1-i,m)*pow_mod(f1,i-1,m))%m;
mat ans=quickmulti(A,n-2);
ans=multi(ans,B);
printf("%I64d\n",ans.d[0][0]);
}
}