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> ...
随机推荐
- 网络协议UDP
------------------------------互联网协议与协议族------------------------------tcp/ip协议介绍:计算机网络协议,不同类型不同厂家之间的终 ...
- 实现NFS共享wordpress
author:JevonWei 版权声明:原创作品 两台Web服务器,一台mysql服务器,一台NFS服务器,一台DNS服务器 拓扑结构 拓扑环境 web1 192.168.198.110 web2 ...
- 使用XmlWriter创建XML文件
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml ...
- iBatis的一个问题
写了一段查询语句,条件中有一条alarmtype<>'1004'这样的条件,原来是这样写的 <![CATA[ and alarmtype<>'1004']]> 然后 ...
- SpringBoot学习开篇
"Spring Framework已有十余年的历史了,已成为Java应用程序开发框架的事实标准.在如此悠久的历史背景下,有人可能会认为Spring放慢了脚步,躺在了自己的荣誉簿上,再也做不出 ...
- Quartz2.2.x官方教程
零.Quartz是什么?能干什么? Quartz是一个开源的任务调度框架.基于定时.定期的策略来执行任务是它的核心功能,比如x年x月的每个星期五上午8点到9点,每隔10分钟执行1次.Quartz有3个 ...
- 201521044152<java程序设计>第一周学习总结
本周学习总结 java开发时间虽然很短,但是发展迅速,已成为现在非常流行的一门语言,很开心能有幸学习java.第一周学习了java的平台,运行环境jdk以及jrt等等新名词,还了解了eclipse的基 ...
- Java-Preferences用法-入门
Properties提供的应用程序解决方案主要存在两个问题: (1)配置文件不能放在主目录中,因为某些OS(如Win9X)没有主目录的概念: (2)没有标准的文件命名规则,存在文件名冲突的可能性. J ...
- java 程序编写规则(自己总结)
1.命名规范 (1)所有的标示符都只能用ASCⅡ字母(A-Z或a-z).数字(0-9)和下划线"_". (2)类名是一个名词,采用大小写混合的方式,每个单词的首字母大写.例如:Us ...
- Project Euler 92:Square digit chains C++
A number chain is created by continuously adding the square of the digits in a number to form a new ...