poj3070矩阵快速幂
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7752 | Accepted: 5501 |
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h> using namespace std;
int N;
struct matrix
{
int a[][];
}origin,res;
matrix multiply(matrix x,matrix y)
{
matrix temp;
memset(temp.a,,sizeof(temp.a));
for(int i=;i<N;i++)
{
for(int j=;j<N;j++)
{
for(int k=;k<N;k++)
{
temp.a[i][j]+=x.a[i][k]*y.a[k][j]%;
temp.a[i][j]%=;
}
}
}
return temp;
}
void calc(int n)
{
memset(res.a,,sizeof(res.a));
origin.a[][]=;origin.a[][]=;
origin.a[][]=;origin.a[][]=;
for(int i=;i<N;i++)
res.a[i][i]=;
while(n)
{
if(n&)
res=multiply(res,origin);
n>>=;
origin=multiply(origin,origin);
}
}
int main()
{
N=;
int n;
while(cin>>n)
{
if(n==-)
break;
if(n)
calc(n-);
if(n)
cout<<res.a[][]<<endl;
else cout<<<<endl;
}
}
poj3070矩阵快速幂的更多相关文章
- poj3070矩阵快速幂求斐波那契数列
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 9368 Desc ...
- POJ3070 矩阵快速幂模板
题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- 矩阵快速幂——POJ3070
矩阵快速幂和普通的快速幂差不多,只不过写起来比较麻烦一点,需要重载*运算符. 模板: struct mat { int m[maxn][maxn]; }unit; mat operator * (ma ...
- poj3070 Fibonacci 矩阵快速幂
学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...
- POJ3070 斐波那契数列 矩阵快速幂
题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...
- 2018.09.25 poj3070 Fibonacci(矩阵快速幂)
传送门 矩阵快速幂板题,写一道来练练手. 这一次在poj做题总算没忘了改万能库. 代码: #include<iostream> #include<cstdio> #define ...
- poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2] f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...
- poj3070 求斐波那契数列第n项 ——矩阵快速幂
题目:http://poj.org/problem?id=3070 用矩阵快速幂加速递推. 代码如下: #include<iostream> #include<cstdio> ...
随机推荐
- 实战案例--TEMPDB暴涨
前言 tempdb暴增,造成磁盘空间不足,甚至影响业务运行. 正文 如图,tempdb log文件从7.40开始突然暴涨,因为 tempdb 0 M到 40G tempdb 所 ...
- UWP 手绘视频创作工具技术分享系列
开篇先来说一下写这篇文章的初衷. 初到来画,通读了来画 UWP App 的代码,发现里面确实有很多比较高深的技术点,同时也是有很多问题的,扩展性,耦合,性能,功能等等.于是我们决定从头重构这个产品,做 ...
- Redis在java中的使用
1.首先安装Redis环境.可以在Windows.linux.别的服务器上搭建Redis环境 2.在java工程中导入必须的jar包 如maven上导入两个jar] <!-- Redis必须包 ...
- Java 强引用 软引用 弱引用 虚引用详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt393 众所周知,java中是JVM负责内存的分配和回收,这是它的优点(使用方 ...
- 基于CAS的SSO(单点登录)实例
第一步 部署CAS-Server(服务端) 1.从CAS官方网站(http://developer.jasig.org/cas/)下载最新版本的CAS-Server(当前最新版本cas-server- ...
- C语言中指针*p[N], (*P)[N],及**p的区别
在C语言编程中指针经常困扰着我们,但是若能灵活运用指针的话,将会使得我们编程变得更加轻松与高效.这里讲下*p[N], (*P)[N],及**p的区别,这也是之前经常困扰我的地方. 这三者的定义分别为: ...
- 【集美大学1411_助教博客】个人作业2——英语学习APP案例分析 成绩
个人作业2--英语学习APP案例分析,截止发稿时间全班31人,提交31,未提交0人.有一名同学已经写了作业但忘记提交了,这次给分了,但下不为例.由于助教这周有点忙,所以点评得非常不及时,请同学们见谅. ...
- 201521123010 《Java程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 ①ArrayList代码分析 1.1 解释ArrayList的contains源代码 ...
- python3中的一些小改动
Python 3.3中使用print是必须要括号因为在python3以上的版本中print不再是一条命令而是一个函数了.
- 201521123036 《Java程序设计》第9周学习总结
本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 书面作业 本次PTA作业题集异常 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中 ...