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

1.使用一个结构体存下矩阵,再写一个二维矩阵乘法函数

2.然后求[1 1 1 0]的n次方?当然不是。

注意:0 ≤ n ≤ 1,000,000,000

如果这样直接乘以n次肯定会超时

可以使用二进制求快速幂

利用二进制求指数幂

举例:

3 ^ 999 = 3 * 3 * 3 * … * 3

直接乘要做998次乘法。但事实上可以这样做,先求出2^k次幂:

3 ^ 2 = 3 * 3

3 ^ 4 = (3 ^ 2) * (3 ^ 2)

3 ^ 8 = (3 ^ 4) * (3 ^ 4)

3 ^ 16 = (3 ^ 8) * (3 ^ 8)

3 ^ 32 = (3 ^ 16) * (3 ^ 16)

3 ^ 64 = (3 ^ 32) * (3 ^ 32)

3 ^ 128 = (3 ^ 64) * (3 ^ 64)

3 ^ 256 = (3 ^ 128) * (3 ^ 128)

3 ^ 512 = (3 ^ 256) * (3 ^ 256)

再相乘:

3 ^ 999

= 3 ^ (512 + 256 + 128 + 64 + 32 + 4 + 2 + 1)

= (3 ^ 512) * (3 ^ 256) * (3 ^ 128) * (3 ^ 64) * (3 ^ 32) * (3 ^ 4) * (3 ^ 2) * 3

把999转为2进制数:1111100111,其个位就是要乘的数。

1   pow ← 1

2   while (n > 0)

3      do if (n mod 2 = 1)

4            then pow ← pow * x

5        x ← x * x

6        n ← n / 2

7      return pow

#include"iostream"
#include"cstdio"
using namespace std; typedef struct
{
int m[][];
}node; node work(node a,node b)
{
node c;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%;
c.m[][]=(a.m[][]*b.m[][]+a.m[][]*b.m[][])%;
return c;
} void caculate(int c)
{
node ans,base;
base.m[][]=base.m[][]=base.m[][]=;
base.m[][]=;
ans.m[][]=ans.m[][]=;
ans.m[][]=ans.m[][]=;
while(c)
{
if(c&) ans=work(ans,base);
base=work(base,base);
c>>=;
}
cout<<ans.m[][]<<endl;
} int main()
{
int n;
while(cin>>n&&n>=)
{
caculate(n);
}
}

最新文章

  1. Error:Execution failed for task &#39;:clean&#39;. &gt; Unable to delete directory :\build\intermediates (转)
  2. SQL Server 中WITH (NOLOCK)浅析
  3. Java内部类详解
  4. bitmap转化base64
  5. POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
  6. Delphi ActiveX Form的使用实例
  7. DOS批量递归删除文件夹
  8. HighCharts官网更新了!(忠实粉的小声音)
  9. 中国行政区域(省,市,县)SQL
  10. 3D正方体
  11. Java EXCEL导入的两种方式JXL和POI
  12. UI2_NSUserDefaults
  13. C++实现数字媒体二维图像变换
  14. centos 6.5 32位 编译安装Mysql
  15. ORACLE数字转换人民币大写
  16. 6个理由告诉你为什么要用NAS
  17. C++一些注意点之异常处理
  18. VueJs学习路线
  19. .net core2 发送电子邮件封装
  20. JS通过decodeURIComponent函数解码

热门文章

  1. codeforces 570 D. Tree Requests (dfs)
  2. DP(两次) UVA 10163 Storage Keepers
  3. IIS7 网站发布
  4. Java对象创建
  5. AJPFX关于this用法和注意事项
  6. 支付宝-API接口解析-转账到银行
  7. 用css制作圆环图表 (vue,sass)
  8. Unity中,保存在OnInspectorGUI中改变的值
  9. mysql 判断null 和 空字符串
  10. 掌握Spark机器学习库-07-随机梯度下降