Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

Output

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875 矩阵快速幂入门题,构造矩阵[fn,fn-1]*[1,1]在自己敲一遍模板就行了。
             [0 ,0 ] [1,0]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long int LL;
using namespace std; int n,mod=; struct Matrix
{
int a[][];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
res.a[i][j]+=(a[i][k]*p.a[k][j]%mod);
}
res.a[i][j]%=mod;
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,int k)
{
Matrix res;
for(int i=;i<;i++)
{
res.a[i][i]=;
}
while(k)
{
if(k&) res=res*base;
base=base*base;
k>>=;
}
return res;
} void init_Matrix()
{
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
base.a[][]=;
} int main()
{
while(scanf("%d",&n)&&n!=-)
{
init_Matrix();
if(n==) printf("0\n");
else if(n==) printf("1\n");
else
{
ans=ans*quick_pow(base,n-);
printf("%d\n",ans.a[][]);
}
}
return ;
}

最新文章

  1. Tcpdump命令行 与 GUI Wireshark
  2. Boost 1.61.0 Library Documentation
  3. RHEL6.4 postfix+dovecot搭建邮件服务器
  4. JS总结之二:DOM对象控制HTML
  5. Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别
  6. 后端分布式系列:分布式存储-HDFS NameNode 设计实现解析
  7. 寒假作业pta1
  8. Go语言环境安装&amp;搭建(Win)
  9. Gpload安装手册(Linux版本)
  10. Linux - 快速进入目录的方法
  11. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-7底层驱动之滴嗒定时器
  12. DevExpress使用方法GridControl总结
  13. 反向路径过滤——reverse path filter
  14. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造
  15. 将 nginx 安装成 windows 的方法
  16. datetime 模块
  17. java几个经典的算法题目----------二维矩阵算法
  18. php中var_dump()函数
  19. Redis常用类型数据操作
  20. stm8 全局变量定义 声明

热门文章

  1. adv钓鱼题
  2. js获取当前时间戳 不需毫秒数
  3. 有关windows系统的EXE和DLL文件说法错误
  4. Push:iOS基于APNS的消息推送
  5. MYSQL相关完整笔记
  6. Python开发程序:生产环境下实时统计网站访问日志信息
  7. isKindOfClass和isMemberOfClass的区别
  8. IE中cookie问题,带下划线的前置域名会不给设cookie,谷歌和火狐浏览器则不受影响
  9. 使用DiskFileItemFactory 实现文件上传 ,设定缓冲区大小和存放临时文件目录。
  10. python爬虫感想