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);
}
}

集训第六周 矩阵快速幂 K题的更多相关文章

  1. luoguP3390(矩阵快速幂模板题)

    链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...

  2. hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

    Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...

  3. POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10521   Accepted: 7477 Descri ...

  4. Final Destination II -- 矩阵快速幂模板题

    求f[n]=f[n-1]+f[n-2]+f[n-3] 我们知道 f[n] f[n-1] f[n-2]         f[n-1]  f[n-2]  f[n-3]         1    1    ...

  5. hdu 2604 矩阵快速幂模板题

    /* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f( ...

  6. HDU 1575 矩阵快速幂裸题

    题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...

  7. POJ3070 斐波那契数列递推 矩阵快速幂模板题

    题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...

  8. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  9. CodeForces 450B (矩阵快速幂模板题+负数取模)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...

随机推荐

  1. postgreSQL 创建user表时引发的表名大写与双引号问题

    在postgreSQL里面,user是一个保留字. 如果你想创建user表,你可能会遭遇一些问题! 如图: 可以看到,这里是无法创建user表的. 你可能会说,我只是没有加双引号"" ...

  2. [洛谷2839/国家集训队]middle

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之 ...

  3. Hdu 5358 First One (尺取法+枚举)

    题目链接: Hdu 5358 First One 题目描述: 数组a有n个元素,S[i,j]定义为a[i]+a[i+1]+.....+a[j],问:这个死东西等于多少? 解题思路: 二分肯定超,这个题 ...

  4. 1272 最大距离 只想到了dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 离散化后,用dp[i]表示向右,大于等于i这个数字的最大位置 dp ...

  5. MongoDB操作简记

    一.数据库操作 1.显示当前选择的数据库 [root@weekend05 ~]# mongod --dbpath /data/db/ [root@weekend05 ~]# mongo MongoDB ...

  6. WCF入门大致思路

    WCF服务: 1.IServer.cs(类似接口,WCF接口) 2.Server.svc(实现了WCF接口)右键浏览器运行可以看到WCF服务链接,类似(http://localhost:4609/Us ...

  7. [转]在 Azure 云服务上设计大规模服务的最佳实践

    本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...

  8. c#中stringbuilder的方法总结

    String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新 ...

  9. 写给W小姐的一封信

    生活 琐碎 Hallo,Preaty.对于跟人说话,我很不擅长如何开头.我不知道什么样的开头是符合我在别人心目中我应有的形象.我不知道什么样的开头符合别人预想中与我相匹配的内容.或者说什么的开头才是一 ...

  10. 日常记录-代码中Background后Padding 失效

    近日,在开发过程中 遇到了 Layout 代码中设置 Background 后,padding失效的问题,只是在Android 4.4.4 和 4.4.2 的手机上遇到了. 网上搜索了下,说是 4.4 ...