hdu 3509 Buge's Fibonacci Number Problem(矩阵乘法+二项式)

网友投稿 269 2022-09-16

hdu 3509 Buge's Fibonacci Number Problem(矩阵乘法+二项式)

题目:​​Fibonacci Number Problem

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

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

​​2010 ACM-ICPC Multi-University Training Contest(8)——Host by ECNU​​

分析:

首先我们知道有这样的定理:

接下来,看一个阶数不大的例子,当k=3时:

写成矩阵的形式:

啊,这里出现了麻烦,等号右边矩阵f前面有a,b系数时会让它的展开式变得非常的难看,这样很难看出规律。那么干脆矩阵里就写f不带a,b,于是成为:

嗯,是的,只带上f。右边是s[n-1]没关系。接下来就是编码。矩阵连乘和二项式构造。很快速的敲完,结果交了N次才过,为什么!!??最后发现是取模——"%m",每一步都不能忽略!仔细!!

#include #include #include using namespace std;typedef long long LL;struct matrie{ LL m[60][60];};matrie A,I;void pascal(LL k){ for(LL i=1;i>=1; } return res;}LL quick(LL a,LL p,LL mod){ LL res=1,temp=a%mod; while(p){ if(p&1)res=res*temp%mod; temp=temp*temp%mod; p>>=1; } return res;}void init(){ for(LL i=0;i<60;i++) for(LL j=0;j<60;j++)A.m[i][j]=0;}void show(int k){ for(int i=0;i>t; LL f1,f2,a,b,k,n,m; for(LL i=0;i<60;i++) I.m[i][i]=1; while(t--){ scanf("%lld %lld %lld %lld %lld %lld %lld",&f1,&f2,&a,&b,&k,&n,&m); if(k==0){ printf("%lld\n",n%m); continue; } if(n==1){ printf("%lld\n",quick(f1,k,m)); continue; } if(n==2){ printf("%lld\n",(quick(f1,k,m)+quick(f2,k,m))%m); continue; } init(); A.m[0][0]=A.m[0][k+1]=1; pascal(k); getab(a,b,m,k); A=quick_mod(n-1,k+2,m); //show(k); LL ans=0; ans=(A.m[0][0]%m)*quick(f1,k,m)%m; for(LL i=1;i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:hdu 1250 Hat's Fibonacci(高精度加法)
下一篇:UVA 11419 SAM I AM(最小点覆盖)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~