用矩阵求斐波那契数列,快速幂log(n),只用求最后4位(加和乘的运算中前面的位数无用)

 #include <stdio.h>
#include <stdlib.h> int main()
{
/*
(x y)(x y)= (x*x+y*s x*y+y*t) =(x*x+y*s y*(x+t))
(s t)(s t) (s*x+t*s s*y+t*t) (s*(x+t) t*t+y*s)
(x y)(a b)= (x*a+y*c x*b+y*d)
(s t)(c d) (s*a+t*c s*b+t*d)
(a b)(f1=1)=(a+b)
(c d)(f2=1) (c+d)
*/
//(a,b)(c,d)一开始为E2
long n,x,y,s,t,xx,yy,ss,tt,a,b,c,d,aa,bb,cc,dd;
while (scanf("%ld",&n))
{
if (n==)
{
printf("0\n");
continue;
}
else if (n==-)
break;
n-=;
x=;
y=;
s=;
t=;
a=;
b=;
c=;
d=;
while (n)
{
if (n%==)
{
aa=a;
bb=b;
cc=c;
dd=d;
a=(x*aa+y*cc)%;
b=(x*bb+y*dd)%;
c=(s*aa+t*cc)%;
d=(s*bb+t*dd)%;
}
xx=x;
yy=y;
ss=s;
tt=t;
x=(xx*xx+yy*ss)%;
y=(yy*(xx+tt))%;
s=(ss*(xx+tt))%;
t=(tt*tt+yy*ss)%;
n=n/;
}
printf("%ld\n",(c+d)%);
} return ;
}
 #include <stdio.h>
#include <stdlib.h> int main()
{
/*
(x y)(x y)= (x*x+y*s x*y+y*t) =(x*x+y*s y*(x+t))
(s t)(s t) (s*x+t*s s*y+t*t) (s*(x+t) t*t+y*s)
(x y)(a b)= (x*a+y*c x*b+y*d)
(s t)(c d) (s*a+t*c s*b+t*d)
(a b)(f1=1)=(a+b)
(c d)(f2=1) (c+d)
*/
//(a,b)(c,d)一开始为E2
long n,x[],y[],s[],t[],a[],b[],c[],d[],w,r,i;
x[]=;
y[]=;
s[]=;
t[]=;
a[]=;
b[]=;
c[]=;
d[]=;
for (i=;i<;i++)
{
x[i]=(x[i-]*x[i-]+y[i-]*s[i-])%;
y[i]=(y[i-]*(x[i-]+t[i-]))%;
s[i]=(s[i-]*(x[i-]+t[i-]))%;
t[i]=(t[i-]*t[i-]+y[i-]*s[i-])%;
}
while (scanf("%ld",&n))
{
if (n==)
{
printf("0\n");
continue;
}
else if (n==-)
break;
n-=;
w=;
r=;
while (n)
{
if (n%==)
{
a[r+]=(x[w]*a[r]+y[w]*c[r])%;
b[r+]=(x[w]*b[r]+y[w]*d[r])%;
c[r+]=(s[w]*a[r]+t[w]*c[r])%;
d[r+]=(s[w]*b[r]+t[w]*d[r])%;
r++;
}
w++;
n=n/;
}
printf("%ld\n",(c[r]+d[r])%);
}
return ;
}

最新文章

  1. 《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》
  2. 放弃iOS4,拥抱iOS5
  3. Canvas 画布小案例
  4. mysql存储过程性能监控和分析
  5. codeforces#FF DIV2C题DZY Loves Sequences(DP)
  6. Robot Framework + appium 启动手机浏览器的两个方法(1)
  7. 使用cxf做webservice接口调用
  8. python函数参数中带有默认参数list的坑
  9. angular 1.5.3各种模块使用(一)
  10. C++继承分析
  11. LaTeX 各种命令,符号
  12. HTTP各个status code是什么意思【已解决】
  13. vue-cli中怎么样使用less
  14. Java线程实现与安全
  15. Django rest framework集成微博第三方登录
  16. KVM上如何让虚拟机支持虚拟化(kvm虚拟化的嵌套)
  17. 5D - Rectangles
  18. How To Install and Configure Elasticsearch on Ubuntu 14.04
  19. C# .NET 开发心得
  20. 个人作业代码GitHub提交步骤

热门文章

  1. 时区提示:Local time zone must be set--see zic manual page 2018的解决办法
  2. 1017 B. The Bits
  3. js方法中拼接html时点击事件中拼接字符串参数
  4. 【个人阅读】M1/M2阶段总结
  5. 《Linux内核分析》第七周: 可执行程序的装载
  6. kNN算法学习(一)
  7. Opentsdb 启动显示配置文件不存在
  8. sring引入mybatis
  9. python 生成器、列表解析式、yield、迭代器
  10. 表格属性和BFC(block framing content)