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> ...
随机推荐
- 关于wamp服务器文件的配置
有的前端朋友想在手机端看PC端开发的html5页面,这时候会在本地PC下载一个wamp,这时候在PC端输入电脑的IP地址或者是直接输入localhost,可以访问www目录下的文件(开发项目必须放置在 ...
- C++ Concept 和Java 接口
C++ Concept 和Java 接口 Concept及接口 我会用Java写个case来解释什么是C++的Concept.Concept可以理解为接口,它是一种广义的接口.不同于Java的Inte ...
- PropertiesUtil demo
package com.spl.save.wmos.base.util; import java.io.File; import java.io.FileInputStream; import jav ...
- diff.js 列表对比算法 源码分析
diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...
- Linux-insmod/rmmod/lsmod驱动模块相关命令(10)
insmod:加载模块 参数: -f 不检查目前kernel版本与模块编译时的kernel版本是否一致,强制将模块载入.-k 将模块设置为自动卸除.-m 输出模块的载入信息.-o <模块名称 ...
- JTemplates 的使用
注意事项:一定要放在Jquery的页面加载完成事件内 : $(function{}); <script src="~/Js/jquery-2.1.0.js">< ...
- 转:【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17225469 在Java中,可以通过配合调用Object对象的wait()方法和no ...
- 201521123108 《Java程序设计》第2周学习总结
1. 本章学习总结 学习了java的知识,虽然还不是太懂,以后一定会取得进步的 2. 书面作业 Q1. 使用Eclipse关联jdk源代码,并查看String对象的源代码(截图)? 答: Q2. 为什 ...
- 201521123013 《Java程序设计》第9周学习总结
1. 本章学习总结 2. 书面作业 Q1.常用异常题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 经常出现Class ...
- Markdown编辑后
一个例子: 例子开始 1. 本章学习总结 今天主要学习了三个知识点 封装 继承 多态 2. 书面作业 Q1. java HelloWorld命令中,HelloWorld这个参数是什么含义? 今天学了一 ...