hdu 3509 Buge's Fibonacci Number Problem(二项分布+矩阵连乘)

Buge's Fibonacci Number Problem

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 464    Accepted Submission(s): 129


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?

 

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
zhouzeyong
 

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3509

题意:如题目中最后一个公式= =

分析:数据比较大,很明显要用矩阵连乘,然后就是推公式,推完公式后敲代码,然后wa了,调试好久都是wa,原来是二项分布的系数与式子中的系数没分开,分开后还是wa= =。。。。然后就是二项分布系数涉及到除法,不能先取模再除。。。之后干脆不取模,就AC了,总体上来看,推公式用了10分钟,敲代码用了10分钟,然后剩下的3个小时就是无尽的debug,看来还是不够细心啊,还有我很讨厌数学题 T_T

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int mm=55;
__int64 x[mm][mm],y[mm][mm],s[mm][mm],A[mm],B[mm],F1[mm],F2[mm],tmp;
int f1,f2,a,b,K,n,m;
int i,j,k,t;
void mul(__int64 x[mm][mm],__int64 y[mm][mm])
{
    int i,j,k;
    for(i=0;i<K+2;++i)
        for(j=0;j<K+2;++j)
            for(s[i][j]=k=0;k<K+2;++k)
            {
                s[i][j]+=x[i][k]*y[k][j];
                if(s[i][j]>m)s[i][j]%=m;
            }
    for(i=0;i<K+2;++i)
        for(j=0;j<K+2;++j)
            x[i][j]=s[i][j];
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d%d%d",&f1,&f2,&a,&b,&K,&n,&m);
        A[0]=B[0]=F1[0]=F2[0]=1;
        for(i=1;i<K+2;++i)
        {
            A[i]=A[i-1]*a;
            B[i]=B[i-1]*b;
            F1[i]=F1[i-1]*f1;
            F2[i]=F2[i-1]*f2;
            if(A[i]>=m)A[i]%=m;
            if(B[i]>=m)B[i]%=m;
            if(F1[i]>=m)F1[i]%=m;
            if(F2[i]>=m)F2[i]%=m;
        }
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        for(i=0;i<K+2;++i)x[i][i]=1;
        y[0][0]=y[0][1]=1;
        for(i=1;i<K+2;++i)
            for(j=1,tmp=1,k=K-i+2;k--;++j)
            {
                y[i][j]=(tmp%m)*A[k];
                if(y[i][j]>=m)y[i][j]%=m;
                y[i][j]*=B[j-1];
                if(y[i][j]>=m)y[i][j]%=m;
                tmp*=k;
                tmp/=j;
            }
        --n;
        while(n)
        {
            if(n&1)mul(x,y);
            mul(y,y);
            n>>=1;
        }
        tmp=F1[K]*x[0][0];
        if(tmp>=m)tmp%=m;
        for(i=1;i<K+2;++i)
        {
            tmp+=((F2[K-i+1]*F1[i-1])%m)*x[0][i];
            if(tmp>=m)tmp%=m;
        }
        printf("%I64d\n",tmp);
    }
    return 0;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值