hdu 3509 Buge's Fibonacci Number Problem(二项分布+矩阵连乘)
来源:互联网 发布:淘宝基础题 编辑:程序博客网 时间:2024/06/05 23:08
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?
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
31 1 1 1 1 2 1000001 1 1 1 1 3 1000001 1 1 1 1 4 100000
Sample Output
247
Source
2010 ACM-ICPC Multi-University Training Contest(8)——Host by ECNU
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;}
- hdu 3509 Buge's Fibonacci Number Problem(二项分布+矩阵连乘)
- hdu 3509 Buge's Fibonacci Number Problem(矩阵乘法+二项式)
- hdu 3509 Buge's Fibonacci Number Problem
- fibonacci扩展+矩阵连乘
- hdu 3306 Another kind of Fibonacci 矩阵连乘
- HDU 3306 Another kind of Fibonacci 矩阵连乘
- hdu 3306 Another kind of Fibonacci(矩阵连乘)
- hdu 1021 Fibonacci Again(矩阵连乘 || 循环节)
- hdu 3117 Fibonacci Numbers(数学推导+矩阵连乘)
- 构造矩阵+矩阵连乘+fibonacci
- hdu 1757 A Simple Math Problem(矩阵连乘)
- POJ3070 Fibonacci (矩阵连乘)
- hdu1588 Fibonacci and 矩阵连乘
- POJ 3070 Fibonacci【矩阵连乘】
- poj 3070 Fibonacci(简单矩阵连乘)
- Not Fibonacci(矩阵连乘)
- HDU 4549 矩阵连乘
- hdu 1757 矩阵连乘
- 如何在网站级别动态更改主题
- C# 切分图片
- 浅谈谈php的设计模式
- java线程系列---队列ArrayBlockingQueue
- vim and python
- hdu 3509 Buge's Fibonacci Number Problem(二项分布+矩阵连乘)
- 在表上创建索引以提高性能
- 使用SubVersion+TortoiseSVN多仓库方式进行版本控制
- Qt引用Windows API方法
- python extension(pywin32) 插入宏到word
- Lucene:基于Java的全文检索引擎简介
- MVVM教程(二):开始MVVM示例
- C# 数字转换成数组
- Do I have a gcc optimization bug or a C code problem?