用矩阵求斐波那契数列,快速幂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. Visual SVN 5.01 Po jie 笔记
  2. cocos2d-x学习
  3. Mysql分库分表方案
  4. Poj1131-Octal Fractions
  5. python 简介
  6. SSH_框架整合6--修改Edit员工信息
  7. [Struts] Hello World Demo
  8. 12 Integer to Roman(int转罗马数字Medium)
  9. java Eclipse debug技巧
  10. Android开发(26)--补间动画(Tween)的实现
  11. java学习笔记----运算符
  12. linux centos6.8 下安装mysql 步骤
  13. Min_25 筛 学习笔记
  14. 不用框架,原生使用python做注册接口/登陆接口/充值接口的测试,做的数据/代码分离
  15. wincc项目移植和复制解决办法
  16. POI2014解题报告
  17. [No0000114]远程桌面剪贴板无法同步本机,无法复制粘贴问题解决
  18. MS Batch AI
  19. 【BZOJ2067】SZN(二分,动态规划,贪心)
  20. Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

热门文章

  1. JSON.NET VS BinaryFormatter 性能
  2. Centos6.9下RabbitMQ集群部署记录
  3. linux系统最小化安装后的初始化脚本
  4. Python进阶量化交易场外篇5——标记A股市场涨跌周期
  5. RSA加密算法深入篇
  6. 安卓开发helloworld
  7. Linux内核分析——进程的描述和进程的创建
  8. pandas的to_csv()使用细节和一些参数
  9. css - 背景图片充满整个屏幕
  10. iOS GCD中级篇 - dispatch_semaphore(信号量)的理解及使用