集训第六周 矩阵快速幂 K题
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题的更多相关文章
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)
Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有 ...
- POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10521 Accepted: 7477 Descri ...
- 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 ...
- hdu 2604 矩阵快速幂模板题
/* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f( ...
- HDU 1575 矩阵快速幂裸题
题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...
随机推荐
- (矩阵快速幂)51NOD 1242斐波那契数列的第N项
斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, ...
- iOS 上传APP到AppStore 卡在 Authenticating with the iTunes store 提示
上传APP的时候,遇到了问题,一直卡在Authenticating with the iTunes store提示这里, 解决办法:在Application Loader里面登录需要上传APP的开发者 ...
- 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- VS2010环境下.NET4.0中Tuple<T>的一个小BUG问题
启动一个桌面程序后,发现一个窗体cfdata=null, 执行时发生错误, 但是在初始化的时候,我明明是cfdata=new Cfdata();为什么会出现这个错误呢. 我开始跟踪,发现当执行cfda ...
- glassfish的启动
一.Glassfish v3 的安装 1. chmod 775/+x glassfish-v3.zip 改权限 2.unzip glassfish-v3.zip 3../asadmin star ...
- 转】MongoDB主从复制实验 master/slave
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! Posted: May 31, 2013 Ta ...
- SP CAML工具
直接一直使用CAML做一些简单的SP列表查询,突然想对CAML进一步了解,于是找到两个常用工具,做以记录: 1 Caml Query Builder : 用于编写CAML查询,对初学者可以了解查询语句 ...
- RabbitMQ四:生产者--队列--消费者
AMQP协议的梳理和名词解析 建议先把上篇AMQP协议先看一遍,理解一下,由于用XMind绘图,电脑屏幕比较小,不能截取全部,如果想要全图和源代码,请下面留言....... 可以点击图片,打开到新的 ...
- Android Studio中找出不再使用的资源
顶部Analyze菜单中选择Run Inspection by Name 在弹出的输入框中输入unused resources
- 浅谈CSS中的定位知识
1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...